Browse Source

Introduce generated type registration helper

- No need to call registerTypes manually
- Remove manual registerTypes calls
Viktor Kopp 5 years ago
parent
commit
0d475a3922

+ 0 - 5
examples/addressbook/main.cpp

@@ -43,11 +43,6 @@ int main(int argc, char *argv[])
 {
     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
     qtprotobuf::QtProtobuf::init();
-    Contact::registerTypes();
-    Contacts::registerTypes();
-    Job::registerTypes();
-    Address::registerTypes();
-    PhoneNumber::registerTypes();
 
     qmlRegisterType<ContactsListModel>("examples.addressbook", 1, 0, "ContactsListModel");
     QGuiApplication app(argc, argv);

+ 0 - 4
examples/simplechat/main.cpp

@@ -48,10 +48,6 @@ int main(int argc, char *argv[])
     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
 
     qtprotobuf::QtProtobuf::init();
-    ChatMessages::registerTypes();
-    ChatMessage::registerTypes();
-    User::registerTypes();
-    Users::registerTypes();
 
     qmlRegisterUncreatableType<ChatMessageModel>("examples.simplechat",  1, 0, "ChatMessageModel", "");
 

+ 7 - 0
src/generator/globalenumssourcegenerator.cpp

@@ -38,6 +38,7 @@ GlobalEnumsSourceGenerator::GlobalEnumsSourceGenerator(const PackagesList &packa
 
 void GlobalEnumsSourceGenerator::run() {
     mPrinter.Print("#include \"globalenums.h\"\n"
+                   "#include \"qprotobufobject.h\"\n"
                    "#include <QQmlEngine>");
 
     std::vector<std::string> namespaces;
@@ -46,6 +47,7 @@ void GlobalEnumsSourceGenerator::run() {
 
         printNamespaces(namespaces);
         printRegisterBody(package.second, namespaces);
+        printRegistrationHelperInvokation();
         encloseNamespaces(namespaces.size());
     }
 }
@@ -90,3 +92,8 @@ void GlobalEnumsSourceGenerator::printRegisterBody(const std::list<const FileDes
     Outdent();
     mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
 }
+
+void GlobalEnumsSourceGenerator::printRegistrationHelperInvokation()
+{
+    mPrinter.Print({{"classname", mClassName}}, "static RegistrationHelper<$classname$> helper;\n");
+}

+ 1 - 0
src/generator/globalenumssourcegenerator.h

@@ -41,6 +41,7 @@ public:
     void run() override;
     void printRegisterBody(const std::list<const ::google::protobuf::FileDescriptor *> &list,
                            const std::vector<std::string> &namespaces);
+    void printRegistrationHelperInvokation();
 };
 
 } //namespace generator

+ 0 - 4
src/generator/protobufclassgenerator.cpp

@@ -35,10 +35,6 @@ using namespace ::google::protobuf;
 using namespace ::google::protobuf::io;
 using namespace ::google::protobuf::compiler;
 
-namespace {
-const std::string VariantList("QVariantList");
-}
-
 ProtobufClassGenerator::ProtobufClassGenerator(const Descriptor *message, std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> out)
     : ClassGeneratorBase(message->full_name(), std::move(out))
     , mMessage(message)

+ 6 - 0
src/generator/protobufsourcegenerator.cpp

@@ -105,4 +105,10 @@ void ProtobufSourceGenerator::printFieldsOrdering() {
     }
     Outdent();
     mPrinter.Print(Templates::SemicolonBlockEnclosureTemplate);
+    mPrinter.Print("\n");
+}
+
+void ProtobufSourceGenerator::printRegistrationHelperInvokation()
+{
+    mPrinter.Print({{"classname", mClassName}}, "static RegistrationHelper<$classname$> helper;\n");
 }

+ 2 - 0
src/generator/protobufsourcegenerator.h

@@ -37,12 +37,14 @@ public:
     ProtobufSourceGenerator(const google::protobuf::Descriptor *message, std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
     void printRegisterBody();
     void printFieldsOrdering();
+    void printRegistrationHelperInvokation();
 
     void run() override {
         printClassHeaderInclude();
         printNamespaces();
         printFieldsOrdering();
         printRegisterBody();
+        printRegistrationHelperInvokation();
         encloseNamespaces();
     }
 };

+ 8 - 0
src/protobuf/qprotobufobject.h

@@ -29,3 +29,11 @@
 
 #define Q_DECLARE_PROTOBUF_SERIALIZERS(T) QByteArray serialize() const { return qtprotobuf::ProtobufObjectPrivate::serialize<T>(this); } \
     void deserialize(const QByteArray &array) { qtprotobuf::ProtobufObjectPrivate::deserialize<T>(this, array); }
+
+template<typename T>
+class RegistrationHelper {
+public:
+    RegistrationHelper() {
+        T::registerTypes();
+    }
+};

+ 0 - 2
tests/test_grpc/clienttest.cpp

@@ -44,8 +44,6 @@ class ClientTest : public ::testing::Test
 protected:
     static void SetUpTestCase() {
         QtProtobuf::init();
-        SimpleStringMessage::registerTypes();
-
     }
     static QCoreApplication m_app;
     static int m_argc;

+ 0 - 4
tests/test_protobuf/deserializationtest.cpp

@@ -384,8 +384,6 @@ TEST_F(DeserializationTest, ComplexTypeDeserializeTest)
 {
     ComplexMessage test;
 
-    SimpleEnumMessage::registerTypes();
-
     test.deserialize(QByteArray::fromHex("1208320671776572747908d3ffffffffffffffff01"));
     ASSERT_EQ(-45, test.testFieldInt());
     ASSERT_TRUE(QString::fromUtf8("qwerty") == test.testComplexField().testFieldString());
@@ -534,8 +532,6 @@ TEST_F(DeserializationTest, RepeatedSFixedInt64MessageTest)
 
 TEST_F(DeserializationTest, RepeatedComplexMessageTest)
 {
-    ComplexMessage::registerTypes();
-    SimpleStringMessage::registerTypes();
     RepeatedComplexMessage test;
     test.deserialize(QByteArray::fromHex("0a0c0819120832067177657274790a0c0819120832067177657274790a0c081912083206717765727479"));
     ASSERT_EQ(3, test.testRepeatedComplex().count());

+ 3 - 7
tests/test_protobuf/simpletest.cpp

@@ -308,7 +308,6 @@ TEST_F(SimpleTest, SimpleExternalEnumMessageTest)
 {
     const char* propertyName = "externalEnum";
     using ExternalGlobalEnums = qtprotobufnamespace1::externaltests::GlobalEnums;
-    ExternalGlobalEnums::registerTypes();
 
     SimpleExternalEnumMessage test;
     int propertyNumber = SimpleExternalEnumMessage::propertyOrdering.at(1); //See externalpackagetest.proto
@@ -686,8 +685,6 @@ TEST_F(SimpleTest, RepeatedSFixedInt64MessageTest)
 
 TEST_F(SimpleTest, StepChildEnumMessageTest)
 {
-    qtprotobufnamespace::tests::SimpleEnumMessage::registerTypes();
-
     const char* propertyName = "localStepChildEnum";
     StepChildEnumMessage test;
     int propertyNumber = StepChildEnumMessage::propertyOrdering.at(1); //See simpletest.proto
@@ -701,7 +698,7 @@ TEST_F(SimpleTest, StepChildEnumMessageTest)
 
 TEST_F(SimpleTest, StepChildEnumListMessageTest)
 {
-    qtprotobufnamespace::tests::SimpleEnumMessage::registerTypes();
+
     const char* propertyName = "localStepChildList";
     StepChildEnumMessage test;
     int propertyNumber = StepChildEnumMessage::propertyOrdering.at(2); //See simpletest.proto
@@ -722,7 +719,7 @@ TEST_F(SimpleTest, StepChildEnumListMessageTest)
 TEST_F(SimpleTest, SimpleSInt32StringMapMessageTest)
 {
     const char* propertyName = "mapField";
-    SimpleSInt32StringMapMessage::registerTypes();
+
     SimpleSInt32StringMapMessage test;
     ASSERT_TRUE(QMetaType::isRegistered(qMetaTypeId<SimpleSInt32StringMapMessage::MapFieldEntry>()));
     int propertyNumber = SimpleSInt32StringMapMessage::propertyOrdering.at(1); //See simpletest.proto
@@ -747,7 +744,7 @@ TEST_F(SimpleTest, SimpleSInt32StringMapMessageTest)
 TEST_F(SimpleTest, SimpleStringStringMapMessageTest)
 {
     const char* propertyName = "mapField";
-    SimpleStringStringMapMessage::registerTypes();
+
     SimpleStringStringMapMessage test;
     ASSERT_TRUE(QMetaType::isRegistered(qMetaTypeId<SimpleStringStringMapMessage::MapFieldEntry>()));
     int propertyNumber = SimpleStringStringMapMessage::propertyOrdering.at(13); //See simpletest.proto
@@ -771,7 +768,6 @@ TEST_F(SimpleTest, SimpleStringStringMapMessageTest)
 
 TEST_F(SimpleTest, EmptyMessageTest)
 {
-    EmptyMessage::registerTypes();
     ASSERT_EQ(0, EmptyMessage::propertyOrdering.size());
     ASSERT_EQ(1, EmptyMessage::staticMetaObject.propertyCount());
 }