Browse Source

Minor code cleanup

- Fix keyword spacing
- Move reference and pointer indicators to property
Alexey Edelev 6 years ago
parent
commit
659a5abe4d

+ 1 - 1
src/generator/classsourcegeneratorbase.cpp

@@ -51,7 +51,7 @@ void ClassSourceGeneratorBase::printClassHeaderInclude()
 
 void ClassSourceGeneratorBase::printUsingNamespaces(const std::unordered_set<std::string> &namespaces)
 {
-    for(auto ns : namespaces) {
+    for (auto ns : namespaces) {
         mPrinter.Print({{"namespace", ns}}, Templates::UsingNamespaceTemplate);
     }
 }

+ 1 - 1
src/generator/clientgenerator.cpp

@@ -65,7 +65,7 @@ void ClientGenerator::printClientIncludes()
     std::unordered_set<std::string> includeSet;
     includeSet.insert("abstractclient");
     includeSet.insert("asyncreply");
-    for(auto type : includeSet) {
+    for (auto type : includeSet) {
         mPrinter.Print({{"include", type}}, Templates::InternalIncludeTemplate);
     }
 }

+ 1 - 1
src/generator/clientsourcegenerator.cpp

@@ -44,7 +44,7 @@ ClientSourceGenerator::ClientSourceGenerator(const google::protobuf::ServiceDesc
 
 void ClientSourceGenerator::printMethods()
 {
-    for(int i = 0; i < mService->method_count(); i++) {
+    for (int i = 0; i < mService->method_count(); i++) {
         const MethodDescriptor* method = mService->method(i);
         std::string inputTypeName = method->input_type()->full_name();
         std::string outputTypeName = method->output_type()->full_name();

+ 2 - 2
src/generator/generator.cpp

@@ -56,7 +56,7 @@ bool QtGenerator::Generate(const FileDescriptor *file,
 
     std::set<std::string> extractedModels;
 
-    for(int i = 0; i < file->message_type_count(); i++) {
+    for (int i = 0; i < file->message_type_count(); i++) {
         const Descriptor *message = file->message_type(i);
         std::string baseFilename(message->name());
         utils::tolower(baseFilename);
@@ -75,7 +75,7 @@ bool QtGenerator::Generate(const FileDescriptor *file,
         classSourceGen.run();
     }
 
-    for(int i = 0; i < file->service_count(); i++) {
+    for (int i = 0; i < file->service_count(); i++) {
         const ServiceDescriptor* service = file->service(i);
         std::string baseFilename(service->name());
         utils::tolower(baseFilename);

+ 3 - 3
src/generator/protobufclassgenerator.cpp

@@ -265,7 +265,7 @@ std::string ProtobufClassGenerator::getTypeName(const FieldDescriptor *field)
         EnumVisibility visibility = getEnumVisibility(field);
         if (visibility == LOCAL_ENUM) {
             typeName = typeName.append(enumType->name());
-        } else if(visibility == GLOBAL_ENUM) {
+        } else if (visibility == GLOBAL_ENUM) {
             typeName = namespaceTypeName.append(Templates::GlobalEnumClassNameTemplate)
                     .append("::").append(enumType->name());
         } else {
@@ -287,7 +287,7 @@ std::string ProtobufClassGenerator::getTypeName(const FieldDescriptor *field)
                 typeName = typeName.append(it->second);
             }
             if (field->is_repeated()) {
-                if(field->type() == FieldDescriptor::TYPE_FLOAT
+                if (field->type() == FieldDescriptor::TYPE_FLOAT
                         || field->type() == FieldDescriptor::TYPE_DOUBLE) {
                     typeName[0] = ::toupper(typeName[0]);
                     typeName = namespaceQtProtoDefinition.append(typeName);
@@ -430,7 +430,7 @@ ProtobufClassGenerator::EnumVisibility ProtobufClassGenerator::getEnumVisibility
 
     for (int i = 0; i < enumFile->message_type_count(); i++) {
         const Descriptor* msg = enumFile->message_type(i);
-        for(int j = 0; j < msg->enum_type_count(); j++) {
+        for (int j = 0; j < msg->enum_type_count(); j++) {
             if (enumType->full_name() == msg->enum_type(j)->full_name()) {
                 return NEIGHBOUR_ENUM;
             }

+ 3 - 3
src/generator/servicegeneratorbase.cpp

@@ -52,7 +52,7 @@ void ServiceGeneratorBase::printIncludes()
 {
     std::unordered_set<std::string> includeSet;
 
-    for(int i = 0; i < mService->method_count(); i++) {
+    for (int i = 0; i < mService->method_count(); i++) {
         const MethodDescriptor* method = mService->method(i);
         std::string inputTypeName = method->input_type()->name();
         std::string outputTypeName = method->output_type()->name();
@@ -62,7 +62,7 @@ void ServiceGeneratorBase::printIncludes()
         includeSet.insert(outputTypeName);
     }
 
-    for(auto type : includeSet) {
+    for (auto type : includeSet) {
         mPrinter.Print({{"include", type}}, Templates::InternalIncludeTemplate);
     }
 }
@@ -75,7 +75,7 @@ void ServiceGeneratorBase::printClassName()
 void ServiceGeneratorBase::printMethodsDeclaration(const char* methodTemplate, const char* methodAsyncTemplate)
 {
     Indent();
-    for(int i = 0; i < mService->method_count(); i++) {
+    for (int i = 0; i < mService->method_count(); i++) {
         const MethodDescriptor* method = mService->method(i);
         std::string inputTypeName = method->input_type()->full_name();
         std::string outputTypeName = method->output_type()->full_name();

+ 9 - 8
src/protobuf/protobufobject.cpp

@@ -29,8 +29,8 @@ using namespace qtprotobuf;
 
 ProtobufObjectPrivate::SerializerRegistry ProtobufObjectPrivate::serializers = {};
 
-
-QByteArray ProtobufObjectPrivate::serializeValue(const QVariant& propertyValue, int fieldIndex, const QLatin1Literal& typeName) const {
+QByteArray ProtobufObjectPrivate::serializeValue(const QVariant &propertyValue, int fieldIndex, const QLatin1Literal &typeName) const
+{
     qProtoDebug() << __func__ << "propertyValue" << propertyValue << "fieldIndex" << fieldIndex << "typeName" << typeName;
     QByteArray result;
     WireTypes type = UnknownWireType;
@@ -139,7 +139,8 @@ QByteArray ProtobufObjectPrivate::serializeValue(const QVariant& propertyValue,
     return result;
 }
 
-QByteArray ProtobufObjectPrivate::serializeUserType(const QVariant &propertyValue, int &fieldIndex) const {
+QByteArray ProtobufObjectPrivate::serializeUserType(const QVariant &propertyValue, int &fieldIndex) const
+{
     qProtoDebug() << __func__ << "propertyValue" << propertyValue << "fieldIndex" << fieldIndex;
     int userType = propertyValue.userType();
 
@@ -154,11 +155,11 @@ QByteArray ProtobufObjectPrivate::serializeUserType(const QVariant &propertyValu
         return serializeListType(propertyValue.value<int32List>(), fieldIndex);
     }
 
-    if(userType == qMetaTypeId<FloatList>()) {
+    if (userType == qMetaTypeId<FloatList>()) {
         return serializeListType(propertyValue.value<FloatList>(), fieldIndex);
     }
 
-    if(userType == qMetaTypeId<DoubleList>()) {
+    if (userType == qMetaTypeId<DoubleList>()) {
         return serializeListType(propertyValue.value<DoubleList>(), fieldIndex);
     }
 
@@ -176,7 +177,7 @@ void ProtobufObjectPrivate::deserializeProperty(WireTypes wireType, const QMetaP
     qProtoDebug() << __func__ << " wireType: " << wireType << " metaProperty: " << typeName << "currentByte:" << QString::number((*it), 16);
     QVariant newPropertyValue;
     int type = metaProperty.type();
-    switch(type) {
+    switch (type) {
     case QMetaType::UInt:
         if (wireType == Fixed32) {
             newPropertyValue = deserializeFixed<fint32>(it);
@@ -280,9 +281,9 @@ void ProtobufObjectPrivate::deserializeUserType(const QMetaProperty &metaType, Q
     } else if (userType == qMetaTypeId<uint64List>()) {
         //TODO: Check if type is fixed
         newValue = deserializeVarintListType<uint64>(it);
-    } else if(userType == qMetaTypeId<FloatList>()) {
+    } else if (userType == qMetaTypeId<FloatList>()) {
         newValue = deserializeListType<float>(it);
-    } else if(userType == qMetaTypeId<DoubleList>()) {
+    } else if (userType == qMetaTypeId<DoubleList>()) {
         newValue = deserializeListType<double>(it);
     } else {
         newValue = deserializeProtobufObjectType(userType, it);

+ 4 - 6
src/protobuf/protobufobject.h

@@ -51,8 +51,9 @@ public:
 
     QByteArray serialize() const {
         qProtoDebug() << T::staticMetaObject.className() << "serialize";
+
         QByteArray result;
-        const T *instance = dynamic_cast<const T *>(this);
+        const QObject *instance = static_cast<const QObject *>(this);
         for (auto field : T::propertyOrdering) {
             int propertyIndex = field.second;
             int fieldIndex = field.first;
@@ -60,8 +61,6 @@ public:
             QMetaProperty metaProperty = T::staticMetaObject.property(propertyIndex);
             const char *propertyName = metaProperty.name();
             const QVariant &propertyValue = instance->property(propertyName);
-            //TODO: flag isFixed looks ugly. Need to define more effective strategy
-            //for type detection.
             result.append(serializeValue(propertyValue, fieldIndex, QLatin1Literal(metaProperty.typeName())));
         }
 
@@ -70,7 +69,6 @@ public:
 
     void deserialize(const QByteArray &array) {
         qProtoDebug() << T::staticMetaObject.className() << "deserialize";
-        //T *instance = dynamic_cast<T *>(this);
 
         for (QByteArray::const_iterator it = array.begin(); it != array.end();) {
             //Each iteration we expect iterator is setup to beginning of next chunk
@@ -121,12 +119,12 @@ private:
 //        return value->serialize();
 //    }
 
-    static QByteArray serializeComplexListType(const ProtobufObjectPrivate* serializer, const QVariant &listValue, int &outFieldIndex) {
+    static QByteArray serializeComplexListType(const ProtobufObjectPrivate *serializer, const QVariant &listValue, int &outFieldIndex) {
         QList<T> list = listValue.value<QList<T>>();
         return serializer->serializeListType(list, outFieldIndex);
     }
 
-    static void deserializeComplexListType(ProtobufObjectPrivate* deserializer, QByteArray::const_iterator &it, QVariant &previous) {
+    static void deserializeComplexListType(ProtobufObjectPrivate *deserializer, QByteArray::const_iterator &it, QVariant &previous) {
         QList<T> previousList = previous.value<QList<T>>();
         QVariant newMember = deserializer->deserializeListType<T>(it);
         previousList.append(newMember.value<T>());

+ 20 - 15
src/protobuf/protobufobject_p.h

@@ -75,12 +75,15 @@ 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 QLatin1Literal &typeName) const;
     QByteArray serializeUserType(const QVariant &propertyValue, int &fieldIndex) 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
@@ -194,7 +197,7 @@ public:
 
         //Use ZigZag convertion first and apply unsigned variant next
         value = (value << 1) ^ (value >> (sizeof(UV) * 8 - 1));
-        uValue = *(UV *)&value;
+        uValue = static_cast<UV>(value);
         return serializeVarint(uValue);
     }
 
@@ -235,9 +238,9 @@ public:
         return result;
     }
 
-    //#####################################################################
-    //                           Deserialization
-    //#####################################################################
+    //###########################################################################
+    //                           Deserialization helpers
+    //###########################################################################
 
     template <typename V,
               typename std::enable_if_t<std::is_floating_point<V>::value
@@ -312,14 +315,14 @@ public:
     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) {
+    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) {
+    QVariant deserializeListType(QByteArray::const_iterator &it) {
         qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
         return deserializeProtobufObjectType(qMetaTypeId<V>(), it);
     }
@@ -328,7 +331,7 @@ public:
               typename std::enable_if_t<std::is_floating_point<V>::value
                                         || std::is_same<V, unsigned int>::value
                                         || std::is_same<V, qulonglong>::value, int> = 0>
-    QVariant deserializeListType(QByteArray::const_iterator& it) {
+    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);
@@ -340,7 +343,7 @@ public:
     }
 
     template <typename V>
-    QVariant deserializeVarintListType(QByteArray::const_iterator& it) {
+    QVariant deserializeVarintListType(QByteArray::const_iterator &it) {
         qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
         QList<V> out;
         unsigned int count = deserializeVarint<unsigned int>(it).toUInt();
@@ -353,7 +356,7 @@ public:
     }
 
     template <typename V>
-    QVariant deserializeVarintListTypeZigZag(QByteArray::const_iterator& it) {
+    QVariant deserializeVarintListTypeZigZag(QByteArray::const_iterator &it) {
         qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
         QList<V> out;
         unsigned int count = deserializeVarint<unsigned int>(it).toUInt();
@@ -367,16 +370,18 @@ public:
 };
 
 /*  Header byte
- *  bits    | 7  6  5  4  3 |  2  1  0
+ *  bits    | 7  6  5  4  3 | 2  1  0
  *  -----------------------------------
- *  meaning |  Field index  |   Type
+ *  meaning |  Field index  |  Type
  */
-unsigned char ProtobufObjectPrivate::encodeHeaderByte(int fieldIndex, WireTypes wireType) {
+unsigned char ProtobufObjectPrivate::encodeHeaderByte(int fieldIndex, WireTypes wireType)
+{
     unsigned char header = (fieldIndex << 3) | wireType;
-    return *(char *)&header;
+    return static_cast<char>(header);
 }
 
-bool ProtobufObjectPrivate::decodeHeaderByte(unsigned char typeByte, int &fieldIndex, WireTypes &wireType) {
+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

+ 1 - 1
tests/serializationtest.cpp

@@ -1504,7 +1504,7 @@ TEST_F(SerializationTest, BoolMessageSerializeTest)
 //TEST_F(SerializationTest, DISABLE_BenchmarkTest)
 //{
 //    SimpleIntMessage msg;
-//    for(int i = INT16_MIN; i < INT16_MAX; i++) {
+//    for (int i = INT16_MIN; i < INT16_MAX; i++) {
 //        msg.setTestFieldInt(i);
 //        msg.serialize();
 //    }