From 101365933f568b225a9f89402c971f461d8626eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20=E2=80=98Touky=E2=80=99=20Huet?= Date: Sat, 14 Jun 2014 22:51:28 +0000 Subject: [PATCH] Added better Custom string support for safe enum --- src/base/enum.cpp | 6 ++++-- src/lol/base/enum.h | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/base/enum.cpp b/src/base/enum.cpp index 97bc060e..03c22136 100644 --- a/src/base/enum.cpp +++ b/src/base/enum.cpp @@ -21,11 +21,12 @@ namespace lol * Safe enum helpers */ -Map BuildEnumMap(char const *str) +Map BuildEnumMap(char const *str, char const **custom) { Map ret; char const *parser = str; int64_t next_value = 0; + int64_t cur_idx = 0; for (;;) { @@ -58,8 +59,9 @@ Map BuildEnumMap(char const *str) } /* Store in the map */ - ret[current_value] = String(name, (int)(name_end - name)); + ret[current_value] = (!custom) ? (String(name, (int)(name_end - name))) : (String(custom[cur_idx])); next_value = current_value + 1; + cur_idx++; } return ret; diff --git a/src/lol/base/enum.h b/src/lol/base/enum.h index 45da4655..c0653d11 100644 --- a/src/lol/base/enum.h +++ b/src/lol/base/enum.h @@ -14,7 +14,7 @@ namespace lol { -extern Map BuildEnumMap(char const *str); +extern Map BuildEnumMap(char const *str, char const **custom); template class SafeEnum : public BASE @@ -39,7 +39,7 @@ public: static bool ready = false; if (!ready) - map = BuildEnumMap(BASE::GetDescription()); + map = BuildEnumMap(BASE::GetDescription(), BASE::GetCustomString()); ready = true; if (map.HasKey((int64_t)m_value)) @@ -74,12 +74,29 @@ public: } }; +#define LOL_OPEN_SAFE_ENUM(name, ...) \ + struct name ## Base \ + { \ + enum Type {__VA_ARGS__}; \ + protected: \ + static inline char const *GetDescription() { return #__VA_ARGS__; } +#define LOL_SAFE_ENUM_CUSTOM_TOSTRING(...) \ + static inline char const **GetCustomString() \ + { \ + static char const *custom_list[] = { __VA_ARGS__ }; \ + return &custom_list[0]; \ + } +#define LOL_CLOSE_SAFE_ENUM(name) \ + }; \ + typedef SafeEnum name; + #define LOL_SAFE_ENUM(name, ...) \ struct name ## Base \ { \ enum Type {__VA_ARGS__}; \ protected: \ - static inline char const *GetDescription() { return #__VA_ARGS__; } \ + static inline char const *GetDescription() { return #__VA_ARGS__; } \ + static inline char const **GetCustomString() { return nullptr; } \ }; \ typedef SafeEnum name;