-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.cpp
99 lines (86 loc) · 2.03 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <USRefl/USRefl.h>
#include <iostream>
#include <cassert>
using namespace Ubpa::USRefl;
using namespace std;
struct A { float a; };
struct B : A { float b; };
struct C : A { float c; };
struct D : B, C { float d; };
template<>
struct Ubpa::USRefl::TypeInfo<A> :
TypeInfoBase<A>
{
static constexpr AttrList attrs = {};
static constexpr FieldList fields = {
Field {TSTR("a"), &Type::a},
};
};
template<>
struct Ubpa::USRefl::TypeInfo<B> :
TypeInfoBase<B, Base<A>>
{
static constexpr AttrList attrs = {};
static constexpr FieldList fields = {
Field {TSTR("b"), &Type::b},
};
};
template<>
struct Ubpa::USRefl::TypeInfo<C> :
TypeInfoBase<C, Base<A>>
{
static constexpr AttrList attrs = {};
static constexpr FieldList fields = {
Field {TSTR("c"), &Type::c},
};
};
template<>
struct Ubpa::USRefl::TypeInfo<D> :
TypeInfoBase<D,
Base<B>,
Base<C>
>
{
static constexpr AttrList attrs = {};
static constexpr FieldList fields = {
Field {TSTR("d"), &Type::d},
};
};
int main() {
TypeInfo<D>::DFS_ForEach([](auto t, std::size_t depth) {
for (std::size_t i = 0; i < depth; i++)
cout << " ";
cout << t.name << endl;
});
cout << "[non DFS]" << endl;
TypeInfo<D>::fields.ForEach([](auto field){
cout << field.name << endl;
});
cout << "[DFS]" << endl;
TypeInfo<D>::DFS_ForEach([](auto t, std::size_t) {
t.fields.ForEach([](auto field) {
cout << field.name << endl;
});
});
constexpr std::size_t fieldNum = TypeInfo<D>::DFS_Acc(0, [](auto acc, auto, auto) {
return acc + 1;
});
cout << "field number : " << fieldNum << endl;
cout << "[var]" << endl;
D d;
d.B::a = 1;
d.C::a = 2;
d.b = 3;
d.c = 4;
d.d = 5;
cout << "[left]" << endl;
TypeInfo<D>::ForEachVarOf(std::move(d), [](auto field, auto&& var) {
static_assert(std::is_rvalue_reference_v<decltype(var)>);
cout << field.name << " : " << var << endl;
});
cout << "[right]" << endl;
TypeInfo<D>::ForEachVarOf(d, [](auto field, auto&& var) {
static_assert(std::is_lvalue_reference_v<decltype(var)>);
cout << field.name << " : " << var << endl;
});
}