qabstractprotobufserializer_p.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #pragma once
  26. #include <QObject>
  27. #include <QVariant>
  28. #include <QMetaObject>
  29. #include <QMetaEnum>
  30. #include <functional>
  31. #include "qtprotobuftypes.h"
  32. #include "qtprotobuflogging.h"
  33. #include "qtprotobufglobal.h"
  34. namespace QtProtobuf {
  35. class QAbstractProtobufSerializer;
  36. class QProtobufSelfcheckIterator;
  37. class QProtobufMetaProperty;
  38. }
  39. namespace QtProtobufPrivate {
  40. //! \private
  41. constexpr int NotUsedFieldIndex = -1;
  42. using Serializer = std::function<void(const QtProtobuf::QAbstractProtobufSerializer *, const QVariant &, const QtProtobuf::QProtobufMetaProperty &, QByteArray &)>;
  43. /*!
  44. * \brief Deserializer is interface function for deserialize method
  45. */
  46. using Deserializer = std::function<void(const QtProtobuf::QAbstractProtobufSerializer *, QtProtobuf::QProtobufSelfcheckIterator &, QVariant &)>;
  47. enum HandlerType {
  48. ObjectHandler,
  49. ListHandler,
  50. MapHandler
  51. };
  52. /*!
  53. * \private
  54. * \brief SerializationHandlers contains set of objects that required for class serializaion/deserialization
  55. */
  56. struct SerializationHandler {
  57. Serializer serializer; /*!< serializer assigned to class */
  58. Deserializer deserializer;/*!< deserializer assigned to class */
  59. HandlerType type;/*!< Serialization WireType */
  60. };
  61. extern Q_PROTOBUF_EXPORT SerializationHandler findHandler(int userType);
  62. extern Q_PROTOBUF_EXPORT void registerHandler(int userType, const SerializationHandler &handlers);
  63. /*!
  64. * \private
  65. * \brief default serializer template for type T inherited of QObject
  66. */
  67. template <typename T,
  68. typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
  69. void serializeObject(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
  70. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  71. buffer.append(serializer->serializeObject(value.value<T *>(), T::protobufMetaObject, metaProperty));
  72. }
  73. /*!
  74. * \private
  75. * \brief default serializer template for list of type T objects inherited of QObject
  76. */
  77. template<typename V,
  78. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  79. void serializeList(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &listValue, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
  80. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  81. QList<QSharedPointer<V>> list = listValue.value<QList<QSharedPointer<V>>>();
  82. qProtoDebug() << __func__ << "listValue.count" << list.count();
  83. buffer.append(serializer->serializeListBegin(metaProperty));
  84. for (auto &value : list) {
  85. if (!value) {
  86. qProtoWarning() << "Null pointer in list";
  87. continue;
  88. }
  89. buffer.append(serializer->serializeListObject(value.data(), V::protobufMetaObject, metaProperty));
  90. }
  91. buffer.append(serializer->serializeListEnd(buffer, metaProperty));
  92. }
  93. /*!
  94. * \private
  95. * \brief default serializer template for map of key K, value V
  96. */
  97. template<typename K, typename V,
  98. typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
  99. void serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
  100. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  101. QMap<K,V> mapValue = value.value<QMap<K,V>>();
  102. buffer.append(serializer->serializeMapBegin(metaProperty));
  103. for (auto it = mapValue.constBegin(); it != mapValue.constEnd(); it++) {
  104. buffer.append(serializer->serializeMapPair(QVariant::fromValue<K>(it.key()), QVariant::fromValue<V>(it.value()), metaProperty));
  105. }
  106. buffer.append(serializer->serializeMapEnd(buffer, metaProperty));
  107. }
  108. /*!
  109. * \private
  110. * \brief default serializer template for map of type key K, value V. Specialization for V inherited of QObject
  111. */
  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, QSharedPointer<V>> mapValue = value.value<QMap<K, QSharedPointer<V>>>();
  117. buffer.append(serializer->serializeMapBegin(metaProperty));
  118. for (auto it = mapValue.constBegin(); it != mapValue.constEnd(); it++) {
  119. if (it.value().isNull()) {
  120. qProtoWarning() << __func__ << "Trying to serialize map value that contains nullptr";
  121. continue;
  122. }
  123. buffer.append(serializer->serializeMapPair(QVariant::fromValue<K>(it.key()), QVariant::fromValue<V *>(it.value().data()), metaProperty));
  124. }
  125. buffer.append(serializer->serializeMapEnd(buffer, metaProperty));
  126. }
  127. /*!
  128. * \private
  129. * \brief default serializer template for enum types
  130. */
  131. template<typename T,
  132. typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
  133. void serializeEnum(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
  134. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  135. buffer.append(serializer->serializeEnum(QtProtobuf::int64(value.value<T>()), QMetaEnum::fromType<T>(), metaProperty));
  136. }
  137. /*!
  138. * \private
  139. * \brief default serializer template for enum list types
  140. */
  141. template<typename T,
  142. typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
  143. void serializeEnumList(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
  144. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  145. QList<QtProtobuf::int64> intList;
  146. for (auto enumValue : value.value<QList<T>>()) {
  147. intList.append(QtProtobuf::int64(enumValue));
  148. }
  149. buffer.append(serializer->serializeEnumList(intList, QMetaEnum::fromType<T>(), metaProperty));
  150. }
  151. /*!
  152. * \private
  153. * \brief default deserializer template for type T inherited of QObject
  154. */
  155. template <typename T,
  156. typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
  157. void deserializeObject(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &to) {
  158. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  159. T *value = new T;
  160. serializer->deserializeObject(value, T::protobufMetaObject, it);
  161. to = QVariant::fromValue<T *>(value);
  162. }
  163. /*!
  164. * \private
  165. * \brief default deserializer template for list of type T objects inherited of QObject
  166. */
  167. template <typename V,
  168. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  169. void deserializeList(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
  170. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  171. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  172. V *newValue = new V;
  173. QList<QSharedPointer<V>> list = previous.value<QList<QSharedPointer<V>>>();
  174. if (serializer->deserializeListObject(newValue, V::protobufMetaObject, it)) {
  175. list.append(QSharedPointer<V>(newValue));
  176. previous.setValue(list);
  177. }
  178. }
  179. /*!
  180. * \private
  181. *
  182. * \brief default deserializer template for map of key K, value V
  183. */
  184. template <typename K, typename V,
  185. typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
  186. void deserializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
  187. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  188. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  189. QMap<K, V> out = previous.value<QMap<K, V>>();
  190. QVariant key = QVariant::fromValue<K>(K());
  191. QVariant value = QVariant::fromValue<V>(V());
  192. if (serializer->deserializeMapPair(key, value, it)) {
  193. out[key.value<K>()] = value.value<V>();
  194. previous = QVariant::fromValue<QMap<K, V>>(out);
  195. }
  196. }
  197. /*!
  198. * \private
  199. *
  200. * \brief default deserializer template for map of type key K, value V. Specialization for V
  201. * inherited of QObject
  202. */
  203. template <typename K, typename V,
  204. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  205. void deserializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
  206. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  207. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  208. auto out = previous.value<QMap<K, QSharedPointer<V>>>();
  209. QVariant key = QVariant::fromValue<K>(K());
  210. QVariant value = QVariant::fromValue<V *>(nullptr);
  211. if (serializer->deserializeMapPair(key, value, it)) {
  212. out[key.value<K>()] = QSharedPointer<V>(value.value<V *>());
  213. previous = QVariant::fromValue<QMap<K, QSharedPointer<V>>>(out);
  214. }
  215. }
  216. /*!
  217. * \private
  218. *
  219. * \brief default deserializer template for enum type T
  220. */
  221. template <typename T,
  222. typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
  223. void deserializeEnum(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &to) {
  224. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  225. QtProtobuf::int64 intValue;
  226. serializer->deserializeEnum(intValue, QMetaEnum::fromType<T>(), it);
  227. to = QVariant::fromValue<T>(static_cast<T>(intValue._t));
  228. }
  229. /*!
  230. * \private
  231. *
  232. * \brief default deserializer template for enumList type T
  233. */
  234. template <typename T,
  235. typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
  236. void deserializeEnumList(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
  237. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  238. QList<QtProtobuf::int64> intList;
  239. serializer->deserializeEnumList(intList, QMetaEnum::fromType<T>(), it);
  240. QList<T> enumList = previous.value<QList<T>>();
  241. for (auto intValue : intList) {
  242. enumList.append(static_cast<T>(intValue._t));
  243. }
  244. previous = QVariant::fromValue<QList<T>>(enumList);
  245. }
  246. }