ReactivePlusPlus
ReactiveX implementation for C++20
Loading...
Searching...
No Matches
take.hpp
1// ReactivePlusPlus library
2//
3// Copyright Aleksey Loginov 2023 - present.
4// Distributed under the Boost Software License, Version 1.0.
5// (See accompanying file LICENSE_1_0.txt or copy at
6// https://www.boost.org/LICENSE_1_0.txt)
7//
8// Project home: https://github.com/victimsnino/ReactivePlusPlus
9//
10
11#pragma once
12
13#include <rpp/operators/fwd.hpp>
14
15#include <rpp/defs.hpp>
16#include <rpp/operators/details/strategy.hpp>
17
18#include <cstddef>
19
20namespace rpp::operators::details
21{
22 template<rpp::constraint::observer TObserver>
24 {
25 static constexpr auto preferred_disposables_mode = rpp::details::observers::disposables_mode::None;
26
27 RPP_NO_UNIQUE_ADDRESS TObserver observer;
28 mutable size_t count{};
29
30 template<typename T>
31 void on_next(T&& v) const
32 {
33 if (count > 0)
34 {
35 --count;
36 observer.on_next(std::forward<T>(v));
37 }
38
39 if (count == 0)
40 observer.on_completed();
41 }
42
43 void on_error(const std::exception_ptr& err) const { observer.on_error(err); }
44
45 void on_completed() const { observer.on_completed(); }
46
47 void set_upstream(const disposable_wrapper& d) { observer.set_upstream(d); }
48
49 bool is_disposed() const { return observer.is_disposed(); }
50 };
51
52 struct take_t : lift_operator<take_t, size_t>
53 {
54 using lift_operator<take_t, size_t>::lift_operator;
55
56 template<rpp::constraint::decayed_type T>
58 {
59 using result_type = T;
60
61 template<rpp::constraint::observer_of_type<result_type> TObserver>
62 using observer_strategy = take_observer_strategy<TObserver>;
63 };
64
65 template<rpp::details::observables::constraint::disposables_strategy Prev>
66 using updated_optimal_disposables_strategy = Prev;
67 };
68} // namespace rpp::operators::details
69
70namespace rpp::operators
71{
92 *
93 * @ingroup filtering_operators
94 * @see https://reactivex.io/documentation/operators/take.html
95 */
96 inline auto take(size_t count)
97 {
98 return details::take_t{count};
99 }
100} // namespace rpp::operators
disposable_wrapper_impl< interface_disposable > disposable_wrapper
Wrapper to keep "simple" disposable. Specialization of rpp::disposable_wrapper_impl.
Definition fwd.hpp:34
auto take(size_t count)
Emit only first count items provided by observable, then send on_completed
Definition take.hpp:92
Definition take.hpp:53