]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // |
2 | // experimental/use_coro.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2021-2022 Klemens D. Morgenstern | |
6 | // (klemens dot morgenstern at gmx dot net) | |
7 | // | |
8 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
9 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
10 | // | |
11 | ||
12 | #ifndef BOOST_ASIO_EXPERIMENTAL_USE_CORO_HPP | |
13 | #define BOOST_ASIO_EXPERIMENTAL_USE_CORO_HPP | |
14 | ||
15 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
16 | # pragma once | |
17 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
18 | ||
19 | #include <boost/asio/detail/config.hpp> | |
20 | #include <optional> | |
21 | #include <boost/asio/bind_cancellation_slot.hpp> | |
22 | #include <boost/asio/bind_executor.hpp> | |
23 | #include <boost/system/error_code.hpp> | |
24 | #include <boost/asio/experimental/detail/partial_promise.hpp> | |
25 | ||
26 | #include <boost/asio/detail/push_options.hpp> | |
27 | ||
28 | namespace boost { | |
29 | namespace asio { | |
30 | ||
31 | class any_io_executor; | |
32 | ||
33 | namespace experimental { | |
34 | ||
35 | /// A @ref completion_token that represents the currently executing resumable | |
36 | /// coroutine. | |
37 | /** | |
38 | * The @c use_coro_t class, with its value @c use_coro, is used to represent an | |
39 | * operation that can be awaited by the current resumable coroutine. This | |
40 | * completion token may be passed as a handler to an asynchronous operation. | |
41 | * For example: | |
42 | * | |
43 | * @code coro<void> my_coroutine(tcp::socket my_socket) | |
44 | * { | |
45 | * std::size_t n = co_await my_socket.async_read_some(buffer, use_coro); | |
46 | * ... | |
47 | * } @endcode | |
48 | * | |
49 | * When used with co_await, the initiating function (@c async_read_some in the | |
50 | * above example) suspends the current coroutine. The coroutine is resumed when | |
51 | * the asynchronous operation completes, and the result of the operation is | |
52 | * returned. | |
53 | */ | |
54 | template <typename Executor = any_io_executor> | |
55 | struct use_coro_t | |
56 | { | |
57 | /// Default constructor. | |
58 | BOOST_ASIO_CONSTEXPR use_coro_t( | |
59 | #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) | |
60 | # if defined(BOOST_ASIO_HAS_SOURCE_LOCATION) | |
61 | boost::asio::detail::source_location location = | |
62 | boost::asio::detail::source_location::current() | |
63 | # endif // defined(BOOST_ASIO_HAS_SOURCE_LOCATION) | |
64 | #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) | |
65 | ) | |
66 | #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) | |
67 | # if defined(BOOST_ASIO_HAS_SOURCE_LOCATION) | |
68 | : file_name_(location.file_name()), | |
69 | line_(location.line()), | |
70 | function_name_(location.function_name()) | |
71 | # else // defined(BOOST_ASIO_HAS_SOURCE_LOCATION) | |
72 | : file_name_(0), | |
73 | line_(0), | |
74 | function_name_(0) | |
75 | # endif // defined(BOOST_ASIO_HAS_SOURCE_LOCATION) | |
76 | #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) | |
77 | { | |
78 | } | |
79 | ||
80 | /// Constructor used to specify file name, line, and function name. | |
81 | BOOST_ASIO_CONSTEXPR use_coro_t(const char* file_name, | |
82 | int line, const char* function_name) | |
83 | #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) | |
84 | : file_name_(file_name), | |
85 | line_(line), | |
86 | function_name_(function_name) | |
87 | #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) | |
88 | { | |
89 | #if !defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) | |
90 | (void)file_name; | |
91 | (void)line; | |
92 | (void)function_name; | |
93 | #endif // !defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) | |
94 | } | |
95 | ||
96 | /// Adapts an executor to add the @c use_coro_t completion token as the | |
97 | /// default. | |
98 | template <typename InnerExecutor> | |
99 | struct executor_with_default : InnerExecutor | |
100 | { | |
101 | /// Specify @c use_coro_t as the default completion token type. | |
102 | typedef use_coro_t default_completion_token_type; | |
103 | ||
104 | /// Construct the adapted executor from the inner executor type. | |
105 | template <typename InnerExecutor1> | |
106 | executor_with_default(const InnerExecutor1& ex, | |
107 | typename constraint< | |
108 | conditional< | |
109 | !is_same<InnerExecutor1, executor_with_default>::value, | |
110 | is_convertible<InnerExecutor1, InnerExecutor>, | |
111 | false_type | |
112 | >::type::value | |
113 | >::type = 0) BOOST_ASIO_NOEXCEPT | |
114 | : InnerExecutor(ex) | |
115 | { | |
116 | } | |
117 | }; | |
118 | ||
119 | /// Type alias to adapt an I/O object to use @c use_coro_t as its | |
120 | /// default completion token type. | |
121 | #if defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES) \ | |
122 | || defined(GENERATING_DOCUMENTATION) | |
123 | template <typename T> | |
124 | using as_default_on_t = typename T::template rebind_executor< | |
125 | executor_with_default<typename T::executor_type> >::other; | |
126 | #endif // defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES) | |
127 | // || defined(GENERATING_DOCUMENTATION) | |
128 | ||
129 | /// Function helper to adapt an I/O object to use @c use_coro_t as its | |
130 | /// default completion token type. | |
131 | template <typename T> | |
132 | static typename decay<T>::type::template rebind_executor< | |
133 | executor_with_default<typename decay<T>::type::executor_type> | |
134 | >::other | |
135 | as_default_on(BOOST_ASIO_MOVE_ARG(T) object) | |
136 | { | |
137 | return typename decay<T>::type::template rebind_executor< | |
138 | executor_with_default<typename decay<T>::type::executor_type> | |
139 | >::other(BOOST_ASIO_MOVE_CAST(T)(object)); | |
140 | } | |
141 | ||
142 | #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) | |
143 | const char* file_name_; | |
144 | int line_; | |
145 | const char* function_name_; | |
146 | #endif // defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) | |
147 | }; | |
148 | ||
149 | /// A @ref completion_token object that represents the currently executing | |
150 | /// resumable coroutine. | |
151 | /** | |
152 | * See the documentation for boost::asio::use_coro_t for a usage example. | |
153 | */ | |
154 | #if defined(GENERATING_DOCUMENTATION) | |
155 | constexpr use_coro_t<> use_coro; | |
156 | #elif defined(BOOST_ASIO_HAS_CONSTEXPR) | |
157 | constexpr use_coro_t<> use_coro(0, 0, 0); | |
158 | #elif defined(BOOST_ASIO_MSVC) | |
159 | __declspec(selectany) use_coro_t<> use_coro(0, 0, 0); | |
160 | #endif | |
161 | ||
162 | } // namespace experimental | |
163 | } // namespace asio | |
164 | } // namespace boost | |
165 | ||
166 | #include <boost/asio/detail/pop_options.hpp> | |
167 | ||
168 | #include <boost/asio/experimental/impl/use_coro.hpp> | |
169 | #include <boost/asio/experimental/coro.hpp> | |
170 | ||
171 | #endif // BOOST_ASIO_EXPERIMENTAL_USE_CORO_HPP |