HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
awaitable.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 "../utility/utility.hpp"
8#include "../macros.hpp"
9#include <coroutine>
10#include <type_traits>
11
12
13
14namespace hi::inline v1 {
15
21template<typename T>
22concept awaitable_direct = requires(T a, std::coroutine_handle<> b)
23{
24 // clang-format off
25 { a.await_ready() } -> std::convertible_to<bool>;
26 a.await_suspend(b);
27 a.await_resume();
28 // clang-format on
29};
30
35template<typename T>
36concept awaitable_member = requires(T a)
37{
38 a.operator co_await();
39};
40
45template<typename T>
46concept awaitable_non_member = requires(T a)
47{
48 operator co_await(static_cast<T&&>(a));
49};
50
59template<typename T>
61 using type = void;
62};
63
68template<awaitable_direct T>
69struct awaitable_cast<T> {
70 using type = std::decay_t<T>;
71
72 [[nodiscard]] type operator()(auto&& rhs) const noexcept
73 {
74 return hi_forward(rhs);
75 }
76};
77
82template<awaitable_member T>
83struct awaitable_cast<T> {
84 using type = std::decay_t<decltype(std::declval<T>().operator co_await())>;
85
86 [[nodiscard]] type operator()(auto&& rhs) const noexcept
87 {
88 return hi_forward(rhs).operator co_await();
89 }
90};
91
96template<awaitable_non_member T>
97struct awaitable_cast<T> {
98 using type = std::decay_t<decltype(operator co_await(std::declval<T>()))>;
99
100 [[nodiscard]] type operator()(auto&& rhs) const noexcept
101 {
102 return operator co_await(hi_forward(rhs));
103 }
104};
105
110template<typename T>
111using awaitable_cast_t = awaitable_cast<T>::type;
112
121template<typename T>
122concept awaitable = not std::is_same_v<awaitable_cast_t<T>, void>;
123
128template<typename T>
130 using type = decltype(std::declval<T>().await_resume());
131};
132
137template<typename T>
138using await_resume_result_t = await_resume_result<T>::type;
139
140}
DOXYGEN BUG.
Definition algorithm.hpp:16
awaitable_cast< T >::type awaitable_cast_t
Resolve the type that is directly-awaitable.
Definition awaitable.hpp:111
await_resume_result< T >::type await_resume_result_t
Get the result type of an awaitable.
Definition awaitable.hpp:138
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
Get the result type of an awaitable.
Definition awaitable.hpp:129
Check if type can be directly co_await on.
Definition awaitable.hpp:22
Check if type can be indirectly co_await on.
Definition awaitable.hpp:36
Check if type can be indirectly co_await on.
Definition awaitable.hpp:46
Check if the type can be co_awaited on after conversion with awaitable_cast.
Definition awaitable.hpp:122