-
Notifications
You must be signed in to change notification settings - Fork 71
/
main.cpp
61 lines (52 loc) · 1.32 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
#include <USRefl/USRefl.h>
#include <iostream>
#include <cassert>
using namespace Ubpa::USRefl;
using namespace std;
struct [[size(8)]] Point {
[[not_serialize]]
float x;
[[info("hello")]]
float y;
float Sum() const {
return x + y;
}
float Sum(float z) const {
return x + y + z;
}
static constexpr std::size_t id = 1024;
};
template<>
struct Ubpa::USRefl::TypeInfo<Point> :
TypeInfoBase<Point>
{
static constexpr AttrList attrs = {
Attr {TSTR("size"), 8},
};
static constexpr FieldList fields = {
Field {TSTR("x"), &Type::x, AttrList {
Attr {TSTR("not_serialize")},
}},
Field {TSTR("y"), &Type::y, AttrList {
Attr {TSTR("info"), "hello"},
}},
Field {TSTR("Sum"), static_cast<float(Type::*)()const>(&Type::Sum)},
Field {TSTR("Sum"), static_cast<float(Type::*)(float)const>(&Type::Sum)},
Field {TSTR("id"), Type::id},
};
};
int main() {
Point p{ 1,2 };
TypeInfo<Point>::fields.ForEach([p](auto field) {
if constexpr (field.is_func) {
if (field.name != "Sum")
return;
if constexpr (field.ValueTypeIsSameWith(static_cast<float(Point::*)()const>(&Point::Sum)))
cout << (p.*(field.value))() << endl;
else if constexpr (field.ValueTypeIsSameWith(static_cast<float(Point::*)(float)const>(&Point::Sum)))
cout << (p.*(field.value))(1.f) << endl;
else
assert(false);
}
});
}