• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2 
3 #pragma once
4 
5 /*! \file rx-connect_forever.hpp
6 
7     \brief  takes a connectable_observable source and calls connect during the construction of the expression.
8             This means that the source starts running without any subscribers and continues running after all subscriptions have been unsubscribed.
9 
10     \return An observable that emitting the items from its source.
11  */
12 
13 #if !defined(RXCPP_OPERATORS_RX_CONNECT_FOREVER_HPP)
14 #define RXCPP_OPERATORS_RX_CONNECT_FOREVER_HPP
15 
16 #include "../rx-includes.hpp"
17 
18 namespace rxcpp {
19 
20 namespace operators {
21 
22 namespace detail {
23 
24 template<class... AN>
25 struct connect_forever_invalid_arguments {};
26 
27 template<class... AN>
28 struct connect_forever_invalid : public rxo::operator_base<connect_forever_invalid_arguments<AN...>> {
29     using type = observable<connect_forever_invalid_arguments<AN...>, connect_forever_invalid<AN...>>;
30 };
31 template<class... AN>
32 using connect_forever_invalid_t = typename connect_forever_invalid<AN...>::type;
33 
34 template<class T, class ConnectableObservable>
35 struct connect_forever : public operator_base<T>
36 {
37     typedef rxu::decay_t<ConnectableObservable> source_type;
38 
39     source_type source;
40 
connect_foreverrxcpp::operators::detail::connect_forever41     explicit connect_forever(source_type o)
42         : source(std::move(o))
43     {
44         source.connect();
45     }
46 
47     template<class Subscriber>
on_subscriberxcpp::operators::detail::connect_forever48     void on_subscribe(Subscriber&& o) const {
49         source.subscribe(std::forward<Subscriber>(o));
50     }
51 };
52 
53 }
54 
55 /*! @copydoc rx-connect_forever.hpp
56 */
57 template<class... AN>
connect_forever(AN &&...an)58 auto connect_forever(AN&&... an)
59 ->     operator_factory<connect_forever_tag, AN...> {
60     return operator_factory<connect_forever_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
61 }
62 
63 }
64 
65 template<>
66 struct member_overload<connect_forever_tag>
67 {
68     template<class ConnectableObservable,
69         class Enabled = rxu::enable_if_all_true_type_t<
70             is_connectable_observable<ConnectableObservable>>,
71         class SourceValue = rxu::value_type_t<ConnectableObservable>,
72         class ConnectForever = rxo::detail::connect_forever<SourceValue, rxu::decay_t<ConnectableObservable>>,
73         class Value = rxu::value_type_t<ConnectForever>,
74         class Result = observable<Value, ConnectForever>
75         >
memberrxcpp::member_overload76     static Result member(ConnectableObservable&& o) {
77         return Result(ConnectForever(std::forward<ConnectableObservable>(o)));
78     }
79 
80     template<class... AN>
memberrxcpp::member_overload81     static operators::detail::connect_forever_invalid_t<AN...> member(AN...) {
82         std::terminate();
83         return {};
84         static_assert(sizeof...(AN) == 10000, "connect_forever takes no arguments");
85     }
86 };
87 
88 }
89 
90 #endif
91