QtProtobuf  0.6
Protobuf plugin to generate Qt classes
qabstractprotobufserializer.h
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
5  *
6  * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
9  * software and associated documentation files (the "Software"), to deal in the Software
10  * without restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
12  * to permit persons to whom the Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all copies
16  * or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
21  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #pragma once //QAbstractProtobufSerializer
27 
28 #include <QObject>
29 #include <QVariant>
30 #include <QMetaObject>
31 
32 #include <unordered_map>
33 #include <functional>
34 #include <memory>
35 
36 #include "qtprotobuftypes.h"
37 #include "qtprotobuflogging.h"
38 #include "qprotobufselfcheckiterator.h"
39 
40 #include "qtprotobufglobal.h"
41 
42 namespace QtProtobuf {
43 
44 class QProtobufMetaProperty;
45 class QProtobufMetaObject;
46 
71 class Q_PROTOBUF_EXPORT QAbstractProtobufSerializer
72 {
73 public:
81  template<typename T>
82  QByteArray serialize(const QObject *object) {
83  Q_ASSERT(object != nullptr);
84  qProtoDebug() << T::staticMetaObject.className() << "serialize";
85  return serializeMessage(object, T::protobufMetaObject);
86  }
87 
97  template<typename T>
98  void deserialize(T *object, const QByteArray &data) {
99  Q_ASSERT(object != nullptr);
100  qProtoDebug() << T::staticMetaObject.className() << "deserialize";
101  //Initialize default object first and make copy aferwards, it's necessary to set default
102  //values of properties that was not stored in data.
103  T newValue;
104  try {
105  deserializeMessage(&newValue, T::protobufMetaObject, data);
106  } catch(...) {
107  *object = newValue;
108  throw;
109  }
110  *object = newValue;
111  }
112 
113  virtual ~QAbstractProtobufSerializer() = default;
114 
122  virtual QByteArray serializeMessage(const QObject *object, const QProtobufMetaObject &metaObject) const = 0;
123 
131  virtual void deserializeMessage(QObject *object, const QProtobufMetaObject &metaObject, const QByteArray &data) const = 0;
132 
141  virtual QByteArray serializeObject(const QObject *object, const QProtobufMetaObject &metaObject, const QProtobufMetaProperty &metaProperty) const = 0;
142 
152  virtual void deserializeObject(QObject *object, const QProtobufMetaObject &metaObject, QProtobufSelfcheckIterator &it) const = 0;
153 
159  virtual QByteArray serializeListBegin(const QProtobufMetaProperty &metaProperty) const {
160  Q_UNUSED(metaProperty);
161  return {};
162  }
163 
171  virtual QByteArray serializeListObject(const QObject *object, const QProtobufMetaObject &metaObject, const QProtobufMetaProperty &metaProperty) const = 0;
172 
179  virtual QByteArray serializeListEnd(QByteArray &buffer, const QProtobufMetaProperty &metaProperty) const {
180  Q_UNUSED(metaProperty);
181  Q_UNUSED(buffer);
182  return {};
183  }
184 
192  virtual bool deserializeListObject(QObject *object, const QProtobufMetaObject &metaObject, QProtobufSelfcheckIterator &it) const = 0;
193 
199  virtual QByteArray serializeMapBegin(const QProtobufMetaProperty &metaProperty) const {
200  Q_UNUSED(metaProperty);
201  return {};
202  }
203 
213  virtual QByteArray serializeMapPair(const QVariant &key, const QVariant &value, const QProtobufMetaProperty &metaProperty) const = 0;
214 
221  virtual QByteArray serializeMapEnd(QByteArray &buffer, const QProtobufMetaProperty &metaProperty) const {
222  Q_UNUSED(metaProperty);
223  return {};
224  }
225 
236  virtual bool deserializeMapPair(QVariant &key, QVariant &value, QProtobufSelfcheckIterator &it) const = 0;
237 
247  virtual QByteArray serializeEnum(int64 value, const QMetaEnum &metaEnum, const QtProtobuf::QProtobufMetaProperty &metaProperty) const = 0;
248 
258  virtual QByteArray serializeEnumList(const QList<int64> &value, const QMetaEnum &metaEnum, const QtProtobuf::QProtobufMetaProperty &metaProperty) const = 0;
259 
266  virtual void deserializeEnum(int64 &value, const QMetaEnum &metaEnum, QProtobufSelfcheckIterator &it) const = 0;
267 
274  virtual void deserializeEnumList(QList<int64> &value, const QMetaEnum &metaEnum, QProtobufSelfcheckIterator &it) const = 0;
275 };
277 }
278 
279 #include "qabstractprotobufserializer_p.h"
280 
286 template<typename T>
287 static void qRegisterProtobufType() {
288  T::registerTypes();
289  QtProtobufPrivate::registerHandler(qMetaTypeId<T *>(), { QtProtobufPrivate::serializeObject<T>,
290  QtProtobufPrivate::deserializeObject<T>, QtProtobufPrivate::ObjectHandler });
291  QtProtobufPrivate::registerHandler(qMetaTypeId<QList<QSharedPointer<T>>>(), { QtProtobufPrivate::serializeList<T>,
292  QtProtobufPrivate::deserializeList<T>, QtProtobufPrivate::ListHandler });
293 }
294 
300 template<typename K, typename V,
301  typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
302 inline void qRegisterProtobufMapType() {
303  QtProtobufPrivate::registerHandler(qMetaTypeId<QMap<K, V>>(), { QtProtobufPrivate::serializeMap<K, V>,
304  QtProtobufPrivate::deserializeMap<K, V>, QtProtobufPrivate::MapHandler });
305 }
306 
313 template<typename K, typename V,
314  typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
315 inline void qRegisterProtobufMapType() {
316  QtProtobufPrivate::registerHandler(qMetaTypeId<QMap<K, QSharedPointer<V>>>(), { QtProtobufPrivate::serializeMap<K, V>,
317  QtProtobufPrivate::deserializeMap<K, V>, QtProtobufPrivate::MapHandler });
318 }
319 
320 
326 template<typename T,
327  typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
328 inline void qRegisterProtobufEnumType() {
329  QtProtobufPrivate::registerHandler(qMetaTypeId<T>(), { QtProtobufPrivate::serializeEnum<T>,
330  QtProtobufPrivate::deserializeEnum<T>, QtProtobufPrivate::ObjectHandler });
331  QtProtobufPrivate::registerHandler(qMetaTypeId<QList<T>>(), { QtProtobufPrivate::serializeEnumList<T>,
332  QtProtobufPrivate::deserializeEnumList<T>, QtProtobufPrivate::ListHandler });
333 }
The QAbstractProtobufSerializer class is interface that represents basic functions for serialization/...
Definition: qabstractprotobufserializer.h:72
virtual QByteArray serializeListObject(const QObject *object, const QProtobufMetaObject &metaObject, const QProtobufMetaProperty &metaProperty) const =0
serializeListObject Method called to serialize object as a part of list property
void deserialize(T *object, const QByteArray &data)
Deserialization of a byte-array into a registered qtproto message object.
Definition: qabstractprotobufserializer.h:98
QByteArray serialize(const QObject *object)
Serialization of a registered qtproto message object into byte-array.
Definition: qabstractprotobufserializer.h:82
virtual QByteArray serializeMapBegin(const QProtobufMetaProperty &metaProperty) const
serializeMapEnd Method called at the begining of map serialization
Definition: qabstractprotobufserializer.h:199
virtual QByteArray serializeEnumList(const QList< int64 > &value, const QMetaEnum &metaEnum, const QtProtobuf::QProtobufMetaProperty &metaProperty) const =0
serializeEnumList Method called to serialize list of enum values
virtual void deserializeObject(QObject *object, const QProtobufMetaObject &metaObject, QProtobufSelfcheckIterator &it) const =0
deserializeObject Deserializes buffer to an object
virtual QByteArray serializeMapEnd(QByteArray &buffer, const QProtobufMetaProperty &metaProperty) const
serializeMapEnd Method called at the end of map serialization
Definition: qabstractprotobufserializer.h:221
virtual QByteArray serializeObject(const QObject *object, const QProtobufMetaObject &metaObject, const QProtobufMetaProperty &metaProperty) const =0
serializeObject Serializes complete object according given propertyOrdering and metaObject informatio...
virtual QByteArray serializeMessage(const QObject *object, const QProtobufMetaObject &metaObject) const =0
serializeMessage
virtual void deserializeMessage(QObject *object, const QProtobufMetaObject &metaObject, const QByteArray &data) const =0
serializeMessage
virtual bool deserializeListObject(QObject *object, const QProtobufMetaObject &metaObject, QProtobufSelfcheckIterator &it) const =0
deserializeListObject Deserializes an object from byte stream as part of list property
virtual QByteArray serializeEnum(int64 value, const QMetaEnum &metaEnum, const QtProtobuf::QProtobufMetaProperty &metaProperty) const =0
serializeEnum Serializes enum value represented as int64 type
virtual QByteArray serializeListEnd(QByteArray &buffer, const QProtobufMetaProperty &metaProperty) const
serializeListEnd Method called at the end of object list serialization
Definition: qabstractprotobufserializer.h:179
virtual void deserializeEnum(int64 &value, const QMetaEnum &metaEnum, QProtobufSelfcheckIterator &it) const =0
deserializeEnum Deserializes enum value from byte stream
virtual QByteArray serializeMapPair(const QVariant &key, const QVariant &value, const QProtobufMetaProperty &metaProperty) const =0
serializeMapPair Serializes QMap pair of key and value to raw data buffer
virtual void deserializeEnumList(QList< int64 > &value, const QMetaEnum &metaEnum, QProtobufSelfcheckIterator &it) const =0
deserializeEnum Deserializes list of enum values from byte stream
virtual bool deserializeMapPair(QVariant &key, QVariant &value, QProtobufSelfcheckIterator &it) const =0
deserializeMapPair Deserializes QMap pair of key and value from raw data
virtual QByteArray serializeListBegin(const QProtobufMetaProperty &metaProperty) const
serializeListBegin Method called at the begining of object list serialization
Definition: qabstractprotobufserializer.h:159
Definition: qprotobufmetaproperty.h:41
The QProtobufSelfcheckIterator class.
Definition: qprotobufselfcheckiterator.h:40