| 1 | /************************************************************************************ |
| 2 | * |
| 3 | * D++, A Lightweight C++ library for Discord |
| 4 | * |
| 5 | * Copyright 2022 Craig Edwards and D++ contributors |
| 6 | * (https://github.com/brainboxdotcc/DPP/graphs/contributors) |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | * |
| 20 | ************************************************************************************/ |
| 21 | |
| 22 | #pragma once |
| 23 | |
| 24 | #include <dpp/export.h> |
| 25 | |
| 26 | #ifndef DPP_NO_CORO |
| 27 | |
| 28 | #if (defined(_LIBCPP_VERSION) and !defined(__cpp_impl_coroutine)) // if libc++ experimental implementation (LLVM < 14) |
| 29 | # define STDCORO_EXPERIMENTAL_HEADER |
| 30 | # define STDCORO_EXPERIMENTAL_NAMESPACE |
| 31 | #endif |
| 32 | |
| 33 | #ifdef STDCORO_GLIBCXX_COMPAT |
| 34 | # define __cpp_impl_coroutine 1 |
| 35 | namespace std { |
| 36 | namespace experimental { |
| 37 | using namespace std; |
| 38 | } |
| 39 | } |
| 40 | #endif |
| 41 | |
| 42 | #ifdef STDCORO_EXPERIMENTAL_HEADER |
| 43 | # include <experimental/coroutine> |
| 44 | #else |
| 45 | # include <coroutine> |
| 46 | #endif |
| 47 | |
| 48 | namespace dpp { |
| 49 | |
| 50 | /** |
| 51 | * @brief Implementation details for internal use only. |
| 52 | * |
| 53 | * @attention This is only meant to be used by D++ internally. Support will not be given regarding the facilities in this namespace. |
| 54 | */ |
| 55 | namespace detail { |
| 56 | #ifdef _DOXYGEN_ |
| 57 | /** |
| 58 | * @brief Alias for either std or std::experimental depending on compiler and library. Used by coroutine implementation. |
| 59 | * |
| 60 | * @todo Remove and use std when all supported libraries have coroutines in it |
| 61 | */ |
| 62 | namespace std_coroutine {} |
| 63 | #else |
| 64 | # ifdef STDCORO_EXPERIMENTAL_NAMESPACE |
| 65 | namespace std_coroutine = std::experimental; |
| 66 | # else |
| 67 | namespace std_coroutine = std; |
| 68 | # endif |
| 69 | #endif |
| 70 | |
| 71 | #ifndef _DOXYGEN_ |
| 72 | /** |
| 73 | * @brief Concept to check if a type has a useable `operator co_await()` member |
| 74 | */ |
| 75 | template <typename T> |
| 76 | concept has_co_await_member = requires (T expr) { expr.operator co_await(); }; |
| 77 | |
| 78 | /** |
| 79 | * @brief Concept to check if a type has a useable overload of the free function `operator co_await(expr)` |
| 80 | */ |
| 81 | template <typename T> |
| 82 | concept has_free_co_await = requires (T expr) { operator co_await(expr); }; |
| 83 | |
| 84 | /** |
| 85 | * @brief Concept to check if a type has useable `await_ready()`, `await_suspend()` and `await_resume()` member functions. |
| 86 | */ |
| 87 | template <typename T> |
| 88 | concept has_await_members = requires (T expr) { expr.await_ready(); expr.await_suspend(); expr.await_resume(); }; |
| 89 | |
| 90 | /** |
| 91 | * @brief Mimics the compiler's behavior of using co_await. That is, it returns whichever works first, in order : `expr.operator co_await();` > `operator co_await(expr)` > `expr` |
| 92 | */ |
| 93 | template <typename T> |
| 94 | requires (has_co_await_member<T>) |
| 95 | decltype(auto) co_await_resolve(T&& expr) noexcept(noexcept(expr.operator co_await())) { |
| 96 | decltype(auto) awaiter = expr.operator co_await(); |
| 97 | return awaiter; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @brief Mimics the compiler's behavior of using co_await. That is, it returns whichever works first, in order : `expr.operator co_await();` > `operator co_await(expr)` > `expr` |
| 102 | */ |
| 103 | template <typename T> |
| 104 | requires (!has_co_await_member<T> && has_free_co_await<T>) |
| 105 | decltype(auto) co_await_resolve(T&& expr) noexcept(noexcept(operator co_await(expr))) { |
| 106 | decltype(auto) awaiter = operator co_await(static_cast<T&&>(expr)); |
| 107 | return awaiter; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @brief Mimics the compiler's behavior of using co_await. That is, it returns whichever works first, in order : `expr.operator co_await();` > `operator co_await(expr)` > `expr` |
| 112 | */ |
| 113 | template <typename T> |
| 114 | requires (!has_co_await_member<T> && !has_free_co_await<T>) |
| 115 | decltype(auto) co_await_resolve(T&& expr) noexcept { |
| 116 | return static_cast<T&&>(expr); |
| 117 | } |
| 118 | |
| 119 | #else |
| 120 | |
| 121 | /** |
| 122 | * @brief Concept to check if a type has a useable `operator co_await()` member |
| 123 | * |
| 124 | * @note This is actually a C++20 concept but Doxygen doesn't do well with them |
| 125 | */ |
| 126 | template <typename T> |
| 127 | inline constexpr bool has_co_await_member; |
| 128 | |
| 129 | /** |
| 130 | * @brief Concept to check if a type has a useable overload of the free function `operator co_await(expr)` |
| 131 | * |
| 132 | * @note This is actually a C++20 concept but Doxygen doesn't do well with them |
| 133 | */ |
| 134 | template <typename T> |
| 135 | inline constexpr bool has_free_co_await; |
| 136 | |
| 137 | /** |
| 138 | * @brief Concept to check if a type has useable `await_ready()`, `await_suspend()` and `await_resume()` member functions. |
| 139 | * |
| 140 | * @note This is actually a C++20 concept but Doxygen doesn't do well with them |
| 141 | */ |
| 142 | template <typename T> |
| 143 | inline constexpr bool has_await_members; |
| 144 | |
| 145 | /** |
| 146 | * @brief Concept to check if a type can be used with co_await |
| 147 | * |
| 148 | * @note This is actually a C++20 concept but Doxygen doesn't do well with them |
| 149 | */ |
| 150 | template <typename T> |
| 151 | inline constexpr bool awaitable_type; |
| 152 | |
| 153 | /** |
| 154 | * @brief Mimics the compiler's behavior of using co_await. That is, it returns whichever works first, in order : `expr.operator co_await();` > `operator co_await(expr)` > `expr` |
| 155 | * |
| 156 | * This function is conditionally noexcept, if the returned expression also is. |
| 157 | */ |
| 158 | decltype(auto) co_await_resolve(auto&& expr) {} |
| 159 | |
| 160 | #endif |
| 161 | |
| 162 | /** |
| 163 | * @brief Convenience alias for the result of a certain awaitable's await_resume. |
| 164 | */ |
| 165 | template <typename T> |
| 166 | using awaitable_result = decltype(co_await_resolve(std::declval<T>()).await_resume()); |
| 167 | |
| 168 | } // namespace detail |
| 169 | |
| 170 | /** |
| 171 | * @brief Concept to check if a type can be used with co_await |
| 172 | */ |
| 173 | template <typename T> |
| 174 | concept awaitable_type = requires (T expr) { detail::co_await_resolve(expr).await_ready(); }; |
| 175 | |
| 176 | struct confirmation_callback_t; |
| 177 | |
| 178 | template <typename R = confirmation_callback_t> |
| 179 | class async; |
| 180 | |
| 181 | template <typename R = void> |
| 182 | #ifndef _DOXYGEN_ |
| 183 | requires (!std::is_reference_v<R>) |
| 184 | #endif |
| 185 | class task; |
| 186 | |
| 187 | template <typename R = void> |
| 188 | class coroutine; |
| 189 | |
| 190 | struct job; |
| 191 | |
| 192 | #ifdef DPP_CORO_TEST |
| 193 | /** |
| 194 | * @brief Allocation count of a certain type, for testing purposes. |
| 195 | * |
| 196 | * @todo Remove when coro is stable |
| 197 | */ |
| 198 | template <typename T> |
| 199 | inline int coro_alloc_count = 0; |
| 200 | #endif |
| 201 | |
| 202 | } // namespace dpp |
| 203 | |
| 204 | #endif /* DPP_NO_CORO */ |
| 205 |