forked from Ubpa/USRefl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
110 lines (97 loc) · 2.56 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
100
101
102
103
104
105
106
107
108
109
110
#include <USRefl/USRefl.h>
#include <iostream>
#include <cassert>
using namespace Ubpa::USRefl;
using namespace std;
struct A { float a; };
struct B : virtual A { float b; };
struct C : virtual A { float c; };
struct D : B, C { float d; };
template<>
struct Ubpa::USRefl::TypeInfo<A> :
TypeInfoBase<A>
{
#ifdef UBPA_USREFL_NOT_USE_NAMEOF
static constexpr char name[2] = "A";
#endif
static constexpr AttrList attrs = {};
static constexpr FieldList fields = {
Field {TSTR("a"), &Type::a},
};
};
template<>
struct Ubpa::USRefl::TypeInfo<B> :
TypeInfoBase<B, Base<A, true>>
{
#ifdef UBPA_USREFL_NOT_USE_NAMEOF
static constexpr char name[2] = "B";
#endif
static constexpr AttrList attrs = {};
static constexpr FieldList fields = {
Field {TSTR("b"), &Type::b},
};
};
template<>
struct Ubpa::USRefl::TypeInfo<C> :
TypeInfoBase<C, Base<A, true>>
{
#ifdef UBPA_USREFL_NOT_USE_NAMEOF
static constexpr char name[2] = "C";
#endif
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>
>
{
#ifdef UBPA_USREFL_NOT_USE_NAMEOF
static constexpr char name[2] = "D";
#endif
static constexpr AttrList attrs = {};
static constexpr FieldList fields = {
Field {TSTR("d"), &Type::d},
};
};
int main() {
cout << "// not fully support in MSVC++ 19.26 because of a bug (2020/07/17)" << endl;
cout << "// https://developercommunity.visualstudio.com/content/problem/1116835/member-pointer-of-a-class-with-a-virtual-base-1.html" << endl;
cout << "[Virtual Bases]" << endl;
constexpr auto vbs = TypeInfo<D>::VirtualBases();
vbs.ForEach([](auto info) {
cout << info.name << endl;
});
cout << "[Tree]" << endl;
TypeInfo<D>::DFS_ForEach([](auto t, size_t depth) {
for (size_t i = 0; i < depth; i++)
cout << " ";
cout << t.name << endl;
});
cout << "[field]" << endl;
TypeInfo<D>::DFS_ForEach([](auto t, size_t) {
t.fields.ForEach([](auto field) {
cout << field.name << endl;
});
});
cout << "[var]" << endl;
D d;
d.a = 1;
d.b = 2;
d.c = 3;
d.d = 4;
cout << "[var : left]" << endl;
TypeInfo<D>::ForEachVarOf(std::move(d), [](auto field, auto&& var) {
static_assert(std::is_rvalue_reference_v<decltype(var)>);
cout << var << endl;
});
cout << "[var : right]" << endl;
TypeInfo<D>::ForEachVarOf(d, [](auto field, auto&& var) {
static_assert(std::is_lvalue_reference_v<decltype(var)>);
cout << field.name << " : " << var << endl;
});
}