ReactivePlusPlus
One more implementation of ReactiveX approach in C++ with care about performance and templates in mind
 
Loading...
Searching...
No Matches
do.hpp
1// ReactivePlusPlus library
2//
3// Copyright Aleksey Loginov 2022 - 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/observables/details/member_overload.hpp> // member_overload
14#include <rpp/observers/constraints.hpp> // constraint::observer_of_type
15
16namespace rpp::details
17{
18struct do_tag;
19}
20
21namespace rpp::details
22{
23template<constraint::decayed_type Type, constraint::observer_of_type<Type> TObs>
24struct do_impl;
25
26template<constraint::decayed_type Type, typename SpecificObservable>
27struct member_overload<Type, SpecificObservable, do_tag>
28{
45 template<constraint::observer_of_type<Type> TObs>
46 auto tap(TObs&& observer) const& requires is_header_included <do_tag, TObs>
47 {
48 return static_cast<const SpecificObservable*>(this)->template lift<Type>(do_impl<Type, std::decay_t<TObs>>{std::forward< TObs>(observer)});
49 }
50
51 template<constraint::observer_of_type<Type> TObs>
52 auto tap(TObs&& observer) && requires is_header_included <do_tag, TObs>
53 {
54 return std::move(*static_cast<SpecificObservable*>(this)).template lift<Type>(do_impl<Type, std::decay_t<TObs>>{std::forward< TObs>(observer)});
55 }
56
75 template<constraint::on_next_fn<Type> OnNextFn, constraint::on_error_fn OnErrorFn = utils::empty_function_t<std::exception_ptr>, constraint::on_completed_fn OnCompletedFn = utils::empty_function_t<>>
76 auto tap(OnNextFn&& on_next, OnErrorFn&& on_error = OnErrorFn{}, OnCompletedFn&& on_completed = OnCompletedFn{}) const& requires is_header_included <do_tag, OnNextFn, OnErrorFn, OnCompletedFn>
77 {
79 return static_cast<const SpecificObservable*>(this)->tap(TObserver{std::forward<OnNextFn>(on_next),
80 std::forward<OnErrorFn>(on_error),
81 std::forward<OnCompletedFn>(on_completed)});
82 }
83
84 template<constraint::on_next_fn<Type> OnNextFn, constraint::on_error_fn OnErrorFn = utils::empty_function_t<std::exception_ptr>, constraint::on_completed_fn OnCompletedFn = utils::empty_function_t<>>
85 auto tap(OnNextFn&& on_next, OnErrorFn&& on_error = OnErrorFn{}, OnCompletedFn&& on_completed = OnCompletedFn{}) && requires is_header_included <do_tag, OnNextFn, OnErrorFn, OnCompletedFn>
86 {
87 using TObserver = specific_observer_with_decayed_args<Type, OnNextFn, OnErrorFn, OnCompletedFn>;
88 return std::move(*static_cast<SpecificObservable*>(this)).tap(TObserver{std::forward<OnNextFn>(on_next),
89 std::forward<OnErrorFn>(on_error),
90 std::forward<OnCompletedFn>(on_completed)});
91 }
92
109 template<constraint::on_next_fn<Type> OnNextFn>
110 auto do_on_next(OnNextFn&& on_next) const& requires is_header_included <do_tag, OnNextFn>
111 {
112 return static_cast<const SpecificObservable*>(this)->tap(std::forward<OnNextFn>(on_next));
113 }
114
115 template<constraint::on_next_fn<Type> OnNextFn>
116 auto do_on_next(OnNextFn&& on_next) && requires is_header_included <do_tag, OnNextFn>
117 {
118 return std::move(*static_cast<SpecificObservable*>(this)).tap(std::forward<OnNextFn>(on_next));
119 }
120
137 template<constraint::on_error_fn OnErrorFn>
138 auto do_on_error(OnErrorFn&& on_error) const& requires is_header_included <do_tag, OnErrorFn>
139 {
140 return static_cast<const SpecificObservable*>(this)->tap(utils::empty_function_t<Type>{}, std::forward<OnErrorFn>(on_error));
141 }
142
143 template<constraint::on_error_fn OnErrorFn>
144 auto do_on_error(OnErrorFn&& on_error) && requires is_header_included <do_tag, OnErrorFn>
145 {
146 return std::move(*static_cast<SpecificObservable*>(this)).tap(utils::empty_function_t<Type>{}, std::forward<OnErrorFn>(on_error));
147 }
148
165 template<constraint::on_completed_fn OnCompletedFn>
166 auto do_on_completed(OnCompletedFn&& on_completed) const& requires is_header_included <do_tag, OnCompletedFn>
167 {
168 return static_cast<const SpecificObservable*>(this)->tap(utils::empty_function_t<Type>{}, utils::empty_function_t<std::exception_ptr>{},std::forward<OnCompletedFn>(on_completed));
169 }
170
171 template<constraint::on_completed_fn OnCompletedFn>
172 auto do_on_completed(OnCompletedFn&& on_completed) && requires is_header_included <do_tag, OnCompletedFn>
173 {
174 return std::move(*static_cast<SpecificObservable*>(this)).tap(utils::empty_function_t<Type>{}, utils::empty_function_t<std::exception_ptr>{}, std::forward<OnCompletedFn>(on_completed));
175 }
176};
177} // namespace rpp::details
Observer specified with specific template types of callbacks to avoid extra heap usage.
Definition: specific_observer.hpp:35
auto tap(TObs &&observer) const &
Register an observer to be called when observable provides any events (on_next/on_error/on_completed)
Definition: do.hpp:46
auto do_on_next(OnNextFn &&on_next) const &
Register an callback to be called when observable provides new item (on_next)
Definition: do.hpp:110
auto do_on_completed(OnCompletedFn &&on_completed) const &
Register an callback to be called when observable provides complete (on_completed)
Definition: do.hpp:166
auto tap(OnNextFn &&on_next, OnErrorFn &&on_error=OnErrorFn{}, OnCompletedFn &&on_completed=OnCompletedFn{}) const &
Register an list of actions to be called when observable provides any events (on_next/on_error/on_com...
Definition: do.hpp:76
auto do_on_error(OnErrorFn &&on_error) const &
Register an callback to be called when observable provides error (on_error)
Definition: do.hpp:138
Definition: do.hpp:25
Definition: member_overload.hpp:19