AusweisApp
Lade ...
Suche ...
Keine Treffer
EnumHelper.h
gehe zur Dokumentation dieser Datei
1
9#pragma once
10
11#include <QDebug>
12#include <QMetaEnum>
13#include <type_traits>
14
15
16#define defineEnumOperators(enumName)\
17 inline QDebug operator<<(QDebug pDbg, enumName pType)\
18 {\
19 QDebugStateSaver saver(pDbg);\
20 return pDbg.noquote() << Enum<enumName>::getName(pType);\
21 }\
22\
23 inline QDebug operator<<(QDebug pDbg, const QList<enumName>& pList)\
24 {\
25 QDebugStateSaver saver(pDbg);\
26 QByteArrayList list;\
27 for (const auto& entry : pList)\
28 {\
29 list << Enum<enumName>::getName(entry).data();\
30 }\
31 return pDbg.noquote().nospace() << '(' << list.join(QByteArrayView(", ")) << ')';\
32 }\
33\
34 inline QString& operator+=(QString& pStr, enumName pType)\
35 {\
36 pStr += Enum<enumName>::getName(pType);\
37 return pStr;\
38 }\
39\
40 inline QString operator+(const QString& pStr, enumName pType)\
41 {\
42 return pStr + Enum<enumName>::getName(pType);\
43 }\
44\
45 inline QString operator+(enumName pType, const QString& pStr)\
46 {\
47 return Enum<enumName>::getName(pType) + pStr;\
48 }\
49\
50 inline bool operator==(std::underlying_type_t<enumName> pType, enumName pName)\
51 {\
52 return static_cast<std::underlying_type_t<enumName>>(pName) == pType;\
53 }\
54 inline bool operator!=(std::underlying_type_t<enumName> pType, enumName pName)\
55 {\
56 return !(pType == pName);\
57 }\
58\
59 inline size_t qHash(enumName pKey, size_t pSeed)\
60 {\
61 return ::qHash(static_cast<std::underlying_type_t<enumName>>(pKey), pSeed);\
62 }
63
64
65#define defineTypedEnumTypeProperty(enumName, enumType, enumProperty, ...)\
66 namespace Enum##enumName\
67 {\
68 Q_NAMESPACE\
69 enumProperty\
70\
71 enum class enumName : enumType\
72 {\
73 __VA_ARGS__\
74 };\
75\
76 Q_ENUM_NS(enumName)\
77\
78 defineEnumOperators(enumName)\
79 }\
80\
81 using namespace Enum##enumName;
82
83
84#define defineTypedEnumType(enumName, enumType, ...) defineTypedEnumTypeProperty(enumName, enumType, , __VA_ARGS__)
85#define defineEnumType(enumName, ...) defineTypedEnumType(enumName, int, __VA_ARGS__)
86
87/* *INDENT-OFF* */
88#define ENUM_HELPER_OP (
89#define ENUM_HELPER_CP )
90#define ENUM_HELPER_CO ,
91/* *INDENT-ON* */
92#define defineEnumTypeQmlExposed(enumName, ...) defineTypedEnumTypeProperty(enumName, int, Q_CLASSINFO ENUM_HELPER_OP "QML.Element" ENUM_HELPER_CO #enumName ENUM_HELPER_CP, __VA_ARGS__)
93
94
95namespace governikus
96{
97
98template<typename EnumTypeT> class Enum
99{
100 using EnumBaseTypeT = std::underlying_type_t<EnumTypeT>;
101
103
104 private:
105 Enum() = delete;
106 ~Enum() = delete;
107
108 public:
110 {
111 return QMetaEnum::fromType<EnumTypeT>();
112 }
113
114
116 {
118 }
119
120
122 {
123 const auto value = static_cast<int>(pType);
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 [[nodiscard]] static bool isValue(int pValue)
175 {
176 return getQtEnumMetaEnum().valueToKey(pValue) != nullptr;
177 }
178
179
181 {
182 return isValue(static_cast<int>(pValue));
183 }
184
185
186 [[nodiscard]] static EnumBaseTypeT getValue(EnumTypeT pType)
187 {
188 return static_cast<EnumBaseTypeT>(pType);
189 }
190
191
192};
193
194
195template<typename T> inline QLatin1String getEnumName(T pType)
196{
197 return Enum<T>::getName(pType);
198}
199
200
201} // namespace governikus
Definition EnumHelper.h:99
static bool isValue(int pValue)
Definition EnumHelper.h:174
static QLatin1String getName()
Definition EnumHelper.h:115
static QLatin1String getName(EnumTypeT pType)
Definition EnumHelper.h:121
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 bool isValue(ushort pValue)
Definition EnumHelper.h:180
static int getCount()
Definition EnumHelper.h:135
static EnumBaseTypeT getValue(EnumTypeT pType)
Definition EnumHelper.h:186
static QMetaEnum getQtEnumMetaEnum()
Definition EnumHelper.h:109
const char * name
Definition http_parser.cpp:473
#define T(v)
Definition http_parser.cpp:237
Implementation of GeneralAuthenticate response APDUs.
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:114
QLatin1String getEnumName(T pType)
Definition EnumHelper.h:195