use_coro.hpp 6.1 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 @c boost::asio::deferred
  44. * for that) but does provide type erasure, as it will always return a @c coro.
  45. */
  46. template <typename Allocator = std::allocator<void>>
  47. struct use_coro_t
  48. {
  49. /// The allocator type. The allocator is used when constructing the
  50. /// @c std::promise object for a given asynchronous operation.
  51. typedef Allocator allocator_type;
  52. /// Default constructor.
  53. constexpr use_coro_t(
  54. allocator_type allocator = allocator_type{}
  55. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  56. # if defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  57. , boost::asio::detail::source_location location =
  58. boost::asio::detail::source_location::current()
  59. # endif // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  60. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  61. )
  62. : allocator_(allocator)
  63. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  64. # if defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  65. , file_name_(location.file_name()),
  66. line_(location.line()),
  67. function_name_(location.function_name())
  68. # else // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  69. , file_name_(0),
  70. line_(0),
  71. function_name_(0)
  72. # endif // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  73. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  74. {
  75. }
  76. /// Specify an alternate allocator.
  77. template <typename OtherAllocator>
  78. use_coro_t<OtherAllocator> rebind(const OtherAllocator& allocator) const
  79. {
  80. return use_future_t<OtherAllocator>(allocator);
  81. }
  82. /// Obtain allocator.
  83. allocator_type get_allocator() const
  84. {
  85. return allocator_;
  86. }
  87. /// Constructor used to specify file name, line, and function name.
  88. constexpr use_coro_t(const char* file_name,
  89. int line, const char* function_name,
  90. allocator_type allocator = allocator_type{}) :
  91. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  92. file_name_(file_name),
  93. line_(line),
  94. function_name_(function_name),
  95. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  96. allocator_(allocator)
  97. {
  98. #if !defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  99. (void)file_name;
  100. (void)line;
  101. (void)function_name;
  102. #endif // !defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  103. }
  104. /// Adapts an executor to add the @c use_coro_t completion token as the
  105. /// default.
  106. template <typename InnerExecutor>
  107. struct executor_with_default : InnerExecutor
  108. {
  109. /// Specify @c use_coro_t as the default completion token type.
  110. typedef use_coro_t default_completion_token_type;
  111. /// Construct the adapted executor from the inner executor type.
  112. template <typename InnerExecutor1>
  113. executor_with_default(const InnerExecutor1& ex,
  114. constraint_t<
  115. conditional_t<
  116. !is_same<InnerExecutor1, executor_with_default>::value,
  117. is_convertible<InnerExecutor1, InnerExecutor>,
  118. false_type
  119. >::value
  120. > = 0) noexcept
  121. : InnerExecutor(ex)
  122. {
  123. }
  124. };
  125. /// Type alias to adapt an I/O object to use @c use_coro_t as its
  126. /// default completion token type.
  127. template <typename T>
  128. using as_default_on_t = typename T::template rebind_executor<
  129. executor_with_default<typename T::executor_type>>::other;
  130. /// Function helper to adapt an I/O object to use @c use_coro_t as its
  131. /// default completion token type.
  132. template <typename T>
  133. static typename decay_t<T>::template rebind_executor<
  134. executor_with_default<typename decay_t<T>::executor_type>
  135. >::other
  136. as_default_on(T&& object)
  137. {
  138. return typename decay_t<T>::template rebind_executor<
  139. executor_with_default<typename decay_t<T>::executor_type>
  140. >::other(static_cast<T&&>(object));
  141. }
  142. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  143. const char* file_name_;
  144. int line_;
  145. const char* function_name_;
  146. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  147. private:
  148. Allocator allocator_;
  149. };
  150. /// A @ref completion_token object that represents the currently executing
  151. /// resumable coroutine.
  152. /**
  153. * See the documentation for boost::asio::use_coro_t for a usage example.
  154. */
  155. #if defined(GENERATING_DOCUMENTATION)
  156. constexpr use_coro_t<> use_coro;
  157. #else
  158. constexpr use_coro_t<> use_coro(0, 0, 0);
  159. #endif
  160. } // namespace experimental
  161. } // namespace asio
  162. } // namespace boost
  163. #include <boost/asio/detail/pop_options.hpp>
  164. #include <boost/asio/experimental/impl/use_coro.hpp>
  165. #include <boost/asio/experimental/coro.hpp>
  166. #endif // BOOST_ASIO_EXPERIMENTAL_USE_CORO_HPP