QtProtobuf  0.6
Protobuf plugin to generate Qt classes
qabstractprotobufserializer_p.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
27 
28 #include <QObject>
29 #include <QVariant>
30 #include <QMetaObject>
31 #include <QMetaEnum>
32 
33 #include <functional>
34 
35 #include "qtprotobuftypes.h"
36 #include "qtprotobuflogging.h"
37 #include "qtprotobufglobal.h"
38 
39 namespace QtProtobuf {
40  class QAbstractProtobufSerializer;
41  class QProtobufSelfcheckIterator;
42  class QProtobufMetaProperty;
43 }
44 
45 namespace QtProtobufPrivate {
47 constexpr int NotUsedFieldIndex = -1;
48 
49 using Serializer = std::function<void(const QtProtobuf::QAbstractProtobufSerializer *, const QVariant &, const QtProtobuf::QProtobufMetaProperty &, QByteArray &)>;
53 using Deserializer = std::function<void(const QtProtobuf::QAbstractProtobufSerializer *, QtProtobuf::QProtobufSelfcheckIterator &, QVariant &)>;
54 
55 enum HandlerType {
56  ObjectHandler,
57  ListHandler,
58  MapHandler
59 };
60 
65 struct SerializationHandler {
66  Serializer serializer;
67  Deserializer deserializer;
68  HandlerType type;
69 };
70 
71 extern Q_PROTOBUF_EXPORT SerializationHandler findHandler(int userType);
72 extern Q_PROTOBUF_EXPORT void registerHandler(int userType, const SerializationHandler &handlers);
73 
78 template <typename T,
79  typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
80 void serializeObject(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
81  Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
82  buffer.append(serializer->serializeObject(value.value<T *>(), T::protobufMetaObject, metaProperty));
83 }
84 
89 template<typename V,
90  typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
91 void serializeList(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &listValue, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
92  Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
93  QList<QSharedPointer<V>> list = listValue.value<QList<QSharedPointer<V>>>();
94 
95  qProtoDebug() << __func__ << "listValue.count" << list.count();
96 
97  buffer.append(serializer->serializeListBegin(metaProperty));
98  for (auto &value : list) {
99  if (!value) {
100  qProtoWarning() << "Null pointer in list";
101  continue;
102  }
103  buffer.append(serializer->serializeListObject(value.data(), V::protobufMetaObject, metaProperty));
104  }
105  buffer.append(serializer->serializeListEnd(buffer, metaProperty));
106 }
107 
112 template<typename K, typename V,
113  typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
114 void serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
115  Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
116  QMap<K,V> mapValue = value.value<QMap<K,V>>();
117  buffer.append(serializer->serializeMapBegin(metaProperty));
118  for (auto it = mapValue.constBegin(); it != mapValue.constEnd(); it++) {
119  buffer.append(serializer->serializeMapPair(QVariant::fromValue<K>(it.key()), QVariant::fromValue<V>(it.value()), metaProperty));
120  }
121  buffer.append(serializer->serializeMapEnd(buffer, metaProperty));
122 }
123 
128 template<typename K, typename V,
129  typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
130 void serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
131  Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
132  QMap<K, QSharedPointer<V>> mapValue = value.value<QMap<K, QSharedPointer<V>>>();
133  buffer.append(serializer->serializeMapBegin(metaProperty));
134  for (auto it = mapValue.constBegin(); it != mapValue.constEnd(); it++) {
135  if (it.value().isNull()) {
136  qProtoWarning() << __func__ << "Trying to serialize map value that contains nullptr";
137  continue;
138  }
139  buffer.append(serializer->serializeMapPair(QVariant::fromValue<K>(it.key()), QVariant::fromValue<V *>(it.value().data()), metaProperty));
140  }
141  buffer.append(serializer->serializeMapEnd(buffer, metaProperty));
142 }
143 
148 template<typename T,
149  typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
150 void serializeEnum(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
151  Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
152  buffer.append(serializer->serializeEnum(QtProtobuf::int64(value.value<T>()), QMetaEnum::fromType<T>(), metaProperty));
153 }
154 
159 template<typename T,
160  typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
161 void serializeEnumList(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
162  Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
163  QList<QtProtobuf::int64> intList;
164  for (auto enumValue : value.value<QList<T>>()) {
165  intList.append(QtProtobuf::int64(enumValue));
166  }
167  buffer.append(serializer->serializeEnumList(intList, QMetaEnum::fromType<T>(), metaProperty));
168 }
169 
174 template <typename T,
175  typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
176 void deserializeObject(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &to) {
177  Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
178  T *value = new T;
179  serializer->deserializeObject(value, T::protobufMetaObject, it);
180  to = QVariant::fromValue<T *>(value);
181 }
182 
187 template <typename V,
188  typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
189 void deserializeList(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
190  Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
191  qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
192 
193  V *newValue = new V;
194  QList<QSharedPointer<V>> list = previous.value<QList<QSharedPointer<V>>>();
195  if (serializer->deserializeListObject(newValue, V::protobufMetaObject, it)) {
196  list.append(QSharedPointer<V>(newValue));
197  previous.setValue(list);
198  }
199 }
200 
206 template <typename K, typename V,
207  typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
208 void deserializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
209  Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
210  qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
211 
212  QMap<K, V> out = previous.value<QMap<K, V>>();
213  QVariant key = QVariant::fromValue<K>(K());
214  QVariant value = QVariant::fromValue<V>(V());
215 
216  if (serializer->deserializeMapPair(key, value, it)) {
217  out[key.value<K>()] = value.value<V>();
218  previous = QVariant::fromValue<QMap<K, V>>(out);
219  }
220 }
221 
228 template <typename K, typename V,
229  typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
230 void deserializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
231  Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
232  qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
233 
234  auto out = previous.value<QMap<K, QSharedPointer<V>>>();
235  QVariant key = QVariant::fromValue<K>(K());
236  QVariant value = QVariant::fromValue<V *>(nullptr);
237 
238  if (serializer->deserializeMapPair(key, value, it)) {
239  out[key.value<K>()] = QSharedPointer<V>(value.value<V *>());
240  previous = QVariant::fromValue<QMap<K, QSharedPointer<V>>>(out);
241  }
242 }
243 
249 template <typename T,
250  typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
251 void deserializeEnum(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &to) {
252  Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
253  QtProtobuf::int64 intValue;
254  serializer->deserializeEnum(intValue, QMetaEnum::fromType<T>(), it);
255  to = QVariant::fromValue<T>(static_cast<T>(intValue._t));
256 }
257 
263 template <typename T,
264  typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
265 void deserializeEnumList(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
266  Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
267  QList<QtProtobuf::int64> intList;
268  serializer->deserializeEnumList(intList, QMetaEnum::fromType<T>(), it);
269  QList<T> enumList = previous.value<QList<T>>();
270  for (auto intValue : intList) {
271  enumList.append(static_cast<T>(intValue._t));
272  }
273  previous = QVariant::fromValue<QList<T>>(enumList);
274 }
275 }
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
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 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