HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
awaitable_timer_intf.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 "../coroutine/module.hpp"
8#include "../time/module.hpp"
9#include "../macros.hpp"
10#include <chrono>
11#include <coroutine>
12#include <functional>
13
14hi_export_module(hikogui.dispatch.awaitable_timer : intf);
15
16namespace hi::inline v1 {
17
19public:
20 template<typename Duration>
21 awaitable_timer(std::chrono::time_point<std::chrono::utc_clock, Duration> deadline) noexcept : _deadline(deadline) {}
22
23 template<typename Rep, typename Period>
24 awaitable_timer(std::chrono::duration<Rep,Period> period) noexcept : awaitable_timer(std::chrono::utc_clock::now() + period) {}
25
26 [[nodiscard]] bool await_ready() const noexcept
27 {
28 return std::chrono::utc_clock::now() > _deadline;
29 }
30
31 void await_suspend(std::coroutine_handle<> handle) noexcept;
32
33 void await_resume() const noexcept {}
34
35private:
36 utc_nanoseconds _deadline;
37 std::shared_ptr<std::function<void()>> _token;
38};
39
44template<typename Rep, typename Period>
45struct awaitable_cast<std::chrono::duration<Rep, Period>> {
46 using type = awaitable_timer;
47
48 [[nodiscard]] type operator()(auto&& rhs) const noexcept
49 {
50 return awaitable_timer{hi_forward(rhs)};
51 }
52};
53
58template<typename Duration>
59struct awaitable_cast<std::chrono::time_point<std::chrono::utc_clock, Duration>> {
60 using type = awaitable_timer;
61
62 [[nodiscard]] type operator()(auto&& rhs) const noexcept
63 {
64 return awaitable_timer{hi_forward(rhs)};
65 }
66};
67
68} // namespace hi::inline v1
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A functor for casting a type to an awaitable.
Definition awaitable.hpp:60
Definition awaitable_timer_intf.hpp:18