Browse Source

Add base template class for serialization/deserialization

- Only inheritance added
Alexey Edelev 6 years ago
parent
commit
893915a2dd
4 changed files with 20 additions and 14 deletions
  1. 5 4
      src/generator/templates.h
  2. 0 5
      src/lib/protobufobject.cpp
  3. 14 4
      src/lib/protobufobject.h
  4. 1 1
      tests/CMakeLists.txt

+ 5 - 4
src/generator/templates.h

@@ -34,6 +34,7 @@ namespace qtprotobuf {
 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 <protobufobject.h>\n"
                                       "#include <unordered_map>\n\n";
 
 static const char *InternalIncludeTemplate =  "#include \"$type_lower$.h\"\n";
@@ -43,7 +44,7 @@ static const char *UniversalListModelIncludeTemplate = "\n#include <universallis
 
 static const char *NamespaceTemplate = "\nnamespace $namespace$ {\n";
 
-static const char *ClassDefinitionTemplate = "\nclass $classname$ : public QObject\n"
+static const char *ClassDefinitionTemplate = "\nclass $classname$ : public ProtobufObject<$classname$>\n"
                       "{\n"
                       "    Q_OBJECT\n";
 
@@ -52,9 +53,9 @@ 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) : QObject(parent)\n";
-static const char *CopyConstructorTemplate = "$classname$(const $classname$ &other) : QObject() {\n";
-static const char *MoveConstructorTemplate = "$classname$($classname$ &&other) : QObject() {\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";

+ 0 - 5
src/lib/protobufobject.cpp

@@ -26,8 +26,3 @@
 #include "protobufobject.h"
 
 using namespace qtprotobuf;
-
-ProtobufObject::ProtobufObject(QObject *parent) : QObject(parent)
-{
-
-}

+ 14 - 4
src/lib/protobufobject.h

@@ -26,22 +26,32 @@
 #pragma once
 
 #include <QObject>
+#include <QDebug>
+
+#include <unordered_map>
 
 namespace qtprotobuf {
 
+template <typename T>
 class ProtobufObject : public QObject
 {
 public:
-    explicit ProtobufObject(QObject *parent = nullptr);
+    explicit ProtobufObject(QObject *parent = nullptr) : QObject(parent)
+    {}
 
-    template <typename T>
     QByteArray serialize() {
+        T* instance = dynamic_cast<T*>(this);
+        for(auto field : T::propertyOrdering) {
+            qDebug() << "serialize map: " << field.first << " to: " << field.second;
+            qDebug() << "Property: " << field.second << T::staticMetaObject.property(field.second).name();
+        }
 
+        return QByteArray();
     }
 
-    template <typename T>
     void deserialize(const QByteArray& array) {
-
+        T* instance = dynamic_cast<T*>(this);
+        //TODO
     }
 };
 

+ 1 - 1
tests/CMakeLists.txt

@@ -21,7 +21,7 @@ file(GLOB GENERATED_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/*.cpp)
 list(FILTER GENERATED_SOURCES EXCLUDE REGEX "moc_.*cpp")
 QT5_WRAP_CPP(MOC_SRCS ${GENERATED_HEADERS})
 
-include_directories(${GTEST_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
+include_directories(${GTEST_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/src/lib)
 
 
 file(GLOB HEADERS ${TESTS_OUT_DIR})