Remove QAbstractProtobufSerializer::(de)serializeObject interface

Replace the interface methods with the qxp::function_ref with the
respective arguments. This will break the dependency between the
protobuf message registry and the QAbstractProtobufSerializer.

Now the registry is standalone unit with own rules that can be used
or not used by the serializers. If serializers want to use the
registry, they now need to supply it with the respective
(de)serializers for the message fields of the protobuf message type.

QAbstractProtobufSerializer implementations meanwhile only need to
know how to (de)serialize the whole messages but not fields. This
was the last chunk of the interface cleaning.

Adjust the implementation of the existing serializers respectively.

Pick-to: 6.8
Task-number: QTBUG-123626
Change-Id: I772a330e8f9757d998f0e7ea138d12bbfce020b8
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Alexey Edelev 2024-08-21 19:09:52 +02:00
parent acac560fb1
commit 8ec55cb0cf
10 changed files with 129 additions and 175 deletions

View File

@ -37,11 +37,6 @@ public:
virtual DeserializationError deserializationError() const = 0; virtual DeserializationError deserializationError() const = 0;
virtual QString deserializationErrorString() const = 0; virtual QString deserializationErrorString() const = 0;
virtual void
serializeObject(const QProtobufMessage *message,
const QtProtobufPrivate::QProtobufFieldInfo &fieldInfo) const = 0;
virtual bool deserializeObject(QProtobufMessage *message) const = 0;
protected: protected:
virtual QByteArray serializeMessage(const QProtobufMessage *message) const = 0; virtual QByteArray serializeMessage(const QProtobufMessage *message) const = 0;
virtual bool deserializeMessage(QProtobufMessage *message, QByteArrayView data) const = 0; virtual bool deserializeMessage(QProtobufMessage *message, QByteArrayView data) const = 0;

View File

@ -187,8 +187,7 @@ public:
return QJsonValue(arr); return QJsonValue(arr);
} }
QProtobufJsonSerializerPrivate(QProtobufJsonSerializer *q) QProtobufJsonSerializerPrivate()
: qPtr(q)
{ {
[[maybe_unused]] static bool initialized = []() -> bool { [[maybe_unused]] static bool initialized = []() -> bool {
handlers[qMetaTypeId<QtProtobuf::int32>()] = createCommonHandler<QtProtobuf::int32>(); handlers[qMetaTypeId<QtProtobuf::int32>()] = createCommonHandler<QtProtobuf::int32>();
@ -275,7 +274,7 @@ public:
auto store = activeValue.toObject(); auto store = activeValue.toObject();
activeValue = QJsonObject(); activeValue = QJsonObject();
auto *messageProperty = propertyValue.value<QProtobufMessage *>(); auto *messageProperty = propertyValue.value<QProtobufMessage *>();
serializeObject(messageProperty); serializeObjectImpl(messageProperty);
store.insert(fieldInfo.jsonName().toString(), activeValue); store.insert(fieldInfo.jsonName().toString(), activeValue);
activeValue = store; activeValue = store;
return; return;
@ -302,6 +301,11 @@ public:
return; return;
} }
auto serializerFunction = [this](const QProtobufMessage *message,
const QProtobufFieldInfo &fieldInfo) {
this->serializeObject(message, fieldInfo);
};
auto handler = QtProtobufPrivate::findHandler(metaType); auto handler = QtProtobufPrivate::findHandler(metaType);
if (handler.serializer) { if (handler.serializer) {
if (fieldFlags & QtProtobufPrivate::FieldFlag::Repeated if (fieldFlags & QtProtobufPrivate::FieldFlag::Repeated
@ -309,12 +313,12 @@ public:
const auto fieldName = fieldInfo.jsonName().toString(); const auto fieldName = fieldInfo.jsonName().toString();
QJsonObject activeObject = activeValue.toObject(); QJsonObject activeObject = activeValue.toObject();
activeValue = activeObject.value(fieldName).toArray(); activeValue = activeObject.value(fieldName).toArray();
handler.serializer(qPtr, propertyValue.constData(), fieldInfo); handler.serializer(serializerFunction, propertyValue.constData(), fieldInfo);
if (!activeValue.toArray().empty()) if (!activeValue.toArray().empty())
activeObject.insert(fieldName, activeValue); activeObject.insert(fieldName, activeValue);
activeValue = activeObject; activeValue = activeObject;
} else { } else {
handler.serializer(qPtr, propertyValue.constData(), fieldInfo); handler.serializer(serializerFunction, propertyValue.constData(), fieldInfo);
} }
} else { } else {
QJsonObject activeObject = activeValue.toObject(); QJsonObject activeObject = activeValue.toObject();
@ -333,24 +337,8 @@ public:
} }
} }
void serializeObject(const QProtobufMessage *message) void serializeObject(const QProtobufMessage *message, const QProtobufFieldInfo &fieldInfo);
{ void serializeObjectImpl(const QProtobufMessage *message);
// if a message is not initialized, just return empty { }
if (message) {
auto ordering = message->propertyOrdering();
Q_ASSERT(ordering != nullptr);
for (int index = 0; index < ordering->fieldCount(); ++index) {
int fieldIndex = ordering->fieldNumber(index);
Q_ASSERT_X(fieldIndex <= ProtobufFieldNumMax && fieldIndex >= ProtobufFieldNumMin,
"",
"fieldIndex is out of range");
QProtobufFieldInfo fieldInfo(*ordering, index);
QVariant propertyValue = QtProtobufSerializerHelpers::messageProperty(message,
fieldInfo);
serializeProperty(propertyValue, fieldInfo);
}
}
}
template <typename T> template <typename T>
static QVariant deserializeCommon(const QJsonValue &value, bool &ok) static QVariant deserializeCommon(const QJsonValue &value, bool &ok)
@ -533,6 +521,9 @@ public:
auto handler = QtProtobufPrivate::findHandler(metaType); auto handler = QtProtobufPrivate::findHandler(metaType);
if (handler.deserializer) { if (handler.deserializer) {
auto deserializerFunction = [this](QProtobufMessage *message) {
return this->deserializeObject(message);
};
if (activeValue.isArray()) { if (activeValue.isArray()) {
QJsonArray array = activeValue.toArray(); QJsonArray array = activeValue.toArray();
if (array.isEmpty()) { if (array.isEmpty()) {
@ -540,15 +531,14 @@ public:
activeValue = {}; activeValue = {};
return propertyData; return propertyData;
} }
if (!array.at(0).isObject()) { // Enum array if (!array.at(0).isObject()) { // Enum array
handler.deserializer(qPtr, propertyData.data()); handler.deserializer(deserializerFunction, propertyData.data());
ok = propertyData.isValid(); ok = propertyData.isValid();
} else { } else {
while (!array.isEmpty() && while (!array.isEmpty() &&
deserializationError == QAbstractProtobufSerializer::NoError) { deserializationError == QAbstractProtobufSerializer::NoError) {
activeValue = array.takeAt(0); activeValue = array.takeAt(0);
handler.deserializer(qPtr, propertyData.data()); handler.deserializer(deserializerFunction, propertyData.data());
} }
ok = propertyData.isValid(); ok = propertyData.isValid();
} }
@ -557,7 +547,7 @@ public:
// This is required to deserialize the map fields. // This is required to deserialize the map fields.
while (!activeValue.isNull() while (!activeValue.isNull()
&& deserializationError == QAbstractProtobufSerializer::NoError) { && deserializationError == QAbstractProtobufSerializer::NoError) {
handler.deserializer(qPtr, propertyData.data()); handler.deserializer(deserializerFunction, propertyData.data());
} }
ok = propertyData.isValid(); ok = propertyData.isValid();
} }
@ -715,9 +705,6 @@ public:
QVariant cachedPropertyValue; QVariant cachedPropertyValue;
int cachedIndex = -1; int cachedIndex = -1;
private:
QProtobufJsonSerializer *qPtr;
}; };
bool QProtobufJsonSerializerPrivate::storeCachedValue(QProtobufMessage *message) bool QProtobufJsonSerializerPrivate::storeCachedValue(QProtobufMessage *message)
@ -736,14 +723,31 @@ bool QProtobufJsonSerializerPrivate::storeCachedValue(QProtobufMessage *message)
QProtobufJsonSerializerPrivate::SerializerRegistry QProtobufJsonSerializerPrivate::handlers = {}; QProtobufJsonSerializerPrivate::SerializerRegistry QProtobufJsonSerializerPrivate::handlers = {};
void QProtobufJsonSerializerPrivate::serializeObjectImpl(const QProtobufMessage *message)
{
// if a message is not initialized, just return empty { }
if (message) {
auto ordering = message->propertyOrdering();
Q_ASSERT(ordering != nullptr);
for (int index = 0; index < ordering->fieldCount(); ++index) {
int fieldIndex = ordering->fieldNumber(index);
Q_ASSERT_X(fieldIndex <= ProtobufFieldNumMax && fieldIndex >= ProtobufFieldNumMin, "",
"fieldIndex is out of range");
QProtobufFieldInfo fieldInfo(*ordering, index);
QVariant propertyValue = QtProtobufSerializerHelpers::messageProperty(message,
fieldInfo);
serializeProperty(propertyValue, fieldInfo);
}
}
}
void QProtobufJsonSerializerPrivate::clearError() void QProtobufJsonSerializerPrivate::clearError()
{ {
deserializationError = QAbstractProtobufSerializer::NoError; deserializationError = QAbstractProtobufSerializer::NoError;
deserializationErrorString.clear(); deserializationErrorString.clear();
} }
QProtobufJsonSerializer::QProtobufJsonSerializer() : QProtobufJsonSerializer::QProtobufJsonSerializer() : d_ptr(new QProtobufJsonSerializerPrivate)
d_ptr(new QProtobufJsonSerializerPrivate(this))
{ {
} }
@ -772,7 +776,7 @@ QByteArray QProtobufJsonSerializer::serializeMessage(const QProtobufMessage *mes
{ {
d_ptr->clearError(); d_ptr->clearError();
d_ptr->activeValue = QJsonObject(); d_ptr->activeValue = QJsonObject();
d_ptr->serializeObject(message); d_ptr->serializeObjectImpl(message);
QJsonDocument doc; QJsonDocument doc;
doc.setObject(d_ptr->activeValue.toObject()); doc.setObject(d_ptr->activeValue.toObject());
d_ptr->activeValue = QJsonObject(); d_ptr->activeValue = QJsonObject();
@ -801,36 +805,31 @@ bool QProtobufJsonSerializer::deserializeMessage(QProtobufMessage *message,
return d_ptr->deserializeObject(message); return d_ptr->deserializeObject(message);
} }
void QProtobufJsonSerializer::serializeObject(const QProtobufMessage *message, void QProtobufJsonSerializerPrivate::serializeObject(const QProtobufMessage *message,
const QProtobufFieldInfo &fieldInfo) const const QProtobufFieldInfo &fieldInfo)
{ {
if (d_ptr->activeValue.isArray()) { if (activeValue.isArray()) {
auto store = d_ptr->activeValue.toArray(); auto store = activeValue.toArray();
d_ptr->activeValue = QJsonObject(); activeValue = QJsonObject();
d_ptr->serializeObject(message); serializeObjectImpl(message);
store.append(d_ptr->activeValue); store.append(activeValue);
d_ptr->activeValue = store; activeValue = store;
} else { } else {
auto store = d_ptr->activeValue.toObject(); auto store = activeValue.toObject();
const QString fieldName = fieldInfo.jsonName().toString(); const QString fieldName = fieldInfo.jsonName().toString();
d_ptr->activeValue = QJsonObject(); activeValue = QJsonObject();
if (fieldInfo.fieldFlags() & QtProtobufPrivate::FieldFlag::Map) { if (fieldInfo.fieldFlags() & QtProtobufPrivate::FieldFlag::Map) {
QJsonObject mapObject = store.value(fieldName).toObject(); QJsonObject mapObject = store.value(fieldName).toObject();
d_ptr->serializeObject(message); serializeObjectImpl(message);
mapObject.insert(message->property("key").toString(), mapObject.insert(message->property("key").toString(),
d_ptr->activeValue.toObject().value("value"_L1)); activeValue.toObject().value("value"_L1));
store.insert(fieldName, mapObject); store.insert(fieldName, mapObject);
} else { } else {
d_ptr->serializeObject(message); serializeObjectImpl(message);
store.insert(fieldName, d_ptr->activeValue); store.insert(fieldName, activeValue);
} }
d_ptr->activeValue = store; activeValue = store;
} }
} }
bool QProtobufJsonSerializer::deserializeObject(QProtobufMessage *message) const
{
return d_ptr->deserializeObject(message);
}
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -25,15 +25,10 @@ public:
DeserializationError deserializationError() const override; DeserializationError deserializationError() const override;
QString deserializationErrorString() const override; QString deserializationErrorString() const override;
private: protected:
QByteArray serializeMessage(const QProtobufMessage *message) const override; QByteArray serializeMessage(const QProtobufMessage *message) const override;
bool deserializeMessage(QProtobufMessage *message, QByteArrayView data) const override; bool deserializeMessage(QProtobufMessage *message, QByteArrayView data) const override;
void serializeObject(const QProtobufMessage *message,
const QtProtobufPrivate::QProtobufFieldInfo &fieldInfo)
const override;
bool deserializeObject(QProtobufMessage *message) const override;
private: private:
std::unique_ptr<QProtobufJsonSerializerPrivate> d_ptr; std::unique_ptr<QProtobufJsonSerializerPrivate> d_ptr;
}; };

View File

@ -11,14 +11,14 @@
#include <QtProtobuf/qtprotobufglobal.h> #include <QtProtobuf/qtprotobufglobal.h>
#include <QtProtobuf/qabstractprotobufserializer.h>
#include <QtProtobuf/qprotobufmessage.h> #include <QtProtobuf/qprotobufmessage.h>
#include <QtProtobuf/qprotobufpropertyordering.h> #include <QtProtobuf/qprotobufpropertyordering.h>
#include <QtProtobuf/qtprotobuftypes.h> #include <QtProtobuf/qtprotobuftypes.h>
#include <QtCore/qmetatype.h>
#include <QtCore/qmetaobject.h>
#include <QtCore/qhash.h> #include <QtCore/qhash.h>
#include <QtCore/qmetaobject.h>
#include <QtCore/qmetatype.h>
#include <QtCore/qxpfunctional.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -35,9 +35,12 @@ struct ProtoTypeRegistrar
namespace QtProtobufPrivate { namespace QtProtobufPrivate {
extern Q_PROTOBUF_EXPORT void registerOrdering(QMetaType type, QProtobufPropertyOrdering ordering); extern Q_PROTOBUF_EXPORT void registerOrdering(QMetaType type, QProtobufPropertyOrdering ordering);
using Serializer = void (*)(const QAbstractProtobufSerializer *, const void *, using MessageFieldSerializer = qxp::function_ref<void(const QProtobufMessage *,
const QProtobufFieldInfo &); const QProtobufFieldInfo &)>;
using Deserializer = void (*)(const QAbstractProtobufSerializer *, void *); using MessageFieldDeserializer = qxp::function_ref<bool(QProtobufMessage *)>;
using Serializer = void (*)(MessageFieldSerializer, const void *, const QProtobufFieldInfo &);
using Deserializer = void (*)(MessageFieldDeserializer, void *);
struct SerializationHandler struct SerializationHandler
{ {
@ -48,11 +51,6 @@ struct SerializationHandler
extern Q_PROTOBUF_EXPORT void registerHandler(QMetaType type, Serializer serializer, extern Q_PROTOBUF_EXPORT void registerHandler(QMetaType type, Serializer serializer,
Deserializer deserializer); Deserializer deserializer);
inline void ensureSerializer(const QAbstractProtobufSerializer *serializer)
{
Q_ASSERT_X(serializer != nullptr, "QAbstractProtobufSerializer", "Serializer is null");
}
inline void ensureValue(const void *valuePtr) inline void ensureValue(const void *valuePtr)
{ {
Q_ASSERT_X(valuePtr != nullptr, "QAbstractProtobufSerializer", "Value is nullptr"); Q_ASSERT_X(valuePtr != nullptr, "QAbstractProtobufSerializer", "Value is nullptr");
@ -60,21 +58,19 @@ inline void ensureValue(const void *valuePtr)
template <typename V, template <typename V,
typename std::enable_if_t<std::is_base_of<QProtobufMessage, V>::value, int> = 0> typename std::enable_if_t<std::is_base_of<QProtobufMessage, V>::value, int> = 0>
void serializeList(const QAbstractProtobufSerializer *serializer, const void *valuePtr, void serializeList(MessageFieldSerializer serializer, const void *valuePtr,
const QProtobufFieldInfo &fieldInfo) const QProtobufFieldInfo &fieldInfo)
{ {
ensureSerializer(serializer);
ensureValue(valuePtr); ensureValue(valuePtr);
for (const auto &value : *static_cast<const QList<V> *>(valuePtr)) for (const auto &value : *static_cast<const QList<V> *>(valuePtr))
serializer->serializeObject(&value, fieldInfo); serializer(&value, fieldInfo);
} }
template <typename K, typename V> template <typename K, typename V>
void serializeMap(const QAbstractProtobufSerializer *serializer, const void *valuePtr, void serializeMap(MessageFieldSerializer serializer, const void *valuePtr,
const QProtobufFieldInfo &fieldInfo) const QProtobufFieldInfo &fieldInfo)
{ {
ensureSerializer(serializer);
ensureValue(valuePtr); ensureValue(valuePtr);
using QProtobufMapEntry = QProtobufMapEntry<K, const V>; using QProtobufMapEntry = QProtobufMapEntry<K, const V>;
@ -90,32 +86,29 @@ void serializeMap(const QAbstractProtobufSerializer *serializer, const void *val
else else
el.setValue(v); el.setValue(v);
serializer->serializeObject(&el, fieldInfo); serializer(&el, fieldInfo);
} }
} }
template <typename V, template <typename V,
typename std::enable_if_t<std::is_base_of<QProtobufMessage, V>::value, int> = 0> typename std::enable_if_t<std::is_base_of<QProtobufMessage, V>::value, int> = 0>
void deserializeList(const QAbstractProtobufSerializer *serializer, void *valuePtr) void deserializeList(MessageFieldDeserializer deserializer, void *valuePtr)
{ {
ensureSerializer(serializer);
ensureValue(valuePtr); ensureValue(valuePtr);
auto *listPtr = static_cast<QList<V> *>(valuePtr); auto *listPtr = static_cast<QList<V> *>(valuePtr);
if (V item; serializer->deserializeObject(&item)) if (V item; deserializer(&item))
listPtr->append(std::move(item)); listPtr->append(std::move(item));
} }
template <typename K, typename V> template <typename K, typename V>
void deserializeMap(const QAbstractProtobufSerializer *serializer, void *valuePtr) void deserializeMap(MessageFieldDeserializer deserializer, void *valuePtr)
{ {
ensureSerializer(serializer);
using QProtobufMapEntry = QProtobufMapEntry<K, V>; using QProtobufMapEntry = QProtobufMapEntry<K, V>;
static_assert(!std::is_pointer_v<typename QProtobufMapEntry::KeyType>, static_assert(!std::is_pointer_v<typename QProtobufMapEntry::KeyType>,
"Map key must not be message"); "Map key must not be message");
auto *mapPtr = static_cast<QHash<K, V> *>(valuePtr); auto *mapPtr = static_cast<QHash<K, V> *>(valuePtr);
if (QProtobufMapEntry el; serializer->deserializeObject(&el)) { if (QProtobufMapEntry el; deserializer(&el)) {
auto it = mapPtr->emplace(std::move(el).key()); auto it = mapPtr->emplace(std::move(el).key());
if constexpr (std::is_pointer_v<typename QProtobufMapEntry::ValueType>) if constexpr (std::is_pointer_v<typename QProtobufMapEntry::ValueType>)

View File

@ -185,7 +185,7 @@ findIntegratedTypeHandler(QMetaType metaType, bool nonPacked)
/*! /*!
Constructs a new serializer instance. Constructs a new serializer instance.
*/ */
QProtobufSerializer::QProtobufSerializer() : d_ptr(new QProtobufSerializerPrivate(this)) QProtobufSerializer::QProtobufSerializer() : d_ptr(new QProtobufSerializerPrivate)
{ {
} }
@ -316,17 +316,6 @@ bool QProtobufSerializerPrivate::deserializeObject(QProtobufMessage *message)
return it.isValid(); return it.isValid();
} }
void QProtobufSerializer::serializeObject(const QProtobufMessage *message,
const QProtobufFieldInfo &fieldInfo) const
{
d_ptr->serializeObject(message, fieldInfo);
}
bool QProtobufSerializer::deserializeObject(QProtobufMessage *message) const
{
return d_ptr->deserializeObject(message);
}
void QProtobufSerializerPrivate::serializeEnumList(const QList<QtProtobuf::int64> &value, void QProtobufSerializerPrivate::serializeEnumList(const QList<QtProtobuf::int64> &value,
const QProtobufFieldInfo &fieldInfo) const QProtobufFieldInfo &fieldInfo)
{ {
@ -356,10 +345,6 @@ bool QProtobufSerializerPrivate::deserializeEnumList(QList<QtProtobuf::int64> &v
return true; return true;
} }
QProtobufSerializerPrivate::QProtobufSerializerPrivate(QProtobufSerializer *q) : q_ptr(q)
{
}
/*! /*!
\internal \internal
Encode a property field index and its type into output bytes. Encode a property field index and its type into output bytes.
@ -519,7 +504,11 @@ void QProtobufSerializerPrivate::serializeProperty(const QVariant &propertyValue
qProtoWarning() << "No serializer for type" << propertyValue.typeName(); qProtoWarning() << "No serializer for type" << propertyValue.typeName();
return; return;
} }
handler.serializer(q_ptr, propertyValue.constData(), fieldInfo);
handler.serializer([this](const QProtobufMessage *message,
const QProtobufFieldInfo
&fieldInfo) { this->serializeObject(message, fieldInfo); },
propertyValue.constData(), fieldInfo);
} }
bool QProtobufSerializerPrivate::deserializeProperty(QProtobufMessage *message) bool QProtobufSerializerPrivate::deserializeProperty(QProtobufMessage *message)
@ -644,7 +633,9 @@ bool QProtobufSerializerPrivate::deserializeProperty(QProtobufMessage *message)
QCoreApplication::translate("QtProtobuf", error.toUtf8().data())); QCoreApplication::translate("QtProtobuf", error.toUtf8().data()));
return false; return false;
} }
handler.deserializer(q_ptr, cachedPropertyValue.data()); handler.deserializer([this](QProtobufMessage
*message) { return this->deserializeObject(message); },
cachedPropertyValue.data());
} }
return true; return true;

View File

@ -27,15 +27,11 @@ public:
QString deserializationErrorString() const override; QString deserializationErrorString() const override;
void shouldPreserveUnknownFields(bool preserveUnknownFields); void shouldPreserveUnknownFields(bool preserveUnknownFields);
private:
protected:
QByteArray serializeMessage(const QProtobufMessage *message) const override; QByteArray serializeMessage(const QProtobufMessage *message) const override;
bool deserializeMessage(QProtobufMessage *message, QByteArrayView data) const override; bool deserializeMessage(QProtobufMessage *message, QByteArrayView data) const override;
void serializeObject(const QProtobufMessage *message,
const QtProtobufPrivate::QProtobufFieldInfo &fieldInfo)
const override;
bool deserializeObject(QProtobufMessage *message) const override;
private: private:
std::unique_ptr<QProtobufSerializerPrivate> d_ptr; std::unique_ptr<QProtobufSerializerPrivate> d_ptr;
}; };

View File

@ -154,7 +154,7 @@ public:
return !value.value<V>().isEmpty(); return !value.value<V>().isEmpty();
} }
explicit QProtobufSerializerPrivate(QProtobufSerializer *q); QProtobufSerializerPrivate() = default;
~QProtobufSerializerPrivate() = default; ~QProtobufSerializerPrivate() = default;
// ########################################################################### // ###########################################################################
// Serializers // Serializers
@ -538,7 +538,6 @@ public:
private: private:
Q_DISABLE_COPY_MOVE(QProtobufSerializerPrivate) Q_DISABLE_COPY_MOVE(QProtobufSerializerPrivate)
QProtobufSerializer *q_ptr;
}; };
inline bool inline bool

View File

@ -40,10 +40,10 @@ constexpr inline bool is_optional_v<std::optional<T>> = true;
template<typename QType, typename PType> template<typename QType, typename PType>
void registerQtTypeHandler() void registerQtTypeHandler()
{ {
registerHandler(QMetaType::fromType<QType>(), registerHandler(
[](const QAbstractProtobufSerializer *serializer, const void *valuePtr, QMetaType::fromType<QType>(),
[](QtProtobufPrivate::MessageFieldSerializer serializer, const void *valuePtr,
const QProtobufFieldInfo &info) { const QProtobufFieldInfo &info) {
QtProtobufPrivate::ensureSerializer(serializer);
QtProtobufPrivate::ensureValue(valuePtr); QtProtobufPrivate::ensureValue(valuePtr);
auto do_convert = [](const QType *qtype) { auto do_convert = [](const QType *qtype) {
@ -55,20 +55,18 @@ void registerQtTypeHandler()
} }
}; };
std::optional<PType> object = std::optional<PType> object = do_convert(static_cast<const QType *>(valuePtr));
do_convert(static_cast<const QType *>(valuePtr));
if (object) { if (object) {
serializer->serializeObject(&(object.value()), info); serializer(&(object.value()), info);
} else { } else {
warnTypeConversionError(); warnTypeConversionError();
} }
}, },
[](const QAbstractProtobufSerializer *serializer, void *valuePtr) { [](QtProtobufPrivate::MessageFieldDeserializer deserializer, void *valuePtr) {
QtProtobufPrivate::ensureSerializer(serializer);
QtProtobufPrivate::ensureValue(valuePtr); QtProtobufPrivate::ensureValue(valuePtr);
PType object; PType object;
serializer->deserializeObject(&object); deserializer(&object);
auto res = convert(object); auto res = convert(object);
auto *qtypePtr = static_cast<QType *>(valuePtr); auto *qtypePtr = static_cast<QType *>(valuePtr);
if constexpr (is_optional_v<decltype(res)>) { if constexpr (is_optional_v<decltype(res)>) {

View File

@ -35,11 +35,9 @@ AnyPrivate::AnyPrivate(const QMetaObject *metaObject,
{ {
} }
static void serializerProxy(const QAbstractProtobufSerializer *serializer, const void *object, static void serializerProxy(QtProtobufPrivate::MessageFieldSerializer serializer,
const QProtobufFieldInfo &fieldInfo) const void *object, const QProtobufFieldInfo &fieldInfo)
{ {
QtProtobufPrivate::ensureSerializer(serializer);
if (object == nullptr) if (object == nullptr)
return; return;
@ -50,14 +48,12 @@ static void serializerProxy(const QAbstractProtobufSerializer *serializer, const
google::protobuf::Any realAny; google::protobuf::Any realAny;
realAny.setValue(any.value()); realAny.setValue(any.value());
realAny.setTypeUrl(any.typeUrl()); realAny.setTypeUrl(any.typeUrl());
serializer->serializeObject(&realAny, fieldInfo); serializer(&realAny, fieldInfo);
} }
static void listSerializerProxy(const QAbstractProtobufSerializer *serializer, const void *object, static void listSerializerProxy(QtProtobufPrivate::MessageFieldSerializer serializer,
const QProtobufFieldInfo &fieldInfo) const void *object, const QProtobufFieldInfo &fieldInfo)
{ {
QtProtobufPrivate::ensureSerializer(serializer);
if (object == nullptr) if (object == nullptr)
return; return;
@ -66,20 +62,19 @@ static void listSerializerProxy(const QAbstractProtobufSerializer *serializer, c
google::protobuf::Any realAny; google::protobuf::Any realAny;
realAny.setValue(any.value()); realAny.setValue(any.value());
realAny.setTypeUrl(any.typeUrl()); realAny.setTypeUrl(any.typeUrl());
serializer->serializeObject(&realAny, fieldInfo); serializer(&realAny, fieldInfo);
} }
} }
static void listDeserializerProxy(const QAbstractProtobufSerializer *deserializer, void *object) static void listDeserializerProxy(QtProtobufPrivate::MessageFieldDeserializer deserializer,
void *object)
{ {
QtProtobufPrivate::ensureSerializer(deserializer);
if (object == nullptr) if (object == nullptr)
return; return;
auto &anyList = *static_cast<QList<Any> *>(object); auto &anyList = *static_cast<QList<Any> *>(object);
google::protobuf::Any realAny; google::protobuf::Any realAny;
if (deserializer->deserializeObject(&realAny)) { if (deserializer(&realAny)) {
Any any; Any any;
any.setTypeUrl(realAny.typeUrl()); any.setTypeUrl(realAny.typeUrl());
any.setValue(realAny.value()); any.setValue(realAny.value());
@ -89,15 +84,14 @@ static void listDeserializerProxy(const QAbstractProtobufSerializer *deserialize
} }
} }
static void deserializerProxy(const QAbstractProtobufSerializer *deserializer, void *object) static void deserializerProxy(QtProtobufPrivate::MessageFieldDeserializer deserializer,
void *object)
{ {
QtProtobufPrivate::ensureSerializer(deserializer);
if (object == nullptr) if (object == nullptr)
return; return;
google::protobuf::Any realAny; google::protobuf::Any realAny;
if (deserializer->deserializeObject(&realAny)) { if (deserializer(&realAny)) {
auto &any = *static_cast<Any *>(object); auto &any = *static_cast<Any *>(object);
any.setTypeUrl(realAny.typeUrl()); any.setTypeUrl(realAny.typeUrl());
any.setValue(realAny.value()); any.setValue(realAny.value());

View File

@ -39,12 +39,6 @@ protected:
{ {
return true; return true;
} }
void serializeObject(const QProtobufMessage *,
const QtProtobufPrivate::QProtobufFieldInfo &) const override
{
}
bool deserializeObject(QProtobufMessage *) const override { return true; }
}; };
} // namespace } // namespace