Gamedev Framework (gf)  0.10.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 "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 
53  Serializer(OutputStream& stream, uint16_t version = 0);
54 
58  uint16_t getVersion() const {
59  return m_version;
60  }
61 
65  void writeBoolean(bool data);
66 
70  void writeChar(char data);
71 
75  void writeSigned8(int8_t data);
76 
80  void writeSigned16(int16_t data);
81 
85  void writeSigned32(int32_t data);
86 
90  void writeSigned64(int64_t data);
91 
95  void writeUnsigned8(uint8_t data);
96 
100  void writeUnsigned16(uint16_t data);
101 
105  void writeUnsigned32(uint32_t data);
106 
110  void writeUnsigned64(uint64_t data);
111 
115  void writeFloat(float data);
116 
120  void writeDouble(double data);
121 
125  void writeString(const char *data, std::size_t size);
126 
130  void writeSizeHeader(std::size_t size);
131 
132  private:
133  void writeBigEndian64(uint64_t data);
134  void writeBigEndian32(uint32_t data);
135  void writeBigEndian16(uint16_t data);
136  void writeBigEndian8(uint8_t data);
137 
138  private:
139  OutputStream *m_stream;
140  uint16_t m_version;
141  };
142 
143 
152  class GF_API Deserializer {
153  public:
159  Deserializer(InputStream& stream);
160 
161 // /**
162 // * @brief Conversion to boolean
163 // *
164 // * @returns True if the archive exists and was opened
165 // */
166 // operator bool() const {
167 // return static_cast<bool>(m_file);
168 // }
169 
173  uint16_t getVersion() const {
174  return m_version;
175  }
176 
182  bool readBoolean(bool& data);
183 
189  bool readChar(char& data);
190 
196  bool readSigned8(int8_t& data);
197 
203  bool readSigned16(int16_t& data);
204 
210  bool readSigned32(int32_t& data);
211 
217  bool readSigned64(int64_t& data);
218 
224  bool readUnsigned8(uint8_t& data);
225 
231  bool readUnsigned16(uint16_t& data);
232 
238  bool readUnsigned32(uint32_t& data);
239 
245  bool readUnsigned64(uint64_t& data);
246 
252  bool readFloat(float& data);
253 
259  bool readDouble(double& data);
260 
266  bool readString(char *data, std::size_t size);
267 
273  bool readSizeHeader(std::size_t& size);
274 
275  private:
276  bool readBigEndian64(uint64_t& data);
277  bool readBigEndian32(uint32_t& data);
278  bool readBigEndian16(uint16_t& data);
279  bool readBigEndian8(uint8_t& data);
280 
281  bool isEof() const;
282 
283  private:
284  InputStream *m_stream;
285  uint16_t m_version;
286  };
287 
288 
289 #ifndef DOXYGEN_SHOULD_SKIP_THIS
290 }
291 #endif
292 }
293 
294 #endif // GF_SERIALIZATION_H
A deserializer from a binary file.
Definition: Serialization.h:152
uint16_t getVersion() const
Conversion to boolean.
Definition: Serialization.h:173
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:58
A serializer to a binary file.
Definition: Serialization.h:45
The namespace for gf classes.
Definition: Action.h:34
Abstract class for custom file input streams.
Definition: Stream.h:54