-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathwith.hpp
495 lines (426 loc) · 13.2 KB
/
with.hpp
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//
// lager - library for functional interactive c++ programs
// Copyright (C) 2017 Juan Pedro Bolivar Puente
//
// This file is part of lager.
//
// lager is free software: you can redistribute it and/or modify
// it under the terms of the MIT License, as detailed in the LICENSE
// file located at the root of this source code distribution,
// or here: <https://github.com/arximboldi/lager/blob/master/LICENSE>
//
#pragma once
#include <lager/detail/lens_nodes.hpp>
#include <lager/detail/merge_nodes.hpp>
#include <lager/detail/xform_nodes.hpp>
#include <lager/tags.hpp>
#include <zug/transducer/filter.hpp>
#include <zug/transducer/map.hpp>
namespace lager {
template <typename T>
class reader;
template <typename T>
class writer;
template <typename T>
class cursor;
template <typename T>
class cursor_base;
template <typename T>
class reader_base;
template <typename T>
class writer_base;
template <typename T>
struct writer_mixin;
template <typename T>
struct cursor_mixin;
template <typename T>
struct reader_mixin;
//! @defgroup cursors
//! @{
/*!
* Returns a transducer for updating the parent values via a up-node. It
* processes the input with the function `updater`, passing to it a value or
* tuple containing the values of the parents of the node as first parameter,
* and the input as second. This mapping can thus return an *updated* version
* of the values in the parents with the new input.
*
* @note This transducer should only be used for the setter of output nodes.
*/
template <typename UpdateT>
auto update(UpdateT&& updater)
{
return [=](auto&& step) {
return [=](auto s, auto&&... is) mutable {
s->refresh();
return step(s, updater(current_from(s->parents()), ZUG_FWD(is)...));
};
};
}
/*!
* Mixing to get common tranducers applied to `this` via the `xform` method.
*/
template <typename Deriv>
struct xform_mixin
{
template <typename... Args>
auto map(Args&&... args) const&
{
return static_cast<const Deriv&>(*this).xform(
zug::map(std::forward<Args>(args))...);
}
template <typename... Args>
auto map(Args&&... args) &&
{
return static_cast<Deriv&&>(std::move(*this))
.xform(zug::map(std::forward<Args>(args))...);
}
template <typename... Args>
auto filter(Args&&... args) const&
{
return static_cast<const Deriv&>(*this).xform(
zug::filter(std::forward<Args>(args))...);
}
template <typename... Args>
auto filter(Args&&... args) &&
{
return static_cast<Deriv&&>(std::move(*this))
.xform(zug::filter(std::forward<Args>(args))...);
}
};
//! @}
namespace detail {
template <template <class> class Result, typename... Nodes>
class with_expr;
template <typename Xform, typename... Nodes>
class with_xform_expr;
template <template <class> class Result,
typename Xform,
typename WXform,
typename... Nodes>
class with_wxform_expr;
template <template <class> class Result, typename Lens, typename... Nodes>
class with_lens_expr;
template <template <typename Node> class Result, typename... Nodes>
auto make_with_expr(std::tuple<std::shared_ptr<Nodes>...> nodes)
-> with_expr<Result, Nodes...>
{
return {std::move(nodes)};
}
template <typename Xform, typename... Nodes>
auto make_with_xform_expr(Xform xform,
std::tuple<std::shared_ptr<Nodes>...> nodes)
-> with_xform_expr<Xform, Nodes...>
{
return {std::move(xform), std::move(nodes)};
}
template <template <class> class Result,
typename Xform,
typename WXform,
typename... Nodes>
auto make_with_wxform_expr(Xform xform,
WXform wxform,
std::tuple<std::shared_ptr<Nodes>...> nodes)
-> with_wxform_expr<Result, Xform, WXform, Nodes...>
{
return {std::move(xform), std::move(wxform), std::move(nodes)};
}
template <template <class> class Result, typename Lens, typename... Nodes>
auto make_with_lens_expr(Lens lens, std::tuple<std::shared_ptr<Nodes>...> nodes)
-> with_lens_expr<Result, Lens, Nodes...>
{
return {std::move(lens), std::move(nodes)};
}
template <typename Result>
struct is_reader_base : std::false_type
{};
template <typename T>
struct is_reader_base<reader_base<T>> : std::true_type
{};
template <typename Deriv>
class with_expr_base : public xform_mixin<Deriv>
{
Deriv&& deriv_() { return std::move(static_cast<Deriv&>(*this)); }
public:
template <typename T>
auto operator[](T&& k) &&
{
using value_t = typename decltype(deriv_().make())::value_type;
auto lens = smart_lens<value_t>::make(std::forward<T>(k));
return deriv_().zoom(lens);
}
template <typename T,
typename U = Deriv,
std::enable_if_t<
std::is_same_v<
typename decltype(std::declval<U>().make())::value_type,
T
>,
int> = 0>
operator reader<T>() &&
{
return std::move(*this).make();
}
template <typename T>
operator writer<T>() &&
{
return std::move(*this).make();
}
template <typename T,
typename U = Deriv,
std::enable_if_t<
std::is_same_v<
typename decltype(std::declval<U>().make())::value_type,
T
>,
int> = 0>
operator cursor<T>() &&
{
return std::move(*this).make();
}
auto make() &&
{
auto node = deriv_().make_node_();
using node_t = typename decltype(node)::element_type;
using cursor_t = typename Deriv::template result_t<node_t>;
return cursor_t{node};
}
template <typename TagT = transactional_tag, typename FnT>
auto setter(FnT&& fn) &&
{
return std::move(*this).make().template setter<TagT>(
std::forward<FnT>(fn));
}
auto make_node_() &&
{
using cursor_t = typename Deriv::template result_t<cursor_node<int>>;
if constexpr (is_reader_base<cursor_t>::value) {
return std::move(static_cast<Deriv&>(*this)).make_reader_node_();
} else {
return std::move(static_cast<Deriv&>(*this)).make_cursor_node_();
}
}
};
template <template <class> class Result, typename... Nodes>
class with_expr : public with_expr_base<with_expr<Result, Nodes...>>
{
friend class with_expr_base<with_expr>;
std::tuple<std::shared_ptr<Nodes>...> nodes_;
template <typename T>
using result_t = Result<T>;
auto make_reader_node_() &&
{
return make_merge_reader_node(std::move(nodes_));
}
auto make_cursor_node_() &&
{
return make_merge_cursor_node(std::move(nodes_));
}
public:
with_expr(std::tuple<std::shared_ptr<Nodes>...>&& n)
: nodes_{std::move(n)}
{}
template <typename Xf>
auto xform(Xf&& xf) &&
{
return make_with_xform_expr(std::forward<Xf>(xf), std::move(nodes_));
}
template <typename Xf, typename WXf>
auto xform(Xf&& xf, WXf&& wxf) &&
{
return make_with_wxform_expr<Result>(
std::forward<Xf>(xf), std::forward<WXf>(wxf), std::move(nodes_));
}
template <typename Lens>
auto zoom(Lens&& l) &&
{
return make_with_lens_expr<Result>(std::forward<Lens>(l),
std::move(nodes_));
}
};
template <typename Xform, typename... Nodes>
class with_xform_expr : public with_expr_base<with_xform_expr<Xform, Nodes...>>
{
friend class with_expr_base<with_xform_expr>;
Xform xform_;
std::tuple<std::shared_ptr<Nodes>...> nodes_;
template <typename T>
using result_t = reader_base<T>;
auto make_reader_node_() &&
{
return make_xform_reader_node(std::move(xform_), std::move(nodes_));
}
public:
template <typename Xf, typename Ns>
with_xform_expr(Xf&& xf, Ns&& n)
: xform_{std::forward<Xf>(xf)}
, nodes_{std::forward<Ns>(n)}
{}
template <typename Xf>
auto xform(Xf&& xf) &&
{
return make_with_xform_expr(zug::comp(std::move(xform_), xf),
std::move(nodes_));
}
template <typename Lens>
auto zoom(Lens&& l) &&
{
return std::move(*this).xform(zug::map([l](auto&&... xs) {
return view(l, zug::tuplify(LAGER_FWD(xs)...));
}));
}
};
template <template <class> class Result,
typename Xform,
typename WXform,
typename... Nodes>
class with_wxform_expr
: public with_expr_base<with_wxform_expr<Result, Xform, WXform, Nodes...>>
{
friend class with_expr_base<with_wxform_expr>;
Xform xform_;
WXform wxform_;
std::tuple<std::shared_ptr<Nodes>...> nodes_;
template <typename T>
using result_t = Result<T>;
auto make_reader_node_() &&
{
return make_xform_reader_node(std::move(xform_), nodes_);
}
auto make_cursor_node_() &&
{
return make_xform_cursor_node(
std::move(xform_), std::move(wxform_), nodes_);
}
public:
template <typename Xf, typename WXf, typename Ns>
with_wxform_expr(Xf&& xf, WXf&& wxf, Ns&& n)
: xform_{std::forward<Xf>(xf)}
, wxform_{std::forward<WXf>(wxf)}
, nodes_{std::forward<Ns>(n)}
{}
template <typename Xf>
auto xform(Xf&& xf) &&
{
return make_with_xform_expr(
zug::comp(std::move(xform_), std::forward<Xf>(xf)),
std::move(nodes_));
}
template <typename Xf, typename WXf>
auto xform(Xf&& xf, WXf&& wxf) &&
{
return make_with_wxform_expr<Result>(
zug::comp(std::move(xform_), std::forward<Xf>(xf)),
zug::comp(std::forward<WXf>(wxf), std::move(wxform_)),
std::move(nodes_));
}
template <typename Lens>
auto zoom(Lens&& l) &&
{
return std::move(*this).make().zoom(std::forward<Lens>(l));
}
};
template <template <class> class Result, typename Lens, typename... Nodes>
class with_lens_expr
: public with_expr_base<with_lens_expr<Result, Lens, Nodes...>>
{
friend class with_expr_base<with_lens_expr>;
Lens lens_;
std::tuple<std::shared_ptr<Nodes>...> nodes_;
template <typename T>
using result_t = Result<T>;
auto make_reader_node_() &&
{
return make_lens_reader_node(std::move(lens_), nodes_);
}
auto make_cursor_node_() &&
{
return make_lens_cursor_node(std::move(lens_), nodes_);
}
public:
template <typename L, typename Ns>
with_lens_expr(L&& l, Ns&& n)
: lens_{std::forward<L>(l)}
, nodes_{std::forward<Ns>(n)}
{}
template <typename Xf>
auto xform(Xf&& xf) &&
{
return make_with_xform_expr(
zug::comp(zug::map([l = std::move(lens_)](auto&&... xs) {
return view(l, zug::tuplify(LAGER_FWD(xs)...));
}),
std::forward<Xf>(xf)),
std::move(nodes_));
}
template <typename Xf, typename WXf>
auto xform(Xf&& xf, WXf&& wxf) &&
{
return std::move(*this).make().xform(std::forward<Xf>(xf),
std::forward<WXf>(wxf));
}
template <typename Lens2>
auto zoom(Lens2&& l) &&
{
return make_with_lens_expr<Result>(
zug::comp(std::move(lens_), std::forward<Lens2>(l)),
std::move(nodes_));
}
};
template <typename... ReaderTs>
auto with_aux(reader_mixin<ReaderTs>&&... ins)
{
return detail::make_with_expr<reader_base>(
std::make_tuple(access::node(std::move(ins).make())...));
}
template <typename... ReaderTs>
auto with_aux(const reader_mixin<ReaderTs>&... ins)
{
return detail::make_with_expr<reader_base>(
std::make_tuple(access::node(ins.make())...));
}
template <typename... WriterTs>
auto with_aux(writer_mixin<WriterTs>&&... ins)
{
return detail::make_with_expr<writer_base>(
std::make_tuple(access::node(std::move(ins).make())...));
}
template <typename... WriterTs>
auto with_aux(const writer_mixin<WriterTs>&... ins)
{
return detail::make_with_expr<writer_base>(
std::make_tuple(access::node(std::move(ins))...));
}
template <typename... CursorTs>
auto with_aux(cursor_mixin<CursorTs>&&... ins)
{
return detail::make_with_expr<cursor_base>(
std::make_tuple(access::node(std::move(ins).make())...));
}
template <typename... CursorTs>
auto with_aux(const cursor_mixin<CursorTs>&... ins)
{
return detail::make_with_expr<cursor_base>(
std::make_tuple(access::node(ins.make())...));
}
} // namespace detail
//! @defgroup cursors
//! @{
/*!
* Returns a temporary object that can be used to describe transformations over
* the given set of cursors.
*
* This temporary object has the methods `zoom`, `xform` and `operator[]` just
* like cursors, but returning a new temporary object each time these are
* applied, composing transformations along the way without creating new nodes.
*
* This temporary object can be reified into an actual cursor, creating an
* associated node, by using the `make` method or by converting it to a
* `cursor<T>` type.
*/
template <typename... Cursors>
auto with(Cursors&&... ins)
{
return detail::with_aux(std::forward<Cursors>(ins).make()...);
}
//! @}
} // namespace lager