use_awaitable.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // use_awaitable.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_USE_AWAITABLE_HPP
  11. #define BOOST_ASIO_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. #if defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
  17. #include <boost/asio/awaitable.hpp>
  18. #include <boost/asio/detail/handler_tracking.hpp>
  19. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  20. # if defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  21. # include <boost/asio/detail/source_location.hpp>
  22. # endif // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  23. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. /// A @ref completion_token that represents the currently executing coroutine.
  28. /**
  29. * The @c use_awaitable_t class, with its value @c use_awaitable, is used to
  30. * represent the currently executing coroutine. This completion token may be
  31. * passed as a handler to an asynchronous operation. For example:
  32. *
  33. * @code awaitable<void> my_coroutine()
  34. * {
  35. * std::size_t n = co_await my_socket.async_read_some(buffer, use_awaitable);
  36. * ...
  37. * } @endcode
  38. *
  39. * When used with co_await, the initiating function (@c async_read_some in the
  40. * above example) suspends the current coroutine. The coroutine is resumed when
  41. * the asynchronous operation completes, and the result of the operation is
  42. * returned.
  43. */
  44. template <typename Executor = any_io_executor>
  45. struct use_awaitable_t
  46. {
  47. /// Default constructor.
  48. constexpr use_awaitable_t(
  49. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  50. # if defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  51. detail::source_location location = detail::source_location::current()
  52. # endif // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  53. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  54. )
  55. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  56. # if defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  57. : file_name_(location.file_name()),
  58. line_(location.line()),
  59. function_name_(location.function_name())
  60. # else // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  61. : file_name_(0),
  62. line_(0),
  63. function_name_(0)
  64. # endif // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
  65. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  66. {
  67. }
  68. /// Constructor used to specify file name, line, and function name.
  69. constexpr use_awaitable_t(const char* file_name,
  70. int line, const char* function_name)
  71. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  72. : file_name_(file_name),
  73. line_(line),
  74. function_name_(function_name)
  75. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  76. {
  77. #if !defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  78. (void)file_name;
  79. (void)line;
  80. (void)function_name;
  81. #endif // !defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  82. }
  83. /// Adapts an executor to add the @c use_awaitable_t completion token as the
  84. /// default.
  85. template <typename InnerExecutor>
  86. struct executor_with_default : InnerExecutor
  87. {
  88. /// Specify @c use_awaitable_t as the default completion token type.
  89. typedef use_awaitable_t default_completion_token_type;
  90. /// Construct the adapted executor from the inner executor type.
  91. template <typename InnerExecutor1>
  92. executor_with_default(const InnerExecutor1& ex,
  93. constraint_t<
  94. conditional_t<
  95. !is_same<InnerExecutor1, executor_with_default>::value,
  96. is_convertible<InnerExecutor1, InnerExecutor>,
  97. false_type
  98. >::value
  99. > = 0) noexcept
  100. : InnerExecutor(ex)
  101. {
  102. }
  103. };
  104. /// Type alias to adapt an I/O object to use @c use_awaitable_t as its
  105. /// default completion token type.
  106. template <typename T>
  107. using as_default_on_t = typename T::template rebind_executor<
  108. executor_with_default<typename T::executor_type>>::other;
  109. /// Function helper to adapt an I/O object to use @c use_awaitable_t as its
  110. /// default completion token type.
  111. template <typename T>
  112. static typename decay_t<T>::template rebind_executor<
  113. executor_with_default<typename decay_t<T>::executor_type>
  114. >::other
  115. as_default_on(T&& object)
  116. {
  117. return typename decay_t<T>::template rebind_executor<
  118. executor_with_default<typename decay_t<T>::executor_type>
  119. >::other(static_cast<T&&>(object));
  120. }
  121. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  122. const char* file_name_;
  123. int line_;
  124. const char* function_name_;
  125. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  126. };
  127. /// A @ref completion_token object that represents the currently executing
  128. /// coroutine.
  129. /**
  130. * See the documentation for boost::asio::use_awaitable_t for a usage example.
  131. */
  132. #if defined(GENERATING_DOCUMENTATION)
  133. constexpr use_awaitable_t<> use_awaitable;
  134. #else
  135. constexpr use_awaitable_t<> use_awaitable(0, 0, 0);
  136. #endif
  137. } // namespace asio
  138. } // namespace boost
  139. #include <boost/asio/detail/pop_options.hpp>
  140. #include <boost/asio/impl/use_awaitable.hpp>
  141. #endif // defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
  142. #endif // BOOST_ASIO_USE_AWAITABLE_HPP