ReactivePlusPlus
ReactiveX implementation for C++20
Loading...
Searching...
No Matches
variant_observable.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/observables/observable.hpp>
13#include <rpp/utils/utils.hpp>
14
15#include <variant>
16
17namespace rpp::details
18{
19 template<constraint::decayed_type Type, constraint::observable_of_type<Type>... Observables>
21 {
23
24 using value_type = Type;
25 template<constraint::decayed_any_of<Observables...> TT>
27 explicit variant_observable_strategy(TT&& observable) // NOLINT
28 : observables(std::forward<TT>(observable))
29 {
30 }
31
34
35 utils::unique_variant<Observables...> observables;
36
37 template<rpp::constraint::observer_of_type<value_type> TObs>
38 void subscribe(TObs&& obs) const
39 {
40 std::visit([&](const auto& o) { o.subscribe(std::forward<TObs>(obs)); }, observables);
41 }
42 };
43} // namespace rpp::details
44
45namespace rpp
46{
52 template<constraint::decayed_type Type, constraint::observable_of_type<Type>... Observables>
54 {
55 using base = rpp::observable<Type, details::variant_observable_strategy<Type, Observables...>>;
56
57 public:
58 using base::base;
59 };
60
61 template<constraint::observable T, constraint::observable_of_type<rpp::utils::extract_observable_type_t<T>>... Observables>
62 variant_observable(std::variant<T, Observables...> variant) -> variant_observable<rpp::utils::extract_observable_type_t<T>, T, Observables...>;
63} // namespace rpp
Base class for any observable used in RPP. It handles core callbacks of observable.
Definition observable.hpp:38
Extension over rpp::observable to provide ability statically keep one of multiple observables.
Definition variant_observable.hpp:54
Definition constraints.hpp:28
Definition constraints.hpp:19
Definition disposables_strategy.hpp:19
Definition variant_observable.hpp:21