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