]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/asio/use_awaitable.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / asio / use_awaitable.hpp
CommitLineData
92f5a8d4
TL
1//
2// use_awaitable.hpp
3// ~~~~~~~~~~~~~~~~~
4//
f67539c2 5// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
92f5a8d4
TL
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
11#ifndef BOOST_ASIO_USE_AWAITABLE_HPP
12#define BOOST_ASIO_USE_AWAITABLE_HPP
13
14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15# pragma once
16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18#include <boost/asio/detail/config.hpp>
19
20#if defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
21
22#include <boost/asio/awaitable.hpp>
20effc67
TL
23#include <boost/asio/detail/handler_tracking.hpp>
24
25#if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
26# if defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
27# include <boost/asio/detail/source_location.hpp>
28# endif // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
29#endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
92f5a8d4
TL
30
31#include <boost/asio/detail/push_options.hpp>
32
33namespace boost {
34namespace asio {
35
36/// A completion token that represents the currently executing coroutine.
37/**
38 * The @c use_awaitable_t class, with its value @c use_awaitable, is used to
39 * represent the currently executing coroutine. This completion token may be
40 * passed as a handler to an asynchronous operation. For example:
41 *
42 * @code awaitable<void> my_coroutine()
43 * {
44 * std::size_t n = co_await my_socket.async_read_some(buffer, use_awaitable);
45 * ...
46 * } @endcode
47 *
48 * When used with co_await, the initiating function (@c async_read_some in the
49 * above example) suspends the current coroutine. The coroutine is resumed when
50 * the asynchronous operation completes, and the result of the operation is
51 * returned.
52 */
20effc67 53template <typename Executor = any_io_executor>
92f5a8d4
TL
54struct use_awaitable_t
55{
56 /// Default constructor.
20effc67
TL
57 BOOST_ASIO_CONSTEXPR use_awaitable_t(
58#if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
59# if defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
60 detail::source_location location = detail::source_location::current()
61# endif // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
62#endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
63 )
64#if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
65# if defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
66 : file_name_(location.file_name()),
67 line_(location.line()),
68 function_name_(location.function_name())
69# else // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
70 : file_name_(0),
71 line_(0),
72 function_name_(0)
73# endif // defined(BOOST_ASIO_HAS_SOURCE_LOCATION)
74#endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
92f5a8d4
TL
75 {
76 }
77
20effc67
TL
78 /// Constructor used to specify file name, line, and function name.
79 BOOST_ASIO_CONSTEXPR use_awaitable_t(const char* file_name,
80 int line, const char* function_name)
81#if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
82 : file_name_(file_name),
83 line_(line),
84 function_name_(function_name)
85#endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
86 {
87#if !defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
88 (void)file_name;
89 (void)line;
90 (void)function_name;
91#endif // !defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
92 }
93
92f5a8d4
TL
94 /// Adapts an executor to add the @c use_awaitable_t completion token as the
95 /// default.
96 template <typename InnerExecutor>
97 struct executor_with_default : InnerExecutor
98 {
99 /// Specify @c use_awaitable_t as the default completion token type.
100 typedef use_awaitable_t default_completion_token_type;
101
102 /// Construct the adapted executor from the inner executor type.
103 executor_with_default(const InnerExecutor& ex) BOOST_ASIO_NOEXCEPT
104 : InnerExecutor(ex)
105 {
106 }
20effc67
TL
107
108 /// Convert the specified executor to the inner executor type, then use
109 /// that to construct the adapted executor.
110 template <typename OtherExecutor>
111 executor_with_default(const OtherExecutor& ex,
112 typename enable_if<
113 is_convertible<OtherExecutor, InnerExecutor>::value
114 >::type* = 0) BOOST_ASIO_NOEXCEPT
115 : InnerExecutor(ex)
116 {
117 }
92f5a8d4
TL
118 };
119
120 /// Type alias to adapt an I/O object to use @c use_awaitable_t as its
121 /// default completion token type.
122#if defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES) \
123 || defined(GENERATING_DOCUMENTATION)
124 template <typename T>
125 using as_default_on_t = typename T::template rebind_executor<
126 executor_with_default<typename T::executor_type> >::other;
127#endif // defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
128 // || defined(GENERATING_DOCUMENTATION)
129
130 /// Function helper to adapt an I/O object to use @c use_awaitable_t as its
131 /// default completion token type.
132 template <typename T>
f67539c2
TL
133 static typename decay<T>::type::template rebind_executor<
134 executor_with_default<typename decay<T>::type::executor_type>
92f5a8d4
TL
135 >::other
136 as_default_on(BOOST_ASIO_MOVE_ARG(T) object)
137 {
f67539c2
TL
138 return typename decay<T>::type::template rebind_executor<
139 executor_with_default<typename decay<T>::type::executor_type>
140 >::other(BOOST_ASIO_MOVE_CAST(T)(object));
92f5a8d4 141 }
20effc67
TL
142
143#if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
144 const char* file_name_;
145 int line_;
146 const char* function_name_;
147#endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
92f5a8d4
TL
148};
149
150/// A completion token object that represents the currently executing coroutine.
151/**
152 * See the documentation for boost::asio::use_awaitable_t for a usage example.
153 */
20effc67 154#if defined(GENERATING_DOCUMENTATION)
92f5a8d4 155constexpr use_awaitable_t<> use_awaitable;
20effc67
TL
156#elif defined(BOOST_ASIO_HAS_CONSTEXPR)
157constexpr use_awaitable_t<> use_awaitable(0, 0, 0);
92f5a8d4 158#elif defined(BOOST_ASIO_MSVC)
20effc67 159__declspec(selectany) use_awaitable_t<> use_awaitable(0, 0, 0);
92f5a8d4
TL
160#endif
161
162} // namespace asio
163} // namespace boost
164
165#include <boost/asio/detail/pop_options.hpp>
166
167#include <boost/asio/impl/use_awaitable.hpp>
168
169#endif // defined(BOOST_ASIO_HAS_CO_AWAIT) || defined(GENERATING_DOCUMENTATION)
170
171#endif // BOOST_ASIO_USE_AWAITABLE_HPP