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-take_while.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_TAKE_WHILE_HPP)
21 #define RXCPP_OPERATORS_RX_TAKE_WHILE_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 take_while_invalid_arguments {};
33 
34 template<class... AN>
35 struct take_while_invalid : public rxo::operator_base<take_while_invalid_arguments<AN...>> {
36  using type = observable<take_while_invalid_arguments<AN...>, take_while_invalid<AN...>>;
37 };
38 template<class... AN>
39 using take_while_invalid_t = typename take_while_invalid<AN...>::type;
40 
41 template<class T, class Predicate>
42 struct take_while
43 {
44  typedef rxu::decay_t<T> source_value_type;
45  typedef rxu::decay_t<Predicate> test_type;
46  test_type test;
47 
48 
49  take_while(test_type t)
50  : test(std::move(t))
51  {
52  }
53 
54  template<class Subscriber>
55  struct take_while_observer
56  {
57  typedef take_while_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  dest_type dest;
62  test_type test;
63 
64  take_while_observer(dest_type d, test_type t)
65  : dest(std::move(d))
66  , test(std::move(t))
67  {
68  }
69  void on_next(source_value_type v) const {
70  if (test(v)) {
71  dest.on_next(v);
72  } else {
73  dest.on_completed();
74  }
75  }
76  void on_error(std::exception_ptr e) const {
77  dest.on_error(e);
78  }
79  void on_completed() const {
80  dest.on_completed();
81  }
82 
83  static subscriber<value_type, observer_type> make(dest_type d, test_type t) {
84  return make_subscriber<value_type>(d, this_type(d, std::move(t)));
85  }
86  };
87 
88  template<class Subscriber>
89  auto operator()(Subscriber dest) const
90  -> decltype(take_while_observer<Subscriber>::make(std::move(dest), test)) {
91  return take_while_observer<Subscriber>::make(std::move(dest), test);
92  }
93 };
94 
95 }
96 
99 template<class... AN>
100 auto take_while(AN&&... an)
102  return operator_factory<take_while_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
103  }
104 
105 }
106 
107 template<>
109 {
110  template<class Observable, class Predicate,
111  class SourceValue = rxu::value_type_t<Observable>,
112  class TakeWhile = rxo::detail::take_while<SourceValue, rxu::decay_t<Predicate>>>
113  static auto member(Observable&& o, Predicate&& p)
114  -> decltype(o.template lift<SourceValue>(TakeWhile(std::forward<Predicate>(p)))) {
115  return o.template lift<SourceValue>(TakeWhile(std::forward<Predicate>(p)));
116  }
117 
118  template<class... AN>
119  static operators::detail::take_while_invalid_t<AN...> member(const AN&...) {
120  std::terminate();
121  return {};
122  static_assert(sizeof...(AN) == 10000, "take_while takes (Predicate)");
123  }
124 };
125 
126 
127 }
128 
129 #endif
Definition: rx-all.hpp:26
typename std::decay< T >::type::value_type value_type_t
Definition: rx-util.hpp:35
static auto member(Observable &&o, Predicate &&p) -> decltype(o.template lift< SourceValue >(TakeWhile(std::forward< Predicate >(p))))
Definition: rx-take_while.hpp:113
Definition: rx-operators.hpp:431
Definition: rx-operators.hpp:69
auto AN
Definition: rx-finally.hpp:105
Definition: rx-operators.hpp:47
auto take_while(AN &&...an) -> operator_factory< take_while_tag, AN... >
For the first items fulfilling the predicate from this observable emit them from the new observable t...
Definition: rx-take_while.hpp:100
static operators::detail::take_while_invalid_t< AN... > member(const AN &...)
Definition: rx-take_while.hpp:119