HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
src
hikogui
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
13
namespace
hi::inline
v1
{
14
15
class
awaitable_timer
{
16
public
:
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
32
private
:
33
utc_nanoseconds _deadline;
34
std::shared_ptr
<
std::function
<void()>> _token;
35
};
36
41
template
<
typename
Rep,
typename
Period>
42
struct
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
55
template
<
typename
Duration>
56
struct
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
hi_forward
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition
utility.hpp:29
std
STL namespace.
v1
DOXYGEN BUG.
Definition
algorithm.hpp:13
v1::awaitable_cast
A functor for casting a type to an awaitable.
Definition
awaitable.hpp:57
v1::awaitable_timer
Definition
awaitable_timer.hpp:15
std::chrono::duration
std::function
std::shared_ptr
std::chrono::time_point
Generated on Mon Apr 22 2024 12:52:19 for HikoGUI by
1.10.0