forked from Ubpa/USRefl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
45 lines (35 loc) · 1.35 KB
/
main.cpp
File metadata and controls
45 lines (35 loc) · 1.35 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
#include <USRefl.h>
#include <iostream>
#include <cassert>
using namespace Ubpa::USRefl;
using namespace std;
template<size_t N>
constexpr size_t Func() { return N; }
enum class [[enum_attr("enum_attr_value")]] Color {
RED [[enumerator_attr("enumerator_attr_value"), func(&Func<1>)]] = 1,
GREEN [[func(&Func<2>)]] = 2,
BLUE [[func(&Func<3>)]] = 4
};
template<>
struct TypeInfo<Color> : TypeInfoBase<Color> {
static constexpr std::string_view name = "Color";
using type = Color;
static constexpr FieldList fields = {
Field{"RED", Color::RED, AttrList{ Attr{ "enumerator_attr", "enumerator_attr_value" },Attr{"func", &Func<1>} }},
Field{"GREEN", Color::GREEN, AttrList{ Attr{"func", &Func<2>} }},
Field{"BLUE", Color::BLUE, AttrList{ Attr{"func", &Func<3>} }}
};
static constexpr AttrList attrs = {
Attr{ "enum_attr", "enum_attr_value" }
};
};
int main() {
TypeInfo<Color>::fields.ForEach([](auto field) {
cout << field.name << endl;
});
static_assert(TypeInfo<Color>::fields.Get<TypeInfo<Color>::fields.Find("RED")>().value == Color::RED);
static_assert(TypeInfo<Color>::fields.Get<TypeInfo<Color>::fields.FindByValue(Color::RED)>().name == "RED");
constexpr Color c = Color::GREEN;
constexpr auto c_attr = TypeInfo<Color>::fields.Get<TypeInfo<Color>::fields.FindByValue(c)>().attrs;
static_assert(c_attr.Get<c_attr.Find("func")>().value() == 2);
}