ReactivePlusPlus
ReactiveX implementation for C++20
Toggle main menu visibility
Loading...
Searching...
No Matches
interval.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/sources/fwd.hpp>
13
14
#include <rpp/defs.hpp>
15
#include <rpp/observables/observable.hpp>
16
17
namespace
rpp::details
18
{
19
struct
interval_schedulable
20
{
21
rpp::schedulers::optional_delay_from_this_timepoint operator()(
const
auto
&
observer
, rpp::schedulers::duration period,
size_t
& counter)
const
22
{
23
observer
.
on_next
(counter++);
24
return
rpp::schedulers::optional_delay_from_this_timepoint{period};
25
}
26
};
27
28
template
<
typename
TScheduler,
typename
TimePo
int
OrDuration>
29
struct
interval_strategy
30
{
31
using
value_type = size_t;
32
using
optimal_disposables_strategy =
rpp::details::observables::fixed_disposables_strategy<0>
;
33
34
RPP_NO_UNIQUE_ADDRESS TScheduler scheduler;
35
TimePointOrDuration initial;
36
rpp::schedulers::duration period;
37
38
template
<rpp::constra
int
::observer_of_type<value_type> TObs>
39
void
subscribe(TObs&&
observer
)
const
40
{
41
const
auto
worker = scheduler.create_worker();
42
worker.schedule(initial,
interval_schedulable
{}, std::forward<TObs>(
observer
), period,
size_t
{});
43
}
44
};
45
}
// namespace rpp::details
46
47
namespace
rpp
48
{
49
template
<schedulers::constra
int
::scheduler TScheduler,
typename
TimePo
int
OrDuration>
50
using
interval_observable =
observable<size_t, details::interval_strategy<TScheduler, TimePointOrDuration>
>;
51
}
// namespace rpp
52
53
namespace
rpp::source
54
{
55
72
* @see https://reactivex.io/documentation/operators/interval.html
73
*/
74
template
<schedulers::constra
int
::scheduler TScheduler>
75
auto
interval
(rpp::schedulers::duration initial, rpp::schedulers::duration period, TScheduler&& scheduler)
76
{
77
return
interval_observable<std::decay_t<TScheduler>, rpp::schedulers::duration>{std::forward<TScheduler>(scheduler), initial, period};
78
}
79
88
* @see https://reactivex.io/documentation/operators/interval.html
89
*/
90
template
<schedulers::constra
int
::scheduler TScheduler>
91
auto
interval
(rpp::schedulers::time_point initial, rpp::schedulers::duration period, TScheduler&& scheduler)
92
{
93
return
interval_observable<std::decay_t<TScheduler>, rpp::schedulers::time_point>{std::forward<TScheduler>(scheduler), initial, period};
94
}
95
107
* @snippet interval.cpp interval period
108
*
109
* @ingroup creational_operators
110
* @see https://reactivex.io/documentation/operators/interval.html
111
*/
112
template
<schedulers::constra
int
::scheduler TScheduler>
113
auto
interval
(rpp::schedulers::duration period, TScheduler&& scheduler)
114
{
115
return
interval
(period, period, std::forward<TScheduler>(scheduler));
116
}
117
}
// namespace rpp::source
rpp::details::observer_impl::on_next
void on_next(const Type &v) const noexcept
Observable calls this method to notify observer about new value.
Definition
observer.hpp:84
rpp::observable
Base class for any observable used in RPP. It handles core callbacks of observable.
Definition
observable.hpp:38
rpp::observer
Base class for any observer used in RPP. It handles core callbacks of observers. Objects of this clas...
Definition
observer.hpp:172
rpp::source::interval
auto interval(rpp::schedulers::duration initial, rpp::schedulers::duration period, TScheduler &&scheduler)
Creates rpp::observable that emits a sequential integer every specified time interval,...
Definition
interval.hpp:72
rpp::details::interval_schedulable
Definition
interval.hpp:20
rpp::details::interval_strategy
Definition
interval.hpp:30
rpp::details::observables::fixed_disposables_strategy
Definition
disposables_strategy.hpp:29
src
rpp
rpp
sources
interval.hpp
Generated by
1.17.0