qprotobufserializerregistry.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>, Viktor Kopp <vifactor@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 <QSharedPointer>
  28. #include <memory>
  29. #include <type_traits>
  30. #include "qtprotobuflogging.h"
  31. #include "selfcheckiterator.h"
  32. #include "qabstractprotobufserializer.h"
  33. #include "qtprotobuf_global.h"
  34. namespace qtprotobuf {
  35. class QTPROTOBUFSHARED_EXPORT QProtobufSerializerRegistry
  36. {
  37. QProtobufSerializerRegistry() = delete;
  38. ~QProtobufSerializerRegistry() = delete;
  39. Q_DISABLE_COPY(QProtobufSerializerRegistry)
  40. QProtobufSerializerRegistry(QProtobufSerializerRegistry &&) = delete;
  41. QProtobufSerializerRegistry &operator =(QProtobufSerializerRegistry &&) = delete;
  42. static QAbstractProtobufSerializer::SerializerRegistry serializers;
  43. static std::unique_ptr<QAbstractProtobufSerializer> basicSerializer;
  44. static QAbstractProtobufSerializer::SerializationHandlers empty;
  45. public:
  46. static const QAbstractProtobufSerializer::SerializationHandlers &handler(int userType);
  47. /**
  48. * @brief Serialization of a registered qtproto message object into byte-array
  49. *
  50. *
  51. * @param[in] object Pointer to QObject containing message to be serialized
  52. * @result serialized message bytes
  53. */
  54. template<typename T>
  55. static QByteArray serialize(const QObject *object) {
  56. qProtoDebug() << T::staticMetaObject.className() << "serialize";
  57. return basicSerializer->serializeObjectCommon(object, T::propertyOrdering, T::staticMetaObject);
  58. }
  59. /**
  60. * @brief Deserialization of a byte-array into a registered qtproto message object
  61. *
  62. * @details Properties in a message are identified via ProtobufObjectPrivate::decodeHeader.
  63. * Bytes corresponding to unexpected properties are skipped without any exception
  64. *
  65. * @param[out] object Pointer to memory where result of deserialization should be injected
  66. * @param[in] array Bytes with serialized message
  67. */
  68. template<typename T>
  69. static void deserialize(QObject *object, const QByteArray &array) {
  70. qProtoDebug() << T::staticMetaObject.className() << "deserialize";
  71. basicSerializer->deserializeObjectCommon(object, array, T::propertyOrdering, T::staticMetaObject);
  72. }
  73. //----------------------Functions to work with QObjects------------------------
  74. template<typename T>
  75. static void registerSerializers() {
  76. serializers[qMetaTypeId<T *>()] = { serializeComplexType<T>,
  77. deserializeComplexType<T>, LengthDelimited };
  78. serializers[qMetaTypeId<QList<QSharedPointer<T>>>()] = { serializeList<T>,
  79. deserializeList<T>, LengthDelimited };
  80. }
  81. template<typename K, typename V,
  82. typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
  83. static void registerMap() {
  84. serializers[qMetaTypeId<QMap<K, V>>()] = { serializeMap<K, V>,
  85. deserializeMap<K, V>, LengthDelimited };
  86. }
  87. template<typename K, typename V,
  88. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  89. static void registerMap() {
  90. serializers[qMetaTypeId<QMap<K, QSharedPointer<V>>>()] = { serializeMap<K, V>,
  91. deserializeMap<K, V>, LengthDelimited };
  92. }
  93. private:
  94. //###########################################################################
  95. // Serialization helpers
  96. //###########################################################################
  97. template <typename T,
  98. typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
  99. static QByteArray serializeComplexType(const QVariant &value, int &/*outFieldIndex*/) {
  100. return basicSerializer->serializeObject(value.value<T *>(), T::propertyOrdering, T::staticMetaObject);
  101. }
  102. //------------------Serialize lists of QObject based classes-----------------
  103. template<typename V,
  104. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  105. static QByteArray serializeList(const QVariant &listValue, int &outFieldIndex) {
  106. QList<QSharedPointer<V>> list = listValue.value<QList<QSharedPointer<V>>>();
  107. qProtoDebug() << __func__ << "listValue.count" << list.count() << "outFiledIndex" << outFieldIndex;
  108. if (list.count() <= 0) {
  109. outFieldIndex = NotUsedFieldIndex;
  110. return QByteArray();
  111. }
  112. QByteArray serializedList;
  113. for (auto &value : list) {
  114. if (!value) {
  115. qProtoWarning() << "Null pointer in list";
  116. continue;
  117. }
  118. serializedList.append(basicSerializer->serializeListObject(value.data(), V::propertyOrdering, V::staticMetaObject, outFieldIndex));
  119. }
  120. outFieldIndex = NotUsedFieldIndex;
  121. return serializedList;
  122. }
  123. //-------------------------Serialize maps of any type------------------------
  124. template<typename K, typename V,
  125. typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
  126. static QByteArray serializeMap(const QVariant &value, int &outFieldIndex) {
  127. QMap<K,V> mapValue = value.value<QMap<K,V>>();
  128. using ItType = typename QMap<K,V>::const_iterator;
  129. QByteArray mapResult;
  130. for ( ItType it = mapValue.constBegin(); it != mapValue.constEnd(); it++) {
  131. mapResult.append(basicSerializer->serializeMapPair(QVariant::fromValue<K>(it.key()), QVariant::fromValue<V>(it.value()), outFieldIndex));
  132. }
  133. outFieldIndex = NotUsedFieldIndex;
  134. return mapResult;
  135. }
  136. template<typename K, typename V,
  137. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  138. static QByteArray serializeMap(const QVariant &value, int &outFieldIndex) {
  139. QMap<K, QSharedPointer<V>> mapValue = value.value<QMap<K, QSharedPointer<V>>>();
  140. using ItType = typename QMap<K, QSharedPointer<V>>::const_iterator;
  141. QByteArray mapResult;
  142. for ( ItType it = mapValue.constBegin(); it != mapValue.constEnd(); it++) {
  143. if (it.value().isNull()) {
  144. qProtoWarning() << __func__ << "Trying to serialize map value that contains nullptr";
  145. continue;
  146. }
  147. mapResult.append(basicSerializer->serializeMapPair(QVariant::fromValue<K>(it.key()), QVariant::fromValue<V *>(it.value().data()), outFieldIndex));
  148. }
  149. outFieldIndex = NotUsedFieldIndex;
  150. return mapResult;
  151. }
  152. //###########################################################################
  153. // Deserialization helpers
  154. //###########################################################################
  155. //-----------------Deserialize lists of QObject based classes----------------
  156. template <typename V,
  157. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  158. static void deserializeList(SelfcheckIterator &it, QVariant &previous) {
  159. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  160. V *newValue = new V;
  161. QList<QSharedPointer<V>> list = previous.value<QList<QSharedPointer<V>>>();
  162. basicSerializer->deserializeListObject(newValue, it, V::propertyOrdering, V::staticMetaObject);
  163. list.append(QSharedPointer<V>(newValue));
  164. previous.setValue(list);
  165. }
  166. //-----------------------Deserialize maps of any type------------------------
  167. template <typename K, typename V,
  168. typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
  169. static void deserializeMap(SelfcheckIterator &it, QVariant &previous) {
  170. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  171. QMap<K, V> out = previous.value<QMap<K, V>>();
  172. QVariant key = QVariant::fromValue<K>(K());
  173. QVariant value = QVariant::fromValue<V>(V());
  174. basicSerializer->deserializeMapPair(key, value, it);
  175. out[key.value<K>()] = value.value<V>();
  176. previous = QVariant::fromValue<QMap<K, V>>(out);
  177. }
  178. template <typename K, typename V,
  179. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  180. static void deserializeMap(SelfcheckIterator &it, QVariant &previous) {
  181. qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
  182. auto out = previous.value<QMap<K, QSharedPointer<V>>>();
  183. QVariant key = QVariant::fromValue<K>(K());
  184. QVariant value = QVariant::fromValue<V *>(nullptr);
  185. basicSerializer->deserializeMapPair(key, value, it);
  186. out[key.value<K>()] = QSharedPointer<V>(value.value<V *>());
  187. previous = QVariant::fromValue<QMap<K, QSharedPointer<V>>>(out);
  188. }
  189. template <typename T,
  190. typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
  191. static void deserializeComplexType(SelfcheckIterator &it, QVariant &to) {
  192. T *value = new T;
  193. basicSerializer->deserializeObject(value, it, T::propertyOrdering, T::staticMetaObject);
  194. to = QVariant::fromValue<T *>(value);
  195. }
  196. };
  197. }