-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathenum.h
More file actions
executable file
·90 lines (75 loc) · 2.76 KB
/
enum.h
File metadata and controls
executable file
·90 lines (75 loc) · 2.76 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
#pragma once
#include "utils.h"
#include "type.h"
#include "constructor.h"
class Enum{
private:
static std::unordered_map<const Type*, std::unordered_map<std::string, int64_t>>& Enums(){
static std::unordered_map<const Type*, std::unordered_map<std::string, int64_t>> enums;
return enums;
}
static std::unordered_map<const Type*, std::unordered_map<int64_t, std::string>>& Enums2(){
static std::unordered_map<const Type*, std::unordered_map<int64_t, std::string>> enums;
return enums;
}
public:
static int64_t GetValue(const Type* enumType, const std::string& enumName);
static std::string GetName(const Type* enumType, int64_t value);
static const Type* GetEnum(const std::string& enumTypeName, bool throwIfNotFound = false);
static std::vector<int64_t> GetValues(const Type* enumType);
static std::vector<std::string> GetNames(const Type* enumType);
template<class EnumClass>
static EnumClass GetValue(const std::string& enumName){
return (EnumClass)GetValue(typeof(EnumClass), enumName);
}
template<class EnumClass>
static std::string GetName(EnumClass enumValue){
return GetName(typeof(EnumClass), (int64_t)enumValue);
}
template<class EnumClass>
static std::vector<EnumClass> GetValues(){
std::vector<EnumClass> ret;
for (auto i : GetValues(typeof(EnumClass)))
ret.push_back((EnumClass)i);
return ret;
}
template<class EnumClass>
static std::vector<std::string> GetNames(){
return GetNames(typeof(EnumClass));
}
static const Type* Register(const std::string& name, const std::string& values, const Type* underlyingType);
};
// macros to define a reflectable enum
// define an enum like this:
//
// REFLECT_ENUM(Color,
// Red,
// Green,
// Blue
// )
//
// and then you can use it like this:
//
// std::string name = Enum::GetName(Color::Red);
// Color color = Enum::GetValue<Color>(name);
//
// or you can list all the items in the enum:
//
// for (auto i : Enum::GetValues<Color>()){
// std::cout << Enum::GetName(i) << " = " << (int)i << std::endl;
// }
//
#define REFLECT_ENUM(typeName, ...) enum class typeName{ __VA_ARGS__ }; \
struct Enum_##typeName{ \
static const Type* GetType(){\
static const Type* type = Enum::Register(#typeName, #__VA_ARGS__, typeof(std::underlying_type<typeName>::type)); \
return type; \
} \
}; \
template<> \
struct ReflectType<typeName>{ \
static const Type* Value(){ \
return Enum_##typeName::GetType(); \
} \
}; \
static const Type* ____##typeName##_dummy = typeof(typeName);