-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreflect.cpp
82 lines (69 loc) · 1.97 KB
/
reflect.cpp
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
#include "mirrow/srefl/reflect.hpp"
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
class Foo final {
public:
void foo() {}
void foo(int) {}
void foo(float) {}
void another() {}
int value_1 = 1;
int value_2 = 2;
};
// clang-format off
#include "mirrow/srefl/srefl_begin.hpp"
srefl_class(Foo,
ctors()
fields(
field(static_cast<void(Foo::*)(void)>(&Foo::foo)),
field(static_cast<void(Foo::*)(int)>(&Foo::foo)),
field(static_cast<void(Foo::*)(float)>(&Foo::foo)),
field(&Foo::another),
field(&Foo::value_1),
field(&Foo::value_2)
)
)
#include "mirrow/srefl/srefl_end.hpp"
// clang-format on
using namespace mirrow::srefl;
TEST_CASE("type_info traits") {
using info = ::mirrow::srefl::type_info<Foo>;
static_assert(!has_ctors_v<info>);
static_assert(has_fields_v<info>);
static_assert(!has_bases_v<info>);
}
TEST_CASE("reflect") {
auto refl = reflect<Foo>();
REQUIRE(!refl.has_bases());
REQUIRE(!refl.has_ctors());
REQUIRE(refl.has_fields());
std::vector<std::string_view> vars;
refl.visit_member_variables([&vars](auto&& value) {
vars.push_back(value.name());
});
REQUIRE(vars == std::vector<std::string_view>{"value_1", "value_2"});
}
enum class MyEnum {
Value1 = 1,
Value2 = 2,
Value3 = 3,
};
// clang-format off
#include "mirrow/srefl/srefl_begin.hpp"
srefl_enum(MyEnum,
enum_value(MyEnum::Value1, "Value1"),
enum_value(MyEnum::Value2, "Value2"),
enum_value(MyEnum::Value3, "Value3")
)
#include "mirrow/srefl/srefl_end.hpp"
// clang-format on
TEST_CASE("reflect enum") {
auto enum_type = reflect<MyEnum>();
auto& values = enum_type.enum_values();
REQUIRE(values[0].name == "Value1");
REQUIRE(values[1].name == "Value2");
REQUIRE(values[2].name == "Value3");
REQUIRE(static_cast<int>(values[0].value) == 1);
REQUIRE(static_cast<int>(values[1].value) == 2);
REQUIRE(static_cast<int>(values[2].value) == 3);
}