RxCpp
The Reactive Extensions for Native (RxCpp) is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators in both C and C++.
rx-on_error_resume_next.hpp
Go to the documentation of this file.
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 
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>
44 {
45  typedef rxu::decay_t<T> value_type;
46  typedef rxu::decay_t<Selector> select_type;
47  typedef decltype((*(select_type*)nullptr)(std::exception_ptr())) fallback_type;
48  select_type selector;
49 
50  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)(std::exception_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 
68  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  }
75  void on_next(value_type v) const {
76  dest.on_next(std::move(v));
77  }
78  void on_error(std::exception_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  }
88  void on_completed() const {
89  dest.on_completed();
90  }
91 
92  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>
99  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 
109 template<class... AN>
112  return operator_factory<on_error_resume_next_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
113 }
114 
117 template<class... 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<>
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>>,
132  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>
138  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
static operators::detail::on_error_resume_next_invalid_t< AN... > member(const AN &...)
Definition: rx-on_error_resume_next.hpp:138
Definition: rx-all.hpp:26
typename std::decay< T >::type::value_type value_type_t
Definition: rx-util.hpp:35
Definition: rx-operators.hpp:276
Definition: rx-operators.hpp:69
auto switch_on_error(AN &&...an) -> operator_factory< on_error_resume_next_tag, AN... >
If an error occurs, take the result from the Selector and subscribe to that instead.
Definition: rx-on_error_resume_next.hpp:118
auto AN
Definition: rx-finally.hpp:105
Definition: rx-operators.hpp:47
auto on_error_resume_next(AN &&...an) -> operator_factory< on_error_resume_next_tag, AN... >
If an error occurs, take the result from the Selector and subscribe to that instead.
Definition: rx-on_error_resume_next.hpp:110
auto on_exception(const F &f, const OnError &c) -> typename std::enable_if< detail::is_on_error< OnError >::value, typename detail::maybe_from_result< F >::type >::type
Definition: rx-observer.hpp:639
static auto member(Observable &&o, Selector &&p) -> decltype(o.template lift< Value >(OnErrorResumeNext(std::forward< Selector >(p))))
Definition: rx-on_error_resume_next.hpp:132