// 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 QPROTOBUFREGISTRATION_H #define QPROTOBUFREGISTRATION_H #if 0 # pragma qt_sync_skip_header_check # pragma qt_sync_stop_processing #endif #include #include #include #include #include #include #include #include QT_BEGIN_NAMESPACE class QProtobufMessage; namespace QtProtobuf { using RegisterFunction = void (*)(); // This struct is used for type registrations in generated code struct ProtoTypeRegistrar { Q_PROTOBUF_EXPORT explicit ProtoTypeRegistrar(QtProtobuf::RegisterFunction initializer); }; } namespace QtProtobufPrivate { extern Q_PROTOBUF_EXPORT void registerOrdering(QMetaType type, QProtobufPropertyOrdering ordering); template ::value, bool> = true> static std::optional> intToEnumList(const QList &v) { QList enumList; for (const auto &intValue : v) enumList.append(static_cast(intValue.t)); return enumList; } template ::value, bool> = true> static QList enumToIntList(const QList &v) { QList intList; for (const auto enumValue : v) intList.append(QtProtobuf::int64(qToUnderlying(enumValue))); return intList; } template ::value, bool> = true> static std::optional> stringToEnumList(const QStringList &v) { static const QMetaEnum metaEnum = QMetaEnum::fromType(); QList enumList; bool ok = false; for (const auto &stringValue : v) { T enumV = T(metaEnum.keyToValue(stringValue.toUtf8().data(), &ok)); if (!ok) return std::nullopt; enumList.append(enumV); } return enumList; } template ::value, bool> = true> static QStringList enumToStringList(const QList &v) { static const QMetaEnum metaEnum = QMetaEnum::fromType(); QStringList stringList; for (const auto enumValue : v) stringList.append(QString::fromUtf8(metaEnum.valueToKey(qToUnderlying(enumValue)))); return stringList; } } // namespace QtProtobufPrivate Q_PROTOBUF_EXPORT void qRegisterProtobufTypes(); template = true> inline void qRegisterProtobufType() { T::registerTypes(); QMetaType::registerMutableView< QList, QProtobufRepeatedIterator>(&QProtobufRepeatedIterator::fromList); QtProtobufPrivate::registerOrdering(QMetaType::fromType(), T::staticPropertyOrdering); } template = true> inline void qRegisterProtobufMapType() { QMetaType::registerMutableView< QHash, QProtobufRepeatedIterator>(&QProtobufRepeatedIterator::fromHash); } template ::value, bool> = true> inline void qRegisterProtobufEnumType() { QMetaType::registerConverter, QList>(QtProtobufPrivate::enumToIntList); QMetaType::registerConverter, QList>(QtProtobufPrivate::intToEnumList); QMetaType::registerConverter, QStringList>(QtProtobufPrivate::enumToStringList); QMetaType::registerConverter>(QtProtobufPrivate::stringToEnumList); } QT_END_NAMESPACE #endif // QPROTOBUFREGISTRATION_H