1#ifndef DAENUMSTRINGUTILS_H
2#define DAENUMSTRINGUTILS_H
103template<
typename EnumType >
107 auto it = map.find(value);
124template<
typename EnumType >
129 auto it = map.find(key);
130 return (it != map.end()) ? *it : defaultValue;
134template<
typename EnumType >
135using DAEnumEntry = std::pair< EnumType, const char* >;
146#ifndef DA_ENUM_STRING_DECLARE
147#define DA_ENUM_STRING_DECLARE(EnumType) \
149 struct DA::DAEnumTraits< EnumType > \
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; \
163#ifndef DA_ENUM_STRING_DECLARE_EXPORT
164#define DA_ENUM_STRING_DECLARE_EXPORT(EXPORT_API, EnumType) \
166 struct EXPORT_API DA::DAEnumTraits< EnumType > \
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; \
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); \
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( \
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); \
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( \
序列化类都是带异常的,使用中需要处理异常
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