use_coro.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // experimental/use_coro.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2021-2022 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 <optional>
  18. #include "asio/bind_cancellation_slot.hpp"
  19. #include "asio/bind_executor.hpp"
  20. #include "asio/error_code.hpp"
  21. #include "asio/experimental/detail/partial_promise.hpp"
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. class any_io_executor;
  25. namespace experimental {
  26. /// A completion token that represents the currently executing resumable
  27. /// coroutine.
  28. /**
  29. * The @c use_coro_t class, with its value @c use_coro, is used to represent an
  30. * operation that can be awaited by the current resumable coroutine. This
  31. * completion token may be passed as a handler to an asynchronous operation.
  32. * For example:
  33. *
  34. * @code coro<void> my_coroutine(tcp::socket my_socket)
  35. * {
  36. * std::size_t n = co_await my_socket.async_read_some(buffer, use_coro);
  37. * ...
  38. * } @endcode
  39. *
  40. * When used with co_await, the initiating function (@c async_read_some in the
  41. * above example) suspends the current coroutine. The coroutine is resumed when
  42. * the asynchronous operation completes, and the result of the operation is
  43. * returned.
  44. */
  45. template <typename Executor = any_io_executor>
  46. struct use_coro_t
  47. {
  48. /// Default constructor.
  49. ASIO_CONSTEXPR use_coro_t(
  50. #if defined(ASIO_ENABLE_HANDLER_TRACKING)
  51. # if defined(ASIO_HAS_SOURCE_LOCATION)
  52. asio::detail::source_location location =
  53. asio::detail::source_location::current()
  54. # endif // defined(ASIO_HAS_SOURCE_LOCATION)
  55. #endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
  56. )
  57. #if defined(ASIO_ENABLE_HANDLER_TRACKING)
  58. # if defined(ASIO_HAS_SOURCE_LOCATION)
  59. : file_name_(location.file_name()),
  60. line_(location.line()),
  61. function_name_(location.function_name())
  62. # else // defined(ASIO_HAS_SOURCE_LOCATION)
  63. : file_name_(0),
  64. line_(0),
  65. function_name_(0)
  66. # endif // defined(ASIO_HAS_SOURCE_LOCATION)
  67. #endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
  68. {
  69. }
  70. /// Constructor used to specify file name, line, and function name.
  71. ASIO_CONSTEXPR use_coro_t(const char* file_name,
  72. int line, const char* function_name)
  73. #if defined(ASIO_ENABLE_HANDLER_TRACKING)
  74. : file_name_(file_name),
  75. line_(line),
  76. function_name_(function_name)
  77. #endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
  78. {
  79. #if !defined(ASIO_ENABLE_HANDLER_TRACKING)
  80. (void)file_name;
  81. (void)line;
  82. (void)function_name;
  83. #endif // !defined(ASIO_ENABLE_HANDLER_TRACKING)
  84. }
  85. /// Adapts an executor to add the @c use_coro_t completion token as the
  86. /// default.
  87. template <typename InnerExecutor>
  88. struct executor_with_default : InnerExecutor
  89. {
  90. /// Specify @c use_coro_t as the default completion token type.
  91. typedef use_coro_t default_completion_token_type;
  92. /// Construct the adapted executor from the inner executor type.
  93. template <typename InnerExecutor1>
  94. executor_with_default(const InnerExecutor1& ex,
  95. typename constraint<
  96. conditional<
  97. !is_same<InnerExecutor1, executor_with_default>::value,
  98. is_convertible<InnerExecutor1, InnerExecutor>,
  99. false_type
  100. >::type::value
  101. >::type = 0) ASIO_NOEXCEPT
  102. : InnerExecutor(ex)
  103. {
  104. }
  105. };
  106. /// Type alias to adapt an I/O object to use @c use_coro_t as its
  107. /// default completion token type.
  108. #if defined(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(ASIO_HAS_ALIAS_TEMPLATES)
  114. // || defined(GENERATING_DOCUMENTATION)
  115. /// Function helper to adapt an I/O object to use @c use_coro_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(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(ASIO_MOVE_CAST(T)(object));
  126. }
  127. #if defined(ASIO_ENABLE_HANDLER_TRACKING)
  128. const char* file_name_;
  129. int line_;
  130. const char* function_name_;
  131. #endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
  132. };
  133. /// A completion token object that represents the currently executing resumable
  134. /// coroutine.
  135. /**
  136. * See the documentation for asio::use_coro_t for a usage example.
  137. */
  138. #if defined(GENERATING_DOCUMENTATION)
  139. constexpr use_coro_t<> use_coro;
  140. #elif defined(ASIO_HAS_CONSTEXPR)
  141. constexpr use_coro_t<> use_coro(0, 0, 0);
  142. #elif defined(ASIO_MSVC)
  143. __declspec(selectany) use_coro_t<> use_coro(0, 0, 0);
  144. #endif
  145. } // namespace experimental
  146. } // namespace asio
  147. #include "asio/detail/pop_options.hpp"
  148. #include "asio/experimental/impl/use_coro.hpp"
  149. #include "asio/experimental/coro.hpp"
  150. #endif // ASIO_EXPERIMENTAL_USE_CORO_HPP