ReactivePlusPlus
One more implementation of ReactiveX approach in C++ with care about performance and templates in mind
 
Loading...
Searching...
No Matches
specific_observer.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/utils/function_traits.hpp> // extract function args
14#include <rpp/utils/functors.hpp> // default arguments
15#include <rpp/observers/state_observer.hpp> // base class
16
17namespace rpp
18{
30template<constraint::decayed_type T,
31 constraint::on_next_fn<T> OnNext = utils::empty_function_t<T>,
32 constraint::on_error_fn OnError = utils::rethrow_error_t,
33 constraint::on_completed_fn OnCompleted = utils::empty_function_t<>>
34class specific_observer : public details::state_observer<T, OnNext, OnError, OnCompleted>
35{
37
38 using base::base;
39public:
40 template<constraint::on_next_fn<T> TOnNext = utils::empty_function_t<T>,
41 constraint::on_error_fn TOnError = utils::rethrow_error_t,
42 constraint::on_completed_fn TOnCompleted = utils::empty_function_t<>>
43 specific_observer(TOnNext&& on_next = {}, TOnError&& on_error = {}, TOnCompleted&& on_completed = {})
44 : base{std::forward<TOnNext>(on_next),
45 std::forward<TOnError>(on_error),
46 std::forward<TOnCompleted>(on_completed)} {}
47
49 : base{std::forward<decltype(on_next)>(on_next),
50 utils::rethrow_error_t{},
51 std::forward<decltype(on_completed)>(on_completed)} {}
52
57 [[nodiscard]] auto as_dynamic() const& { return dynamic_observer<T>{*this}; }
58 [[nodiscard]] auto as_dynamic()&& { return dynamic_observer<T>{std::move(*this)}; }
59};
60
61template<typename OnNext>
62specific_observer(OnNext) -> specific_observer<utils::decayed_function_argument_t<OnNext>, OnNext>;
63
64template<typename OnNext, constraint::on_error_fn OnError, typename ...Args>
65specific_observer(OnNext, OnError, Args...) -> specific_observer<utils::decayed_function_argument_t<OnNext>, OnNext, OnError, Args...>;
66
67template<typename OnNext, constraint::on_completed_fn OnCompleted>
68specific_observer(OnNext, OnCompleted) -> specific_observer<utils::decayed_function_argument_t<OnNext>, OnNext, utils::rethrow_error_t, OnCompleted>;
69
74template<constraint::decayed_type Type>
75auto make_specific_observer() -> specific_observer_with_decayed_args<Type>
76{
77 return {};
78}
79
80template<constraint::decayed_type Type, constraint::on_next_fn<Type> OnNext>
81auto make_specific_observer( OnNext&& on_next) -> specific_observer_with_decayed_args<Type, OnNext>
82{
83 return {std::forward<OnNext>(on_next)};
84}
85
86template<constraint::decayed_type Type, constraint::on_next_fn<Type> OnNext, constraint::on_completed_fn OnCompleted>
87auto make_specific_observer(OnNext&& on_next, OnCompleted&& on_completed) -> specific_observer_with_decayed_args<Type, OnNext, utils::rethrow_error_t, OnCompleted>
88{
89 return {std::forward<OnNext>(on_next), std::forward<OnCompleted>(on_completed)};
90}
91
92template<constraint::decayed_type Type, constraint::on_next_fn<Type> OnNext, constraint::on_error_fn OnError>
93auto make_specific_observer(OnNext&& on_next,
94 OnError&& on_error) -> specific_observer_with_decayed_args<Type, OnNext, OnError>
95{
96 return {std::forward<OnNext>(on_next), std::forward<OnError>(on_error)};
97}
98
99template<constraint::decayed_type Type, constraint::on_next_fn<Type> OnNext, constraint::on_error_fn OnError, constraint::on_completed_fn OnCompleted>
100auto make_specific_observer(OnNext&& on_next,
101 OnError&& on_error,
102 OnCompleted&& on_completed) -> specific_observer_with_decayed_args<Type, OnNext, OnError, OnCompleted>
103{
104 return {std::forward<OnNext>(on_next),
105 std::forward<OnError>(on_error),
106 std::forward<OnCompleted>(on_completed)};
107}
108
109
110namespace details
111{
112 template<constraint::decayed_type Type, typename ...Args>
113 using deduce_specific_observer_type_t = decltype(make_specific_observer<Type>(std::declval<Args>()...));
114} // namespace details
115} // namespace rpp
Special type of specific_observer which has some state which this observer stores and pass to each ca...
Definition: state_observer.hpp:34
void on_next(const T &v) const
Observable calls this methods to notify observer about new value.
Definition: state_observer.hpp:52
void on_completed() const
Observable calls this method to notify observer about finish of work.
Definition: state_observer.hpp:81
void on_error(const std::exception_ptr &err) const
Observable calls this method to notify observer about some error during generation next data.
Definition: state_observer.hpp:72
Dynamic (type-erased) version of observer (comparing to specific_observer)
Definition: dynamic_observer.hpp:109
Observer specified with specific template types of callbacks to avoid extra heap usage.
Definition: specific_observer.hpp:35
auto as_dynamic() const &
Converting current rpp::specific_observer to rpp::dynamic_observer alternative with erasing of type (...
Definition: specific_observer.hpp:57
Definition: fwd.hpp:27
Definition: fwd.hpp:25