Nuria Framework - Core
Core module of the NuriaProject Framework
 All Classes Functions Typedefs Enumerations Enumerator Groups
serializer.hpp
1 /* Copyright (c) 2014, The Nuria Project
2  * This software is provided 'as-is', without any express or implied
3  * warranty. In no event will the authors be held liable for any damages
4  * arising from the use of this software.
5  * Permission is granted to anyone to use this software for any purpose,
6  * including commercial applications, and to alter it and redistribute it
7  * freely, subject to the following restrictions:
8  * 1. The origin of this software must not be misrepresented; you must not
9  * claim that you wrote the original software. If you use this software
10  * in a product, an acknowledgment in the product documentation would be
11  * appreciated but is not required.
12  * 2. Altered source versions must be plainly marked as such, and must not be
13  * misrepresented as being the original software.
14  * 3. This notice may not be removed or altered from any source
15  * distribution.
16  */
17 
18 #ifndef NURIA_SERIALIZER_HPP
19 #define NURIA_SERIALIZER_HPP
20 
21 #include "essentials.hpp"
22 #include "metaobject.hpp"
23 #include <QStringList>
24 #include <QVariant>
25 #include <QObject>
26 
27 namespace Nuria {
28 
29 class SerializerPrivate;
30 
74 class NURIA_CORE_EXPORT Serializer {
75 public:
76 
77  enum RecursionDepth {
78  NoRecursion = 0,
79  InfiniteRecursion = -3
80  };
81 
87  typedef std::function< MetaObject *(const QByteArray & /* typeName */) > MetaObjectFinder;
88 
98  typedef std::function< void *(MetaObject * /* metaObject */, QVariantMap & /* data */) > InstanceCreator;
99 
103  Serializer (MetaObjectFinder metaObjectFinder = defaultMetaObjectFinder,
104  InstanceCreator instanceCreator = defaultInstanceCreator);
105 
107  ~Serializer ();
108 
112  QVector< QByteArray > exclude () const;
113 
119  void setExclude (const QVector< QByteArray > &list);
120 
125  QVector< QByteArray > allowedTypes () const;
126 
130  void setAllowedTypes (const QVector< QByteArray > &list) const;
131 
135  QStringList failedFields () const;
136 
141  int recursionDepth () const;
142 
146  void setRecursionDepth (int maxDepth);
147 
160  void *deserialize (const QVariantMap &data, MetaObject *meta);
161 
163  void *deserialize (const QVariantMap &data, const QByteArray &typeName);
164 
166  template< typename T >
167  T *deserialize (const QVariantMap &data) {
168  MetaObject *meta = MetaObject::of< T > ();
169  return meta ? reinterpret_cast< T * > (deserialize (data, meta)) : nullptr;
170  }
171 
177  bool populate (void *object, MetaObject *meta, const QVariantMap &data);
178 
180  bool populate (void *object, const QByteArray &typeName, const QVariantMap &data);
181 
183  template< typename T >
184  bool populate (T *object, const QVariantMap &data) {
185  MetaObject *meta = MetaObject::of< T > ();
186  return meta ? populate (object, meta, data) : false;
187  }
188 
203  QVariantMap serialize (void *object, MetaObject *meta);
204 
206  QVariantMap serialize (void *object, const QByteArray &typeName);
207 
212  static MetaObject *defaultMetaObjectFinder (const QByteArray &typeName);
213 
219  static void *defaultInstanceCreator (MetaObject *metaObject, QVariantMap &data);
220 
221 private:
222  SerializerPrivate *d;
223 
224  QVariantMap serializeImpl (void *object, MetaObject *meta);
225  bool populateImpl (void *object, MetaObject *meta, const QVariantMap &data);
226  bool variantToField (QVariant &value, const QByteArray &targetType,
227  int targetId, int sourceId, int pointerId, bool &ignored);
228  bool fieldToVariant (QVariant &value, bool &ignore);
229  bool readField (void *object, Nuria::MetaField &field, QVariantMap &data);
230  bool writeField (void *object, Nuria::MetaField &field, const QVariantMap &data);
231 };
232 
233 }
234 
235 #endif // NURIA_SERIALIZER_HPP
T * deserialize(const QVariantMap &data)
Definition: serializer.hpp:167
bool populate(T *object, const QVariantMap &data)
Definition: serializer.hpp:184
The MetaField class lets you access fields of types.
Definition: metaobject.hpp:297
std::function< void *(MetaObject *, QVariantMap &) > InstanceCreator
Definition: serializer.hpp:98
The MetaObject class provides access to meta-data of types at run-time.
Definition: metaobject.hpp:563
(De-)Serializer for arbitary types based on Nuria::MetaObject.
Definition: serializer.hpp:74
std::function< MetaObject *(const QByteArray &) > MetaObjectFinder
Definition: serializer.hpp:87