Browse Source

Improve serializer interface

- Align serialize/deserializer common functions names
- Align interface for serialization of length-delimited user types
Alexey Edelev 5 years ago
parent
commit
3542ffb403

+ 3 - 3
src/protobuf/qabstractprotobufserializer.h

@@ -127,7 +127,7 @@ public:
      * \param[in] metaObject Meta object information for given \a object
      * \return Raw serialized data represented as byte array
      */
-    virtual QByteArray serializeObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const = 0;
+    virtual QByteArray serializeObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject, int fieldIndex) const = 0;
 
     /*!
      * \brief deserializeObject Deserializes buffer to an \a object
@@ -194,8 +194,8 @@ public:
  */
 template<typename T>
 static void qRegisterProtobufType() {
-    QtProtobufPrivate::registerHandler(qMetaTypeId<T *>(), { QtProtobufPrivate::serializeComplexType<T>,
-            QtProtobufPrivate::deserializeComplexType<T>, QtProtobuf::LengthDelimited });
+    QtProtobufPrivate::registerHandler(qMetaTypeId<T *>(), { QtProtobufPrivate::serializeObject<T>,
+            QtProtobufPrivate::deserializeObject<T>, QtProtobuf::LengthDelimited });
     QtProtobufPrivate::registerHandler(qMetaTypeId<QList<QSharedPointer<T>>>(), { QtProtobufPrivate::serializeList<T>,
             QtProtobufPrivate::deserializeList<T>, QtProtobuf::LengthDelimited });
 }

+ 7 - 13
src/protobuf/qabstractprotobufserializer_p.h

@@ -44,7 +44,7 @@ namespace QtProtobufPrivate {
 //! \private
 constexpr int NotUsedFieldIndex = -1;
 
-using Serializer = std::function<QByteArray(const QtProtobuf::QAbstractProtobufSerializer *, const QVariant &, int &)>;
+using Serializer = std::function<QByteArray(const QtProtobuf::QAbstractProtobufSerializer *, const QVariant &, int)>;
 /*!
  * \brief Deserializer is interface function for deserialize method
  */
@@ -69,9 +69,9 @@ extern Q_PROTOBUF_EXPORT void registerHandler(int userType, const SerializationH
  */
 template <typename T,
           typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
-QByteArray serializeComplexType(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, int &/*outFieldIndex*/) {
+QByteArray serializeObject(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, int outFieldIndex) {
     Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "serializer set is not setup");
-    return serializer->serializeObject(value.value<T *>(), T::propertyOrdering, T::staticMetaObject);
+    return serializer->serializeObject(value.value<T *>(), T::propertyOrdering, T::staticMetaObject, outFieldIndex);
 }
 
 /*!
@@ -81,14 +81,13 @@ QByteArray serializeComplexType(const QtProtobuf::QAbstractProtobufSerializer *s
  */
 template<typename V,
          typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
-QByteArray serializeList(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &listValue, int &outFieldIndex) {
+QByteArray serializeList(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &listValue, int outFieldIndex) {
     Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "serializer set is not setup");
     QList<QSharedPointer<V>> list = listValue.value<QList<QSharedPointer<V>>>();
 
     qProtoDebug() << __func__ << "listValue.count" << list.count() << "outFiledIndex" << outFieldIndex;
 
     if (list.count() <= 0) {
-        outFieldIndex = NotUsedFieldIndex;
         return QByteArray();
     }
 
@@ -100,9 +99,6 @@ QByteArray serializeList(const QtProtobuf::QAbstractProtobufSerializer *serializ
         }
         serializedList.append(serializer->serializeListObject(value.data(), V::propertyOrdering, V::staticMetaObject, outFieldIndex));
     }
-
-    outFieldIndex = NotUsedFieldIndex;
-
     return serializedList;
 }
 
@@ -113,7 +109,7 @@ QByteArray serializeList(const QtProtobuf::QAbstractProtobufSerializer *serializ
  */
 template<typename K, typename V,
          typename std::enable_if_t<!std::is_base_of<QObject, V>::value, int> = 0>
-QByteArray serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, int &outFieldIndex) {
+QByteArray serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, int outFieldIndex) {
     Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "serializer set is not setup");
     QMap<K,V> mapValue = value.value<QMap<K,V>>();
     using ItType = typename QMap<K,V>::const_iterator;
@@ -122,7 +118,6 @@ QByteArray serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serialize
     for ( ItType it = mapValue.constBegin(); it != mapValue.constEnd(); it++) {
         mapResult.append(serializer->serializeMapPair(QVariant::fromValue<K>(it.key()), QVariant::fromValue<V>(it.value()), outFieldIndex));
     }
-    outFieldIndex = NotUsedFieldIndex;
     return mapResult;
 }
 
@@ -134,7 +129,7 @@ QByteArray serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serialize
  */
 template<typename K, typename V,
          typename std::enable_if_t<std::is_base_of<QObject, V>::value, int> = 0>
-QByteArray serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, int &outFieldIndex) {
+QByteArray serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serializer, const QVariant &value, int outFieldIndex) {
     Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "serializer set is not setup");
     QMap<K, QSharedPointer<V>> mapValue = value.value<QMap<K, QSharedPointer<V>>>();
     using ItType = typename QMap<K, QSharedPointer<V>>::const_iterator;
@@ -147,7 +142,6 @@ QByteArray serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serialize
         }
         mapResult.append(serializer->serializeMapPair(QVariant::fromValue<K>(it.key()), QVariant::fromValue<V *>(it.value().data()), outFieldIndex));
     }
-    outFieldIndex = NotUsedFieldIndex;
     return mapResult;
 }
 
@@ -158,7 +152,7 @@ QByteArray serializeMap(const QtProtobuf::QAbstractProtobufSerializer *serialize
  */
 template <typename T,
           typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
-void deserializeComplexType(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &to) {
+void deserializeObject(const QtProtobuf::QAbstractProtobufSerializer *serializer, QtProtobuf::QProtobufSelfcheckIterator &it, QVariant &to) {
     Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "serializer set is not setup");
     T *value = new T;
     serializer->deserializeObject(value, it, T::propertyOrdering, T::staticMetaObject);

+ 1 - 1
src/protobuf/qprotobufjsonserializer.cpp

@@ -86,7 +86,7 @@ void QProtobufJsonSerializer::deserializeMessage(QObject *object, const QByteArr
     Q_UNUSED(metaObject)
 }
 
-QByteArray QProtobufJsonSerializer::serializeObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const
+QByteArray QProtobufJsonSerializer::serializeObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject, int /*fieldIndex*/) const
 {
     QByteArray result = "{";
     result.append(serializeMessage(object, propertyOrdering, metaObject));

+ 1 - 1
src/protobuf/qprotobufjsonserializer.h

@@ -44,7 +44,7 @@ protected:
     QByteArray serializeMessage(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const  override;
     void deserializeMessage(QObject *object, const QByteArray &data, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const override;
 
-    QByteArray serializeObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const override;
+    QByteArray serializeObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject, int fieldIndex) const override;
     void deserializeObject(QObject *object, QProtobufSelfcheckIterator &it, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const override;
 
     QByteArray serializeListObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject, int fieldIndex) const override;

+ 9 - 10
src/protobuf/qprotobufserializer.cpp

@@ -90,9 +90,11 @@ void QProtobufSerializer::deserializeMessage(QObject *object, const QByteArray &
     }
 }
 
-QByteArray QProtobufSerializer::serializeObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const
+QByteArray QProtobufSerializer::serializeObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject, int fieldIndex) const
 {
-    return QProtobufSerializerPrivate::prependLengthDelimitedSize(serializeMessage(object, propertyOrdering, metaObject));
+    QByteArray result = QProtobufSerializerPrivate::encodeHeader(fieldIndex, LengthDelimited);
+    result.append(QProtobufSerializerPrivate::prependLengthDelimitedSize(serializeMessage(object, propertyOrdering, metaObject)));
+    return result;
 }
 
 void QProtobufSerializer::deserializeObject(QObject *object, QProtobufSelfcheckIterator &it, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const
@@ -103,9 +105,7 @@ void QProtobufSerializer::deserializeObject(QObject *object, QProtobufSelfcheckI
 
 QByteArray QProtobufSerializer::serializeListObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject, int fieldIndex) const
 {
-    QByteArray result = QProtobufSerializerPrivate::encodeHeader(fieldIndex, LengthDelimited);
-    result.append(serializeObject(object, propertyOrdering, metaObject));
-    return result;
+    return serializeObject(object, propertyOrdering, metaObject, fieldIndex);
 }
 
 void QProtobufSerializer::deserializeListObject(QObject *object, QProtobufSelfcheckIterator &it, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const
@@ -217,16 +217,15 @@ QByteArray QProtobufSerializerPrivate::serializeProperty(const QVariant &propert
     if (basicIt != QProtobufSerializerPrivate::handlers.end()) {
         type = basicIt->second.type;
         result.append(basicIt->second.serializer(propertyValue, fieldIndex));
+        if (fieldIndex != QtProtobufPrivate::NotUsedFieldIndex
+                && type != UnknownWireType) {
+            result.prepend(QProtobufSerializerPrivate::encodeHeader(fieldIndex, type));
+        }
     } else {
         auto &handler = QtProtobufPrivate::findHandler(userType);
         type = handler.type;
         result.append(handler.serializer(q_ptr, propertyValue, fieldIndex));//throws if not implemented
     }
-
-    if (fieldIndex != QtProtobufPrivate::NotUsedFieldIndex
-            && type != UnknownWireType) {
-        result.prepend(QProtobufSerializerPrivate::encodeHeader(fieldIndex, type));
-    }
     return result;
 }
 

+ 1 - 1
src/protobuf/qprotobufserializer.h

@@ -45,7 +45,7 @@ protected:
     QByteArray serializeMessage(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const override;
     void deserializeMessage(QObject *object, const QByteArray &data, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const override;
 
-    QByteArray serializeObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const override;
+    QByteArray serializeObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject, int fieldIndex) const override;
     void deserializeObject(QObject *object, QProtobufSelfcheckIterator &it, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject) const override;
 
     QByteArray serializeListObject(const QObject *object, const QProtobufPropertyOrdering &propertyOrdering, const QMetaObject &metaObject, int fieldIndex) const override;