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.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_HPP)
21 #define RXCPP_OPERATORS_RX_TAKE_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_invalid_arguments {};
33 
34 template<class... AN>
35 struct take_invalid : public rxo::operator_base<take_invalid_arguments<AN...>> {
36  using type = observable<take_invalid_arguments<AN...>, take_invalid<AN...>>;
37 };
38 template<class... AN>
39 using take_invalid_t = typename take_invalid<AN...>::type;
40 
41 template<class T, class Observable, class Count>
42 struct take : public operator_base<T>
43 {
44  typedef rxu::decay_t<Observable> source_type;
45  typedef rxu::decay_t<Count> count_type;
46  struct values
47  {
48  values(source_type s, count_type t)
49  : source(std::move(s))
50  , count(std::move(t))
51  {
52  }
53  source_type source;
54  count_type count;
55  };
56  values initial;
57 
58  take(source_type s, count_type t)
59  : initial(std::move(s), std::move(t))
60  {
61  }
62 
63  struct mode
64  {
65  enum type {
66  taking, // capture messages
67  triggered, // ignore messages
68  errored, // error occured
69  stopped // observable completed
70  };
71  };
72 
73  template<class Subscriber>
74  void on_subscribe(const Subscriber& s) const {
75 
76  typedef Subscriber output_type;
77  struct state_type
78  : public std::enable_shared_from_this<state_type>
79  , public values
80  {
81  state_type(const values& i, const output_type& oarg)
82  : values(i)
83  , mode_value(mode::taking)
84  , out(oarg)
85  {
86  }
87  typename mode::type mode_value;
88  output_type out;
89  };
90  // take a copy of the values for each subscription
91  auto state = std::make_shared<state_type>(initial, s);
92 
93  composite_subscription source_lifetime;
94 
95  s.add(source_lifetime);
96 
97  state->source.subscribe(
98  // split subscription lifetime
99  source_lifetime,
100  // on_next
101  [state, source_lifetime](T t) {
102  if (state->mode_value < mode::triggered) {
103  if (--state->count > 0) {
104  state->out.on_next(t);
105  } else {
106  state->mode_value = mode::triggered;
107  state->out.on_next(t);
108  // must shutdown source before signaling completion
109  source_lifetime.unsubscribe();
110  state->out.on_completed();
111  }
112  }
113  },
114  // on_error
115  [state](std::exception_ptr e) {
116  state->mode_value = mode::errored;
117  state->out.on_error(e);
118  },
119  // on_completed
120  [state]() {
121  state->mode_value = mode::stopped;
122  state->out.on_completed();
123  }
124  );
125  }
126 };
127 
128 }
129 
132 template<class... AN>
133 auto take(AN&&... an)
134  -> operator_factory<take_tag, AN...> {
135  return operator_factory<take_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
136 }
137 
138 }
139 
140 template<>
142 {
143  template<class Observable,
144  class Count,
145  class Enabled = rxu::enable_if_all_true_type_t<
147  class SourceValue = rxu::value_type_t<Observable>,
148  class Take = rxo::detail::take<SourceValue, rxu::decay_t<Observable>, rxu::decay_t<Count>>,
149  class Value = rxu::value_type_t<Take>,
150  class Result = observable<Value, Take>>
151  static Result member(Observable&& o, Count&& c) {
152  return Result(Take(std::forward<Observable>(o), std::forward<Count>(c)));
153  }
154 
155  template<class... AN>
156  static operators::detail::take_invalid_t<AN...> member(AN...) {
157  std::terminate();
158  return {};
159  static_assert(sizeof...(AN) == 10000, "take takes (optional Count)");
160  }
161 };
162 
163 }
164 
165 #endif
auto count() -> operator_factory< reduce_tag, int, rxu::count, rxu::detail::take_at< 0 >>
For each item from this observable reduce it by incrementing a count.
Definition: rx-reduce.hpp:412
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
static Result member(Observable &&o, Count &&c)
Definition: rx-take.hpp:151
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
Definition: rx-operators.hpp:417
static operators::detail::take_invalid_t< AN... > member(AN...)
Definition: rx-take.hpp:156
auto take(AN &&...an) -> operator_factory< take_tag, AN... >
For the first count items from this observable emit them from the new observable that is returned...
Definition: rx-take.hpp:133
Definition: rx-predef.hpp:177