qabstractprotobufserializer_p.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. * \brief SerializationHandlers contains set of objects that required for class serializaion/deserialization
  54. */
  55. struct SerializationHandler {
  56. Serializer serializer; /*!< serializer assigned to class */
  57. Deserializer deserializer;/*!< deserializer assigned to class */
  58. HandlerType type;/*!< Serialization WireType */
  59. };
  60. extern Q_PROTOBUF_EXPORT SerializationHandler &findHandler(int userType);
  61. extern Q_PROTOBUF_EXPORT void registerHandler(int userType, const SerializationHandler &handlers);
  62. /*!
  63. * \private
  64. * \brief default serializer template for type T inherited of QObject
  65. */
  66. template <typename T,
  67. typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
  68. void serializeObject(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
  69. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  70. buffer.append(serializer->serializeObject(value.value<T *>(), T::protobufMetaObject, metaProperty));
  71. }
  72. /*!
  73. * \private
  74. * \brief default serializer template for list of type T objects inherited of QObject
  75. */
  76. template<typename V,
  77. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  78. void serializeList(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &listValue, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
  79. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  80. QList<QSharedPointer<V>> list = listValue.value<QList<QSharedPointer<V>>>();
  81. qProtoDebug() << __func__ << "listValue.count" << list.count();
  82. if (list.count() <= 0) {
  83. return;
  84. }
  85. for (auto &value : list) {
  86. if (!value) {
  87. qProtoWarning() << "Null pointer in list";
  88. continue;
  89. }
  90. buffer.append(serializer->serializeListObject(value.data(), V::protobufMetaObject, metaProperty));
  91. }
  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. for (auto it = mapValue.constBegin(); it != mapValue.constEnd(); it++) {
  103. buffer.append(serializer->serializeMapPair(QVariant::fromValue<K>(it.key()), QVariant::fromValue<V>(it.value()), metaProperty));
  104. }
  105. }
  106. /*!
  107. * \private
  108. * \brief default serializer template for map of type key K, value V. Specialization for V inherited of QObject
  109. */
  110. template<typename K, typename V,
  111. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  112. void serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
  113. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  114. QMap<K, QSharedPointer<V>> mapValue = value.value<QMap<K, QSharedPointer<V>>>();
  115. for (auto it = mapValue.constBegin(); it != mapValue.constEnd(); it++) {
  116. if (it.value().isNull()) {
  117. qProtoWarning() << __func__ << "Trying to serialize map value that contains nullptr";
  118. continue;
  119. }
  120. buffer.append(serializer->serializeMapPair(QVariant::fromValue<K>(it.key()), QVariant::fromValue<V *>(it.value().data()), metaProperty));
  121. }
  122. }
  123. /*!
  124. * \private
  125. * \brief default serializer template for enum types
  126. */
  127. template<typename T,
  128. typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
  129. void serializeEnum(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
  130. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  131. buffer.append(serializer->serializeEnum(QtProtobuf::int64(value.value<T>()), QMetaEnum::fromType<T>(), metaProperty));
  132. }
  133. /*!
  134. * \private
  135. * \brief default serializer template for enum list types
  136. */
  137. template<typename T,
  138. typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
  139. void serializeEnumList(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
  140. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  141. QList<QtProtobuf::int64> intList;
  142. for (auto enumValue : value.value<QList<T>>()) {
  143. intList.append(QtProtobuf::int64(enumValue));
  144. }
  145. buffer.append(serializer->serializeEnumList(intList, QMetaEnum::fromType<T>(), metaProperty));
  146. }
  147. /*!
  148. * \private
  149. * \brief default deserializer template for type T inherited of QObject
  150. */
  151. template <typename T,
  152. typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
  153. void deserializeObject(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &to) {
  154. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  155. T *value = new T;
  156. serializer->deserializeObject(value, T::protobufMetaObject, it);
  157. to = QVariant::fromValue<T *>(value);
  158. }
  159. /*!
  160. * \private
  161. * \brief default deserializer template for list of type T objects inherited of QObject
  162. */
  163. template <typename V,
  164. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  165. void deserializeList(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
  166. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  167. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  168. V *newValue = new V;
  169. QList<QSharedPointer<V>> list = previous.value<QList<QSharedPointer<V>>>();
  170. serializer->deserializeListObject(newValue, V::protobufMetaObject, it);
  171. list.append(QSharedPointer<V>(newValue));
  172. previous.setValue(list);
  173. }
  174. /*!
  175. * \private
  176. *
  177. * \brief default deserializer template for map of key K, value V
  178. */
  179. template <typename K, typename V,
  180. typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
  181. void deserializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
  182. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  183. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  184. QMap<K, V> out = previous.value<QMap<K, V>>();
  185. QVariant key = QVariant::fromValue<K>(K());
  186. QVariant value = QVariant::fromValue<V>(V());
  187. serializer->deserializeMapPair(key, value, it);
  188. out[key.value<K>()] = value.value<V>();
  189. previous = QVariant::fromValue<QMap<K, V>>(out);
  190. }
  191. /*!
  192. * \private
  193. *
  194. * \brief default deserializer template for map of type key K, value V. Specialization for V
  195. * inherited of QObject
  196. */
  197. template <typename K, typename V,
  198. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  199. void deserializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
  200. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  201. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  202. auto out = previous.value<QMap<K, QSharedPointer<V>>>();
  203. QVariant key = QVariant::fromValue<K>(K());
  204. QVariant value = QVariant::fromValue<V *>(nullptr);
  205. serializer->deserializeMapPair(key, value, it);
  206. out[key.value<K>()] = QSharedPointer<V>(value.value<V *>());
  207. previous = QVariant::fromValue<QMap<K, QSharedPointer<V>>>(out);
  208. }
  209. template <typename T,
  210. typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
  211. void deserializeEnum(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &to) {
  212. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  213. QtProtobuf::int64 intValue;
  214. serializer->deserializeEnum(intValue, QMetaEnum::fromType<T>(), it);
  215. to = QVariant::fromValue<T>(static_cast<T>(intValue._t));
  216. }
  217. template <typename T,
  218. typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
  219. void deserializeEnumList(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
  220. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
  221. QList<QtProtobuf::int64> intList;
  222. serializer->deserializeEnumList(intList, QMetaEnum::fromType<T>(), it);
  223. QList<T> enumList = previous.value<QList<T>>();
  224. for (auto intValue : intList) {
  225. enumList.append(static_cast<T>(intValue._t));
  226. }
  227. previous = QVariant::fromValue<QList<T>>(enumList);
  228. }
  229. }