// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. #pragma once /*! \file rx-publish.hpp \brief Turn a cold observable hot and allow connections to the source to be independent of subscriptions. Turn a cold observable hot, send the most recent value to any new subscriber, and allow connections to the source to be independent of subscriptions. \tparam T the type of the emitted item (optional). \param first an initial item to be emitted by the resulting observable at connection time before emitting the items from the source observable; not emitted to observers that subscribe after the time of connection (optional). \param cs the subscription to control lifetime (optional). \return rxcpp::connectable_observable that upon connection causes the source observable to emit items to its observers. \sample \snippet publish.cpp publish subject sample \snippet output.txt publish subject sample \sample \snippet publish.cpp publish behavior sample \snippet output.txt publish behavior sample \sample \snippet publish.cpp publish diamond samethread sample \snippet output.txt publish diamond samethread sample \sample \snippet publish.cpp publish diamond bgthread sample \snippet output.txt publish diamond bgthread sample \sample \snippet ref_count.cpp ref_count other diamond sample \snippet output.txt ref_count other diamond sample */ #if !defined(RXCPP_OPERATORS_RX_PUBLISH_HPP) #define RXCPP_OPERATORS_RX_PUBLISH_HPP #include "../rx-includes.hpp" #include "./rx-multicast.hpp" namespace rxcpp { namespace operators { namespace detail { template struct publish_invalid_arguments {}; template struct publish_invalid : public rxo::operator_base> { using type = observable, publish_invalid>; }; template using publish_invalid_t = typename publish_invalid::type; } /*! @copydoc rx-publish.hpp */ template auto publish(AN&&... an) -> operator_factory { return operator_factory(std::make_tuple(std::forward(an)...)); } /*! \brief Turn a cold observable hot and allow connections to the source to be independent of subscriptions. \tparam Coordination the type of the scheduler. \param cn a scheduler all values are queued and delivered on. \param cs the subscription to control lifetime (optional). \return rxcpp::connectable_observable that upon connection causes the source observable to emit items to its observers, on the specified scheduler. \sample \snippet publish.cpp publish_synchronized sample \snippet output.txt publish_synchronized sample */ template auto publish_synchronized(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 Subject = rxsub::subject, class Multicast = rxo::detail::multicast, Subject>, class Result = connectable_observable > static Result member(Observable&& o) { return Result(Multicast(std::forward(o), Subject(composite_subscription()))); } template>, class SourceValue = rxu::value_type_t, class Subject = rxsub::subject, class Multicast = rxo::detail::multicast, Subject>, class Result = connectable_observable > static Result member(Observable&& o, composite_subscription cs) { return Result(Multicast(std::forward(o), Subject(cs))); } template>, class SourceValue = rxu::value_type_t, class Subject = rxsub::behavior, class Multicast = rxo::detail::multicast, Subject>, class Result = connectable_observable > static Result member(Observable&& o, T first, composite_subscription cs = composite_subscription()) { return Result(Multicast(std::forward(o), Subject(first, cs))); } template static operators::detail::publish_invalid_t member(AN...) { std::terminate(); return {}; static_assert(sizeof...(AN) == 10000, "publish takes (optional CompositeSubscription) or (T, optional CompositeSubscription)"); } }; template<> struct member_overload { template, is_coordination>, class SourceValue = rxu::value_type_t, class Subject = rxsub::synchronize>, class Multicast = rxo::detail::multicast, Subject>, class Result = connectable_observable > static Result member(Observable&& o, Coordination&& cn, composite_subscription cs = composite_subscription()) { return Result(Multicast(std::forward(o), Subject(std::forward(cn), cs))); } template static operators::detail::publish_invalid_t member(AN...) { std::terminate(); return {}; static_assert(sizeof...(AN) == 10000, "publish_synchronized takes (Coordination, optional CompositeSubscription)"); } }; } #endif