-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cpp
314 lines (245 loc) · 6.59 KB
/
demo.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) 2021 Christopher Taylor
// Copyright (c) 2020 Christopher Taylor
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/fusion/include/sequence.hpp>
#include <boost/fusion/include/algorithm.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/type_traits.hpp>
#include <variant>
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <tuple>
#include <optional>
#include "sybpp.hpp"
using namespace sybpp;
struct demo {
int here;
std::string beer;
};
BOOST_FUSION_ADAPT_STRUCT(
demo,
(int, here)
(std::string, beer)
)
struct demo_recurse {
demo d;
};
BOOST_FUSION_ADAPT_STRUCT(
demo_recurse,
(demo, d)
)
struct demo_vec {
std::vector<int> here;
std::string beer;
};
BOOST_FUSION_ADAPT_STRUCT(
demo_vec,
(std::vector<int>, here)
(std::string, beer)
)
struct demo_var {
using var_t = std::variant<std::monostate, double, int>;
var_t here;
std::string beer;
};
BOOST_FUSION_ADAPT_STRUCT(
demo_var,
(demo_var::var_t, here)
(std::string, beer)
)
using pair_value_t = std::pair<int, int>;
struct demo_pair {
pair_value_t value;
};
BOOST_FUSION_ADAPT_STRUCT(
demo_pair,
(pair_value_t, value)
)
using tuple_value_t = std::tuple<int, int>;
struct demo_tuple {
tuple_value_t value;
};
BOOST_FUSION_ADAPT_STRUCT(
demo_tuple,
(tuple_value_t, value)
)
using opt_value_t = std::optional<int>;
struct demo_opt {
opt_value_t value;
};
BOOST_FUSION_ADAPT_STRUCT(
demo_opt,
(opt_value_t, value)
)
void update_demo(demo & d) {
d.here += 1;
}
void update_here(int & d) {
d += 1;
}
void update_beer(std::string & d) {
d = "3";
}
struct count {
std::int64_t value;
count() : value(0) {}
void operator()(int t) {
value++;
}
};
void count_fn(count & c, int t) {
c(t);
}
int main(int argc, char ** argv) {
demo d{1, "2"};
demo c{1, "2"};
demo e{1, "2"};
demo_recurse f{ demo{1, "2"} };
demo_vec g { {0,1,2}, "here" };
demo_var h { 0, "here" };
demo_pair dpair;
demo_tuple dtuple;
demo_opt dopt{1};
// update member variable of struct
//
{
using mk_transform_demo = everywhere<demo, decltype(update_demo)>;
mk_transform_demo dt(update_demo);
apply(dt, d);
std::cout << "value should be 2\t" << d.here << std::endl;
}
// update integer
//
{
using mk_transform_demo = everywhere<int, decltype(update_here)>;
mk_transform_demo dt(update_here);
apply(dt, c);
std::cout << "value should be 2\t" << d.here << std::endl;
}
// update string
//
{
using mk_transform_demo = everywhere<std::string, decltype(update_beer)>;
mk_transform_demo dt(update_beer);
apply(dt, e);
std::cout << "value should be 3\t" << e.beer << std::endl;
apply(dt, f);
std::cout << "value should be 3\t" << f.d.beer << std::endl;
}
// update struct
//
{
using mk_transform_demo = everywhere<int, decltype(update_here)>;
mk_transform_demo dt(update_here);
apply(dt, f);
std::cout << "value should be 2\t" << f.d.here << std::endl;
}
// update stl-container
//
{
using mk_transform_demo = everywhere<int, decltype(update_here)>;
mk_transform_demo dt(update_here);
apply(dt, g);
std::cout << "input {0,1,2}; expecting {1,2,3}; computed ->\t";
std::for_each(std::begin(g.here), std::end(g.here), [](const auto& v) {
std::cout << v << ' ';
});
std::cout << std::endl;
}
// update variant
//
{
using mk_transform_demo = everywhere<int, decltype(update_here)>;
mk_transform_demo dt(update_here);
apply(dt, h);
std::cout << "value should be 1\t" << std::get<2>(h.here) << std::endl;
}
// update with functor
//
{
count ct{};
std::cout << "value should be 0\t" << ct.value << std::endl;
using mk_transform_demo = everywhere<int, decltype(ct)>;
mk_transform_demo dt(ct);
apply(dt, c);
apply(dt, d);
apply(dt, e);
apply(dt, f);
apply(dt, g);
apply(dt, h);
std::cout << "value should be 8\t" << ct.value << std::endl;
}
// update with std::bind
//
{
count ct{};
std::cout << "value should be 0\t" << ct.value << std::endl;
auto fn = std::bind(count_fn, std::ref(ct), std::placeholders::_1);
using mk_transform_demo = everywhere<int, decltype(fn)>;
mk_transform_demo dt(fn);
apply(dt, c);
apply(dt, d);
apply(dt, e);
apply(dt, f);
apply(dt, g);
apply(dt, h);
std::cout << "value should be 8\t" << ct.value << std::endl;
}
// update with lambda
//
{
count ct{};
std::cout << "value should be 0\t" << ct.value << std::endl;
auto fn = [&ct](int t) { ct(t); };
using mk_transform_demo = everywhere<int, decltype(fn)>;
mk_transform_demo dt(fn);
apply(dt, c);
apply(dt, d);
apply(dt, e);
apply(dt, f);
apply(dt, g);
apply(dt, h);
std::cout << "value should be 8\t" << ct.value << std::endl;
}
{
count ct{};
auto fn = [&ct](int t) { ct(t); };
using mk_transform_demo = everywhere<int, decltype(fn)>;
mk_transform_demo dt(fn);
apply(dt, dpair);
std::cout << "value should be 2\t" << ct.value << std::endl;
}
{
count ct{};
auto fn = [&ct](int t) { ct(t); };
using mk_transform_demo = everywhere<int, decltype(fn)>;
mk_transform_demo dt(fn);
apply(dt, dtuple);
std::cout << "value should be 2\t" << ct.value << std::endl;
}
{
count ct{};
auto fn = [&ct](int t) { ct(t); };
using mk_transform_demo = everywhere<int, decltype(fn)>;
mk_transform_demo dt(fn);
apply(dt, dopt);
std::cout << "value should be 1\t" << ct.value << std::endl;
}
{
count ct{};
auto fn = [&ct](std::optional<int> t) { ct(*t); };
using mk_transform_demo = everywhere<std::optional<int>, decltype(fn)>;
mk_transform_demo dt(fn);
apply(dt, dopt);
std::cout << "value should be 1\t" << ct.value << std::endl;
}
return 0;
}