| 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 | #pragma once |
| 22 | |
| 23 | #include <dpp/utility.h> |
| 24 | |
| 25 | #include <dpp/coro/awaitable.h> |
| 26 | |
| 27 | namespace dpp { |
| 28 | |
| 29 | struct async_dummy : awaitable_dummy { |
| 30 | std::shared_ptr<int> dummy_shared_state = nullptr; |
| 31 | }; |
| 32 | |
| 33 | } |
| 34 | |
| 35 | #ifndef DPP_NO_CORO |
| 36 | |
| 37 | #include "coro.h" |
| 38 | |
| 39 | #include <utility> |
| 40 | #include <type_traits> |
| 41 | #include <functional> |
| 42 | #include <atomic> |
| 43 | #include <cstddef> |
| 44 | |
| 45 | namespace dpp { |
| 46 | |
| 47 | namespace detail { |
| 48 | |
| 49 | namespace async { |
| 50 | |
| 51 | /** |
| 52 | * @brief Shared state of the async and its callback, to be used across threads. |
| 53 | */ |
| 54 | template <typename R> |
| 55 | struct callback { |
| 56 | /** |
| 57 | * @brief Promise object to set the result into |
| 58 | */ |
| 59 | std::shared_ptr<basic_promise<R>> promise{nullptr}; |
| 60 | |
| 61 | /** |
| 62 | * @brief Call operator, sets the value in the promise and notifies any awaiter |
| 63 | * |
| 64 | * @param v Callback value |
| 65 | */ |
| 66 | template <typename U = R> |
| 67 | void operator()(const U& v) const requires (std::convertible_to<const U&, R>) { |
| 68 | promise->set_value(v); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @brief Call operator, sets the value in the promise and notifies any awaiter |
| 73 | * |
| 74 | * @param v Callback value |
| 75 | */ |
| 76 | template <typename U = R> |
| 77 | void operator()(U&& v) const requires (std::convertible_to<U&&, R>) { |
| 78 | promise->set_value(std::move(v)); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @brief Call operator, sets the value in the promise and notifies any awaiter |
| 83 | */ |
| 84 | void operator()() const requires (std::is_void_v<R>) |
| 85 | { |
| 86 | promise->set_value(); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | } // namespace async |
| 91 | |
| 92 | } // namespace detail |
| 93 | |
| 94 | struct confirmation_callback_t; |
| 95 | |
| 96 | /** |
| 97 | * @class async async.h coro/async.h |
| 98 | * @brief A co_await-able object handling an API call in parallel with the caller. |
| 99 | * |
| 100 | * This class is the return type of the dpp::cluster::co_* methods, but it can also be created manually to wrap any async call. |
| 101 | * |
| 102 | * @remark - The coroutine may be resumed in another thread, do not rely on thread_local variables. |
| 103 | * @warning - This feature is EXPERIMENTAL. The API may change at any time and there may be bugs. Please report any to <a href="https://github.com/brainboxdotcc/DPP/issues">GitHub issues</a> or to the <a href="https://discord.gg/dpp">D++ Discord server</a>. |
| 104 | * @tparam R The return type of the API call. Defaults to confirmation_callback_t |
| 105 | */ |
| 106 | template <typename R> |
| 107 | class async : public awaitable<R> { |
| 108 | /** |
| 109 | * @brief Callable object to pass to API calls |
| 110 | */ |
| 111 | detail::async::callback<R> api_callback{}; |
| 112 | |
| 113 | /** |
| 114 | * @brief Internal promise constructor, grabs a promise object for the callback to use |
| 115 | */ |
| 116 | explicit async(std::shared_ptr<basic_promise<R>> &&promise) : awaitable<R>{promise.get()}, api_callback{std::move(promise)} {} |
| 117 | |
| 118 | public: |
| 119 | using awaitable<R>::awaitable; // use awaitable's constructors |
| 120 | using awaitable<R>::operator=; // use async_base's assignment operator |
| 121 | using awaitable<R>::await_ready; // expose await_ready as public |
| 122 | |
| 123 | /** |
| 124 | * @brief The return type of the API call. Defaults to confirmation_callback_t |
| 125 | */ |
| 126 | using result_type = R; |
| 127 | |
| 128 | /** |
| 129 | * @brief Construct an async object wrapping an object method, the call is made immediately by forwarding to <a href="https://en.cppreference.com/w/cpp/utility/functional/invoke">std::invoke</a> and can be awaited later to retrieve the result. |
| 130 | * |
| 131 | * @param obj The object to call the method on |
| 132 | * @param fun The method of the object to call. Its last parameter must be a callback taking a parameter of type R |
| 133 | * @param args Parameters to pass to the method, excluding the callback |
| 134 | */ |
| 135 | template <typename Obj, typename Fun, typename... Args> |
| 136 | #ifndef _DOXYGEN_ |
| 137 | requires std::invocable<Fun, Obj, Args..., std::function<void(R)>> |
| 138 | #endif |
| 139 | explicit async(Obj &&obj, Fun &&fun, Args&&... args) : async{std::make_shared<basic_promise<R>>()} { |
| 140 | std::invoke(std::forward<Fun>(fun), std::forward<Obj>(obj), std::forward<Args>(args)..., api_callback); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @brief Construct an async object wrapping an invokeable object, the call is made immediately by forwarding to <a href="https://en.cppreference.com/w/cpp/utility/functional/invoke">std::invoke</a> and can be awaited later to retrieve the result. |
| 145 | * |
| 146 | * @param fun The object to call using <a href="https://en.cppreference.com/w/cpp/utility/functional/invoke">std::invoke</a>. Its last parameter must be a callable taking a parameter of type R |
| 147 | * @param args Parameters to pass to the object, excluding the callback |
| 148 | */ |
| 149 | template <typename Fun, typename... Args> |
| 150 | #ifndef _DOXYGEN_ |
| 151 | requires std::invocable<Fun, Args..., std::function<void(R)>> |
| 152 | #endif |
| 153 | explicit async(Fun &&fun, Args&&... args) : async{std::make_shared<basic_promise<R>>()} { |
| 154 | std::invoke(std::forward<Fun>(fun), std::forward<Args>(args)..., api_callback); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @brief Copy constructor is disabled. |
| 159 | */ |
| 160 | async(const async&) = delete; |
| 161 | |
| 162 | /** |
| 163 | * @brief Move constructor, moves the awaitable async object |
| 164 | */ |
| 165 | async(async&&) = default; |
| 166 | |
| 167 | /** |
| 168 | * @brief Copy assignment operator is disabled. |
| 169 | */ |
| 170 | async& operator=(const async&) = delete; |
| 171 | |
| 172 | /** |
| 173 | * @brief Move assignment operator, moves the awaitable async object |
| 174 | */ |
| 175 | async& operator=(async&&) = default; |
| 176 | |
| 177 | /** |
| 178 | * @brief Destructor, signals to the callback that the async object is gone and shouldn't be notified of the result |
| 179 | */ |
| 180 | ~async() { |
| 181 | this->abandon(); |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | DPP_CHECK_ABI_COMPAT(async<>, async_dummy); |
| 186 | |
| 187 | } |
| 188 | |
| 189 | #endif /* DPP_NO_CORO */ |