use_awaitable.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // impl/use_awaitable.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_IMPL_USE_AWAITABLE_HPP
  11. #define BOOST_ASIO_IMPL_USE_AWAITABLE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/async_result.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. namespace detail {
  21. template <typename Executor, typename T>
  22. class awaitable_handler_base
  23. : public awaitable_thread<Executor>
  24. {
  25. public:
  26. typedef void result_type;
  27. typedef awaitable<T, Executor> awaitable_type;
  28. // Construct from the entry point of a new thread of execution.
  29. awaitable_handler_base(awaitable<void, Executor> a, const Executor& ex)
  30. : awaitable_thread<Executor>(std::move(a), ex)
  31. {
  32. }
  33. // Transfer ownership from another awaitable_thread.
  34. explicit awaitable_handler_base(awaitable_thread<Executor>* h)
  35. : awaitable_thread<Executor>(std::move(*h))
  36. {
  37. }
  38. protected:
  39. awaitable_frame<T, Executor>* frame() noexcept
  40. {
  41. return static_cast<awaitable_frame<T, Executor>*>(this->top_of_stack_);
  42. }
  43. };
  44. template <typename, typename...>
  45. class awaitable_handler;
  46. template <typename Executor>
  47. class awaitable_handler<Executor, void>
  48. : public awaitable_handler_base<Executor, void>
  49. {
  50. public:
  51. using awaitable_handler_base<Executor, void>::awaitable_handler_base;
  52. void operator()()
  53. {
  54. this->frame()->attach_thread(this);
  55. this->frame()->return_void();
  56. this->frame()->pop_frame();
  57. this->pump();
  58. }
  59. };
  60. template <typename Executor>
  61. class awaitable_handler<Executor, boost::system::error_code>
  62. : public awaitable_handler_base<Executor, void>
  63. {
  64. public:
  65. using awaitable_handler_base<Executor, void>::awaitable_handler_base;
  66. void operator()(const boost::system::error_code& ec)
  67. {
  68. this->frame()->attach_thread(this);
  69. if (ec)
  70. this->frame()->set_error(ec);
  71. else
  72. this->frame()->return_void();
  73. this->frame()->pop_frame();
  74. this->pump();
  75. }
  76. };
  77. template <typename Executor>
  78. class awaitable_handler<Executor, std::exception_ptr>
  79. : public awaitable_handler_base<Executor, void>
  80. {
  81. public:
  82. using awaitable_handler_base<Executor, void>::awaitable_handler_base;
  83. void operator()(std::exception_ptr ex)
  84. {
  85. this->frame()->attach_thread(this);
  86. if (ex)
  87. this->frame()->set_except(ex);
  88. else
  89. this->frame()->return_void();
  90. this->frame()->pop_frame();
  91. this->pump();
  92. }
  93. };
  94. template <typename Executor, typename T>
  95. class awaitable_handler<Executor, T>
  96. : public awaitable_handler_base<Executor, T>
  97. {
  98. public:
  99. using awaitable_handler_base<Executor, T>::awaitable_handler_base;
  100. template <typename Arg>
  101. void operator()(Arg&& arg)
  102. {
  103. this->frame()->attach_thread(this);
  104. this->frame()->return_value(std::forward<Arg>(arg));
  105. this->frame()->pop_frame();
  106. this->pump();
  107. }
  108. };
  109. template <typename Executor, typename T>
  110. class awaitable_handler<Executor, boost::system::error_code, T>
  111. : public awaitable_handler_base<Executor, T>
  112. {
  113. public:
  114. using awaitable_handler_base<Executor, T>::awaitable_handler_base;
  115. template <typename Arg>
  116. void operator()(const boost::system::error_code& ec, Arg&& arg)
  117. {
  118. this->frame()->attach_thread(this);
  119. if (ec)
  120. this->frame()->set_error(ec);
  121. else
  122. this->frame()->return_value(std::forward<Arg>(arg));
  123. this->frame()->pop_frame();
  124. this->pump();
  125. }
  126. };
  127. template <typename Executor, typename T>
  128. class awaitable_handler<Executor, std::exception_ptr, T>
  129. : public awaitable_handler_base<Executor, T>
  130. {
  131. public:
  132. using awaitable_handler_base<Executor, T>::awaitable_handler_base;
  133. template <typename Arg>
  134. void operator()(std::exception_ptr ex, Arg&& arg)
  135. {
  136. this->frame()->attach_thread(this);
  137. if (ex)
  138. this->frame()->set_except(ex);
  139. else
  140. this->frame()->return_value(std::forward<Arg>(arg));
  141. this->frame()->pop_frame();
  142. this->pump();
  143. }
  144. };
  145. template <typename Executor, typename... Ts>
  146. class awaitable_handler
  147. : public awaitable_handler_base<Executor, std::tuple<Ts...>>
  148. {
  149. public:
  150. using awaitable_handler_base<Executor,
  151. std::tuple<Ts...>>::awaitable_handler_base;
  152. template <typename... Args>
  153. void operator()(Args&&... args)
  154. {
  155. this->frame()->attach_thread(this);
  156. this->frame()->return_values(std::forward<Args>(args)...);
  157. this->frame()->pop_frame();
  158. this->pump();
  159. }
  160. };
  161. template <typename Executor, typename... Ts>
  162. class awaitable_handler<Executor, boost::system::error_code, Ts...>
  163. : public awaitable_handler_base<Executor, std::tuple<Ts...>>
  164. {
  165. public:
  166. using awaitable_handler_base<Executor,
  167. std::tuple<Ts...>>::awaitable_handler_base;
  168. template <typename... Args>
  169. void operator()(const boost::system::error_code& ec, Args&&... args)
  170. {
  171. this->frame()->attach_thread(this);
  172. if (ec)
  173. this->frame()->set_error(ec);
  174. else
  175. this->frame()->return_values(std::forward<Args>(args)...);
  176. this->frame()->pop_frame();
  177. this->pump();
  178. }
  179. };
  180. template <typename Executor, typename... Ts>
  181. class awaitable_handler<Executor, std::exception_ptr, Ts...>
  182. : public awaitable_handler_base<Executor, std::tuple<Ts...>>
  183. {
  184. public:
  185. using awaitable_handler_base<Executor,
  186. std::tuple<Ts...>>::awaitable_handler_base;
  187. template <typename... Args>
  188. void operator()(std::exception_ptr ex, Args&&... args)
  189. {
  190. this->frame()->attach_thread(this);
  191. if (ex)
  192. this->frame()->set_except(ex);
  193. else
  194. this->frame()->return_values(std::forward<Args>(args)...);
  195. this->frame()->pop_frame();
  196. this->pump();
  197. }
  198. };
  199. } // namespace detail
  200. #if !defined(GENERATING_DOCUMENTATION)
  201. template <typename Executor, typename R, typename... Args>
  202. class async_result<use_awaitable_t<Executor>, R(Args...)>
  203. {
  204. public:
  205. typedef typename detail::awaitable_handler<
  206. Executor, typename decay<Args>::type...> handler_type;
  207. typedef typename handler_type::awaitable_type return_type;
  208. #if defined(_MSC_VER)
  209. template <typename T>
  210. static T dummy_return()
  211. {
  212. return std::move(*static_cast<T*>(nullptr));
  213. }
  214. template <>
  215. static void dummy_return()
  216. {
  217. }
  218. #endif // defined(_MSC_VER)
  219. template <typename Initiation, typename... InitArgs>
  220. static return_type initiate(Initiation initiation,
  221. use_awaitable_t<Executor>, InitArgs... args)
  222. {
  223. co_await [&](auto* frame)
  224. {
  225. handler_type handler(frame->detach_thread());
  226. std::move(initiation)(std::move(handler), std::move(args)...);
  227. return static_cast<handler_type*>(nullptr);
  228. };
  229. for (;;) {} // Never reached.
  230. #if defined(_MSC_VER)
  231. co_return dummy_return<typename return_type::value_type>();
  232. #endif // defined(_MSC_VER)
  233. }
  234. };
  235. #endif // !defined(GENERATING_DOCUMENTATION)
  236. } // namespace asio
  237. } // namespace boost
  238. #include <boost/asio/detail/pop_options.hpp>
  239. #endif // BOOST_ASIO_IMPL_USE_AWAITABLE_HPP