1// Copyright (C) 2025 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 QMULTIMEDIA_ENUM_TO_STRING_CONVERTER_P_H
5#define QMULTIMEDIA_ENUM_TO_STRING_CONVERTER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qglobal_p.h>
19#include <QtCore/qstring.h>
20
21#include <optional>
22
23QT_BEGIN_NAMESPACE
24
25#define QT_MM_CAT(x, y) QT_MM_IMPL_CAT(x, y)
26#define QT_MM_IMPL_CAT(x, y) x##y
27
28// clang-format off
29
30#define QT_MM_IMPL_GEN_CASE_MAP_ENUM_TO_STRING(SYMBOL, STRING) \
31 case SYMBOL: \
32 return QStringLiteral(STRING);
33
34// clang-format on
35
36#define QT_MM_IMPL_GEN_CASES_ENUM_TO_STRING(seq) \
37 QT_MM_CAT(QT_MM_IMPL_GEN_CASES_ENUM_TO_STRING_1 seq, _END) \
38 static_assert(true, "force semicolon")
39#define QT_MM_IMPL_GEN_CASES_ENUM_TO_STRING_1(x, y) \
40 QT_MM_IMPL_GEN_CASE_MAP_ENUM_TO_STRING(x, y) QT_MM_IMPL_GEN_CASES_ENUM_TO_STRING_2
41#define QT_MM_IMPL_GEN_CASES_ENUM_TO_STRING_2(x, y) \
42 QT_MM_IMPL_GEN_CASE_MAP_ENUM_TO_STRING(x, y) QT_MM_IMPL_GEN_CASES_ENUM_TO_STRING_1
43#define QT_MM_IMPL_GEN_CASES_ENUM_TO_STRING_1_END
44#define QT_MM_IMPL_GEN_CASES_ENUM_TO_STRING_2_END
45
46namespace QtMultimediaPrivate {
47
48struct EnumName
49{
50};
51
52template <typename Enum, typename Role = EnumName>
53struct StringResolver
54{
55 static std::optional<QString> toQString(Enum);
56};
57
58} // namespace QtMultimediaPrivate
59
60// clang-format off
61
62#define QT_MM_MAKE_STRING_RESOLVER(Enum, EnumName, ...) \
63 template <> \
64 struct QtMultimediaPrivate::StringResolver<Enum, EnumName> \
65 { \
66 static std::optional<QString> toQString(Enum arg) \
67 { \
68 switch (arg) { \
69 QT_MM_IMPL_GEN_CASES_ENUM_TO_STRING(__VA_ARGS__); \
70 default: \
71 return std::nullopt; \
72 } \
73 } \
74 }; \
75 static_assert(true, "force semicolon")
76
77// clang-format on
78
79#define QT_MM_DEFINE_QDEBUG_ENUM(EnumType) \
80 QDebug operator<<(QDebug dbg, EnumType arg) \
81 { \
82 std::optional<QString> resolved = \
83 QtMultimediaPrivate::StringResolver<EnumType>::toQString(arg); \
84 if (resolved) \
85 dbg << *resolved; \
86 else \
87 dbg << "Unknown Enum value"; \
88 return dbg; \
89 }
90
91QT_END_NAMESPACE
92
93#endif // QMULTIMEDIA_ENUM_TO_STRING_CONVERTER_P_H
94

source code of qtmultimedia/src/multimedia/qmultimedia_enum_to_string_converter_p.h