Introduce ProtobufFieldPresenceChecker

The namespace contains the functionality that was a part of
QProtobufSerializerPrivate before. Move this functionality to a
separate file, since we want to unlink QProtobufJsonSerializer
implementation from the QProtobufSerializer one.

Pick-to: 6.8
Task-number: QTBUG-128812
Change-Id: I7b97ced9fbfa1076b2e898832575c1931923a2c3
Reviewed-by: Dennis Oberst <dennis.oberst@qt.io>
This commit is contained in:
Alexey Edelev 2024-09-26 02:28:31 +02:00
parent 4577291626
commit 11f053d77c
5 changed files with 104 additions and 41 deletions

View File

@ -3,6 +3,7 @@
qt_internal_add_module(Protobuf
SOURCES
protobuffieldpresencechecker_p.h
qtprotobufglobal.h
qabstractprotobufserializer.cpp qabstractprotobufserializer.h
qprotobufjsonserializer.cpp qprotobufjsonserializer.h

View File

@ -0,0 +1,80 @@
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef PROTOBUFFIELDPRESENCECHECKER_P_H
#define PROTOBUFFIELDPRESENCECHECKER_P_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists purely as an
// implementation detail. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtProtobuf/qtprotobuftypes.h>
#include <QtCore/qtconfigmacros.h>
#include <QtCore/qxptype_traits.h>
#include <type_traits>
QT_BEGIN_NAMESPACE
namespace ProtobufFieldPresenceChecker
{
template <typename T>
using HasIsEmpty = decltype(&T::isEmpty);
template <typename T>
using has_is_empty_method = qxp::is_detected<HasIsEmpty, T>;
template <typename T>
constexpr inline bool IsProtobufTrivialScalarValueType = false;
template <>
constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::int32> = true;
template <>
constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::int64> = true;
template <>
constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::sint32> = true;
template <>
constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::sint64> = true;
template <>
constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::uint32> = true;
template <>
constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::uint64> = true;
template <>
constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::fixed32> = true;
template <>
constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::fixed64> = true;
template <>
constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::sfixed32> = true;
template <>
constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::sfixed64> = true;
template <>
constexpr inline bool IsProtobufTrivialScalarValueType<float> = true;
template <>
constexpr inline bool IsProtobufTrivialScalarValueType<double> = true;
template <>
constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::boolean> = true;
using Function = bool (*)(const QVariant &);
template <typename T, std::enable_if_t<IsProtobufTrivialScalarValueType<T>, bool> = true>
static bool isPresent(const QVariant &value)
{
return value.value<T>() != T{};
}
template <typename T, std::enable_if_t<has_is_empty_method<T>::value, bool> = true>
static bool isPresent(const QVariant &value)
{
return !value.value<T>().isEmpty();
}
}
QT_END_NAMESPACE
#endif // PROTOBUFFIELDPRESENCECHECKER_P_H

View File

@ -3,6 +3,7 @@
#include <QtProtobuf/qprotobufjsonserializer.h>
#include <QtProtobuf/private/protobuffieldpresencechecker_p.h>
#include <QtProtobuf/private/qprotobufregistration_p.h>
#include <QtProtobuf/private/qprotobufserializer_p.h>
#include <QtProtobuf/private/qtprotobufdefs_p.h>
@ -105,7 +106,7 @@ public:
Serializer serializer;
// deserializer assigned to class
Deserializer deserializer;
QProtobufSerializerPrivate::IsPresentChecker isPresent;
ProtobufFieldPresenceChecker::Function isPresent;
};
template <typename T>
@ -113,7 +114,7 @@ public:
{
return { QProtobufJsonSerializerPrivate::serializeCommon<T>,
QProtobufJsonSerializerPrivate::deserializeCommon<T>,
QProtobufSerializerPrivate::isPresent<T> };
ProtobufFieldPresenceChecker::isPresent<T> };
}
template <typename L, typename T>
@ -121,7 +122,7 @@ public:
{
return { QProtobufJsonSerializerPrivate::serializeList<L>,
QProtobufJsonSerializerPrivate::deserializeList<L, T>,
QProtobufSerializerPrivate::isPresent<L> };
ProtobufFieldPresenceChecker::isPresent<L> };
}
template<typename T,

View File

@ -43,32 +43,25 @@ using SerializerRegistryType =
namespace {
#define QT_CONSTRUCT_PROTOBUF_SERIALIZATION_HANDLER(Type, WireType) \
{ \
QMetaType::fromType<Type>(), \
QProtobufSerializerPrivate::serializeWrapper< \
Type, QProtobufSerializerPrivate::serializeBasic<Type>>, \
QProtobufSerializerPrivate::deserializeBasic<Type>, \
QProtobufSerializerPrivate::isPresent<Type>, WireType \
}
#define QT_CONSTRUCT_PROTOBUF_LIST_SERIALIZATION_HANDLER(ListType, Type) \
{ \
QMetaType::fromType<ListType>(), \
QProtobufSerializerPrivate::serializeWrapper< \
ListType, QProtobufSerializerPrivate::serializeListType<Type>>, \
QProtobufSerializerPrivate::deserializeList<Type>, \
QProtobufSerializerPrivate::isPresent<ListType>, \
QtProtobuf::WireTypes::LengthDelimited \
}
#define QT_CONSTRUCT_PROTOBUF_SERIALIZATION_HANDLER(Type, WireType) \
{ QMetaType::fromType<Type>(), \
QProtobufSerializerPrivate::serializeWrapper< \
Type, QProtobufSerializerPrivate::serializeBasic<Type>>, \
QProtobufSerializerPrivate::deserializeBasic<Type>, \
ProtobufFieldPresenceChecker::isPresent<Type>, WireType }
#define QT_CONSTRUCT_PROTOBUF_LIST_SERIALIZATION_HANDLER(ListType, Type) \
{ QMetaType::fromType<ListType>(), \
QProtobufSerializerPrivate::serializeWrapper< \
ListType, QProtobufSerializerPrivate::serializeListType<Type>>, \
QProtobufSerializerPrivate::deserializeList<Type>, \
ProtobufFieldPresenceChecker::isPresent<ListType>, QtProtobuf::WireTypes::LengthDelimited }
#define QT_CONSTRUCT_PROTOBUF_NON_PACKED_LIST_SERIALIZATION_HANDLER(ListType, Type, WireType) \
{ \
QMetaType::fromType<ListType>(), \
QProtobufSerializerPrivate::serializeNonPackedWrapper< \
ListType, QProtobufSerializerPrivate::serializeNonPackedList<Type>>, \
QProtobufSerializerPrivate::deserializeNonPackedList<Type>, \
QProtobufSerializerPrivate::isPresent<ListType>, WireType \
}
{ QMetaType::fromType<ListType>(), \
QProtobufSerializerPrivate::serializeNonPackedWrapper< \
ListType, QProtobufSerializerPrivate::serializeNonPackedList<Type>>, \
QProtobufSerializerPrivate::deserializeNonPackedList<Type>, \
ProtobufFieldPresenceChecker::isPresent<ListType>, WireType }
constexpr SerializerRegistryType<30> IntegratedTypesSerializers = { {
QT_CONSTRUCT_PROTOBUF_SERIALIZATION_HANDLER(float, QtProtobuf::WireTypes::Fixed32),

View File

@ -22,6 +22,7 @@
#include <QtProtobuf/qprotobufrepeatediterator.h>
#include <QtProtobuf/qtprotobuftypes.h>
#include <QtProtobuf/private/protobuffieldpresencechecker_p.h>
#include <QtProtobuf/private/qprotobufselfcheckiterator_p.h>
#include <QtProtobuf/private/qtprotobuflogging_p.h>
@ -131,7 +132,6 @@ public:
// Deserializer is interface function for deserialize method
using Deserializer = bool (*)(QProtobufSelfcheckIterator &, QVariant &);
// Function checks if value in QVariant is considered to be non-ignorable.
using IsPresentChecker = bool (*)(const QVariant &);
// SerializationHandlers contains set of objects that required for class
// serializaion/deserialization
@ -140,22 +140,10 @@ public:
QMetaType metaType;
Serializer serializer; // serializer assigned to class
Deserializer deserializer; // deserializer assigned to class
IsPresentChecker isPresent; // checks if contains non-ignorable value
ProtobufFieldPresenceChecker::Function isPresent; // checks if contains non-ignorable value
QtProtobuf::WireTypes wireType; // Serialization WireType
};
template<typename V, std::enable_if_t<IsI32OrI64<V>::value || IsInt<V>::value, int> = 0>
static bool isPresent(const QVariant &value)
{
return value.value<V>() != 0;
}
template<typename V, std::enable_if_t<!IsI32OrI64<V>::value && !IsInt<V>::value, int> = 0>
static bool isPresent(const QVariant &value)
{
return !value.value<V>().isEmpty();
}
QProtobufSerializerPrivate() = default;
~QProtobufSerializerPrivate() = default;
// ###########################################################################