-
Notifications
You must be signed in to change notification settings - Fork 183
/
filter_stream.hpp
254 lines (218 loc) · 8.12 KB
/
filter_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
/*
* 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/bind_back.hpp>
#include <unifex/manual_lifetime.hpp>
#include <unifex/std_concepts.hpp>
#include <unifex/stream_concepts.hpp>
#include <exception>
#include <functional>
#include <type_traits>
#include <utility>
#include <unifex/detail/prologue.hpp>
namespace unifex {
namespace _filter {
template <typename StreamSender, typename FilterFunc, typename Receiver>
struct _op {
struct type;
};
template <typename StreamSender, typename FilterFunc, typename Receiver>
using operation = typename _op<
remove_cvref_t<StreamSender>,
remove_cvref_t<FilterFunc>,
remove_cvref_t<Receiver>>::type;
template <typename StreamSender, typename FilterFunc, typename Receiver>
struct _next_receiver {
struct type;
};
template <typename StreamSender, typename FilterFunc, typename Receiver>
using next_receiver = typename _next_receiver<
remove_cvref_t<StreamSender>,
remove_cvref_t<FilterFunc>,
remove_cvref_t<Receiver>>::type;
template <typename StreamSender, typename FilterFunc, typename Receiver>
struct _next_receiver<StreamSender, FilterFunc, Receiver>::type {
using next_receiver_t = next_receiver<StreamSender, FilterFunc, Receiver>;
operation<StreamSender, FilterFunc, 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_TRY {
const bool doFilter = !std::invoke(op.filter_, std::as_const(values)...);
if (doFilter) {
op.next_.destruct();
op.nextEngaged_ = false;
op.next_.construct_with([&] {
return unifex::connect(next(op.stream_), next_receiver_t{op});
});
op.nextEngaged_ = true;
unifex::start(op.next_.get());
} else {
unifex::set_value(
std::move(op.receiver_), std::forward<Values>(values)...);
}
}
UNIFEX_CATCH(...) {
unifex::set_error(std::move(op.receiver_), std::current_exception());
}
}
void set_done() && noexcept { unifex::set_done(std::move(op_.receiver_)); }
template <typename Error>
void set_error(Error&& e) && noexcept {
unifex::set_error(std::move(op_.receiver_), std::forward<Error>(e));
}
};
template <typename StreamSender, typename FilterFunc, typename Receiver>
struct _op<StreamSender, FilterFunc, Receiver>::type {
StreamSender& stream_;
FilterFunc& filter_;
Receiver receiver_;
using next_receiver_t = next_receiver<StreamSender, FilterFunc, Receiver>;
using next_op =
manual_lifetime<next_operation_t<StreamSender, next_receiver_t>>;
next_op next_;
bool nextEngaged_{false};
template <typename Receiver2>
explicit type(
StreamSender& stream,
FilterFunc& filter,
Receiver2&& receiver) noexcept(std::
is_nothrow_constructible_v<
Receiver,
Receiver2>&&
is_nothrow_connectable_v<
next_sender_t<StreamSender>,
Receiver&>)
: stream_(stream)
, filter_(filter)
, receiver_(std::forward<Receiver2>(receiver)) {
next_.construct_with(
[&] { return unifex::connect(next(stream_), next_receiver_t{*this}); });
nextEngaged_ = true;
}
~type() {
if (nextEngaged_) {
next_.destruct();
}
}
void start() noexcept { unifex::start(next_.get()); }
};
template <typename StreamSender, typename FilterFunc>
struct _sender {
struct type;
};
template <typename StreamSender, typename FilterFunc>
using sender =
typename _sender<remove_cvref_t<StreamSender>, remove_cvref_t<FilterFunc>>::
type;
template <typename StreamSender, typename FilterFunc>
struct _sender<StreamSender, FilterFunc>::type {
using sender = type;
StreamSender& stream_;
FilterFunc& filter_;
// value and error types just point to the ones from the stream sender
template <
template <typename...>
class Variant,
template <typename...>
class Tuple>
using value_types =
sender_value_types_t<next_sender_t<StreamSender>, Variant, Tuple>;
template <template <typename...> class Variant>
using error_types = typename concat_type_lists_unique_t<
sender_error_types_t<next_sender_t<StreamSender>, type_list>,
type_list<std::exception_ptr>>::template apply<Variant>;
static constexpr bool sends_done =
sender_traits<next_sender_t<StreamSender>>::sends_done;
template <typename Receiver>
using operation_t = operation<StreamSender, FilterFunc, Receiver>;
template <typename Receiver>
using next_receiver_t = next_receiver<StreamSender, FilterFunc, 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>>) //
friend operation_t<Receiver> tag_invoke(
tag_t<connect>, Self&& self, Receiver&& receiver) {
return operation_t<Receiver>{
self.stream_,
std::move(self).filter_,
std::forward<Receiver>(receiver)};
}
};
} // namespace _filter
namespace _filter_stream {
template <typename StreamSender, typename FilterFunc>
struct _filter_stream {
struct type;
};
template <typename StreamSender, typename FilterFunc>
struct _filter_stream<StreamSender, FilterFunc>::type {
UNIFEX_NO_UNIQUE_ADDRESS StreamSender stream_;
UNIFEX_NO_UNIQUE_ADDRESS FilterFunc filter_;
friend auto tag_invoke(tag_t<next>, type& s) noexcept(
std::is_nothrow_constructible_v<
_filter::sender<StreamSender, FilterFunc>,
StreamSender&,
FilterFunc&>) {
return _filter::sender<StreamSender, FilterFunc>{s.stream_, s.filter_};
}
friend auto
tag_invoke(tag_t<cleanup>, type& s) noexcept(noexcept(cleanup(s.stream_))) {
return cleanup(s.stream_);
}
};
template <typename StreamSender, typename FilterFunc>
using filter_stream = typename _filter_stream<
remove_cvref_t<StreamSender>,
remove_cvref_t<FilterFunc>>::type;
struct _fn {
template <typename StreamSender, typename FilterFunc>
auto operator()(StreamSender&& stream, FilterFunc&& filterFunc) const
noexcept(std::is_nothrow_constructible_v<
filter_stream<StreamSender, FilterFunc>,
StreamSender,
FilterFunc>) -> filter_stream<StreamSender, FilterFunc> {
return filter_stream<StreamSender, FilterFunc>{
std::forward<StreamSender>(stream),
std::forward<FilterFunc>(filterFunc)};
}
template <typename FilterFunc>
constexpr auto operator()(FilterFunc&& filterFunc) const
noexcept(std::is_nothrow_invocable_v<tag_t<bind_back>, _fn, FilterFunc>)
-> bind_back_result_t<_fn, FilterFunc> {
return bind_back(*this, std::forward<FilterFunc>(filterFunc));
}
};
} // namespace _filter_stream
inline constexpr _filter_stream::_fn filter_stream{};
} // namespace unifex
#include <unifex/detail/epilogue.hpp>