Omnidome
Fulldome Mapping Software Toolkit
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PropertyMap.h
Go to the documentation of this file.
1 /* Copyright (c) 2014-2016 "Omnidome" by cr8tr
2  * Dome Mapping Projection Software (http://omnido.me).
3  * Omnidome was created by Michael Winkelmann aka Wilston Oreo (@WilstonOreo)
4  *
5  * This file is part of Omnidome.
6  *
7  * Omnidome is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Affero General Public License for more details.
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef OMNI_SERIALIZATION_PROPERTYMAP_H_
20 #define OMNI_SERIALIZATION_PROPERTYMAP_H_
21 
22 #include <memory>
23 #include <map>
24 #include <functional>
25 #include <QString>
26 #include <QByteArray>
27 #include <QBuffer>
28 #include <QDataStream>
29 #include <omni/Id.h>
30 #include <omni/exception.h>
34 
35 namespace omni {
36  namespace serialization {
37  namespace exception {
38  /// Checksum exception which is raids when checksums mismatch
39  class ChecksumError : public Error {
40  public:
42 
43  ChecksumError(QString _got, QString _expected, QString const& _id = QString()) :
44  got_(_got),
45  expected_(_expected),
46  id_(_id) {}
47 
48  inline QString message() const throw() {
49  QString _s("ChecksumError: Expected %1, got %2.");
50 
51  _s = _s.arg(expected_).arg(got_);
52 
53  if (!id_.isEmpty()) {
54  _s += QString(" On id %1.").arg(QString(id_));
55  }
56  return _s;
57  }
58 
59  private:
60  QString got_;
61  QString expected_;
62  QString id_;
63  };
64 
65  class PropertyExisting : public Error {
66  public:
68 
69  PropertyExisting(QString const& _id) : id_(_id) {}
70 
71  inline QString message() const throw() {
72  return QString("Property with id %1 already exists!").arg(QString(id_));
73  }
74 
75  private:
76  QString id_;
77  };
78 
79  class PropertyNotExisting : public Error {
80  public:
82 
83  PropertyNotExisting(QString const& _id) : id_(_id) {}
84 
85  inline QString message() const throw() {
86  return QString("Property with id %1 does not exist!").arg(id_);
87  }
88 
89  private:
90  QString id_;
91  };
92  }
93 
94  /**@brief Property map to store properties in a QBuffer with an id
95  **/
96  class PropertyMap : public Interface {
97  public:
98  PropertyMap();
99 
100  /// Construct from stream
101  PropertyMap(QDataStream&);
102 
103  /// Put an object of type T with id into property map
104  template<typename T, typename ... ARGS>
105  PropertyMap& put(QString const& _id, T const& _t, ARGS&& ... _args) {
106  if (properties_.count(_id) != 0) {
107  throw exception::PropertyExisting(_id);
108  }
109  QBuffer _buf;
110  _buf.open(QIODevice::WriteOnly);
111  QDataStream _s(&_buf); // ,QIODevice::WriteOnly);
112  serialize(_s, _t, _args ...);
113  properties_[_id] = _buf.data();
114  return *this;
115  }
116 
117  /// Read an object of type T with id from property map
118  template<typename T, typename ... ARGS>
119  PropertyMap const& get(QString const& _id, T& _t, ARGS&& ... _args) const {
120  if (properties_.count(_id) == 0) {
121  //throw exception::PropertyNotExisting(_id);
122  //return;
123  return *this;
124  }
125 
126  QDataStream _s(properties_.at(_id));
127  deserialize(_s, _t, _args ...);
128  return *this;
129  }
130 
131  /// Read an object of type T with id from property map
132  template<typename T, typename ... ARGS>
133  PropertyMap const& get(QString const& _id, T *_t, ARGS&& ... _args) const {
134  if (properties_.count(_id) == 0) {
135  //throw exception::PropertyNotExisting(_id);
136  return *this;
137  }
138 
139  QDataStream _s(properties_.at(_id));
140  deserialize(_s, _t, _args ...);
141  return *this;
142  }
143 
144  /// Get value from id (with optional default value when id is not present)
145  template<typename T>
146  T getValue(QString const& _id, T const& _default = T()) const {
147  if (properties_.count(_id) == 0) {
148  return _default;
149  }
150  T _t;
151  QDataStream _s(properties_.at(_id));
152  deserialize(_s, _t);
153  return _t;
154  }
155 
156  /// Read an object of type T with id from property map
157  template<typename FACTORY_FUNCTOR, typename ... ARGS>
158  PropertyMap const& getPtr(QString const& _id,
159  FACTORY_FUNCTOR _f,
160  ARGS&& ... _args) const {
161  if (properties_.count(_id) == 0) {
163  }
164  QDataStream _s(properties_.at(_id));
165  deserializePtr(_s, _f, _args ...);
166  return *this;
167  }
168 
169  /**@brief Get value from
170  **/
171  template<typename T, typename CLASS, typename MEM_FN>
172  PropertyMap const& get(QString const& _id,
173  CLASS& _class,
174  MEM_FN _memFn,
175  T const& _default = T()) const {
176  T _value = _default;
177 
178  try {
179  get<T>(_id, _value);
180  _memFn(_class, _value);
181  } catch (exception::PropertyNotExisting& e) {
182  return *this;
183  }
184  return *this;
185  }
186 
187  /// Write an object into property map
188  template<typename T, typename ... ARGS>
189  PropertyMap& operator()(QString const& _id, T const& _t) {
190  return put(_id, _t);
191  }
192 
193  /// Read an object from property map
194  template<typename T>
195  PropertyMap const& operator()(QString const& _id, T& _t) const {
196  return get(_id, _t);
197  }
198 
199  /// Return list of ids
200  std::vector<QString> ids() const {
201  std::vector<QString> _ids;
202  for (auto& _idValue : properties_) {
203  _ids.push_back(_idValue.first);
204  }
205  return _ids;
206  }
207 
208  /// Write property map to stream
209  void toStream(QDataStream&) const;
210 
211  /// Read property map from stream
212  void fromStream(QDataStream&);
213 
214  private:
215  typedef std::map<QString, QByteArray>property_map;
216 
217  /// Make MD5 checksum from byte array
218  static QString makeChecksum(QByteArray const& _b);
219 
220  /// Make MD5 checksum from byte array
221  static QString makeChecksum(QString const& _s);
222 
223  /// Make MD5 checksum from property map
224  static QString makeChecksum(property_map const& _properties);
225 
227  };
228  }
229 
231 }
232 
234 
235 
236 #endif /* OMNI_SERIALIZATION_PROPERTYMAP_H_ */
T getValue(QString const &_id, T const &_default=T()) const
Get value from id (with optional default value when id is not present)
Definition: PropertyMap.h:146
QString id_
Definition: PropertyMap.h:62
std::vector< QString > ids() const
Return list of ids.
Definition: PropertyMap.h:200
QString message() const
Definition: PropertyMap.h:48
Abstract interface for serialization Holds two member functions for serialization and deserializatio...
Definition: Interface.h:31
void toStream(QDataStream &) const
Write property map to stream.
Definition: PropertyMap.cpp:36
STREAM & serialize(STREAM &_stream, T const &_t)
Serialize object to stream.
Definition: traits.h:140
QString message() const
Definition: PropertyMap.h:85
PropertyMap()
Definition: PropertyMap.cpp:28
#define OMNI_EXCEPTION(EXCEPTION)
Definition: exception.h:55
#define OMNI_DECL_STREAM_OPERATORS(CLASS)
Definition: Interface.h:53
QString got_
Definition: PropertyMap.h:60
STREAM & deserialize(STREAM &_stream, T &_t, ARGS &&..._args)
Deserialize object of type T from stream with optional additional arguments.
Definition: traits.h:125
STREAM & deserializePtr(STREAM &_stream, F _f, ARGS &&..._args)
Deserialize a pointer from stream. Functor f must return a pointer which is constructed from a facto...
Definition: pointer.h:78
property_map properties_
Definition: PropertyMap.h:226
QString id_
Definition: PropertyMap.h:76
Checksum exception which is raids when checksums mismatch.
Definition: PropertyMap.h:39
PropertyMap & put(QString const &_id, T const &_t, ARGS &&..._args)
Put an object of type T with id into property map.
Definition: PropertyMap.h:105
PropertyMap const & operator()(QString const &_id, T &_t) const
Read an object from property map.
Definition: PropertyMap.h:195
PropertyMap const & getPtr(QString const &_id, FACTORY_FUNCTOR _f, ARGS &&..._args) const
Read an object of type T with id from property map.
Definition: PropertyMap.h:158
static QString makeChecksum(QByteArray const &_b)
Make MD5 checksum from byte array.
Definition: PropertyMap.cpp:74
Property map to store properties in a QBuffer with an id.
Definition: PropertyMap.h:96
std::map< QString, QByteArray > property_map
Definition: PropertyMap.h:215
QString message() const
Definition: PropertyMap.h:71
void fromStream(QDataStream &)
Read property map from stream.
Definition: PropertyMap.cpp:48
QString id_
Definition: PropertyMap.h:90
QString expected_
Definition: PropertyMap.h:61
PropertyMap & operator()(QString const &_id, T const &_t)
Write an object into property map.
Definition: PropertyMap.h:189