use_awaitable.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // use_awaitable.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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 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. BOOST_ASIO_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. BOOST_ASIO_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. executor_with_default(const InnerExecutor& ex) BOOST_ASIO_NOEXCEPT
  92. : InnerExecutor(ex)
  93. {
  94. }
  95. /// Convert the specified executor to the inner executor type, then use
  96. /// that to construct the adapted executor.
  97. template <typename OtherExecutor>
  98. executor_with_default(const OtherExecutor& ex,
  99. typename constraint<
  100. is_convertible<OtherExecutor, InnerExecutor>::value
  101. >::type = 0) BOOST_ASIO_NOEXCEPT
  102. : InnerExecutor(ex)
  103. {
  104. }
  105. };
  106. /// Type alias to adapt an I/O object to use @c use_awaitable_t as its
  107. /// default completion token type.
  108. #if defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES) \
  109. || defined(GENERATING_DOCUMENTATION)
  110. template <typename T>
  111. using as_default_on_t = typename T::template rebind_executor<
  112. executor_with_default<typename T::executor_type> >::other;
  113. #endif // defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
  114. // || defined(GENERATING_DOCUMENTATION)
  115. /// Function helper to adapt an I/O object to use @c use_awaitable_t as its
  116. /// default completion token type.
  117. template <typename T>
  118. static typename decay<T>::type::template rebind_executor<
  119. executor_with_default<typename decay<T>::type::executor_type>
  120. >::other
  121. as_default_on(BOOST_ASIO_MOVE_ARG(T) object)
  122. {
  123. return typename decay<T>::type::template rebind_executor<
  124. executor_with_default<typename decay<T>::type::executor_type>
  125. >::other(BOOST_ASIO_MOVE_CAST(T)(object));
  126. }
  127. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  128. const char* file_name_;
  129. int line_;
  130. const char* function_name_;
  131. #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  132. };
  133. /// A completion token object that represents the currently executing coroutine.
  134. /**
  135. * See the documentation for boost::asio::use_awaitable_t for a usage example.
  136. */
  137. #if defined(GENERATING_DOCUMENTATION)
  138. constexpr use_awaitable_t<> use_awaitable;
  139. #elif defined(BOOST_ASIO_HAS_CONSTEXPR)
  140. constexpr use_awaitable_t<> use_awaitable(0, 0, 0);
  141. #elif defined(BOOST_ASIO_MSVC)
  142. __declspec(selectany) use_awaitable_t<> use_awaitable(0, 0, 0);
  143. #endif
  144. } // namespace asio
  145. } // namespace boost
  146. #include <boost/asio/detail/pop_options.hpp>
  147. #include <boost/asio/impl/use_awaitable.hpp>
  148. #endif // defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
  149. #endif // BOOST_ASIO_USE_AWAITABLE_HPP