-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathenum.cpp
More file actions
executable file
·95 lines (77 loc) · 3.02 KB
/
enum.cpp
File metadata and controls
executable file
·95 lines (77 loc) · 3.02 KB
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
#include "enum.h"
#include "type.h"
#include "string_helper.h"
#include "exception.h"
const Type* Enum::GetEnum(const std::string& enumTypeName, bool throwIfNotFound){
const Type* enumType = Type::GetType(enumTypeName);
if (throwIfNotFound){
if (enumType == nullptr){
THROW_EXCEPTION(EnumNotFound, "enum '" + enumTypeName + "' not found");
}
else if (!enumType->IsEnum()){
THROW_EXCEPTION(NotAnEnum, "'" + enumTypeName + "' is not an enum type");
}
}
if (enumType != nullptr && !enumType->IsEnum()){
return nullptr;
}
return enumType;
}
int64_t Enum::GetValue(const Type* enumType, const std::string& enumName){
if (!enumType->IsEnum())
THROW_EXCEPTION(NotAnEnum, "'" + enumType->GetName() + "' is not an enum type");
auto& map = Enums()[enumType];
if (map.find(enumName) == map.end())
THROW_EXCEPTION(EnumNameNotFound, "enum '" + enumType->GetName() + "' has no member named '" + enumName + "'");
return map[enumName];
}
std::string Enum::GetName(const Type* enumType, int64_t value){
if (!enumType->IsEnum())
THROW_EXCEPTION(NotAnEnum, "'" + enumType->GetName() + "' is not an enum type");
auto& map = Enums2()[enumType];
if (map.find(value) == map.end())
THROW_EXCEPTION(EnumValueNotFound, "enum '" + enumType->GetName() + "' has no member valued '" + std::to_string(value) + "'");
return map[value];
}
std::vector<int64_t> Enum::GetValues(const Type* enumType){
if (!enumType->IsEnum())
THROW_EXCEPTION(NotAnEnum, "'" + enumType->GetName() + "' is not an enum type");
std::vector<int64_t> values;
for (auto& i : Enums()[enumType]){
values.push_back(i.second);
}
return values;
}
std::vector<std::string> Enum::GetNames(const Type* enumType){
if (!enumType->IsEnum())
THROW_EXCEPTION(NotAnEnum, "'" + enumType->GetName() + "' is not an enum type");
std::vector<std::string> names;
for (auto& i : Enums()[enumType]){
names.push_back(i.first);
}
return names;
}
const Type* Enum::Register(const std::string& name, const std::string& values, const Type* underlyingType){
auto items = StringHelper::Split(StringHelper::RemoveSpaces(values), ',');
std::unordered_map<std::string, int64_t> map;
const Type* type = Type::RegisterEnum(new Type, name, underlyingType);
int num = 0;
for (auto& i : items){
if ((int)i.find('=') >= 0){
auto kv = StringHelper::Split(i, '=');
num = atoi(kv[1].c_str());
map.insert(std::make_pair(kv[0], num));
}
else{
map.insert(std::make_pair(i, num));
}
num++;
}
std::unordered_map<int64_t, std::string> map2;
for (auto& i : map){
map2.insert(std::make_pair(i.second, i.first));
}
Enums().insert(std::make_pair(type, map));
Enums2().insert(std::make_pair(type, map2));
return type;
}