Thrive
serialization.h
1 #pragma once
2 
3 #include "scripting/luabind.h"
4 
5 #include <cstdint>
6 #include <OgreColourValue.h>
7 #include <OgreMath.h>
8 #include <OgrePlane.h>
9 #include <OgreQuaternion.h>
10 #include <OgreVector3.h>
11 #include <string>
12 #include <map>
13 #include <vector>
14 
15 static_assert(
16  CHAR_BIT == 8,
17  "char must be 8 bit long for properly portable serialization."
18 );
19 
20 
21 namespace Ogre {
22  class ColourValue;
23  class Degree;
24  class Plane;
25  class Quaternion;
26  class Radian;
27  class Vector3;
28 }
29 
30 namespace thrive {
31 
32 
37 
38 public:
39 
48  static luabind::scope
49  luaBindings();
50 
55 
62  const StorageContainer& other
63  );
64 
71  StorageContainer&& other
72  );
73 
78 
87  const StorageContainer& other
88  );
89 
98  StorageContainer&& other
99  );
100 
109  bool
110  contains(
111  const std::string& key
112  ) const;
113 
126  template<typename T>
127  bool
128  contains(
129  const std::string& key
130  ) const;
131 
147  template<typename T>
148  T
149  get(
150  const std::string& key,
151  const T& defaultValue = T()
152  ) const;
153 
158  std::list<std::string>
159  keys() const;
160 
169  luabind::object
170  luaGet(
171  const std::string& key,
172  luabind::object defaultValue
173  ) const;
174 
187  template<typename T>
188  void
189  set(
190  const std::string& key,
191  T value
192  );
193 
194  friend std::ostream&
195  operator << (
196  std::ostream& stream,
197  const StorageContainer& storage
198  );
199 
200  friend std::istream&
201  operator >> (
202  std::istream& stream,
203  StorageContainer& storage
204  );
205 
206 private:
207 
208  struct Implementation;
209  std::unique_ptr<Implementation> m_impl;
210 };
211 
220 std::ostream&
221 operator << (
222  std::ostream& stream,
223  const StorageContainer& storage
224 );
225 
234 std::istream&
235 operator >> (
236  std::istream& stream,
237  StorageContainer& storage
238 );
239 
240 
244 class StorageList : public std::vector<StorageContainer> {
245 
246 public:
247 
257  static luabind::scope
258  luaBindings();
259 
263  StorageList();
264 
270  StorageList(
271  const StorageList& other
272  );
273 
279  StorageList(
280  StorageList&& other
281  );
282 
289  StorageList&
290  operator = (
291  const StorageList& other
292  );
293 
300  StorageList&
301  operator = (
302  StorageList&& other
303  );
304 
311  void
312  append(
313  StorageContainer element
314  );
315 
327  get(
328  size_t index
329  );
330 
331 };
332 
340 #define STORABLE_TYPE(typeName) \
341  template<> \
342  bool \
343  StorageContainer::contains<typeName>( \
344  const std::string& key \
345  ) const; \
346  \
347  template<> \
348  typeName \
349  StorageContainer::get<typeName>( \
350  const std::string& key, \
351  const typeName& defaultValue \
352  ) const; \
353  \
354  template<> \
355  void \
356  StorageContainer::set<typeName>( \
357  const std::string& key, \
358  typeName value \
359  );
360 
361 // Native types
362 STORABLE_TYPE(bool)
363 STORABLE_TYPE(char)
364 STORABLE_TYPE(int8_t)
365 STORABLE_TYPE(int16_t)
366 STORABLE_TYPE(int32_t)
367 STORABLE_TYPE(int64_t)
368 STORABLE_TYPE(uint8_t)
369 STORABLE_TYPE(uint16_t)
370 STORABLE_TYPE(uint32_t)
371 STORABLE_TYPE(uint64_t)
372 STORABLE_TYPE(float)
373 STORABLE_TYPE(double)
374 STORABLE_TYPE(std::string)
375 STORABLE_TYPE(StorageContainer)
376 STORABLE_TYPE(StorageList)
377 
378 // Compound types
379 STORABLE_TYPE(Ogre::Degree)
380 STORABLE_TYPE(Ogre::Plane)
381 STORABLE_TYPE(Ogre::Vector3)
382 STORABLE_TYPE(Ogre::Quaternion)
383 STORABLE_TYPE(Ogre::ColourValue)
384 }
StorageList & operator=(const StorageList &other)
Copy assignment.
bool contains(const std::string &key) const
Checks for a key.
luabind::object luaGet(const std::string &key, luabind::object defaultValue) const
Lua version of StorageContainer::get.
Definition: collision_system.h:158
static luabind::scope luaBindings()
Lua bindings.
void set(const std::string &key, T value)
Sets a value in this container.
std::list< std::string > keys() const
Returns a list of all keys in this container.
StorageContainer()
Constructor.
friend std::istream & operator>>(std::istream &stream, StorageContainer &storage)
Input stream operator for StorageContainer.
Definition: bullet_ogre_conversion.h:6
static luabind::scope luaBindings()
Lua bindings.
friend std::ostream & operator<<(std::ostream &stream, const StorageContainer &storage)
Output stream operator for StorageContainer.
A list of StorageContainers.
Definition: serialization.h:244
Definition: debug_drawing.h:8
~StorageContainer()
Destructor.
StorageContainer & operator=(const StorageContainer &other)
Copy-assignment.
void append(StorageContainer element)
Appends a StorageContainer to this list.
StorageList()
Constructor.
A key-value storage for serialization.
Definition: serialization.h:36