forked from Ubpa/USRefl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
62 lines (49 loc) · 1.43 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
#include <USRefl/USRefl.h>
#include <iostream>
using namespace Ubpa::USRefl;
using namespace std;
struct [[size(8)]] Point {
[[not_serialize]]
float x;
[[info("hello")]]
float y;
static constexpr size_t id = 1024;
};
template<>
struct TypeInfo<Point> : TypeInfoBase<Point> {
static constexpr FieldList fields = {
Field{"x", &Point::x, AttrList{ Attr{ "not_serialize", true } }},
Field{"y", &Point::y, AttrList{ Attr{ "info", "hello" } }},
Field{"id", &Point::id, AttrList{ }}
};
static constexpr AttrList attrs = {
Attr{ "size", 8 }
};
};
int main() {
Point p{ 1,2 };
TypeInfo<Point>::fields.ForEach([](auto field) {
cout << field.name << endl;
field.attrs.ForEach([](auto attr) {
cout << "name : " << attr.name << endl;
if constexpr (attr.has_value)
cout << "value : " << attr.value << endl;
});
});
constexpr auto y_idx = TypeInfo<Point>::fields.Find("y");
constexpr auto y_field = TypeInfo<Point>::fields.Get<y_idx>();
static_assert(y_field.name == "y");
static_assert(TypeInfo<Point>::fields.Contains("x"));
TypeInfo<Point>::attrs.ForEach([](auto attr) {
cout << "name : " << attr.name << endl;
if constexpr (!attr.has_value)
cout << "value : " << attr.value << endl;
});
TypeInfo<Point>::ForEachVarOf(p, [](auto&& var) {
cout << var << endl;
});
TypeInfo<Point>::fields.ForEach([](auto field) {
if constexpr (field.is_static)
cout << field.name << ": " << *field.value << endl;
});
}