awaitable.hpp 2.8 KB

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