NuriaProject Framework  0.1
The NuriaProject Framework
serializer.hpp
1 /* Copyright (c) 2014-2015, The Nuria Project
2  * The NuriaProject Framework is free software: you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the License,
5  * or (at your option) any later version.
6  *
7  * The NuriaProject Framework is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public License
13  * along with The NuriaProject Framework.
14  * If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef NURIA_SERIALIZER_HPP
18 #define NURIA_SERIALIZER_HPP
19 
20 #include "essentials.hpp"
21 #include "metaobject.hpp"
22 #include <QStringList>
23 #include <QVariant>
24 #include <QObject>
25 
26 namespace Nuria {
27 
28 class SerializerPrivate;
29 
71 class NURIA_CORE_EXPORT Serializer {
72 public:
73 
74  enum RecursionDepth {
75  NoRecursion = 0,
76  InfiniteRecursion = -3
77  };
78 
84  typedef std::function< MetaObject *(const QByteArray & /* typeName */) > MetaObjectFinder;
85 
95  typedef std::function< void *(MetaObject * /* metaObject */, QVariantMap & /* data */) > InstanceCreator;
96 
102  typedef std::function< bool(QVariant & /* variant */, int /* toType */) > ValueConverter;
103 
107  Serializer (MetaObjectFinder metaObjectFinder = defaultMetaObjectFinder,
108  InstanceCreator instanceCreator = defaultInstanceCreator,
109  ValueConverter valueConverter = defaultValueConverter);
110 
112  ~Serializer ();
113 
117  QVector< QByteArray > exclude () const;
118 
124  void setExclude (const QVector< QByteArray > &list);
125 
130  QVector< QByteArray > allowedTypes () const;
131 
135  void setAllowedTypes (const QVector< QByteArray > &list) const;
136 
140  QStringList failedFields () const;
141 
146  int recursionDepth () const;
147 
151  void setRecursionDepth (int maxDepth);
152 
165  void *deserialize (const QVariantMap &data, MetaObject *meta);
166 
168  void *deserialize (const QVariantMap &data, const QByteArray &typeName);
169 
171  template< typename T >
172  T *deserialize (const QVariantMap &data) {
173  MetaObject *meta = MetaObject::of< T > ();
174  return meta ? reinterpret_cast< T * > (deserialize (data, meta)) : nullptr;
175  }
176 
182  bool populate (void *object, MetaObject *meta, const QVariantMap &data);
183 
185  bool populate (void *object, const QByteArray &typeName, const QVariantMap &data);
186 
188  template< typename T >
189  bool populate (T *object, const QVariantMap &data) {
190  MetaObject *meta = MetaObject::of< T > ();
191  return meta ? populate (object, meta, data) : false;
192  }
193 
208  QVariantMap serialize (void *object, MetaObject *meta);
209 
211  QVariantMap serialize (void *object, const QByteArray &typeName);
212 
217  static MetaObject *defaultMetaObjectFinder (const QByteArray &typeName);
218 
223  static void *defaultInstanceCreator (MetaObject *metaObject, QVariantMap &data);
224 
229  static bool defaultValueConverter (QVariant &variant, int toType);
230 
231 private:
232  SerializerPrivate *d;
233 
234  QVariantMap serializeImpl (void *object, MetaObject *meta);
235  bool populateImpl (void *object, MetaObject *meta, const QVariantMap &data);
236  bool variantToField (QVariant &value, const QByteArray &targetType,
237  int targetId, int sourceId, int pointerId, bool &ignored);
238  bool fieldToVariant (QVariant &value, bool &ignore);
239  bool readField (void *object, Nuria::MetaField &field, QVariantMap &data);
240  bool writeField (void *object, Nuria::MetaField &field, const QVariantMap &data);
241 };
242 
243 }
244 
245 #endif // NURIA_SERIALIZER_HPP
The MetaObject class provides access to meta-data of types at run-time.
Definition: metaobject.hpp:560
The MetaField class lets you access fields of types.
Definition: metaobject.hpp:296
(De-)Serializer for arbitary types based on Nuria::MetaObject.
Definition: serializer.hpp:71
bool populate(T *object, const QVariantMap &data)
Definition: serializer.hpp:189
T * deserialize(const QVariantMap &data)
Definition: serializer.hpp:172
Definition: abstractsessionmanager.hpp:24
std::function< MetaObject *(const QByteArray &) > MetaObjectFinder
Definition: serializer.hpp:84
std::function< bool(QVariant &, int) > ValueConverter
Definition: serializer.hpp:102
std::function< void *(MetaObject *, QVariantMap &) > InstanceCreator
Definition: serializer.hpp:95