qabstractprotobufserializer.h 15 KB

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