-
Notifications
You must be signed in to change notification settings - Fork 41
/
Enum.h
154 lines (107 loc) · 4.88 KB
/
Enum.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
////////////////////////////////////////////////////////////////////////////////
// -------------------------------------------------------------------------- //
// //
// (C) 2010-2017 Robot Developers //
// See LICENSE for licensing info //
// //
// -------------------------------------------------------------------------- //
////////////////////////////////////////////////////////////////////////////////
//----------------------------------------------------------------------------//
// Prefaces //
//----------------------------------------------------------------------------//
#pragma once
#include "Global.h"
#include <string>
#include <unordered_map>
ROBOT_NS_BEGIN
#ifdef ROBOT_OS_WIN
#pragma warning (push)
// Ignore the VS C4251 warning
#pragma warning (disable:4251)
#endif
//----------------------------------------------------------------------------//
// Macros //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
#define ROBOT_ENUM( type ) template<> std::unordered_map<std::string, type> \
ROBOT_NS_PRE (Enum<type>)::mMap = std::unordered_map<std::string, type>(); \
template<> ROBOT_NS_PRE (Enum<type>)::Enum (void)
////////////////////////////////////////////////////////////////////////////////
#define _ROBOT_ENUM_MAP_1( value ) mMap[#value] = value;
#define _ROBOT_ENUM_MAP_2( value, key ) mMap[ key ] = value;
#define _ROBOT_ENUM_SELECT( _1, _2, NAME, ...) NAME
#define _ROBOT_ENUM_EXPAND( expression ) expression
#define ROBOT_ENUM_MAP( ... ) _ROBOT_ENUM_EXPAND (_ROBOT_ENUM_SELECT \
(__VA_ARGS__, _ROBOT_ENUM_MAP_2, _ROBOT_ENUM_MAP_1) (__VA_ARGS__))
//----------------------------------------------------------------------------//
// Classes //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
template <typename Type> class Enum
{
public:
//----------------------------------------------------------------------------//
// Types //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
typedef std::unordered_map<std::string, Type> ValueMap;
private:
//----------------------------------------------------------------------------//
// Constructors //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
Enum (void);
public:
//----------------------------------------------------------------------------//
// Functions //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
static uintptr Size (void)
{
// Initialize the static parser
if (mMap.empty()) Enum<Type>();
// Return map size
return mMap.size();
}
////////////////////////////////////////////////////////////////////////////////
static const ValueMap& GetMap (void)
{
// Initialize the static parser
if (mMap.empty()) Enum<Type>();
return mMap;
}
////////////////////////////////////////////////////////////////////////////////
static Type Parse (const std::string& key,
Type defaultValue = (Type) -1)
{
// Initialize the static parser
if (mMap.empty()) Enum<Type>();
// Search for a matching key
return mMap.find (key) != mMap.end()
? mMap.at (key) : defaultValue;
}
////////////////////////////////////////////////////////////////////////////////
static std::string Parse (Type value,
const char* defaultValue = "")
{
// Initialize the static parser
if (mMap.empty()) Enum<Type>();
// Find the matching value
for (const auto& i : mMap)
{
if (i.second == value)
return i.first;
}
// Nothing was found
return defaultValue;
}
private:
//----------------------------------------------------------------------------//
// Fields //
//----------------------------------------------------------------------------//
static ValueMap mMap;
};
#ifdef ROBOT_OS_WIN
#pragma warning (pop)
#endif
ROBOT_NS_END