forked from therocode/meta_enum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnum.h
140 lines (117 loc) · 5.71 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
#pragma once
#include "meta_enum_lite/include/meta_enum.hpp"
// Define an enum/enum class type with reflection at global scope
#define vg_enum_global(Type, UnderlyingType, ...) meta_enum_global(Type, UnderlyingType, __VA_ARGS__)
#define vg_enum_class_global(Type, UnderlyingType, ...) meta_enum_class_global(Type, UnderlyingType, __VA_ARGS__)
// Define an enum/enum class type under namespace (use fully qualified names to support nested namespaces)
#define vg_enum(Namespace, Type, UnderlyingType, ...) meta_enum_namespace(Namespace, Type, UnderlyingType, __VA_ARGS__)
#define vg_enum_class(Namespace, Type, UnderlyingType, ...) meta_enum_class_namespace(Namespace, Type, UnderlyingType, __VA_ARGS__)
namespace vg
{
namespace core
{
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr auto operator &(E lhs, E rhs)
{
return static_cast<E>(static_cast<std::underlying_type_t<E>>(lhs) & static_cast<std::underlying_type_t<E>>(rhs));
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr auto operator |(E lhs, E rhs)
{
return static_cast<E>(static_cast<std::underlying_type_t<E>>(lhs) | static_cast<std::underlying_type_t<E>>(rhs));
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr auto operator ^(E lhs, E rhs)
{
return static_cast<E>(static_cast<std::underlying_type_t<E>>(lhs) ^ static_cast<std::underlying_type_t<E>>(rhs));
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr auto operator ~(E lhs)
{
return static_cast<E>(~static_cast<std::underlying_type_t<E>>(lhs));
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr auto operator &=(E & lhs, E rhs)
{
return lhs = lhs & rhs;
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr auto operator |=(E & lhs, E rhs)
{
return lhs = lhs | rhs;
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr auto operator ^=(E & lhs, E rhs)
{
return lhs = lhs ^ rhs;
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr auto asInteger(E e)
{
return static_cast<std::underlying_type_t<E>>(e);
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr bool asBool(E e)
{
return 0 != static_cast<std::underlying_type_t<E>>(e);
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr bool testAnyFlags(E _value, E _flags)
{
return 0 != static_cast<std::underlying_type_t<E>>(_value & _flags);
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr bool testAllFlags(E _value, E _flags)
{
return _flags == (_flags & _value);
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr auto enumCount()
{
return getEnumMembers<E>().size();
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr auto enumPairs()
{
const auto & members = getEnumMembers<E>();
std::vector<std::pair<E, vg::core::string>> pairs;
pairs.reserve(members.size());
for (auto i = 0; i < members.size(); ++i)
{
const auto & member = members[i];
pairs.push_back({ member.value, vg::core::string(member.name.data(), member.name.size()) });
}
return pairs;
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr auto enumValue(uint _index)
{
const auto & members = getEnumMembers<E>();
if (_index < members.size())
return members[_index].value;
return (E)0;
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr auto isEnumValue(E e)
{
const auto & members = getEnumMembers<E>();
for (auto i = 0; i < members.size(); ++i)
{
const auto & member = members[i];
if (member.value == e)
return true;
}
return false;
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr auto asString(E e)
{
return getEnumString(e);
}
//--------------------------------------------------------------------------------------
template <typename E> inline constexpr const char * asCString(E e)
{
return getEnumCString(e);
}
}
}