use_awaitable.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // use_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_USE_AWAITABLE_HPP
  11. #define BOOST_ASIO_USE_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 <boost/asio/awaitable.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. /// A completion token that represents the currently executing coroutine.
  22. /**
  23. * The @c use_awaitable_t class, with its value @c use_awaitable, is used to
  24. * represent the currently executing coroutine. This completion token may be
  25. * passed as a handler to an asynchronous operation. For example:
  26. *
  27. * @code awaitable<void> my_coroutine()
  28. * {
  29. * std::size_t n = co_await my_socket.async_read_some(buffer, use_awaitable);
  30. * ...
  31. * } @endcode
  32. *
  33. * When used with co_await, the initiating function (@c async_read_some in the
  34. * above example) suspends the current coroutine. The coroutine is resumed when
  35. * the asynchronous operation completes, and the result of the operation is
  36. * returned.
  37. */
  38. template <typename Executor = executor>
  39. struct use_awaitable_t
  40. {
  41. /// Default constructor.
  42. BOOST_ASIO_CONSTEXPR use_awaitable_t()
  43. {
  44. }
  45. /// Adapts an executor to add the @c use_awaitable_t completion token as the
  46. /// default.
  47. template <typename InnerExecutor>
  48. struct executor_with_default : InnerExecutor
  49. {
  50. /// Specify @c use_awaitable_t as the default completion token type.
  51. typedef use_awaitable_t default_completion_token_type;
  52. /// Construct the adapted executor from the inner executor type.
  53. executor_with_default(const InnerExecutor& ex) BOOST_ASIO_NOEXCEPT
  54. : InnerExecutor(ex)
  55. {
  56. }
  57. };
  58. /// Type alias to adapt an I/O object to use @c use_awaitable_t as its
  59. /// default completion token type.
  60. #if defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES) \
  61. || defined(GENERATING_DOCUMENTATION)
  62. template <typename T>
  63. using as_default_on_t = typename T::template rebind_executor<
  64. executor_with_default<typename T::executor_type> >::other;
  65. #endif // defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
  66. // || defined(GENERATING_DOCUMENTATION)
  67. /// Function helper to adapt an I/O object to use @c use_awaitable_t as its
  68. /// default completion token type.
  69. template <typename T>
  70. static typename T::template rebind_executor<
  71. executor_with_default<typename T::executor_type>
  72. >::other
  73. as_default_on(BOOST_ASIO_MOVE_ARG(T) object)
  74. {
  75. return typename as_default_on_t<typename decay<T>::type>::type(
  76. BOOST_ASIO_MOVE_CAST(T)(object));
  77. }
  78. };
  79. /// A completion token object that represents the currently executing coroutine.
  80. /**
  81. * See the documentation for boost::asio::use_awaitable_t for a usage example.
  82. */
  83. #if defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  84. constexpr use_awaitable_t<> use_awaitable;
  85. #elif defined(BOOST_ASIO_MSVC)
  86. __declspec(selectany) use_awaitable_t<> use_awaitable;
  87. #endif
  88. } // namespace asio
  89. } // namespace boost
  90. #include <boost/asio/detail/pop_options.hpp>
  91. #include <boost/asio/impl/use_awaitable.hpp>
  92. #endif // defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
  93. #endif // BOOST_ASIO_USE_AWAITABLE_HPP