qabstractprotobufserializer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <unordered_map>
  30. #include <functional>
  31. #include "qtprotobuftypes.h"
  32. #include "qtprotobuflogging.h"
  33. #include "qprotobufselfcheckiterator.h"
  34. #include "qtprotobufglobal.h"
  35. namespace qtprotobuf {
  36. /*!
  37. * \addtogroup QtProtobuf
  38. * \{
  39. */
  40. /*!
  41. * \brief The QAbstractProtobufSerializer class is interface that represents basic functions for serialization/deserialization
  42. *
  43. * \details This class is used by QProtobufSerializerRegistry to access basic serialization routines.
  44. *
  45. */
  46. class Q_PROTOBUF_EXPORT QAbstractProtobufSerializer
  47. {
  48. public:
  49. /*!
  50. * \brief Serializer is interface function for serialize method
  51. */
  52. using Serializer = std::function<QByteArray(const QVariant &, int &)>;
  53. /*!
  54. * \brief Deserializer is interface function for deserialize method
  55. */
  56. using Deserializer = std::function<void(QProtobufSelfcheckIterator &, QVariant &)>;
  57. /*!
  58. * \brief SerializationHandlers contains set of objects that required for class serializaion/deserialization
  59. */
  60. struct SerializationHandlers {
  61. Serializer serializer; /*!< serializer assigned to class */
  62. Deserializer deserializer;/*!< deserializer assigned to class */
  63. WireTypes type;/*!< Serialization WireType */
  64. };
  65. /*!
  66. * \brief SerializerRegistry is container to store mapping between metatype identifier and serialization handlers.
  67. */
  68. using SerializerRegistry = std::unordered_map<int/*metatypeid*/, SerializationHandlers>;
  69. virtual ~QAbstractProtobufSerializer() = default;
  70. virtual QByteArray serializeProperty(const QVariant &propertyValue, int fieldIndex, bool isEnum) = 0;
  71. virtual void deserializeProperty(QObject *object, QProtobufSelfcheckIterator &it, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) = 0;
  72. virtual QByteArray serializeObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) = 0;
  73. virtual void deserializeObject(QObject *object, QProtobufSelfcheckIterator &it, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) = 0;
  74. virtual QByteArray serializeListObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject, int fieldIndex) = 0;
  75. virtual void deserializeListObject(QObject *object, QProtobufSelfcheckIterator &it, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) = 0;
  76. virtual QByteArray serializeMapPair(const QVariant &key, const QVariant &value, int fieldIndex) = 0;
  77. virtual void deserializeMapPair(QVariant &key, QVariant &value, QProtobufSelfcheckIterator &it) = 0;
  78. QByteArray serializeObjectCommon(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject);
  79. void deserializeObjectCommon(QObject *object, const QByteArray &array, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject);
  80. const SerializerRegistry& handlers() { return m_handlers; }
  81. protected:
  82. SerializerRegistry m_handlers;//TODO: move to d-prointer
  83. };
  84. /*!
  85. * \brief The QProtobufSerializer class
  86. */
  87. class Q_PROTOBUF_EXPORT QProtobufSerializer : public QAbstractProtobufSerializer
  88. {
  89. public:
  90. QProtobufSerializer();
  91. ~QProtobufSerializer() = default;
  92. QByteArray serializeProperty(const QVariant &propertyValue, int fieldIndex, bool isEnum) override;
  93. void deserializeProperty(QObject *object, QProtobufSelfcheckIterator &it, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) override;
  94. QByteArray serializeObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) override;
  95. void deserializeObject(QObject *object, QProtobufSelfcheckIterator &it, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) override;
  96. QByteArray serializeListObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject, int fieldIndex) override;
  97. void deserializeListObject(QObject *object, QProtobufSelfcheckIterator &it, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) override;
  98. QByteArray serializeMapPair(const QVariant &key, const QVariant &value, int fieldIndex) override;
  99. void deserializeMapPair(QVariant &key, QVariant &value, QProtobufSelfcheckIterator &it) override;
  100. };
  101. /*! \} */
  102. //! \private
  103. constexpr int NotUsedFieldIndex = -1;
  104. }