Explorar el Código

Hide types registration

- Remove member registerTypes function and hide registration inside
- Minor code improvements
Alexey Edelev hace 5 años
padre
commit
e1b00310c0

+ 3 - 8
src/generator/globalenumsgenerator.cpp

@@ -40,9 +40,6 @@ void GlobalEnumsGenerator::startEnum(const std::vector<std::string>& namespaces)
     printNamespaces(namespaces);
     printEnumClass();
     printPublic();
-    Indent();
-    mPrinter.Print(Templates::ComplexTypeRegistrationMethodTemplate);
-    Outdent();
     printPrivate();
     printConstructor();
 }
@@ -51,10 +48,8 @@ void GlobalEnumsGenerator::printConstructor()
 {
     Indent();
     mPrinter.Print({{"classname", mClassName}}, Templates::ConstructorHeaderTemplate);
-    mPrinter.Print({{"classname", mClassName}}, Templates::CopyConstructorTemplate);
-    encloseNamespaces(1);
-    mPrinter.Print({{"classname", mClassName}}, Templates::MoveConstructorTemplate);
-    encloseNamespaces(1);
+    mPrinter.Print({{"classname", mClassName}}, Templates::DeletedCopyConstructorTemplate);
+    mPrinter.Print({{"classname", mClassName}}, Templates::DeletedMoveConstructorTemplate);
     Outdent();
 }
 
@@ -103,7 +98,7 @@ void GlobalEnumsGenerator::printMetatype(const google::protobuf::FileDescriptor
 
 void GlobalEnumsGenerator::encloseEnum(const std::vector<std::string>& namespaces) {
     encloseClass();
-    encloseNamespaces(namespaces.size());
+    encloseNamespaces(static_cast<int>(namespaces.size()));
 }
 
 void GlobalEnumsGenerator::printEnumClass() {

+ 0 - 1
src/generator/globalenumsgenerator.h

@@ -33,7 +33,6 @@ namespace generator {
 
 class GlobalEnumsGenerator : public ClassGeneratorBase
 {
-    const ::google::protobuf::FileDescriptor *mFile;
     PackagesList mPackageList;
 public:
     GlobalEnumsGenerator(const PackagesList &packageList, std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> out);

+ 3 - 5
src/generator/globalenumssourcegenerator.cpp

@@ -48,8 +48,7 @@ void GlobalEnumsSourceGenerator::run() {
 
         printNamespaces(namespaces);
         printRegisterBody(package.second, namespaces);
-        printRegistrationHelperInvokation();
-        encloseNamespaces(namespaces.size());
+        encloseNamespaces(static_cast<int>(namespaces.size()));
     }
 }
 
@@ -74,7 +73,6 @@ void GlobalEnumsSourceGenerator::printRegisterBody(const std::list<const FileDes
 
     mPrinter.Print(registrationProperties, Templates::ComplexGlobalEnumRegistrationTemplate);
     Indent();
-    Indent();
     mPrinter.Print(registrationProperties, Templates::QmlRegisterTypeUncreatableTemplate);
 
     for (auto file : list) {
@@ -90,11 +88,11 @@ void GlobalEnumsSourceGenerator::printRegisterBody(const std::list<const FileDes
     }
     Outdent();
     mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
-    Outdent();
+    printRegistrationHelperInvokation();
     mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
 }
 
 void GlobalEnumsSourceGenerator::printRegistrationHelperInvokation()
 {
-    mPrinter.Print({{"classname", mClassName}}, "static qtprotobuf::RegistrationHelper<$classname$> helper;\n");
+    mPrinter.Print(Templates::RegistratorTemplate);
 }

+ 4 - 12
src/generator/protobufclassgenerator.cpp

@@ -243,7 +243,7 @@ bool ProtobufClassGenerator::producePropertyMap(const FieldDescriptor *field, Pr
     utils::tolower(typeNameLower);
 
     std::string capProperty = field->camelcase_name();
-    capProperty[0] = ::toupper(capProperty[0]);
+    capProperty[0] = static_cast<char>(::toupper(capProperty[0]));
 
     std::string typeNameNoList = typeName;
     if (field->is_repeated() && !field->is_map()) {
@@ -280,7 +280,7 @@ void ProtobufClassGenerator::printConstructor()
         const FieldDescriptor* field = mMessage->field(i);
         std::string fieldTypeName = getTypeName(field, mMessage);
         std::string fieldName = field->name();
-        fieldName[0] = ::tolower(fieldName[0]);
+        fieldName[0] = static_cast<char>(::tolower(fieldName[0]));
         if (field->is_repeated() || field->is_map()) {
             parameterList += "const " + fieldTypeName + " &" + fieldName + " = {}";
         } else {
@@ -320,7 +320,7 @@ void ProtobufClassGenerator::printConstructor()
     for (int i = 0; i < mMessage->field_count(); i++) {
         const FieldDescriptor* field = mMessage->field(i);
         std::string fieldName = field->name();
-        fieldName[0] = ::tolower(fieldName[0]);
+        fieldName[0] =  static_cast<char>(::tolower(fieldName[0]));
         mPrinter.Print({{"property_name", fieldName}}, Templates::PropertyInitializerTemplate);
     }
     mPrinter.Print(Templates::ConstructorContentTemplate);
@@ -456,13 +456,6 @@ void ProtobufClassGenerator::printProperties()
     Outdent();
 }
 
-void ProtobufClassGenerator::printRegisterTypes()
-{
-    Indent();
-    mPrinter.Print(Templates::ComplexTypeRegistrationMethodTemplate);
-    Outdent();
-}
-
 void ProtobufClassGenerator::printListType()
 {
     mPrinter.Print({{"classname", mClassName}}, Templates::ComplexListTypeUsingTemplate);
@@ -496,7 +489,7 @@ std::set<std::string> ProtobufClassGenerator::extractModels() const
             extractedModels.insert(typeName);
         }
     }
-    return std::move(extractedModels);
+    return extractedModels;
 }
 
 void ProtobufClassGenerator::printSerializers()
@@ -514,7 +507,6 @@ void ProtobufClassGenerator::run()
     printClassDeclaration();
     printProperties();
     printPublic();
-    printRegisterTypes();
     printFieldsOrderingDefinition();
     printSerializers();
     printPrivate();

+ 0 - 1
src/generator/protobufclassgenerator.h

@@ -60,7 +60,6 @@ public:
     void printProperties();
     void printFieldsOrderingDefinition();
     void printClassMembers();
-    void printRegisterTypes();
     void printConstructor();
     void printListType();
     void printInclude(const google::protobuf::FieldDescriptor *field, std::set<std::string> &existingIncludes);

+ 3 - 6
src/generator/protobufsourcegenerator.cpp

@@ -49,14 +49,9 @@ void ProtobufSourceGenerator::printRegisterBody()
     mPrinter.Print(registrationProperties,
                    Templates::ComplexTypeRegistrationTemplate);
     Indent();
-    Indent();
     mPrinter.Print(registrationProperties, Templates::RegisterQmlListPropertyMetaTypeTemplate);
     mPrinter.Print(registrationProperties, Templates::QmlRegisterTypeTemplate);
-    Outdent();
-    Outdent();
 
-    Indent();
-    Indent();
     for (int i = 0; i < mMessage->field_count(); i++) {
         const FieldDescriptor* field = mMessage->field(i);
         if (field->type() == FieldDescriptor::TYPE_ENUM
@@ -86,6 +81,8 @@ void ProtobufSourceGenerator::printRegisterBody()
     mPrinter.Print({{"classname", mClassName}}, Templates::RegisterSerializersTemplate);
     Outdent();
     mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
+    Indent();
+    printRegistrationHelperInvokation();
     Outdent();
     mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
 }
@@ -110,5 +107,5 @@ void ProtobufSourceGenerator::printFieldsOrdering() {
 
 void ProtobufSourceGenerator::printRegistrationHelperInvokation()
 {
-    mPrinter.Print({{"classname", mClassName}}, "static qtprotobuf::RegistrationHelper<$classname$> helper;\n");
+    mPrinter.Print(Templates::RegistratorTemplate);
 }

+ 0 - 1
src/generator/protobufsourcegenerator.h

@@ -45,7 +45,6 @@ public:
         printNamespaces();
         printFieldsOrdering();
         printRegisterBody();
-        printRegistrationHelperInvokation();
         encloseNamespaces();
     }
 };

+ 12 - 14
src/generator/templates.cpp

@@ -45,20 +45,15 @@ 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::ComplexTypeRegistrationMethodTemplate = "static void registerTypes();\n";
-const char *Templates::ComplexTypeRegistrationTemplate = "void $classname$::registerTypes()\n{\n"
-                                                         "    static bool registationDone = false;\n"
-                                                         "    if (!registationDone) {\n"
-                                                         "        registationDone = true;\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::ComplexTypeRegistrationTemplate = "namespace {\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 = "void $classname$::registerTypes()\n{\n"
-                                                               "    static bool registationDone = false;\n"
-                                                               "    if (!registationDone) {\n"
-                                                               "        registationDone = true;\n";
+const char *Templates::ComplexGlobalEnumRegistrationTemplate = "namespace {\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";
 const char *Templates::MapTypeUsingTemplate = "using $classname$ = QMap<$key$, $value$>;\n";
@@ -90,6 +85,8 @@ const char *Templates::ConstructorTemplate = "$classname$();\n";
 const char *Templates::ConstructorHeaderTemplate = "$classname$(){}\n";
 const char *Templates::CopyConstructorTemplate = "$classname$(const $classname$ &other) : QObject() {\n";
 const char *Templates::MoveConstructorTemplate = "$classname$($classname$ &&other) : QObject() {\n";
+const char *Templates::DeletedCopyConstructorTemplate = "$classname$(const $classname$ &) = delete;\n";
+const char *Templates::DeletedMoveConstructorTemplate = "$classname$($classname$ &&) = delete;\n";
 const char *Templates::CopyFieldTemplate = "m_$property_name$ = other.m_$property_name$;\n";
 const char *Templates::MoveComplexFieldTemplate = "m_$property_name$ = std::move(other.m_$property_name$);\n";
 const char *Templates::MoveFieldTemplate = "m_$property_name$ = std::exchange(other.m_$property_name$, 0);\n";
@@ -160,7 +157,7 @@ const char *Templates::SimpleBlockEnclosureTemplate = "}\n\n";
 const char *Templates::SemicolonBlockEnclosureTemplate = "};\n";
 const char *Templates::EmptyBlockTemplate = "{}\n\n";
 const char *Templates::PropertyInitializerTemplate = "\n    ,m_$property_name$($property_name$)";
-const char *Templates::ConstructorContentTemplate = "\n{\n    registerTypes();\n}\n";
+const char *Templates::ConstructorContentTemplate = "\n{\n}\n";
 
 const char *Templates::DeclareMetaTypeTemplate = "Q_DECLARE_METATYPE($namespaces$::$classname$)\n";
 const char *Templates::DeclareMetaTypeListTemplate = "Q_DECLARE_METATYPE($namespaces$::$classname$List)\n";
@@ -213,6 +210,7 @@ const char *Templates::ClientMethodDefinitionAsync2Template = "\nvoid $classname
 
 const char *Templates::SerializersTemplate = "Q_DECLARE_PROTOBUF_SERIALIZERS($classname$)\n";
 const char *Templates::RegisterSerializersTemplate = "qtprotobuf::ProtobufObjectPrivate::registerSerializers<$classname$>();\n";
+const char *Templates::RegistratorTemplate = "static qtprotobuf::RegistrationHelper helper(registerTypes);\n";
 const char *Templates::QmlRegisterTypeTemplate = "qmlRegisterType<$namespaces$::$classname$>(\"$package$\", 1, 0, \"$classname$\");\n";
 const char *Templates::QmlRegisterTypeUncreatableTemplate = "qmlRegisterUncreatableType<$namespaces$::$classname$>(\"$package$\", 1, 0, \"$classname$\", \"$namespaces$::$classname$ Could not be created from qml context\");\n";
 

+ 3 - 1
src/generator/templates.h

@@ -42,7 +42,6 @@ public:
     static const char *ExternalIncludeTemplate;
     static const char *GlobalEnumIncludeTemplate;
     static const char *UsingQtProtobufNamespaceTemplate;
-    static const char *ComplexTypeRegistrationMethodTemplate;
     static const char *ComplexTypeRegistrationTemplate;
     static const char *ComplexGlobalEnumRegistrationTemplate;
     static const char *ComplexGlobalEnumFieldRegistrationTemplate;
@@ -71,6 +70,8 @@ public:
     static const char *ConstructorTemplate;
     static const char *CopyConstructorTemplate;
     static const char *MoveConstructorTemplate;
+    static const char *DeletedCopyConstructorTemplate;
+    static const char *DeletedMoveConstructorTemplate;
     static const char *CopyFieldTemplate;
     static const char *MoveComplexFieldTemplate;
     static const char *MoveFieldTemplate;
@@ -110,6 +111,7 @@ public:
     static const char *MapSerializationRegisterTemplate;
     static const char *SerializersTemplate;
     static const char *RegisterSerializersTemplate;
+    static const char *RegistratorTemplate;
     static const char *QmlRegisterTypeTemplate;
     static const char *QmlRegisterTypeUncreatableTemplate;
     //Service templates

+ 28 - 29
src/protobuf/qtprotobuf.cpp

@@ -33,36 +33,35 @@
 
 namespace qtprotobuf {
 
-class QtProtobuf {
-public:
-    static void registerTypes() {
-        registerProtobufType(int32);
-        registerProtobufType(int64);
-        registerProtobufType(uint32);
-        registerProtobufType(uint64);
-        registerProtobufType(sint32);
-        registerProtobufType(sint64);
-        registerProtobufType(fixed32);
-        registerProtobufType(fixed64);
-        registerProtobufType(sfixed32);
-        registerProtobufType(sfixed64);
+namespace  {
+void registerTypes() {
+    registerProtobufType(int32);
+    registerProtobufType(int64);
+    registerProtobufType(uint32);
+    registerProtobufType(uint64);
+    registerProtobufType(sint32);
+    registerProtobufType(sint64);
+    registerProtobufType(fixed32);
+    registerProtobufType(fixed64);
+    registerProtobufType(sfixed32);
+    registerProtobufType(sfixed64);
 
-        registerProtobufType(int32List);
-        registerProtobufType(int64List);
-        registerProtobufType(uint32List);
-        registerProtobufType(uint64List);
-        registerProtobufType(sint32List);
-        registerProtobufType(sint64List);
-        registerProtobufType(fixed32List);
-        registerProtobufType(fixed64List);
-        registerProtobufType(sfixed32List);
-        registerProtobufType(sfixed64List);
+    registerProtobufType(int32List);
+    registerProtobufType(int64List);
+    registerProtobufType(uint32List);
+    registerProtobufType(uint64List);
+    registerProtobufType(sint32List);
+    registerProtobufType(sint64List);
+    registerProtobufType(fixed32List);
+    registerProtobufType(fixed64List);
+    registerProtobufType(sfixed32List);
+    registerProtobufType(sfixed64List);
 
-        registerProtobufType(DoubleList);
-        registerProtobufType(FloatList);
-        ProtobufObjectPrivate::registerSerializers();
-    }
-};
+    registerProtobufType(DoubleList);
+    registerProtobufType(FloatList);
+    ProtobufObjectPrivate::registerSerializers();
+}
+}
 
-static RegistrationHelper<QtProtobuf> helper;
+static RegistrationHelper helper(registerTypes);
 }

+ 3 - 3
src/protobuf/registrationhelper.h

@@ -23,13 +23,13 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
+#include <functional>
 namespace qtprotobuf {
 
-template<typename T>
 class RegistrationHelper {
 public:
-    RegistrationHelper() {
-        T::registerTypes();
+    RegistrationHelper(const std::function<void(void)> &registrator) {
+        registrator();
     }
 };