123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- /*
- * 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
- #include <QObject>
- #include <QMetaObject>
- #include <QMetaProperty>
- #include <QBitArray>
- #include <unordered_map>
- #include <memory>
- #include <type_traits>
- #include <typeinfo>
- #include <functional>
- #include <qtprotobuftypes.h>
- #include <qtprotobuflogging.h>
- #define ASSERT_FIELD_NUMBER(X) Q_ASSERT_X(X < 128 && X > 0 && X != NotUsedFieldIndex, T::staticMetaObject.className(), "fieldIndex is out of range")
- namespace qtprotobuf {
- enum WireTypes {
- UnknownWireType = -1,
- Varint = 0,
- Fixed64 = 1,
- LengthDelimited = 2,
- Fixed32 = 5
- };
- constexpr int NotUsedFieldIndex = -1;
- class ProtobufObjectPrivate : public QObject
- {
- protected:
- using ListSerializer = std::function<QByteArray(const ProtobufObjectPrivate *, const QVariant &, int &)>;
- using ListDeserializer = std::function<void(ProtobufObjectPrivate *, QByteArray::const_iterator &, QVariant &)>;
- struct SerializationHandlers {
- ListSerializer serializer;
- ListDeserializer deserializer;
- };
- using SerializerRegistry = std::unordered_map<int/*metatypeid*/, SerializationHandlers>;
- static SerializerRegistry serializers;
- public:
- explicit ProtobufObjectPrivate(QObject *parent = nullptr) : QObject(parent) {}
- virtual QByteArray serializePrivate() const = 0;
- virtual void deserializePrivate(const QByteArray &data) = 0;
- inline static unsigned char encodeHeaderByte(int fieldIndex, WireTypes wireType);
- inline static bool decodeHeaderByte(unsigned char typeByte, int &fieldIndex, WireTypes &wireType);
- QByteArray serializeValue(const QVariant &propertyValue, int fieldIndex, const QLatin1Literal &typeName) const;
- QByteArray serializeUserType(const QVariant &propertyValue, int &fieldIndex, const QLatin1Literal &typeName) const;
- void deserializeProperty(WireTypes wireType, const QMetaProperty &metaProperty, QByteArray::const_iterator &it);
- void deserializeUserType(const QMetaProperty &metaType, QByteArray::const_iterator &it, QVariant &newValue);
- //###########################################################################
- // Serialization helpers
- //###########################################################################
- QByteArray serializeLengthDelimited(const QByteArray &data) const {
- qProtoDebug() << __func__ << "data.size" << data.size() << "data" << data.toHex();
- //Varint serialize field size and apply result as starting point
- QByteArray result = serializeVarintZero(static_cast<unsigned int>(data.size()));
- result.append(data);
- return result;
- }
- template<typename V,
- typename std::enable_if_t<std::is_signed<V>::value, int> = 0>
- QByteArray serializeListType(const QList<V> &listValue, int &outFieldIndex) const {
- qProtoDebug() << __func__ << "listValue.count" << listValue.count() << "outFiledIndex" << outFieldIndex;
- if (listValue.count() <= 0) {
- outFieldIndex = NotUsedFieldIndex;
- return QByteArray();
- }
- QByteArray serializedList;
- for (auto &value : listValue) {
- serializedList.append(serializeVarint(value));
- }
- //If internal field type is not LengthDelimited, exact amount of fields to be specified
- serializedList.prepend(serializeVarintZero(static_cast<unsigned int>(serializedList.size())));
- return serializedList;
- }
- template<typename V,
- typename std::enable_if_t<std::is_signed<V>::value, int> = 0>
- QByteArray serializeListTypeZigZag(const QList<V> &listValue, int &outFieldIndex) const {
- qProtoDebug() << __func__ << "listValue.count" << listValue.count() << "outFiledIndex" << outFieldIndex;
- if (listValue.count() <= 0) {
- outFieldIndex = NotUsedFieldIndex;
- return QByteArray();
- }
- QByteArray serializedList;
- for (auto &value : listValue) {
- serializedList.append(serializeVarintZigZag(value));
- }
- //If internal field type is not LengthDelimited, exact amount of fields to be specified
- serializedList.prepend(serializeVarintZero(static_cast<unsigned int>(serializedList.size())));
- return serializedList;
- }
- template<typename V,
- typename std::enable_if_t<std::is_unsigned<V>::value, int> = 0>
- QByteArray serializeListType(const QList<V> &listValue, int &outFieldIndex) const {
- qProtoDebug() << __func__ << "listValue.count" << listValue.count() << "outFiledIndex" << outFieldIndex;
- if (listValue.count() <= 0) {
- outFieldIndex = NotUsedFieldIndex;
- return QByteArray();
- }
- QByteArray serializedList;
- for (auto &value : listValue) {
- serializedList.append(serializeVarint(value));
- }
- //If internal field type is not LengthDelimited, exact amount of fields to be specified
- serializedList.prepend(serializeVarintZero(static_cast<unsigned int>(serializedList.size())));
- return serializedList;
- }
- template<typename V,
- typename std::enable_if_t<std::is_floating_point<V>::value
- || std::is_same<V, unsigned int>::value
- || std::is_same<V, qulonglong>::value
- || std::is_same<V, int>::value
- || std::is_same<V, qlonglong>::value, int> = 0>
- QByteArray serializeFixedListType(const QList<V> &listValue, int &outFieldIndex) const {
- qProtoDebug() << __func__ << "listValue.count" << listValue.count() << "outFiledIndex" << outFieldIndex;
- if (listValue.count() <= 0) {
- outFieldIndex = NotUsedFieldIndex;
- return QByteArray();
- }
- QByteArray serializedList;
- for (auto &value : listValue) {
- serializedList.append(serializeFixed(value));
- }
- //If internal field type is not LengthDelimited, exact amount of fields to be specified
- serializedList.prepend(serializeVarintZero(static_cast<unsigned int>(serializedList.size())));
- return serializedList;
- }
- template<typename V,
- typename std::enable_if_t<std::is_same<V, QString>::value
- || std::is_same<V, QByteArray>::value, int> = 0>
- QByteArray serializeListType(const QList<V> &listValue, int &outFieldIndex) const {
- qProtoDebug() << __func__ << "listValue.count" << listValue.count() << "outFiledIndex" << outFieldIndex;
- if (listValue.count() <= 0) {
- outFieldIndex = NotUsedFieldIndex;
- return QByteArray();
- }
- QByteArray serializedList;
- for (auto &value : listValue) {
- serializedList.append(serializeValue(value, outFieldIndex, QLatin1Literal()));
- }
- outFieldIndex = NotUsedFieldIndex;
- return serializedList;
- }
- template<typename V,
- typename std::enable_if_t<std::is_base_of<ProtobufObjectPrivate, V>::value, int> = 0>
- QByteArray serializeListType(const QList<V> &listValue, int &outFieldIndex) const {
- qProtoDebug() << __func__ << "listValue.count" << listValue.count() << "outFiledIndex" << outFieldIndex;
- if (listValue.count() <= 0) {
- outFieldIndex = NotUsedFieldIndex;
- return QByteArray();
- }
- QByteArray serializedList;
- for (auto &value : listValue) {
- QByteArray serializedValue = serializeLengthDelimited(value.serialize());
- serializedValue.prepend(encodeHeaderByte(outFieldIndex, LengthDelimited));
- serializedList.append(serializedValue);
- }
- outFieldIndex = NotUsedFieldIndex;
- return serializedList;
- }
- template <typename V,
- typename std::enable_if_t<std::is_floating_point<V>::value
- || std::is_same<V, unsigned int>::value
- || std::is_same<V, qulonglong>::value
- || std::is_same<V, int>::value
- || std::is_same<V, qlonglong>::value, int> = 0>
- QByteArray serializeFixed(V value) const {
- qProtoDebug() << __func__ << "value" << value;
- //Reserve required amount of bytes
- QByteArray result(sizeof(V), '\0');
- *(V*)(result.data()) = value;
- return result;
- }
- template <typename V, typename UV = typename std::make_unsigned<V>::type,
- typename std::enable_if_t<std::is_signed<V>::value, int> = 0>
- QByteArray serializeVarint(V value) const {
- qProtoDebug() << __func__ << "value" << value;
- return serializeVarint(static_cast<UV>(value));
- }
- template <typename V, typename UV = typename std::make_unsigned<V>::type,
- typename std::enable_if_t<std::is_signed<V>::value, int> = 0>
- QByteArray serializeVarintZigZag(V value) const {
- qProtoDebug() << __func__ << "value" << value;
- UV uValue = 0;
- //Use ZigZag convertion first and apply unsigned variant next
- value = (value << 1) ^ (value >> (sizeof(UV) * 8 - 1));
- uValue = static_cast<UV>(value);
- return serializeVarint(uValue);
- }
- template <typename V,
- typename std::enable_if_t<std::is_unsigned<V>::value, int> = 0>
- QByteArray serializeVarint(V value) const {
- qProtoDebug() << __func__ << "value" << value;
- QByteArray result;
- //Reserve maximum required amount of bytes
- result.reserve(sizeof(V));
- while (value > 0) {
- //Put first 7 bits to result buffer and mark as not last
- result.append((value & 0x7F) | 0x80);
- //Devide values to chunks of 7 bits, move to next chunk
- value >>= 7;
- }
- //TODO: Zero case.
- //Aligned to reference cpp implementation. Where 0 ignored.
- //if (result.isEmpty()) {
- // result.append('\0');
- //}
- //Mark last chunk as last
- result.data()[result.size() - 1] &= ~0x80;
- return result;
- }
- template <typename V,
- typename std::enable_if_t<std::is_unsigned<V>::value, int> = 0>
- QByteArray serializeVarintZero(V value) const {
- qProtoDebug() << __func__ << "value" << value;
- QByteArray result = serializeVarint(value);
- //Zero case.
- if (result.isEmpty()) {
- result.append('\0');
- }
- return result;
- }
- //###########################################################################
- // Deserialization helpers
- //###########################################################################
- template <typename V,
- typename std::enable_if_t<std::is_floating_point<V>::value
- || std::is_same<V, int>::value
- || std::is_same<V, qlonglong>::value
- || std::is_same<V, unsigned int>::value
- || std::is_same<V, qulonglong>::value, int> = 0>
- QVariant deserializeFixed(QByteArray::const_iterator &it) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- QVariant newPropertyValue(QVariant::fromValue(*(V*)it));
- it += sizeof(V);
- return newPropertyValue;
- }
- template <typename V,
- typename std::enable_if_t<std::is_unsigned<V>::value, int> = 0>
- QVariant deserializeVarint(QByteArray::const_iterator &it) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- return QVariant::fromValue(deserializeVarintCommon<V>(it));
- }
- template <typename V, typename UV = typename std::make_unsigned<V>::type,
- typename std::enable_if_t<std::is_signed<V>::value, int> = 0>
- QVariant deserializeVarintZigZag(QByteArray::const_iterator &it) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- UV unsignedValue = deserializeVarintCommon<UV>(it);
- V value = (unsignedValue >> 1) ^ (-(unsignedValue & 1));
- return QVariant::fromValue(value);
- }
- template <typename V, typename UV = typename std::make_unsigned<V>::type,
- typename std::enable_if_t<std::is_signed<V>::value, int> = 0>
- QVariant deserializeVarint(QByteArray::const_iterator &it) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- UV unsignedValue = deserializeVarintCommon<UV>(it);
- V value = static_cast<V>(unsignedValue);
- return QVariant::fromValue(value);
- }
- template <typename V>
- V deserializeVarintCommon(QByteArray::const_iterator &it) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- V value = 0;
- int k = 0;
- while (true) {
- uint64_t byte = static_cast<uint64_t>(*it);
- value += (byte & 0x7f) << k;
- k += 7;
- if (((*it) & 0x80) == 0) {
- break;
- }
- ++it;
- }
- ++it;
- return value;
- }
- QByteArray deserializeLengthDelimited(QByteArray::const_iterator &it) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- unsigned int length = deserializeVarint<unsigned int>(it).toUInt();
- QByteArray result(it, length);
- it += length;
- return result;
- }
- QVariant deserializeProtobufObjectType(int userType, QByteArray::const_iterator &it) {
- auto value = reinterpret_cast<ProtobufObjectPrivate *>(QMetaType::create(userType));
- value->deserializePrivate(deserializeLengthDelimited(it));
- return QVariant(userType, value);
- }
- template <typename V,
- typename std::enable_if_t<std::is_same<V, QString>::value
- || std::is_same<V, QByteArray>::value, int> = 0>
- QByteArray deserializeListType(QByteArray::const_iterator &it) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- return deserializeLengthDelimited(it);
- }
- template <typename V,
- typename std::enable_if_t<std::is_base_of<ProtobufObjectPrivate, V>::value, int> = 0>
- QVariant deserializeListType(QByteArray::const_iterator &it) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- return deserializeProtobufObjectType(qMetaTypeId<V>(), it);
- }
- template <typename V,
- typename std::enable_if_t<std::is_floating_point<V>::value
- || std::is_same<V, unsigned int>::value
- || std::is_same<V, qulonglong>::value
- || std::is_same<V, int>::value
- || std::is_same<V, qlonglong>::value, int> = 0>
- QVariant deserializeListType(QByteArray::const_iterator &it) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- QList<V> out;
- unsigned int count = deserializeVarint<unsigned int>(it).toUInt() / sizeof(V);
- for (unsigned int i = 0; i < count; i++) {
- QVariant variant = deserializeFixed<V>(it);
- out.append(variant.value<V>());
- }
- return QVariant::fromValue(out);
- }
- template <typename V>
- QVariant deserializeVarintListType(QByteArray::const_iterator &it) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- QList<V> out;
- unsigned int count = deserializeVarint<unsigned int>(it).toUInt();
- QByteArray::const_iterator lastVarint = it + count;
- while (it != lastVarint) {
- QVariant variant = deserializeVarint<V>(it);
- out.append(variant.value<V>());
- }
- return QVariant::fromValue(out);
- }
- template <typename V>
- QVariant deserializeVarintListTypeZigZag(QByteArray::const_iterator &it) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- QList<V> out;
- unsigned int count = deserializeVarint<unsigned int>(it).toUInt();
- QByteArray::const_iterator lastVarint = it + count;
- while (it != lastVarint) {
- QVariant variant = deserializeVarintZigZag<V>(it);
- out.append(variant.value<V>());
- }
- return QVariant::fromValue(out);
- }
- };
- /* Header byte
- * bits | 7 6 5 4 3 | 2 1 0
- * -----------------------------------
- * meaning | Field index | Type
- */
- unsigned char ProtobufObjectPrivate::encodeHeaderByte(int fieldIndex, WireTypes wireType)
- {
- unsigned char header = (fieldIndex << 3) | wireType;
- return static_cast<char>(header);
- }
- bool ProtobufObjectPrivate::decodeHeaderByte(unsigned char typeByte, int &fieldIndex, WireTypes &wireType)
- {
- wireType = static_cast<WireTypes>(typeByte & 0x07);
- fieldIndex = typeByte >> 3;
- return fieldIndex < 128 && fieldIndex > 0 && (wireType == Varint
- || wireType == Fixed64
- || wireType == Fixed32
- || wireType == LengthDelimited);
- }
- }
|