qabstractprotobufserializer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 //QAbstractProtobufSerializer
  26. #include <QObject>
  27. #include <QVariant>
  28. #include <QMetaObject>
  29. #include <unordered_map>
  30. #include <functional>
  31. #include <memory>
  32. #include "qtprotobuftypes.h"
  33. #include "qtprotobuflogging.h"
  34. #include "qprotobufselfcheckiterator.h"
  35. #include "qtprotobufglobal.h"
  36. namespace QtProtobuf {
  37. class QProtobufMetaProperty;
  38. class QProtobufMetaObject;
  39. /*!
  40. * \addtogroup QtProtobuf
  41. * \{
  42. */
  43. /*!
  44. * \brief The QAbstractProtobufSerializer class is interface that represents basic functions for serialization/deserialization
  45. *
  46. * \details The QAbstractProtobufSerializer class registers serializers/deserializers for classes inherited of QObject.
  47. * To register serializers for user-defined class it has to be inherited of QObject and contains
  48. * Q_DECLARE_PROTOBUF_SERIALIZERS macro's.
  49. * \code{.cpp}
  50. * class MyType : public QObject
  51. * {
  52. * Q_OBJECT
  53. * Q_PROTOBUF_OBJECT
  54. * Q_PROPERTY(qprotobuf::sint32 prop READ prop WRITE setProp NOTIFY propChanged)
  55. * ...
  56. * Q_DECLARE_PROTOBUF_SERIALIZERS(MyType)
  57. * };
  58. * \endcode
  59. * Practically code above is generated automaticaly by running qtprotobufgenerator or using cmake build macro
  60. * generate_qtprotobuf, based on .proto files. But it's still possible to reuse manually written code if needed.
  61. *
  62. * This class should be used as base for specific serializers. The handlers property contains all
  63. * message-specific serializers and should be used while serialization/deserialization. Inherited classes should reimplement
  64. * scope of virtual methods that used by registred message serialization/deserialization functions.
  65. */
  66. class Q_PROTOBUF_EXPORT QAbstractProtobufSerializer
  67. {
  68. public:
  69. /*!
  70. * \brief Serialization of a registered qtproto message object into byte-array
  71. *
  72. *
  73. * \param[in] object Pointer to QObject containing message to be serialized
  74. * \result serialized message bytes
  75. */
  76. template<typename T>
  77. QByteArray serialize(const QObject *object) {
  78. Q_ASSERT(object != nullptr);
  79. qProtoDebug() << T::staticMetaObject.className() << "serialize";
  80. return serializeMessage(object, T::protobufMetaObject);
  81. }
  82. /*!
  83. * \brief Deserialization of a byte-array into a registered qtproto message object
  84. *
  85. * \details Properties in a message are identified via ProtobufObjectPrivate::decodeHeader.
  86. * Bytes corresponding to unexpected properties are skipped without any exception
  87. *
  88. * \param[out] object Pointer to memory where result of deserialization should be injected
  89. * \param[in] array Bytes with serialized message
  90. */
  91. template<typename T>
  92. void deserialize(QObject *object, const QByteArray &array) {
  93. Q_ASSERT(object != nullptr);
  94. qProtoDebug() << T::staticMetaObject.className() << "deserialize";
  95. deserializeMessage(object, T::protobufMetaObject, array);
  96. }
  97. virtual ~QAbstractProtobufSerializer() = default;
  98. /*!
  99. * \brief serializeMessage
  100. * \param object
  101. * \param propertyOrdering
  102. * \param metaObject
  103. * \return
  104. */
  105. virtual QByteArray serializeMessage(const QObject *object, const QProtobufMetaObject &metaObject) const = 0;
  106. /*!
  107. * \brief serializeMessage
  108. * \param object
  109. * \param propertyOrdering
  110. * \param metaObject
  111. * \return
  112. */
  113. virtual void deserializeMessage(QObject *object, const QProtobufMetaObject &metaObject, const QByteArray &data) const = 0;
  114. /*!
  115. * \brief serializeObject Serializes complete \a object according given \a propertyOrdering and \a metaObject
  116. * information
  117. * \param[in] object Pointer to object to be serialized
  118. * \param[in] metaObject Protobuf meta object information for given \a object
  119. * \param[in] metaProperty Information about property to be serialized
  120. * \return Raw serialized data represented as byte array
  121. */
  122. virtual QByteArray serializeObject(const QObject *object, const QProtobufMetaObject &metaObject, const QProtobufMetaProperty &metaProperty) const = 0;
  123. /*!
  124. * \brief deserializeObject Deserializes buffer to an \a object
  125. * \param[out] object Pointer to pre-allocated object
  126. * \param[in] metaObject Protobuf meta object information for given \a object. Static meta object usualy is used to get actual
  127. * property value and write new property to \a object
  128. * \param[in] it Pointer to beging of buffer where object serialized data is located
  129. * \param[in] propertyOrdering Ordering of properties for given \a object
  130. * \param[in] metaProperty Information about property to be serialized
  131. */
  132. virtual void deserializeObject(QObject *object, const QProtobufMetaObject &metaObject, QProtobufSelfcheckIterator &it) const = 0;
  133. /*!
  134. * \brief serializeListObject Method called to serialize \a object as a part of list property
  135. * \param[in] object Pointer to object that will be serialized
  136. * \param[in] metaObject Protobuf meta object information for given \a object
  137. * \param[in] metaProperty Information about property to be serialized
  138. * \return Raw serialized data represented as byte array
  139. */
  140. virtual QByteArray serializeListObject(const QObject *object, const QProtobufMetaObject &metaObject, const QProtobufMetaProperty &metaProperty) const = 0;
  141. /*!
  142. * \brief deserializeListObject Deserializes an \a object from byte stream as part of list property
  143. * \param[out] object Pointer to pre-allocated object, that will be appended to list property
  144. * \param[in] Protobuf meta object information for given \a object. Static meta object usualy is used to get actual
  145. * property value and write new property to \a object
  146. * \param[in] it Pointer to beging of buffer where object serialized data is located
  147. */
  148. virtual void deserializeListObject(QObject *object, const QProtobufMetaObject &metaObject, QProtobufSelfcheckIterator &it) const = 0;
  149. /*!
  150. * \brief serializeMapPair Serializes QMap pair of \a key and \a value to raw data buffer
  151. * \param[in] key Map key
  152. * \param[in] value Map value for given \a key
  153. * \param[in] metaProperty Information about property to be serialized
  154. * \return Raw serialized data represented as byte array
  155. *
  156. * \see https://developers.google.com/protocol-buffers/docs/proto3#maps for details
  157. */
  158. virtual QByteArray serializeMapPair(const QVariant &key, const QVariant &value, const QProtobufMetaProperty &metaProperty) const = 0;
  159. /*!
  160. * \brief deserializeMapPair Deserializes QMap pair of \a key and \a value from raw data
  161. * \param[out] key Buffer that will be used to store deserialized key. When passed to function, QVariant
  162. * already stores default constructed value. So it's possible to receive meta information about type from it.
  163. * \param[out] value Buffer that will be used to store deserialized value. When passed to function, QVariant
  164. * already stores default constructed value. So it's possible to receive meta information about type from it.
  165. * \param[in] it Points to serialized raw key/value data
  166. *
  167. * \see https://developers.google.com/protocol-buffers/docs/proto3#maps for details
  168. */
  169. virtual void deserializeMapPair(QVariant &key, QVariant &value, QProtobufSelfcheckIterator &it) const = 0;
  170. /*!
  171. * \brief serializeEnum Serializes enum value represented as int64 type
  172. * \param[in] value Enum value to be serialized
  173. * \param[in] metaEnum Information about enumeration type
  174. * \param[in] metaProperty Information about property to be serialized
  175. * \return Raw serialized data represented as byte array
  176. *
  177. * \see https://developers.google.com/protocol-buffers/docs/proto3#maps for details
  178. */
  179. virtual QByteArray serializeEnum(int64 value, const QMetaEnum &metaEnum, const QtProtobuf::QProtobufMetaProperty &metaProperty) const = 0;
  180. /*!
  181. * \brief serializeEnumList Method called to serialize list of enum values
  182. * \param[in] value List of enum values to be serialized, represented as int64
  183. * \param[in] metaEnum Information about enumeration type
  184. * \param[in] metaProperty Information about property to be serialized
  185. * \return Raw serialized data represented as byte array
  186. *
  187. * \see https://developers.google.com/protocol-buffers/docs/proto3#maps for details
  188. */
  189. virtual QByteArray serializeEnumList(const QList<int64> &value, const QMetaEnum &metaEnum, const QtProtobuf::QProtobufMetaProperty &metaProperty) const = 0;
  190. /*!
  191. * \brief deserializeEnum Deserializes enum value from byte stream
  192. * \param[out] value Buffer that will be used to collect new enum value
  193. * \param[in] metaEnum Information about enumeration type
  194. * \param[in] it Points to serialized raw key/value data
  195. */
  196. virtual void deserializeEnum(int64 &value, const QMetaEnum &metaEnum, QProtobufSelfcheckIterator &it) const = 0;
  197. /*!
  198. * \brief deserializeEnum Deserializes list of enum values from byte stream
  199. * \param[out] value QList that will be used to collect deserialized enum values
  200. * \param[in] metaEnum Information about enumeration type
  201. * \param[in] it Points to serialized raw key/value data
  202. */
  203. virtual void deserializeEnumList(QList<int64> &value, const QMetaEnum &metaEnum, QProtobufSelfcheckIterator &it) const = 0;
  204. };
  205. /*! \} */
  206. }
  207. #include "qabstractprotobufserializer_p.h"
  208. /*!
  209. * \brief Registers serializers for type T in QtProtobuf global serializers registry
  210. *
  211. * \details generates default serializers for type T. Type T has to be inherited of QObject.
  212. */
  213. template<typename T>
  214. static void qRegisterProtobufType() {
  215. QtProtobufPrivate::registerHandler(qMetaTypeId<T *>(), { QtProtobufPrivate::serializeObject<T>,
  216. QtProtobufPrivate::deserializeObject<T>, QtProtobufPrivate::ObjectHandler });
  217. QtProtobufPrivate::registerHandler(qMetaTypeId<QList<QSharedPointer<T>>>(), { QtProtobufPrivate::serializeList<T>,
  218. QtProtobufPrivate::deserializeList<T>, QtProtobufPrivate::ListHandler });
  219. }
  220. /*!
  221. * \brief Registers serializers for type QMap<K, V> in QtProtobuf global serializers registry
  222. *
  223. * \details generates default serializers for QMap<K, V>.
  224. */
  225. template<typename K, typename V,
  226. typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
  227. inline void qRegisterProtobufMapType() {
  228. QtProtobufPrivate::registerHandler(qMetaTypeId<QMap<K, V>>(), { QtProtobufPrivate::serializeMap<K, V>,
  229. QtProtobufPrivate::deserializeMap<K, V>, QtProtobufPrivate::MapHandler });
  230. }
  231. /*!
  232. * \brief Registers serializers for type QMap<K, V> in QtProtobuf global serializers registry
  233. *
  234. * \details generates default serializers for QMap<K, V>. Specialization for V type
  235. * inherited of QObject.
  236. */
  237. template<typename K, typename V,
  238. typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
  239. inline void qRegisterProtobufMapType() {
  240. QtProtobufPrivate::registerHandler(qMetaTypeId<QMap<K, QSharedPointer<V>>>(), { QtProtobufPrivate::serializeMap<K, V>,
  241. QtProtobufPrivate::deserializeMap<K, V>, QtProtobufPrivate::MapHandler });
  242. }
  243. /*!
  244. * \brief Registers serializers for enumeration type in QtProtobuf global serializers registry
  245. *
  246. * \details generates default serializers for enumeration and QList of enumerations.
  247. */
  248. template<typename T,
  249. typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
  250. inline void qRegisterProtobufEnumType() {
  251. QtProtobufPrivate::registerHandler(qMetaTypeId<T>(), { QtProtobufPrivate::serializeEnum<T>,
  252. QtProtobufPrivate::deserializeEnum<T>, QtProtobufPrivate::ObjectHandler });
  253. QtProtobufPrivate::registerHandler(qMetaTypeId<QList<T>>(), { QtProtobufPrivate::serializeEnumList<T>,
  254. QtProtobufPrivate::deserializeEnumList<T>, QtProtobufPrivate::ListHandler });
  255. }