@@ -10,6 +10,30 @@ export namespace CppUtils::Thread
1010{
1111 class ScheduledEventDispatcher final
1212 {
13+ private:
14+ template<String::Hasher eventName, class... Args>
15+ struct EmitTask final
16+ {
17+ ScheduledEventDispatcher* self;
18+ std::tuple<Args...> payload;
19+
20+ private:
21+ struct EmitCaller final
22+ {
23+ Execution::EventDispatcher& dispatcher;
24+ inline auto operator()(const Args&... args) const -> void
25+ {
26+ dispatcher.emit<eventName>(args...);
27+ }
28+ };
29+
30+ public:
31+ inline auto operator()() -> void
32+ {
33+ std::apply(EmitCaller{self->m_eventDispatcher}, payload);
34+ }
35+ };
36+
1337 public:
1438 using Clock = std::chrono::steady_clock;
1539 using TimePoint = Clock::time_point;
@@ -31,21 +55,13 @@ export namespace CppUtils::Thread
3155 template<String::Hasher eventName = Type::Hash{}, class... Args, Chrono::Duration Delay = std::chrono::milliseconds>
3256 inline auto emit(Delay delay, Args&&... args) -> void
3357 {
34- m_scheduler.schedule ([this , payload = std::make_tuple (std::forward<Args>(args)...)] {
35- std::apply ([this ](const auto &... args) {
36- m_eventDispatcher.emit <eventName>(args...);
37- }, payload);
38- }, delay);
58+ m_scheduler.schedule(EmitTask<eventName, std::decay_t<Args>...>{this, std::make_tuple(std::forward<Args>(args)...)}, delay);
3959 }
4060
4161 template<String::Hasher eventName = Type::Hash{}, class... Args>
4262 inline auto emit(TimePoint when, Args&&... args) -> void
4363 {
44- m_scheduler.schedule ([this , payload = std::make_tuple (std::forward<Args>(args)...)] {
45- std::apply ([this ](const auto &... args) {
46- m_eventDispatcher.emit <eventName>(args...);
47- }, payload);
48- }, when);
64+ m_scheduler.schedule(EmitTask<eventName, std::decay_t<Args>...>{this, std::make_tuple(std::forward<Args>(args)...)}, when);
4965 }
5066
5167 template<String::Hasher eventName = Type::Hash{}, class... Args>
0 commit comments