use_coro.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // experimental/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 BOOST_ASIO_EXPERIMENTAL_USE_CORO_HPP
  12. #define BOOST_ASIO_EXPERIMENTAL_USE_CORO_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #include <memory>
  18. #include <boost/asio/deferred.hpp>
  19. #include <boost/asio/detail/source_location.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. class any_io_executor;
  24. namespace experimental {
  25. /// A @ref completion_token that creates another coro for the task completion.
  26. /**
  27. * The @c use_coro_t class, with its value @c use_coro, is used to represent an
  28. * operation that can be awaited by the current resumable coroutine. This
  29. * completion token may be passed as a handler to an asynchronous operation.
  30. * For example:
  31. *
  32. * @code coro<void> my_coroutine(tcp::socket my_socket)
  33. * {
  34. * std::size_t n = co_await my_socket.async_read_some(buffer, use_coro);
  35. * ...
  36. * } @endcode
  37. *
  38. * When used with co_await, the initiating function (@c async_read_some in the
  39. * above example) suspends the current coroutine. The coroutine is resumed when
  40. * the asynchronous operation completes, and the result of the operation is
  41. * returned.
  42. *
  43. * Note that this token is not the most efficient (use the default completion
  44. * token @c boost::asio::deferred for that) but does provide type erasure, as it
  45. * will always return a @c coro.
  46. */
  47. template <typename Allocator = std::allocator<void>>
  48. struct use_coro_t
  49. {
  50. /// The allocator type. The allocator is used when constructing the
  51. /// @c std::promise object for a given asynchronous operation.
  52. typedef Allocator allocator_type;
  53. /// Default constructor.
  54. constexpr use_coro_t(
  55. allocator_type allocator = allocator_type{}
  56. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  57. # if defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  58. , boost::asio::detail::source_location location =
  59. boost::asio::detail::source_location::current()
  60. # endif // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  61. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  62. )
  63. : allocator_(allocator)
  64. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  65. # if defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  66. , file_name_(location.file_name()),
  67. line_(location.line()),
  68. function_name_(location.function_name())
  69. # else // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  70. , file_name_(0),
  71. line_(0),
  72. function_name_(0)
  73. # endif // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  74. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  75. {
  76. }
  77. /// Specify an alternate allocator.
  78. template <typename OtherAllocator>
  79. use_coro_t<OtherAllocator> rebind(const OtherAllocator& allocator) const
  80. {
  81. return use_future_t<OtherAllocator>(allocator);
  82. }
  83. /// Obtain allocator.
  84. allocator_type get_allocator() const
  85. {
  86. return allocator_;
  87. }
  88. /// Constructor used to specify file name, line, and function name.
  89. constexpr use_coro_t(const char* file_name,
  90. int line, const char* function_name,
  91. allocator_type allocator = allocator_type{}) :
  92. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  93. file_name_(file_name),
  94. line_(line),
  95. function_name_(function_name),
  96. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  97. allocator_(allocator)
  98. {
  99. #if !defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  100. (void)file_name;
  101. (void)line;
  102. (void)function_name;
  103. #endif // !defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  104. }
  105. /// Adapts an executor to add the @c use_coro_t completion token as the
  106. /// default.
  107. template <typename InnerExecutor>
  108. struct executor_with_default : InnerExecutor
  109. {
  110. /// Specify @c use_coro_t as the default completion token type.
  111. typedef use_coro_t default_completion_token_type;
  112. /// Construct the adapted executor from the inner executor type.
  113. template <typename InnerExecutor1>
  114. executor_with_default(const InnerExecutor1& ex,
  115. constraint_t<
  116. conditional_t<
  117. !is_same<InnerExecutor1, executor_with_default>::value,
  118. is_convertible<InnerExecutor1, InnerExecutor>,
  119. false_type
  120. >::value
  121. > = 0) noexcept
  122. : InnerExecutor(ex)
  123. {
  124. }
  125. };
  126. /// Type alias to adapt an I/O object to use @c use_coro_t as its
  127. /// default completion token type.
  128. template <typename T>
  129. using as_default_on_t = typename T::template rebind_executor<
  130. executor_with_default<typename T::executor_type>>::other;
  131. /// Function helper to adapt an I/O object to use @c use_coro_t as its
  132. /// default completion token type.
  133. template <typename T>
  134. static typename decay_t<T>::template rebind_executor<
  135. executor_with_default<typename decay_t<T>::executor_type>
  136. >::other
  137. as_default_on(T&& object)
  138. {
  139. return typename decay_t<T>::template rebind_executor<
  140. executor_with_default<typename decay_t<T>::executor_type>
  141. >::other(static_cast<T&&>(object));
  142. }
  143. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  144. const char* file_name_;
  145. int line_;
  146. const char* function_name_;
  147. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  148. private:
  149. Allocator allocator_;
  150. };
  151. /// A @ref completion_token object that represents the currently executing
  152. /// resumable coroutine.
  153. /**
  154. * See the documentation for boost::asio::use_coro_t for a usage example.
  155. */
  156. #if defined(GENERATING_DOCUMENTATION)
  157. BOOST_ASIO_INLINE_VARIABLE constexpr use_coro_t<> use_coro;
  158. #else
  159. BOOST_ASIO_INLINE_VARIABLE constexpr use_coro_t<> use_coro(0, 0, 0);
  160. #endif
  161. } // namespace experimental
  162. } // namespace asio
  163. } // namespace boost
  164. #include <boost/asio/detail/pop_options.hpp>
  165. #include <boost/asio/experimental/impl/use_coro.hpp>
  166. #include <boost/asio/experimental/coro.hpp>
  167. #endif // BOOST_ASIO_EXPERIMENTAL_USE_CORO_HPP