-
Notifications
You must be signed in to change notification settings - Fork 8
/
smartenum.hpp
165 lines (137 loc) · 5.13 KB
/
smartenum.hpp
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
155
156
157
158
159
160
161
162
163
164
165
#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include <cstdint>
#include <iostream>
namespace smart_enum
{
inline std::string trimWhitespace(std::string str)
{
// trim trailing whitespace
size_t endPos = str.find_last_not_of(" \t");
if(std::string::npos != endPos)
{
str = str.substr(0, endPos + 1);
}
// trim leading spaces
size_t startPos = str.find_first_not_of(" \t");
if(std::string::npos != startPos)
{
str = str.substr(startPos);
}
return str;
}
inline std::string extractEntry(std::string& valuesString)
{
std::string result;
size_t nextCommaPos = valuesString.find(',');
if(nextCommaPos != std::string::npos)
{
std::string segment = valuesString.substr(0, nextCommaPos);
valuesString.erase(0, nextCommaPos + 1);
result = trimWhitespace(segment);
}
else
{
result = trimWhitespace(valuesString);
valuesString = "";
};
return result;
};
inline std::unordered_map<int32_t, std::string> makeEnumNameMap(std::string enumValuesString)
{
std::unordered_map<int32_t, std::string> nameMap;
int32_t currentEnumValue = 0;
while(enumValuesString != "")
{
std::string currentEnumEntry = extractEntry(enumValuesString);
size_t equalSignPos = currentEnumEntry.find('=');
if(equalSignPos != std::string::npos)
{
std::string rightHandSide = currentEnumEntry.substr(equalSignPos + 1);
currentEnumValue = std::stoi(rightHandSide, 0, 0);
currentEnumEntry.erase(equalSignPos);
}
currentEnumEntry = trimWhitespace(currentEnumEntry);
nameMap[currentEnumValue] = currentEnumEntry;
currentEnumValue++;
}
return nameMap;
}
template<typename Type>
std::vector<Type> makeEnumList(std::string enumValuesString)
{
std::vector<Type> enumList;
int32_t currentEnumValue = 0;
while(enumValuesString != "")
{
std::string currentEnumEntry = extractEntry(enumValuesString);
size_t equalSignPos = currentEnumEntry.find('=');
if(equalSignPos != std::string::npos)
{
std::string rightHandSide = currentEnumEntry.substr(equalSignPos + 1);
currentEnumValue = std::stoi(rightHandSide, 0, 0);
currentEnumEntry.erase(equalSignPos);
}
currentEnumEntry = trimWhitespace(currentEnumEntry);
enumList.push_back(static_cast<Type>(currentEnumValue));
currentEnumValue++;
}
return enumList;
}
inline std::unordered_map<std::string, int32_t> makeEnumValuesMap(std::string enumValuesString)
{
std::unordered_map<std::string, int32_t> nameMap;
int32_t currentEnumValue = 0;
while(enumValuesString != "")
{
std::string currentEnumEntry = extractEntry(enumValuesString);
size_t equalSignPos = currentEnumEntry.find('=');
if(equalSignPos != std::string::npos)
{
std::string rightHandSide = currentEnumEntry.substr(equalSignPos + 1);
currentEnumValue = std::stoi(rightHandSide);
currentEnumEntry.erase(equalSignPos);
}
currentEnumEntry = trimWhitespace(currentEnumEntry);
nameMap[currentEnumEntry] = currentEnumValue;
currentEnumValue++;
}
return nameMap;
}
}
#define smart_enum(Type, ...) enum Type { __VA_ARGS__}; \
static std::unordered_map<int32_t, std::string> Type##_enum_names = smart_enum::makeEnumNameMap(#__VA_ARGS__);\
static std::vector<Type> Type##_list = smart_enum::makeEnumList<Type>(#__VA_ARGS__);\
static std::unordered_map<std::string, int32_t> Type##_enum_values = smart_enum::makeEnumValuesMap(#__VA_ARGS__);\
\
inline const std::string& Type##_to_string(Type value) \
{ \
return Type##_enum_names.at((int32_t)value);\
} \
\
inline const Type Type##_to_enum(const std::string& name)\
{\
return static_cast<Type>(Type##_enum_values.at(name));\
}\
#define smart_enum_class(Type, ...) enum class Type { __VA_ARGS__}; \
static std::unordered_map<int32_t, std::string> Type##_enum_names = smart_enum::makeEnumNameMap(#__VA_ARGS__);\
static std::vector<Type> Type##_list = smart_enum::makeEnumList<Type>(#__VA_ARGS__);\
static std::unordered_map<std::string, int32_t> Type##_enum_values = smart_enum::makeEnumValuesMap(#__VA_ARGS__);\
\
inline const std::string& to_string(Type value) \
{ \
return Type##_enum_names.at((int32_t)value);\
} \
\
inline std::ostream& operator<<(std::ostream& outStream, Type value)\
{\
outStream << to_string(value);\
return outStream;\
}\
\
inline const Type Type##_to_enum_class(const std::string& name)\
{\
return static_cast<Type>(Type##_enum_values.at(name));\
}