AusweisApp2
EnumHelper.h
gehe zur Dokumentation dieser Datei
1 
7 #pragma once
8 
9 #include <QDebug>
10 #include <QMetaEnum>
11 #include <type_traits>
12 
13 
14 namespace governikus
15 {
16 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
17 #define defineQHash(enumName)\
18  inline size_t qHash(enumName pKey, size_t pSeed)\
19  {\
20  return ::qHash(static_cast<std::underlying_type<enumName>::type>(pKey), pSeed);\
21  }
22 #else
23 #define defineQHash(enumName)\
24  inline uint qHash(enumName pKey, uint pSeed)\
25  {\
26  return ::qHash(static_cast<std::underlying_type<enumName>::type>(pKey), pSeed);\
27  }
28 #endif
29 
30 #define defineEnumOperators(enumName)\
31  inline QDebug operator<<(QDebug pDbg, enumName pType)\
32  {\
33  QDebugStateSaver saver(pDbg);\
34  return pDbg.noquote() << Enum<enumName>::getName(pType);\
35  }\
36 \
37  inline QString& operator+=(QString & pStr, enumName pType)\
38  {\
39  pStr += Enum<enumName>::getName(pType);\
40  return pStr;\
41  }\
42 \
43  inline QString operator+(const QString& pStr, enumName pType)\
44  {\
45  return pStr + Enum<enumName>::getName(pType);\
46  }\
47 \
48  inline QString operator+(enumName pType, const QString& pStr)\
49  {\
50  return Enum<enumName>::getName(pType) + pStr;\
51  }\
52 \
53  inline bool operator==(std::underlying_type<enumName>::type pType, enumName pName)\
54  {\
55  return static_cast<std::underlying_type<enumName>::type>(pName) == pType;\
56  }\
57  inline bool operator!=(std::underlying_type<enumName>::type pType, enumName pName)\
58  {\
59  return !(pType == pName);\
60  }\
61  defineQHash(enumName)
62 
63 
64 #define defineTypedEnumType(enumName, enumType, ...)\
65  class Enum##enumName\
66  {\
67  Q_GADGET\
68  private:\
69  Enum##enumName();\
70  Q_DISABLE_COPY(Enum##enumName)\
71 \
72  public:\
73  enum class enumName : enumType\
74  {\
75  __VA_ARGS__\
76  };\
77 \
78  Q_ENUM(enumName)\
79  };\
80 \
81  using enumName = Enum##enumName::enumName;\
82 \
83  defineEnumOperators(enumName)
84 
85 
86 #define defineEnumType(enumName, ...) defineTypedEnumType(enumName, int, __VA_ARGS__)
87 
88 
89 template<typename EnumTypeT> class Enum
90 {
91  using EnumBaseTypeT = typename std::underlying_type<EnumTypeT>::type;
92 
93  private:
94  Enum() = delete;
95  Q_DISABLE_COPY(Enum)
96 
97  public:
98  static inline QMetaEnum getQtEnumMetaEnum()
99  {
100  return QMetaEnum::fromType<EnumTypeT>();
101  }
102 
103 
104  static QLatin1String getName()
105  {
106  return QLatin1String(getQtEnumMetaEnum().name());
107  }
108 
109 
110  static QLatin1String getName(EnumTypeT pType)
111  {
112  const int value = static_cast<int>(pType);
113  const char* const name = getQtEnumMetaEnum().valueToKey(value);
114  if (Q_UNLIKELY(name == nullptr))
115  {
116  qCritical().noquote().nospace() << "CRITICAL CONVERSION MISMATCH: UNKNOWN 0x" << QString::number(value, 16);
117  return QLatin1String();
118  }
119 
120  return QLatin1String(name);
121  }
122 
123 
124  static int getCount()
125  {
126  return getQtEnumMetaEnum().keyCount();
127  }
128 
129 
130  static QVector<EnumTypeT> getList()
131  {
132  QVector<EnumTypeT> list;
133 
134  const QMetaEnum metaEnum = getQtEnumMetaEnum();
135  list.reserve(metaEnum.keyCount());
136  for (int i = 0; i < metaEnum.keyCount(); ++i)
137  {
138  list << static_cast<EnumTypeT>(metaEnum.value(i));
139  }
140 
141  return list;
142  }
143 
144 
145  static EnumTypeT fromString(const char* const pValue, EnumTypeT pDefault)
146  {
147  bool ok = false;
148  int key = getQtEnumMetaEnum().keyToValue(pValue, &ok);
149  if (ok)
150  {
151  return static_cast<EnumTypeT>(key);
152  }
153  return pDefault;
154  }
155 
156 
157  static EnumTypeT fromString(const QString& pValue, EnumTypeT pDefaultType)
158  {
159  return fromString(pValue.toUtf8().constData(), pDefaultType);
160  }
161 
162 
163  static bool isValue(int pValue)
164  {
165  return getQtEnumMetaEnum().valueToKey(pValue) != nullptr;
166  }
167 
168 
169  static bool isValue(uchar pValue)
170  {
171  return isValue(static_cast<int>(pValue));
172  }
173 
174 
175  static bool isValue(char pValue)
176  {
177  return isValue(static_cast<uchar>(pValue));
178  }
179 
180 
181  static EnumBaseTypeT getValue(EnumTypeT pType)
182  {
183  return static_cast<EnumBaseTypeT>(pType);
184  }
185 
186 
187 };
188 
189 
190 template<typename T> inline QLatin1String getEnumName(T pType)
191 {
192  return Enum<T>::getName(pType);
193 }
194 
195 
196 } // namespace governikus
Definition: EnumHelper.h:90
static bool isValue(int pValue)
Definition: EnumHelper.h:163
static QLatin1String getName()
Definition: EnumHelper.h:104
static QLatin1String getName(EnumTypeT pType)
Definition: EnumHelper.h:110
static bool isValue(uchar pValue)
Definition: EnumHelper.h:169
static EnumTypeT fromString(const char *const pValue, EnumTypeT pDefault)
Definition: EnumHelper.h:145
static EnumTypeT fromString(const QString &pValue, EnumTypeT pDefaultType)
Definition: EnumHelper.h:157
static QVector< EnumTypeT > getList()
Definition: EnumHelper.h:130
static int getCount()
Definition: EnumHelper.h:124
static bool isValue(char pValue)
Definition: EnumHelper.h:175
static EnumBaseTypeT getValue(EnumTypeT pType)
Definition: EnumHelper.h:181
static QMetaEnum getQtEnumMetaEnum()
Definition: EnumHelper.h:98
const char * name
Definition: http_parser.cpp:473
#define T(v)
Definition: http_parser.cpp:237
Implementation of ActivationContext for Intent based activation on Android systems.
Definition: ActivationContext.h:15
QLatin1String getEnumName(T pType)
Definition: EnumHelper.h:190