Explorar el Código

Implement enum serialization

- Fix memory leak
- Update type detection mechanism
- Make explicit serializer for QString
Alexey Edelev hace 6 años
padre
commit
bc7cce5265

+ 5 - 0
src/grpc/abstractclient.cpp

@@ -43,6 +43,11 @@ AbstractClient::AbstractClient(const QString &service, QObject *parent) : QObjec
 
 }
 
+AbstractClient::~AbstractClient()
+{
+    delete d;
+}
+
 void AbstractClient::attachChannel(std::shared_ptr<AbstractChannel> channel)
 {
     d->channel = channel;

+ 1 - 0
src/grpc/abstractclient.h

@@ -44,6 +44,7 @@ public:
 
 protected:
     AbstractClient(const QString &service, QObject *parent = nullptr);
+    virtual ~AbstractClient();
 
     template<typename A, typename R>
     bool call(const QString &method, const A &arg, R &ret) {

+ 18 - 11
src/protobuf/protobufobject.cpp

@@ -67,12 +67,14 @@ namespace {
     static const char *sfint64ListTypeName = "sfint64List";
 }
 
-QByteArray ProtobufObjectPrivate::serializeValue(const QVariant &propertyValue, int fieldIndex, const QLatin1Literal &typeName) const
+QByteArray ProtobufObjectPrivate::serializeValue(const QVariant &propertyValue, int fieldIndex, const QMetaProperty &metaProperty) const
 {
-    qProtoDebug() << __func__ << "propertyValue" << propertyValue << "fieldIndex" << fieldIndex << "typeName" << typeName;
+    QLatin1Literal typeName(metaProperty.typeName());
     QByteArray result;
     WireTypes type = UnknownWireType;
-    int resultSize = result.size();
+    qProtoDebug() << __func__ << "propertyValue" << propertyValue << "fieldIndex" << fieldIndex << "typeName"
+                  << typeName << static_cast<QMetaType::Type>(propertyValue.type());
+
     switch (static_cast<QMetaType::Type>(propertyValue.type())) {
     case QMetaType::Int:
         type = Varint;
@@ -86,7 +88,7 @@ QByteArray ProtobufObjectPrivate::serializeValue(const QVariant &propertyValue,
         } else {
             result.append(serializeVarint(propertyValue.toLongLong()));
         }
-        if (resultSize == result.size()) {
+        if (0 == result.size()) {
             fieldIndex = NotUsedFieldIndex;
         }
         break;
@@ -102,7 +104,7 @@ QByteArray ProtobufObjectPrivate::serializeValue(const QVariant &propertyValue,
         } else {
             result.append(serializeVarint(propertyValue.toLongLong()));
         }
-        if (resultSize == result.size()) {
+        if (0 == result.size()) {
             fieldIndex = NotUsedFieldIndex;
         }
         break;
@@ -116,7 +118,7 @@ QByteArray ProtobufObjectPrivate::serializeValue(const QVariant &propertyValue,
         break;
     case QMetaType::QString:
         type = LengthDelimited;
-        result.append(serializeLengthDelimited(propertyValue.toString().toUtf8()));
+        result.append(serializeLengthDelimited(propertyValue.toString()));
         break;
     case QMetaType::QByteArray:
         type = LengthDelimited;
@@ -131,8 +133,13 @@ QByteArray ProtobufObjectPrivate::serializeValue(const QVariant &propertyValue,
         result.append(serializeListType(propertyValue.value<QByteArrayList>(), fieldIndex));
         break;
     case QMetaType::User:
-        type = LengthDelimited;
-        result.append(serializeUserType(propertyValue, fieldIndex, typeName));
+        if(metaProperty.isEnumType()) {
+            type = Varint;
+            result.append(serializeVarint(propertyValue.toLongLong()));
+        } else {
+            type = LengthDelimited;
+            result.append(serializeUserType(propertyValue, fieldIndex, typeName));
+        }
         break;
     case QMetaType::UInt:
         if (typeName == fint32TypeNameP
@@ -143,7 +150,7 @@ QByteArray ProtobufObjectPrivate::serializeValue(const QVariant &propertyValue,
             type = Varint;
             result.append(serializeVarint(propertyValue.toUInt()));
         }
-        if (resultSize == result.size()) {
+        if (0 == result.size()) {
             fieldIndex = NotUsedFieldIndex;
         }
         break;
@@ -156,14 +163,14 @@ QByteArray ProtobufObjectPrivate::serializeValue(const QVariant &propertyValue,
             type = Varint;
             result.append(serializeVarint(propertyValue.toULongLong()));
         }
-        if (resultSize == result.size()) {
+        if (0 == result.size()) {
             fieldIndex = NotUsedFieldIndex;
         }
         break;
     case QMetaType::Bool:
         type = Varint;
         result.append(serializeVarint(propertyValue.toUInt()));
-        if (resultSize == result.size()) {
+        if (0 == result.size()) {
             fieldIndex = NotUsedFieldIndex;
         }
         break;

+ 1 - 1
src/protobuf/protobufobject.h

@@ -61,7 +61,7 @@ public:
             QMetaProperty metaProperty = T::staticMetaObject.property(propertyIndex);
             const char *propertyName = metaProperty.name();
             const QVariant &propertyValue = instance->property(propertyName);
-            result.append(serializeValue(propertyValue, fieldIndex, QLatin1Literal(metaProperty.typeName())));
+            result.append(serializeValue(propertyValue, fieldIndex, metaProperty));
         }
 
         return result;

+ 8 - 2
src/protobuf/protobufobject_p.h

@@ -72,7 +72,7 @@ public:
     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 serializeValue(const QVariant &propertyValue, int fieldIndex, const QMetaProperty &metaProperty) const;
     QByteArray serializeUserType(const QVariant &propertyValue, int &fieldIndex, const QLatin1Literal &typeName) const;
 
     void deserializeProperty(WireTypes wireType, const QMetaProperty &metaProperty, QByteArray::const_iterator &it);
@@ -81,6 +81,11 @@ public:
     //###########################################################################
     //                           Serialization helpers
     //###########################################################################
+    QByteArray serializeLengthDelimited(const QString &data) const {
+        qProtoDebug() << __func__ << "data.size" << data.size() << "data" << data;
+        return serializeLengthDelimited(data.toUtf8());
+    }
+
     QByteArray serializeLengthDelimited(const QByteArray &data) const {
         qProtoDebug() << __func__ << "data.size" << data.size() << "data" << data.toHex();
 
@@ -183,7 +188,8 @@ public:
 
         QByteArray serializedList;
         for (auto &value : listValue) {
-            serializedList.append(serializeValue(value, outFieldIndex, QLatin1Literal()));
+            serializedList.append(encodeHeaderByte(outFieldIndex, LengthDelimited));
+            serializedList.append(serializeLengthDelimited(value));
         }
 
         outFieldIndex = NotUsedFieldIndex;

+ 1 - 1
tests/CMakeLists.txt

@@ -11,7 +11,7 @@ add_custom_target(${testgeneration})
 add_custom_command(TARGET ${testgeneration}
         COMMAND ${Protobuf_PROTOC_EXECUTABLE} --plugin=protoc-gen-${PROJECT_NAME}=$<TARGET_FILE:${PROJECT_NAME}> --qtprotobuf_out=${TESTS_OUT_DIR} -I=${CMAKE_CURRENT_SOURCE_DIR}/proto/ ${PROTO_FILES}
         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/proto/
-        DEPENDS ${PROJECT_NAME} ${PROTO_FILES}
+        DEPENDS ${PROTO_FILES}
         COMMENT "Generating test headers"
 )
 add_dependencies(${testgeneration} ${PROJECT_NAME})

+ 3 - 3
tests/serializationtest.cpp

@@ -1627,13 +1627,13 @@ TEST_F(SerializationTest, BoolMessageSerializeTest)
     ASSERT_TRUE(result == QByteArray::fromHex(""));
 }
 
-TEST_F(SerializationTest, SimpleEnumMessageTest)
+TEST_F(SerializationTest, SimpleEnumMessageSerializeTest)
 {
     SimpleEnumMessage test;
-    test.setLocalEnum(SimpleEnumMessage::LOCAL_ENUM_VALUE0);
+    test.setLocalEnum(SimpleEnumMessage::LOCAL_ENUM_VALUE2);
     QByteArray result = test.serialize();
     ASSERT_EQ(2, result.size());
-    ASSERT_TRUE(result == QByteArray::fromHex("0801"));
+    ASSERT_TRUE(result == QByteArray::fromHex("0802"));
 }
 
 //TEST_F(SerializationTest, DISABLE_BenchmarkTest)