Async++ unknown
Async (co_await/co_return) code for C++
Loading...
Searching...
No Matches
concepts.h
1#pragma once
3#include <functional>
4#include <type_traits>
5
6namespace asyncpp::detail {
8 template<typename T>
9 struct is_coroutine_handle : std::false_type {};
10
12 template<typename TPromise>
13 struct is_coroutine_handle<coroutine_handle<TPromise>> : std::true_type {};
14
16 template<typename T>
17 inline constexpr bool is_coroutine_handle_v = is_coroutine_handle<T>::value;
18
20 template<typename T>
21 concept is_valid_await_suspend_return_value =
22 std::is_void_v<T> || std::is_same_v<T, bool> || is_coroutine_handle_v<T>;
23
25 template<typename T>
26 concept is_awaiter = requires(T&& await) {
27 { await.await_ready() } -> std::convertible_to<bool>;
28 { await.await_suspend(std::declval<coroutine_handle<>>()) } -> is_valid_await_suspend_return_value<>;
29 { await.await_resume() };
30 };
31
36 template<typename T>
37 struct await_return_type;
38 template<bool b, typename T>
39 struct await_return_type_impl;
40
41 template<typename T>
42 struct await_return_type_impl<true, T> {
43 using type = decltype(std::declval<T>().await_resume());
44 };
45
46 template<typename T>
47 struct await_return_type_impl<false, T> {
48 using type = typename await_return_type<decltype(std::declval<T>().operator co_await())>::type;
49 };
50
51 template<typename T>
52 struct await_return_type {
53 using type = typename await_return_type_impl<is_awaiter<T>, T>::type;
54 };
55
56} // namespace asyncpp::detail
57
58namespace asyncpp {
60 template<typename T>
61 concept Dispatcher = requires(T&& disp) {
62 { disp.push(std::declval<std::function<void()>>) };
63 };
64
66 template<class Allocator>
67 concept ByteAllocator = requires(Allocator&& alloc) {
68 { std::allocator_traits<Allocator>::allocate(alloc, 0) } -> std::convertible_to<std::byte*>;
69 { std::allocator_traits<Allocator>::deallocate(alloc, std::declval<std::byte*>(), 0) };
70 };
71} // namespace asyncpp
Check if a type is a valid allocator providing std::byte allocations.
Definition concepts.h:67
Check if T implements the dispatcher interface.
Definition concepts.h:61
Provides a consistent import interface for coroutine, experimental/coroutine or a best effort fallbac...