// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. #pragma once #if !defined(RXCPP_RX_SUBSCRIBER_HPP) #define RXCPP_RX_SUBSCRIBER_HPP #include "rx-includes.hpp" namespace rxcpp { template struct subscriber_base : public observer_base, public subscription_base { typedef tag_subscriber subscriber_tag; }; /*! \brief binds an observer that consumes values with a composite_subscription that controls lifetime. \ingroup group-core */ template> class subscriber : public subscriber_base { static_assert(!is_subscriber::value, "not allowed to nest subscribers"); static_assert(is_observer::value, "subscriber must contain an observer"); typedef subscriber this_type; typedef rxu::decay_t observer_type; composite_subscription lifetime; observer_type destination; trace_id id; struct nextdetacher { ~nextdetacher() { trace_activity().on_next_return(*that); if (do_unsubscribe) { that->unsubscribe(); } } nextdetacher(const this_type* that) : that(that) , do_unsubscribe(true) { } template void operator()(U u) { trace_activity().on_next_enter(*that, u); RXCPP_TRY { that->destination.on_next(std::move(u)); do_unsubscribe = false; } RXCPP_CATCH(...) { auto ex = rxu::current_exception(); trace_activity().on_error_enter(*that, ex); that->destination.on_error(std::move(ex)); trace_activity().on_error_return(*that); } } const this_type* that; volatile bool do_unsubscribe; }; struct errordetacher { ~errordetacher() { trace_activity().on_error_return(*that); that->unsubscribe(); } errordetacher(const this_type* that) : that(that) { } inline void operator()(rxu::error_ptr ex) { trace_activity().on_error_enter(*that, ex); that->destination.on_error(std::move(ex)); } const this_type* that; }; struct completeddetacher { ~completeddetacher() { trace_activity().on_completed_return(*that); that->unsubscribe(); } completeddetacher(const this_type* that) : that(that) { } inline void operator()() { trace_activity().on_completed_enter(*that); that->destination.on_completed(); } const this_type* that; }; subscriber(); public: typedef typename composite_subscription::weak_subscription weak_subscription; subscriber(const this_type& o) : lifetime(o.lifetime) , destination(o.destination) , id(o.id) { } subscriber(this_type&& o) : lifetime(std::move(o.lifetime)) , destination(std::move(o.destination)) , id(std::move(o.id)) { } template friend class subscriber; template subscriber( const subscriber& o, typename std::enable_if< !std::is_same>::value && std::is_same>::value, void**>::type = nullptr) : lifetime(o.lifetime) , destination(o.destination.as_dynamic()) , id(o.id) { } template subscriber(trace_id id, composite_subscription cs, U&& o) : lifetime(std::move(cs)) , destination(std::forward(o)) , id(std::move(id)) { static_assert(!is_subscriber::value, "cannot nest subscribers"); static_assert(is_observer::value, "must pass observer to subscriber"); trace_activity().create_subscriber(*this); } this_type& operator=(this_type o) { lifetime = std::move(o.lifetime); destination = std::move(o.destination); id = std::move(o.id); return *this; } const observer_type& get_observer() const { return destination; } observer_type& get_observer() { return destination; } const composite_subscription& get_subscription() const { return lifetime; } composite_subscription& get_subscription() { return lifetime; } trace_id get_id() const { return id; } subscriber as_dynamic() const { return subscriber(id, lifetime, destination.as_dynamic()); } // observer // template void on_next(V&& v) const { if (!is_subscribed()) { return; } nextdetacher protect(this); protect(std::forward(v)); } void on_error(rxu::error_ptr e) const { if (!is_subscribed()) { return; } errordetacher protect(this); protect(std::move(e)); } void on_completed() const { if (!is_subscribed()) { return; } completeddetacher protect(this); protect(); } // composite_subscription // bool is_subscribed() const { return lifetime.is_subscribed(); } weak_subscription add(subscription s) const { return lifetime.add(std::move(s)); } template auto add(F f) const -> typename std::enable_if::value, weak_subscription>::type { return lifetime.add(make_subscription(std::move(f))); } void remove(weak_subscription w) const { return lifetime.remove(std::move(w)); } void clear() const { return lifetime.clear(); } void unsubscribe() const { return lifetime.unsubscribe(); } }; template auto make_subscriber( subscriber o) -> subscriber { return subscriber(std::move(o)); } // observer // template auto make_subscriber() -> typename std::enable_if< detail::is_on_next_of>::value, subscriber>>>::type { return subscriber>>(trace_id::make_next_id_subscriber(), composite_subscription(), observer>(detail::OnNextEmpty())); } template auto make_subscriber( const observer& o) -> subscriber> { return subscriber>(trace_id::make_next_id_subscriber(), composite_subscription(), o); } template auto make_subscriber(const Observer& o) -> typename std::enable_if< is_observer::value && !is_subscriber::value, subscriber>::type { return subscriber(trace_id::make_next_id_subscriber(), composite_subscription(), o); } template auto make_subscriber(const Observer& o) -> typename std::enable_if< !detail::is_on_next_of::value && !is_subscriber::value && !is_subscription::value && !is_observer::value, subscriber>>::type { return subscriber>(trace_id::make_next_id_subscriber(), composite_subscription(), o); } template auto make_subscriber(const OnNext& on) -> typename std::enable_if< detail::is_on_next_of::value, subscriber>>::type { return subscriber>(trace_id::make_next_id_subscriber(), composite_subscription(), observer(on)); } template auto make_subscriber(const OnNext& on, const OnError& oe) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value, subscriber>>::type { return subscriber>(trace_id::make_next_id_subscriber(), composite_subscription(), observer(on, oe)); } template auto make_subscriber(const OnNext& on, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_completed::value, subscriber>>::type { return subscriber>(trace_id::make_next_id_subscriber(), composite_subscription(), observer(on, detail::OnErrorEmpty(), oc)); } template auto make_subscriber(const OnNext& on, const OnError& oe, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value && detail::is_on_completed::value, subscriber>>::type { return subscriber>(trace_id::make_next_id_subscriber(), composite_subscription(), observer(on, oe, oc)); } // explicit lifetime // template auto make_subscriber(const composite_subscription& cs) -> subscriber>> { return subscriber>>(trace_id::make_next_id_subscriber(), cs, observer>(detail::OnNextEmpty())); } template auto make_subscriber(const composite_subscription& cs, const observer& o) -> subscriber> { return subscriber>(trace_id::make_next_id_subscriber(), cs, o); } template auto make_subscriber(const composite_subscription& cs, const subscriber& s) -> subscriber { return subscriber(trace_id::make_next_id_subscriber(), cs, s.get_observer()); } template auto make_subscriber(const composite_subscription& cs, const Observer& o) -> typename std::enable_if< !is_subscriber::value && is_observer::value, subscriber>::type { return subscriber(trace_id::make_next_id_subscriber(), cs, o); } template auto make_subscriber(const composite_subscription& cs, const Observer& o) -> typename std::enable_if< !detail::is_on_next_of::value && !is_subscriber::value && !is_subscription::value && !is_observer::value, subscriber>>::type { return subscriber>(trace_id::make_next_id_subscriber(), cs, make_observer(o)); } template auto make_subscriber(const composite_subscription& cs, const OnNext& on) -> typename std::enable_if< detail::is_on_next_of::value, subscriber>>::type { return subscriber>(trace_id::make_next_id_subscriber(), cs, observer(on)); } template auto make_subscriber(const composite_subscription& cs, const OnNext& on, const OnError& oe) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value, subscriber>>::type { return subscriber>(trace_id::make_next_id_subscriber(), cs, observer(on, oe)); } template auto make_subscriber(const composite_subscription& cs, const OnNext& on, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_completed::value, subscriber>>::type { return subscriber>(trace_id::make_next_id_subscriber(), cs, observer(on, detail::OnErrorEmpty(), oc)); } template auto make_subscriber(const composite_subscription& cs, const OnNext& on, const OnError& oe, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value && detail::is_on_completed::value, subscriber>>::type { return subscriber>(trace_id::make_next_id_subscriber(), cs, observer(on, oe, oc)); } // explicit id // template auto make_subscriber(trace_id id) -> subscriber>> { return subscriber>>(std::move(id), composite_subscription(), observer>(detail::OnNextEmpty())); } template auto make_subscriber(trace_id id, const composite_subscription& cs) -> subscriber>> { return subscriber>>(std::move(id), cs, observer>(detail::OnNextEmpty())); } template auto make_subscriber(trace_id id, const observer& o) -> subscriber> { return subscriber>(std::move(id), composite_subscription(), o); } template auto make_subscriber(trace_id id, const composite_subscription& cs, const observer& o) -> subscriber> { return subscriber>(std::move(id), cs, o); } template auto make_subscriber(trace_id id, const Observer& o) -> typename std::enable_if< is_observer::value, subscriber>::type { return subscriber(std::move(id), composite_subscription(), o); } template auto make_subscriber(trace_id id, const composite_subscription& cs, const Observer& o) -> typename std::enable_if< is_observer::value, subscriber>::type { return subscriber(std::move(id), cs, o); } template auto make_subscriber(trace_id id, const Observer& o) -> typename std::enable_if< !detail::is_on_next_of::value && !is_subscriber::value && !is_subscription::value && !is_observer::value, subscriber>>::type { return subscriber>(std::move(id), composite_subscription(), o); } template auto make_subscriber(trace_id id, const composite_subscription& cs, const Observer& o) -> typename std::enable_if< !detail::is_on_next_of::value && !is_subscriber::value && !is_subscription::value && !is_observer::value, subscriber>>::type { return subscriber>(std::move(id), cs, o); } template auto make_subscriber(trace_id id, const OnNext& on) -> typename std::enable_if< detail::is_on_next_of::value, subscriber>>::type { return subscriber>(std::move(id), composite_subscription(), observer(on)); } template auto make_subscriber(trace_id id, const composite_subscription& cs, const OnNext& on) -> typename std::enable_if< detail::is_on_next_of::value, subscriber>>::type { return subscriber>(std::move(id), cs, observer(on)); } template auto make_subscriber(trace_id id, const OnNext& on, const OnError& oe) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value, subscriber>>::type { return subscriber>(std::move(id), composite_subscription(), observer(on, oe)); } template auto make_subscriber(trace_id id, const composite_subscription& cs, const OnNext& on, const OnError& oe) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value, subscriber>>::type { return subscriber>(std::move(id), cs, observer(on, oe)); } template auto make_subscriber(trace_id id, const OnNext& on, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_completed::value, subscriber>>::type { return subscriber>(std::move(id), composite_subscription(), observer(on, detail::OnErrorEmpty(), oc)); } template auto make_subscriber(trace_id id, const composite_subscription& cs, const OnNext& on, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_completed::value, subscriber>>::type { return subscriber>(std::move(id), cs, observer(on, detail::OnErrorEmpty(), oc)); } template auto make_subscriber(trace_id id, const OnNext& on, const OnError& oe, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value && detail::is_on_completed::value, subscriber>>::type { return subscriber>(std::move(id), composite_subscription(), observer(on, oe, oc)); } template auto make_subscriber(trace_id id, const composite_subscription& cs, const OnNext& on, const OnError& oe, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value && detail::is_on_completed::value, subscriber>>::type { return subscriber>(std::move(id), cs, observer(on, oe, oc)); } // chain defaults from subscriber // template auto make_subscriber(const subscriber& scbr, const observer& o) -> subscriber> { auto r = subscriber>(trace_id::make_next_id_subscriber(), scbr.get_subscription(), o); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const observer& o) -> subscriber> { auto r = subscriber>(std::move(id), scbr.get_subscription(), o); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const Observer& o) -> typename std::enable_if< is_observer::value, subscriber>::type { auto r = subscriber(std::move(id), scbr.get_subscription(), o); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, const Observer& o) -> typename std::enable_if< !is_subscription::value && is_observer::value, subscriber>::type { auto r = subscriber(trace_id::make_next_id_subscriber(), scbr.get_subscription(), o); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, const Observer& o) -> typename std::enable_if< !detail::is_on_next_of::value && !is_subscriber::value && !is_subscription::value && !is_observer::value, subscriber>>::type { auto r = subscriber>(trace_id::make_next_id_subscriber(), scbr.get_subscription(), make_observer(o)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const Observer& o) -> typename std::enable_if< !detail::is_on_next_of::value && !is_subscriber::value && !is_subscription::value && !is_observer::value, subscriber>>::type { auto r = subscriber>(std::move(id), scbr.get_subscription(), o); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, const OnNext& on) -> typename std::enable_if< detail::is_on_next_of::value, subscriber>>::type { auto r = subscriber>(trace_id::make_next_id_subscriber(), scbr.get_subscription(), observer(on)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const OnNext& on) -> typename std::enable_if< detail::is_on_next_of::value, subscriber>>::type { auto r = subscriber>(std::move(id), scbr.get_subscription(), observer(on)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, const OnNext& on, const OnError& oe) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value, subscriber>>::type { auto r = subscriber>(trace_id::make_next_id_subscriber(), scbr.get_subscription(), observer(on, oe)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const OnNext& on, const OnError& oe) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value, subscriber>>::type { auto r = subscriber>(std::move(id), scbr.get_subscription(), observer(on, oe)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, const OnNext& on, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_completed::value, subscriber>>::type { auto r = subscriber>(trace_id::make_next_id_subscriber(), scbr.get_subscription(), observer(on, detail::OnErrorEmpty(), oc)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const OnNext& on, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_completed::value, subscriber>>::type { auto r = subscriber>(std::move(id), scbr.get_subscription(), observer(on, detail::OnErrorEmpty(), oc)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, const OnNext& on, const OnError& oe, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value && detail::is_on_completed::value, subscriber>>::type { auto r = subscriber>(trace_id::make_next_id_subscriber(), scbr.get_subscription(), observer(on, oe, oc)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const OnNext& on, const OnError& oe, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value && detail::is_on_completed::value, subscriber>>::type { auto r = subscriber>(std::move(id), scbr.get_subscription(), observer(on, oe, oc)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& , const composite_subscription& cs, const observer& o) -> subscriber> { return subscriber>(trace_id::make_next_id_subscriber(), cs, o); } template auto make_subscriber(const subscriber&, trace_id id, const composite_subscription& cs, const observer& o) -> subscriber> { return subscriber>(std::move(id), cs, o); } template auto make_subscriber(const subscriber& scbr, const composite_subscription& cs, const Observer& o) -> typename std::enable_if< is_observer::value, subscriber>::type { auto r = subscriber(trace_id::make_next_id_subscriber(), cs, o); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const composite_subscription& cs, const Observer& o) -> typename std::enable_if< is_observer::value, subscriber>::type { auto r = subscriber(std::move(id), cs, o); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, const composite_subscription& cs, const Observer& o) -> typename std::enable_if< !detail::is_on_next_of::value && !is_subscriber::value && !is_subscription::value && !is_observer::value, subscriber>>::type { auto r = subscriber>(trace_id::make_next_id_subscriber(), cs, o); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const composite_subscription& cs, const Observer& o) -> typename std::enable_if< !detail::is_on_next_of::value && !is_subscriber::value && !is_subscription::value && !is_observer::value, subscriber>>::type { auto r = subscriber>(std::move(id), cs, o); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, const composite_subscription& cs, const OnNext& on) -> typename std::enable_if< detail::is_on_next_of::value, subscriber>>::type { auto r = subscriber>(trace_id::make_next_id_subscriber(), cs, observer(on)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const composite_subscription& cs, const OnNext& on) -> typename std::enable_if< detail::is_on_next_of::value, subscriber>>::type { auto r = subscriber>(std::move(id), cs, observer(on)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, const composite_subscription& cs, const OnNext& on, const OnError& oe) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value, subscriber>>::type { auto r = subscriber>(trace_id::make_next_id_subscriber(), cs, observer(on, oe)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const composite_subscription& cs, const OnNext& on, const OnError& oe) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value, subscriber>>::type { auto r = subscriber>(std::move(id), cs, observer(on, oe)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, const composite_subscription& cs, const OnNext& on, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_completed::value, subscriber>>::type { auto r = subscriber>(trace_id::make_next_id_subscriber(), cs, observer(on, detail::OnErrorEmpty(), oc)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const composite_subscription& cs, const OnNext& on, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_completed::value, subscriber>>::type { auto r = subscriber>(std::move(id), cs, observer(on, detail::OnErrorEmpty(), oc)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, const composite_subscription& cs, const OnNext& on, const OnError& oe, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value && detail::is_on_completed::value, subscriber>>::type { auto r = subscriber>(trace_id::make_next_id_subscriber(), cs, observer(on, oe, oc)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const composite_subscription& cs, const OnNext& on, const OnError& oe, const OnCompleted& oc) -> typename std::enable_if< detail::is_on_next_of::value && detail::is_on_error::value && detail::is_on_completed::value, subscriber>>::type { auto r = subscriber>(std::move(id), cs, observer(on, oe, oc)); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, const composite_subscription& cs) -> subscriber { auto r = subscriber(scbr.get_id(), cs, scbr.get_observer()); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id, const composite_subscription& cs) -> subscriber { auto r = subscriber(std::move(id), cs, scbr.get_observer()); trace_activity().connect(r, scbr); return r; } template auto make_subscriber(const subscriber& scbr, trace_id id) -> subscriber { auto r = subscriber(std::move(id), scbr.get_subscription(), scbr.get_observer()); trace_activity().connect(r, scbr); return r; } } #endif