-
Notifications
You must be signed in to change notification settings - Fork 182
/
reduce_stream.hpp
439 lines (396 loc) · 13.6 KB
/
reduce_stream.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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <unifex/config.hpp>
#include <unifex/async_trace.hpp>
#include <unifex/bind_back.hpp>
#include <unifex/exception.hpp>
#include <unifex/get_stop_token.hpp>
#include <unifex/manual_lifetime.hpp>
#include <unifex/receiver_concepts.hpp>
#include <unifex/sender_concepts.hpp>
#include <unifex/std_concepts.hpp>
#include <unifex/stream_concepts.hpp>
#include <unifex/type_list.hpp>
#include <unifex/type_traits.hpp>
#include <unifex/unstoppable_token.hpp>
#include <exception>
#include <functional>
#include <type_traits>
#include <utility>
#include <unifex/detail/prologue.hpp>
namespace unifex {
namespace _reduce {
template <
typename StreamSender,
typename State,
typename ReducerFunc,
typename Receiver>
struct _op {
struct type;
};
template <
typename StreamSender,
typename State,
typename ReducerFunc,
typename Receiver>
using operation = typename _op<
remove_cvref_t<StreamSender>,
remove_cvref_t<State>,
remove_cvref_t<ReducerFunc>,
remove_cvref_t<Receiver>>::type;
template <
typename StreamSender,
typename State,
typename ReducerFunc,
typename Receiver>
struct _error_cleanup_receiver {
struct type;
};
template <
typename StreamSender,
typename State,
typename ReducerFunc,
typename Receiver>
using error_cleanup_receiver = typename _error_cleanup_receiver<
remove_cvref_t<StreamSender>,
remove_cvref_t<State>,
remove_cvref_t<ReducerFunc>,
remove_cvref_t<Receiver>>::type;
template <
typename StreamSender,
typename State,
typename ReducerFunc,
typename Receiver>
struct _error_cleanup_receiver<StreamSender, State, ReducerFunc, Receiver>::
type {
operation<StreamSender, State, ReducerFunc, Receiver>& op_;
std::exception_ptr ex_;
// No value() in cleanup receiver
template <typename Error>
void set_error(Error error) noexcept {
auto& op = op_;
unifex::deactivate_union_member(op.errorCleanup_);
unifex::set_error(static_cast<Receiver&&>(op.receiver_), (Error &&) error);
}
void set_done() noexcept {
auto& op = op_;
auto ex = std::move(ex_);
unifex::deactivate_union_member(op.errorCleanup_);
unifex::set_error(static_cast<Receiver&&>(op.receiver_), std::move(ex));
}
template(typename CPO) //
(requires is_receiver_query_cpo_v<CPO>) //
friend auto tag_invoke(CPO cpo, const type& r) noexcept(
std::is_nothrow_invocable_v<CPO, const Receiver&>)
-> std::invoke_result_t<CPO, const Receiver&> {
return std::move(cpo)(std::as_const(r.op_.receiver_));
}
friend unstoppable_token
tag_invoke(tag_t<get_stop_token>, const type&) noexcept {
return {};
}
#if UNIFEX_ENABLE_CONTINUATION_VISITATIONS
template <typename Func>
friend void
tag_invoke(tag_t<visit_continuations>, const type& r, Func&& func) {
std::invoke(func, r.op_.receiver_);
}
#endif
};
template <
typename StreamSender,
typename State,
typename ReducerFunc,
typename Receiver>
struct _done_cleanup_receiver {
struct type;
};
template <
typename StreamSender,
typename State,
typename ReducerFunc,
typename Receiver>
using done_cleanup_receiver = typename _done_cleanup_receiver<
remove_cvref_t<StreamSender>,
remove_cvref_t<State>,
remove_cvref_t<ReducerFunc>,
remove_cvref_t<Receiver>>::type;
template <
typename StreamSender,
typename State,
typename ReducerFunc,
typename Receiver>
struct _done_cleanup_receiver<StreamSender, State, ReducerFunc, Receiver>::
type {
operation<StreamSender, State, ReducerFunc, Receiver>& op_;
template <typename Error>
void set_error(Error error) && noexcept {
auto& op = op_;
unifex::deactivate_union_member(op.doneCleanup_);
unifex::set_error(static_cast<Receiver&&>(op.receiver_), (Error &&) error);
}
void set_done() && noexcept {
auto& op = op_;
unifex::deactivate_union_member(op.doneCleanup_);
unifex::set_value(std::move(op.receiver_), std::move(op.state_));
}
template(typename CPO) //
(requires is_receiver_query_cpo_v<CPO>) //
friend auto tag_invoke(CPO cpo, const type& r) noexcept(
std::is_nothrow_invocable_v<CPO, const Receiver&>)
-> std::invoke_result_t<CPO, const Receiver&> {
return std::move(cpo)(std::as_const(r.op_.receiver_));
}
friend unstoppable_token
tag_invoke(tag_t<get_stop_token>, const type&) noexcept {
return {};
}
#if UNIFEX_ENABLE_CONTINUATION_VISITATIONS
template <typename Func>
friend void
tag_invoke(tag_t<visit_continuations>, const type& r, Func&& func) {
std::invoke(func, r.op_.receiver_);
}
#endif
};
template <
typename StreamSender,
typename State,
typename ReducerFunc,
typename Receiver>
struct _next_receiver {
struct type;
};
template <
typename StreamSender,
typename State,
typename ReducerFunc,
typename Receiver>
using next_receiver = typename _next_receiver<
remove_cvref_t<StreamSender>,
remove_cvref_t<State>,
remove_cvref_t<ReducerFunc>,
remove_cvref_t<Receiver>>::type;
template <
typename StreamSender,
typename State,
typename ReducerFunc,
typename Receiver>
struct _next_receiver<StreamSender, State, ReducerFunc, Receiver>::type {
using error_cleanup_receiver_t =
error_cleanup_receiver<StreamSender, State, ReducerFunc, Receiver>;
using done_cleanup_receiver_t =
done_cleanup_receiver<StreamSender, State, ReducerFunc, Receiver>;
using next_receiver_t =
next_receiver<StreamSender, State, ReducerFunc, Receiver>;
operation<StreamSender, State, ReducerFunc, Receiver>& op_;
template(typename CPO) //
(requires is_receiver_query_cpo_v<CPO>) //
friend auto tag_invoke(CPO cpo, const type& r) noexcept(
std::is_nothrow_invocable_v<CPO, const Receiver&>)
-> std::invoke_result_t<CPO, const Receiver&> {
return std::move(cpo)(std::as_const(r.op_.receiver_));
}
#if UNIFEX_ENABLE_CONTINUATION_VISITATIONS
template <typename Func>
friend void
tag_invoke(tag_t<visit_continuations>, const type& r, Func&& func) {
std::invoke(func, r.op_.receiver_);
}
#endif
template <typename... Values>
void set_value(Values... values) && noexcept {
auto& op = op_;
unifex::deactivate_union_member(op.next_);
UNIFEX_TRY {
op.state_ =
std::invoke(op.reducer_, std::move(op.state_), (Values &&) values...);
unifex::activate_union_member_with(op.next_, [&] {
return unifex::connect(next(op.stream_), next_receiver_t{op});
});
unifex::start(op.next_.get());
}
UNIFEX_CATCH(...) {
unifex::activate_union_member_with(op.errorCleanup_, [&] {
return unifex::connect(
cleanup(op.stream_),
error_cleanup_receiver_t{op, std::current_exception()});
});
unifex::start(op.errorCleanup_.get());
}
}
void set_done() && noexcept {
auto& op = op_;
unifex::deactivate_union_member(op.next_);
unifex::activate_union_member_with(op.doneCleanup_, [&] {
return unifex::connect(cleanup(op.stream_), done_cleanup_receiver_t{op});
});
unifex::start(op.doneCleanup_.get());
}
void set_error(std::exception_ptr ex) && noexcept {
auto& op = op_;
unifex::deactivate_union_member(op.next_);
unifex::activate_union_member_with(op.errorCleanup_, [&] {
return unifex::connect(
cleanup(op.stream_), error_cleanup_receiver_t{op, std::move(ex)});
});
unifex::start(op.errorCleanup_.get());
}
template <typename Error>
void set_error(Error&& e) && noexcept {
std::move(*this).set_error(make_exception_ptr((Error &&) e));
}
};
template <
typename StreamSender,
typename State,
typename ReducerFunc,
typename Receiver>
struct _op<StreamSender, State, ReducerFunc, Receiver>::type {
UNIFEX_NO_UNIQUE_ADDRESS StreamSender stream_;
UNIFEX_NO_UNIQUE_ADDRESS State state_;
UNIFEX_NO_UNIQUE_ADDRESS ReducerFunc reducer_;
UNIFEX_NO_UNIQUE_ADDRESS Receiver receiver_;
using next_receiver_t =
next_receiver<StreamSender, State, ReducerFunc, Receiver>;
using error_cleanup_receiver_t =
error_cleanup_receiver<StreamSender, State, ReducerFunc, Receiver>;
using done_cleanup_receiver_t =
done_cleanup_receiver<StreamSender, State, ReducerFunc, Receiver>;
using next_op =
manual_lifetime<next_operation_t<StreamSender, next_receiver_t>>;
using error_op = manual_lifetime<
cleanup_operation_t<StreamSender, error_cleanup_receiver_t>>;
using done_op = manual_lifetime<
cleanup_operation_t<StreamSender, done_cleanup_receiver_t>>;
union {
next_op next_;
error_op errorCleanup_;
done_op doneCleanup_;
};
template <
typename StreamSender2,
typename State2,
typename ReducerFunc2,
typename Receiver2>
explicit type(
StreamSender2&& stream,
State2&& state,
ReducerFunc2&& reducer,
Receiver2&& receiver)
: stream_(std::forward<StreamSender2>(stream))
, state_(std::forward<State2>(state))
, reducer_(std::forward<ReducerFunc2>(reducer))
, receiver_(std::forward<Receiver2>(receiver)) {}
~type() {} // Due to the union member, this is load-bearing. DO NOT DELETE.
void start() noexcept {
UNIFEX_TRY {
unifex::activate_union_member_with(next_, [&] {
return unifex::connect(next(stream_), next_receiver_t{*this});
});
unifex::start(next_.get());
}
UNIFEX_CATCH(...) {
unifex::set_error(
static_cast<Receiver&&>(receiver_), std::current_exception());
}
}
};
template <typename StreamSender, typename State, typename ReducerFunc>
struct _sender {
struct type;
};
template <typename StreamSender, typename State, typename ReducerFunc>
using sender = typename _sender<
remove_cvref_t<StreamSender>,
remove_cvref_t<State>,
remove_cvref_t<ReducerFunc>>::type;
template <typename StreamSender, typename State, typename ReducerFunc>
struct _sender<StreamSender, State, ReducerFunc>::type {
using sender = type;
StreamSender stream_;
State initialState_;
ReducerFunc reducer_;
template <
template <typename...>
class Variant,
template <typename...>
class Tuple>
using value_types = Variant<Tuple<State>>;
template <template <typename...> class Variant>
using error_types = typename concat_type_lists_unique_t<
sender_error_types_t<next_sender_t<StreamSender>, type_list>,
sender_error_types_t<cleanup_sender_t<StreamSender>, type_list>,
type_list<std::exception_ptr>>::template apply<Variant>;
static constexpr bool sends_done = false;
template <typename Receiver>
using operation_t = operation<StreamSender, State, ReducerFunc, Receiver>;
template <typename Receiver>
using next_receiver_t =
next_receiver<StreamSender, State, ReducerFunc, Receiver>;
template <typename Receiver>
using error_cleanup_receiver_t =
error_cleanup_receiver<StreamSender, State, ReducerFunc, Receiver>;
template <typename Receiver>
using done_cleanup_receiver_t =
done_cleanup_receiver<StreamSender, State, ReducerFunc, Receiver>;
template(typename Self, typename Receiver) //
(requires same_as<remove_cvref_t<Self>, type> AND receiver<Receiver> AND
sender_to<next_sender_t<StreamSender>, next_receiver_t<Receiver>> AND
sender_to<
cleanup_sender_t<StreamSender>,
error_cleanup_receiver_t<Receiver>> AND
sender_to<
cleanup_sender_t<StreamSender>,
done_cleanup_receiver_t<Receiver>>) //
friend operation_t<Receiver> tag_invoke(
tag_t<connect>, Self&& self, Receiver&& receiver) {
return operation_t<Receiver>{
static_cast<Self&&>(self).stream_,
static_cast<Self&&>(self).initialState_,
static_cast<Self&&>(self).reducer_,
(Receiver &&) receiver};
}
};
} // namespace _reduce
namespace _reduce_cpo {
inline const struct _fn {
template <typename StreamSender, typename State, typename ReducerFunc>
auto operator()(
StreamSender&& stream, State&& initialState, ReducerFunc&& reducer) const
noexcept(std::is_nothrow_constructible_v<
_reduce::sender<StreamSender, State, ReducerFunc>,
StreamSender,
State,
ReducerFunc>)
-> _reduce::sender<StreamSender, State, ReducerFunc> {
return _reduce::sender<StreamSender, State, ReducerFunc>{
(StreamSender &&) stream,
(State &&) initialState,
(ReducerFunc &&) reducer};
}
template <typename State, typename ReducerFunc>
constexpr auto operator()(State&& initialState, ReducerFunc&& reducer) const
noexcept(std::is_nothrow_invocable_v<tag_t<bind_back>, _fn, State, ReducerFunc>)
-> bind_back_result_t<_fn, State, ReducerFunc> {
return bind_back(*this, (State &&) initialState, (ReducerFunc &&) reducer);
}
} reduce_stream{};
} // namespace _reduce_cpo
using _reduce_cpo::reduce_stream;
} // namespace unifex
#include <unifex/detail/epilogue.hpp>