ReactivePlusPlus
One more implementation of ReactiveX approach in C++ with care about performance and templates in mind
 
Loading...
Searching...
No Matches
state_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/observers/fwd.hpp>
14#include <rpp/utils/function_traits.hpp> // extract argument type
15#include <rpp/defs.hpp>
16
17#include <exception>
18#include <tuple>
19
20namespace rpp::details
21{
25template<constraint::decayed_type T,
26 typename OnNext,
27 typename OnError,
28 typename OnCompleted,
29 constraint::decayed_type ...States>
30requires (std::invocable<OnNext, T, States...> &&
31 std::invocable<OnError, std::exception_ptr, States...> &&
32 std::invocable<OnCompleted, States...>)
33class state_observer : public details::typed_observer_tag<T>
34{
35public:
36 template<typename ...TStates>
37 requires (constraint::decayed_same_as<States, TStates> && ...)
38 state_observer(std::invocable<T, States...> auto&& on_next,
39 std::invocable<std::exception_ptr, States...> auto&& on_error,
40 std::invocable<States...> auto&& on_completed,
41 TStates&& ...states)
42 : m_on_next{std::forward<decltype(on_next)>(on_next)}
43 , m_on_err{std::forward<decltype(on_error)>(on_error)}
44 , m_on_completed{std::forward<decltype(on_completed)>(on_completed)}
45 , m_state{std::forward<TStates>(states)...} {}
46
52 void on_next(const T& v) const
53 {
54 std::apply([&v, this](const States& ...states) { m_on_next(v, states...); }, m_state);
55 }
56
62 void on_next(T&& v) const
63 {
64 std::apply([&v, this](const States& ...states) { m_on_next(std::move(v), states...); }, m_state);
65 }
66
72 void on_error(const std::exception_ptr& err) const
73 {
74 std::apply([&err, this](const States& ...states) { m_on_err(err, states...); }, m_state);
75 }
76
81 void on_completed() const
82 {
83 std::apply(m_on_completed, m_state);
84 }
85
86private:
87 RPP_NO_UNIQUE_ADDRESS OnNext m_on_next;
88 RPP_NO_UNIQUE_ADDRESS OnError m_on_err;
89 RPP_NO_UNIQUE_ADDRESS OnCompleted m_on_completed;
90
91 RPP_NO_UNIQUE_ADDRESS std::tuple<States...> m_state;
92};
93
94template<typename TOnNext, typename ...Args>
95state_observer(TOnNext, Args...)->state_observer<std::decay_t<utils::function_argument_t<TOnNext>>, TOnNext, Args...>;
96
97} // namespace rpp::details
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_next(T &&v) const
Observable calls this methods to notify observer about new value.
Definition: state_observer.hpp:62
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
Definition: fwd.hpp:20