use_coro.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // experimental/impl/use_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_IMPL_USE_CORO_HPP
  12. #define ASIO_EXPERIMENTAL_IMPL_USE_CORO_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/deferred.hpp"
  17. #include "asio/experimental/coro.hpp"
  18. #include "asio/detail/push_options.hpp"
  19. namespace asio {
  20. #if !defined(GENERATING_DOCUMENTATION)
  21. template <typename Allocator, typename R>
  22. struct async_result<experimental::use_coro_t<Allocator>, R()>
  23. {
  24. template <typename Initiation, typename... InitArgs>
  25. static auto initiate_impl(Initiation initiation,
  26. std::allocator_arg_t, Allocator, InitArgs... args)
  27. -> experimental::coro<void() noexcept, void,
  28. asio::associated_executor_t<Initiation>, Allocator>
  29. {
  30. co_await deferred_async_operation<R(), Initiation, InitArgs...>(
  31. deferred_init_tag{}, std::move(initiation), std::move(args)...);
  32. }
  33. template <typename... InitArgs>
  34. static auto initiate_impl(asio::detail::initiation_archetype<R()>,
  35. std::allocator_arg_t, Allocator, InitArgs... args)
  36. -> experimental::coro<void(), void,
  37. asio::any_io_executor, Allocator>;
  38. template <typename Initiation, typename... InitArgs>
  39. static auto initiate(Initiation initiation,
  40. experimental::use_coro_t<Allocator> tk, InitArgs&&... args)
  41. {
  42. return initiate_impl(std::move(initiation), std::allocator_arg,
  43. tk.get_allocator(), std::forward<InitArgs>(args)...);
  44. }
  45. };
  46. template <typename Allocator, typename R>
  47. struct async_result<
  48. experimental::use_coro_t<Allocator>, R(asio::error_code)>
  49. {
  50. template <typename Initiation, typename... InitArgs>
  51. static auto initiate_impl(Initiation initiation,
  52. std::allocator_arg_t, Allocator, InitArgs... args)
  53. -> experimental::coro<void() noexcept, void,
  54. asio::associated_executor_t<Initiation>, Allocator>
  55. {
  56. co_await deferred_async_operation<
  57. R(asio::error_code), Initiation, InitArgs...>(
  58. deferred_init_tag{}, std::move(initiation), std::move(args)...);
  59. }
  60. template <typename... InitArgs>
  61. static auto initiate_impl(
  62. asio::detail::initiation_archetype<R(asio::error_code)>,
  63. std::allocator_arg_t, Allocator, InitArgs... args)
  64. -> experimental::coro<void(), void,
  65. asio::any_io_executor, Allocator>;
  66. template <typename Initiation, typename... InitArgs>
  67. static auto initiate(Initiation initiation,
  68. experimental::use_coro_t<Allocator> tk, InitArgs&&... args)
  69. {
  70. return initiate_impl(std::move(initiation), std::allocator_arg,
  71. tk.get_allocator(), std::forward<InitArgs>(args)...);
  72. }
  73. };
  74. template <typename Allocator, typename R>
  75. struct async_result<
  76. experimental::use_coro_t<Allocator>, R(std::exception_ptr)>
  77. {
  78. template <typename Initiation, typename... InitArgs>
  79. static auto initiate_impl(Initiation initiation,
  80. std::allocator_arg_t, Allocator, InitArgs... args)
  81. -> experimental::coro<void(), void,
  82. asio::associated_executor_t<Initiation>, Allocator>
  83. {
  84. co_await deferred_async_operation<
  85. R(std::exception_ptr), Initiation, InitArgs...>(
  86. deferred_init_tag{}, std::move(initiation), std::move(args)...);
  87. }
  88. template <typename... InitArgs>
  89. static auto initiate_impl(
  90. asio::detail::initiation_archetype<R(std::exception_ptr)>,
  91. std::allocator_arg_t, Allocator, InitArgs... args)
  92. -> experimental::coro<void(), void,
  93. asio::any_io_executor, Allocator>;
  94. template <typename Initiation, typename... InitArgs>
  95. static auto initiate(Initiation initiation,
  96. experimental::use_coro_t<Allocator> tk, InitArgs&&... args)
  97. {
  98. return initiate_impl(std::move(initiation), std::allocator_arg,
  99. tk.get_allocator(), std::forward<InitArgs>(args)...);
  100. }
  101. };
  102. template <typename Allocator, typename R, typename T>
  103. struct async_result<experimental::use_coro_t<Allocator>, R(T)>
  104. {
  105. template <typename Initiation, typename... InitArgs>
  106. static auto initiate_impl(Initiation initiation,
  107. std::allocator_arg_t, Allocator, InitArgs... args)
  108. -> experimental::coro<void() noexcept, T,
  109. asio::associated_executor_t<Initiation>, Allocator>
  110. {
  111. co_return co_await deferred_async_operation<R(T), Initiation, InitArgs...>(
  112. deferred_init_tag{}, std::move(initiation), std::move(args)...);
  113. }
  114. template <typename... InitArgs>
  115. static auto initiate_impl(asio::detail::initiation_archetype<R(T)>,
  116. std::allocator_arg_t, Allocator, InitArgs... args)
  117. -> experimental::coro<void() noexcept, T,
  118. asio::any_io_executor, Allocator>;
  119. template <typename Initiation, typename... InitArgs>
  120. static auto initiate(Initiation initiation,
  121. experimental::use_coro_t<Allocator> tk, InitArgs&&... args)
  122. {
  123. return initiate_impl(std::move(initiation), std::allocator_arg,
  124. tk.get_allocator(), std::forward<InitArgs>(args)...);
  125. }
  126. };
  127. template <typename Allocator, typename R, typename T>
  128. struct async_result<
  129. experimental::use_coro_t<Allocator>, R(asio::error_code, T)>
  130. {
  131. template <typename Initiation, typename... InitArgs>
  132. static auto initiate_impl(Initiation initiation,
  133. std::allocator_arg_t, Allocator, InitArgs... args)
  134. -> experimental::coro<void(), T,
  135. asio::associated_executor_t<Initiation>, Allocator>
  136. {
  137. co_return co_await deferred_async_operation<
  138. R(asio::error_code, T), Initiation, InitArgs...>(
  139. deferred_init_tag{}, std::move(initiation), std::move(args)...);
  140. }
  141. template <typename... InitArgs>
  142. static auto initiate_impl(
  143. asio::detail::initiation_archetype<
  144. R(asio::error_code, T)>,
  145. std::allocator_arg_t, Allocator, InitArgs... args)
  146. -> experimental::coro<void(), T, asio::any_io_executor, Allocator>;
  147. template <typename Initiation, typename... InitArgs>
  148. static auto initiate(Initiation initiation,
  149. experimental::use_coro_t<Allocator> tk, InitArgs&&... args)
  150. {
  151. return initiate_impl(std::move(initiation), std::allocator_arg,
  152. tk.get_allocator(), std::forward<InitArgs>(args)...);
  153. }
  154. };
  155. template <typename Allocator, typename R, typename T>
  156. struct async_result<
  157. experimental::use_coro_t<Allocator>, R(std::exception_ptr, T)>
  158. {
  159. template <typename Initiation, typename... InitArgs>
  160. static auto initiate_impl(Initiation initiation,
  161. std::allocator_arg_t, Allocator, InitArgs... args)
  162. -> experimental::coro<void(), T,
  163. asio::associated_executor_t<Initiation>, Allocator>
  164. {
  165. co_return co_await deferred_async_operation<
  166. R(std::exception_ptr, T), Initiation, InitArgs...>(
  167. deferred_init_tag{}, std::move(initiation), std::move(args)...);
  168. }
  169. template <typename... InitArgs>
  170. static auto initiate_impl(
  171. asio::detail::initiation_archetype<R(std::exception_ptr, T)>,
  172. std::allocator_arg_t, Allocator, InitArgs... args)
  173. -> experimental::coro<void(), T, asio::any_io_executor, Allocator>;
  174. template <typename Initiation, typename... InitArgs>
  175. static auto initiate(Initiation initiation,
  176. experimental::use_coro_t<Allocator> tk, InitArgs&&... args)
  177. {
  178. return initiate_impl(std::move(initiation), std::allocator_arg,
  179. tk.get_allocator(), std::forward<InitArgs>(args)...);
  180. }
  181. };
  182. #endif // !defined(GENERATING_DOCUMENTATION)
  183. } // namespace asio
  184. #include "asio/detail/pop_options.hpp"
  185. #endif // ASIO_EXPERIMENTAL_IMPL_USE_CORO_HPP