123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- /*
- * MIT License
- *
- * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
- *
- * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this
- * software and associated documentation files (the "Software"), to deal in the Software
- * without restriction, including without limitation the rights to use, copy, modify,
- * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
- * to permit persons to whom the Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies
- * or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
- * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
- #pragma once //QAbstractProtobufSerializer
- #include <QObject>
- #include <QVariant>
- #include <QMetaObject>
- #include <unordered_map>
- #include <functional>
- #include <memory>
- #include "qtprotobuftypes.h"
- #include "qtprotobuflogging.h"
- #include "qprotobufselfcheckiterator.h"
- #include "qtprotobufglobal.h"
- namespace QtProtobuf {
- class QProtobufMetaProperty;
- class QProtobufMetaObject;
- /*!
- * \addtogroup QtProtobuf
- * \{
- */
- /*!
- * \brief The QAbstractProtobufSerializer class is interface that represents basic functions for serialization/deserialization
- *
- * \details The QAbstractProtobufSerializer class registers serializers/deserializers for classes inherited of QObject.
- * To register serializers for user-defined class it has to be inherited of QObject and contains
- * Q_DECLARE_PROTOBUF_SERIALIZERS macro's.
- * \code{.cpp}
- * class MyType : public QObject
- * {
- * Q_OBJECT
- * Q_PROTOBUF_OBJECT
- * Q_PROPERTY(qprotobuf::sint32 prop READ prop WRITE setProp NOTIFY propChanged)
- * ...
- * Q_DECLARE_PROTOBUF_SERIALIZERS(MyType)
- * };
- * \endcode
- * Practically code above is generated automaticaly by running qtprotobufgenerator or using cmake build macro
- * generate_qtprotobuf, based on .proto files. But it's still possible to reuse manually written code if needed.
- *
- * This class should be used as base for specific serializers. The handlers property contains all
- * message-specific serializers and should be used while serialization/deserialization. Inherited classes should reimplement
- * scope of virtual methods that used by registred message serialization/deserialization functions.
- */
- class Q_PROTOBUF_EXPORT QAbstractProtobufSerializer
- {
- public:
- /*!
- * \brief Serialization of a registered qtproto message object into byte-array
- *
- *
- * \param[in] object Pointer to QObject containing message to be serialized
- * \result serialized message bytes
- */
- template<typename T>
- QByteArray serialize(const QObject *object) {
- Q_ASSERT(object != nullptr);
- qProtoDebug() << T::staticMetaObject.className() << "serialize";
- return serializeMessage(object, T::protobufMetaObject);
- }
- /*!
- * \brief Deserialization of a byte-array into a registered qtproto message object
- *
- * \details Properties in a message are identified via ProtobufObjectPrivate::decodeHeader.
- * Bytes corresponding to unexpected properties are skipped without any exception
- *
- * \param[out] object Pointer to memory where result of deserialization should be injected
- * \param[in] array Bytes with serialized message
- */
- template<typename T>
- void deserialize(QObject *object, const QByteArray &array) {
- Q_ASSERT(object != nullptr);
- qProtoDebug() << T::staticMetaObject.className() << "deserialize";
- deserializeMessage(object, T::protobufMetaObject, array);
- }
- virtual ~QAbstractProtobufSerializer() = default;
- /*!
- * \brief serializeMessage
- * \param object
- * \param propertyOrdering
- * \param metaObject
- * \return
- */
- virtual QByteArray serializeMessage(const QObject *object, const QProtobufMetaObject &metaObject) const = 0;
- /*!
- * \brief serializeMessage
- * \param object
- * \param propertyOrdering
- * \param metaObject
- * \return
- */
- virtual void deserializeMessage(QObject *object, const QProtobufMetaObject &metaObject, const QByteArray &data) const = 0;
- /*!
- * \brief serializeObject Serializes complete \a object according given \a propertyOrdering and \a metaObject
- * information
- * \param[in] object Pointer to object to be serialized
- * \param[in] metaObject Protobuf meta object information for given \a object
- * \param[in] metaProperty Information about property to be serialized
- * \return Raw serialized data represented as byte array
- */
- virtual QByteArray serializeObject(const QObject *object, const QProtobufMetaObject &metaObject, const QProtobufMetaProperty &metaProperty) const = 0;
- /*!
- * \brief deserializeObject Deserializes buffer to an \a object
- * \param[out] object Pointer to pre-allocated object
- * \param[in] metaObject Protobuf meta object information for given \a object. Static meta object usualy is used to get actual
- * property value and write new property to \a object
- * \param[in] it Pointer to beging of buffer where object serialized data is located
- * \param[in] propertyOrdering Ordering of properties for given \a object
- * \param[in] metaProperty Information about property to be serialized
- */
- virtual void deserializeObject(QObject *object, const QProtobufMetaObject &metaObject, QProtobufSelfcheckIterator &it) const = 0;
- /*!
- * \brief serializeListObject Method called to serialize \a object as a part of list property
- * \param[in] object Pointer to object that will be serialized
- * \param[in] metaObject Protobuf meta object information for given \a object
- * \param[in] metaProperty Information about property to be serialized
- * \return Raw serialized data represented as byte array
- */
- virtual QByteArray serializeListObject(const QObject *object, const QProtobufMetaObject &metaObject, const QProtobufMetaProperty &metaProperty) const = 0;
- /*!
- * \brief deserializeListObject Deserializes an \a object from byte stream as part of list property
- * \param[out] object Pointer to pre-allocated object, that will be appended to list property
- * \param[in] Protobuf meta object information for given \a object. Static meta object usualy is used to get actual
- * property value and write new property to \a object
- * \param[in] it Pointer to beging of buffer where object serialized data is located
- */
- virtual void deserializeListObject(QObject *object, const QProtobufMetaObject &metaObject, QProtobufSelfcheckIterator &it) const = 0;
- /*!
- * \brief serializeMapPair Serializes QMap pair of \a key and \a value to raw data buffer
- * \param[in] key Map key
- * \param[in] value Map value for given \a key
- * \param[in] metaProperty Information about property to be serialized
- * \return Raw serialized data represented as byte array
- *
- * \see https://developers.google.com/protocol-buffers/docs/proto3#maps for details
- */
- virtual QByteArray serializeMapPair(const QVariant &key, const QVariant &value, const QProtobufMetaProperty &metaProperty) const = 0;
- /*!
- * \brief deserializeMapPair Deserializes QMap pair of \a key and \a value from raw data
- * \param[out] key Buffer that will be used to store deserialized key. When passed to function, QVariant
- * already stores default constructed value. So it's possible to receive meta information about type from it.
- * \param[out] value Buffer that will be used to store deserialized value. When passed to function, QVariant
- * already stores default constructed value. So it's possible to receive meta information about type from it.
- * \param[in] it Points to serialized raw key/value data
- *
- * \see https://developers.google.com/protocol-buffers/docs/proto3#maps for details
- */
- virtual void deserializeMapPair(QVariant &key, QVariant &value, QProtobufSelfcheckIterator &it) const = 0;
- /*!
- * \brief serializeEnum Serializes enum value represented as int64 type
- * \param[in] value Enum value to be serialized
- * \param[in] metaEnum Information about enumeration type
- * \param[in] metaProperty Information about property to be serialized
- * \return Raw serialized data represented as byte array
- *
- * \see https://developers.google.com/protocol-buffers/docs/proto3#maps for details
- */
- virtual QByteArray serializeEnum(int64 value, const QMetaEnum &metaEnum, const QtProtobuf::QProtobufMetaProperty &metaProperty) const = 0;
- /*!
- * \brief serializeEnumList Method called to serialize list of enum values
- * \param[in] value List of enum values to be serialized, represented as int64
- * \param[in] metaEnum Information about enumeration type
- * \param[in] metaProperty Information about property to be serialized
- * \return Raw serialized data represented as byte array
- *
- * \see https://developers.google.com/protocol-buffers/docs/proto3#maps for details
- */
- virtual QByteArray serializeEnumList(const QList<int64> &value, const QMetaEnum &metaEnum, const QtProtobuf::QProtobufMetaProperty &metaProperty) const = 0;
- /*!
- * \brief deserializeEnum Deserializes enum value from byte stream
- * \param[out] value Buffer that will be used to collect new enum value
- * \param[in] metaEnum Information about enumeration type
- * \param[in] it Points to serialized raw key/value data
- */
- virtual void deserializeEnum(int64 &value, const QMetaEnum &metaEnum, QProtobufSelfcheckIterator &it) const = 0;
- /*!
- * \brief deserializeEnum Deserializes list of enum values from byte stream
- * \param[out] value QList that will be used to collect deserialized enum values
- * \param[in] metaEnum Information about enumeration type
- * \param[in] it Points to serialized raw key/value data
- */
- virtual void deserializeEnumList(QList<int64> &value, const QMetaEnum &metaEnum, QProtobufSelfcheckIterator &it) const = 0;
- };
- /*! \} */
- }
- #include "qabstractprotobufserializer_p.h"
- /*!
- * \brief Registers serializers for type T in QtProtobuf global serializers registry
- *
- * \details generates default serializers for type T. Type T has to be inherited of QObject.
- */
- template<typename T>
- static void qRegisterProtobufType() {
- T::registerTypes();
- QtProtobufPrivate::registerHandler(qMetaTypeId<T *>(), { QtProtobufPrivate::serializeObject<T>,
- QtProtobufPrivate::deserializeObject<T>, QtProtobufPrivate::ObjectHandler });
- QtProtobufPrivate::registerHandler(qMetaTypeId<QList<QSharedPointer<T>>>(), { QtProtobufPrivate::serializeList<T>,
- QtProtobufPrivate::deserializeList<T>, QtProtobufPrivate::ListHandler });
- }
- /*!
- * \brief Registers serializers for type QMap<K, V> in QtProtobuf global serializers registry
- *
- * \details generates default serializers for QMap<K, V>.
- */
- template<typename K, typename V,
- typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
- inline void qRegisterProtobufMapType() {
- QtProtobufPrivate::registerHandler(qMetaTypeId<QMap<K, V>>(), { QtProtobufPrivate::serializeMap<K, V>,
- QtProtobufPrivate::deserializeMap<K, V>, QtProtobufPrivate::MapHandler });
- }
- /*!
- * \brief Registers serializers for type QMap<K, V> in QtProtobuf global serializers registry
- *
- * \details generates default serializers for QMap<K, V>. Specialization for V type
- * inherited of QObject.
- */
- template<typename K, typename V,
- typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
- inline void qRegisterProtobufMapType() {
- QtProtobufPrivate::registerHandler(qMetaTypeId<QMap<K, QSharedPointer<V>>>(), { QtProtobufPrivate::serializeMap<K, V>,
- QtProtobufPrivate::deserializeMap<K, V>, QtProtobufPrivate::MapHandler });
- }
- /*!
- * \brief Registers serializers for enumeration type in QtProtobuf global serializers registry
- *
- * \details generates default serializers for enumeration and QList of enumerations.
- */
- template<typename T,
- typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
- inline void qRegisterProtobufEnumType() {
- QtProtobufPrivate::registerHandler(qMetaTypeId<T>(), { QtProtobufPrivate::serializeEnum<T>,
- QtProtobufPrivate::deserializeEnum<T>, QtProtobufPrivate::ObjectHandler });
- QtProtobufPrivate::registerHandler(qMetaTypeId<QList<T>>(), { QtProtobufPrivate::serializeEnumList<T>,
- QtProtobufPrivate::deserializeEnumList<T>, QtProtobufPrivate::ListHandler });
- }
|