AusweisApp
Lade ...
Suche ...
Keine Treffer
EnumHelper.h
gehe zur Dokumentation dieser Datei
1
5#pragma once
6
7#include <QDebug>
8#include <QMetaEnum>
9#include <type_traits>
10
11
12#define defineEnumOperators(enumName)\
13 inline QDebug operator<<(QDebug pDbg, enumName pType)\
14 {\
15 QDebugStateSaver saver(pDbg);\
16 return pDbg.noquote() << Enum<enumName>::getName(pType);\
17 }\
18\
19 inline QDebug operator<<(QDebug pDbg, const QList<enumName>& pList)\
20 {\
21 QDebugStateSaver saver(pDbg);\
22 QByteArrayList list;\
23 for (const auto& entry : pList)\
24 {\
25 list << Enum<enumName>::getName(entry).data();\
26 }\
27 return pDbg.noquote().nospace() << '(' << list.join(QByteArrayView(", ")) << ')';\
28 }\
29\
30 inline QString& operator+=(QString& pStr, enumName pType)\
31 {\
32 pStr += Enum<enumName>::getName(pType);\
33 return pStr;\
34 }\
35\
36 inline QString operator+(const QString& pStr, enumName pType)\
37 {\
38 return pStr + Enum<enumName>::getName(pType);\
39 }\
40\
41 inline QString operator+(enumName pType, const QString& pStr)\
42 {\
43 return Enum<enumName>::getName(pType) + pStr;\
44 }\
45\
46 inline bool operator==(std::underlying_type_t<enumName> pType, enumName pName)\
47 {\
48 return static_cast<std::underlying_type_t<enumName>>(pName) == pType;\
49 }\
50 inline bool operator!=(std::underlying_type_t<enumName> pType, enumName pName)\
51 {\
52 return !(pType == pName);\
53 }\
54\
55 inline size_t qHash(enumName pKey, size_t pSeed)\
56 {\
57 return ::qHash(static_cast<std::underlying_type_t<enumName>>(pKey), pSeed);\
58 }
59
60
61#define defineTypedEnumTypeProperty(enumName, enumType, enumProperty, ...)\
62 namespace Enum##enumName\
63 {\
64 Q_NAMESPACE\
65 enumProperty\
66\
67 enum class enumName : enumType\
68 {\
69 __VA_ARGS__\
70 };\
71\
72 Q_ENUM_NS(enumName)\
73\
74 defineEnumOperators(enumName)\
75 }\
76\
77 using namespace Enum##enumName;
78
79
80#define defineTypedEnumType(enumName, enumType, ...) defineTypedEnumTypeProperty(enumName, enumType, , __VA_ARGS__)
81#define defineEnumType(enumName, ...) defineTypedEnumType(enumName, int, __VA_ARGS__)
82
83/* *INDENT-OFF* */
84#define ENUM_HELPER_OP (
85#define ENUM_HELPER_CP )
86#define ENUM_HELPER_CO ,
87/* *INDENT-ON* */
88#define defineEnumTypeQmlExposed(enumName, ...) defineTypedEnumTypeProperty(enumName, int, Q_CLASSINFO ENUM_HELPER_OP "QML.Element" ENUM_HELPER_CO #enumName ENUM_HELPER_CP, __VA_ARGS__)
89
90
91namespace governikus
92{
93
94template<typename EnumTypeT> class Enum
95{
96 using EnumBaseTypeT = std::underlying_type_t<EnumTypeT>;
97
99
100 private:
101 Enum() = delete;
102 ~Enum() = delete;
103
104 public:
106 {
107 return QMetaEnum::fromType<EnumTypeT>();
108 }
109
110
112 {
114 }
115
116
118 {
119#if (QT_VERSION >= QT_VERSION_CHECK(6, 9, 0))
120 const auto value = static_cast<quint64>(pType);
121#else
122 const auto value = static_cast<int>(pType);
123#endif
124 const char* const name = getQtEnumMetaEnum().valueToKey(value);
125 if (Q_UNLIKELY(name == nullptr))
126 {
127 qCritical().noquote().nospace() << "CRITICAL CONVERSION MISMATCH: UNKNOWN 0x" << QString::number(value, 16);
128 return QLatin1String();
129 }
130
131 return QLatin1String(name);
132 }
133
134
135 [[nodiscard]] static int getCount()
136 {
137 return getQtEnumMetaEnum().keyCount();
138 }
139
140
141 [[nodiscard]] static QList<EnumTypeT> getList()
142 {
143 QList<EnumTypeT> list;
144
146 list.reserve(metaEnum.keyCount());
147 for (int i = 0; i < metaEnum.keyCount(); ++i)
148 {
150 }
151
152 return list;
153 }
154
155
156 [[nodiscard]] static EnumTypeT fromString(const char* const pValue, EnumTypeT pDefault)
157 {
158 bool ok = false;
159 int key = getQtEnumMetaEnum().keyToValue(pValue, &ok);
160 if (ok)
161 {
162 return static_cast<EnumTypeT>(key);
163 }
164 return pDefault;
165 }
166
167
169 {
170 return fromString(pValue.toUtf8().constData(), pDefaultType);
171 }
172
173
174#if (QT_VERSION >= QT_VERSION_CHECK(6, 9, 0))
176#else
177 [[nodiscard]] static bool isValue(int pValue)
178#endif
179 {
180 return getQtEnumMetaEnum().valueToKey(pValue) != nullptr;
181 }
182
183
184#if (QT_VERSION < QT_VERSION_CHECK(6, 9, 0))
185 [[nodiscard]] static bool isValue(ushort pValue)
186 {
187 return isValue(static_cast<int>(pValue));
188 }
189
190
191#endif
192
193 [[nodiscard]] static EnumBaseTypeT getValue(EnumTypeT pType)
194 {
195 return static_cast<EnumBaseTypeT>(pType);
196 }
197
198
199};
200
201
202template<typename T> inline QLatin1String getEnumName(T pType)
203{
204 return Enum<T>::getName(pType);
205}
206
207
208} // namespace governikus
Definition EnumHelper.h:95
static QLatin1String getName()
Definition EnumHelper.h:111
static QLatin1String getName(EnumTypeT pType)
Definition EnumHelper.h:117
static EnumTypeT fromString(const char *const pValue, EnumTypeT pDefault)
Definition EnumHelper.h:156
static EnumTypeT fromString(const QString &pValue, EnumTypeT pDefaultType)
Definition EnumHelper.h:168
static QList< EnumTypeT > getList()
Definition EnumHelper.h:141
static int getCount()
Definition EnumHelper.h:135
static EnumBaseTypeT getValue(EnumTypeT pType)
Definition EnumHelper.h:193
static QMetaEnum getQtEnumMetaEnum()
Definition EnumHelper.h:105
static bool isValue(quint64 pValue)
Definition EnumHelper.h:175
const char * name
Definition http_parser.cpp:473
#define T(v)
Definition http_parser.cpp:237
Defines the AccessRight and AccessRole enum.
Definition CommandApdu.h:17
QSharedPointer< T > decodeObject(const QByteArray &pData, bool pLogging=true)
Template function for decoding an OpenSSL type from DER encoded QByteArray.
Definition ASN1TemplateUtil.h:112
QLatin1String getEnumName(T pType)
Definition EnumHelper.h:202