Sfoglia il codice sorgente

Replace mName by mTypeMap

- mName could be covered by mTypeMap. It doesn't make sense to
  keep this property in common generator class.
- Keep mName for gRPC generator, since it doesn't have mTypeMap
Alexey Edelev 3 anni fa
parent
commit
f4eb55accc

+ 2 - 2
src/generator/descriptorprinterbase.h

@@ -49,7 +49,7 @@ class DescriptorPrinterBase : public BasePrinter
 public:
     DescriptorPrinterBase(const T* descriptor, const std::shared_ptr<::google::protobuf::io::Printer> &printer) : BasePrinter(printer)
       , mDescriptor(descriptor)
-      , mName(utils::upperCaseName(descriptor->name()))
+      , mName(utils::upperCaseName(descriptor->name()))// TODO: migrate to typemaps for the client and service generators
     {}
     virtual ~DescriptorPrinterBase() = default;
 public:
@@ -77,7 +77,7 @@ public:
 
 protected:
     const T* mDescriptor;
-    std::string mName;
+    std::string mName;// TODO: migrate to typemaps for the client and service generators
     TypeMap mTypeMap;
 };
 

+ 5 - 6
src/generator/enumdeclarationprinter.cpp

@@ -33,7 +33,6 @@ EnumDeclarationPrinter::EnumDeclarationPrinter(const EnumDescriptor *descriptor,
     DescriptorPrinterBase<google::protobuf::EnumDescriptor>(descriptor, printer)
 {
     mTypeMap = common::produceEnumTypeMap(descriptor, nullptr);
-    mName += Templates::EnumClassSuffix;
 }
 
 void EnumDeclarationPrinter::startEnum()
@@ -42,7 +41,7 @@ void EnumDeclarationPrinter::startEnum()
     printEnumClass();
     printPublicBlock();
     Indent();
-    mPrinter->Print({{"classname", mName}}, Templates::ManualRegistrationDeclaration);
+    mPrinter->Print(mTypeMap, Templates::ManualRegistrationDeclaration);
     Outdent();
     printPrivateBlock();
     printConstructor();
@@ -72,9 +71,9 @@ void EnumDeclarationPrinter::printEnum()
 void EnumDeclarationPrinter::printConstructor()
 {
     Indent();
-    mPrinter->Print({{"classname", mName}}, Templates::ConstructorHeaderTemplate);
-    mPrinter->Print({{"classname", mName}}, Templates::DeletedCopyConstructorTemplate);
-    mPrinter->Print({{"classname", mName}}, Templates::DeletedMoveConstructorTemplate);
+    mPrinter->Print(mTypeMap, Templates::ConstructorHeaderTemplate);
+    mPrinter->Print(mTypeMap, Templates::DeletedCopyConstructorTemplate);
+    mPrinter->Print(mTypeMap, Templates::DeletedMoveConstructorTemplate);
     Outdent();
 }
 
@@ -92,5 +91,5 @@ void EnumDeclarationPrinter::encloseEnum()
 
 void EnumDeclarationPrinter::printEnumClass()
 {
-    mPrinter->Print({{"classname", mName}}, Templates::ClassDeclarationTemplate);
+    mPrinter->Print(mTypeMap, Templates::ClassDeclarationTemplate);
 }

+ 0 - 2
src/generator/enumdefinitionprinter.cpp

@@ -36,7 +36,6 @@ EnumDefinitionPrinter::EnumDefinitionPrinter(const google::protobuf::EnumDescrip
     DescriptorPrinterBase<google::protobuf::EnumDescriptor>(descriptor, printer)
 {
     mTypeMap = common::produceEnumTypeMap(descriptor, nullptr);
-    mName += Templates::EnumClassSuffix;
 }
 
 void EnumDefinitionPrinter::run() {
@@ -48,7 +47,6 @@ void EnumDefinitionPrinter::run() {
 void EnumDefinitionPrinter::printRegisterBody()
 {
     auto typeMap = mTypeMap;
-    typeMap["classname"] = mName;
     mPrinter->Print(typeMap, Templates::EnumRegistrarTemplate);
     mPrinter->Print(typeMap, Templates::ManualRegistrationGlobalEnumDefinition);
     Indent();

+ 4 - 1
src/generator/generatorcommon.cpp

@@ -112,6 +112,7 @@ TypeMap common::produceMessageTypeMap(const ::Descriptor *type, const Descriptor
     std::string scopeListName = scopeNamespaces.empty() ? listName : (scopeNamespaces + "::" + listName);
 
     return {
+        {"classname", name},
         {"type", name},
         {"full_type", fullName},
         {"scope_type", scopeName},
@@ -160,6 +161,7 @@ TypeMap common::produceEnumTypeMap(const EnumDescriptor *type, const Descriptor
     }
 
     return {
+        {"classname", enumGadget},
         {"type", name},
         {"full_type", fullName},
         {"scope_type", scopeName},
@@ -318,9 +320,10 @@ PropertyMap common::producePropertyMap(const FieldDescriptor *field, const Descr
     propertyMap["property_name_cap"] = propertyNameCap;
     propertyMap["scriptable"] = scriptable;
 
+    auto scopeTypeMap = produceMessageTypeMap(scope, nullptr);
     propertyMap["key_type"] = "";
     propertyMap["value_type"] = "";
-    propertyMap["classname"] = scope != nullptr ? utils::upperCaseName(scope->name()) : "";
+    propertyMap["classname"] = scope != nullptr ? scopeTypeMap["classname"] : "";
     propertyMap["number"] = std::to_string(field->number());
 
     if (field->is_map()) {

+ 16 - 16
src/generator/messagedeclarationprinter.cpp

@@ -46,7 +46,7 @@ MessageDeclarationPrinter::MessageDeclarationPrinter(const Descriptor *message,
 void MessageDeclarationPrinter::printClassForwardDeclarationPrivate()
 {
     if (common::hasNestedMessages(mDescriptor)) {
-        mPrinter->Print({{"namespace", mName + Templates::QtProtobufNestedNamespace}}, Templates::NamespaceTemplate);
+        mPrinter->Print({{"namespace", mTypeMap["classname"] + Templates::QtProtobufNestedNamespace}}, Templates::NamespaceTemplate);
         common::iterateNestedMessages(mDescriptor, [this](const ::google::protobuf::Descriptor *nestedMessage) {
             MessageDeclarationPrinter nesterPrinter(nestedMessage, mPrinter);
             nesterPrinter.printClassForwardDeclarationPrivate();
@@ -54,8 +54,8 @@ void MessageDeclarationPrinter::printClassForwardDeclarationPrivate()
         mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
     }
 
-    mPrinter->Print({{"classname", mName}}, Templates::ProtoClassForwardDeclarationTemplate);
-    mPrinter->Print({{"classname", mName}}, Templates::ComplexListTypeUsingTemplate);
+    mPrinter->Print(mTypeMap, Templates::ProtoClassForwardDeclarationTemplate);
+    mPrinter->Print(mTypeMap, Templates::ComplexListTypeUsingTemplate);
 }
 
 void MessageDeclarationPrinter::printClassForwardDeclaration()
@@ -75,7 +75,7 @@ void MessageDeclarationPrinter::printClassDeclaration()
 
 void MessageDeclarationPrinter::printClassDeclarationPrivate()
 {
-    mPrinter->Print({{"namespace", mName + Templates::QtProtobufNestedNamespace}}, Templates::NamespaceTemplate);
+    mPrinter->Print({{"namespace", mTypeMap["classname"] + Templates::QtProtobufNestedNamespace}}, Templates::NamespaceTemplate);
     common::iterateNestedMessages(mDescriptor, [this](const ::google::protobuf::Descriptor *nestedMessage) {
         MessageDeclarationPrinter nesterPrinter(nestedMessage, mPrinter);
         nesterPrinter.printClassDeclarationPrivate();
@@ -92,29 +92,29 @@ void MessageDeclarationPrinter::printClassDeclarationPrivate()
 void MessageDeclarationPrinter::printCopyFunctionality()
 {
     assert(mDescriptor != nullptr);
-    mPrinter->Print({{"classname", mName}},
+    mPrinter->Print(mTypeMap,
                     Templates::CopyConstructorDeclarationTemplate);
 
-    mPrinter->Print({{"classname", mName}},
+    mPrinter->Print(mTypeMap,
                     Templates::AssignmentOperatorDeclarationTemplate);
 }
 
 void MessageDeclarationPrinter::printMoveSemantic()
 {
     assert(mDescriptor != nullptr);
-    mPrinter->Print({{"classname", mName}},
+    mPrinter->Print(mTypeMap,
                     Templates::MoveConstructorDeclarationTemplate);
 
-    mPrinter->Print({{"classname", mName}},
+    mPrinter->Print(mTypeMap,
                     Templates::MoveAssignmentOperatorDeclarationTemplate);
 }
 
 void MessageDeclarationPrinter::printComparisonOperators()
 {
     assert(mDescriptor != nullptr);
-    mPrinter->Print({{"classname", mName}}, Templates::EqualOperatorDeclarationTemplate);
+    mPrinter->Print(mTypeMap, Templates::EqualOperatorDeclarationTemplate);
 
-    mPrinter->Print({{"classname", mName}}, Templates::NotEqualOperatorDeclarationTemplate);
+    mPrinter->Print(mTypeMap, Templates::NotEqualOperatorDeclarationTemplate);
 }
 
 void MessageDeclarationPrinter::printConstructors()
@@ -132,7 +132,7 @@ void MessageDeclarationPrinter::printConstructors()
 void MessageDeclarationPrinter::printConstructor(int fieldCount)
 {
     std::vector<std::string> parameterList;
-    mPrinter->Print({{"classname", mName}}, Templates::ProtoConstructorBeginTemplate);
+    mPrinter->Print(mTypeMap, Templates::ProtoConstructorBeginTemplate);
     for (int i = 0; i < fieldCount; i++) {
         const FieldDescriptor *field = mDescriptor->field(i);
         const char *parameterTemplate = Templates::ConstructorParameterTemplate;
@@ -148,7 +148,7 @@ void MessageDeclarationPrinter::printConstructor(int fieldCount)
         mPrinter->Print(common::producePropertyMap(field, mDescriptor), parameterTemplate);
     }
 
-    mPrinter->Print({{"classname", mName}}, Templates::ProtoConstructorEndTemplate);
+    mPrinter->Print(mTypeMap, Templates::ProtoConstructorEndTemplate);
 }
 
 void MessageDeclarationPrinter::printMaps()
@@ -176,7 +176,7 @@ void MessageDeclarationPrinter::printNested()
 
 void MessageDeclarationPrinter::printClassDeclarationBegin()
 {
-    mPrinter->Print({{"classname", mName}}, Templates::ProtoClassDeclarationBeginTemplate);
+    mPrinter->Print(mTypeMap, Templates::ProtoClassDeclarationBeginTemplate);
 }
 
 void MessageDeclarationPrinter::printMetaTypesDeclaration()
@@ -397,7 +397,7 @@ void MessageDeclarationPrinter::printClassBody()
     printSetters();
 
     Indent();
-    mPrinter->Print({{"classname", mName}}, Templates::ManualRegistrationDeclaration);
+    mPrinter->Print(mTypeMap, Templates::ManualRegistrationDeclaration);
     Outdent();
 
     printSignalsBlock();
@@ -414,7 +414,7 @@ void MessageDeclarationPrinter::printClassBody()
 
 void MessageDeclarationPrinter::printListType()
 {
-    mPrinter->Print({{"classname", mName}}, Templates::ComplexListTypeUsingTemplate);
+    mPrinter->Print(mTypeMap, Templates::ComplexListTypeUsingTemplate);
 }
 
 void MessageDeclarationPrinter::printClassMembers()
@@ -434,7 +434,7 @@ void MessageDeclarationPrinter::printClassMembers()
 
 void MessageDeclarationPrinter::printDestructor()
 {
-    mPrinter->Print({{"classname", mName}}, "virtual ~$classname$();\n");
+    mPrinter->Print(mTypeMap, "virtual ~$classname$();\n");
 }
 
 void MessageDeclarationPrinter::printFieldEnum()

+ 12 - 12
src/generator/messagedefinitionprinter.cpp

@@ -40,7 +40,7 @@ MessageDefinitionPrinter::MessageDefinitionPrinter(const Descriptor *message, co
 void MessageDefinitionPrinter::printClassDefinitionPrivate()
 {
     if (common::hasNestedMessages(mDescriptor)) {
-        mPrinter->Print({{"namespace", mName + Templates::QtProtobufNestedNamespace}}, Templates::NamespaceTemplate);
+        mPrinter->Print({{"namespace", mTypeMap["classname"] + Templates::QtProtobufNestedNamespace}}, Templates::NamespaceTemplate);
         common::iterateNestedMessages(mDescriptor, [this](const Descriptor *nestedMessage) {
             MessageDefinitionPrinter nestedPrinter(nestedMessage, mPrinter);
             nestedPrinter.printClassDefinitionPrivate();
@@ -89,7 +89,7 @@ void MessageDefinitionPrinter::printRegisterBody()
 }
 
 void MessageDefinitionPrinter::printFieldsOrdering() {
-    mPrinter->Print({{"type", mName}}, Templates::FieldsOrderingContainerTemplate);
+    mPrinter->Print({{"type", mTypeMap["classname"]}}, Templates::FieldsOrderingContainerTemplate);
     Indent();
     for (int i = 0; i < mDescriptor->field_count(); i++) {
         const FieldDescriptor *field = mDescriptor->field(i);
@@ -208,7 +208,7 @@ void MessageDefinitionPrinter::printCopyFunctionality()
         assignmentOperatorTemplate = Templates::EmptyAssignmentOperatorDefinitionTemplate;
     }
 
-    mPrinter->Print({{"classname", mName}},
+    mPrinter->Print(mTypeMap,
                     constructorTemplate);
     common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
         if (common::isPureMessage(field)) {
@@ -228,7 +228,7 @@ void MessageDefinitionPrinter::printCopyFunctionality()
     Outdent();
     mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
 
-    mPrinter->Print({{"classname", mName}}, assignmentOperatorTemplate);
+    mPrinter->Print(mTypeMap, assignmentOperatorTemplate);
     Indent();
     common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
         if (common::isPureMessage(field)) {
@@ -253,7 +253,7 @@ void MessageDefinitionPrinter::printMoveSemantic()
         assignmentOperatorTemplate = Templates::EmptyMoveAssignmentOperatorDefinitionTemplate;
     }
 
-    mPrinter->Print({{"classname", mName}},
+    mPrinter->Print(mTypeMap,
                     constructorTemplate);
     common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
         if (common::isPureMessage(field)) {
@@ -285,7 +285,7 @@ void MessageDefinitionPrinter::printMoveSemantic()
     Outdent();
     mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
 
-    mPrinter->Print({{"classname", mName}}, assignmentOperatorTemplate);
+    mPrinter->Print(mTypeMap, assignmentOperatorTemplate);
     Indent();
     common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, const PropertyMap &propertyMap) {
         if (field->type() == FieldDescriptor::TYPE_MESSAGE
@@ -315,12 +315,12 @@ void MessageDefinitionPrinter::printComparisonOperators()
 {
     assert(mDescriptor != nullptr);
     if (mDescriptor->field_count() <= 0) {
-        mPrinter->Print({{"classname", mName}}, Templates::EmptyEqualOperatorDefinitionTemplate);
-        mPrinter->Print({{"classname", mName}}, Templates::NotEqualOperatorDefinitionTemplate);
+        mPrinter->Print(mTypeMap, Templates::EmptyEqualOperatorDefinitionTemplate);
+        mPrinter->Print(mTypeMap, Templates::NotEqualOperatorDefinitionTemplate);
         return;
     }
 
-    mPrinter->Print({{"classname", mName}}, Templates::EqualOperatorDefinitionTemplate);
+    mPrinter->Print(mTypeMap, Templates::EqualOperatorDefinitionTemplate);
 
     bool isFirst = true;
     common::iterateMessageFields(mDescriptor, [&](const FieldDescriptor *field, PropertyMap &propertyMap) {
@@ -347,7 +347,7 @@ void MessageDefinitionPrinter::printComparisonOperators()
     mPrinter->Print(";\n");
     mPrinter->Print(Templates::SimpleBlockEnclosureTemplate);
 
-    mPrinter->Print({{"classname", mName}}, Templates::NotEqualOperatorDefinitionTemplate);
+    mPrinter->Print(mTypeMap, Templates::NotEqualOperatorDefinitionTemplate);
 }
 
 void MessageDefinitionPrinter::printGetters()
@@ -387,7 +387,7 @@ void MessageDefinitionPrinter::printGetters()
 
 void MessageDefinitionPrinter::printDestructor()
 {
-    mPrinter->Print({{"classname", mName}}, Templates::RegistrarTemplate);
-    mPrinter->Print({{"classname", mName}}, "$classname$::~$classname$()\n"
+    mPrinter->Print(mTypeMap, Templates::RegistrarTemplate);
+    mPrinter->Print(mTypeMap, "$classname$::~$classname$()\n"
                                                  "{}\n\n");
 }