ThunderEgg  1.0.0
BufferWriter.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_BUFFERWRITER_H
22 #define THUNDEREGG_BUFFERWRITER_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;
49 
57  template<typename T>
58  static constexpr bool isSerializable()
59  {
60  return std::is_base_of<Serializable, T>::value;
61  }
62 
63 public:
68  BufferWriter() = default;
74  BufferWriter(char* buffer) { this->buffer = buffer; }
80  int getPos() { return pos; }
89  {
90  pos += obj.serialize(buffer == nullptr ? nullptr : (buffer + pos));
91  return *this;
92  }
101  template<typename T>
102  typename std::enable_if<!isSerializable<T>(), BufferWriter>::type& operator<<(const T& obj)
103  {
104  if (buffer != nullptr) {
105  for (size_t i = 0; i < sizeof(T); i++) {
106  buffer[pos] = reinterpret_cast<const char*>(&obj)[i];
107  pos++;
108  }
109  } else {
110  pos += sizeof(T);
111  }
112  return *this;
113  }
114 };
115 } // namespace ThunderEgg
116 #endif
ThunderEgg::Serializable::serialize
virtual int serialize(char *buffer) const =0
Serialize object into buffer.
ThunderEgg::BufferWriter::BufferWriter
BufferWriter(char *buffer)
Create a new BufferWriter with given buffer.
Definition: BufferWriter.h:74
ThunderEgg::BufferWriter
Class that is used to help serialize objects into a buffer.
Definition: BufferWriter.h:38
ThunderEgg::Serializable
Interface for serializing objects.
Definition: Serializable.h:34
ThunderEgg
The ThunderEgg namespace.
Definition: BiLinearGhostFiller.h:31
Serializable.h
Serializable class.
ThunderEgg::BufferWriter::operator<<
std::enable_if<!isSerializable< T >), BufferWriter >::type & operator<<(const T &obj)
Add an object to the buffer.
Definition: BufferWriter.h:102
ThunderEgg::BufferWriter::operator<<
BufferWriter & operator<<(const Serializable &obj)
Add object to the buffer.
Definition: BufferWriter.h:88
ThunderEgg::BufferWriter::getPos
int getPos()
get the current position in the buffer
Definition: BufferWriter.h:80
ThunderEgg::BufferWriter::BufferWriter
BufferWriter()=default
Create a new BufferWriter with the buffer set to nullptr. This is helpful for determining the size ne...