ThunderEgg  1.0.0
BufferReader.h
Go to the documentation of this file.
1 /***************************************************************************
2  * ThunderEgg, a library for solvers on adaptively refined block-structured
3  * Cartesian grids.
4  *
5  * Copyright (c) 2018-2021 Scott Aiton
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  ***************************************************************************/
20 
21 #ifndef THUNDEREGG_BUFFERREADER_H
22 #define THUNDEREGG_BUFFERREADER_H
23 
30 #include <cstddef>
31 #include <iostream>
32 #include <type_traits>
33 
34 namespace ThunderEgg {
39 {
40 private:
44  char* buffer = nullptr;
48  int pos = 0;
56  template<typename T>
57  static constexpr bool isSerializable()
58  {
59  return std::is_base_of<Serializable, T>::value;
60  }
61 
62 public:
68  BufferReader(char* buffer) { this->buffer = buffer; }
74  int getPos() { return pos; }
83  {
84  pos += obj.deserialize(buffer + pos);
85  return *this;
86  }
95  template<typename T>
96  typename std::enable_if<!isSerializable<T>(), BufferReader>::type& operator>>(T& obj)
97  {
98  for (size_t i = 0; i < sizeof(T); i++) {
99  reinterpret_cast<char*>(&obj)[i] = buffer[pos];
100  pos++;
101  }
102  return *this;
103  }
104 };
105 } // namespace ThunderEgg
106 #endif
ThunderEgg::Serializable
Interface for serializing objects.
Definition: Serializable.h:34
ThunderEgg::BufferReader::operator>>
std::enable_if<!isSerializable< T >), BufferReader >::type & operator>>(T &obj)
Get an object from the buffer.
Definition: BufferReader.h:96
ThunderEgg
The ThunderEgg namespace.
Definition: BiLinearGhostFiller.h:31
Serializable.h
Serializable class.
ThunderEgg::BufferReader::BufferReader
BufferReader(char *buffer)
Create a new BufferReader with given buffer.
Definition: BufferReader.h:68
ThunderEgg::BufferReader::getPos
int getPos()
get the current position in the buffer
Definition: BufferReader.h:74
ThunderEgg::BufferReader::operator>>
BufferReader & operator>>(Serializable &obj)
Get an object of the buffer.
Definition: BufferReader.h:82
ThunderEgg::Serializable::deserialize
virtual int deserialize(char *buffer)=0
Deserialize an object.
ThunderEgg::BufferReader
Class that is used to help read serialized objects from a buffer.
Definition: BufferReader.h:38