mirror of https://github.com/qt/qtgrpc.git
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:
parent
4577291626
commit
11f053d77c
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
qt_internal_add_module(Protobuf
|
qt_internal_add_module(Protobuf
|
||||||
SOURCES
|
SOURCES
|
||||||
|
protobuffieldpresencechecker_p.h
|
||||||
qtprotobufglobal.h
|
qtprotobufglobal.h
|
||||||
qabstractprotobufserializer.cpp qabstractprotobufserializer.h
|
qabstractprotobufserializer.cpp qabstractprotobufserializer.h
|
||||||
qprotobufjsonserializer.cpp qprotobufjsonserializer.h
|
qprotobufjsonserializer.cpp qprotobufjsonserializer.h
|
||||||
|
|
|
@ -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
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <QtProtobuf/qprotobufjsonserializer.h>
|
#include <QtProtobuf/qprotobufjsonserializer.h>
|
||||||
|
|
||||||
|
#include <QtProtobuf/private/protobuffieldpresencechecker_p.h>
|
||||||
#include <QtProtobuf/private/qprotobufregistration_p.h>
|
#include <QtProtobuf/private/qprotobufregistration_p.h>
|
||||||
#include <QtProtobuf/private/qprotobufserializer_p.h>
|
#include <QtProtobuf/private/qprotobufserializer_p.h>
|
||||||
#include <QtProtobuf/private/qtprotobufdefs_p.h>
|
#include <QtProtobuf/private/qtprotobufdefs_p.h>
|
||||||
|
@ -105,7 +106,7 @@ public:
|
||||||
Serializer serializer;
|
Serializer serializer;
|
||||||
// deserializer assigned to class
|
// deserializer assigned to class
|
||||||
Deserializer deserializer;
|
Deserializer deserializer;
|
||||||
QProtobufSerializerPrivate::IsPresentChecker isPresent;
|
ProtobufFieldPresenceChecker::Function isPresent;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
@ -113,7 +114,7 @@ public:
|
||||||
{
|
{
|
||||||
return { QProtobufJsonSerializerPrivate::serializeCommon<T>,
|
return { QProtobufJsonSerializerPrivate::serializeCommon<T>,
|
||||||
QProtobufJsonSerializerPrivate::deserializeCommon<T>,
|
QProtobufJsonSerializerPrivate::deserializeCommon<T>,
|
||||||
QProtobufSerializerPrivate::isPresent<T> };
|
ProtobufFieldPresenceChecker::isPresent<T> };
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename L, typename T>
|
template <typename L, typename T>
|
||||||
|
@ -121,7 +122,7 @@ public:
|
||||||
{
|
{
|
||||||
return { QProtobufJsonSerializerPrivate::serializeList<L>,
|
return { QProtobufJsonSerializerPrivate::serializeList<L>,
|
||||||
QProtobufJsonSerializerPrivate::deserializeList<L, T>,
|
QProtobufJsonSerializerPrivate::deserializeList<L, T>,
|
||||||
QProtobufSerializerPrivate::isPresent<L> };
|
ProtobufFieldPresenceChecker::isPresent<L> };
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T,
|
template<typename T,
|
||||||
|
|
|
@ -44,31 +44,24 @@ using SerializerRegistryType =
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
#define QT_CONSTRUCT_PROTOBUF_SERIALIZATION_HANDLER(Type, WireType) \
|
#define QT_CONSTRUCT_PROTOBUF_SERIALIZATION_HANDLER(Type, WireType) \
|
||||||
{ \
|
{ QMetaType::fromType<Type>(), \
|
||||||
QMetaType::fromType<Type>(), \
|
|
||||||
QProtobufSerializerPrivate::serializeWrapper< \
|
QProtobufSerializerPrivate::serializeWrapper< \
|
||||||
Type, QProtobufSerializerPrivate::serializeBasic<Type>>, \
|
Type, QProtobufSerializerPrivate::serializeBasic<Type>>, \
|
||||||
QProtobufSerializerPrivate::deserializeBasic<Type>, \
|
QProtobufSerializerPrivate::deserializeBasic<Type>, \
|
||||||
QProtobufSerializerPrivate::isPresent<Type>, WireType \
|
ProtobufFieldPresenceChecker::isPresent<Type>, WireType }
|
||||||
}
|
|
||||||
#define QT_CONSTRUCT_PROTOBUF_LIST_SERIALIZATION_HANDLER(ListType, Type) \
|
#define QT_CONSTRUCT_PROTOBUF_LIST_SERIALIZATION_HANDLER(ListType, Type) \
|
||||||
{ \
|
{ QMetaType::fromType<ListType>(), \
|
||||||
QMetaType::fromType<ListType>(), \
|
|
||||||
QProtobufSerializerPrivate::serializeWrapper< \
|
QProtobufSerializerPrivate::serializeWrapper< \
|
||||||
ListType, QProtobufSerializerPrivate::serializeListType<Type>>, \
|
ListType, QProtobufSerializerPrivate::serializeListType<Type>>, \
|
||||||
QProtobufSerializerPrivate::deserializeList<Type>, \
|
QProtobufSerializerPrivate::deserializeList<Type>, \
|
||||||
QProtobufSerializerPrivate::isPresent<ListType>, \
|
ProtobufFieldPresenceChecker::isPresent<ListType>, QtProtobuf::WireTypes::LengthDelimited }
|
||||||
QtProtobuf::WireTypes::LengthDelimited \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define QT_CONSTRUCT_PROTOBUF_NON_PACKED_LIST_SERIALIZATION_HANDLER(ListType, Type, WireType) \
|
#define QT_CONSTRUCT_PROTOBUF_NON_PACKED_LIST_SERIALIZATION_HANDLER(ListType, Type, WireType) \
|
||||||
{ \
|
{ QMetaType::fromType<ListType>(), \
|
||||||
QMetaType::fromType<ListType>(), \
|
|
||||||
QProtobufSerializerPrivate::serializeNonPackedWrapper< \
|
QProtobufSerializerPrivate::serializeNonPackedWrapper< \
|
||||||
ListType, QProtobufSerializerPrivate::serializeNonPackedList<Type>>, \
|
ListType, QProtobufSerializerPrivate::serializeNonPackedList<Type>>, \
|
||||||
QProtobufSerializerPrivate::deserializeNonPackedList<Type>, \
|
QProtobufSerializerPrivate::deserializeNonPackedList<Type>, \
|
||||||
QProtobufSerializerPrivate::isPresent<ListType>, WireType \
|
ProtobufFieldPresenceChecker::isPresent<ListType>, WireType }
|
||||||
}
|
|
||||||
|
|
||||||
constexpr SerializerRegistryType<30> IntegratedTypesSerializers = { {
|
constexpr SerializerRegistryType<30> IntegratedTypesSerializers = { {
|
||||||
QT_CONSTRUCT_PROTOBUF_SERIALIZATION_HANDLER(float, QtProtobuf::WireTypes::Fixed32),
|
QT_CONSTRUCT_PROTOBUF_SERIALIZATION_HANDLER(float, QtProtobuf::WireTypes::Fixed32),
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#include <QtProtobuf/qprotobufrepeatediterator.h>
|
#include <QtProtobuf/qprotobufrepeatediterator.h>
|
||||||
#include <QtProtobuf/qtprotobuftypes.h>
|
#include <QtProtobuf/qtprotobuftypes.h>
|
||||||
|
|
||||||
|
#include <QtProtobuf/private/protobuffieldpresencechecker_p.h>
|
||||||
#include <QtProtobuf/private/qprotobufselfcheckiterator_p.h>
|
#include <QtProtobuf/private/qprotobufselfcheckiterator_p.h>
|
||||||
#include <QtProtobuf/private/qtprotobuflogging_p.h>
|
#include <QtProtobuf/private/qtprotobuflogging_p.h>
|
||||||
|
|
||||||
|
@ -131,7 +132,6 @@ public:
|
||||||
// Deserializer is interface function for deserialize method
|
// Deserializer is interface function for deserialize method
|
||||||
using Deserializer = bool (*)(QProtobufSelfcheckIterator &, QVariant &);
|
using Deserializer = bool (*)(QProtobufSelfcheckIterator &, QVariant &);
|
||||||
// Function checks if value in QVariant is considered to be non-ignorable.
|
// 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
|
// SerializationHandlers contains set of objects that required for class
|
||||||
// serializaion/deserialization
|
// serializaion/deserialization
|
||||||
|
@ -140,22 +140,10 @@ public:
|
||||||
QMetaType metaType;
|
QMetaType metaType;
|
||||||
Serializer serializer; // serializer assigned to class
|
Serializer serializer; // serializer assigned to class
|
||||||
Deserializer deserializer; // deserializer 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
|
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;
|
||||||
~QProtobufSerializerPrivate() = default;
|
~QProtobufSerializerPrivate() = default;
|
||||||
// ###########################################################################
|
// ###########################################################################
|
||||||
|
|
Loading…
Reference in New Issue