• 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-switch_if_empty.hpp
6 
7     \brief If the source Observable terminates without emitting any items, emits items from a backup Observable.
8 
9     \tparam BackupSource the type of the backup observable.
10 
11     \param t a backup observable that is used if the source observable is empty.
12 
13     \return Observable that emits items from a backup observable if the source observable is empty.
14 
15     \sample
16     \snippet switch_if_empty.cpp switch_if_empty sample
17     \snippet output.txt switch_if_empty sample
18 */
19 
20 #if !defined(RXCPP_OPERATORS_RX_SWITCH_IF_EMPTY_HPP)
21 #define RXCPP_OPERATORS_RX_SWITCH_IF_EMPTY_HPP
22 
23 #include "../rx-includes.hpp"
24 
25 namespace rxcpp {
26 
27 namespace operators {
28 
29 namespace detail {
30 
31 template<class... AN>
32 struct switch_if_empty_invalid_arguments {};
33 
34 template<class... AN>
35 struct switch_if_empty_invalid : public rxo::operator_base<switch_if_empty_invalid_arguments<AN...>> {
36     using type = observable<switch_if_empty_invalid_arguments<AN...>, switch_if_empty_invalid<AN...>>;
37 };
38 template<class... AN>
39 using switch_if_empty_invalid_t = typename switch_if_empty_invalid<AN...>::type;
40 
41 template<class T, class BackupSource>
42 struct switch_if_empty
43 {
44     typedef rxu::decay_t<T> source_value_type;
45     typedef rxu::decay_t<BackupSource> backup_source_type;
46 
47     backup_source_type backup;
48 
switch_if_emptyrxcpp::operators::detail::switch_if_empty49     switch_if_empty(backup_source_type b)
50         : backup(std::move(b))
51     {
52     }
53 
54     template<class Subscriber>
55     struct switch_if_empty_observer
56     {
57         typedef switch_if_empty_observer<Subscriber> this_type;
58         typedef source_value_type value_type;
59         typedef rxu::decay_t<Subscriber> dest_type;
60         typedef observer<value_type, this_type> observer_type;
61 
62         dest_type dest;
63         composite_subscription lifetime;
64         backup_source_type backup;
65         mutable bool is_empty;
66 
switch_if_empty_observerrxcpp::operators::detail::switch_if_empty::switch_if_empty_observer67         switch_if_empty_observer(dest_type d, composite_subscription cs, backup_source_type b)
68             : dest(std::move(d))
69             , lifetime(std::move(cs))
70             , backup(std::move(b))
71             , is_empty(true)
72         {
73             dest.add(lifetime);
74         }
on_nextrxcpp::operators::detail::switch_if_empty::switch_if_empty_observer75         void on_next(source_value_type v) const {
76             is_empty = false;
77             dest.on_next(std::move(v));
78         }
on_errorrxcpp::operators::detail::switch_if_empty::switch_if_empty_observer79         void on_error(rxu::error_ptr e) const {
80             dest.on_error(std::move(e));
81         }
on_completedrxcpp::operators::detail::switch_if_empty::switch_if_empty_observer82         void on_completed() const {
83             if(!is_empty) {
84                 dest.on_completed();
85             } else {
86                 backup.subscribe(dest);
87             }
88         }
89 
makerxcpp::operators::detail::switch_if_empty::switch_if_empty_observer90         static subscriber<value_type, observer_type> make(dest_type d, backup_source_type b) {
91             auto cs = composite_subscription();
92             return make_subscriber<value_type>(cs, observer_type(this_type(std::move(d), cs, std::move(b))));
93         }
94     };
95 
96     template<class Subscriber>
operator ()rxcpp::operators::detail::switch_if_empty97     auto operator()(Subscriber dest) const
98         -> decltype(switch_if_empty_observer<Subscriber>::make(std::move(dest), std::move(backup))) {
99         return      switch_if_empty_observer<Subscriber>::make(std::move(dest), std::move(backup));
100     }
101 };
102 
103 }
104 
105 /*! @copydoc rx-switch_if_empty.hpp
106 */
107 template<class... AN>
switch_if_empty(AN &&...an)108 auto switch_if_empty(AN&&... an)
109     ->      operator_factory<switch_if_empty_tag, AN...> {
110      return operator_factory<switch_if_empty_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
111 }
112 
113 /*! \brief If the source Observable terminates without emitting any items, emits a default item and completes.
114 
115     \tparam Value the type of the value to emit.
116 
117     \param v the default value to emit.
118 
119     \return Observable that emits the specified default item if the source observable is empty.
120 
121     \sample
122     \snippet default_if_empty.cpp default_if_empty sample
123     \snippet output.txt default_if_empty sample
124 */
125 template<class... AN>
default_if_empty(AN &&...an)126 auto default_if_empty(AN&&... an)
127     ->      operator_factory<default_if_empty_tag, AN...> {
128      return operator_factory<default_if_empty_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
129 }
130 
131 }
132 
133 template<>
134 struct member_overload<switch_if_empty_tag>
135 {
136     template<class Observable, class BackupSource,
137         class Enabled = rxu::enable_if_all_true_type_t<
138             all_observables<Observable, BackupSource>>,
139         class SourceValue = rxu::value_type_t<Observable>,
140         class SwitchIfEmpty = rxo::detail::switch_if_empty<SourceValue, rxu::decay_t<BackupSource>>>
memberrxcpp::member_overload141     static auto member(Observable&& o, BackupSource&& b)
142         -> decltype(o.template lift<SourceValue>(SwitchIfEmpty(std::forward<BackupSource>(b)))) {
143         return      o.template lift<SourceValue>(SwitchIfEmpty(std::forward<BackupSource>(b)));
144     }
145 
146     template<class... AN>
memberrxcpp::member_overload147     static operators::detail::switch_if_empty_invalid_t<AN...> member(AN...) {
148         std::terminate();
149         return {};
150         static_assert(sizeof...(AN) == 10000, "switch_if_empty takes (BackupSource)");
151     }
152 };
153 
154 template<>
155 struct member_overload<default_if_empty_tag>
156 {
157     template<class Observable, class Value,
158         class Enabled = rxu::enable_if_all_true_type_t<
159             is_observable<Observable>>,
160         class SourceValue = rxu::value_type_t<Observable>,
161         class BackupSource = decltype(rxs::from(std::declval<SourceValue>())),
162         class DefaultIfEmpty = rxo::detail::switch_if_empty<SourceValue, BackupSource>>
memberrxcpp::member_overload163     static auto member(Observable&& o, Value v)
164         -> decltype(o.template lift<SourceValue>(DefaultIfEmpty(rxs::from(std::move(v))))) {
165         return      o.template lift<SourceValue>(DefaultIfEmpty(rxs::from(std::move(v))));
166     }
167 
168     template<class... AN>
memberrxcpp::member_overload169     static operators::detail::switch_if_empty_invalid_t<AN...> member(AN...) {
170         std::terminate();
171         return {};
172         static_assert(sizeof...(AN) == 10000, "default_if_empty takes (Value)");
173     }
174 };
175 
176 }
177 
178 #endif
179