Gamedev Framework (gf)  0.8.0
A C++14 framework for 2D games
Serialization.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 "BinaryFile.h"
28 #include "Portability.h"
29 
30 namespace gf {
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 inline namespace v1 {
33 #endif
34 
35  struct DataObject;
36 
47  class GF_API Serializer {
48  public:
54  Serializer(const Path& filename);
55 
59  void writeNil();
60 
64  void writeBoolean(bool data);
65 
69  void writeSigned(int64_t data);
70 
74  void writeUnsigned(uint64_t data);
75 
79  void writeFloat(float data);
80 
84  void writeDouble(double data);
85 
89  void writeString(const char *data, uint32_t size);
90 
94  void writeBinary(const uint8_t *data, uint32_t size);
95 
99  void writeArrayHeader(uint32_t size);
100 
104  void writeMapHeader(uint32_t size);
105 
109  void writeExtension(int8_t type, const uint8_t *data, uint32_t size);
110 
111  private:
112  void writeBigEndian64(uint64_t data);
113  void writeBigEndian32(uint32_t data);
114  void writeBigEndian16(uint16_t data);
115 
116  private:
117  BinaryFile m_file;
118  };
119 
130  class GF_API Deserializer {
131  public:
137  Deserializer(const Path& filename);
138 
144  bool readNil();
145 
151  bool readBoolean(bool& data);
152 
158  bool readSigned(int64_t& data);
159 
165  bool readUnsigned(uint64_t& data);
166 
172  bool readFloat(float& data);
173 
179  bool readDouble(double& data);
180 
186  bool readStringHeader(uint32_t& size);
187 
193  bool readString(char *data, uint32_t size);
194 
200  bool readBinaryHeader(uint32_t& size);
201 
207  bool readBinary(uint8_t *data, uint32_t size);
208 
214  bool readArrayHeader(uint32_t& size);
215 
221  bool readMapHeader(uint32_t& size);
222 
228  bool readExtensionHeader(int8_t& type, uint32_t& size);
229 
235  bool readExtension(uint8_t *data, uint32_t size);
236 
237 
243  bool readDataObject(DataObject& object);
244 
245  private:
246  bool readRawInt8(uint64_t& raw);
247  bool readRawInt16(uint64_t& raw);
248  bool readRawInt32(uint64_t& raw);
249  bool readRawInt64(uint64_t& raw);
250 
251  bool readSize8(uint32_t& size);
252  bool readSize16(uint32_t& size);
253  bool readSize32(uint32_t& size);
254 
255  bool readDataObjectMap(DataObject& object);
256  bool readDataObjectArray(DataObject& object);
257  bool readDataObjectString(DataObject& object);
258  bool readDataObjectBinary(DataObject& object);
259  bool readDataObjectExtension(DataObject& object);
260 
261  private:
262  bool readBigEndian64(uint64_t& data);
263  bool readBigEndian32(uint32_t& data);
264  bool readBigEndian16(uint16_t& data);
265 
266  bool isEof() const;
267  uint8_t getByte() const;
268  void nextByte();
269 
270  private:
271  BinaryFile m_file;
272  uint8_t m_next;
273  bool m_eof;
274  };
275 
276 
277 #ifndef DOXYGEN_SHOULD_SKIP_THIS
278 }
279 #endif
280 }
281 
282 #endif // GF_SERIALIZATION_H
A deserializer from a binary file.
Definition: Serialization.h:130
A serializer to a binary file.
Definition: Serialization.h:47
The namespace for gf classes.
Definition: Action.h:34
A binary file that can be read or written.
Definition: BinaryFile.h:41
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:41
A data object.
Definition: DataObject.h:200