]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/experimental/co_spawn.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / asio / experimental / co_spawn.hpp
1 //
2 // experimental/co_spawn.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 #ifndef BOOST_ASIO_EXPERIMENTAL_CO_SPAWN_HPP
12 #define BOOST_ASIO_EXPERIMENTAL_CO_SPAWN_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 #include <utility>
20 #include <boost/asio/compose.hpp>
21 #include <boost/asio/detail/type_traits.hpp>
22 #include <boost/asio/experimental/coro.hpp>
23 #include <boost/asio/experimental/deferred.hpp>
24 #include <boost/asio/experimental/prepend.hpp>
25 #include <boost/asio/redirect_error.hpp>
26
27 #include <boost/asio/detail/push_options.hpp>
28
29 namespace boost {
30 namespace asio {
31 namespace experimental {
32 namespace detail {
33
34 template <typename T, typename U, typename Executor>
35 struct coro_spawn_op
36 {
37 coro<T, U, Executor> c;
38
39 void operator()(auto& self)
40 {
41 auto op = c.async_resume(deferred);
42 std::move(op)((prepend)(std::move(self), 0));
43 }
44
45 void operator()(auto& self, int, auto... res)
46 {
47 self.complete(std::move(res)...);
48 }
49 };
50
51 } // namespace detail
52
53 /// Spawn a resumable coroutine.
54 /**
55 * This function spawns the coroutine for execution on its executor. It binds
56 * the lifetime of the coroutine to the executor.
57 *
58 * @param c The coroutine
59 *
60 * @param token The completion token
61 *
62 * @returns Implementation defined
63 */
64 template <typename T, typename Executor, typename CompletionToken>
65 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
66 CompletionToken, void(std::exception_ptr, T))
67 co_spawn(coro<void, T, Executor> c, CompletionToken&& token)
68 {
69 auto exec = c.get_executor();
70 return async_compose<CompletionToken, void(std::exception_ptr, T)>(
71 detail::coro_spawn_op<void, T, Executor>{std::move(c)},
72 token, exec);
73 }
74
75 /// Spawn a resumable coroutine.
76 /**
77 * This function spawns the coroutine for execution on its executor. It binds
78 * the lifetime of the coroutine to the executor.
79 *
80 * @param c The coroutine
81 *
82 * @param token The completion token
83 *
84 * @returns Implementation defined
85 */
86 template <typename T, typename Executor, typename CompletionToken>
87 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
88 CompletionToken, void(std::exception_ptr, T))
89 co_spawn(coro<void(), T, Executor> c, CompletionToken&& token)
90 {
91 auto exec = c.get_executor();
92 return async_compose<CompletionToken, void(std::exception_ptr, T)>(
93 detail::coro_spawn_op<void(), T, Executor>{std::move(c)},
94 token, exec);
95 }
96
97 /// Spawn a resumable coroutine.
98 /**
99 * This function spawns the coroutine for execution on its executor. It binds
100 * the lifetime of the coroutine to the executor.
101 *
102 * @param c The coroutine
103 *
104 * @param token The completion token
105 *
106 * @returns Implementation defined
107 */
108 template <typename T, typename Executor, typename CompletionToken>
109 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(T))
110 co_spawn(coro<void() noexcept, T, Executor> c, CompletionToken&& token)
111 {
112 auto exec = c.get_executor();
113 return async_compose<CompletionToken, void(T)>(
114 detail::coro_spawn_op<void() noexcept, T, Executor>{std::move(c)},
115 token, exec);
116 }
117
118 /// Spawn a resumable coroutine.
119 /**
120 * This function spawns the coroutine for execution on its executor. It binds
121 * the lifetime of the coroutine to the executor.
122 *
123 * @param c The coroutine
124 *
125 * @param token The completion token
126 *
127 * @returns Implementation defined
128 */
129 template <typename Executor, typename CompletionToken>
130 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
131 CompletionToken, void(std::exception_ptr))
132 co_spawn(coro<void, void, Executor> c, CompletionToken&& token)
133 {
134 auto exec = c.get_executor();
135 return async_compose<CompletionToken, void(std::exception_ptr)>(
136 detail::coro_spawn_op<void, void, Executor>{std::move(c)},
137 token, exec);
138 }
139
140 /// Spawn a resumable coroutine.
141 /**
142 * This function spawns the coroutine for execution on its executor. It binds
143 * the lifetime of the coroutine to the executor.
144 *
145 * @param c The coroutine
146 *
147 * @param token The completion token
148 *
149 * @returns Implementation defined
150 */
151 template <typename Executor, typename CompletionToken>
152 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
153 CompletionToken, void(std::exception_ptr))
154 co_spawn(coro<void(), void, Executor> c, CompletionToken&& token)
155 {
156 auto exec = c.get_executor();
157 return async_compose<CompletionToken, void(std::exception_ptr)>(
158 detail::coro_spawn_op<void(), void, Executor>{std::move(c)},
159 token, exec);
160 }
161
162 /// Spawn a resumable coroutine.
163 /**
164 * This function spawns the coroutine for execution on its executor. It binds
165 * the lifetime of the coroutine to the executor.
166 *
167 * @param c The coroutine
168 *
169 * @param token The completion token
170 *
171 * @returns Implementation defined
172 */
173 template <typename Executor, typename CompletionToken>
174 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void())
175 co_spawn(coro<void() noexcept, void, Executor> c, CompletionToken&& token)
176 {
177 auto exec = c.get_executor();
178 return async_compose<CompletionToken, void()>(
179 detail::coro_spawn_op<void() noexcept, void, Executor>{std::move(c)},
180 token, exec);
181 }
182
183 } // namespace detail
184 } // namespace asio
185 } // namespace boost
186
187 #include <boost/asio/detail/pop_options.hpp>
188
189 #endif //BOOST_ASIO_EXPERIMENTAL_CO_SPAWN_HPP