| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef PROTOBUFFIELDPRESENCECHECKER_P_H |
| 5 | #define PROTOBUFFIELDPRESENCECHECKER_P_H |
| 6 | // |
| 7 | // W A R N I N G |
| 8 | // ------------- |
| 9 | // |
| 10 | // This file is not part of the Qt API. It exists purely as an |
| 11 | // implementation detail. This header file may change from version to |
| 12 | // version without notice, or even be removed. |
| 13 | // |
| 14 | // We mean it. |
| 15 | // |
| 16 | |
| 17 | #include <QtProtobuf/qtprotobuftypes.h> |
| 18 | |
| 19 | #include <QtCore/qtconfigmacros.h> |
| 20 | #include <QtCore/qxptype_traits.h> |
| 21 | |
| 22 | #include <type_traits> |
| 23 | |
| 24 | QT_BEGIN_NAMESPACE |
| 25 | |
| 26 | namespace ProtobufFieldPresenceChecker |
| 27 | { |
| 28 | template <typename T> |
| 29 | using HasIsEmpty = decltype(&T::isEmpty); |
| 30 | template <typename T> |
| 31 | using has_is_empty_method = qxp::is_detected<HasIsEmpty, T>; |
| 32 | |
| 33 | template <typename T> |
| 34 | constexpr inline bool IsProtobufTrivialScalarValueType = false; |
| 35 | template <> |
| 36 | constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::int32> = true; |
| 37 | template <> |
| 38 | constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::int64> = true; |
| 39 | template <> |
| 40 | constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::sint32> = true; |
| 41 | template <> |
| 42 | constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::sint64> = true; |
| 43 | template <> |
| 44 | constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::uint32> = true; |
| 45 | template <> |
| 46 | constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::uint64> = true; |
| 47 | template <> |
| 48 | constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::fixed32> = true; |
| 49 | template <> |
| 50 | constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::fixed64> = true; |
| 51 | template <> |
| 52 | constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::sfixed32> = true; |
| 53 | template <> |
| 54 | constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::sfixed64> = true; |
| 55 | template <> |
| 56 | constexpr inline bool IsProtobufTrivialScalarValueType<float> = true; |
| 57 | template <> |
| 58 | constexpr inline bool IsProtobufTrivialScalarValueType<double> = true; |
| 59 | template <> |
| 60 | constexpr inline bool IsProtobufTrivialScalarValueType<QtProtobuf::boolean> = true; |
| 61 | |
| 62 | using Function = bool (*)(const QVariant &); |
| 63 | |
| 64 | template <typename T, std::enable_if_t<IsProtobufTrivialScalarValueType<T>, bool> = true> |
| 65 | static bool isPresent(const QVariant &value) |
| 66 | { |
| 67 | return value.value<T>() != T{}; |
| 68 | } |
| 69 | |
| 70 | template <typename T, std::enable_if_t<has_is_empty_method<T>::value, bool> = true> |
| 71 | static bool isPresent(const QVariant &value) |
| 72 | { |
| 73 | return !value.value<T>().isEmpty(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | QT_END_NAMESPACE |
| 79 | |
| 80 | #endif // PROTOBUFFIELDPRESENCECHECKER_P_H |
| 81 | |