qabstractprotobufserializer_p.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <functional>
  30. #include "qtprotobuftypes.h"
  31. #include "qtprotobuflogging.h"
  32. #include "qtprotobufglobal.h"
  33. namespace QtProtobuf {
  34. class QAbstractProtobufSerializer;
  35. class QProtobufSelfcheckIterator;
  36. class QProtobufMetaProperty;
  37. }
  38. namespace QtProtobufPrivate {
  39. //! \private
  40. constexpr int NotUsedFieldIndex = -1;
  41. using Serializer = std::function<void(const QtProtobuf::QAbstractProtobufSerializer *, const QVariant &, const QtProtobuf::QProtobufMetaProperty &, QByteArray &)>;
  42. /*!
  43. * \brief Deserializer is interface function for deserialize method
  44. */
  45. using Deserializer = std::function<void(const QtProtobuf::QAbstractProtobufSerializer *, QtProtobuf::QProtobufSelfcheckIterator &, QVariant &)>;
  46. enum HandlerType {
  47. ObjectHandler,
  48. ListHandler,
  49. MapHandler
  50. };
  51. /*!
  52. * \brief SerializationHandlers contains set of objects that required for class serializaion/deserialization
  53. */
  54. struct SerializationHandler {
  55. Serializer serializer; /*!< serializer assigned to class */
  56. Deserializer deserializer;/*!< deserializer assigned to class */
  57. HandlerType type;/*!< Serialization WireType */
  58. };
  59. extern Q_PROTOBUF_EXPORT SerializationHandler &findHandler(int userType);
  60. extern Q_PROTOBUF_EXPORT void registerHandler(int userType, const SerializationHandler &handlers);
  61. /*!
  62. * \private
  63. *
  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 set is not setup");
  70. buffer.append(serializer->serializeObject(value.value<T *>(), T::protobufMetaObject, metaProperty));
  71. }
  72. /*!
  73. * \private
  74. *
  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 set is not setup");
  81. QList<QSharedPointer<V>> list = listValue.value<QList<QSharedPointer<V>>>();
  82. qProtoDebug() << __func__ << "listValue.count" << list.count();
  83. if (list.count() <= 0) {
  84. return;
  85. }
  86. for (auto &value : list) {
  87. if (!value) {
  88. qProtoWarning() << "Null pointer in list";
  89. continue;
  90. }
  91. buffer.append(serializer->serializeListObject(value.data(), V::protobufMetaObject, metaProperty));
  92. }
  93. }
  94. /*!
  95. * \private
  96. *
  97. * \brief default serializer template for map of key K, value V
  98. */
  99. template<typename K, typename V,
  100. typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
  101. void serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
  102. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "serializer set is not setup");
  103. QMap<K,V> mapValue = value.value<QMap<K,V>>();
  104. for (auto it = mapValue.constBegin(); it != mapValue.constEnd(); it++) {
  105. buffer.append(serializer->serializeMapPair(QVariant::fromValue<K>(it.key()), QVariant::fromValue<V>(it.value()), metaProperty));
  106. }
  107. }
  108. /*!
  109. * \private
  110. *
  111. * \brief default serializer template for map of type key K, value V. Specialization for V
  112. * inherited of QObject
  113. */
  114. template<typename K, typename V,
  115. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  116. void serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, const QtProtobuf::QProtobufMetaProperty &metaProperty, QByteArray &buffer) {
  117. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "serializer set is not setup");
  118. QMap<K, QSharedPointer<V>> mapValue = value.value<QMap<K, QSharedPointer<V>>>();
  119. for (auto it = mapValue.constBegin(); it != mapValue.constEnd(); it++) {
  120. if (it.value().isNull()) {
  121. qProtoWarning() << __func__ << "Trying to serialize map value that contains nullptr";
  122. continue;
  123. }
  124. buffer.append(serializer->serializeMapPair(QVariant::fromValue<K>(it.key()), QVariant::fromValue<V *>(it.value().data()), metaProperty));
  125. }
  126. }
  127. /*!
  128. * \private
  129. *
  130. * \brief default deserializer template for type T inherited of QObject
  131. */
  132. template <typename T,
  133. typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
  134. void deserializeObject(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &to) {
  135. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "serializer set is not setup");
  136. T *value = new T;
  137. serializer->deserializeObject(value, T::protobufMetaObject, it);
  138. to = QVariant::fromValue<T *>(value);
  139. }
  140. /*!
  141. * \private
  142. *
  143. * \brief default deserializer template for list of type T objects inherited of QObject
  144. */
  145. template <typename V,
  146. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  147. void deserializeList(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
  148. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "serializer set is not setup");
  149. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  150. V *newValue = new V;
  151. QList<QSharedPointer<V>> list = previous.value<QList<QSharedPointer<V>>>();
  152. serializer->deserializeListObject(newValue, V::protobufMetaObject, it);
  153. list.append(QSharedPointer<V>(newValue));
  154. previous.setValue(list);
  155. }
  156. /*!
  157. * \private
  158. *
  159. * \brief default deserializer template for map of key K, value V
  160. */
  161. template <typename K, typename V,
  162. typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
  163. void deserializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &previous) {
  164. Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "serializer set is not setup");
  165. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  166. QMap<K, V> out = previous.value<QMap<K, V>>();
  167. QVariant key = QVariant::fromValue<K>(K());
  168. QVariant value = QVariant::fromValue<V>(V());
  169. serializer->deserializeMapPair(key, value, it);
  170. out[key.value<K>()] = value.value<V>();
  171. previous = QVariant::fromValue<QMap<K, V>>(out);
  172. }
  173. /*!
  174. * \private
  175. *
  176. * \brief default deserializer template for map of type key K, value V. Specialization for V
  177. * inherited of QObject
  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 set is not setup");
  183. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  184. auto out = previous.value<QMap<K, QSharedPointer<V>>>();
  185. QVariant key = QVariant::fromValue<K>(K());
  186. QVariant value = QVariant::fromValue<V *>(nullptr);
  187. serializer->deserializeMapPair(key, value, it);
  188. out[key.value<K>()] = QSharedPointer<V>(value.value<V *>());
  189. previous = QVariant::fromValue<QMap<K, QSharedPointer<V>>>(out);
  190. }
  191. }