ReactivePlusPlus
ReactiveX implementation for C++20
Loading...
Searching...
No Matches
map.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 <type_traits>
19
20namespace rpp::operators::details
21{
22 template<rpp::constraint::observer TObserver, rpp::constraint::decayed_type Fn>
24 {
25 static constexpr auto preferred_disposables_mode = rpp::details::observers::disposables_mode::None;
26
27 RPP_NO_UNIQUE_ADDRESS TObserver observer;
28 RPP_NO_UNIQUE_ADDRESS Fn fn;
29
30 template<typename T>
31 void on_next(T&& v) const
32 {
33 observer.on_next(fn(std::forward<T>(v)));
34 }
35
36 void on_error(const std::exception_ptr& err) const { observer.on_error(err); }
37
38 void on_completed() const { observer.on_completed(); }
39
40 void set_upstream(const disposable_wrapper& d) { observer.set_upstream(d); }
41
42 bool is_disposed() const { return observer.is_disposed(); }
43 };
44
45 template<rpp::constraint::decayed_type Fn>
46 struct map_t : lift_operator<map_t<Fn>, Fn>
47 {
48 using lift_operator<map_t<Fn>, Fn>::lift_operator;
49
50 template<rpp::constraint::decayed_type T>
52 {
53 static_assert(!std::same_as<void, std::invoke_result_t<Fn, T>>, "Fn is not invocable with T");
54
55 using result_type = std::invoke_result_t<Fn, T>;
56
57 template<rpp::constraint::observer_of_type<result_type> TObserver>
58 using observer_strategy = map_observer_strategy<TObserver, Fn>;
59 };
60
61 template<rpp::details::observables::constraint::disposables_strategy Prev>
62 using updated_optimal_disposables_strategy = Prev;
63 };
64} // namespace rpp::operators::details
65
66namespace rpp::operators
67{
94 * @see https://reactivex.io/documentation/operators/map.html
95 */
96 template<typename Fn>
97 requires (!utils::is_not_template_callable<Fn> || !std::same_as<void, std::invoke_result_t<Fn, rpp::utils::convertible_to_any>>)
98 auto map(Fn&& callable)
99 {
100 return details::map_t<std::decay_t<Fn>>{std::forward<Fn>(callable)};
101 }
102} // namespace rpp::operators
Definition function_traits.hpp:45
disposable_wrapper_impl< interface_disposable > disposable_wrapper
Wrapper to keep "simple" disposable. Specialization of rpp::disposable_wrapper_impl.
Definition fwd.hpp:34
auto map(Fn &&callable)
Transforms the items emitted by an Observable via applying a function to each item and emitting resul...
Definition map.hpp:94
Definition map.hpp:47