// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. #pragma once /*! \file rx-ref_count.hpp \brief Make some \c connectable_observable behave like an ordinary \c observable. Uses a reference count of the subscribers to control the connection to the published observable. The first subscription will cause a call to \c connect(), and the last \c unsubscribe will unsubscribe the connection. There are 2 variants of the operator: \li \c ref_count(): calls \c connect on the \c source \c connectable_observable. \li \c ref_count(other): calls \c connect on the \c other \c connectable_observable. \tparam ConnectableObservable the type of the \c other \c connectable_observable (optional) \param other \c connectable_observable to call \c connect on (optional) If \c other is omitted, then \c source is used instead (which must be a \c connectable_observable). Otherwise, \c source can be a regular \c observable. \return An \c observable that emits the items from its \c source. \sample \snippet ref_count.cpp ref_count other diamond sample \snippet output.txt ref_count other diamond sample */ #if !defined(RXCPP_OPERATORS_RX_REF_COUNT_HPP) #define RXCPP_OPERATORS_RX_REF_COUNT_HPP #include "../rx-includes.hpp" namespace rxcpp { namespace operators { namespace detail { template struct ref_count_invalid_arguments {}; template struct ref_count_invalid : public rxo::operator_base> { using type = observable, ref_count_invalid>; }; template using ref_count_invalid_t = typename ref_count_invalid::type; // ref_count(other) takes a regular observable source, not a connectable_observable. // use template specialization to avoid instantiating 'subscribe' for two different types // which would cause a compilation error. template struct ref_count_state_base { ref_count_state_base(connectable_type other, observable_type source) : connectable(std::move(other)) , subscribable(std::move(source)) {} connectable_type connectable; // connects to this. subscribes to this if subscribable empty. observable_type subscribable; // subscribes to this if non-empty. template void subscribe(Subscriber&& o) { subscribable.subscribe(std::forward(o)); } }; // Note: explicit specializations have to be at namespace scope prior to C++17. template struct ref_count_state_base { explicit ref_count_state_base(connectable_type c) : connectable(std::move(c)) {} connectable_type connectable; // connects to this. subscribes to this if subscribable empty. template void subscribe(Subscriber&& o) { connectable.subscribe(std::forward(o)); } }; template // note: type order flipped versus the operator. struct ref_count : public operator_base { typedef rxu::decay_t observable_type; typedef rxu::decay_t connectable_type; // ref_count() == false // ref_count(other) == true using has_observable_t = rxu::negation>; static constexpr bool has_observable_v = has_observable_t::value; struct ref_count_state : public std::enable_shared_from_this, public ref_count_state_base { template >> explicit ref_count_state(connectable_type source) : ref_count_state_base(std::move(source)) , subscribers(0) { } template ref_count_state(connectable_type other, typename std::enable_if::type source) : ref_count_state_base(std::move(other), std::move(source)) , subscribers(0) { } std::mutex lock; long subscribers; composite_subscription connection; }; std::shared_ptr state; // connectable_observable source = ...; // source.ref_count(); // // calls connect on source after the subscribe on source. template >> explicit ref_count(connectable_type source) : state(std::make_shared(std::move(source))) { } // connectable_observable other = ...; // observable source = ...; // source.ref_count(other); // // calls connect on 'other' after the subscribe on 'source'. template ref_count(connectable_type other, typename std::enable_if::type source) : state(std::make_shared(std::move(other), std::move(source))) { } template void on_subscribe(Subscriber&& o) const { std::unique_lock guard(state->lock); auto needConnect = ++state->subscribers == 1; auto keepAlive = state; guard.unlock(); o.add( [keepAlive](){ std::unique_lock guard_unsubscribe(keepAlive->lock); if (--keepAlive->subscribers == 0) { keepAlive->connection.unsubscribe(); keepAlive->connection = composite_subscription(); } }); keepAlive->subscribe(std::forward(o)); if (needConnect) { keepAlive->connectable.connect(keepAlive->connection); } } }; } /*! @copydoc rx-ref_count.hpp */ template auto ref_count(AN&&... an) -> operator_factory { return operator_factory(std::make_tuple(std::forward(an)...)); } } template<> struct member_overload { template>, class SourceValue = rxu::value_type_t, class RefCount = rxo::detail::ref_count>, class Value = rxu::value_type_t, class Result = observable > static Result member(ConnectableObservable&& o) { return Result(RefCount(std::forward(o))); } template, is_connectable_observable>, class SourceValue = rxu::value_type_t, class RefCount = rxo::detail::ref_count, rxu::decay_t>, class Value = rxu::value_type_t, class Result = observable > static Result member(Observable&& o, ConnectableObservable&& other) { return Result(RefCount(std::forward(other), std::forward(o))); } template static operators::detail::ref_count_invalid_t member(AN...) { std::terminate(); return {}; static_assert(sizeof...(AN) == 10000, "ref_count takes (optional ConnectableObservable)"); } }; } #endif