forked from Ubpa/USRefl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
60 lines (53 loc) · 1.31 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
#include <USRefl/USRefl.h>
#include <iostream>
#include <array>
using namespace Ubpa::USRefl;
using namespace std;
struct [[size(8)]] Point {
[[not_serialize]]
float x;
[[info("hello")]]
float y;
float z;
};
template<>
struct TypeInfo<Point> : TypeInfoBase<Point> {
static constexpr FieldList fields = {
Field{"x", &Point::x, AttrList{ Attr{ "not_serialize" } }},
Field{"y", &Point::y, AttrList{ Attr{ "info", "hello" } }},
Field{"z", &Point::z}
};
static constexpr AttrList attrs = {
Attr{ "size", 8 }
};
};
template<typename T, size_t... Ns>
constexpr auto GetXZ(std::index_sequence<Ns...>) {
// get fields with name "x" or "z"
constexpr auto masks = TypeInfo<T>::fields.Accumulate(
std::array<bool, TypeInfo<T>::fields.size>{},
[&, idx = 0](auto acc, auto field) mutable {
acc[idx] = field.name == "x" || field.name == "z";
idx++;
return acc;
}
);
constexpr auto fields = TypeInfo<T>::fields.Accumulate<masks[Ns]...>(
ElemList<>{},
[&](auto acc, auto field) {
return acc.Push(field);
}
);
return fields;
}
template<typename T>
constexpr auto GetXZ() {
return GetXZ<T>(std::make_index_sequence<TypeInfo<T>::fields.size>{});
}
int main() {
// get fields with name "x" or "z"
constexpr auto fields = GetXZ<Point>();
fields.ForEach([](auto field) {
cout << field.name << endl;
});
}