ReactivePlusPlus
ReactiveX implementation for C++20
Loading...
Searching...
No Matches
worker.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
11#pragma once
12
13#include <rpp/schedulers/fwd.hpp>
14
15#include <rpp/defs.hpp>
16#include <rpp/disposables/disposable_wrapper.hpp>
17#include <rpp/utils/constraints.hpp>
18
19namespace rpp::schedulers
20{
21 template<rpp::schedulers::constraint::strategy Strategy>
22 class worker
23 {
24 public:
25 template<typename... Args>
27 explicit worker(Args&&... args)
28 : m_strategy(std::forward<Args>(args)...)
29 {
30 }
31
32 worker(const worker&) = default;
33 worker(worker&&) noexcept = default;
34
35 template<rpp::schedulers::constraint::schedulable_handler Handler, typename... Args, constraint::schedulable_fn<Handler, Args...> Fn>
36 void schedule(Fn&& fn, Handler&& handler, Args&&... args) const
37 {
38 schedule(duration{}, std::forward<Fn>(fn), std::forward<Handler>(handler), std::forward<Args>(args)...);
39 }
40
41 template<rpp::schedulers::constraint::schedulable_handler Handler, typename... Args, constraint::schedulable_fn<Handler, Args...> Fn>
42 void schedule(const duration delay, Fn&& fn, Handler&& handler, Args&&... args) const
43 {
44 if constexpr (constraint::defer_for_strategy<Strategy>)
45 m_strategy.defer_for(delay, std::forward<Fn>(fn), std::forward<Handler>(handler), std::forward<Args>(args)...);
46 else
47 schedule(now() + delay, std::forward<Fn>(fn), std::forward<Handler>(handler), std::forward<Args>(args)...);
48 }
49
50 template<rpp::schedulers::constraint::schedulable_handler Handler, typename... Args, constraint::schedulable_fn<Handler, Args...> Fn>
51 void schedule(const time_point tp, Fn&& fn, Handler&& handler, Args&&... args) const
52 {
53 if constexpr (constraint::defer_to_strategy<Strategy>)
54 m_strategy.defer_to(tp, std::forward<Fn>(fn), std::forward<Handler>(handler), std::forward<Args>(args)...);
55 else
56 schedule(tp - now(), std::forward<Fn>(fn), std::forward<Handler>(handler), std::forward<Args>(args)...);
57 }
58
59 static rpp::schedulers::time_point now() { return Strategy::now(); }
60
61 private:
62 RPP_NO_UNIQUE_ADDRESS Strategy m_strategy;
63 };
64} // namespace rpp::schedulers
Definition constraints.hpp:43
Definition constraints.hpp:31