DAWorkbench 0.0.1
DAWorkbench API
载入中...
搜索中...
未找到
DAEnumStringUtils.hpp
1#ifndef DAENUMSTRINGUTILS_H
2#define DAENUMSTRINGUTILS_H
3#include <QString>
4#include <QHash>
67namespace DA
68{
69
70// =================================================================================
71// DAEnumTraits 模板声明
72// =================================================================================
85template< typename T >
87
88// =================================================================================
89// 通用转换函数(模板实现)
90// =================================================================================
103template< typename EnumType >
104QString enumToString(EnumType value)
105{
107 auto it = map.find(value);
108 return (it != map.end()) ? *it : DAEnumTraits< EnumType >::defaultValueStr;
109}
110
124template< typename EnumType >
125EnumType stringToEnum(const QString& s, EnumType defaultValue = DAEnumTraits< EnumType >::defaultValue)
126{
128 QString key = DAEnumTraits< EnumType >::caseSensitive ? s : s.toLower();
129 auto it = map.find(key);
130 return (it != map.end()) ? *it : defaultValue;
131}
132
133// 辅助类型定义
134template< typename EnumType >
135using DAEnumEntry = std::pair< EnumType, const char* >;
136
137} // End DA
138// =================================================================================
139// 宏定义
140// =================================================================================
141
142// ======================= 声明宏 =======================
146#ifndef DA_ENUM_STRING_DECLARE
147#define DA_ENUM_STRING_DECLARE(EnumType) \
148 template<> \
149 struct DA::DAEnumTraits< EnumType > \
150 { \
151 public: \
152 static const QHash< EnumType, QString > enumToStringMap; \
153 static const QHash< QString, EnumType > stringToEnumMap; \
154 static const bool caseSensitive; \
155 static const EnumType defaultValue; \
156 static const QString defaultValueStr; \
157 };
158#endif
159
163#ifndef DA_ENUM_STRING_DECLARE_EXPORT
164#define DA_ENUM_STRING_DECLARE_EXPORT(EXPORT_API, EnumType) \
165 template<> \
166 struct EXPORT_API DA::DAEnumTraits< EnumType > \
167 { \
168 public: \
169 static const QHash< EnumType, QString > enumToStringMap; \
170 static const QHash< QString, EnumType > stringToEnumMap; \
171 static const bool caseSensitive; \
172 static const EnumType defaultValue; \
173 static const QString defaultValueStr; \
174 };
175#endif
213#ifndef DA_ENUM_STRING_SENSITIVE_DEFINE
214#define DA_ENUM_STRING_SENSITIVE_DEFINE(EnumType, DefaultValue, ...) \
215 const QHash< EnumType, QString > DA::DAEnumTraits< EnumType >::enumToStringMap = { __VA_ARGS__ }; \
216 const QHash< QString, EnumType > DA::DAEnumTraits< EnumType >::stringToEnumMap = []() { \
217 QHash< QString, EnumType > tmp; \
218 const std::initializer_list< DAEnumEntry< EnumType > >& entries = { __VA_ARGS__ }; \
219 for (const auto& pair : entries) { \
220 tmp.insert(pair.second, pair.first); \
221 } \
222 return tmp; \
223 }(); \
224 const bool DA::DAEnumTraits< EnumType >::caseSensitive = false; \
225 const EnumType DA::DAEnumTraits< EnumType >::defaultValue = DefaultValue; \
226 const QString DA::DAEnumTraits< EnumType >::defaultValueStr = DA::DAEnumTraits< EnumType >::enumToStringMap.value( \
227 DefaultValue)
228#endif
229
230// ---------------------------------------------------------------------------------
270#ifndef DA_ENUM_STRING_INSENSITIVE_DEFINE
271#define DA_ENUM_STRING_INSENSITIVE_DEFINE(EnumType, DefaultValue, ...) \
272 const QHash< EnumType, QString > DA::DAEnumTraits< EnumType >::enumToStringMap = { __VA_ARGS__ }; \
273 const QHash< QString, EnumType > DA::DAEnumTraits< EnumType >::stringToEnumMap = []() { \
274 QHash< QString, EnumType > tmp; \
275 const std::initializer_list< DAEnumEntry< EnumType > >& entries = { __VA_ARGS__ }; \
276 for (const auto& pair : entries) { \
277 tmp.insert(QString(pair.second).toLower(), pair.first); \
278 } \
279 return tmp; \
280 }(); \
281 const bool DA::DAEnumTraits< EnumType >::caseSensitive = false; \
282 const EnumType DA::DAEnumTraits< EnumType >::defaultValue = DefaultValue; \
283 const QString DA::DAEnumTraits< EnumType >::defaultValueStr = DA::DAEnumTraits< EnumType >::enumToStringMap.value( \
284 DefaultValue)
285#endif
286
287#endif // DAENUMSTRINGUTILS_H
序列化类都是带异常的,使用中需要处理异常
Definition AppMainWindow.cpp:44
QString enumToString(EnumType value)
将枚举值转换为对应的字符串
Definition DAEnumStringUtils.hpp:104
EnumType stringToEnum(const QString &s, EnumType defaultValue=DAEnumTraits< EnumType >::defaultValue)
将字符串转换为对应的枚举值
Definition DAEnumStringUtils.hpp:125
枚举类型特性模板,用于定义枚举与字符串的映射关系
Definition DAEnumStringUtils.hpp:86