awaitable.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // 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_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. frame_ = std::exchange(other.frame_, nullptr);
  68. return *this;
  69. }
  70. /// Checks if the awaitable refers to a future result.
  71. bool valid() const noexcept
  72. {
  73. return !!frame_;
  74. }
  75. #if !defined(GENERATING_DOCUMENTATION)
  76. // Support for co_await keyword.
  77. bool await_ready() const noexcept
  78. {
  79. return false;
  80. }
  81. // Support for co_await keyword.
  82. template <class U>
  83. void await_suspend(
  84. detail::coroutine_handle<detail::awaitable_frame<U, Executor>> h)
  85. {
  86. frame_->push_frame(&h.promise());
  87. }
  88. // Support for co_await keyword.
  89. T await_resume()
  90. {
  91. return awaitable(static_cast<awaitable&&>(*this)).frame_->get();
  92. }
  93. #endif // !defined(GENERATING_DOCUMENTATION)
  94. private:
  95. template <typename> friend class detail::awaitable_thread;
  96. template <typename, typename> friend class detail::awaitable_frame;
  97. // Not copy constructible or copy assignable.
  98. awaitable(const awaitable&) = delete;
  99. awaitable& operator=(const awaitable&) = delete;
  100. // Construct the awaitable from a coroutine's frame object.
  101. explicit awaitable(detail::awaitable_frame<T, Executor>* a)
  102. : frame_(a)
  103. {
  104. }
  105. detail::awaitable_frame<T, Executor>* frame_;
  106. };
  107. } // namespace asio
  108. } // namespace boost
  109. #include <boost/asio/detail/pop_options.hpp>
  110. #include <boost/asio/impl/awaitable.hpp>
  111. #endif // defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
  112. #endif // BOOST_ASIO_AWAITABLE_HPP