awaitable.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // awaitable.hpp
  3. // ~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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 <boost/asio/any_io_executor.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace detail {
  27. #if defined(BOOST_ASIO_HAS_STD_COROUTINE)
  28. using std::coroutine_handle;
  29. using std::suspend_always;
  30. #else // defined(BOOST_ASIO_HAS_STD_COROUTINE)
  31. using std::experimental::coroutine_handle;
  32. using std::experimental::suspend_always;
  33. #endif // defined(BOOST_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 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. /// Checks if the awaitable refers to a future result.
  63. bool valid() const noexcept
  64. {
  65. return !!frame_;
  66. }
  67. #if !defined(GENERATING_DOCUMENTATION)
  68. // Support for co_await keyword.
  69. bool await_ready() const noexcept
  70. {
  71. return false;
  72. }
  73. // Support for co_await keyword.
  74. template <class U>
  75. void await_suspend(
  76. detail::coroutine_handle<detail::awaitable_frame<U, Executor>> h)
  77. {
  78. frame_->push_frame(&h.promise());
  79. }
  80. // Support for co_await keyword.
  81. T await_resume()
  82. {
  83. return awaitable(static_cast<awaitable&&>(*this)).frame_->get();
  84. }
  85. #endif // !defined(GENERATING_DOCUMENTATION)
  86. private:
  87. template <typename> friend class detail::awaitable_thread;
  88. template <typename, typename> friend class detail::awaitable_frame;
  89. // Not copy constructible or copy assignable.
  90. awaitable(const awaitable&) = delete;
  91. awaitable& operator=(const awaitable&) = delete;
  92. // Construct the awaitable from a coroutine's frame object.
  93. explicit awaitable(detail::awaitable_frame<T, Executor>* a)
  94. : frame_(a)
  95. {
  96. }
  97. detail::awaitable_frame<T, Executor>* frame_;
  98. };
  99. } // namespace asio
  100. } // namespace boost
  101. #include <boost/asio/detail/pop_options.hpp>
  102. #include <boost/asio/impl/awaitable.hpp>
  103. #endif // defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
  104. #endif // BOOST_ASIO_AWAITABLE_HPP