Gamedev Framework (gf)  0.12.0
A C++14 framework for 2D games
Serialization.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 "BufferRef.h"
28 #include "Portability.h"
29 #include "Stream.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:
47 
54  Serializer(OutputStream& stream, uint16_t version = 0);
55 
59  uint16_t getVersion() const {
60  return m_version;
61  }
62 
66  void writeBoolean(bool data);
67 
71  void writeChar(char data);
72 
76  void writeSigned8(int8_t data);
77 
81  void writeSigned16(int16_t data);
82 
86  void writeSigned32(int32_t data);
87 
91  void writeSigned64(int64_t data);
92 
96  void writeUnsigned8(uint8_t data);
97 
101  void writeUnsigned16(uint16_t data);
102 
106  void writeUnsigned32(uint32_t data);
107 
111  void writeUnsigned64(uint64_t data);
112 
116  void writeFloat(float data);
117 
121  void writeDouble(double data);
122 
126  void writeString(const char *data, std::size_t size);
127 
131  void writeSizeHeader(std::size_t size);
132 
133  private:
134  void writeBigEndian64(uint64_t data);
135  void writeBigEndian32(uint32_t data);
136  void writeBigEndian16(uint16_t data);
137  void writeBigEndian8(uint8_t data);
138 
139  private:
140  OutputStream *m_stream;
141  uint16_t m_version;
142  };
143 
144 
153  class GF_API Deserializer {
154  public:
160  Deserializer(InputStream& stream);
161 
165  uint16_t getVersion() const {
166  return m_version;
167  }
168 
174  bool readBoolean(bool& data);
175 
181  bool readChar(char& data);
182 
188  bool readSigned8(int8_t& data);
189 
195  bool readSigned16(int16_t& data);
196 
202  bool readSigned32(int32_t& data);
203 
209  bool readSigned64(int64_t& data);
210 
216  bool readUnsigned8(uint8_t& data);
217 
223  bool readUnsigned16(uint16_t& data);
224 
230  bool readUnsigned32(uint32_t& data);
231 
237  bool readUnsigned64(uint64_t& data);
238 
244  bool readFloat(float& data);
245 
251  bool readDouble(double& data);
252 
258  bool readString(char *data, std::size_t size);
259 
265  bool readSizeHeader(std::size_t& size);
266 
267  private:
268  bool readBigEndian64(uint64_t& data);
269  bool readBigEndian32(uint32_t& data);
270  bool readBigEndian16(uint16_t& data);
271  bool readBigEndian8(uint8_t& data);
272 
273  bool isEof() const;
274 
275  private:
276  InputStream *m_stream;
277  uint16_t m_version;
278  };
279 
280 
281 #ifndef DOXYGEN_SHOULD_SKIP_THIS
282 }
283 #endif
284 }
285 
286 #endif // GF_SERIALIZATION_H
A deserializer from a binary file.
Definition: Serialization.h:153
uint16_t getVersion() const
Get the version of the current archive format.
Definition: Serialization.h:165
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:59
A serializer to a binary file.
Definition: Serialization.h:45
The namespace for gf classes.
Definition: Action.h:35
Abstract class for custom file input streams.
Definition: Stream.h:54