coro.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // experimental/coro.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2021-2023 Klemens D. Morgenstern
  6. // (klemens dot morgenstern at gmx dot net)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef ASIO_EXPERIMENTAL_CORO_HPP
  12. #define ASIO_EXPERIMENTAL_CORO_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/config.hpp"
  17. #include "asio/dispatch.hpp"
  18. #include "asio/error.hpp"
  19. #include "asio/error_code.hpp"
  20. #include "asio/experimental/coro_traits.hpp"
  21. #include "asio/experimental/detail/coro_promise_allocator.hpp"
  22. #include "asio/experimental/detail/partial_promise.hpp"
  23. #include "asio/post.hpp"
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. namespace experimental {
  27. namespace detail {
  28. template <typename Signature, typename Return,
  29. typename Executor, typename Allocator>
  30. struct coro_promise;
  31. template <typename T, typename Coroutine>
  32. struct coro_with_arg;
  33. } // namespace detail
  34. /// The main type of a resumable coroutine.
  35. /**
  36. * Template parameter @c Yield specifies type or signature used by co_yield,
  37. * @c Return specifies the type used for co_return, and @c Executor specifies
  38. * the underlying executor type.
  39. */
  40. template <typename Yield = void, typename Return = void,
  41. typename Executor = any_io_executor,
  42. typename Allocator = std::allocator<void>>
  43. struct coro
  44. {
  45. /// The traits of the coroutine. See asio::experimental::coro_traits
  46. /// for details.
  47. using traits = coro_traits<Yield, Return, Executor>;
  48. /// The value that can be passed into a symmetrical cororoutine. @c void if
  49. /// asymmetrical.
  50. using input_type = typename traits::input_type;
  51. /// The type that can be passed out through a co_yield.
  52. using yield_type = typename traits::yield_type;
  53. /// The type that can be passed out through a co_return.
  54. using return_type = typename traits::return_type;
  55. /// The type received by a co_await or async_resume. Its a combination of
  56. /// yield and return.
  57. using result_type = typename traits::result_type;
  58. /// The signature used by the async_resume.
  59. using signature_type = typename traits::signature_type;
  60. /// Whether or not the coroutine is noexcept.
  61. constexpr static bool is_noexcept = traits::is_noexcept;
  62. /// The error type of the coroutine. Void for noexcept
  63. using error_type = typename traits::error_type;
  64. /// Completion handler type used by async_resume.
  65. using completion_handler = typename traits::completion_handler;
  66. /// The internal promise-type of the coroutine.
  67. using promise_type = detail::coro_promise<Yield, Return, Executor, Allocator>;
  68. #if !defined(GENERATING_DOCUMENTATION)
  69. template <typename T, typename Coroutine>
  70. friend struct detail::coro_with_arg;
  71. #endif // !defined(GENERATING_DOCUMENTATION)
  72. /// The executor type.
  73. using executor_type = Executor;
  74. /// The allocator type.
  75. using allocator_type = Allocator;
  76. #if !defined(GENERATING_DOCUMENTATION)
  77. friend struct detail::coro_promise<Yield, Return, Executor, Allocator>;
  78. #endif // !defined(GENERATING_DOCUMENTATION)
  79. /// The default constructor, gives an invalid coroutine.
  80. coro() = default;
  81. /// Move constructor.
  82. coro(coro&& lhs) noexcept
  83. : coro_(std::exchange(lhs.coro_, nullptr))
  84. {
  85. }
  86. coro(const coro&) = delete;
  87. /// Move assignment.
  88. coro& operator=(coro&& lhs) noexcept
  89. {
  90. std::swap(coro_, lhs.coro_);
  91. return *this;
  92. }
  93. coro& operator=(const coro&) = delete;
  94. /// Destructor. Destroys the coroutine, if it holds a valid one.
  95. /**
  96. * @note This does not cancel an active coroutine. Destructing a resumable
  97. * coroutine, i.e. one with a call to async_resume that has not completed, is
  98. * undefined behaviour.
  99. */
  100. ~coro()
  101. {
  102. if (coro_ != nullptr)
  103. {
  104. struct destroyer
  105. {
  106. detail::coroutine_handle<promise_type> handle;
  107. destroyer(const detail::coroutine_handle<promise_type>& handle)
  108. : handle(handle)
  109. { }
  110. destroyer(destroyer&& lhs)
  111. : handle(std::exchange(lhs.handle, nullptr))
  112. {
  113. }
  114. destroyer(const destroyer&) = delete;
  115. void operator()() {}
  116. ~destroyer()
  117. {
  118. if (handle)
  119. handle.destroy();
  120. }
  121. };
  122. auto handle =
  123. detail::coroutine_handle<promise_type>::from_promise(*coro_);
  124. if (handle)
  125. asio::dispatch(coro_->get_executor(), destroyer{handle});
  126. }
  127. }
  128. /// Get the used executor.
  129. executor_type get_executor() const
  130. {
  131. if (coro_)
  132. return coro_->get_executor();
  133. if constexpr (std::is_default_constructible_v<Executor>)
  134. return Executor{};
  135. else
  136. throw std::logic_error("Coroutine has no executor");
  137. }
  138. /// Get the used allocator.
  139. allocator_type get_allocator() const
  140. {
  141. if (coro_)
  142. return coro_->get_allocator();
  143. if constexpr (std::is_default_constructible_v<Allocator>)
  144. return Allocator{};
  145. else
  146. throw std::logic_error(
  147. "Coroutine has no available allocator without a constructed promise");
  148. }
  149. /// Resume the coroutine.
  150. /**
  151. * @param token The completion token of the async resume.
  152. *
  153. * @attention Calling an invalid coroutine with a noexcept signature is
  154. * undefined behaviour.
  155. *
  156. * @note This overload is only available for coroutines without an input
  157. * value.
  158. */
  159. template <typename CompletionToken>
  160. requires std::is_void_v<input_type>
  161. auto async_resume(CompletionToken&& token) &
  162. {
  163. return async_initiate<CompletionToken,
  164. typename traits::completion_handler>(
  165. initiate_async_resume(this), token);
  166. }
  167. /// Resume the coroutine.
  168. /**
  169. * @param token The completion token of the async resume.
  170. *
  171. * @attention Calling an invalid coroutine with a noexcept signature is
  172. * undefined behaviour.
  173. *
  174. * @note This overload is only available for coroutines with an input value.
  175. */
  176. template <typename CompletionToken, detail::convertible_to<input_type> T>
  177. auto async_resume(T&& ip, CompletionToken&& token) &
  178. {
  179. return async_initiate<CompletionToken,
  180. typename traits::completion_handler>(
  181. initiate_async_resume(this), token, std::forward<T>(ip));
  182. }
  183. /// Operator used for coroutines without input value.
  184. auto operator co_await() requires (std::is_void_v<input_type>)
  185. {
  186. return awaitable_t{*this};
  187. }
  188. /// Operator used for coroutines with input value.
  189. /**
  190. * @param ip The input value
  191. *
  192. * @returns An awaitable handle.
  193. *
  194. * @code
  195. * coro<void> push_values(coro<double(int)> c)
  196. * {
  197. * std::optional<double> res = co_await c(42);
  198. * }
  199. * @endcode
  200. */
  201. template <detail::convertible_to<input_type> T>
  202. auto operator()(T&& ip)
  203. {
  204. return detail::coro_with_arg<std::decay_t<T>, coro>{
  205. std::forward<T>(ip), *this};
  206. }
  207. /// Check whether the coroutine is open, i.e. can be resumed.
  208. bool is_open() const
  209. {
  210. if (coro_)
  211. {
  212. auto handle =
  213. detail::coroutine_handle<promise_type>::from_promise(*coro_);
  214. return handle && !handle.done();
  215. }
  216. else
  217. return false;
  218. }
  219. /// Check whether the coroutine is open, i.e. can be resumed.
  220. explicit operator bool() const { return is_open(); }
  221. private:
  222. struct awaitable_t;
  223. struct initiate_async_resume;
  224. explicit coro(promise_type* const cr) : coro_(cr) {}
  225. promise_type* coro_{nullptr};
  226. };
  227. /// A generator is a coro that returns void and yields value.
  228. template<typename T, typename Executor = asio::any_io_executor,
  229. typename Allocator = std::allocator<void>>
  230. using generator = coro<T, void, Executor, Allocator>;
  231. /// A task is a coro that does not yield values
  232. template<typename T, typename Executor = asio::any_io_executor,
  233. typename Allocator = std::allocator<void>>
  234. using task = coro<void(), T, Executor, Allocator>;
  235. } // namespace experimental
  236. } // namespace asio
  237. #include "asio/detail/pop_options.hpp"
  238. #include "asio/experimental/impl/coro.hpp"
  239. #endif // ASIO_EXPERIMENTAL_CORO_HPP