123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- QT_BEGIN_NAMESPACE
- class QProtobufSerializer;
- //! \private
- class QProtobufSerializerPrivate final
- {
- Q_DISABLE_COPY_MOVE(QProtobufSerializerPrivate)
- public:
-
- using Serializer = QByteArray(*)(const QVariant &, int &);
-
- using Deserializer = void(*)(QProtobufSelfcheckIterator &, QVariant &);
-
- struct SerializationHandlers {
- Serializer serializer;
- Deserializer deserializer;
- WireTypes type;
- };
- using SerializerRegistry = std::unordered_map<int, SerializationHandlers>;
- QProtobufSerializerPrivate(QProtobufSerializer *q);
- ~QProtobufSerializerPrivate() = default;
-
-
-
- template <typename V,
- typename std::enable_if_t<std::is_integral<V>::value
- && std::is_unsigned<V>::value, int> = 0>
- static QByteArray serializeVarintCommon(const V &value) {
- qProtoDebug() << __func__ << "value" << value;
- V varint = value;
- QByteArray result;
- while (varint != 0) {
-
- result.append((varint & 0b01111111) | 0b10000000);
-
- varint >>= 7;
- }
- if (result.isEmpty()) {
- result.append('\0');
- }
- result.data()[result.size() - 1] &= ~0b10000000;
- return result;
- }
-
-
- template <typename V,
- typename std::enable_if_t<std::is_floating_point<V>::value, int> = 0>
- static QByteArray serializeBasic(const V &value, int &) {
- qProtoDebug() << __func__ << "value" << value;
-
- QByteArray result(sizeof(V), '\0');
- *reinterpret_cast<V *>(result.data()) = value;
- return result;
- }
-
- template <typename V,
- typename std::enable_if_t<std::is_same<V, QtProtobuf::fixed32>::value
- || std::is_same<V, QtProtobuf::fixed64>::value
- || std::is_same<V, QtProtobuf::sfixed32>::value
- || std::is_same<V, QtProtobuf::sfixed64>::value, int> = 0>
- static QByteArray serializeBasic(const V &value, int &) {
- qProtoDebug() << __func__ << "value" << value;
-
- QByteArray result(sizeof(V), '\0');
- *reinterpret_cast<V *>(result.data()) = value;
- return result;
- }
-
- template <typename V,
- typename std::enable_if_t<std::is_integral<V>::value
- && std::is_signed<V>::value, int> = 0>
- static QByteArray serializeBasic(const V &value, int &outFieldIndex) {
- qProtoDebug() << __func__ << "value" << value;
- using UV = typename std::make_unsigned<V>::type;
- UV uValue = 0;
-
- V zigZagValue = (value << 1) ^ (value >> (sizeof(UV) * 8 - 1));
- uValue = static_cast<UV>(zigZagValue);
- return serializeBasic(uValue, outFieldIndex);
- }
- template <typename V,
- typename std::enable_if_t<std::is_same<V, QtProtobuf::int32>::value
- || std::is_same<V, QtProtobuf::int64>::value, int> = 0>
- static QByteArray serializeBasic(const V &value, int &outFieldIndex) {
- qProtoDebug() << __func__ << "value" << value;
- using UV = typename std::make_unsigned<V>::type;
- return serializeBasic(static_cast<UV>(value), outFieldIndex);
- }
-
- template <typename V,
- typename std::enable_if_t<std::is_integral<V>::value
- && std::is_unsigned<V>::value, int> = 0>
- static QByteArray serializeBasic(const V &value, int &outFieldIndex) {
- qProtoDebug() << __func__ << "value" << value;
- V varint = value;
- QByteArray result;
- while (varint != 0) {
-
- result.append((varint & 0b01111111) | 0b10000000);
-
- varint >>= 7;
- }
-
-
- if (result.size() == 0) {
- outFieldIndex = QtProtobufPrivate::NotUsedFieldIndex;
- } else {
-
- result.data()[result.size() - 1] &= ~0b10000000;
- }
- return result;
- }
-
- template <typename V,
- typename std::enable_if_t<std::is_same<V, QString>::value, int> = 0>
- static QByteArray serializeBasic(const V &value, int &) {
- return serializeLengthDelimited(value.toUtf8());
- }
- template <typename V,
- typename std::enable_if_t<std::is_same<V, QByteArray>::value, int> = 0>
- static QByteArray serializeBasic(const V &value, int &) {
- return serializeLengthDelimited(value);
- }
-
- template<typename V,
- typename std::enable_if_t<!(std::is_same<V, QString>::value
- || std::is_base_of<QObject, V>::value), int> = 0>
- static QByteArray serializeListType(const QList<V> &listValue, int &outFieldIndex) {
- qProtoDebug() << __func__ << "listValue.count" << listValue.count() << "outFiledIndex" << outFieldIndex;
- if (listValue.count() <= 0) {
- outFieldIndex = QtProtobufPrivate::NotUsedFieldIndex;
- return QByteArray();
- }
- int empty = QtProtobufPrivate::NotUsedFieldIndex;
- QByteArray serializedList;
- for (auto &value : listValue) {
- QByteArray element = serializeBasic<V>(value, empty);
- if (element.isEmpty()) {
- element.append('\0');
- }
- serializedList.append(element);
- }
-
- serializedList = prependLengthDelimitedSize(serializedList);
- return serializedList;
- }
- template<typename V,
- typename std::enable_if_t<std::is_same<V, QString>::value, int> = 0>
- static QByteArray serializeListType(const QStringList &listValue, int &outFieldIndex) {
- qProtoDebug() << __func__ << "listValue.count" << listValue.count() << "outFiledIndex" << outFieldIndex;
- if (listValue.count() <= 0) {
- outFieldIndex = QtProtobufPrivate::NotUsedFieldIndex;
- return QByteArray();
- }
- QByteArray serializedList;
- for (auto &value : listValue) {
- serializedList.append(QProtobufSerializerPrivate::encodeHeader(outFieldIndex, LengthDelimited));
- serializedList.append(serializeLengthDelimited(value.toUtf8()));
- }
- outFieldIndex = QtProtobufPrivate::NotUsedFieldIndex;
- return serializedList;
- }
-
-
-
- template <typename V,
- typename std::enable_if_t<std::is_integral<V>::value
- && std::is_unsigned<V>::value, int> = 0>
- static V deserializeVarintCommon(QProtobufSelfcheckIterator &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 & 0b01111111) << k;
- k += 7;
- if (((*it) & 0b10000000) == 0) {
- break;
- }
- ++it;
- }
- ++it;
- return value;
- }
-
- template <typename V,
- typename std::enable_if_t<std::is_floating_point<V>::value
- || std::is_same<V, QtProtobuf::fixed32>::value
- || std::is_same<V, QtProtobuf::fixed64>::value
- || std::is_same<V, QtProtobuf::sfixed32>::value
- || std::is_same<V, QtProtobuf::sfixed64>::value, int> = 0>
- static void deserializeBasic(QProtobufSelfcheckIterator &it, QVariant &variantValue) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- variantValue = QVariant::fromValue(*(V *)((QByteArray::const_iterator&)it));
- it += sizeof(V);
- }
- template <typename V,
- typename std::enable_if_t<std::is_integral<V>::value
- && std::is_unsigned<V>::value, int> = 0>
- static void deserializeBasic(QProtobufSelfcheckIterator &it, QVariant &variantValue) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- variantValue = QVariant::fromValue(deserializeVarintCommon<V>(it));
- }
- template <typename V,
- typename std::enable_if_t<std::is_integral<V>::value
- && std::is_signed<V>::value,int> = 0>
- static void deserializeBasic(QProtobufSelfcheckIterator &it, QVariant &variantValue) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- using UV = typename std::make_unsigned<V>::type;
- UV unsignedValue = deserializeVarintCommon<UV>(it);
- V value = (unsignedValue >> 1) ^ (-1 * (unsignedValue & 1));
- variantValue = QVariant::fromValue<V>(value);
- }
- template <typename V,
- typename std::enable_if_t<std::is_same<QtProtobuf::int32, V>::value
- || std::is_same<QtProtobuf::int64, V>::value, int> = 0>
- static void deserializeBasic(QProtobufSelfcheckIterator &it, QVariant &variantValue) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- using UV = typename std::make_unsigned<V>::type;
- UV unsignedValue = deserializeVarintCommon<UV>(it);
- V value = static_cast<V>(unsignedValue);
- variantValue = QVariant::fromValue(value);
- }
-
- template <typename V,
- typename std::enable_if_t<std::is_same<QByteArray, V>::value, int> = 0>
- static void deserializeBasic(QProtobufSelfcheckIterator &it, QVariant &variantValue) {
- variantValue = QVariant::fromValue(deserializeLengthDelimited(it));
- }
- template <typename V,
- typename std::enable_if_t<std::is_same<QString, V>::value, int> = 0>
- static void deserializeBasic(QProtobufSelfcheckIterator &it, QVariant &variantValue) {
- variantValue = QVariant::fromValue(QString::fromUtf8(deserializeLengthDelimited(it)));
- }
-
- template <typename V,
- typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
- static void deserializeList(QProtobufSelfcheckIterator &it, QVariant &previousValue) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- QList<V> out;
- unsigned int count = deserializeVarintCommon<QtProtobuf::uint32>(it);
- QProtobufSelfcheckIterator lastVarint = it + count;
- while (it != lastVarint) {
- QVariant variantValue;
- deserializeBasic<V>(it, variantValue);
- out.append(variantValue.value<V>());
- }
- previousValue.setValue(out);
- }
-
-
-
- static QByteArray deserializeLengthDelimited(QProtobufSelfcheckIterator &it) {
- qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
- unsigned int length = deserializeVarintCommon<QtProtobuf::uint32>(it);
- QByteArray result((QByteArray::const_iterator&)it, length);
- it += length;
- return result;
- }
- static QByteArray serializeLengthDelimited(const QByteArray &data) {
- qProtoDebug() << __func__ << "data.size" << data.size() << "data" << data.toHex();
- QByteArray result(data);
-
- result = prependLengthDelimitedSize(result);
- return result;
- }
- static bool decodeHeader(QProtobufSelfcheckIterator &it, int &fieldIndex, WireTypes &wireType);
- static QByteArray encodeHeader(int fieldIndex, WireTypes wireType);
-
- static QByteArray prependLengthDelimitedSize(const QByteArray &data)
- {
- return serializeVarintCommon<uint32_t>(data.size()) + data;
- }
- template <typename T,
- QByteArray(*s)(const T &, int &)>
- static QByteArray serializeWrapper(const QVariant &variantValue, int &fieldIndex) {
- if (variantValue.isNull()) {
- fieldIndex = QtProtobufPrivate::NotUsedFieldIndex;
- return QByteArray();
- }
- const T& value = *(static_cast<const T *>(variantValue.data()));
- return s(value, fieldIndex);
- }
- template <typename T, QByteArray(*s)(const T &, int &), Deserializer d, WireTypes type,
- typename std::enable_if_t<!std::is_base_of<QObject, T>::value, int> = 0>
- static void wrapSerializer() {
- handlers[qMetaTypeId<T>()] = {
- serializeWrapper<T, s>,
- d,
- type
- };
- }
- template <typename T, typename S, QByteArray(*s)(const S &, int &), Deserializer d, WireTypes type,
- typename std::enable_if_t<!std::is_base_of<QObject, T>::value, int> = 0>
- static void wrapSerializer() {
- handlers[qMetaTypeId<T>()] = {
- serializeWrapper<S, s>,
- d,
- type
- };
- }
-
-
- static int skipSerializedFieldBytes(QProtobufSelfcheckIterator &it, WireTypes type);
- static void skipVarint(QProtobufSelfcheckIterator &it);
- static void skipLengthDelimited(QProtobufSelfcheckIterator &it);
- QByteArray serializeProperty(const QVariant &propertyValue, const QProtobufMetaProperty &metaProperty);
- void deserializeProperty(QObject *object, const QProtobufMetaObject &metaObject, QProtobufSelfcheckIterator &it);
- void deserializeMapPair(QVariant &key, QVariant &value, QProtobufSelfcheckIterator &it);
- private:
- static SerializerRegistry handlers;
- QProtobufSerializer *q_ptr;
- };
- inline QByteArray QProtobufSerializerPrivate::encodeHeader(int fieldIndex, WireTypes wireType)
- {
- uint32_t header = (fieldIndex << 3) | wireType;
- return serializeVarintCommon<uint32_t>(header);
- }
- inline bool QProtobufSerializerPrivate::decodeHeader(QProtobufSelfcheckIterator &it, int &fieldIndex, WireTypes &wireType)
- {
- uint32_t header = deserializeVarintCommon<uint32_t>(it);
- wireType = static_cast<WireTypes>(header & 0b00000111);
- fieldIndex = header >> 3;
- constexpr int maxFieldIndex = (1 << 29) - 1;
- return fieldIndex <= maxFieldIndex && fieldIndex > 0 && (wireType == Varint
- || wireType == Fixed64
- || wireType == Fixed32
- || wireType == LengthDelimited);
- }
- QT_END_NAMESPACE
|