ReactivePlusPlus
ReactiveX implementation for C++20
Toggle main menu visibility
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/AlexInLog/ReactivePlusPlus
9
10
#pragma once
11
12
#include <rpp/observables/observable.hpp>
13
#include <rpp/utils/utils.hpp>
14
15
#include <variant>
16
17
namespace
rpp::details
18
{
19
template
<constra
int
::decayed_type Type, constra
int
::observable_of_type<Type>... Observables>
20
struct
variant_observable_strategy
21
{
22
using
optimal_disposables_strategy = rpp::details::observables::default_disposables_strategy;
23
24
using
value_type = Type;
25
template
<
constraint::decayed_any_of
<Observables...> TT>
26
requires
(!
constraint::decayed_same_as<variant_observable_strategy, TT>
)
27
explicit
variant_observable_strategy(TT&&
observable
)
// NOLINT
28
: observables(std::forward<TT>(
observable
))
29
{
30
}
31
32
variant_observable_strategy(
const
variant_observable_strategy& other) =
default
;
33
variant_observable_strategy(variant_observable_strategy&& other)
noexcept
=
default
;
34
35
utils::unique_variant<Observables...> observables;
36
37
template
<rpp::constra
int
::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
45
namespace
rpp
46
{
52
template
<constra
int
::decayed_type Type, constra
int
::observable_of_type<Type>... Observables>
53
class
variant_observable
:
public
rpp::observable
<Type,
details::variant_observable_strategy
<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
<constra
int
::observable T, constra
int
::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
rpp::observable
Base class for any observable used in RPP. It handles core callbacks of observable.
Definition
observable.hpp:38
rpp::variant_observable
Extension over rpp::observable to provide ability statically keep one of multiple observables.
Definition
variant_observable.hpp:54
rpp::constraint::decayed_any_of
Definition
constraints.hpp:28
rpp::constraint::decayed_same_as
Definition
constraints.hpp:19
rpp::details::variant_observable_strategy
Definition
variant_observable.hpp:21
src
rpp
rpp
observables
variant_observable.hpp
Generated by
1.17.0