use_coro.hpp 5.7 KB

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