HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
awaitable_timer.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "awaitable.hpp"
8#include "chrono.hpp"
9#include <chrono>
10#include <coroutine>
11#include <functional>
12
13namespace hi::inline v1 {
14
16public:
17 template<typename Duration>
18 awaitable_timer(std::chrono::time_point<std::chrono::utc_clock, Duration> deadline) noexcept : _deadline(deadline) {}
19
20 template<typename Rep, typename Period>
21 awaitable_timer(std::chrono::duration<Rep,Period> period) noexcept : awaitable_timer(std::chrono::utc_clock::now() + period) {}
22
23 [[nodiscard]] bool await_ready() const noexcept
24 {
25 return std::chrono::utc_clock::now() > _deadline;
26 }
27
28 void await_suspend(std::coroutine_handle<> handle) noexcept;
29
30 void await_resume() const noexcept {}
31
32private:
33 utc_nanoseconds _deadline;
34 std::shared_ptr<std::function<void()>> _token;
35};
36
41template<typename Rep, typename Period>
42struct awaitable_cast<std::chrono::duration<Rep, Period>> {
43 using type = awaitable_timer;
44
45 [[nodiscard]] type operator()(auto&& rhs) const noexcept
46 {
47 return awaitable_timer{hi_forward(rhs)};
48 }
49};
50
55template<typename Duration>
56struct awaitable_cast<std::chrono::time_point<std::chrono::utc_clock, Duration>> {
57 using type = awaitable_timer;
58
59 [[nodiscard]] type operator()(auto&& rhs) const noexcept
60 {
61 return awaitable_timer{hi_forward(rhs)};
62 }
63};
64
65} // namespace hi::inline v1
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition required.hpp:29
STL namespace.
A functor for casting a type to an awaitable.
Definition awaitable.hpp:58
Definition awaitable_timer.hpp:15