Browse Source

Implement fixed int types support by adding extra metatype and "using" definition

Tatyana Borisova 6 years ago
parent
commit
0040b0caa8

+ 3 - 0
src/generator/classgeneratorbase.cpp

@@ -127,6 +127,9 @@ void ClassGeneratorBase::printIncludes(const Descriptor *message, std::set<std::
         std::transform(std::begin(modelTypeNameLower), std::end(modelTypeNameLower), std::begin(modelTypeNameLower), ::tolower);
         mPrinter.Print({{"type_lower", modelTypeNameLower}}, InternalIncludeTemplate);
     }
+
+    // Add using of template for correct build of autogenerated classes
+    mPrinter.Print(UsingQtPeotobufNamespaceTemplate);
 }
 
 void ClassGeneratorBase::printNamespaces(const std::string &package)

+ 13 - 12
src/generator/templates.h

@@ -41,14 +41,15 @@ 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 *UsingQtPeotobufNamespaceTemplate = "\nusing namespace qtprotobuf;\n";
 static const char *NamespaceTemplate = "\nnamespace $namespace$ {\n";
 
 static const char *NonProtoClassDefinitionTemplate = "\nclass $classname$ : public QObject\n"
-                      "{\n"
-                      "    Q_OBJECT\n";
+                                                     "{\n"
+                                                     "    Q_OBJECT\n";
 static const char *ClassDefinitionTemplate = "\nclass $classname$ : public ProtobufObject<$classname$>\n"
-                      "{\n"
-                      "    Q_OBJECT\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";
@@ -66,7 +67,7 @@ static const char *AssignmentOperatorTemplate = "$classname$ &operator =(const $
 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 ";
+                                           "    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"
@@ -84,11 +85,11 @@ static const char *SetterTemplateSimpleType = "void set$property_name_cap$($type
                                               "}\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";
+                                               "    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";
@@ -114,8 +115,8 @@ static const std::unordered_map<::google::protobuf::FieldDescriptor::Type, std::
     //        {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"},
-    //        {FieldDescriptor::TYPE_FIXED64, "int"},//Not supported see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
-    //        {FieldDescriptor::TYPE_FIXED32, "int"},//Not supported see https://doc.qt.io/qt-5/qtqml-typesystem-basictypes.html
+    {::google::protobuf::FieldDescriptor::TYPE_FIXED64, "FixedInt64"},
+    {::google::protobuf::FieldDescriptor::TYPE_FIXED32, "FixedInt32"},
     {::google::protobuf::FieldDescriptor::TYPE_BOOL, "bool"},
     {::google::protobuf::FieldDescriptor::TYPE_STRING, "QString"},
     {::google::protobuf::FieldDescriptor::TYPE_GROUP, ""},//Not supported and deprecated in protobuf

+ 2 - 0
src/lib/qtprotobuf.h

@@ -40,6 +40,8 @@ public:
             qRegisterMetaType<IntList>("IntList");
             qRegisterMetaType<FloatList>("FloatList");
             qRegisterMetaType<DoubleList>("DoubleList");
+            qRegisterMetaType<FixedInt32>("FixedInt32");
+            qRegisterMetaType<FixedInt64>("FixedInt64");
             registationDone = true;
         }
     }

+ 5 - 0
src/lib/qtprotobuftypes.h

@@ -34,8 +34,13 @@ using IntList = QList<int>;
 using FloatList = QList<float>;
 using DoubleList = QList<double>;
 
+using FixedInt32 = unsigned int;
+using FixedInt64 = unsigned long;
+
 }
 
 Q_DECLARE_METATYPE(qtprotobuf::IntList)
 Q_DECLARE_METATYPE(qtprotobuf::FloatList)
 Q_DECLARE_METATYPE(qtprotobuf::DoubleList)
+Q_DECLARE_METATYPE(qtprotobuf::FixedInt32)
+Q_DECLARE_METATYPE(qtprotobuf::FixedInt64)

+ 8 - 0
tests/proto/simpletest.proto

@@ -22,6 +22,14 @@ message SimpleBytesMessage {
     bytes testFieldBytes = 1;
 }
 
+message SimpleFixedInt32Message {
+    fixed32 testFieldFixedInt32 = 1;
+}
+
+message SimpleFixedInt64Message {
+    fixed64 testFieldFixedInt64 = 1;
+}
+
 message ComplexMessage {
     int32 testFieldInt = 1;
     SimpleStringMessage testComplexField = 2;

+ 19 - 0
tests/serializationtest.cpp

@@ -26,6 +26,8 @@
 #include "serializationtest.h"
 
 #include "simpleintmessage.h"
+#include "simplefixedint32message.h"
+#include "simplefixedint64message.h"
 #include "simplefloatmessage.h"
 #include "simpledoublemessage.h"
 #include "simplestringmessage.h"
@@ -179,6 +181,23 @@ TEST_F(SerializationTest, IntMessageSerializeTest)
     ASSERT_EQ(result.at(3), '\x04');
 }
 
+// Need help with result size
+//TEST_F(SerializationTest, FixedInt32MessageSerializeTest)
+//{
+//    return;
+//    SimpleFixedInt32Message test;
+//    test.setTestFieldFixedInt32(15);
+//    QByteArray result = test.serialize();
+//    ASSERT_EQ(result.size(), 2);
+//    ASSERT_EQ(result.at(0), 0x08);
+//    ASSERT_EQ(result.at(1), '\x1e');
+//}
+
+//TEST_F(SerializationTest, FixedInt64MessageSerializeTest)
+//{
+//    return;
+//}
+
 TEST_F(SerializationTest, FloatMessageSerializeTest)
 {
     constexpr int FloatMessageSize = 5;

+ 27 - 1
tests/simpletest.cpp

@@ -26,6 +26,8 @@
 #include "simpletest.h"
 
 #include "simpleintmessage.h"
+#include "simplefixedint32message.h"
+#include "simplefixedint64message.h"
 #include "simplestringmessage.h"
 #include "simplefloatmessage.h"
 #include "simpledoublemessage.h"
@@ -58,6 +60,30 @@ TEST_F(SimpleTest, SimpleIntMessageTest)
     ASSERT_EQ(test.testFieldInt(), 1);
 }
 
+TEST_F(SimpleTest, SimpleFixedInt32MessageTest)
+{
+    const char* propertyName = "testFieldFixedInt32";
+    SimpleFixedInt32Message test;
+    int propertyNumber = SimpleFixedInt32Message::propertyOrdering.at(1); //See simpletest.proto
+    ASSERT_EQ(SimpleFixedInt32Message::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<FixedInt32>());
+    ASSERT_STREQ(SimpleFixedInt32Message::staticMetaObject.property(propertyNumber).name(), propertyName);
+    ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
+    ASSERT_EQ(test.property(propertyName).value<FixedInt32>(), 1);
+    ASSERT_EQ(test.testFieldFixedInt32(), 1);
+}
+
+TEST_F(SimpleTest, SimpleFixedInt64MessageTest)
+{
+    const char* propertyName = "testFieldFixedInt64";
+    SimpleFixedInt64Message test;
+    int propertyNumber = SimpleFixedInt64Message::propertyOrdering.at(1); //See simpletest.proto
+    ASSERT_EQ(SimpleFixedInt64Message::staticMetaObject.property(propertyNumber).type(), qMetaTypeId<FixedInt64>());
+    ASSERT_STREQ(SimpleFixedInt64Message::staticMetaObject.property(propertyNumber).name(), propertyName);
+    ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue(1)));
+    ASSERT_EQ(test.property(propertyName).value<FixedInt64>(), 1);
+    ASSERT_EQ(test.testFieldFixedInt64(), 1);
+}
+
 TEST_F(SimpleTest, SimpleStringMessageTest)
 {
     const char* propertyName = "testFieldString";
@@ -77,7 +103,7 @@ TEST_F(SimpleTest, SimpleFloatMessageTest)
     int propertyNumber = SimpleFloatMessage::propertyOrdering.at(7); //See simpletest.proto
     ASSERT_EQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).type(), QMetaType::Float);
     ASSERT_STREQ(SimpleFloatMessage::staticMetaObject.property(propertyNumber).name(), "testFieldFloat");
-    ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<float>(1.55)));
+    ASSERT_TRUE(test.setProperty(propertyName, QVariant::fromValue<float>(1.55f)));
     ASSERT_TRUE(qFuzzyCompare(test.property(propertyName).toFloat(), 1.55f));
     ASSERT_TRUE(qFuzzyCompare(test.testFieldFloat(), 1.55f));
 }