// 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_CONNECTABLE_OBSERVABLE_HPP) #define RXCPP_RX_CONNECTABLE_OBSERVABLE_HPP #include "rx-includes.hpp" namespace rxcpp { namespace detail { template struct has_on_connect { struct not_void {}; template static auto check(int) -> decltype((*(CT*)nullptr).on_connect(composite_subscription())); template static not_void check(...); typedef decltype(check(0)) detail_result; static const bool value = std::is_same::value; }; } template class dynamic_connectable_observable : public dynamic_observable { struct state_type : public std::enable_shared_from_this { typedef std::function onconnect_type; onconnect_type on_connect; }; std::shared_ptr state; template void construct(const dynamic_observable& o, tag_dynamic_observable&&) { state = o.state; } template void construct(dynamic_observable&& o, tag_dynamic_observable&&) { state = std::move(o.state); } template void construct(SO&& source, rxs::tag_source&&) { auto so = std::make_shared>(std::forward(source)); state->on_connect = [so](composite_subscription cs) mutable { so->on_connect(std::move(cs)); }; } public: typedef tag_dynamic_observable dynamic_observable_tag; dynamic_connectable_observable() { } template explicit dynamic_connectable_observable(SOF sof) : dynamic_observable(sof) , state(std::make_shared()) { construct(std::move(sof), typename std::conditional::value, tag_dynamic_observable, rxs::tag_source>::type()); } template dynamic_connectable_observable(SF&& sf, CF&& cf) : dynamic_observable(std::forward(sf)) , state(std::make_shared()) { state->on_connect = std::forward(cf); } using dynamic_observable::on_subscribe; void on_connect(composite_subscription cs) const { state->on_connect(std::move(cs)); } }; template connectable_observable make_dynamic_connectable_observable(Source&& s) { return connectable_observable(dynamic_connectable_observable(std::forward(s))); } /*! \brief a source of values that is shared across all subscribers and does not start until connectable_observable::connect() is called. \ingroup group-observable */ template class connectable_observable : public observable { typedef connectable_observable this_type; typedef observable base_type; typedef rxu::decay_t source_operator_type; static_assert(detail::has_on_connect::value, "inner must have on_connect method void(composite_subscription)"); public: typedef tag_connectable_observable observable_tag; connectable_observable() { } explicit connectable_observable(const SourceOperator& o) : base_type(o) { } explicit connectable_observable(SourceOperator&& o) : base_type(std::move(o)) { } // implicit conversion between observables of the same value_type template connectable_observable(const connectable_observable& o) : base_type(o) {} // implicit conversion between observables of the same value_type template connectable_observable(connectable_observable&& o) : base_type(std::move(o)) {} /// /// takes any function that will take this observable and produce a result value. /// this is intended to allow externally defined operators, that use subscribe, /// to be connected into the expression. /// template auto op(OperatorFactory&& of) const -> decltype(of(*(const this_type*)nullptr)) { return of(*this); static_assert(is_operator_factory_for::value, "Function passed for op() must have the signature Result(SourceObservable)"); } /// /// performs type-forgetting conversion to a new composite_observable /// connectable_observable as_dynamic() { return *this; } composite_subscription connect(composite_subscription cs = composite_subscription()) { base_type::source_operator.on_connect(cs); return cs; } /*! @copydoc rx-ref_count.hpp */ template auto ref_count(AN... an) const /// \cond SHOW_SERVICE_MEMBERS -> decltype(observable_member(ref_count_tag{}, *(this_type*)nullptr, std::forward(an)...)) /// \endcond { return observable_member(ref_count_tag{}, *this, std::forward(an)...); } /*! @copydoc rx-connect_forever.hpp */ template auto connect_forever(AN... an) const /// \cond SHOW_SERVICE_MEMBERS -> decltype(observable_member(connect_forever_tag{}, *(this_type*)nullptr, std::forward(an)...)) /// \endcond { return observable_member(connect_forever_tag{}, *this, std::forward(an)...); } }; } // // support range() >> filter() >> subscribe() syntax // '>>' is spelled 'stream' // template auto operator >> (const rxcpp::connectable_observable& source, OperatorFactory&& of) -> decltype(source.op(std::forward(of))) { return source.op(std::forward(of)); } // // support range() | filter() | subscribe() syntax // '|' is spelled 'pipe' // template auto operator | (const rxcpp::connectable_observable& source, OperatorFactory&& of) -> decltype(source.op(std::forward(of))) { return source.op(std::forward(of)); } #endif