ReactivePlusPlus
ReactiveX implementation for C++20
Loading...
Searching...
No Matches
lambda_observer.hpp
1// ReactivePlusPlus library
2//
3// Copyright Aleksey Loginov 2023 - 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/defs.hpp>
13#include <rpp/disposables/composite_disposable.hpp>
14#include <rpp/observers/observer.hpp>
15#include <rpp/utils/function_traits.hpp>
16
17namespace rpp::details::observers
18{
20 std::invocable<Type> OnNext,
21 std::invocable<const std::exception_ptr&> OnError,
22 std::invocable<> OnCompleted>
24 {
25 static constexpr auto preferred_disposables_mode = rpp::details::observers::disposables_mode::Auto;
26
27 template<rpp::constraint::decayed_same_as<OnNext> TOnNext, rpp::constraint::decayed_same_as<OnError> TOnError, rpp::constraint::decayed_same_as<OnCompleted> TOnCompleted>
28 explicit lambda_strategy(TOnNext&& on_next, TOnError&& on_error, TOnCompleted&& on_completed)
29 : on_next{std::forward<TOnNext>(on_next)}
30 , on_error{std::forward<TOnError>(on_error)}
31 , on_completed{std::forward<TOnCompleted>(on_completed)}
32 {
33 }
34
35 RPP_NO_UNIQUE_ADDRESS OnNext on_next{};
36 RPP_NO_UNIQUE_ADDRESS OnError on_error{};
37 RPP_NO_UNIQUE_ADDRESS OnCompleted on_completed{};
38
39 static void set_upstream(const disposable_wrapper&) noexcept {}
40
41 static bool is_disposed() noexcept { return false; }
42 };
43} // namespace rpp::details::observers
44
45namespace rpp
46{
47 template<constraint::decayed_type Type,
48 std::invocable<Type> OnNext,
49 std::invocable<const std::exception_ptr&> OnError,
50 std::invocable<> OnCompleted>
51 auto make_lambda_observer(OnNext&& on_next,
52 OnError&& on_error,
53 OnCompleted&& on_completed) -> lambda_observer<Type,
54 std::decay_t<OnNext>,
55 std::decay_t<OnError>,
56 std::decay_t<OnCompleted>>
57 {
58 return lambda_observer<Type,
59 std::decay_t<OnNext>,
60 std::decay_t<OnError>,
61 std::decay_t<OnCompleted>>{
62 std::forward<OnNext>(on_next),
63 std::forward<OnError>(on_error),
64 std::forward<OnCompleted>(on_completed)};
65 }
66
67 template<constraint::decayed_type Type,
68 std::invocable<Type> OnNext,
69 std::invocable<const std::exception_ptr&> OnError,
70 std::invocable<> OnCompleted>
72 OnNext&& on_next,
73 OnError&& on_error,
74 OnCompleted&& on_completed) -> lambda_observer_with_external_disposable<Type,
75 std::decay_t<OnNext>,
76 std::decay_t<OnError>,
77 std::decay_t<OnCompleted>>
78 {
80 std::decay_t<OnNext>,
81 std::decay_t<OnError>,
82 std::decay_t<OnCompleted>>{
83 d,
84 std::forward<OnNext>(on_next),
85 std::forward<OnError>(on_error),
86 std::forward<OnCompleted>(on_completed)};
87 }
88} // namespace rpp
Base class for any observer used in RPP. It handles core callbacks of observers. Objects of this clas...
Definition observer.hpp:172
Definition constraints.hpp:22
auto make_lambda_observer(OnNext &&on_next, OnError &&on_error={}, OnCompleted &&on_completed={}) -> lambda_observer< Type, std::decay_t< OnNext >, std::decay_t< OnError >, std::decay_t< OnCompleted > >
Constructs observer specialized with passed callbacks. Most easiesest way to construct observer "on t...
Definition lambda_observer.hpp:51
Definition lambda_observer.hpp:24