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-retry.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_RETRY_HPP)
21 #define RXCPP_OPERATORS_RX_RETRY_HPP
22 
23 #include "../rx-includes.hpp"
25 
26 namespace rxcpp {
27 
28 namespace operators {
29 
30 namespace detail {
31 
32 template<class... AN>
33 struct retry_invalid_arguments {};
34 
35 template<class... AN>
36 struct retry_invalid : public rxo::operator_base<retry_invalid_arguments<AN...>> {
37  using type = observable<retry_invalid_arguments<AN...>, retry_invalid<AN...>>;
38 };
39 template<class... AN>
40 using retry_invalid_t = typename retry_invalid<AN...>::type;
41 
42 // Contain retry variations in a namespace
43 namespace retry {
44  struct event_handlers {
45  template <typename State>
46  static inline void on_error(State& state, std::exception_ptr& e) {
47  state->update();
48  // Use specialized predicate for finite/infinte case
49  if (state->completed_predicate()) {
50  state->out.on_error(e);
51  } else {
52  state->do_subscribe();
53  }
54  }
55 
56  template <typename State>
57  static inline void on_completed(State& state) {
58  state->out.on_completed();
59  }
60  };
61 
62  // Finite repeat case (explicitely limited with the number of times)
63  template <class T, class Observable, class Count>
64  using finite = ::rxcpp::operators::detail::retry_repeat_common::finite
65  <event_handlers, T, Observable, Count>;
66 
67  // Infinite repeat case
68  template <class T, class Observable>
69  using infinite = ::rxcpp::operators::detail::retry_repeat_common::infinite
70  <event_handlers, T, Observable>;
71 
72 }
73 } // detail
74 
77 template<class... AN>
78 auto retry(AN&&... an)
80  return operator_factory<retry_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
81 }
82 
83 }
84 
85 template<>
87 {
88  template<class Observable,
90  class SourceValue = rxu::value_type_t<Observable>,
91  class Retry = rxo::detail::retry::infinite<SourceValue, rxu::decay_t<Observable>>,
92  class Value = rxu::value_type_t<Retry>,
93  class Result = observable<Value, Retry>
94  >
95  static Result member(Observable&& o) {
96  return Result(Retry(std::forward<Observable>(o)));
97  }
98 
99  template<class Observable,
100  class Count,
102  class SourceValue = rxu::value_type_t<Observable>,
103  class Retry = rxo::detail::retry::finite<SourceValue, rxu::decay_t<Observable>, rxu::decay_t<Count>>,
104  class Value = rxu::value_type_t<Retry>,
105  class Result = observable<Value, Retry>
106  >
107  static Result member(Observable&& o, Count&& c) {
108  return Result(Retry(std::forward<Observable>(o), std::forward<Count>(c)));
109  }
110 
111  template<class... AN>
112  static operators::detail::retry_invalid_t<AN...> member(const AN&...) {
113  std::terminate();
114  return {};
115  static_assert(sizeof...(AN) == 10000, "retry takes (optional Count)");
116  }
117 };
118 
119 }
120 
121 #endif
Definition: rx-all.hpp:26
typename std::decay< T >::type::value_type value_type_t
Definition: rx-util.hpp:35
Definition: rx-operators.hpp:69
auto AN
Definition: rx-finally.hpp:105
Definition: rx-operators.hpp:339
typename std::decay< T >::type decay_t
Definition: rx-util.hpp:36
Definition: rx-operators.hpp:47
typename std::enable_if< all_true_type< BN... >::value >::type enable_if_all_true_type_t
Definition: rx-util.hpp:114
a source of values. subscribe or use one of the operator methods that return a new observable...
Definition: rx-observable.hpp:510
static Result member(Observable &&o, Count &&c)
Definition: rx-retry.hpp:107
auto retry(AN &&...an) -> operator_factory< retry_tag, AN... >
Retry this observable for the given number of times.
Definition: rx-retry.hpp:78
static operators::detail::retry_invalid_t< AN... > member(const AN &...)
Definition: rx-retry.hpp:112
Implementation commonalities between retry and repeat operators abstracted away from rx-retry...
static Result member(Observable &&o)
Definition: rx-retry.hpp:95