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 "awaitable.hpp"
8#include "../concurrency/concurrency.hpp"
9#include "../time/time.hpp"
10#include "../macros.hpp"
11#include <chrono>
12#include <coroutine>
13#include <functional>
14
15hi_export_module(hikogui.dispatch : awaitable_timer_intf);
16
17hi_export namespace hi::inline v1 {
18
20public:
21 awaitable_timer(awaitable_timer &&) noexcept = default;
22 awaitable_timer &operator=(awaitable_timer &&) noexcept = default;
23
24 awaitable_timer(awaitable_timer const &other) noexcept : _deadline(other._deadline) {}
25
26 awaitable_timer &operator=(awaitable_timer const &other) noexcept
27 {
28 _callback = nullptr;
29 _deadline = other._deadline;
30 return *this;
31 }
32
33 template<typename Duration>
35 {
36 }
37
38 template<typename Rep, typename Period>
39 awaitable_timer(std::chrono::duration<Rep, Period> period) noexcept : awaitable_timer(std::chrono::utc_clock::now() + period)
40 {
41 }
42
43 [[nodiscard]] bool await_ready() const noexcept
44 {
45 return std::chrono::utc_clock::now() > _deadline;
46 }
47
48 void await_suspend(std::coroutine_handle<> handle) noexcept;
49
50 void await_resume() const noexcept {}
51
52private:
53 utc_nanoseconds _deadline;
54 callback<void()> _callback;
55};
56
57template<typename Rep, typename Period>
58struct awaitable_cast<std::chrono::duration<Rep, Period>> {
59 awaitable_timer operator()(std::chrono::duration<Rep, Period> const& rhs) const noexcept
60 {
61 return awaitable_timer{rhs};
62 }
63};
64
65template<typename Duration>
66struct awaitable_cast<std::chrono::time_point<std::chrono::utc_clock, Duration>> {
68 {
69 return awaitable_timer{rhs};
70 }
71};
72
73} // namespace hi::inline v1
STL namespace.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition awaitable.hpp:41
Definition awaitable_timer_intf.hpp:19