ReactivePlusPlus
ReactiveX implementation for C++20
Toggle main menu visibility
Loading...
Searching...
No Matches
last.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
11
#pragma once
12
13
#include <rpp/operators/fwd.hpp>
14
15
#include <rpp/defs.hpp>
16
#include <rpp/operators/details/strategy.hpp>
17
18
#include <optional>
19
20
namespace
rpp::operators::details
21
{
22
template
<rpp::constra
int
::decayed_type Type, rpp::constra
int
::observer TObserver>
23
struct
last_observer_strategy
24
{
25
static
constexpr
auto
preferred_disposables_mode = rpp::details::observers::disposables_mode::None;
26
27
RPP_NO_UNIQUE_ADDRESS TObserver observer;
28
mutable
std::optional<Type> value{};
29
30
template
<
typename
T>
31
void
on_next(T&& v)
const
32
{
33
value.emplace(std::forward<T>(v));
34
}
35
36
void
on_completed()
const
37
{
38
if
(value.has_value())
39
{
40
observer.on_next(std::move(value).value());
41
observer.on_completed();
42
}
43
else
44
observer.on_error(std::make_exception_ptr(
utils::not_enough_emissions
{
"last() operator expects at least one emission from observable before completion"
}));
45
}
46
47
void
on_error(
const
std::exception_ptr& err)
const
{ observer.on_error(err); }
48
49
void
set_upstream(
const
disposable_wrapper
& d) { observer.set_upstream(d); }
50
51
bool
is_disposed()
const
{
return
observer.is_disposed(); }
52
};
53
54
struct
last_t
: lift_operator<last_t>
55
{
56
using
lift_operator<
last_t
>::lift_operator;
57
58
template
<rpp::constra
int
::decayed_type T>
59
struct
operator_traits
60
{
61
using
result_type = T;
62
63
template
<rpp::constra
int
::observer_of_type<result_type> TObserver>
64
using
observer_strategy =
last_observer_strategy<T, TObserver>
;
65
};
66
67
template
<rpp::details::observables::constra
int
::disposables_strategy Prev>
68
using
updated_optimal_disposables_strategy = Prev;
69
};
70
}
// namespace rpp::operators::details
71
72
namespace
rpp::operators
73
{
95
*
96
* @ingroup filtering_operators
97
* @see https://reactivex.io/documentation/operators/last.html
98
*/
99
inline
auto
last
()
100
{
101
return
details::last_t
{};
102
}
103
}
// namespace rpp::operators
rpp::disposable_wrapper
disposable_wrapper_impl< interface_disposable > disposable_wrapper
Wrapper to keep "simple" disposable. Specialization of rpp::disposable_wrapper_impl.
Definition
fwd.hpp:34
rpp::operators::last
auto last()
Emit only the last item provided before on_completed.
Definition
last.hpp:95
rpp::operators::details::last_observer_strategy
Definition
last.hpp:24
rpp::operators::details::last_t::operator_traits
Definition
last.hpp:60
rpp::operators::details::last_t
Definition
last.hpp:55
rpp::utils::not_enough_emissions
Definition
exceptions.hpp:18
src
rpp
rpp
operators
last.hpp
Generated by
1.17.0