-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmirror_test.cpp
More file actions
84 lines (61 loc) · 2.29 KB
/
Copy pathmirror_test.cpp
File metadata and controls
84 lines (61 loc) · 2.29 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
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
#include "mirror.h"
#include <iostream>
struct X { short a; int b; };
MIRROR_REFLECTION_DEFINE(X, a, b);
// A Simple foreach functor
struct Printer {
template <typename T> void run(const char* name, const T& x) {
std::cout << name << " = " << x << ", ";
}
};
// A Simple foreach functor, modify fields.
struct Setter {
Setter(int val) : val_(val) {}
template <typename T> void run(const char* name, T& x) {
x = val_;
}
int val_;
};
void test() {
X x{1, 2};
using Reflection = mirror::Reflection<X>;
// ForeachField
// Run ForeachField(Functor, X&)
Reflection::ForeachField(Printer(), x); // a = 1, b = 2,
std::cout << std::endl;
// Run ForeachField(Functor, const X&)
Reflection::ForeachField(Printer(), X(x)); // a = 1, b = 2,
std::cout << std::endl;
// Modify field in ForeachField
Reflection::ForeachField(Setter(886), x); // x.a = 886, x.b = 886
Reflection::ForeachField(Printer(), x); // a = 886, b = 886,
std::cout << std::endl;
// Some field api.
// Reflection::field_name<field_id>()
std::cout << Reflection::field_name<0>() << std::endl; // a
// Reflection::FieldType<>
std::cout << std::is_same<short, Reflection::FieldType<0>>::value << std::endl; // true
// Reflection::get<field_id>(X&)
Reflection::get<0>(x) = 42;
Reflection::ForeachField(Printer(), x); // a = 42, b = 886,
std::cout << std::endl;
// Reflection::get<field_id>(const X&)
std::cout << Reflection::get<0>(X(x)) << std::endl; // 42
// You can use Reflection::Field<field_id>, if you want.
// Reflection::Field<field_id>::name()
std::cout << Reflection::Field<1>::name() << std::endl; // b
// Reflection::Field<field_id>::type
std::cout << std::is_same<int, Reflection::Field<1>::type>::value << std::endl; // true
// Reflection::Field<field_id>::get(X&)
Reflection::Field<1>::get(x) = 84;
Reflection::ForeachField(Printer(), x); // a = 42, b = 84,
std::cout << std::endl;
// Reflection::Field<field_id>::get(const X&)
std::cout << Reflection::Field<1>::get(X(x)) << std::endl; // 84
// Reflection::FieldStdTuple == std::tuple<Reflection::Field<0>, Reflection::Field<1>...>
std::cout << std::is_same<Reflection::FieldStdTuple,
std::tuple<Reflection::Field<0>, Reflection::Field<1>>>::value << std::endl; // true
}
int main() {
test();
}