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-switch_if_empty.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_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 
49  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 
67  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  }
75  void on_next(source_value_type v) const {
76  is_empty = false;
77  dest.on_next(std::move(v));
78  }
79  void on_error(std::exception_ptr e) const {
80  dest.on_error(std::move(e));
81  }
82  void on_completed() const {
83  if(!is_empty) {
84  dest.on_completed();
85  } else {
86  backup.subscribe(dest);
87  }
88  }
89 
90  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>
97  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 
107 template<class... AN>
108 auto switch_if_empty(AN&&... an)
110  return operator_factory<switch_if_empty_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
111 }
112 
125 template<class... AN>
126 auto default_if_empty(AN&&... an)
128  return operator_factory<default_if_empty_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
129 }
130 
131 }
132 
133 template<>
135 {
136  template<class Observable, class BackupSource,
137  class Enabled = rxu::enable_if_all_true_type_t<
139  class SourceValue = rxu::value_type_t<Observable>,
140  class SwitchIfEmpty = rxo::detail::switch_if_empty<SourceValue, rxu::decay_t<BackupSource>>>
141  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>
147  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<>
156 {
157  template<class Observable, class Value,
158  class Enabled = rxu::enable_if_all_true_type_t<
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>>
163  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>
169  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
auto default_if_empty(AN &&...an) -> operator_factory< default_if_empty_tag, AN... >
If the source Observable terminates without emitting any items, emits a default item and completes...
Definition: rx-switch_if_empty.hpp:126
Definition: rx-util.hpp:100
Definition: rx-all.hpp:26
typename std::decay< T >::type::value_type value_type_t
Definition: rx-util.hpp:35
static operators::detail::switch_if_empty_invalid_t< AN... > member(AN...)
Definition: rx-switch_if_empty.hpp:169
Definition: rx-operators.hpp:69
Definition: rx-operators.hpp:408
auto AN
Definition: rx-finally.hpp:105
static auto member(Observable &&o, BackupSource &&b) -> decltype(o.template lift< SourceValue >(SwitchIfEmpty(std::forward< BackupSource >(b))))
Definition: rx-switch_if_empty.hpp:141
Definition: rx-operators.hpp:47
linq_driver< iter_cursor< typename util::container_traits< TContainer >::iterator > > from(TContainer &c)
Definition: linq.hpp:556
auto is_empty(AN &&...an) -> operator_factory< is_empty_tag, AN... >
Returns an Observable that emits true if the source Observable is empty, otherwise false...
Definition: rx-all.hpp:128
typename std::enable_if< all_true_type< BN... >::value >::type enable_if_all_true_type_t
Definition: rx-util.hpp:114
static operators::detail::switch_if_empty_invalid_t< AN... > member(AN...)
Definition: rx-switch_if_empty.hpp:147
static auto member(Observable &&o, Value v) -> decltype(o.template lift< SourceValue >(DefaultIfEmpty(rxs::from(std::move(v)))))
Definition: rx-switch_if_empty.hpp:163
auto switch_if_empty(AN &&...an) -> operator_factory< switch_if_empty_tag, AN... >
If the source Observable terminates without emitting any items, emits items from a backup Observable...
Definition: rx-switch_if_empty.hpp:108
Definition: rx-operators.hpp:402
Definition: rx-predef.hpp:177