ReactivePlusPlus
One more implementation of ReactiveX approach in C++ with care about performance and templates in mind
 
Loading...
Searching...
No Matches
subscriber_with_state.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#pragma once
11
12#include <rpp/observers/state_observer.hpp>
13#include <rpp/observers/dynamic_observer.hpp>
14#include <rpp/subscriptions/composite_subscription.hpp>
15#include <rpp/subscribers/specific_subscriber.hpp>
16
17namespace rpp::details
18{
19template<constraint::decayed_type Type,
20 typename... States,
21 std::invocable<Type, std::decay_t<States>...> OnNext,
22 std::invocable<std::exception_ptr, std::decay_t<States>...> OnError,
23 std::invocable<std::decay_t<States>...> OnCompleted>
24auto create_subscriber_with_state(composite_subscription sub,
25 OnNext&& on_next,
26 OnError&& on_error,
27 OnCompleted&& on_completed,
28 States&&... states)
29{
30 using TObs = state_observer<Type,
31 std::decay_t<OnNext>,
32 std::decay_t<OnError>,
33 std::decay_t<OnCompleted>,
34 std::decay_t<States>...>;
35 return make_specific_subscriber<Type, TObs>(std::move(sub),
36 std::forward<OnNext>(on_next),
37 std::forward<OnError>(on_error),
38 std::forward<OnCompleted>(on_completed),
39 std::forward<States>(states)...);
40}
41
42template<constraint::decayed_type Type,
43 typename... States,
44 std::invocable<Type, std::decay_t<States>...> OnNext,
45 std::invocable<std::exception_ptr, std::decay_t<States>...> OnError,
46 std::invocable<std::decay_t<States>...> OnCompleted>
47auto create_subscriber_with_dynamic_state(composite_subscription sub,
48 OnNext&& on_next,
49 OnError&& on_error,
50 OnCompleted&& on_completed,
51 States&&... states)
52{
53 using TObs = dynamic_state_observer<Type, std::decay_t<States>...>;
54 return make_specific_subscriber<Type, TObs>(std::move(sub),
55 std::forward<OnNext>(on_next),
56 std::forward<OnError>(on_error),
57 std::forward<OnCompleted>(on_completed),
58 std::forward<States>(states)...);
59}
60} // namespace rpp::details