13#include <rpp/observers/constraints.hpp>
14#include <rpp/observers/state_observer.hpp>
15#include <rpp/utils/function_traits.hpp>
16#include <rpp/utils/functors.hpp>
17#include <rpp/defs.hpp>
24template<constra
int::decayed_type T>
29 virtual void on_next(
const T& v)
const = 0;
30 virtual void on_next(T&& v)
const = 0;
31 virtual void on_error(
const std::exception_ptr& err)
const = 0;
32 virtual void on_completed()
const = 0;
35template<constra
int::decayed_type T, constra
int::observer_of_type<T> TObserver>
39 template<
typename ...Args>
40 requires std::constructible_from<TObserver, Args...>
42 : m_observer{ std::forward<Args>(args)... } {}
44 void on_next(
const T& v)
const override { m_observer.on_next(v); }
45 void on_next(T&& v)
const override { m_observer.on_next(std::move(v)); }
46 void on_error(
const std::exception_ptr& err)
const override { m_observer.on_error(err); }
47 void on_completed()
const override { m_observer.on_completed(); }
50 RPP_NO_UNIQUE_ADDRESS TObserver m_observer;
53template<constra
int::decayed_type T, constra
int::observer_of_type<T> TObserver,
typename ...Args>
54std::shared_ptr<dynamic_observer_state_base<T>> make_dynamic_observer_state(Args&& ...args)
requires std::constructible_from<std::decay_t<TObserver>, Args...>
56 return std::make_shared<dynamic_observer_state<T, std::decay_t<TObserver>>>(std::forward<Args>(args)...);
59template<constraint::decayed_type T,
typename ...Args>
60std::shared_ptr<dynamic_observer_state_base<T>> make_dynamic_observer_state_from_fns(Args&& ...args)
62 return make_dynamic_observer_state<T, details::state_observer<T, std::decay_t<Args>...>>(std::forward<Args>(args)...);
65template<constraint::decayed_type T, constraint::decayed_type ...States>
66class dynamic_state_observer :
public state_observer<T, utils::forwarding_on_next_for_pointer,utils::forwarding_on_error_for_pointer, utils::forwarding_on_completed_for_pointer, std::shared_ptr<dynamic_observer_state_base<T>>>
70 template<
typename ...TStates>
71 requires (constraint::decayed_same_as<States, TStates> && ...)
73 std::invocable<std::exception_ptr, States...>
auto&&
on_error,
76 :
base{utils::forwarding_on_next_for_pointer{},
77 utils::forwarding_on_error_for_pointer{},
78 utils::forwarding_on_completed_for_pointer{},
79 make_dynamic_observer_state_from_fns<T>(std::forward<
decltype(
on_next)>(
on_next),
82 std::forward<TStates>(states)...)} {}
85 :
base{utils::forwarding_on_next_for_pointer{},
86 utils::forwarding_on_error_for_pointer{},
87 utils::forwarding_on_completed_for_pointer{},
91 template<constra
int::observer_of_type<T> TObserver>
94 :
dynamic_state_observer{details::make_dynamic_observer_state<T, std::decay_t<TObserver>>(std::forward<TObserver>(obs))} {}
107template<constra
int::decayed_type T>
111 template<constra
int::on_next_fn<T> OnNext = utils::empty_function_t<T>,
112 constra
int::on_error_fn OnError = utils::rethrow_error_t,
113 constra
int::on_completed_fn OnCompleted = utils::empty_function_t<>>
121 utils::rethrow_error_t{},
124 template<constra
int::observer_of_type<T> TObserver>
135template<constra
int::observer TObserver>
136dynamic_observer(TObserver)->dynamic_observer<utils::extract_observer_type_t<TObserver>>;
138template<
typename OnNext,
typename ...Args>
139dynamic_observer(OnNext, Args...)->dynamic_observer<utils::decayed_function_argument_t<OnNext>>;
Definition: dynamic_observer.hpp:37
Definition: dynamic_observer.hpp:67
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
const dynamic_observer< T > & as_dynamic() const
Do nothing for rpp::dynamic_observer. Created only for unification of interfaces with rpp::specific_o...
Definition: dynamic_observer.hpp:132
Definition: dynamic_observer.hpp:26