浏览代码

Move contructor of generated class to source file

- Moving constructor of generated class fixes issue #135
- Add named namespaces to static initializers
Alexey Edelev 5 年之前
父节点
当前提交
084893a804

+ 0 - 7
src/generator/protobufclassgenerator.cpp

@@ -317,13 +317,6 @@ void ProtobufClassGenerator::printConstructor()
     mPrinter.Print({{"classname", mClassName},
                     {"parameter_list", parameterList}}, Templates::ProtoConstructorTemplate);
 
-    for (int i = 0; i < mMessage->field_count(); i++) {
-        const FieldDescriptor *field = mMessage->field(i);
-        std::string fieldName = field->name();
-        fieldName[0] =  static_cast<char>(::tolower(fieldName[0]));
-        mPrinter.Print({{"property_name", fieldName}}, Templates::PropertyInitializerTemplate);
-    }
-    mPrinter.Print(Templates::ConstructorContentTemplate);
 }
 
 void ProtobufClassGenerator::printMaps()

+ 53 - 2
src/generator/protobufsourcegenerator.cpp

@@ -81,9 +81,7 @@ void ProtobufSourceGenerator::printRegisterBody()
     mPrinter.Print({{"classname", mClassName}}, Templates::RegisterSerializersTemplate);
     Outdent();
     mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
-    Indent();
     printRegistrationHelperInvokation();
-    Outdent();
     mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
 }
 
@@ -109,3 +107,56 @@ void ProtobufSourceGenerator::printRegistrationHelperInvokation()
 {
     mPrinter.Print(Templates::RegistratorTemplate);
 }
+
+void ProtobufSourceGenerator::printConstructor()
+{
+    std::string parameterList;
+    for (int i = 0; i < mMessage->field_count(); i++) {
+        const FieldDescriptor *field = mMessage->field(i);
+        std::string fieldTypeName = getTypeName(field, mMessage);
+        std::string fieldName = field->name();
+        fieldName[0] = static_cast<char>(::tolower(fieldName[0]));
+        if (field->is_repeated() || field->is_map()) {
+            parameterList += "const " + fieldTypeName + " &" + fieldName;
+        } else {
+            switch (field->type()) {
+            case FieldDescriptor::TYPE_DOUBLE:
+            case FieldDescriptor::TYPE_FLOAT:
+                parameterList += fieldTypeName + " " + fieldName;
+                break;
+            case FieldDescriptor::TYPE_FIXED32:
+            case FieldDescriptor::TYPE_FIXED64:
+            case FieldDescriptor::TYPE_INT32:
+            case FieldDescriptor::TYPE_INT64:
+            case FieldDescriptor::TYPE_SINT32:
+            case FieldDescriptor::TYPE_SINT64:
+            case FieldDescriptor::TYPE_UINT32:
+            case FieldDescriptor::TYPE_UINT64:
+                parameterList += fieldTypeName + " " + fieldName;
+                break;
+            case FieldDescriptor::TYPE_BOOL:
+                parameterList += fieldTypeName + " " + fieldName;
+                break;
+            case FieldDescriptor::TYPE_BYTES:
+            case FieldDescriptor::TYPE_STRING:
+            case FieldDescriptor::TYPE_MESSAGE:
+                parameterList += "const " + fieldTypeName + " &" + fieldName;
+                break;
+            default:
+                parameterList += fieldTypeName + " " + fieldName;
+                break;
+            }
+        }
+        parameterList += ", ";
+    }
+    mPrinter.Print({{"classname", mClassName},
+                    {"parameter_list", parameterList}}, Templates::ProtoConstructorDefinitionTemplate);
+
+    for (int i = 0; i < mMessage->field_count(); i++) {
+        const FieldDescriptor *field = mMessage->field(i);
+        std::string fieldName = field->name();
+        fieldName[0] =  static_cast<char>(::tolower(fieldName[0]));
+        mPrinter.Print({{"property_name", fieldName}}, Templates::PropertyInitializerTemplate);
+    }
+    mPrinter.Print(Templates::ConstructorContentTemplate);
+}

+ 2 - 0
src/generator/protobufsourcegenerator.h

@@ -38,6 +38,7 @@ public:
     void printRegisterBody();
     void printFieldsOrdering();
     void printRegistrationHelperInvokation();
+    void printConstructor();
 
     void run() override {
         printClassHeaderInclude();
@@ -45,6 +46,7 @@ public:
         printNamespaces();
         printFieldsOrdering();
         printRegisterBody();
+        printConstructor();
         encloseNamespaces();
     }
 };

+ 4 - 3
src/generator/templates.cpp

@@ -45,14 +45,14 @@ const char *Templates::ExternalIncludeTemplate = "#include <$include$>\n";
 const char *Templates::GlobalEnumIncludeTemplate = "#include <globalenums.h>\n";
 
 const char *Templates::UsingQtProtobufNamespaceTemplate = "\nusing namespace qtprotobuf;\n";
-const char *Templates::ComplexTypeRegistrationTemplate = "namespace {\n"
+const char *Templates::ComplexTypeRegistrationTemplate = "namespace $classname$Private {\n"
                                                          "void registerTypes()\n{\n"
                                                          "    qRegisterMetaType<$classname$>(\"$classname$\");\n"
                                                          "    qRegisterMetaType<$classname$List>(\"$classname$List\");\n"
                                                          "    qRegisterMetaType<$classname$>(\"$namespaces$::$classname$\");\n"
                                                          "    qRegisterMetaType<$classname$List>(\"$namespaces$::$classname$List\");\n"
                                                          "";
-const char *Templates::ComplexGlobalEnumRegistrationTemplate = "namespace {\n"
+const char *Templates::ComplexGlobalEnumRegistrationTemplate = "namespace $classname$Private {\n"
                                                                "void registerTypes()\n{\n";
 const char *Templates::ComplexGlobalEnumFieldRegistrationTemplate = "qRegisterMetaType<$classname$::$enum$>(\"$namespaces$::$classname$::$enum$\");\n";
 const char *Templates::ComplexListTypeUsingTemplate = "using $classname$List = QList<QSharedPointer<$classname$>>;\n";
@@ -82,7 +82,8 @@ const char *Templates::PublicBlockTemplate = "\npublic:\n";
 const char *Templates::PrivateBlockTemplate = "\nprivate:\n";
 const char *Templates::EnumDefinitionTemplate = "enum $enum$ {\n";
 const char *Templates::EnumFieldTemplate = "$enumvalue$ = $value$,\n";
-const char *Templates::ProtoConstructorTemplate = "$classname$($parameter_list$QObject *parent = nullptr) : QObject(parent)";
+const char *Templates::ProtoConstructorTemplate = "$classname$($parameter_list$QObject *parent = nullptr);\n";
+const char *Templates::ProtoConstructorDefinitionTemplate = "$classname$::$classname$($parameter_list$QObject *parent) : QObject(parent)";
 const char *Templates::ConstructorTemplate = "$classname$();\n";
 const char *Templates::ConstructorHeaderTemplate = "$classname$(){}\n";
 const char *Templates::CopyConstructorTemplate = "$classname$(const $classname$ &other) : QObject() {\n";

+ 1 - 0
src/generator/templates.h

@@ -67,6 +67,7 @@ public:
     static const char *EnumDefinitionTemplate;
     static const char *EnumFieldTemplate;
     static const char *ProtoConstructorTemplate;
+    static const char *ProtoConstructorDefinitionTemplate;
     static const char *ConstructorTemplate;
     static const char *CopyConstructorTemplate;
     static const char *MoveConstructorTemplate;