Gamedev Framework (gf)  0.9.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 "BufferRef.h"
29 #include "Portability.h"
30 #include "StringRef.h"
31 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36 
45  class GF_API Serializer {
46  public:
52  Serializer(const Path& filename, uint16_t version = 0);
53 
59  operator bool() const {
60  return static_cast<bool>(m_file);
61  }
62 
66  uint16_t getVersion() const {
67  return m_version;
68  }
69 
73  void writeBoolean(bool data);
74 
78  void writeChar(char data);
79 
83  void writeSigned8(int8_t data);
84 
88  void writeSigned16(int16_t data);
89 
93  void writeSigned32(int32_t data);
94 
98  void writeSigned64(int64_t data);
99 
103  void writeUnsigned8(uint8_t data);
104 
108  void writeUnsigned16(uint16_t data);
109 
113  void writeUnsigned32(uint32_t data);
114 
118  void writeUnsigned64(uint64_t data);
119 
123  void writeFloat(float data);
124 
128  void writeDouble(double data);
129 
133  void writeString(const char *data, std::size_t size);
134 
138  void writeSizeHeader(std::size_t size);
139 
140  private:
141  void writeBigEndian64(uint64_t data);
142  void writeBigEndian32(uint32_t data);
143  void writeBigEndian16(uint16_t data);
144  void writeBigEndian8(uint8_t data);
145 
146  private:
147  BinaryFile m_file;
148  uint16_t m_version;
149  };
150 
159  class GF_API Deserializer {
160  public:
166  Deserializer(const Path& filename);
167 
173  operator bool() const {
174  return static_cast<bool>(m_file);
175  }
176 
180  uint16_t getVersion() const {
181  return m_version;
182  }
183 
189  bool readBoolean(bool& data);
190 
196  bool readChar(char& data);
197 
203  bool readSigned8(int8_t& data);
204 
210  bool readSigned16(int16_t& data);
211 
217  bool readSigned32(int32_t& data);
218 
224  bool readSigned64(int64_t& data);
225 
231  bool readUnsigned8(uint8_t& data);
232 
238  bool readUnsigned16(uint16_t& data);
239 
245  bool readUnsigned32(uint32_t& data);
246 
252  bool readUnsigned64(uint64_t& data);
253 
259  bool readFloat(float& data);
260 
266  bool readDouble(double& data);
267 
273  bool readString(char *data, std::size_t size);
274 
280  bool readSizeHeader(std::size_t& size);
281 
282  private:
283  bool readBigEndian64(uint64_t& data);
284  bool readBigEndian32(uint32_t& data);
285  bool readBigEndian16(uint16_t& data);
286  bool readBigEndian8(uint8_t& data);
287 
288  bool isEof() const;
289 
290  private:
291  BinaryFile m_file;
292  uint16_t m_version;
293  bool m_eof;
294  };
295 
296 
297 #ifndef DOXYGEN_SHOULD_SKIP_THIS
298 }
299 #endif
300 }
301 
302 #endif // GF_SERIALIZATION_H
A deserializer from a binary file.
Definition: Serialization.h:159
uint16_t getVersion() const
Get the version of the current archive format.
Definition: Serialization.h:180
uint16_t getVersion() const
Get the version of the current archive format.
Definition: Serialization.h:66
A serializer to a binary file.
Definition: Serialization.h:45
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