Gamedev Framework (gf)  0.8.0
A C++14 framework for 2D games
DataObject.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_DATA_OBJECT_H
22 #define GF_DATA_OBJECT_H
23 
24 #include <cstdint>
25 
26 #include "Portability.h"
27 
28 namespace gf {
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 inline namespace v1 {
31 #endif
32 
39  enum class DataType {
40  Nil,
41  Boolean,
42  Signed,
43  Unsigned,
44  Float,
45  Double,
46  String,
47  Binary,
48  Array,
49  Map,
50  Extension,
51  };
52 
53  struct DataObject;
54 
61  struct GF_API DataString {
62  uint32_t size;
63  const char *data;
64  };
65 
74  struct GF_API DataBinary {
75  uint32_t size;
76  const uint8_t *data;
77  };
78 
87  struct GF_API DataArray {
88  uint32_t size;
90 
98  DataObject *begin() noexcept;
99 
107  DataObject *end() noexcept;
108 
116  const DataObject *begin() const noexcept;
117 
125  const DataObject *end() const noexcept;
126  };
127 
128  struct GF_API DataKeyValue;
129 
138  struct GF_API DataMap {
139  uint32_t size;
140  DataKeyValue *data;
141 
148  DataKeyValue *begin() noexcept;
149 
157  DataKeyValue *end() noexcept;
158 
165  const DataKeyValue *begin() const noexcept;
166 
174  const DataKeyValue *end() const noexcept;
175  };
176 
185  struct GF_API DataExtension {
186  uint32_t size;
187  int8_t type;
188  const uint8_t *data;
189  };
190 
200  struct GF_API DataObject {
202 
203  union {
204  bool boolean;
205  uint64_t u64;
206  int64_t i64;
207  float f32;
208  double f64;
212  DataMap map;
214  };
215 
222  : type(DataType::Nil)
223  {
224  }
225 
229  explicit DataObject(std::nullptr_t)
230  : type(DataType::Nil)
231  {
232  }
233 
237  explicit DataObject(bool data)
238  : type(DataType::Boolean)
239  , boolean(data)
240  {
241 
242  }
243 
247  explicit DataObject(uint8_t data)
248  : type(DataType::Unsigned)
249  , u64(data)
250  {
251 
252  }
253 
257  explicit DataObject(int8_t data)
258  : type(DataType::Signed)
259  , i64(data)
260  {
261 
262  }
263 
267  explicit DataObject(uint16_t data)
268  : type(DataType::Unsigned)
269  , u64(data)
270  {
271 
272  }
273 
277  explicit DataObject(int16_t data)
278  : type(DataType::Signed)
279  , i64(data)
280  {
281 
282  }
283 
287  explicit DataObject(uint32_t data)
288  : type(DataType::Unsigned)
289  , u64(data)
290  {
291 
292  }
293 
297  explicit DataObject(int32_t data)
298  : type(DataType::Signed)
299  , i64(data)
300  {
301 
302  }
303 
307  explicit DataObject(uint64_t data)
308  : type(DataType::Unsigned)
309  , u64(data)
310  {
311 
312  }
313 
317  explicit DataObject(int64_t data)
318  : type(DataType::Signed)
319  , i64(data)
320  {
321 
322  }
323 
327  explicit DataObject(float data)
328  : type(DataType::Float)
329  , f32(data)
330  {
331 
332  }
333 
337  explicit DataObject(double data)
338  : type(DataType::Double)
339  , f64(data)
340  {
341 
342  }
343 
347  DataObject(const DataObject&) = delete;
348 
352  DataObject& operator=(const DataObject&) = delete;
353 
357  DataObject(DataObject&& other);
358 
362  DataObject& operator=(DataObject&& other);
363 
367  ~DataObject();
368 
374  void clear();
375 
376  private:
377  void move(DataObject&& other);
378  };
379 
386  struct DataKeyValue {
389  };
390 
391 #ifndef DOXYGEN_SHOULD_SKIP_THIS
392 }
393 #endif
394 }
395 
396 #endif // GF_DATA_OBJECT_H
DataObject value
The value of the pair.
Definition: DataObject.h:388
An extension object.
Definition: DataObject.h:185
An array object.
Definition: DataObject.h:87
DataType
Generic data type for data objects.
Definition: DataObject.h:39
DataObject()
Default constructor.
Definition: DataObject.h:221
uint32_t size
The size of the map.
Definition: DataObject.h:139
int64_t i64
A signed integer object.
Definition: DataObject.h:206
DataObject(int16_t data)
Constructor for a signed object.
Definition: DataObject.h:277
A double precision float object.
DataObject(int32_t data)
Constructor for a signed object.
Definition: DataObject.h:297
DataObject(std::nullptr_t)
Constructor for a null object.
Definition: DataObject.h:229
bool boolean
A boolean object.
Definition: DataObject.h:204
uint32_t size
The size of the extension.
Definition: DataObject.h:186
A map object.
DataBinary binary
A binary object.
Definition: DataObject.h:210
DataObject(uint32_t data)
Constructor for an unsigned object.
Definition: DataObject.h:287
DataObject(double data)
Constructor for a double precision float object.
Definition: DataObject.h:337
DataObject(bool data)
Constructor for a boolean object.
Definition: DataObject.h:237
A binary object.
Definition: DataObject.h:74
DataObject(uint64_t data)
Constructor for an unsigned object.
Definition: DataObject.h:307
float f32
A single precision float object.
Definition: DataObject.h:207
A single precision float object.
A signed integer object.
An extension object.
DataKeyValue * data
The data of the map.
Definition: DataObject.h:140
A key-value pair of data objects.
Definition: DataObject.h:386
double f64
A double precision float object.
Definition: DataObject.h:208
const char * data
The data of the string.
Definition: DataObject.h:63
uint32_t size
The size of the array.
Definition: DataObject.h:88
An unsigned integer object.
const uint8_t * data
The data of the binary.
Definition: DataObject.h:76
uint32_t size
The size of the string.
Definition: DataObject.h:62
DataObject(uint8_t data)
Constructor for an unsigned object.
Definition: DataObject.h:247
The namespace for gf classes.
Definition: Action.h:34
uint32_t size
The size of the binary.
Definition: DataObject.h:75
An array object.
A string object.
uint64_t u64
An unsigned integer object.
Definition: DataObject.h:205
DataObject key
The key of the pair.
Definition: DataObject.h:387
DataMap map
A map object.
Definition: DataObject.h:212
DataObject(int8_t data)
Constructor for a signed object.
Definition: DataObject.h:257
A null object.
DataArray array
An array object.
Definition: DataObject.h:211
A map object.
Definition: DataObject.h:138
DataString string
A string object.
Definition: DataObject.h:209
DataObject(int64_t data)
Constructor for a signed object.
Definition: DataObject.h:317
A data object.
Definition: DataObject.h:200
DataType type
The type of the object.
Definition: DataObject.h:201
int8_t type
The type of the extension.
Definition: DataObject.h:187
A binary object.
DataExtension extension
An extension object.
Definition: DataObject.h:213
A string object.
Definition: DataObject.h:61
A boolean object.
DataObject(float data)
Constructor for a single precision float object.
Definition: DataObject.h:327
DataObject(uint16_t data)
Constructor for an unsigned object.
Definition: DataObject.h:267
DataObject * data
The data of the array.
Definition: DataObject.h:89
const uint8_t * data
The data of the extension.
Definition: DataObject.h:188