libcoro  1.0
Coroutine support library for C++20
common.h
Go to the documentation of this file.
1 
3 #pragma once
4 
5 #include <coroutine>
6 #include <atomic>
7 #include <concepts>
8 #include <bit>
9 
60 namespace coro {
61 
63 template<typename T>
64 concept await_suspend_valid_return_value = (std::is_void_v<T>||std::is_convertible_v<T, bool>||std::is_convertible_v<T, std::coroutine_handle<> >);
65 
66 template<typename T>
67 concept directly_awaitable = requires(T v, std::coroutine_handle<> h) {
68  {v.await_ready()}->std::convertible_to<bool>;
69  {v.await_suspend(h)}->await_suspend_valid_return_value;
70  {v.await_resume()};
71 };
72 
73 template<typename T>
74 concept indirectly_awaitable = requires(T v) {
75  {v.operator co_await()} -> directly_awaitable;
76 };
77 
79 template<typename T>
80 concept awaitable = directly_awaitable<T> || indirectly_awaitable<T>;
81 
82 template<typename T, typename RetVal>
83 concept directly_awaitable_r = requires(T v, std::coroutine_handle<> h) {
84  {v.await_ready()}->std::convertible_to<bool>;
85  {v.await_suspend(h)}->await_suspend_valid_return_value;
86  {v.await_resume()}->std::convertible_to<RetVal>;
87 };
88 
89 template<typename T, typename RetVal>
90 concept indirectly_awaitable_r = requires(T v) {
91  {v.operator co_await()} -> directly_awaitable_r<RetVal>;
92 };
93 
94 template<typename T, typename RetVal>
95 concept awaitable_r = directly_awaitable_r<T,RetVal> || indirectly_awaitable_r<T,RetVal>;
96 
97 template<typename T>
98 struct awaitable_result_impl;
99 
100 template<directly_awaitable T>
101 struct awaitable_result_impl<T> {
102  using type = decltype(std::declval<T>().await_resume());
103 };
104 
105 template<indirectly_awaitable T>
106 struct awaitable_result_impl<T> {
107  using type = typename awaitable_result_impl<decltype(std::declval<T>().operator co_await())>::type;
108 };
109 
110 template<awaitable T>
111 using awaitable_result = typename awaitable_result_impl<T>::type;
112 
113 
114 #ifdef __clang__
115 #define CORO_OPT_BARRIER [[clang::optnone]]
116 #else
118 
125 #define CORO_OPT_BARRIER
126 #endif
127 
128 class ident_t {
129 public:
130  ident_t() = default;
131  template<typename X>
132  ident_t(std::coroutine_handle<X> h):_addr(h.address()) {}
133  bool operator==(const ident_t &other) const = default;
134  bool operator<=>(const ident_t &other) const = default;
135 
136  friend std::size_t hash(const ident_t &other) {
137  return std::bit_cast<std::size_t>(other._addr);
138  }
139 
140  const void *address() const {return _addr;}
141 
142 protected:
143  const void *_addr = 0;
144 };
145 
146 
147 
148 
149 }
150 
151 
152 
153 
concept awaitable
Tests, whether T is coroutine awaitable.
Definition: common.h:80
concept await_suspend_valid_return_value
Tests, whether T is valid await_suspend return value.
Definition: common.h:64
main namespace
Definition: aggregator.h:8