Browse Source

Fix enum properties generation

- Few generated code alignements
- QString now passed by const reference
Alexey Edelev 6 years ago
parent
commit
1f62a4c565
2 changed files with 19 additions and 12 deletions
  1. 16 9
      src/generator/generator.cpp
  2. 3 3
      src/generator/templates.h

+ 16 - 9
src/generator/generator.cpp

@@ -70,7 +70,7 @@ static std::unordered_map<FieldDescriptor::Type, std::string> TypeReflection = {
 //    {FieldDescriptor::TYPE_MESSAGE, ""},//Custom typename
     {FieldDescriptor::TYPE_BYTES, "QByteArray"},
     {FieldDescriptor::TYPE_UINT32, "int"},//Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
-    {FieldDescriptor::TYPE_ENUM, "$custom_enumtype$"},
+//    {FieldDescriptor::TYPE_ENUM, ""},//Custom typename
     {FieldDescriptor::TYPE_SFIXED32, "int"},
     //        {FieldDescriptor::TYPE_SFIXED64, "int"},//Not supported see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
     {FieldDescriptor::TYPE_SINT32, "int"},
@@ -148,20 +148,21 @@ private:
         printQEnums();
 
         //public section
-        mPrinter.Print("public:\n");
+        printPublic();
         printConstructor();
         for(int i = 0; i < mMessage->field_count(); i++) {
             printField(mMessage->field(i), GetterTemplate);
         }
         for(int i = 0; i < mMessage->field_count(); i++) {
             auto field = mMessage->field(i);
-            if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
+            if (field->type() == FieldDescriptor::TYPE_MESSAGE
+                    || field->type() == FieldDescriptor::TYPE_STRING) {
                 printField(field, SetterTemplateComplexType);
             } else {
                 printField(field, SetterTemplateSimpleType);
             }
         }
-        mPrinter.Print("signals:\n");
+        mPrinter.Print("\nsignals:\n");
         for(int i = 0; i < mMessage->field_count(); i++) {
             printField(mMessage->field(i), SignalTemplate);
         }
@@ -186,6 +187,8 @@ private:
         string typeName;
         if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
             typeName = field->message_type()->name();
+        } else if (field->type() == FieldDescriptor::TYPE_ENUM) {
+            typeName = field->enum_type()->name();
         } else {
             auto it = TypeReflection.find(field->type());
             if(it != std::end(TypeReflection)) {
@@ -210,8 +213,8 @@ private:
             const auto enumDescr = mMessage->enum_type(i);
             mPrinter.Print(enumDescr->name().c_str());
         }
-        mPrinter.Print(")\n\n");
-        mPrinter.Print("public:\n");
+        mPrinter.Print(")\n");
+        printPublic();
         for(int i = 0; mMessage->enum_type_count(); i++) {
             const auto enumDescr = mMessage->enum_type(i);
             mPrinter.Print({{"enum", enumDescr->name()}}, "    enum $enum$ {\n");
@@ -228,7 +231,7 @@ private:
         //FIXME: Explicit default values are not allowed in proto3 seems
         //this function is useless
         mPrinter.Print({{"classname", mMessage->name()}},
-                       "$classname$(QObject parent = nullptr) : QObject(parent)\n");
+                       "    $classname$(QObject parent = nullptr) : QObject(parent)\n");
         for (int i = 0; i < mMessage->field_count(); i++) {
             const FieldDescriptor* field = mMessage->field(i);
             std::string defaultValue;
@@ -264,11 +267,15 @@ private:
                 if (defaultValue.size() > 0) {
                     mPrinter.Print({{"property_name", field->camelcase_name()},
                                     {"default_value", defaultValue}},
-                                   ", $m_property_name$($default_value$)\n");
+                                   "    , $m_property_name$($default_value$)\n");
                 }
             }
         }
-        mPrinter.Print("{}\n\n");
+        mPrinter.Print("    {}\n\n");
+    }
+
+    void printPublic() {
+        mPrinter.Print("\npublic:\n");
     }
 };
 

+ 3 - 3
src/generator/templates.h

@@ -27,12 +27,12 @@
 
 namespace qtprotobuf {
 
-static const char* StartTemplate = "class $classname$ : public QObject\n"
+static const char* StartTemplate = "\nclass $classname$ : public QObject\n"
                       "{\n"
                       "    Q_OBJECT\n";
 
-static const char* NamespaceTemplate = "namespace $namespace$\n"
-                                  "{\n";
+static const char* NamespaceTemplate = "\nnamespace $namespace$\n"
+                                  "{\n\n";
 static const char* PropertyTemplate = "    Q_PROPERTY($type$ $property_name$ READ $property_name$ WRITE set$property_name_cap$ NOTIFY $property_name$Changed)\n";
 static const char* GetterTemplate = "    $type$ $property_name$() const {\n"
                                     "        return m_$property_name$;\n"