]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/experimental/detail/partial_promise.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / asio / experimental / detail / partial_promise.hpp
1 //
2 // experimental/detail/partial_promise.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_DETAIL_PARTIAL_PROMISE_HPP
13 #define BOOST_ASIO_EXPERIMENTAL_DETAIL_PARTIAL_PROMISE_HPP
14
15 #include <boost/asio/detail/config.hpp>
16 #include <boost/asio/awaitable.hpp>
17 #include <boost/asio/experimental/coro_traits.hpp>
18
19 #if defined(BOOST_ASIO_HAS_STD_COROUTINE)
20 # include <coroutine>
21 #else // defined(BOOST_ASIO_HAS_STD_COROUTINE)
22 # include <experimental/coroutine>
23 #endif // defined(BOOST_ASIO_HAS_STD_COROUTINE)
24
25 namespace boost {
26 namespace asio {
27 namespace experimental {
28 namespace detail {
29
30 #if defined(BOOST_ASIO_HAS_STD_COROUTINE)
31
32 using std::coroutine_handle;
33 using std::coroutine_traits;
34 using std::suspend_never;
35 using std::suspend_always;
36 using std::noop_coroutine;
37
38 #else // defined(BOOST_ASIO_HAS_STD_COROUTINE)
39
40 using std::experimental::coroutine_handle;
41 using std::experimental::coroutine_traits;
42 using std::experimental::suspend_never;
43 using std::experimental::suspend_always;
44 using std::experimental::noop_coroutine;
45
46 #endif // defined(BOOST_ASIO_HAS_STD_COROUTINE)
47
48 struct partial_promise
49 {
50 auto initial_suspend() noexcept
51 {
52 return boost::asio::detail::suspend_always{};
53 }
54
55 auto final_suspend() noexcept
56 {
57 struct awaitable_t
58 {
59 partial_promise *p;
60
61 constexpr bool await_ready() noexcept { return true; }
62
63 auto await_suspend(boost::asio::detail::coroutine_handle<>) noexcept
64 {
65 p->get_return_object().destroy();
66 }
67
68 constexpr void await_resume() noexcept {}
69 };
70
71 return awaitable_t{this};
72 }
73
74 void return_void() {}
75
76 coroutine_handle<partial_promise> get_return_object()
77 {
78 return coroutine_handle<partial_promise>::from_promise(*this);
79 }
80
81 void unhandled_exception()
82 {
83 assert(false);
84 }
85 };
86
87 } // namespace detail
88 } // namespace experimental
89 } // namespace asio
90 } // namespace boost
91
92 #if defined(BOOST_ASIO_HAS_STD_COROUTINE)
93
94 namespace std {
95
96 template <typename ... Args>
97 struct coroutine_traits<
98 coroutine_handle<boost::asio::experimental::detail::partial_promise>,
99 Args...>
100 {
101 using promise_type = boost::asio::experimental::detail::partial_promise;
102 };
103
104 } // namespace std
105
106 #else // defined(BOOST_ASIO_HAS_STD_COROUTINE)
107
108 namespace std { namespace experimental {
109
110 template <typename... Args>
111 struct coroutine_traits<
112 coroutine_handle<boost::asio::experimental::detail::partial_promise>,
113 Args...>
114 {
115 using promise_type = boost::asio::experimental::detail::partial_promise;
116 };
117
118 }} // namespace std::experimental
119
120 #endif // defined(BOOST_ASIO_HAS_STD_COROUTINE)
121
122 namespace boost {
123 namespace asio {
124 namespace experimental {
125 namespace detail {
126
127 template <typename CompletionToken>
128 auto post_coroutine(CompletionToken token) noexcept
129 -> coroutine_handle<partial_promise>
130 {
131 post(std::move(token));
132 co_return;
133 }
134
135 template <execution::executor Executor, typename CompletionToken>
136 auto post_coroutine(Executor exec, CompletionToken token) noexcept
137 -> coroutine_handle<partial_promise>
138 {
139 post(exec, std::move(token));
140 co_return;
141 }
142
143 template <detail::execution_context Context, typename CompletionToken>
144 auto post_coroutine(Context &ctx, CompletionToken token) noexcept
145 -> coroutine_handle<partial_promise>
146 {
147 post(ctx, std::move(token));
148 co_return;
149 }
150
151 template <typename CompletionToken>
152 auto dispatch_coroutine(CompletionToken token) noexcept
153 -> coroutine_handle<partial_promise>
154 {
155 dispatch(std::move(token));
156 co_return;
157 }
158
159 template <execution::executor Executor, typename CompletionToken>
160 auto dispatch_coroutine(Executor exec, CompletionToken token) noexcept
161 -> coroutine_handle<partial_promise>
162 {
163 dispatch(exec, std::move(token));
164 co_return;
165 }
166
167 template <detail::execution_context Context, typename CompletionToken>
168 auto dispatch_coroutine(Context &ctx, CompletionToken token) noexcept
169 -> coroutine_handle<partial_promise>
170 {
171 dispatch(ctx, std::move(token));
172 co_return;
173 }
174
175 } // namespace detail
176 } // namespace experimental
177 } // namespace asio
178 } // namespace boost
179
180 #endif // BOOST_ASIO_EXPERIMENTAL_DETAIL_PARTIAL_PROMISE_HPP