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
12hi_export_module(hikogui.dispatch.awaitable);
13
14hi_export namespace hi::inline v1 {
15
21template<typename T>
22concept awaitable = requires(std::remove_reference_t<T>& a, std::coroutine_handle<> b) {
23 // clang-format off
24 { a.await_ready() } -> std::convertible_to<bool>;
25 a.await_suspend(b);
26 a.await_resume();
27 // clang-format on
28};
29
30template<typename T>
31concept awaitable_with_co_await_member = requires(std::remove_reference_t<T>& a) {
32 { a.operator co_await() } -> awaitable;
33};
34
35template<typename T>
36concept awaitable_with_co_await_free_function = requires(std::remove_reference_t<T>& a) {
37 { operator co_await(a) } -> awaitable;
38};
39
40template<typename T>
42
43template<awaitable T>
44struct awaitable_cast<T> {
45 template<typename RHS>
46 decltype(auto) operator()(RHS&& rhs) const noexcept
47 {
48 return std::forward<RHS>(rhs);
49 }
50};
51
52template<awaitable_with_co_await_member T>
53struct awaitable_cast<T> {
54 template<typename RHS>
55 decltype(auto) operator()(RHS&& rhs) const noexcept
56 {
57 return std::forward<RHS>(rhs).operator co_await();
58 }
59};
60
61template<awaitable_with_co_await_free_function T>
62struct awaitable_cast<T> {
63 template<typename RHS>
64 decltype(auto) operator()(RHS&& rhs) const noexcept
65 {
66 return operator co_await(std::forward<RHS>(rhs));
67 }
68};
69
72template<typename T>
73concept convertible_to_awaitable = requires(std::remove_reference_t<T>& rhs) { awaitable_cast<std::remove_cvref_t<T>>{}(rhs); };
74
79template<awaitable T>
81 using type = decltype(std::declval<T>().await_resume());
82};
83
88template<awaitable T>
89using await_resume_result_t = await_resume_result<T>::type;
90
91} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
await_resume_result< T >::type await_resume_result_t
Get the result type of an awaitable.
Definition awaitable.hpp:89
Definition awaitable.hpp:41
Get the result type of an awaitable.
Definition awaitable.hpp:80
Check if type can be directly co_await on.
Definition awaitable.hpp:22
Definition awaitable.hpp:31
Check if type can be casted with awaitable_cast to an awaitable.
Definition awaitable.hpp:73