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