5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_USE_AWAITABLE_HPP
12 #define BOOST_ASIO_USE_AWAITABLE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
22 #include <boost/asio/awaitable.hpp>
24 #include <boost/asio/detail/push_options.hpp>
29 /// A completion token that represents the currently executing coroutine.
31 * The @c use_awaitable_t class, with its value @c use_awaitable, is used to
32 * represent the currently executing coroutine. This completion token may be
33 * passed as a handler to an asynchronous operation. For example:
35 * @code awaitable<void> my_coroutine()
37 * std::size_t n = co_await my_socket.async_read_some(buffer, use_awaitable);
41 * When used with co_await, the initiating function (@c async_read_some in the
42 * above example) suspends the current coroutine. The coroutine is resumed when
43 * the asynchronous operation completes, and the result of the operation is
46 template <typename Executor = executor>
47 struct use_awaitable_t
49 /// Default constructor.
50 BOOST_ASIO_CONSTEXPR use_awaitable_t()
54 /// Adapts an executor to add the @c use_awaitable_t completion token as the
56 template <typename InnerExecutor>
57 struct executor_with_default : InnerExecutor
59 /// Specify @c use_awaitable_t as the default completion token type.
60 typedef use_awaitable_t default_completion_token_type;
62 /// Construct the adapted executor from the inner executor type.
63 executor_with_default(const InnerExecutor& ex) BOOST_ASIO_NOEXCEPT
69 /// Type alias to adapt an I/O object to use @c use_awaitable_t as its
70 /// default completion token type.
71 #if defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES) \
72 || defined(GENERATING_DOCUMENTATION)
74 using as_default_on_t = typename T::template rebind_executor<
75 executor_with_default<typename T::executor_type> >::other;
76 #endif // defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
77 // || defined(GENERATING_DOCUMENTATION)
79 /// Function helper to adapt an I/O object to use @c use_awaitable_t as its
80 /// default completion token type.
82 static typename T::template rebind_executor<
83 executor_with_default<typename T::executor_type>
85 as_default_on(BOOST_ASIO_MOVE_ARG(T) object)
87 return typename as_default_on_t<typename decay<T>::type>::type(
88 BOOST_ASIO_MOVE_CAST(T)(object));
92 /// A completion token object that represents the currently executing coroutine.
94 * See the documentation for boost::asio::use_awaitable_t for a usage example.
96 #if defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
97 constexpr use_awaitable_t<> use_awaitable;
98 #elif defined(BOOST_ASIO_MSVC)
99 __declspec(selectany) use_awaitable_t<> use_awaitable;
105 #include <boost/asio/detail/pop_options.hpp>
107 #include <boost/asio/impl/use_awaitable.hpp>
109 #endif // defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
111 #endif // BOOST_ASIO_USE_AWAITABLE_HPP