Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

awaitable.hpp 2.7KB

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