Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
Serialization.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_SERIALIZATION_H
22 #define GF_SERIALIZATION_H
23 
24 #include <cstddef>
25 #include <cstdint>
26 
27 #include "CoreApi.h"
28 #include "Stream.h"
29 
30 namespace gf {
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 inline namespace v1 {
33 #endif
34 
43  class GF_CORE_API Serializer {
44  public:
45 
52  Serializer(OutputStream& stream, uint16_t version = 0);
53 
57  uint16_t getVersion() const {
58  return m_version;
59  }
60 
64  void writeBoolean(bool data);
65 
69  void writeChar(char data);
70 
74  void writeSigned8(int8_t data);
75 
79  void writeSigned16(int16_t data);
80 
84  void writeSigned32(int32_t data);
85 
89  void writeSigned64(int64_t data);
90 
94  void writeUnsigned8(uint8_t data);
95 
99  void writeUnsigned16(uint16_t data);
100 
104  void writeUnsigned32(uint32_t data);
105 
109  void writeUnsigned64(uint64_t data);
110 
114  void writeFloat(float data);
115 
119  void writeDouble(double data);
120 
124  void writeString(const char *data, std::size_t size);
125 
129  void writeSizeHeader(std::size_t size);
130 
131  private:
132  void writeBigEndian64(uint64_t data);
133  void writeBigEndian32(uint32_t data);
134  void writeBigEndian16(uint16_t data);
135  void writeBigEndian8(uint8_t data);
136 
137  private:
138  OutputStream *m_stream;
139  uint16_t m_version;
140  };
141 
142 
151  class GF_CORE_API Deserializer {
152  public:
158  Deserializer(InputStream& stream);
159 
163  uint16_t getVersion() const {
164  return m_version;
165  }
166 
172  bool readBoolean(bool& data);
173 
179  bool readChar(char& data);
180 
186  bool readSigned8(int8_t& data);
187 
193  bool readSigned16(int16_t& data);
194 
200  bool readSigned32(int32_t& data);
201 
207  bool readSigned64(int64_t& data);
208 
214  bool readUnsigned8(uint8_t& data);
215 
221  bool readUnsigned16(uint16_t& data);
222 
228  bool readUnsigned32(uint32_t& data);
229 
235  bool readUnsigned64(uint64_t& data);
236 
242  bool readFloat(float& data);
243 
249  bool readDouble(double& data);
250 
256  bool readString(char *data, std::size_t size);
257 
263  bool readSizeHeader(std::size_t& size);
264 
265  private:
266  bool readBigEndian64(uint64_t& data);
267  bool readBigEndian32(uint32_t& data);
268  bool readBigEndian16(uint16_t& data);
269  bool readBigEndian8(uint8_t& data);
270 
271  bool isEof() const;
272 
273  private:
274  InputStream *m_stream;
275  uint16_t m_version;
276  };
277 
278 
279 #ifndef DOXYGEN_SHOULD_SKIP_THIS
280 }
281 #endif
282 }
283 
284 #endif // GF_SERIALIZATION_H
A deserializer from a binary file.
Definition: Serialization.h:151
uint16_t getVersion() const
Get the version of the current archive format.
Definition: Serialization.h:163
Abstract class for custom file output streams.
Definition: Stream.h:135
uint16_t getVersion() const
Get the version of the current archive format.
Definition: Serialization.h:57
A serializer to a binary file.
Definition: Serialization.h:43
The namespace for gf classes.
Definition: Action.h:35
Abstract class for custom file input streams.
Definition: Stream.h:54