-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScheduledEventDispatcher.mpp
More file actions
97 lines (82 loc) · 2.57 KB
/
ScheduledEventDispatcher.mpp
File metadata and controls
97 lines (82 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
export module CppUtils.Thread.ScheduledEventDispatcher;
import std;
import CppUtils.Chrono.Concept;
import CppUtils.Execution.EventDispatcher;
import CppUtils.String.Hash;
import CppUtils.Thread.Scheduler;
export namespace CppUtils::Thread
{
class ScheduledEventDispatcher final
{
private:
template<String::Hasher eventName, class... Args>
struct EmitTask final
{
ScheduledEventDispatcher* self;
std::tuple<Args...> payload;
private:
struct EmitCaller final
{
Execution::EventDispatcher& dispatcher;
inline auto operator()(const Args&... args) const -> void
{
dispatcher.emit<eventName>(args...);
}
};
public:
inline auto operator()() -> void
{
std::apply(EmitCaller{self->m_eventDispatcher}, payload);
}
};
public:
using Clock = std::chrono::steady_clock;
using TimePoint = Clock::time_point;
explicit ScheduledEventDispatcher(
Scheduler::Clock::duration step = std::chrono::milliseconds(10),
std::size_t numberThreads = std::thread::hardware_concurrency(),
std::function<void(std::exception_ptr)> onError = nullptr,
std::function<void()> finally = nullptr):
m_scheduler{step, numberThreads, std::move(onError), std::move(finally)}
{}
template<String::Hasher... eventNames>
inline auto subscribe(auto&& function) -> void
{
m_eventDispatcher.subscribe<eventNames...>(std::forward<decltype(function)>(function));
}
template<String::Hasher eventName = Type::Hash{}, class... Args, Chrono::Duration Delay = std::chrono::milliseconds>
inline auto emit(Delay delay, Args&&... args) -> void
{
m_scheduler.schedule(EmitTask<eventName, std::decay_t<Args>...>{this, std::make_tuple(std::forward<Args>(args)...)}, delay);
}
template<String::Hasher eventName = Type::Hash{}, class... Args>
inline auto emit(TimePoint when, Args&&... args) -> void
{
m_scheduler.schedule(EmitTask<eventName, std::decay_t<Args>...>{this, std::make_tuple(std::forward<Args>(args)...)}, when);
}
template<String::Hasher eventName = Type::Hash{}, class... Args>
inline auto emit(Args&&... args) -> void
{
emit<eventName>(std::chrono::milliseconds{0}, std::forward<Args>(args)...);
}
inline auto waitUntilFinished() -> void
{
m_scheduler.waitUntilFinished();
}
inline auto flush() -> void
{
m_scheduler.flush();
}
[[nodiscard]] inline auto getScheduledTasksCount() const -> std::size_t
{
return m_scheduler.getScheduledTasksCount();
}
inline auto cancelAll() -> void
{
m_scheduler.cancelAll();
}
private:
Scheduler m_scheduler;
Execution::EventDispatcher m_eventDispatcher;
};
}