5 #if !defined(RXCPP_RX_OBSERVER_HPP) 6 #define RXCPP_RX_OBSERVER_HPP 24 void operator()(
const T&)
const {}
28 void operator()(std::exception_ptr)
const {
35 void operator()(std::exception_ptr)
const {
38 struct OnCompletedEmpty
40 void operator()()
const {}
43 template<
class T,
class State,
class OnNext>
48 OnNextForward() : onnext() {}
49 explicit OnNextForward(onnext_t on) : onnext(std::move(on)) {}
51 void operator()(state_t& s, T& t)
const {
54 void operator()(state_t& s, T&& t)
const {
58 template<
class T,
class State>
59 struct OnNextForward<T, State, void>
63 void operator()(state_t& s, T& t)
const {
66 void operator()(state_t& s, T&& t)
const {
71 template<
class State,
class OnError>
76 OnErrorForward() : onerror() {}
77 explicit OnErrorForward(onerror_t oe) : onerror(std::move(oe)) {}
79 void operator()(state_t& s, std::exception_ptr ep)
const {
84 struct OnErrorForward<State, void>
88 void operator()(state_t& s, std::exception_ptr ep)
const {
93 template<
class State,
class OnCompleted>
94 struct OnCompletedForward
98 OnCompletedForward() : oncompleted() {}
99 explicit OnCompletedForward(oncompleted_t oc) : oncompleted(std::move(oc)) {}
100 oncompleted_t oncompleted;
101 void operator()(state_t& s)
const {
105 template<
class State>
106 struct OnCompletedForward<State, void>
108 OnCompletedForward() {}
109 void operator()(State& s)
const {
114 template<
class T,
class F>
118 template<
class CT,
class CF>
119 static auto check(
int) -> decltype((*(CF*)
nullptr)(*(CT*)
nullptr));
120 template<
class CT,
class CF>
121 static not_void check(...);
124 static const bool value = std::is_same<detail_result, void>::value;
132 static auto check(
int) -> decltype((*(CF*)
nullptr)(*(std::exception_ptr*)
nullptr));
134 static not_void check(...);
136 static const bool value = std::is_same<decltype(check<rxu::decay_t<F>>(0)),
void>::value;
139 template<
class State,
class F>
140 struct is_on_error_for
144 static auto check(
int) -> decltype((*(CF*)
nullptr)(*(State*)
nullptr, *(std::exception_ptr*)
nullptr));
146 static not_void check(...);
148 static const bool value = std::is_same<decltype(check<rxu::decay_t<F>>(0)),
void>::value;
152 struct is_on_completed
156 static auto check(
int) -> decltype((*(CF*)
nullptr)());
158 static not_void check(...);
160 static const bool value = std::is_same<decltype(check<rxu::decay_t<F>>(0)),
void>::value;
178 template<
class T,
class State,
class OnNext,
class OnError,
class OnCompleted>
184 using on_next_t =
typename std::conditional<
185 !std::is_same<void, OnNext>::value,
187 detail::OnNextForward<T, State, OnNext>>::type;
189 !std::is_same<void, OnError>::value,
191 detail::OnErrorForward<State, OnError>>::type;
193 !std::is_same<void, OnCompleted>::value,
195 detail::OnCompletedForward<State, OnCompleted>>::type;
206 : state(std::move(s))
207 , onnext(std::move(n))
208 , onerror(std::move(e))
209 , oncompleted(std::move(c))
213 : state(std::move(s))
214 , onnext(std::move(n))
216 , oncompleted(std::move(c))
223 , oncompleted(o.oncompleted)
227 : state(std::move(o.state))
228 , onnext(std::move(o.onnext))
229 , onerror(std::move(o.onerror))
230 , oncompleted(std::move(o.oncompleted))
234 state = std::move(o.state);
235 onnext = std::move(o.onnext);
236 onerror = std::move(o.onerror);
237 oncompleted = std::move(o.oncompleted);
245 onnext(state, std::move(t));
269 template<
class T,
class OnNext,
class OnError,
class OnCompleted>
270 class observer<T, detail::stateless_observer_tag, OnNext, OnError, OnCompleted> :
public observer_base<T>
274 using on_next_t =
typename std::conditional<
275 !std::is_same<void, OnNext>::value,
276 rxu::decay_t<OnNext>,
277 detail::OnNextEmpty<T>>::type;
279 !std::is_same<void, OnError>::value,
280 rxu::decay_t<OnError>,
281 detail::OnErrorEmpty>::type;
283 !std::is_same<void, OnCompleted>::value,
284 rxu::decay_t<OnCompleted>,
285 detail::OnCompletedEmpty>::type;
293 static_assert(detail::is_on_next_of<T, on_next_t>::value,
"Function supplied for on_next must be a function with the signature void(T);");
294 static_assert(detail::is_on_error<on_error_t>::value,
"Function supplied for on_error must be a function with the signature void(std::exception_ptr);");
295 static_assert(detail::is_on_completed<on_completed_t>::value,
"Function supplied for on_completed must be a function with the signature void();");
305 : onnext(std::move(n))
306 , onerror(std::move(e))
307 , oncompleted(std::move(c))
313 , oncompleted(o.oncompleted)
317 : onnext(std::move(o.onnext))
318 , onerror(std::move(o.onerror))
319 , oncompleted(std::move(o.oncompleted))
323 onnext = std::move(o.onnext);
324 onerror = std::move(o.onerror);
325 oncompleted = std::move(o.oncompleted);
329 void on_next(T& t)
const {
332 void on_next(T&& t)
const {
333 onnext(std::move(t));
335 void on_error(std::exception_ptr e)
const {
338 void on_completed()
const {
350 struct virtual_observer :
public std::enable_shared_from_this<virtual_observer<T>>
352 virtual ~virtual_observer() {}
353 virtual void on_next(T&)
const {};
354 virtual void on_next(T&&)
const {};
355 virtual void on_error(std::exception_ptr)
const {};
356 virtual void on_completed()
const {};
359 template<
class T,
class Observer>
360 struct specific_observer :
public virtual_observer<T>
362 explicit specific_observer(Observer o)
363 : destination(std::move(o))
367 Observer destination;
368 virtual void on_next(T& t)
const {
369 destination.on_next(t);
371 virtual void on_next(T&& t)
const {
372 destination.on_next(std::move(t));
374 virtual void on_error(std::exception_ptr e)
const {
375 destination.on_error(e);
377 virtual void on_completed()
const {
378 destination.on_completed();
401 using virtual_observer = detail::virtual_observer<T>;
403 std::shared_ptr<virtual_observer> destination;
405 template<
class Observer>
406 static auto make_destination(Observer o)
407 -> std::shared_ptr<virtual_observer> {
408 return std::make_shared<detail::specific_observer<T, Observer>>(std::move(o));
416 : destination(o.destination)
420 : destination(std::move(o.destination))
424 template<
class Observer>
426 : destination(make_destination(std::move(o)))
431 destination = std::move(o.destination);
439 destination->on_next(std::forward<V>(v));
444 destination->on_error(e);
449 destination->on_completed();
458 template<
class T,
class DefaultOnError = detail::OnErrorEmpty>
464 template<
class T,
class DefaultOnError = detail::OnErrorEmpty,
class U,
class State,
class OnNext,
class OnError,
class OnCompleted>
469 template<
class T,
class DefaultOnError = detail::OnErrorEmpty,
class Observer>
471 ->
typename std::enable_if<
472 !detail::is_on_next_of<T, Observer>::value &&
473 !detail::is_on_error<Observer>::value &&
476 return std::move(ob);
478 template<
class T,
class DefaultOnError = detail::OnErrorEmpty,
class Observer>
480 ->
typename std::enable_if<
481 !detail::is_on_next_of<T, Observer>::value &&
482 !detail::is_on_error<Observer>::value &&
483 !is_observer<Observer>::value,
487 template<
class T,
class DefaultOnError = detail::OnErrorEmpty,
class OnNext>
489 ->
typename std::enable_if<
490 detail::is_on_next_of<T, OnNext>::value,
495 template<
class T,
class DefaultOnError = detail::OnErrorEmpty,
class OnError>
497 ->
typename std::enable_if<
498 detail::is_on_error<OnError>::value,
500 return observer<T, detail::stateless_observer_tag, detail::OnNextEmpty<T>, OnError>(
501 detail::OnNextEmpty<T>(), std::move(oe));
503 template<
class T,
class DefaultOnError = detail::OnErrorEmpty,
class OnNext,
class OnError>
505 ->
typename std::enable_if<
506 detail::is_on_next_of<T, OnNext>::value &&
507 detail::is_on_error<OnError>::value,
510 std::move(on), std::move(oe));
512 template<
class T,
class DefaultOnError = detail::OnErrorEmpty,
class OnNext,
class OnCompleted>
514 ->
typename std::enable_if<
515 detail::is_on_next_of<T, OnNext>::value &&
516 detail::is_on_completed<OnCompleted>::value,
519 std::move(on), DefaultOnError(), std::move(oc));
521 template<
class T,
class DefaultOnError = detail::OnErrorEmpty,
class OnNext,
class OnError,
class OnCompleted>
523 ->
typename std::enable_if<
524 detail::is_on_next_of<T, OnNext>::value &&
525 detail::is_on_error<OnError>::value &&
526 detail::is_on_completed<OnCompleted>::value,
529 std::move(on), std::move(oe), std::move(oc));
533 template<
class T,
class State,
class OnNext>
535 ->
typename std::enable_if<
536 !detail::is_on_next_of<T, State>::value &&
537 !detail::is_on_error<State>::value,
540 std::move(os), std::move(on));
542 template<
class T,
class State,
class OnError>
544 ->
typename std::enable_if<
545 !detail::is_on_next_of<T, State>::value &&
546 !detail::is_on_error<State>::value &&
547 detail::is_on_error_for<State, OnError>::value,
549 return observer<T, State, detail::OnNextEmpty<T>, OnError>(
550 std::move(os), detail::OnNextEmpty<T>(), std::move(oe));
552 template<
class T,
class State,
class OnNext,
class OnError>
554 ->
typename std::enable_if<
555 !detail::is_on_next_of<T, State>::value &&
556 !detail::is_on_error<State>::value &&
557 detail::is_on_error_for<State, OnError>::value,
560 std::move(os), std::move(on), std::move(oe));
562 template<
class T,
class State,
class OnNext,
class OnCompleted>
564 ->
typename std::enable_if<
565 !detail::is_on_next_of<T, State>::value &&
566 !detail::is_on_error<State>::value,
569 std::move(os), std::move(on), std::move(oc));
571 template<
class T,
class State,
class OnNext,
class OnError,
class OnCompleted>
573 ->
typename std::enable_if<
574 !detail::is_on_next_of<T, State>::value &&
575 !detail::is_on_error<State>::value &&
576 detail::is_on_error_for<State, OnError>::value,
579 std::move(os), std::move(on), std::move(oe), std::move(oc));
582 template<
class T,
class Observer>
584 ->
typename std::enable_if<
585 !detail::is_on_next_of<T, Observer>::value,
589 template<
class T,
class OnNext>
591 ->
typename std::enable_if<
592 detail::is_on_next_of<T, OnNext>::value,
595 make_observer<T>(std::forward<OnNext>(on)));
597 template<
class T,
class OnNext,
class OnError>
599 ->
typename std::enable_if<
600 detail::is_on_next_of<T, OnNext>::value &&
601 detail::is_on_error<OnError>::value,
604 make_observer<T>(std::forward<OnNext>(on), std::forward<OnError>(oe)));
606 template<
class T,
class OnNext,
class OnCompleted>
608 ->
typename std::enable_if<
609 detail::is_on_next_of<T, OnNext>::value &&
610 detail::is_on_completed<OnCompleted>::value,
613 make_observer<T>(std::forward<OnNext>(on), std::forward<OnCompleted>(oc)));
615 template<
class T,
class OnNext,
class OnError,
class OnCompleted>
617 ->
typename std::enable_if<
618 detail::is_on_next_of<T, OnNext>::value &&
619 detail::is_on_error<OnError>::value &&
620 detail::is_on_completed<OnCompleted>::value,
623 make_observer<T>(std::forward<OnNext>(on), std::forward<OnError>(oe), std::forward<OnCompleted>(oc)));
629 struct maybe_from_result
631 typedef decltype((*(F*)
nullptr)()) decl_result_type;
633 typedef rxu::maybe<result_type> type;
638 template<
class F,
class OnError>
640 ->
typename std::enable_if<detail::is_on_error<OnError>::value,
typename detail::maybe_from_result<F>::type>::type {
641 typename detail::maybe_from_result<F>::type r;
645 c(std::current_exception());
650 template<
class F,
class Subscriber>
652 ->
typename std::enable_if<is_subscriber<Subscriber>::value,
typename detail::maybe_from_result<F>::type>::type {
653 typename detail::maybe_from_result<F>::type r;
657 s.on_error(std::current_exception());
observer()
Definition: rx-observer.hpp:412
Definition: rx-predef.hpp:88
Definition: rx-all.hpp:26
auto make_observer_dynamic(Observer o) -> typename std::enable_if< !detail::is_on_next_of< T, Observer >::value, observer< T >>::type
Definition: rx-observer.hpp:583
observer< T > as_dynamic() const
Definition: rx-observer.hpp:453
observer(state_t s, on_next_t n, on_completed_t c)
Definition: rx-observer.hpp:212
observer(Observer o)
Definition: rx-observer.hpp:425
void on_completed() const
Definition: rx-observer.hpp:447
observer(this_type &&o)
Definition: rx-observer.hpp:419
void on_completed() const
Definition: rx-observer.hpp:250
observer< T > as_dynamic() const
Definition: rx-observer.hpp:253
typename std::decay< T >::type decay_t
Definition: rx-util.hpp:36
Definition: rx-predef.hpp:100
this_type & operator=(this_type o)
Definition: rx-observer.hpp:430
void on_next(T &&t) const
Definition: rx-observer.hpp:244
observer(state_t s, on_next_t n=on_next_t(), on_error_t e=on_error_t(), on_completed_t c=on_completed_t())
Definition: rx-observer.hpp:205
this_type & operator=(this_type o)
Definition: rx-observer.hpp:233
observer(const this_type &o)
Definition: rx-observer.hpp:219
typename std::conditional< !std::is_same< void, OnCompleted >::value, rxu::decay_t< OnCompleted >, detail::OnCompletedForward< State, OnCompleted >>::type on_completed_t
Definition: rx-observer.hpp:195
tag_dynamic_observer dynamic_observer_tag
Definition: rx-observer.hpp:396
void on_next(T &t) const
Definition: rx-observer.hpp:241
tag_observer observer_tag
Definition: rx-observer.hpp:17
void on_error(std::exception_ptr e) const
Definition: rx-observer.hpp:442
auto make_observer() -> observer< T, detail::stateless_observer_tag, detail::OnNextEmpty< T >, DefaultOnError >
Definition: rx-observer.hpp:459
void on_next(V &&v) const
Definition: rx-observer.hpp:437
Definition: rx-observer.hpp:14
consumes values from an observable using State that may implement on_next, on_error and on_completed ...
Definition: rx-observer.hpp:179
void on_error(std::exception_ptr e) const
Definition: rx-observer.hpp:247
observer(this_type &&o)
Definition: rx-observer.hpp:226
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
T value_type
Definition: rx-observer.hpp:16
consumes values from an observable using type-forgetting (shared allocated state with virtual methods...
Definition: rx-observer.hpp:393
auto as_dynamic() -> detail::dynamic_factory
Definition: rx-subscribe.hpp:117
Definition: rx-predef.hpp:90
typename std::conditional< !std::is_same< void, OnNext >::value, rxu::decay_t< OnNext >, detail::OnNextForward< T, State, OnNext >>::type on_next_t
Definition: rx-observer.hpp:187
observer(const this_type &o)
Definition: rx-observer.hpp:415
rxu::decay_t< State > state_t
Definition: rx-observer.hpp:183
typename std::conditional< !std::is_same< void, OnError >::value, rxu::decay_t< OnError >, detail::OnErrorForward< State, OnError >>::type on_error_t
Definition: rx-observer.hpp:191