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
5 /*! \file rx-take.hpp
6
7 \brief For the first count items from this observable emit them from the new observable that is returned.
8
9 \tparam Count the type of the items counter.
10
11 \param t the number of items to take.
12
13 \return An observable that emits only the first t items emitted by the source Observable, or all of the items from the source observable if that observable emits fewer than t items.
14
15 \sample
16 \snippet take.cpp take sample
17 \snippet output.txt take sample
18 */
19
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 {
valuesrxcpp::operators::detail::take::values48 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
takerxcpp::operators::detail::take58 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>
on_subscriberxcpp::operators::detail::take74 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](rxu::error_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
130 /*! @copydoc rx-take.hpp
131 */
132 template<class... AN>
take(AN &&...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<>
141 struct member_overload<take_tag>
142 {
143 template<class Observable,
144 class Count,
145 class Enabled = rxu::enable_if_all_true_type_t<
146 is_observable<Observable>>,
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>>
memberrxcpp::member_overload151 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>
memberrxcpp::member_overload156 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
166