43 #if !defined(RXCPP_OPERATORS_RX_MERGE_HPP) 44 #define RXCPP_OPERATORS_RX_MERGE_HPP 46 #include "../rx-includes.hpp" 55 struct merge_invalid_arguments {};
58 struct merge_invalid :
public rxo::operator_base<merge_invalid_arguments<AN...>> {
59 using type = observable<merge_invalid_arguments<
AN...>, merge_invalid<
AN...>>;
62 using merge_invalid_t =
typename merge_invalid<
AN...>::type;
64 template<
class T,
class Observable,
class Coordination>
66 :
public operator_base<rxu::value_type_t<rxu::decay_t<T>>>
71 typedef merge<T, Observable, Coordination> this_type;
73 typedef rxu::decay_t<T> source_value_type;
74 typedef rxu::decay_t<Observable> source_type;
76 typedef typename source_type::source_operator_type source_operator_type;
77 typedef typename source_value_type::value_type value_type;
79 typedef rxu::decay_t<Coordination> coordination_type;
80 typedef typename coordination_type::coordinator_type coordinator_type;
84 values(source_operator_type o, coordination_type sf)
85 : source_operator(std::move(o))
86 , coordination(std::move(sf))
89 source_operator_type source_operator;
90 coordination_type coordination;
94 merge(
const source_type& o, coordination_type sf)
95 : initial(o.source_operator, std::move(sf))
99 template<
class Subscriber>
100 void on_subscribe(Subscriber scbr)
const {
103 typedef Subscriber output_type;
105 struct merge_state_type
106 :
public std::enable_shared_from_this<merge_state_type>
109 merge_state_type(values i, coordinator_type coor, output_type oarg)
111 , source(i.source_operator)
112 , pendingCompletions(0)
113 , coordinator(std::move(coor))
114 , out(std::move(oarg))
117 observable<source_value_type, source_operator_type> source;
120 int pendingCompletions;
121 coordinator_type coordinator;
125 auto coordinator = initial.coordination.create_coordinator(scbr.get_subscription());
128 auto state = std::make_shared<merge_state_type>(initial, std::move(coordinator), std::move(scbr));
130 composite_subscription outercs;
134 state->out.add(outercs);
137 [&](){
return state->coordinator.in(state->source);},
139 if (source.empty()) {
143 ++state->pendingCompletions;
147 auto sink = make_subscriber<source_value_type>(
151 [state](source_value_type st) {
153 composite_subscription innercs;
157 auto innercstoken = state->out.add(innercs);
160 state->out.remove(innercstoken);
163 auto selectedSource = state->coordinator.in(st);
165 ++state->pendingCompletions;
168 auto sinkInner = make_subscriber<value_type>(
172 [state, st](value_type ct) {
173 state->out.on_next(std::move(ct));
176 [state](std::exception_ptr e) {
177 state->out.on_error(e);
181 if (--state->pendingCompletions == 0) {
182 state->out.on_completed();
187 auto selectedSinkInner = state->coordinator.out(sinkInner);
188 selectedSource.subscribe(std::move(selectedSinkInner));
191 [state](std::exception_ptr e) {
192 state->out.on_error(e);
196 if (--state->pendingCompletions == 0) {
197 state->out.on_completed();
202 [&](){
return state->coordinator.out(sink);},
204 if (selectedSink.empty()) {
207 source->subscribe(std::move(selectedSink.get()));
215 template<
class...
AN>
226 template<
class Observable,
230 class Merge = rxo::detail::merge<SourceValue, rxu::decay_t<Observable>,
identity_one_worker>,
238 template<
class Observable,
class Coordination,
240 is_observable<Observable>,
247 static Result
member(Observable&& o, Coordination&& cn) {
248 return Result(Merge(std::forward<Observable>(o), std::forward<Coordination>(cn)));
251 template<
class Observable,
class Value0,
class... ValueN,
261 static Result
member(Observable&& o, Value0&& v0, ValueN&&... vn) {
265 template<
class Observable,
class Coordination,
class Value0,
class... ValueN,
268 is_coordination<Coordination>>,
276 static Result
member(Observable&& o, Coordination&& cn, Value0&& v0, ValueN&&... vn) {
277 return Result(Merge(
rxs::from(o.as_dynamic(), v0.as_dynamic(), vn.as_dynamic()...), std::forward<Coordination>(cn)));
280 template<
class...
AN>
281 static operators::detail::merge_invalid_t<
AN...>
member(
AN...) {
284 static_assert(
sizeof...(
AN) == 10000,
"merge takes (optional Coordination, optional Value0, optional ValueN...)");
Definition: rx-util.hpp:100
Definition: rx-all.hpp:26
typename std::decay< T >::type::value_type value_type_t
Definition: rx-util.hpp:35
auto make_subscription() -> subscription
Definition: rx-subscription.hpp:197
Definition: rx-operators.hpp:69
auto AN
Definition: rx-finally.hpp:105
Definition: rx-operators.hpp:255
typename std::decay< T >::type decay_t
Definition: rx-util.hpp:36
Definition: rx-operators.hpp:47
static const bool value
Definition: rx-predef.hpp:123
linq_driver< iter_cursor< typename util::container_traits< TContainer >::iterator > > from(TContainer &c)
Definition: linq.hpp:556
typename std::enable_if< all_true_type< BN... >::value >::type enable_if_all_true_type_t
Definition: rx-util.hpp:114
a source of values. subscribe or use one of the operator methods that return a new observable...
Definition: rx-observable.hpp:510
Definition: rx-util.hpp:325
static Result member(Observable &&o)
Definition: rx-merge.hpp:234
auto merge(AN &&...an) -> operator_factory< merge_tag, AN... >
For each given observable subscribe. For each item emitted from all of the given observables, deliver from the new observable that is returned.
Definition: rx-merge.hpp:216
auto on_exception(const F &f, const OnError &c) -> typename std::enable_if< detail::is_on_error< OnError >::value, typename detail::maybe_from_result< F >::type >::type
Definition: rx-observer.hpp:639
Definition: rx-coordination.hpp:114
static Result member(Observable &&o, Coordination &&cn, Value0 &&v0, ValueN &&...vn)
Definition: rx-merge.hpp:276
static operators::detail::merge_invalid_t< AN... > member(AN...)
Definition: rx-merge.hpp:281
static Result member(Observable &&o, Value0 &&v0, ValueN &&...vn)
Definition: rx-merge.hpp:261
static Result member(Observable &&o, Coordination &&cn)
Definition: rx-merge.hpp:247
identity_one_worker identity_current_thread()
Definition: rx-coordination.hpp:175
Definition: rx-predef.hpp:177
Definition: rx-coordination.hpp:37