Explorar o código

Improve registration of qtprotobuf classes

- Hide registration of qtprotobuf classes using RegistrationHelper
- Remove deprecated usage of QtProtobuf::init()
Alexey Edelev %!s(int64=5) %!d(string=hai) anos
pai
achega
02e424b7e7

+ 0 - 2
examples/addressbook/main.cpp

@@ -29,7 +29,6 @@
 #include <QQmlApplicationEngine>
 #include <QQmlContext>
 
-#include <qtprotobuf.h>
 #include "addressbookengine.h"
 #include <contacts.h>
 #include <contact.h>
@@ -42,7 +41,6 @@ using namespace qtprotobuf::examples;
 int main(int argc, char *argv[])
 {
     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
-    qtprotobuf::QtProtobuf::init();
 
     qmlRegisterType<ContactsListModel>("examples.addressbook", 1, 0, "ContactsListModel");
     QGuiApplication app(argc, argv);

+ 0 - 3
examples/simplechat/main.cpp

@@ -29,7 +29,6 @@
 #include <QQmlApplicationEngine>
 #include <QQmlContext>
 
-#include <qtprotobuf.h>
 #include <chatmessage.h>
 #include <user.h>
 #include <chatmessages.h>
@@ -47,8 +46,6 @@ int main(int argc, char *argv[])
 
     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
 
-    qtprotobuf::QtProtobuf::init();
-
     qmlRegisterUncreatableType<ChatMessageModel>("examples.simplechat",  1, 0, "ChatMessageModel", "");
 
     QQmlApplicationEngine engine;

+ 3 - 2
src/generator/globalenumssourcegenerator.cpp

@@ -37,8 +37,9 @@ GlobalEnumsSourceGenerator::GlobalEnumsSourceGenerator(const PackagesList &packa
   , mPackageList(packageList) {}
 
 void GlobalEnumsSourceGenerator::run() {
-    mPrinter.Print("#include \"globalenums.h\"\n"
-                   "#include \"qprotobufobject.h\"\n"
+    mPrinter.Print("#include <globalenums.h>\n"
+                   "#include <qprotobufobject.h>\n"
+                   "#include <registrationhelper.h>\n\n"
                    "#include <QQmlEngine>");
 
     std::vector<std::string> namespaces;

+ 1 - 0
src/generator/protobufsourcegenerator.h

@@ -41,6 +41,7 @@ public:
 
     void run() override {
         printClassHeaderInclude();
+        mPrinter.Print({{"include", "registrationhelper.h"}}, Templates::ExternalIncludeTemplate);
         printNamespaces();
         printFieldsOrdering();
         printRegisterBody();

+ 2 - 2
src/protobuf/CMakeLists.txt

@@ -21,10 +21,10 @@ file(GLOB HEADERS
     qprotobufobject_p.h
     qprotobufobject.h
     qtprotobuftypes.h
-    qtprotobuf.h
     selfcheckiterator.h
     qtprotobuflogging.h
-    qtprotobuf_global.h)
+    qtprotobuf_global.h
+    registrationhelper.h)
 
 add_library(${TARGET} SHARED ${SOURCES})
 target_compile_definitions(${TARGET} PRIVATE QTPROTOBUF_LIB)

+ 1 - 1
src/protobuf/QtProtobufGen.cmake.in

@@ -45,7 +45,7 @@ function(generate_qtprotobuf)
                 ${PROTO_FILES}
             WORKING_DIRECTORY ${OUT_DIR}
             DEPENDS ${PROTO_FILES} ${QTPROTOBUF_EXECUTABLE}
-            COMMENT "Generating test headers"
+            COMMENT "Generating QtProtobuf sources ${QTPROTOBUF_GENERATED_SOURCES} ${QTPROTOBUF_GENERATED_HEADERS}"
     )
     
     add_custom_target(${GEN_TARGET} DEPENDS ${QTPROTOBUF_GENERATED_SOURCES} ${QTPROTOBUF_GENERATED_HEADERS} ${PROTO_FILES})

+ 0 - 12
src/protobuf/qprotobufobject.h

@@ -29,15 +29,3 @@
 
 #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); }
-
-namespace qtprotobuf {
-
-template<typename T>
-class RegistrationHelper {
-public:
-    RegistrationHelper() {
-        T::registerTypes();
-    }
-};
-
-}

+ 43 - 1
src/protobuf/qtprotobuf.cpp

@@ -23,4 +23,46 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
-#include "qtprotobuf.h"
+#include "qtprotobuftypes.h"
+#include "qprotobufobject.h"
+#include "registrationhelper.h"
+
+//registerProtobufType Not a part of API
+#define registerProtobufType(X) qRegisterMetaType<X>(# X);\
+                                qRegisterMetaType<X>("qtprotobuf::"# X);
+
+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);
+
+        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();
+    }
+};
+
+static RegistrationHelper<QtProtobuf> helper;
+}

+ 0 - 74
src/protobuf/qtprotobuf.h

@@ -1,74 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
- *
- * 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.
- */
-
-#pragma once
-
-#include "qtprotobuftypes.h"
-#include "qprotobufobject.h"
-
-//registerProtobufType Not a part of API
-#define registerProtobufType(X) qRegisterMetaType<X>(# X);\
-                                qRegisterMetaType<X>("qtprotobuf::"# X);
-
-namespace qtprotobuf {
-
-class QTPROTOBUFSHARED_EXPORT QtProtobuf {
-public:
-    static void init() {
-        static bool registationDone = false;
-        if (!registationDone) {
-            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(DoubleList);
-            registerProtobufType(FloatList);
-            ProtobufObjectPrivate::registerSerializers();
-            registationDone = true;
-        }
-    }
-};
-
-}
-
-#undef registerProtobufType

+ 37 - 0
src/protobuf/registrationhelper.h

@@ -0,0 +1,37 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2019 Viktor Kopp <vifactor@gmail.com>, Alexey Edelev <semlanik@gmail.com>
+ *
+ * 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.
+ */
+
+namespace qtprotobuf {
+
+template<typename T>
+class RegistrationHelper {
+public:
+    RegistrationHelper() {
+        T::registerTypes();
+    }
+};
+
+}
+

+ 0 - 2
tests/test_grpc/clienttest.cpp

@@ -25,7 +25,6 @@
 
 #include "testserviceclient.h"
 #include "http2channel.h"
-#include "qtprotobuf.h"
 #include "insecurecredentials.h"
 #include "blobmessage.h"
 
@@ -43,7 +42,6 @@ class ClientTest : public ::testing::Test
 {
 protected:
     static void SetUpTestCase() {
-        QtProtobuf::init();
     }
     static QCoreApplication m_app;
     static int m_argc;

+ 0 - 2
tests/test_protobuf/deserializationtest.cpp

@@ -24,7 +24,6 @@
  */
 
 #include "deserializationtest.h"
-#include "qtprotobuf.h"
 #include "simpleboolmessage.h"
 #include "simplefixedint32message.h"
 #include "simplefixedint64message.h"
@@ -93,7 +92,6 @@ using namespace qtprotobuf;
 
 DeserializationTest::DeserializationTest()
 {
-    QtProtobuf::init();
 }
 
 TEST_F(DeserializationTest, FixedInt32MessageDeserializeTest)

+ 0 - 3
tests/test_protobuf/serializationtest.cpp

@@ -76,15 +76,12 @@
 #include "fieldindextest3message.h"
 #include "fieldindextest4message.h"
 
-#include "qtprotobuf.h"
-
 using namespace qtprotobufnamespace::tests;
 using namespace qtprotobuf::tests;
 using namespace qtprotobuf;
 
 SerializationTest::SerializationTest()
 {
-    qtprotobuf::QtProtobuf::init();
 }
 
 TEST_F(SerializationTest, IntMessageSerializeTest)

+ 0 - 2
tests/test_protobuf/simpletest.cpp

@@ -66,7 +66,6 @@
 #include "emptymessage.h"
 
 #include "globalenums.h"
-#include "qtprotobuf.h"
 #include <QVariantList>
 #include <QMetaProperty>
 
@@ -83,7 +82,6 @@ public:
     // see simpletest.proto for property names and their field indices
     SimpleTest()
     {
-        QtProtobuf::init();
     }
 
     template<typename MessageType, typename PropertyType>