awaitable.hpp 3.3 KB

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