// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. #pragma once #if !defined(RXCPP_SOURCES_RX_DEFER_HPP) #define RXCPP_SOURCES_RX_DEFER_HPP #include "../rx-includes.hpp" /*! \file rx-defer.hpp \brief Returns an observable that calls the specified observable factory to create an observable for each new observer that subscribes. \tparam ObservableFactory the type of the observable factory \param of the observable factory function to invoke for each observer that subscribes to the resulting observable \return observable whose observers' subscriptions trigger an invocation of the given observable factory function \sample \snippet defer.cpp defer sample \snippet output.txt defer sample */ namespace rxcpp { namespace sources { namespace detail { template struct defer_traits { typedef rxu::decay_t observable_factory_type; typedef decltype((*(observable_factory_type*)nullptr)()) collection_type; typedef typename collection_type::value_type value_type; }; template struct defer : public source_base>> { typedef defer this_type; typedef defer_traits traits; typedef typename traits::observable_factory_type observable_factory_type; typedef typename traits::collection_type collection_type; observable_factory_type observable_factory; defer(observable_factory_type of) : observable_factory(std::move(of)) { } template void on_subscribe(Subscriber o) const { auto selectedCollection = on_exception( [this](){return this->observable_factory();}, o); if (selectedCollection.empty()) { return; } selectedCollection->subscribe(o); } }; } /*! @copydoc rx-defer.hpp */ template auto defer(ObservableFactory of) -> observable>, detail::defer> { return observable>, detail::defer>( detail::defer(std::move(of))); } } } #endif