LeviLamina
Loading...
Searching...
No Matches
SleepAwaiter.h
1#pragma once
2
3#include <coroutine>
4
5#include "ll/api/coro/Executor.h"
6
7namespace ll::coro {
9 ExecutorRef exec;
10 Duration dur;
11
12public:
13 template <class R, class P>
14 constexpr SleepAwaiter(std::chrono::duration<R, P> dur, ExecutorRef exec = nullptr)
15 : exec(exec),
16 dur(std::chrono::duration_cast<Duration>(dur)) {}
17
18 template <class C, class D>
19 constexpr SleepAwaiter(std::chrono::time_point<C, D> time, ExecutorRef exec = nullptr)
20 : SleepAwaiter(time - C::now(), exec) {}
21
22 constexpr void setExecutor(ExecutorRef ex) { exec = ex; }
23
24 constexpr bool await_ready() const noexcept { return dur <= Duration{0}; }
25 void await_suspend(std::coroutine_handle<> handle) { exec->executeAfter(handle, dur); }
26 constexpr void await_resume() const noexcept {}
27};
28} // namespace ll::coro
Definition SleepAwaiter.h:8
Definition optional_ref.h:10