use_awaitable.hpp 5.0 KB

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