Browse Source

Fix compile warnings

- Warnings are errors from now
- Fix all existing warnings
Alexey Edelev 6 years ago
parent
commit
8087285272

+ 2 - 1
CMakeLists.txt

@@ -21,7 +21,8 @@ add_executable(${PROJECT_NAME} src/generator/main.cpp
     src/generator/servergenerator.cpp
     src/generator/protobufclassgenerator.cpp
     src/generator/globalenumsgenerator.cpp
-    src/generator/servicegeneratorbase.cpp)
+    src/generator/servicegeneratorbase.cpp
+    src/generator/templates.cpp)
 
 if(WIN32)
     #Needs to set path to protobuf libraries

+ 1 - 0
cmake/ProjectDefinitions.cmake

@@ -1,3 +1,4 @@
 set(CMAKE_CXX_STANDARD 14)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
+set(CMAKE_CXX_FLAGS "-Wall -Werror")

+ 9 - 9
src/generator/classgeneratorbase.cpp

@@ -49,7 +49,7 @@ ClassGeneratorBase::ClassGeneratorBase(std::string fullClassName, std::unique_pt
 
 void ClassGeneratorBase::printPreamble()
 {
-    mPrinter.Print(PreambleTemplate);
+    mPrinter.Print(Templates::PreambleTemplate);
 }
 
 void ClassGeneratorBase::printNamespaces()
@@ -60,24 +60,24 @@ void ClassGeneratorBase::printNamespaces()
 void ClassGeneratorBase::printNamespaces(const std::vector<std::string> &namespaces)
 {
     for (auto ns: namespaces) {
-        mPrinter.Print({{"namespace", ns}}, NamespaceTemplate);
+        mPrinter.Print({{"namespace", ns}}, Templates::NamespaceTemplate);
     }
 }
 
 void ClassGeneratorBase::printClassDeclaration()
 {
-    mPrinter.Print({{"classname", mClassName}}, ClassDefinitionTemplate);
+    mPrinter.Print({{"classname", mClassName}}, Templates::ClassDefinitionTemplate);
 }
 
 void ClassGeneratorBase::encloseClass()
 {
-    mPrinter.Print(SemicolonBlockEnclosureTemplate);
+    mPrinter.Print(Templates::SemicolonBlockEnclosureTemplate);
 }
 
 void ClassGeneratorBase::encloseNamespaces(int count)
 {
     for (int i = 0; i < count; i++) {
-        mPrinter.Print(SimpleBlockEnclosureTemplate);
+        mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
     }
 }
 
@@ -88,21 +88,21 @@ void ClassGeneratorBase::encloseNamespaces()
 
 void ClassGeneratorBase::printPublic()
 {
-    mPrinter.Print(PublicBlockTemplate);
+    mPrinter.Print(Templates::PublicBlockTemplate);
 }
 
 void ClassGeneratorBase::printMetaTypeDeclaration()
 {
     std::string namespaces;
-    for(int i = 0; i < mNamespaces.size(); i++) {
+    for(size_t i = 0; i < mNamespaces.size(); i++) {
         if(i > 0) {
             namespaces = namespaces.append("::");
         }
         namespaces = namespaces.append(mNamespaces[i]);
     }
     mPrinter.Print({{"classname", mClassName}, {"namespaces", namespaces}},
-                   DeclareMetaTypeTemplate);
+                   Templates::DeclareMetaTypeTemplate);
     mPrinter.Print({{"classname", mClassName}, {"namespaces", namespaces}},
-                   DeclareComplexListTypeTemplate);
+                   Templates::DeclareComplexListTypeTemplate);
 }
 

+ 4 - 4
src/generator/classgeneratorbase.h

@@ -76,16 +76,16 @@ protected:
         Indent();
         for (int i = 0; i < message->enum_type_count(); i++) {
             const auto enumDescr = message->enum_type(i);
-            mPrinter.Print({{"enum", enumDescr->name()}}, EnumDefinitionTemplate);
+            mPrinter.Print({{"enum", enumDescr->name()}}, Templates::EnumDefinitionTemplate);
             Indent();
             for (int j = 0; j < enumDescr->value_count(); j++) {
                 const auto valueDescr = enumDescr->value(j);
                 mPrinter.Print({{"enumvalue", valueDescr->name()},
-                                {"value", std::to_string(valueDescr->number())}}, EnumFieldTemplate);
+                                {"value", std::to_string(valueDescr->number())}}, Templates::EnumFieldTemplate);
             }
             Outdent();
-            mPrinter.Print(SemicolonBlockEnclosureTemplate);
-            mPrinter.Print({{"type", enumDescr->name().c_str()}}, QEnumTemplate);
+            mPrinter.Print(Templates::SemicolonBlockEnclosureTemplate);
+            mPrinter.Print({{"type", enumDescr->name().c_str()}}, Templates::QEnumTemplate);
         }
         Outdent();
     }

+ 6 - 6
src/generator/generator.cpp

@@ -67,11 +67,11 @@ public:
     void printClassHeaderInclude() {
         std::string includeFileName = mClassName;
         utils::tolower(includeFileName);
-        mPrinter.Print({{"type_lower", includeFileName}}, InternalIncludeTemplate);
+        mPrinter.Print({{"type_lower", includeFileName}}, Templates::InternalIncludeTemplate);
     }
 
     void printFieldsOrdering() {
-        mPrinter.Print({{"type", mClassName}}, FieldsOrderingContainerTemplate);
+        mPrinter.Print({{"type", mClassName}}, Templates::FieldsOrderingContainerTemplate);
         Indent();
         for (int i = 0; i < mMessage->field_count(); i++) {
             const FieldDescriptor* field = mMessage->field(i);
@@ -81,22 +81,22 @@ public:
             //property_number is incremented by 1 because user properties stating from 1.
             //Property with index 0 is "objectName"
             mPrinter.Print({{"field_number", std::to_string(field->number())},
-                            {"property_number", std::to_string(i + 1)}}, FieldOrderTemplate);
+                            {"property_number", std::to_string(i + 1)}}, Templates::FieldOrderTemplate);
         }
         Outdent();
-        mPrinter.Print(SemicolonBlockEnclosureTemplate);
+        mPrinter.Print(Templates::SemicolonBlockEnclosureTemplate);
     }
 
     void printRegisterBody()
     {
         std::string namespaces;
-        for(int i = 0; i < mNamespaces.size(); i++) {
+        for(size_t i = 0; i < mNamespaces.size(); i++) {
             if(i > 0) {
                 namespaces = namespaces.append("::");
             }
             namespaces = namespaces.append(mNamespaces[i]);
         }
-        mPrinter.Print({{"classname", mClassName}, {"namespaces", namespaces}}, ComplexTypeRegistrationTemplate);
+        mPrinter.Print({{"classname", mClassName}, {"namespaces", namespaces}}, Templates::ComplexTypeRegistrationTemplate);
     }
 };
 

+ 1 - 1
src/generator/globalenumsgenerator.cpp

@@ -64,5 +64,5 @@ void GlobalEnumsGenerator::encloseEnum(const std::vector<std::string>& namespace
 }
 
 void GlobalEnumsGenerator::printEnumClass() {
-    mPrinter.Print({{"classname", mClassName}}, NonProtoClassDefinitionTemplate);
+    mPrinter.Print({{"classname", mClassName}}, Templates::NonProtoClassDefinitionTemplate);
 }

+ 41 - 41
src/generator/protobufclassgenerator.cpp

@@ -49,26 +49,26 @@ void ProtobufClassGenerator::printCopyFunctionality()
 {
     assert(mMessage != nullptr);
     mPrinter.Print({{"classname", mClassName}},
-                   CopyConstructorTemplate);
+                   Templates::CopyConstructorTemplate);
 
     Indent();
     for (int i = 0; i < mMessage->field_count(); i++) {
-        printField(mMessage->field(i), CopyFieldTemplate);
+        printField(mMessage->field(i), Templates::CopyFieldTemplate);
     }
     Outdent();
 
-    mPrinter.Print(SimpleBlockEnclosureTemplate);
+    mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
     mPrinter.Print({{"classname", mClassName}},
-                   AssignmentOperatorTemplate);
+                   Templates::AssignmentOperatorTemplate);
 
     Indent();
     for (int i = 0; i < mMessage->field_count(); i++) {
-        printField(mMessage->field(i), CopyFieldTemplate);
+        printField(mMessage->field(i), Templates::CopyFieldTemplate);
     }
-    mPrinter.Print(AssignmentOperatorReturnTemplate);
+    mPrinter.Print(Templates::AssignmentOperatorReturnTemplate);
     Outdent();
 
-    mPrinter.Print(SimpleBlockEnclosureTemplate);
+    mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
 
 }
 
@@ -76,36 +76,36 @@ void ProtobufClassGenerator::printMoveSemantic()
 {
     assert(mMessage != nullptr);
     mPrinter.Print({{"classname", mClassName}},
-                   MoveConstructorTemplate);
+                   Templates::MoveConstructorTemplate);
 
     Indent();
     for (int i = 0; i < mMessage->field_count(); i++) {
         const FieldDescriptor* field = mMessage->field(i);
         if (isComplexType(field) || field->is_repeated()) {
-            printField(field, MoveComplexFieldTemplate);
+            printField(field, Templates::MoveComplexFieldTemplate);
         } else {
-            printField(field, MoveFieldTemplate);
+            printField(field, Templates::MoveFieldTemplate);
         }
     }
     Outdent();
 
-    mPrinter.Print(SimpleBlockEnclosureTemplate);
+    mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
     mPrinter.Print({{"classname", mClassName}},
-                   MoveAssignmentOperatorTemplate);
+                   Templates::MoveAssignmentOperatorTemplate);
 
     Indent();
     for (int i = 0; i < mMessage->field_count(); i++) {
         const FieldDescriptor* field = mMessage->field(i);
         if (isComplexType(field) || field->is_repeated()) {
-            printField(field, MoveComplexFieldTemplate);
+            printField(field, Templates::MoveComplexFieldTemplate);
         } else {
-            printField(field, MoveFieldTemplate);
+            printField(field, Templates::MoveFieldTemplate);
         }
     }
-    mPrinter.Print(AssignmentOperatorReturnTemplate);
+    mPrinter.Print(Templates::AssignmentOperatorReturnTemplate);
     Outdent();
 
-    mPrinter.Print(SimpleBlockEnclosureTemplate);
+    mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
 }
 
 void ProtobufClassGenerator::printComparisonOperators()
@@ -113,7 +113,7 @@ void ProtobufClassGenerator::printComparisonOperators()
     assert(mMessage != nullptr);
     bool isFirst = true;
     PropertyMap properties;
-    mPrinter.Print({{"type", mClassName}}, EqualOperatorTemplate);
+    mPrinter.Print({{"type", mClassName}}, Templates::EqualOperatorTemplate);
     for (int i = 0; i < mMessage->field_count(); i++) {
         const FieldDescriptor* field = mMessage->field(i);
         if (producePropertyMap(field, properties)) {
@@ -124,7 +124,7 @@ void ProtobufClassGenerator::printComparisonOperators()
                 Indent();
                 isFirst = false;
             }
-            mPrinter.Print(properties, EqualOperatorPropertyTemplate);
+            mPrinter.Print(properties, Templates::EqualOperatorPropertyTemplate);
         }
     }
 
@@ -135,16 +135,16 @@ void ProtobufClassGenerator::printComparisonOperators()
     }
 
     mPrinter.Print(";\n");
-    mPrinter.Print(SimpleBlockEnclosureTemplate);
+    mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
 
-    mPrinter.Print({{"type", mClassName}}, NotEqualOperatorTemplate);
+    mPrinter.Print({{"type", mClassName}}, Templates::NotEqualOperatorTemplate);
 }
 
 void ProtobufClassGenerator::printIncludes(std::set<std::string> listModel)
 {
     assert(mMessage != nullptr);
 
-    mPrinter.Print(DefaultProtobufIncludesTemplate);
+    mPrinter.Print(Templates::DefaultProtobufIncludesTemplate);
 
     PropertyMap properties;
     std::set<std::string> existingIncludes;
@@ -155,9 +155,9 @@ void ProtobufClassGenerator::printIncludes(std::set<std::string> listModel)
         if (producePropertyMap(field, properties)) {
             if (field->type() == FieldDescriptor::TYPE_MESSAGE) {
                 newInclude = properties["type_lower"];
-                includeTemplate = InternalIncludeTemplate;
+                includeTemplate = Templates::InternalIncludeTemplate;
             } else if (field->type() == FieldDescriptor::TYPE_STRING) {
-                includeTemplate = ExternalIncludeTemplate;
+                includeTemplate = Templates::ExternalIncludeTemplate;
             } else {
                 continue;
             }
@@ -171,7 +171,7 @@ void ProtobufClassGenerator::printIncludes(std::set<std::string> listModel)
                 std::string stringInclude = properties["type"];
                 if (stringInclude == VariantList
                         && existingIncludes.find(stringInclude) == std::end(existingIncludes)) {
-                    mPrinter.Print(properties, ExternalIncludeTemplate);
+                    mPrinter.Print(properties, Templates::ExternalIncludeTemplate);
                     existingIncludes.insert(stringInclude);
                 }
             }
@@ -180,13 +180,13 @@ void ProtobufClassGenerator::printIncludes(std::set<std::string> listModel)
 
     // Print List model class name
     if (listModel.size() > 0) {
-        mPrinter.Print(ListModelsIncludeTemplate);
+        mPrinter.Print(Templates::ListModelsIncludeTemplate);
     }
 
     for(auto modelTypeName : listModel) {
         std::string modelTypeNameLower(modelTypeName);
         utils::tolower(modelTypeNameLower);
-        mPrinter.Print({{"type_lower", modelTypeNameLower}}, InternalIncludeTemplate);
+        mPrinter.Print({{"type_lower", modelTypeNameLower}}, Templates::InternalIncludeTemplate);
     }
 }
 
@@ -214,8 +214,8 @@ std::string ProtobufClassGenerator::getTypeName(const FieldDescriptor *field)
         }
         typeName = field->enum_type()->name();
     } else {
-        auto it = TypeReflection.find(field->type());
-        if (it != std::end(TypeReflection)) {
+        auto it = Templates::TypeReflection.find(field->type());
+        if (it != std::end(Templates::TypeReflection)) {
             typeName = it->second;
             if (field->is_repeated()) {
                 std::string namespaceDefinition("qtprotobuf::");
@@ -276,8 +276,8 @@ bool ProtobufClassGenerator::isComplexType(const FieldDescriptor *field)
 
 void ProtobufClassGenerator::printConstructor()
 {
-    mPrinter.Print({{"classname", mClassName}}, ConstructorTemplate);
-    mPrinter.Print(ConstructorContentTemplate);
+    mPrinter.Print({{"classname", mClassName}}, Templates::ConstructorTemplate);
+    mPrinter.Print(Templates::ConstructorContentTemplate);
 }
 
 void ProtobufClassGenerator::printProperties()
@@ -287,12 +287,12 @@ void ProtobufClassGenerator::printProperties()
     Indent();
     for (int i = 0; i < mMessage->field_count(); i++) {
         const FieldDescriptor* field = mMessage->field(i);
-        const char* propertyTemplate = field->type() == FieldDescriptor::TYPE_MESSAGE ? MessagePropertyTemplate :
-                                                                                        PropertyTemplate;
+        const char* propertyTemplate = field->type() == FieldDescriptor::TYPE_MESSAGE ? Templates::MessagePropertyTemplate :
+                                                                                        Templates::PropertyTemplate;
         printField(field, propertyTemplate);
     }
     for (int i = 0; i < mMessage->field_count(); i++) {
-        printField(mMessage->field(i), MemberTemplate);
+        printField(mMessage->field(i), Templates::MemberTemplate);
     }
     Outdent();
 
@@ -309,42 +309,42 @@ void ProtobufClassGenerator::printProperties()
     printComparisonOperators();
 
     for (int i = 0; i < mMessage->field_count(); i++) {
-        printField(mMessage->field(i), GetterTemplate);
+        printField(mMessage->field(i), Templates::GetterTemplate);
     }
     for (int i = 0; i < mMessage->field_count(); i++) {
         auto field = mMessage->field(i);
         if (field->type() == FieldDescriptor::TYPE_MESSAGE
                 || field->type() == FieldDescriptor::TYPE_STRING) {
-            printField(field, SetterTemplateComplexType);
+            printField(field, Templates::SetterTemplateComplexType);
         } else {
-            printField(field, SetterTemplateSimpleType);
+            printField(field, Templates::SetterTemplateSimpleType);
         }
     }
     Outdent();
 
-    mPrinter.Print(SignalsBlockTemplate);
+    mPrinter.Print(Templates::SignalsBlockTemplate);
 
     Indent();
     for (int i = 0; i < mMessage->field_count(); i++) {
-        printField(mMessage->field(i), SignalTemplate);
+        printField(mMessage->field(i), Templates::SignalTemplate);
     }
     Outdent();
 }
 
 void ProtobufClassGenerator::printRegisterTypes()
 {
-    mPrinter.Print(ComplexTypeRegistrationMethodTemplate);
+    mPrinter.Print(Templates::ComplexTypeRegistrationMethodTemplate);
 }
 
 void ProtobufClassGenerator::printListType()
 {
-    mPrinter.Print({{"classname", mClassName}}, ComplexListTypeUsingTemplate);
+    mPrinter.Print({{"classname", mClassName}}, Templates::ComplexListTypeUsingTemplate);
 }
 
 void ProtobufClassGenerator::printFieldsOrderingDefinition()
 {
     Indent();
-    mPrinter.Print(FieldsOrderingDefinitionContainerTemplate);
+    mPrinter.Print(Templates::FieldsOrderingDefinitionContainerTemplate);
     Outdent();
 }
 

+ 2 - 2
src/generator/servicegeneratorbase.cpp

@@ -62,11 +62,11 @@ void ServiceGeneratorBase::printIncludes()
     }
 
     for(auto type : includeSet) {
-        mPrinter.Print({{"type_lower", type}}, InternalIncludeTemplate);
+        mPrinter.Print({{"type_lower", type}}, Templates::InternalIncludeTemplate);
     }
 }
 
 void ServiceGeneratorBase::printClassName()
 {
-    mPrinter.Print({{"classname", mClassName}}, NonProtoClassDefinitionTemplate);
+    mPrinter.Print({{"classname", mClassName}}, Templates::NonProtoClassDefinitionTemplate);
 }

+ 147 - 0
src/generator/templates.cpp

@@ -0,0 +1,147 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>, Tatyana Borisova <tanusshhka@mail.ru>
+ *
+ * This file is part of qtprotobuf project https://git.semlanik.org/semlanik/qtprotobuf
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this
+ * software and associated documentation files (the "Software"), to deal in the Software
+ * without restriction, including without limitation the rights to use, copy, modify,
+ * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
+ * to permit persons to whom the Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies
+ * or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+ * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+ * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+ * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "templates.h"
+
+using namespace qtprotobuf::generator;
+
+const char *Templates::DefaultProtobufIncludesTemplate = "#include <QMetaType>\n"
+                                              "#include <protobufobject.h>\n"
+                                              "#include <unordered_map>\n\n";
+
+const char *Templates::PreambleTemplate = "/* This file is autogenerated. DO NOT CHANGE. All changes will be lost */\n\n"
+                                      "#pragma once\n\n"
+                                      "#include <QObject>\n"
+                                      "#include <QMetaType>\n"
+                                      "#include <protobufobject.h>\n"
+                                      "#include <unordered_map>\n\n";
+
+const char *Templates::InternalIncludeTemplate =  "#include \"$type_lower$.h\"\n";
+const char *Templates::ExternalIncludeTemplate = "#include <$type$>\n";
+const char *Templates::ListModelsIncludeTemplate = "#include <QList>\n";
+
+const char *Templates::UsingQtProtobufNamespaceTemplate = "\nusing namespace qtprotobuf;\n";
+const char *Templates::ComplexTypeRegistrationMethodTemplate = "\nstatic void registerTypes();\n";
+const char *Templates::ComplexTypeRegistrationTemplate = "void $classname$::registerTypes()\n{\n"
+                                                     "    static bool registationDone = false;\n"
+                                                     "    if (!registationDone) {\n\n"
+                                                     "        int metaTypeId = qRegisterMetaType<$classname$>(\"$classname$\");\n"
+                                                     "        int listMetaTypeId = qRegisterMetaType<$classname$List>(\"$classname$List\");\n"
+                                                     "        qRegisterMetaType<$namespaces$::$classname$>(\"$namespaces$::$classname$\");\n"
+                                                     "        qRegisterMetaType<$namespaces$::$classname$List>(\"$namespaces$::$classname$List\");\n"
+                                                     "        registerSerializers(metaTypeId, listMetaTypeId);\n"
+                                                     "    }\n}\n";
+const char *Templates::ComplexListTypeUsingTemplate = "using $classname$List = QList<$classname$>;\n";
+
+const char *Templates::NamespaceTemplate = "\nnamespace $namespace$ {\n";
+
+const char *Templates::NonProtoClassDefinitionTemplate = "\nclass $classname$ : public QObject\n"
+                                                     "{\n"
+                                                     "    Q_OBJECT\n";
+const char *Templates::ClassDefinitionTemplate = "\nclass $classname$ final : public qtprotobuf::ProtobufObject<$classname$>\n"
+                                             "{\n"
+                                             "    Q_OBJECT\n";
+
+const char *Templates::PropertyTemplate = "Q_PROPERTY($type$ $property_name$ READ $property_name$ WRITE set$property_name_cap$ NOTIFY $property_name$Changed)\n";
+const char *Templates::MessagePropertyTemplate = "Q_PROPERTY($type$ $property_name$ READ $property_name$ WRITE set$property_name_cap$ NOTIFY $property_name$Changed)\n";
+const char *Templates::MemberTemplate = "$type$ m_$property_name$;\n";
+const char *Templates::PublicBlockTemplate = "\npublic:\n";
+const char *Templates::EnumDefinitionTemplate = "enum $enum$ {\n";
+const char *Templates::EnumFieldTemplate = "$enumvalue$ = $value$,\n";
+const char *Templates::ConstructorTemplate = "$classname$(QObject *parent = nullptr) : ProtobufObject(parent)\n";
+const char *Templates::CopyConstructorTemplate = "$classname$(const $classname$ &other) : ProtobufObject() {\n";
+const char *Templates::MoveConstructorTemplate = "$classname$($classname$ &&other) : ProtobufObject() {\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";
+const char *Templates::AssignmentOperatorTemplate = "$classname$ &operator =(const $classname$ &other) {\n";
+const char *Templates::AssignmentOperatorReturnTemplate = "return *this;\n";
+const char *Templates::MoveAssignmentOperatorTemplate = "$classname$ &operator =($classname$ &&other) {\n";
+const char *Templates::EqualOperatorTemplate = "bool operator ==(const $type$ &other) const {\n"
+                                           "    return ";
+const char *Templates::EqualOperatorPropertyTemplate = "m_$property_name$ == other.m_$property_name$";
+const char *Templates::NotEqualOperatorTemplate = "bool operator !=(const $type$ &other) const {\n"
+                                              "    return !this->operator ==(other);\n"
+                                              "}\n\n";
+
+const char *Templates::GetterTemplate = "$type$ $property_name$() const {\n"
+                                    "    return m_$property_name$;\n"
+                                    "}\n\n";
+
+const char *Templates::SetterTemplateSimpleType = "void set$property_name_cap$($type$ $property_name$) {\n"
+                                              "    if (m_$property_name$ != $property_name$) {\n"
+                                              "        m_$property_name$ = $property_name$;\n"
+                                              "        $property_name$Changed();\n"
+                                              "    }\n"
+                                              "}\n\n";
+
+const char *Templates::SetterTemplateComplexType = "void set$property_name_cap$(const $type$ &$property_name$) {\n"
+                                               "    if (m_$property_name$ != $property_name$) {\n"
+                                               "        m_$property_name$ = $property_name$;\n"
+                                               "        $property_name$Changed();\n"
+                                               "    }\n"
+                                               "}\n\n";
+
+const char *Templates::SignalsBlockTemplate = "\nsignals:\n";
+const char *Templates::SignalTemplate = "void $property_name$Changed();\n";
+
+const char *Templates::FieldsOrderingDefinitionContainerTemplate = "static const std::unordered_map<int/*field number*/, int/*property number*/> propertyOrdering;\n";
+
+const char *Templates::FieldsOrderingContainerTemplate = "const std::unordered_map<int, int> $type$::propertyOrdering = {";
+const char *Templates::FieldOrderTemplate = "{$field_number$,$property_number$}";
+
+const char *Templates::EnumTemplate = "$type$";
+
+const char *Templates::SimpleBlockEnclosureTemplate = "}\n\n";
+const char *Templates::SemicolonBlockEnclosureTemplate = "};\n";
+const char *Templates::EmptyBlockTemplate = "{}\n\n";
+const char *Templates::ConstructorContentTemplate = "{\n    registerTypes();\n}\n";
+
+const char *Templates::DeclareMetaTypeTemplate = "Q_DECLARE_METATYPE($namespaces$::$classname$)\n";
+const char *Templates::DeclareComplexListTypeTemplate = "Q_DECLARE_METATYPE($namespaces$::$classname$List)\n";
+
+const char *Templates::QEnumTemplate = "Q_ENUM($type$)\n";
+
+
+const std::unordered_map<::google::protobuf::FieldDescriptor::Type, std::string> Templates::TypeReflection = {
+    {::google::protobuf::FieldDescriptor::TYPE_DOUBLE, "double"},
+    {::google::protobuf::FieldDescriptor::TYPE_FLOAT, "float"},
+    //        {FieldDescriptor::TYPE_INT64, "int"},//Not supported see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
+    //        {FieldDescriptor::TYPE_UINT64,"int"},//Not supported see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
+    {::google::protobuf::FieldDescriptor::TYPE_INT32, "int"},
+    {::google::protobuf::FieldDescriptor::TYPE_FIXED64, "qtprotobuf::FixedInt64"},
+    {::google::protobuf::FieldDescriptor::TYPE_FIXED32, "qtprotobuf::FixedInt32"},
+    {::google::protobuf::FieldDescriptor::TYPE_BOOL, "bool"},
+    {::google::protobuf::FieldDescriptor::TYPE_STRING, "QString"},
+    {::google::protobuf::FieldDescriptor::TYPE_GROUP, ""},//Not supported and deprecated in protobuf
+    //    {FieldDescriptor::TYPE_MESSAGE, ""},//Custom typename
+    {::google::protobuf::FieldDescriptor::TYPE_BYTES, "QByteArray"},
+    {::google::protobuf::FieldDescriptor::TYPE_UINT32, "int"},//Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
+    //    {FieldDescriptor::TYPE_ENUM, ""},//Custom typename
+    {::google::protobuf::FieldDescriptor::TYPE_SFIXED32, "int"},
+    //        {FieldDescriptor::TYPE_SFIXED64, "int"},//Not supported see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
+    {::google::protobuf::FieldDescriptor::TYPE_SINT32, "int"},
+    //        {FieldDescriptor::TYPE_SINT64, "int"},//Not supported see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
+};

+ 49 - 117
src/generator/templates.h

@@ -32,123 +32,55 @@
 namespace qtprotobuf {
 namespace generator {
 
-static const char *DefaultProtobufIncludesTemplate = "#include <QMetaType>\n"
-                                                     "#include <protobufobject.h>\n"
-                                                     "#include <unordered_map>\n\n";
-
-static const char *PreambleTemplate = "/* This file is autogenerated. DO NOT CHANGE. All changes will be lost */\n\n"
-                                      "#pragma once\n\n"
-                                      "#include <QObject>\n"
-                                      "#include <QMetaType>\n"
-                                      "#include <protobufobject.h>\n"
-                                      "#include <unordered_map>\n\n";
-
-static const char *InternalIncludeTemplate =  "#include \"$type_lower$.h\"\n";
-static const char *ExternalIncludeTemplate = "#include <$type$>\n";
-static const char *ListModelsIncludeTemplate = "#include <QList>\n";
-
-static const char *UsingQtProtobufNamespaceTemplate = "\nusing namespace qtprotobuf;\n";
-static const char *ComplexTypeRegistrationMethodTemplate = "\nstatic void registerTypes();\n";
-static const char *ComplexTypeRegistrationTemplate = "void $classname$::registerTypes()\n{\n"
-                                                     "    static bool registationDone = false;\n"
-                                                     "    if (!registationDone) {\n\n"
-                                                     "        int metaTypeId = qRegisterMetaType<$classname$>(\"$classname$\");\n"
-                                                     "        int listMetaTypeId = qRegisterMetaType<$classname$List>(\"$classname$List\");\n"
-                                                     "        qRegisterMetaType<$namespaces$::$classname$>(\"$namespaces$::$classname$\");\n"
-                                                     "        qRegisterMetaType<$namespaces$::$classname$List>(\"$namespaces$::$classname$List\");\n"
-                                                     "        registerSerializers(metaTypeId, listMetaTypeId);\n"
-                                                     "    }\n}\n";
-static const char *ComplexListTypeUsingTemplate = "using $classname$List = QList<$classname$>;\n";
-
-static const char *NamespaceTemplate = "\nnamespace $namespace$ {\n";
-
-static const char *NonProtoClassDefinitionTemplate = "\nclass $classname$ : public QObject\n"
-                                                     "{\n"
-                                                     "    Q_OBJECT\n";
-static const char *ClassDefinitionTemplate = "\nclass $classname$ final : public qtprotobuf::ProtobufObject<$classname$>\n"
-                                             "{\n"
-                                             "    Q_OBJECT\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 *MessagePropertyTemplate = "Q_PROPERTY($type$ $property_name$ READ $property_name$ WRITE set$property_name_cap$ NOTIFY $property_name$Changed)\n";
-static const char *MemberTemplate = "$type$ m_$property_name$;\n";
-static const char *PublicBlockTemplate = "\npublic:\n";
-static const char *EnumDefinitionTemplate = "enum $enum$ {\n";
-static const char *EnumFieldTemplate = "$enumvalue$ = $value$,\n";
-static const char *ConstructorTemplate = "$classname$(QObject *parent = nullptr) : ProtobufObject(parent)\n";
-static const char *CopyConstructorTemplate = "$classname$(const $classname$ &other) : ProtobufObject() {\n";
-static const char *MoveConstructorTemplate = "$classname$($classname$ &&other) : ProtobufObject() {\n";
-static const char *CopyFieldTemplate = "m_$property_name$ = other.m_$property_name$;\n";
-static const char *MoveComplexFieldTemplate = "m_$property_name$ = std::move(other.m_$property_name$);\n";
-static const char *MoveFieldTemplate = "m_$property_name$ = std::exchange(other.m_$property_name$, 0);\n";
-static const char *AssignmentOperatorTemplate = "$classname$ &operator =(const $classname$ &other) {\n";
-static const char *AssignmentOperatorReturnTemplate = "return *this;\n";
-static const char *MoveAssignmentOperatorTemplate = "$classname$ &operator =($classname$ &&other) {\n";
-static const char *EqualOperatorTemplate = "bool operator ==(const $type$ &other) const {\n"
-                                           "    return ";
-static const char *EqualOperatorPropertyTemplate = "m_$property_name$ == other.m_$property_name$";
-static const char *NotEqualOperatorTemplate = "bool operator !=(const $type$ &other) const {\n"
-                                              "    return !this->operator ==(other);\n"
-                                              "}\n\n";
-
-static const char *GetterTemplate = "$type$ $property_name$() const {\n"
-                                    "    return m_$property_name$;\n"
-                                    "}\n\n";
-
-static const char *SetterTemplateSimpleType = "void set$property_name_cap$($type$ $property_name$) {\n"
-                                              "    if (m_$property_name$ != $property_name$) {\n"
-                                              "        m_$property_name$ = $property_name$;\n"
-                                              "        $property_name$Changed();\n"
-                                              "    }\n"
-                                              "}\n\n";
-
-static const char *SetterTemplateComplexType = "void set$property_name_cap$(const $type$ &$property_name$) {\n"
-                                               "    if (m_$property_name$ != $property_name$) {\n"
-                                               "        m_$property_name$ = $property_name$;\n"
-                                               "        $property_name$Changed();\n"
-                                               "    }\n"
-                                               "}\n\n";
-
-static const char *SignalsBlockTemplate = "\nsignals:\n";
-static const char *SignalTemplate = "void $property_name$Changed();\n";
-
-static const char *FieldsOrderingDefinitionContainerTemplate = "static const std::unordered_map<int/*field number*/, int/*property number*/> propertyOrdering;\n";
-
-static const char *FieldsOrderingContainerTemplate = "const std::unordered_map<int, int> $type$::propertyOrdering = {";
-static const char *FieldOrderTemplate = "{$field_number$,$property_number$}";
-
-static const char *EnumTemplate = "$type$";
-
-static const char *SimpleBlockEnclosureTemplate = "}\n\n";
-static const char *SemicolonBlockEnclosureTemplate = "};\n";
-static const char *EmptyBlockTemplate = "{}\n\n";
-static const char *ConstructorContentTemplate = "{\n    registerTypes();\n}\n";
-
-static const char *DeclareMetaTypeTemplate = "Q_DECLARE_METATYPE($namespaces$::$classname$)\n";
-static const char *DeclareComplexListTypeTemplate = "Q_DECLARE_METATYPE($namespaces$::$classname$List)\n";
-
-static const char *QEnumTemplate = "Q_ENUM($type$)\n";
-
-
-static const std::unordered_map<::google::protobuf::FieldDescriptor::Type, std::string> TypeReflection = {
-    {::google::protobuf::FieldDescriptor::TYPE_DOUBLE, "double"},
-    {::google::protobuf::FieldDescriptor::TYPE_FLOAT, "float"},
-    //        {FieldDescriptor::TYPE_INT64, "int"},//Not supported see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
-    //        {FieldDescriptor::TYPE_UINT64,"int"},//Not supported see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
-    {::google::protobuf::FieldDescriptor::TYPE_INT32, "int"},
-    {::google::protobuf::FieldDescriptor::TYPE_FIXED64, "qtprotobuf::FixedInt64"},
-    {::google::protobuf::FieldDescriptor::TYPE_FIXED32, "qtprotobuf::FixedInt32"},
-    {::google::protobuf::FieldDescriptor::TYPE_BOOL, "bool"},
-    {::google::protobuf::FieldDescriptor::TYPE_STRING, "QString"},
-    {::google::protobuf::FieldDescriptor::TYPE_GROUP, ""},//Not supported and deprecated in protobuf
-    //    {FieldDescriptor::TYPE_MESSAGE, ""},//Custom typename
-    {::google::protobuf::FieldDescriptor::TYPE_BYTES, "QByteArray"},
-    {::google::protobuf::FieldDescriptor::TYPE_UINT32, "int"},//Limited usage see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
-    //    {FieldDescriptor::TYPE_ENUM, ""},//Custom typename
-    {::google::protobuf::FieldDescriptor::TYPE_SFIXED32, "int"},
-    //        {FieldDescriptor::TYPE_SFIXED64, "int"},//Not supported see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
-    {::google::protobuf::FieldDescriptor::TYPE_SINT32, "int"},
-    //        {FieldDescriptor::TYPE_SINT64, "int"},//Not supported see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
+class Templates {
+public:
+    static const char *DefaultProtobufIncludesTemplate;
+    static const char *PreambleTemplate;
+    static const char *InternalIncludeTemplate;
+    static const char *ExternalIncludeTemplate;
+    static const char *ListModelsIncludeTemplate;
+    static const char *UsingQtProtobufNamespaceTemplate;
+    static const char *ComplexTypeRegistrationMethodTemplate;
+    static const char *ComplexTypeRegistrationTemplate;
+    static const char *ComplexListTypeUsingTemplate;
+    static const char *NamespaceTemplate;
+    static const char *NonProtoClassDefinitionTemplate;
+    static const char *ClassDefinitionTemplate;
+    static const char *PropertyTemplate;
+    static const char *MessagePropertyTemplate;
+    static const char *MemberTemplate;
+    static const char *PublicBlockTemplate;
+    static const char *EnumDefinitionTemplate;
+    static const char *EnumFieldTemplate;
+    static const char *ConstructorTemplate;
+    static const char *CopyConstructorTemplate;
+    static const char *MoveConstructorTemplate;
+    static const char *CopyFieldTemplate;
+    static const char *MoveComplexFieldTemplate;
+    static const char *MoveFieldTemplate;
+    static const char *AssignmentOperatorTemplate;
+    static const char *AssignmentOperatorReturnTemplate;
+    static const char *MoveAssignmentOperatorTemplate;
+    static const char *EqualOperatorTemplate;
+    static const char *EqualOperatorPropertyTemplate;
+    static const char *NotEqualOperatorTemplate;
+    static const char *GetterTemplate;
+    static const char *SetterTemplateSimpleType;
+    static const char *SetterTemplateComplexType;
+    static const char *SignalsBlockTemplate;
+    static const char *SignalTemplate;
+    static const char *FieldsOrderingDefinitionContainerTemplate;
+    static const char *FieldsOrderingContainerTemplate;
+    static const char *FieldOrderTemplate;
+    static const char *EnumTemplate;
+    static const char *SimpleBlockEnclosureTemplate;
+    static const char *SemicolonBlockEnclosureTemplate;
+    static const char *EmptyBlockTemplate;
+    static const char *ConstructorContentTemplate;
+    static const char *DeclareMetaTypeTemplate;
+    static const char *DeclareComplexListTypeTemplate;
+    static const char *QEnumTemplate;
+    static const std::unordered_map<::google::protobuf::FieldDescriptor::Type, std::string> TypeReflection;
 };
 
 } //namespace generator

+ 3 - 3
src/generator/utils.h

@@ -37,8 +37,8 @@ class FileDescriptor;
 
 namespace qtprotobuf {
 namespace generator {
-namespace utils {
-
+class utils {
+public:
 static void split(const std::string &str, std::vector<std::string> &container, char delim)
 {
     container.clear();
@@ -61,7 +61,7 @@ static void tolower(std::string& str) {
     std::transform(std::begin(str), std::end(str), std::begin(str), ::tolower);
 }
 
-} //namespace utils
+};
 
 using PackagesList = std::unordered_map<std::string/*package*/, std::list<const ::google::protobuf::FileDescriptor *>>;
 

+ 4 - 4
src/lib/protobufobject.h

@@ -84,7 +84,7 @@ public:
         qProtoDebug() << __func__ << "propertyValue" << propertyValue << "fieldIndex" << fieldIndex << "isFixed" << isFixed;
         QByteArray result;
         WireTypes type = UnknownWireType;
-        switch (propertyValue.type()) {
+        switch (static_cast<QMetaType::Type>(propertyValue.type())) {
         case QMetaType::Int:
             type = Varint;
             result.append(serializeVarint(propertyValue.toInt()));
@@ -265,7 +265,7 @@ public:
         result.reserve(sizeof(V));
         while (value > 0) {
             //Put first 7 bits to result buffer and mark as not last
-            result.append(value & 0x7F | 0x80);
+            result.append((value & 0x7F) | 0x80);
             //Devide values to chunks of 7 bits, move to next chunk
             value >>= 7;
         }
@@ -423,7 +423,7 @@ public:
         qProtoDebug() << __func__;
         QList<V> out;
         unsigned int count = deserializeVarint<unsigned int>(it).toUInt() / sizeof(V);
-        for (int i = 0; i < count; i++) {
+        for (unsigned int i = 0; i < count; i++) {
             QVariant variant = deserializeFixed<V>(it);
             out.append(variant.value<V>());
         }
@@ -501,7 +501,7 @@ public:
 
     void deserialize(const QByteArray &array) {
         qProtoDebug() << T::staticMetaObject.className() << "deserialize";
-        T *instance = dynamic_cast<T *>(this);
+        //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