awaitable.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // awaitable.hpp
  3. // ~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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_AWAITABLE_HPP
  11. #define BOOST_ASIO_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. #if defined(BOOST_ASIO_HAS_STD_COROUTINE)
  18. # include <coroutine>
  19. #else // defined(BOOST_ASIO_HAS_STD_COROUTINE)
  20. # include <experimental/coroutine>
  21. #endif // defined(BOOST_ASIO_HAS_STD_COROUTINE)
  22. #include <utility>
  23. #include <boost/asio/any_io_executor.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. #if defined(BOOST_ASIO_HAS_STD_COROUTINE)
  29. using std::coroutine_handle;
  30. using std::suspend_always;
  31. #else // defined(BOOST_ASIO_HAS_STD_COROUTINE)
  32. using std::experimental::coroutine_handle;
  33. using std::experimental::suspend_always;
  34. #endif // defined(BOOST_ASIO_HAS_STD_COROUTINE)
  35. template <typename> class awaitable_thread;
  36. template <typename, typename> class awaitable_frame;
  37. } // namespace detail
  38. /// The return type of a coroutine or asynchronous operation.
  39. template <typename T, typename Executor = any_io_executor>
  40. class BOOST_ASIO_NODISCARD awaitable
  41. {
  42. public:
  43. /// The type of the awaited value.
  44. typedef T value_type;
  45. /// The executor type that will be used for the coroutine.
  46. typedef Executor executor_type;
  47. /// Default constructor.
  48. constexpr awaitable() noexcept
  49. : frame_(nullptr)
  50. {
  51. }
  52. /// Move constructor.
  53. awaitable(awaitable&& other) noexcept
  54. : frame_(std::exchange(other.frame_, nullptr))
  55. {
  56. }
  57. /// Destructor
  58. ~awaitable()
  59. {
  60. if (frame_)
  61. frame_->destroy();
  62. }
  63. /// Move assignment.
  64. awaitable& operator=(awaitable&& other) noexcept
  65. {
  66. if (this != &other)
  67. {
  68. if (frame_)
  69. frame_->destroy();
  70. frame_ = std::exchange(other.frame_, nullptr);
  71. }
  72. return *this;
  73. }
  74. /// Checks if the awaitable refers to a future result.
  75. bool valid() const noexcept
  76. {
  77. return !!frame_;
  78. }
  79. #if !defined(GENERATING_DOCUMENTATION)
  80. // Support for co_await keyword.
  81. bool await_ready() const noexcept
  82. {
  83. return false;
  84. }
  85. // Support for co_await keyword.
  86. template <class U>
  87. void await_suspend(
  88. detail::coroutine_handle<detail::awaitable_frame<U, Executor>> h)
  89. {
  90. frame_->push_frame(&h.promise());
  91. }
  92. // Support for co_await keyword.
  93. T await_resume()
  94. {
  95. return awaitable(static_cast<awaitable&&>(*this)).frame_->get();
  96. }
  97. #endif // !defined(GENERATING_DOCUMENTATION)
  98. private:
  99. template <typename> friend class detail::awaitable_thread;
  100. template <typename, typename> friend class detail::awaitable_frame;
  101. // Not copy constructible or copy assignable.
  102. awaitable(const awaitable&) = delete;
  103. awaitable& operator=(const awaitable&) = delete;
  104. // Construct the awaitable from a coroutine's frame object.
  105. explicit awaitable(detail::awaitable_frame<T, Executor>* a)
  106. : frame_(a)
  107. {
  108. }
  109. detail::awaitable_frame<T, Executor>* frame_;
  110. };
  111. } // namespace asio
  112. } // namespace boost
  113. #include <boost/asio/detail/pop_options.hpp>
  114. #include <boost/asio/impl/awaitable.hpp>
  115. #if defined(BOOST_ASIO_HEADER_ONLY)
  116. # include <boost/asio/impl/awaitable.ipp>
  117. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  118. #endif // defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
  119. #endif // BOOST_ASIO_AWAITABLE_HPP