The Dirtchamber
A mixed reality testing environment for real-time global illumination algorithms
serializer.h
Go to the documentation of this file.
1 /*
2  * Dune D3D library - Tobias Alexander Franke 2014
3  * For copyright and license see LICENSE
4  * http://www.tobias-franke.eu
5  */
6 
9 #ifndef DUNE_SERIALIZER
10 #define DUNE_SERIALIZER
11 
12 #include "exception.h"
13 
14 #include <boost/any.hpp>
15 #include <map>
16 
17 #include "unicode.h"
18 
19 namespace dune
20 {
21  typedef int BOOL;
22 
41  class serializer
42  {
43  protected:
44  std::map<tstring, tstring> properties_;
45 
46  public:
48  void save(const tstring& filename);
49 
51  void load(const tstring& filename);
52 
63  template<typename T>
64  T get(const tstring& key) const
65  {
66  auto i = properties_.find(key);
67 
68  if (i == properties_.end())
69  throw dune::exception(L"Unknown key: " + key);
70 
71  T value;
72  tstringstream ss(i->second);
73 
74  ss >> std::boolalpha >> value;
75  return value;
76  }
77 
88  template<typename T>
89  void put(const tstring& key, const T& value)
90  {
91  tstringstream ss;
92  ss << value;
93  properties_[key] = ss.str();
94  }
95 
96  // Special case for type BOOL
97  template<>
98  void put(const tstring& key, const BOOL& value)
99  {
100  tstringstream ss;
101 
102  // BOOL is actually int, meh
103  if (value == 0)
104  ss << std::boolalpha << false;
105  else if (value == 1)
106  ss << std::boolalpha << true;
107  else
108  ss << value;
109 
110  properties_[key] = ss.str();
111  }
112  };
113 }
114 
115 #endif
void save(const tstring &filename)
Save the current key-value's into a file specified by filename.
Definition: serializer.cpp:59
Exception class.
Definition: exception.h:25
Seralizer to read/write Dune objects from/into JSON/XML.
Definition: serializer.h:41
void put(const tstring &key, const T &value)
Add a new key-value pair or overwrite an existing key with a new value.
Definition: serializer.h:89
void load(const tstring &filename)
Load key-value pairs from a file specified by filename.
Definition: serializer.cpp:51