Bladeren bron

Encapsulate qprotobuf private handlers

- Hide QProtobufSerializerPrivate handlers into private class space
Alexey Edelev 5 jaren geleden
bovenliggende
commit
b803aeb5ee
2 gewijzigde bestanden met toevoegingen van 36 en 29 verwijderingen
  1. 34 28
      src/protobuf/qprotobufserializer.cpp
  2. 2 1
      src/protobuf/qprotobufserializer_p.h

+ 34 - 28
src/protobuf/qprotobufserializer.cpp

@@ -133,30 +133,7 @@ QByteArray QProtobufSerializer::serializeMapPair(const QVariant &key, const QVar
 
 void QProtobufSerializer::deserializeMapPair(QVariant &key, QVariant &value, QProtobufSelfcheckIterator &it) const
 {
-    int mapIndex = 0;
-    WireTypes type = WireTypes::UnknownWireType;
-    unsigned int count = QProtobufSerializerPrivate::deserializeVarintCommon<uint32>(it);
-    qProtoDebug() << __func__ << "count:" << count;
-    QProtobufSelfcheckIterator last = it + count;
-    while (it != last) {
-        QProtobufSerializerPrivate::decodeHeader(it, mapIndex, type);
-        if (mapIndex == 1) {
-            //Only simple types are supported as keys
-            int userType = key.userType();
-            auto &handler = QProtobufSerializerPrivate::handlers.at(userType);//throws if not found
-            handler.deserializer(it, key);
-        } else {
-            //TODO: replace with some common function
-            int userType = value.userType();
-            auto basicIt = QProtobufSerializerPrivate::handlers.find(userType);
-            if (basicIt != QProtobufSerializerPrivate::handlers.end()) {
-                basicIt->second.deserializer(it, value);
-            } else {
-                auto &handler = QtProtobufPrivate::findHandler(userType);
-                handler.deserializer(this, it, value);//throws if not implemented
-            }
-        }
-    }
+    d_ptr->deserializeMapPair(key, value, it);
 }
 
 QByteArray QProtobufSerializer::serializeEnum(int64 value, const QMetaEnum &/*metaEnum*/, const QtProtobuf::QProtobufMetaProperty &metaProperty) const
@@ -284,8 +261,8 @@ QByteArray QProtobufSerializerPrivate::serializeProperty(const QVariant &propert
 
     //TODO: replace with some common function
     int fieldIndex = metaProperty.protoFieldIndex();
-    auto basicIt = QProtobufSerializerPrivate::handlers.find(userType);
-    if (basicIt != QProtobufSerializerPrivate::handlers.end()) {
+    auto basicIt = handlers.find(userType);
+    if (basicIt != handlers.end()) {
         type = basicIt->second.type;
         result.append(basicIt->second.serializer(propertyValue, fieldIndex));
         if (fieldIndex != QtProtobufPrivate::NotUsedFieldIndex
@@ -330,14 +307,43 @@ void QProtobufSerializerPrivate::deserializeProperty(QObject *object, const QPro
     int userType = metaProperty.userType();
 
     //TODO: replace with some common function
-    auto basicIt = QProtobufSerializerPrivate::handlers.find(userType);
-    if (basicIt != QProtobufSerializerPrivate::handlers.end()) {
+    auto basicIt = handlers.find(userType);
+    if (basicIt != handlers.end()) {
         basicIt->second.deserializer(it, newPropertyValue);
     } else {
         auto &handler = QtProtobufPrivate::findHandler(userType);
         handler.deserializer(q_ptr, it, newPropertyValue);
     }
+
     metaProperty.write(object, newPropertyValue);
 }
 
+void QProtobufSerializerPrivate::deserializeMapPair(QVariant &key, QVariant &value, QProtobufSelfcheckIterator &it)
+{
+    int mapIndex = 0;
+    WireTypes type = WireTypes::UnknownWireType;
+    unsigned int count = QProtobufSerializerPrivate::deserializeVarintCommon<uint32>(it);
+    qProtoDebug() << __func__ << "count:" << count;
+    QProtobufSelfcheckIterator last = it + count;
+    while (it != last) {
+        QProtobufSerializerPrivate::decodeHeader(it, mapIndex, type);
+        if (mapIndex == 1) {
+            //Only simple types are supported as keys
+            int userType = key.userType();
+            auto &handler = handlers.at(userType);//throws if not found
+            handler.deserializer(it, key);
+        } else {
+            //TODO: replace with some common function
+            int userType = value.userType();
+            auto basicIt = handlers.find(userType);
+            if (basicIt != handlers.end()) {
+                basicIt->second.deserializer(it, value);
+            } else {
+                auto &handler = QtProtobufPrivate::findHandler(userType);
+                handler.deserializer(q_ptr, it, value);//throws if not implemented
+            }
+        }
+    }
+}
+
 QProtobufSerializerPrivate::SerializerRegistry QProtobufSerializerPrivate::handlers = {};

+ 2 - 1
src/protobuf/qprotobufserializer_p.h

@@ -442,8 +442,9 @@ public:
     QByteArray serializeProperty(const QVariant &propertyValue, const QProtobufMetaProperty &metaProperty);
     void deserializeProperty(QObject *object, const QProtobufMetaObject &metaObject, QProtobufSelfcheckIterator &it);
 
-    static SerializerRegistry handlers;
+    void deserializeMapPair(QVariant &key, QVariant &value, QProtobufSelfcheckIterator &it);
 private:
+    static SerializerRegistry handlers;
     QProtobufSerializer *q_ptr;
 };