-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
207 lines (161 loc) · 5.14 KB
/
example.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <string>
//#define REFLECTO_UNSAFE_BUT_USABLE
//#define REFLECTO_SUPPORT_BITFIELDS
#include "reflecto.h"
template<typename Type, typename = void>
struct can_ostream : std::false_type {};
template<typename Type>
struct can_ostream<Type, std::void_t<decltype(std::cout << std::declval<Type>())>> : std::true_type {};
template<size_t Level, size_t Index, typename Type>
struct ActionOstreamOrFlat {
static const reflecto::VisitAction value = can_ostream<Type>::value
? reflecto::VisitAction::Call
: reflecto::VisitAction::Flat;
};
struct TestSimple {
int i = 123;
double j = 1.23;
};
void test_simple() {
std::cout << "\ntest_simple:\n";
TestSimple ts;
const TestSimple cts;
auto view1 = reflecto::get_view(ts);
auto view2 = reflecto::get_view(cts);
std::cout << std::get<0>(view1) << " " << std::get<1>(view1) << "\n";
std::cout << std::get<0>(view2) << " " << std::get<1>(view2) << "\n";
reflecto::for_each_member(ts, [](auto&& v) { ++v; std::cout << v << " ";});
std::cout << "\n";
reflecto::for_each_member(cts, [](auto&& v) { std::cout << v << " ";});
std::cout << "\n";
}
struct Test {
int i = 123;
double d = 1.23;
std::string s = "string";
};
struct Test2 {
int i = 123;
double d = 1.23;
Test t {};
float f = -1.23f;
};
void test_flatten() {
std::cout << "\ntest_flatten:\n";
Test2 t;
reflecto::for_each_member(t, [](auto&& v) { std::cout << sizeof(v) << "() ";});
std::cout << "\n";
reflecto::for_each_member_flatten<ActionOstreamOrFlat>(
t,
[](auto&& v) { std::cout << sizeof(v) << "(" << v << ") ";}
);
std::cout << "\n";
}
struct Test4 {
int i;
int& ri;
};
void test_reference() {
std::cout << "\ntest_reference:\n";
int value = 42;
Test4 t { -3, value};
auto view = reflecto::get_view(t);
std::cout << std::get<0>(view) << " " << std::get<1>(view) << "\n";
reflecto::for_each_member(t, [](auto&& v) { std::cout << v << " ";});
std::cout << "\n";
std::get<1>(view)++;
std::cout << std::get<0>(view) << " " << std::get<1>(view) << "\n";
reflecto::for_each_member(t, [](auto&& v) { std::cout << v << " ";});
std::cout << "\n";
}
struct Test3 {
int i = 0;
Test2 t2 {};
double d = 0;
};
void test_pretty_print() {
std::cout << "\ntest_pretty_print:\n";
Test3 t;
std::string indent = "\t";
std::cout << "Test3 {\n";
reflecto::for_each_member_flatten<ActionOstreamOrFlat>(
t,
[&indent](size_t level, size_t index, auto&& v) {
std::cout << indent << level << '.' << index << ": " << v << "\n";
},
[&indent](size_t level, size_t index) {
std::cout << indent << level << '.' << index << ": {\n";
indent.push_back('\t');
},
[&indent](size_t, size_t) {
indent.pop_back();
std::cout << indent << "}\n";
}
);
std::cout << "}\n";
}
struct Inner {
std::string s = "str";
};
struct Struct {
int i = 1;
double j = 2.0;
Inner inner {};
};
void example() {
Struct test;
// create a tuple of references to members (first layer only)
auto view = reflecto::get_view(test);
std::cout << std::get<0>(view) << " " << std::get<1>(view) << " "
<< std::get<2>(view).s << "\n"; // note .s here. Inner cannot be put into `cout` directly
// call function for each member (first layer only)
reflecto::for_each_member(test, [](auto&& v) { std::cout << sizeof(v) << " ";});
std::cout << "\n";
//
reflecto::for_each_member_flatten<ActionOstreamOrFlat>(
// what struct to traverce
test,
// call this if Action is Call. you can add extra arguments `size_t level, size_t index` before `auto&& v`
[](auto&& v) {
std::cout << v << " ";
}
// add void (size_t level, size_t index) callback on each flattening action
// add void (size_t level, size_t index) callback when we pop from inside of most inner struct
);
}
#if defined(REFLECTO_SUPPORT_BITFIELDS)
struct WithFields {
unsigned int i1 : 1;
unsigned int i2 : 1;
unsigned int i3 : 1;
unsigned int i4 : 1;
unsigned int i5 : 1;
unsigned int i6 : 1;
unsigned int i7 : 1;
unsigned int i8 : 1;
unsigned int i9 : 1;
};
void test_bitfields() {
std::cout << "\ntest_bitfields:\n";
std::cout << sizeof (WithFields) << " " << reflecto::details::count_members<WithFields>() << "\n";
WithFields wf;
wf.i7 = 1;
std::cout << wf.i1 << wf.i2 << wf.i3 << wf.i4 << wf.i5 << wf.i6 << wf.i7 << wf.i8 << wf.i9 << "\n" ;
reflecto::for_each_member(wf, [](auto&& v) { std::cout << v;});
std::cout << "\n";
}
#endif
int main()
{
test_simple();
test_flatten();
test_reference();
test_pretty_print();
std::cout << "example()\n";
example();
#if defined(REFLECTO_SUPPORT_BITFIELDS)
test_bitfields();
#endif
return 0;
}