• 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-on_error_resume_next.hpp
6 
7     \brief If an error occurs, take the result from the Selector and subscribe to that instead.
8 
9     \tparam Selector the actual type of a function of the form `observable<T>(rxu::error_ptr)`
10 
11     \param s the function of the form `observable<T>(rxu::error_ptr)`
12 
13     \return Observable that emits the items from the source observable and switches to a new observable on error.
14 
15     \sample
16     \snippet on_error_resume_next.cpp on_error_resume_next sample
17     \snippet output.txt on_error_resume_next sample
18 */
19 
20 #if !defined(RXCPP_OPERATORS_RX_ON_ERROR_RESUME_NEXT_HPP)
21 #define RXCPP_OPERATORS_RX_ON_ERROR_RESUME_NEXT_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 on_error_resume_next_invalid_arguments {};
33 
34 template<class... AN>
35 struct on_error_resume_next_invalid : public rxo::operator_base<on_error_resume_next_invalid_arguments<AN...>> {
36     using type = observable<on_error_resume_next_invalid_arguments<AN...>, on_error_resume_next_invalid<AN...>>;
37 };
38 template<class... AN>
39 using on_error_resume_next_invalid_t = typename on_error_resume_next_invalid<AN...>::type;
40 
41 
42 template<class T, class Selector>
43 struct on_error_resume_next
44 {
45     typedef rxu::decay_t<T> value_type;
46     typedef rxu::decay_t<Selector> select_type;
47     typedef decltype((*(select_type*)nullptr)(rxu::error_ptr())) fallback_type;
48     select_type selector;
49 
on_error_resume_nextrxcpp::operators::detail::on_error_resume_next50     on_error_resume_next(select_type s)
51         : selector(std::move(s))
52     {
53     }
54 
55     template<class Subscriber>
56     struct on_error_resume_next_observer
57     {
58         typedef on_error_resume_next_observer<Subscriber> this_type;
59         typedef rxu::decay_t<T> value_type;
60         typedef rxu::decay_t<Selector> select_type;
61         typedef decltype((*(select_type*)nullptr)(rxu::error_ptr())) fallback_type;
62         typedef rxu::decay_t<Subscriber> dest_type;
63         typedef observer<T, this_type> observer_type;
64         dest_type dest;
65         composite_subscription lifetime;
66         select_type selector;
67 
on_error_resume_next_observerrxcpp::operators::detail::on_error_resume_next::on_error_resume_next_observer68         on_error_resume_next_observer(dest_type d, composite_subscription cs, select_type s)
69             : dest(std::move(d))
70             , lifetime(std::move(cs))
71             , selector(std::move(s))
72         {
73             dest.add(lifetime);
74         }
on_nextrxcpp::operators::detail::on_error_resume_next::on_error_resume_next_observer75         void on_next(value_type v) const {
76             dest.on_next(std::move(v));
77         }
on_errorrxcpp::operators::detail::on_error_resume_next::on_error_resume_next_observer78         void on_error(rxu::error_ptr e) const {
79             auto selected = on_exception(
80                 [&](){
81                     return this->selector(std::move(e));},
82                 dest);
83             if (selected.empty()) {
84                 return;
85             }
86             selected->subscribe(dest);
87         }
on_completedrxcpp::operators::detail::on_error_resume_next::on_error_resume_next_observer88         void on_completed() const {
89             dest.on_completed();
90         }
91 
makerxcpp::operators::detail::on_error_resume_next::on_error_resume_next_observer92         static subscriber<T, observer_type> make(dest_type d, select_type s) {
93             auto cs = composite_subscription();
94             return make_subscriber<T>(cs, observer_type(this_type(std::move(d), cs, std::move(s))));
95         }
96     };
97 
98     template<class Subscriber>
operator ()rxcpp::operators::detail::on_error_resume_next99     auto operator()(Subscriber dest) const
100         -> decltype(on_error_resume_next_observer<Subscriber>::make(std::move(dest), selector)) {
101         return      on_error_resume_next_observer<Subscriber>::make(std::move(dest), selector);
102     }
103 };
104 
105 }
106 
107 /*! @copydoc rx-on_error_resume_next.hpp
108 */
109 template<class... AN>
on_error_resume_next(AN &&...an)110 auto on_error_resume_next(AN&&... an)
111     ->     operator_factory<on_error_resume_next_tag, AN...> {
112     return operator_factory<on_error_resume_next_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
113 }
114 
115 /*! @copydoc rx-on_error_resume_next.hpp
116 */
117 template<class... AN>
switch_on_error(AN &&...an)118 auto switch_on_error(AN&&... an)
119     ->     operator_factory<on_error_resume_next_tag, AN...> {
120     return operator_factory<on_error_resume_next_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
121 }
122 
123 }
124 
125 template<>
126 struct member_overload<on_error_resume_next_tag>
127 {
128     template<class Observable, class Selector,
129             class SourceValue = rxu::value_type_t<Observable>,
130             class OnErrorResumeNext = rxo::detail::on_error_resume_next<SourceValue, rxu::decay_t<Selector>>,
131             class Value = rxu::value_type_t<OnErrorResumeNext>>
memberrxcpp::member_overload132     static auto member(Observable&& o, Selector&& p)
133     -> decltype(o.template lift<Value>(OnErrorResumeNext(std::forward<Selector>(p)))) {
134         return      o.template lift<Value>(OnErrorResumeNext(std::forward<Selector>(p)));
135     }
136 
137     template<class... AN>
memberrxcpp::member_overload138     static operators::detail::on_error_resume_next_invalid_t<AN...> member(const AN&...) {
139         std::terminate();
140         return {};
141         static_assert(sizeof...(AN) == 10000, "on_error_resume_next takes (Selector)");
142     }
143 };
144 
145 }
146 
147 #endif
148