Explorar o código

Rework serializers registry

- Replace access to serializers registry by unordered_map::at() function
  that throws exception in case if serializer was not registred
Alexey Edelev %!s(int64=5) %!d(string=hai) anos
pai
achega
e6c82cea1f
Modificáronse 2 ficheiros con 9 adicións e 13 borrados
  1. 2 6
      src/protobuf/qprotobufobject_p.cpp
  2. 7 7
      src/protobuf/qprotobufobject_p.h

+ 2 - 6
src/protobuf/qprotobufobject_p.cpp

@@ -98,9 +98,7 @@ QByteArray ProtobufObjectPrivate::serializeUserType(const QVariant &propertyValu
     qProtoDebug() << __func__ << "propertyValue" << propertyValue << "fieldIndex" << fieldIndex;
 
     int userType = propertyValue.userType();
-    auto serializer = serializers[userType];
-    Q_ASSERT_X(serializer.serializer, "ProtobufObjectPrivate", "Serialization of unknown type is impossible. QtProtobuf::init() missed?");
-
+    auto serializer = serializers.at(userType);//Throws exception if not found
     type = serializer.type;
     return serializer.serializer(propertyValue, fieldIndex);
 }
@@ -142,8 +140,6 @@ void ProtobufObjectPrivate::deserializeUserType(const QMetaProperty &metaType, Q
     qProtoDebug() << __func__ << "userType" << metaType.userType() << "typeName" << metaType.typeName()
                   << "currentByte:" << QString::number((*it), 16);
     int userType = metaType.userType();
-    auto deserializer = serializers[userType].deserializer;
-    Q_ASSERT_X(deserializer, "ProtobufObjectPrivate", "Deserialization of unknown type is impossible. QtProtobuf::init() missed?");
-
+    auto deserializer = serializers.at(userType).deserializer;//Throws exception if not found
     deserializer(it, newValue);
 }

+ 7 - 7
src/protobuf/qprotobufobject_p.h

@@ -303,8 +303,8 @@ public:
     static QByteArray serializeMap(const QMap<K,V> &mapValue, int &outFieldIndex) {
         using ItType = typename QMap<K,V>::const_iterator;
         QByteArray mapResult;
-        auto kSerializer = serializers[qMetaTypeId<K>()];
-        auto vSerializer = serializers[qMetaTypeId<V>()];
+        auto kSerializer = serializers.at(qMetaTypeId<K>());//Throws exception if not found
+        auto vSerializer = serializers.at(qMetaTypeId<V>());//Throws exception if not found
 
         for ( ItType it = mapValue.constBegin(); it != mapValue.constEnd(); it++) {
             QByteArray result;
@@ -322,8 +322,8 @@ public:
     static QByteArray serializeMap(const QMap<K, QSharedPointer<V>> &mapValue, int &outFieldIndex) {
         using ItType = typename QMap<K, QSharedPointer<V>>::const_iterator;
         QByteArray mapResult;
-        auto kSerializer = serializers[qMetaTypeId<K>()];
-        auto vSerializer = serializers[qMetaTypeId<V*>()];
+        auto kSerializer = serializers.at(qMetaTypeId<K>());//Throws exception if not found
+        auto vSerializer = serializers.at(qMetaTypeId<V*>());//Throws exception if not found
 
         for ( ItType it = mapValue.constBegin(); it != mapValue.constEnd(); it++) {
             QByteArray result;
@@ -477,7 +477,7 @@ public:
     static QVariant deserializeList(QByteArray::const_iterator &it) {
         qProtoDebug() << __func__ << "currentByte:" << QString::number((*it), 16);
         QVariant newValue;
-        serializers[qMetaTypeId<V*>()].deserializer(it, newValue);
+        serializers.at(qMetaTypeId<V*>()).deserializer(it, newValue);//Throws exception if not found
         return newValue;
     }
 
@@ -560,7 +560,7 @@ public:
     template <typename T,
               typename std::enable_if_t<std::is_base_of<QObject, T>::value, int> = 0>
     static T *deserializeMapHelper(QByteArray::const_iterator &it) {
-        auto serializer = serializers[qMetaTypeId<T *>()];
+        auto serializer = serializers.at(qMetaTypeId<T *>());//Throws exception if not found
         QVariant value;
         serializer.deserializer(it, value);
         return value.value<T *>();
@@ -569,7 +569,7 @@ public:
     template <typename T,
               typename std::enable_if_t<!std::is_base_of<QObject, T>::value, int> = 0>
     static T deserializeMapHelper(QByteArray::const_iterator &it) {
-        auto serializer = serializers[qMetaTypeId<T>()];
+        auto serializer = serializers.at(qMetaTypeId<T>());//Throws exception if not found
         QVariant value;
         serializer.deserializer(it, value);
         return value.value<T>();