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/module.hpp"
8#include <coroutine>
9#include <type_traits>
10
11namespace hi::inline v1 {
12
18template<typename T>
19concept awaitable_direct = requires(T a, std::coroutine_handle<> b)
20{
21 // clang-format off
22 { a.await_ready() } -> std::convertible_to<bool>;
23 a.await_suspend(b);
24 a.await_resume();
25 // clang-format on
26};
27
32template<typename T>
33concept awaitable_member = requires(T a)
34{
35 a.operator co_await();
36};
37
42template<typename T>
43concept awaitable_non_member = requires(T a)
44{
45 operator co_await(static_cast<T&&>(a));
46};
47
56template<typename T>
58 using type = void;
59};
60
65template<awaitable_direct T>
66struct awaitable_cast<T> {
67 using type = std::decay_t<T>;
68
69 [[nodiscard]] type operator()(auto&& rhs) const noexcept
70 {
71 return hi_forward(rhs);
72 }
73};
74
79template<awaitable_member T>
80struct awaitable_cast<T> {
81 using type = std::decay_t<decltype(std::declval<T>().operator co_await())>;
82
83 [[nodiscard]] type operator()(auto&& rhs) const noexcept
84 {
85 return hi_forward(rhs).operator co_await();
86 }
87};
88
93template<awaitable_non_member T>
94struct awaitable_cast<T> {
95 using type = std::decay_t<decltype(operator co_await(std::declval<T>()))>;
96
97 [[nodiscard]] type operator()(auto&& rhs) const noexcept
98 {
99 return operator co_await(hi_forward(rhs));
100 }
101};
102
107template<typename T>
108using awaitable_cast_t = hi_typename awaitable_cast<T>::type;
109
118template<typename T>
119concept awaitable = not std::is_same_v<awaitable_cast_t<T>, void>;
120
125template<typename T>
127 using type = decltype(std::declval<T>().await_resume());
128};
129
134template<typename T>
135using await_resume_result_t = hi_typename await_resume_result<T>::type;
136
137}
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
DOXYGEN BUG.
Definition algorithm.hpp:13
hi_typename await_resume_result< T >::type await_resume_result_t
Get the result type of an awaitable.
Definition awaitable.hpp:135
hi_typename awaitable_cast< T >::type awaitable_cast_t
Resolve the type that is directly-awaitable.
Definition awaitable.hpp:108
A functor for casting a type to an awaitable.
Definition awaitable.hpp:57
Get the result type of an awaitable.
Definition awaitable.hpp:126
Check if type can be directly co_await on.
Definition awaitable.hpp:19
Check if type can be indirectly co_await on.
Definition awaitable.hpp:33
Check if type can be indirectly co_await on.
Definition awaitable.hpp:43
Check if the type can be co_awaited on after conversion with awaitable_cast.
Definition awaitable.hpp:119