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
58 lines (47 loc) · 1.17 KB
/
Copy pathmain.cpp
File metadata and controls
58 lines (47 loc) · 1.17 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
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <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 size_t id = 1024;
};
template<>
struct TypeInfo<Point> {
static constexpr std::string_view name = "Point";
using type = Point;
static constexpr FieldList fields = {
Field{"x", &Point::x, AttrList{ Attr{ "not_serialize", true } }},
Field{"y", &Point::y, AttrList{ Attr{ "info", "hello" } }},
Field{"Sum", overload_v<>(&Point::Sum)},
Field{"Sum", overload_v<float>(&Point::Sum)}
};
static constexpr AttrList attrs = {
Attr{ "size", 8 }
};
};
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(overload_v<>(&Point::Sum)))
cout << (p.*(field.value))() << endl;
else if constexpr (field.ValueTypeIsSameWith(overload_v<float>(&Point::Sum)))
cout << (p.*(field.value))(1.f) << endl;
else
assert(false);
}
});
}