]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/wrapped_handler.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
1e59de90 | 5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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_DETAIL_WRAPPED_HANDLER_HPP | |
12 | #define BOOST_ASIO_DETAIL_WRAPPED_HANDLER_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/bind_handler.hpp> | |
19 | #include <boost/asio/detail/handler_alloc_helpers.hpp> | |
20 | #include <boost/asio/detail/handler_cont_helpers.hpp> | |
21 | #include <boost/asio/detail/handler_invoke_helpers.hpp> | |
22 | ||
23 | #include <boost/asio/detail/push_options.hpp> | |
24 | ||
25 | namespace boost { | |
26 | namespace asio { | |
27 | namespace detail { | |
28 | ||
29 | struct is_continuation_delegated | |
30 | { | |
31 | template <typename Dispatcher, typename Handler> | |
32 | bool operator()(Dispatcher&, Handler& handler) const | |
33 | { | |
34 | return boost_asio_handler_cont_helpers::is_continuation(handler); | |
35 | } | |
36 | }; | |
37 | ||
38 | struct is_continuation_if_running | |
39 | { | |
40 | template <typename Dispatcher, typename Handler> | |
41 | bool operator()(Dispatcher& dispatcher, Handler&) const | |
42 | { | |
43 | return dispatcher.running_in_this_thread(); | |
44 | } | |
45 | }; | |
46 | ||
47 | template <typename Dispatcher, typename Handler, | |
48 | typename IsContinuation = is_continuation_delegated> | |
49 | class wrapped_handler | |
50 | { | |
51 | public: | |
52 | typedef void result_type; | |
53 | ||
54 | wrapped_handler(Dispatcher dispatcher, Handler& handler) | |
55 | : dispatcher_(dispatcher), | |
56 | handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)) | |
57 | { | |
58 | } | |
59 | ||
60 | #if defined(BOOST_ASIO_HAS_MOVE) | |
61 | wrapped_handler(const wrapped_handler& other) | |
62 | : dispatcher_(other.dispatcher_), | |
63 | handler_(other.handler_) | |
64 | { | |
65 | } | |
66 | ||
67 | wrapped_handler(wrapped_handler&& other) | |
68 | : dispatcher_(other.dispatcher_), | |
69 | handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_)) | |
70 | { | |
71 | } | |
72 | #endif // defined(BOOST_ASIO_HAS_MOVE) | |
73 | ||
74 | void operator()() | |
75 | { | |
76 | dispatcher_.dispatch(BOOST_ASIO_MOVE_CAST(Handler)(handler_)); | |
77 | } | |
78 | ||
79 | void operator()() const | |
80 | { | |
81 | dispatcher_.dispatch(handler_); | |
82 | } | |
83 | ||
84 | template <typename Arg1> | |
85 | void operator()(const Arg1& arg1) | |
86 | { | |
87 | dispatcher_.dispatch(detail::bind_handler(handler_, arg1)); | |
88 | } | |
89 | ||
90 | template <typename Arg1> | |
91 | void operator()(const Arg1& arg1) const | |
92 | { | |
93 | dispatcher_.dispatch(detail::bind_handler(handler_, arg1)); | |
94 | } | |
95 | ||
96 | template <typename Arg1, typename Arg2> | |
97 | void operator()(const Arg1& arg1, const Arg2& arg2) | |
98 | { | |
99 | dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2)); | |
100 | } | |
101 | ||
102 | template <typename Arg1, typename Arg2> | |
103 | void operator()(const Arg1& arg1, const Arg2& arg2) const | |
104 | { | |
105 | dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2)); | |
106 | } | |
107 | ||
108 | template <typename Arg1, typename Arg2, typename Arg3> | |
109 | void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) | |
110 | { | |
111 | dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3)); | |
112 | } | |
113 | ||
114 | template <typename Arg1, typename Arg2, typename Arg3> | |
115 | void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) const | |
116 | { | |
117 | dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3)); | |
118 | } | |
119 | ||
120 | template <typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
121 | void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, | |
122 | const Arg4& arg4) | |
123 | { | |
124 | dispatcher_.dispatch( | |
125 | detail::bind_handler(handler_, arg1, arg2, arg3, arg4)); | |
126 | } | |
127 | ||
128 | template <typename Arg1, typename Arg2, typename Arg3, typename Arg4> | |
129 | void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, | |
130 | const Arg4& arg4) const | |
131 | { | |
132 | dispatcher_.dispatch( | |
133 | detail::bind_handler(handler_, arg1, arg2, arg3, arg4)); | |
134 | } | |
135 | ||
136 | template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, | |
137 | typename Arg5> | |
138 | void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, | |
139 | const Arg4& arg4, const Arg5& arg5) | |
140 | { | |
141 | dispatcher_.dispatch( | |
142 | detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5)); | |
143 | } | |
144 | ||
145 | template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, | |
146 | typename Arg5> | |
147 | void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, | |
148 | const Arg4& arg4, const Arg5& arg5) const | |
149 | { | |
150 | dispatcher_.dispatch( | |
151 | detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5)); | |
152 | } | |
153 | ||
154 | //private: | |
155 | Dispatcher dispatcher_; | |
156 | Handler handler_; | |
157 | }; | |
158 | ||
159 | template <typename Handler, typename Context> | |
160 | class rewrapped_handler | |
161 | { | |
162 | public: | |
163 | explicit rewrapped_handler(Handler& handler, const Context& context) | |
164 | : context_(context), | |
165 | handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)) | |
166 | { | |
167 | } | |
168 | ||
169 | explicit rewrapped_handler(const Handler& handler, const Context& context) | |
170 | : context_(context), | |
171 | handler_(handler) | |
172 | { | |
173 | } | |
174 | ||
175 | #if defined(BOOST_ASIO_HAS_MOVE) | |
176 | rewrapped_handler(const rewrapped_handler& other) | |
177 | : context_(other.context_), | |
178 | handler_(other.handler_) | |
179 | { | |
180 | } | |
181 | ||
182 | rewrapped_handler(rewrapped_handler&& other) | |
183 | : context_(BOOST_ASIO_MOVE_CAST(Context)(other.context_)), | |
184 | handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_)) | |
185 | { | |
186 | } | |
187 | #endif // defined(BOOST_ASIO_HAS_MOVE) | |
188 | ||
189 | void operator()() | |
190 | { | |
191 | handler_(); | |
192 | } | |
193 | ||
194 | void operator()() const | |
195 | { | |
196 | handler_(); | |
197 | } | |
198 | ||
199 | //private: | |
200 | Context context_; | |
201 | Handler handler_; | |
202 | }; | |
203 | ||
204 | template <typename Dispatcher, typename Handler, typename IsContinuation> | |
20effc67 TL |
205 | inline asio_handler_allocate_is_deprecated |
206 | asio_handler_allocate(std::size_t size, | |
7c673cae FG |
207 | wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler) |
208 | { | |
20effc67 TL |
209 | #if defined(BOOST_ASIO_NO_DEPRECATED) |
210 | boost_asio_handler_alloc_helpers::allocate(size, this_handler->handler_); | |
211 | return asio_handler_allocate_is_no_longer_used(); | |
212 | #else // defined(BOOST_ASIO_NO_DEPRECATED) | |
7c673cae FG |
213 | return boost_asio_handler_alloc_helpers::allocate( |
214 | size, this_handler->handler_); | |
20effc67 | 215 | #endif // defined(BOOST_ASIO_NO_DEPRECATED) |
7c673cae FG |
216 | } |
217 | ||
218 | template <typename Dispatcher, typename Handler, typename IsContinuation> | |
20effc67 TL |
219 | inline asio_handler_deallocate_is_deprecated |
220 | asio_handler_deallocate(void* pointer, std::size_t size, | |
7c673cae FG |
221 | wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler) |
222 | { | |
223 | boost_asio_handler_alloc_helpers::deallocate( | |
224 | pointer, size, this_handler->handler_); | |
20effc67 TL |
225 | #if defined(BOOST_ASIO_NO_DEPRECATED) |
226 | return asio_handler_deallocate_is_no_longer_used(); | |
227 | #endif // defined(BOOST_ASIO_NO_DEPRECATED) | |
7c673cae FG |
228 | } |
229 | ||
230 | template <typename Dispatcher, typename Handler, typename IsContinuation> | |
231 | inline bool asio_handler_is_continuation( | |
232 | wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler) | |
233 | { | |
234 | return IsContinuation()(this_handler->dispatcher_, this_handler->handler_); | |
235 | } | |
236 | ||
237 | template <typename Function, typename Dispatcher, | |
238 | typename Handler, typename IsContinuation> | |
20effc67 TL |
239 | inline asio_handler_invoke_is_deprecated |
240 | asio_handler_invoke(Function& function, | |
7c673cae FG |
241 | wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler) |
242 | { | |
243 | this_handler->dispatcher_.dispatch( | |
244 | rewrapped_handler<Function, Handler>( | |
245 | function, this_handler->handler_)); | |
20effc67 TL |
246 | #if defined(BOOST_ASIO_NO_DEPRECATED) |
247 | return asio_handler_invoke_is_no_longer_used(); | |
248 | #endif // defined(BOOST_ASIO_NO_DEPRECATED) | |
7c673cae FG |
249 | } |
250 | ||
251 | template <typename Function, typename Dispatcher, | |
252 | typename Handler, typename IsContinuation> | |
20effc67 TL |
253 | inline asio_handler_invoke_is_deprecated |
254 | asio_handler_invoke(const Function& function, | |
7c673cae FG |
255 | wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler) |
256 | { | |
257 | this_handler->dispatcher_.dispatch( | |
258 | rewrapped_handler<Function, Handler>( | |
259 | function, this_handler->handler_)); | |
20effc67 TL |
260 | #if defined(BOOST_ASIO_NO_DEPRECATED) |
261 | return asio_handler_invoke_is_no_longer_used(); | |
262 | #endif // defined(BOOST_ASIO_NO_DEPRECATED) | |
7c673cae FG |
263 | } |
264 | ||
265 | template <typename Handler, typename Context> | |
20effc67 TL |
266 | inline asio_handler_allocate_is_deprecated |
267 | asio_handler_allocate(std::size_t size, | |
7c673cae FG |
268 | rewrapped_handler<Handler, Context>* this_handler) |
269 | { | |
20effc67 TL |
270 | #if defined(BOOST_ASIO_NO_DEPRECATED) |
271 | boost_asio_handler_alloc_helpers::allocate(size, this_handler->handler_); | |
272 | return asio_handler_allocate_is_no_longer_used(); | |
273 | #else // defined(BOOST_ASIO_NO_DEPRECATED) | |
7c673cae | 274 | return boost_asio_handler_alloc_helpers::allocate( |
20effc67 TL |
275 | size, this_handler->handler_); |
276 | #endif // defined(BOOST_ASIO_NO_DEPRECATED) | |
7c673cae FG |
277 | } |
278 | ||
279 | template <typename Handler, typename Context> | |
20effc67 TL |
280 | inline asio_handler_deallocate_is_deprecated |
281 | asio_handler_deallocate(void* pointer, std::size_t size, | |
7c673cae FG |
282 | rewrapped_handler<Handler, Context>* this_handler) |
283 | { | |
284 | boost_asio_handler_alloc_helpers::deallocate( | |
285 | pointer, size, this_handler->context_); | |
20effc67 TL |
286 | #if defined(BOOST_ASIO_NO_DEPRECATED) |
287 | return asio_handler_deallocate_is_no_longer_used(); | |
288 | #endif // defined(BOOST_ASIO_NO_DEPRECATED) | |
7c673cae FG |
289 | } |
290 | ||
291 | template <typename Dispatcher, typename Context> | |
292 | inline bool asio_handler_is_continuation( | |
293 | rewrapped_handler<Dispatcher, Context>* this_handler) | |
294 | { | |
295 | return boost_asio_handler_cont_helpers::is_continuation( | |
296 | this_handler->context_); | |
297 | } | |
298 | ||
299 | template <typename Function, typename Handler, typename Context> | |
20effc67 TL |
300 | inline asio_handler_invoke_is_deprecated |
301 | asio_handler_invoke(Function& function, | |
7c673cae FG |
302 | rewrapped_handler<Handler, Context>* this_handler) |
303 | { | |
304 | boost_asio_handler_invoke_helpers::invoke( | |
305 | function, this_handler->context_); | |
20effc67 TL |
306 | #if defined(BOOST_ASIO_NO_DEPRECATED) |
307 | return asio_handler_invoke_is_no_longer_used(); | |
308 | #endif // defined(BOOST_ASIO_NO_DEPRECATED) | |
7c673cae FG |
309 | } |
310 | ||
311 | template <typename Function, typename Handler, typename Context> | |
20effc67 TL |
312 | inline asio_handler_invoke_is_deprecated |
313 | asio_handler_invoke(const Function& function, | |
7c673cae FG |
314 | rewrapped_handler<Handler, Context>* this_handler) |
315 | { | |
316 | boost_asio_handler_invoke_helpers::invoke( | |
317 | function, this_handler->context_); | |
20effc67 TL |
318 | #if defined(BOOST_ASIO_NO_DEPRECATED) |
319 | return asio_handler_invoke_is_no_longer_used(); | |
320 | #endif // defined(BOOST_ASIO_NO_DEPRECATED) | |
7c673cae FG |
321 | } |
322 | ||
323 | } // namespace detail | |
324 | } // namespace asio | |
325 | } // namespace boost | |
326 | ||
327 | #include <boost/asio/detail/pop_options.hpp> | |
328 | ||
329 | #endif // BOOST_ASIO_DETAIL_WRAPPED_HANDLER_HPP |