use_awaitable.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. BOOST_ASIO_CONSTEXPR use_awaitable_t()
  42. {
  43. }
  44. };
  45. /// A completion token object that represents the currently executing coroutine.
  46. /**
  47. * See the documentation for boost::asio::use_awaitable_t for a usage example.
  48. */
  49. #if defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  50. constexpr use_awaitable_t<> use_awaitable;
  51. #elif defined(BOOST_ASIO_MSVC)
  52. __declspec(selectany) use_awaitable_t<> use_awaitable;
  53. #endif
  54. } // namespace asio
  55. } // namespace boost
  56. #include <boost/asio/detail/pop_options.hpp>
  57. #include <boost/asio/impl/use_awaitable.hpp>
  58. #endif // defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
  59. #endif // BOOST_ASIO_USE_AWAITABLE_HPP