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 "required.hpp"
8#include "architecture.hpp"
9#include <coroutine>
10#include <type_traits>
11
12namespace hi::inline v1 {
13
19template<typename T>
20concept awaitable_direct = requires(T a, std::coroutine_handle<> b)
21{
22 // clang-format off
23 { a.await_ready() } -> std::convertible_to<bool>;
24 a.await_suspend(b);
25 a.await_resume();
26 // clang-format on
27};
28
33template<typename T>
34concept awaitable_member = requires(T a)
35{
36 a.operator co_await();
37};
38
43template<typename T>
44concept awaitable_non_member = requires(T a)
45{
46 operator co_await(static_cast<T&&>(a));
47};
48
57template<typename T>
59 using type = void;
60};
61
66template<awaitable_direct T>
67struct awaitable_cast<T> {
68 using type = std::decay_t<T>;
69
70 [[nodiscard]] type operator()(auto&& rhs) const noexcept
71 {
72 return hi_forward(rhs);
73 }
74};
75
80template<awaitable_member T>
81struct awaitable_cast<T> {
82 using type = std::decay_t<decltype(std::declval<T>().operator co_await())>;
83
84 [[nodiscard]] type operator()(auto&& rhs) const noexcept
85 {
86 return hi_forward(rhs).operator co_await();
87 }
88};
89
94template<awaitable_non_member T>
95struct awaitable_cast<T> {
96 using type = std::decay_t<decltype(operator co_await(std::declval<T>()))>;
97
98 [[nodiscard]] type operator()(auto&& rhs) const noexcept
99 {
100 return operator co_await(hi_forward(rhs));
101 }
102};
103
108template<typename T>
109using awaitable_cast_t = hi_typename awaitable_cast<T>::type;
110
119template<typename T>
120concept awaitable = not std::is_same_v<awaitable_cast_t<T>, void>;
121
126template<typename T>
128 using type = decltype(std::declval<T>().await_resume());
129};
130
135template<typename T>
136using await_resume_result_t = hi_typename await_resume_result<T>::type;
137
138}
This file includes required definitions.
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition required.hpp:29
Functions and macros for handling architectural difference between compilers, CPUs and operating syst...
A functor for casting a type to an awaitable.
Definition awaitable.hpp:58
Get the result type of an awaitable.
Definition awaitable.hpp:127
Check if type can be directly co_await on.
Definition awaitable.hpp:20
Check if type can be indirectly co_await on.
Definition awaitable.hpp:34
Check if type can be indirectly co_await on.
Definition awaitable.hpp:44
Check if the type can be co_awaited on after conversion with awaitable_cast.
Definition awaitable.hpp:120