Browse Source

Added better Custom string support for safe enum

undefined
Benjamin ‘Touky’ Huet Sam Hocevar <sam@hocevar.net> 10 years ago
parent
commit
101365933f
2 changed files with 24 additions and 5 deletions
  1. +4
    -2
      src/base/enum.cpp
  2. +20
    -3
      src/lol/base/enum.h

+ 4
- 2
src/base/enum.cpp View File

@@ -21,11 +21,12 @@ namespace lol
* Safe enum helpers
*/

Map<int64_t, String> BuildEnumMap(char const *str)
Map<int64_t, String> BuildEnumMap(char const *str, char const **custom)
{
Map<int64_t, String> ret;
char const *parser = str;
int64_t next_value = 0;
int64_t cur_idx = 0;

for (;;)
{
@@ -58,8 +59,9 @@ Map<int64_t, String> 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;


+ 20
- 3
src/lol/base/enum.h View File

@@ -14,7 +14,7 @@
namespace lol
{

extern Map<int64_t, String> BuildEnumMap(char const *str);
extern Map<int64_t, String> BuildEnumMap(char const *str, char const **custom);

template<typename BASE, typename T = typename BASE::Type>
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 ## Base> 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 ## Base> name;



Loading…
Cancel
Save