Переглянути джерело

Implement copy functionality for generated messages

Tatyana Borisova 6 роки тому
батько
коміт
82646ee2e1

+ 22 - 0
src/generator/classgeneratorbase.cpp

@@ -134,6 +134,7 @@ void ClassGeneratorBase::printProperties(const Descriptor *message)
     //public section
     printPublic();
     printConstructor();
+    printCopyFunctionality(message);
     printEqualOperator(message);
     for (int i = 0; i < message->field_count(); i++) {
         printField(message->field(i), GetterTemplate);
@@ -199,6 +200,27 @@ std::string ClassGeneratorBase::getTypeName(const FieldDescriptor *field)
     return typeName;
 }
 
+void ClassGeneratorBase::printCopyFunctionality(const ::google::protobuf::Descriptor *message)
+{
+    mPrinter.Print({{"classname", mClassName}},
+                   "    $classname$(const $classname$ &other) : QObject(other.parent()) {\n");
+
+    for (int i = 0; i < message->field_count(); i++) {
+        printField(message->field(i), CopyClassFunctionalityTemplate);
+    }
+
+    mPrinter.Print("    }\n\n");
+    mPrinter.Print({{"classname", mClassName}},
+                   "    $classname$ &operator =(const $classname$ &other) {\n");
+
+    for (int i = 0; i < message->field_count(); i++) {
+        printField(message->field(i), CopyClassFunctionalityTemplate);
+    }
+
+    mPrinter.Print("    }\n\n");
+
+}
+
 void ClassGeneratorBase::printConstructor()
 {
     mPrinter.Print({{"classname", mClassName}},

+ 1 - 0
src/generator/classgeneratorbase.h

@@ -82,6 +82,7 @@ protected:
         }
     }
 
+    void printCopyFunctionality(const ::google::protobuf::Descriptor *message);
     void printEqualOperator(const ::google::protobuf::Descriptor *message);
     void printProperties(const ::google::protobuf::Descriptor *message);
     void printConstructor();

+ 5 - 2
src/generator/templates.h

@@ -41,6 +41,9 @@ static const char *StartTemplate = "\nclass $classname$ : public QObject\n"
 
 static const char *NamespaceTemplate = "\nnamespace $namespace$\n"
                                   "{\n\n";
+
+static const char *CopyClassFunctionalityTemplate = "        m_$property_name$ = other.m_$property_name$;\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"
@@ -69,10 +72,10 @@ static const char *ExternalIncludeTemplate = "#include <$type$>\n";
 static const char *EnumTemplate = "$type$";
 static const char *ModelClassTemplate = "using $type$Model = UniversalListModel<$type$>;\n";
 
-static const char *EqualOperatorTemplate = "    operator ==(const $type$& other) {\n"
+static const char *EqualOperatorTemplate = "    bool operator ==(const $type$ &other) {\n"
                                           "        return ";
 static const char *EqualOperatorPropertyTemplate = "m_$property_name$ == other.m_$property_name$";
-static const char *SimpleBlockEnclosureTemplate = "}\n";
+static const char *SimpleBlockEnclosureTemplate = "}\n\n";
 
 static const std::unordered_map<::google::protobuf::FieldDescriptor::Type, std::string> TypeReflection = {
     {::google::protobuf::FieldDescriptor::TYPE_DOUBLE, "qreal"},