]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // |
2 | // bind_executor.hpp | |
3 | // ~~~~~~~~~~~~~~~~~ | |
4 | // | |
1e59de90 | 5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
b32b8144 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_BIND_EXECUTOR_HPP | |
12 | #define BOOST_ASIO_BIND_EXECUTOR_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 <boost/asio/detail/type_traits.hpp> | |
20 | #include <boost/asio/detail/variadic_templates.hpp> | |
21 | #include <boost/asio/associated_executor.hpp> | |
1e59de90 | 22 | #include <boost/asio/associator.hpp> |
b32b8144 | 23 | #include <boost/asio/async_result.hpp> |
20effc67 | 24 | #include <boost/asio/execution/executor.hpp> |
b32b8144 FG |
25 | #include <boost/asio/execution_context.hpp> |
26 | #include <boost/asio/is_executor.hpp> | |
27 | #include <boost/asio/uses_executor.hpp> | |
28 | ||
29 | #include <boost/asio/detail/push_options.hpp> | |
30 | ||
31 | namespace boost { | |
32 | namespace asio { | |
33 | namespace detail { | |
34 | ||
b32b8144 FG |
35 | // Helper to automatically define nested typedef result_type. |
36 | ||
37 | template <typename T, typename = void> | |
38 | struct executor_binder_result_type | |
39 | { | |
40 | protected: | |
41 | typedef void result_type_or_void; | |
42 | }; | |
43 | ||
44 | template <typename T> | |
45 | struct executor_binder_result_type<T, | |
20effc67 | 46 | typename void_type<typename T::result_type>::type> |
b32b8144 FG |
47 | { |
48 | typedef typename T::result_type result_type; | |
49 | protected: | |
50 | typedef result_type result_type_or_void; | |
51 | }; | |
52 | ||
53 | template <typename R> | |
54 | struct executor_binder_result_type<R(*)()> | |
55 | { | |
56 | typedef R result_type; | |
57 | protected: | |
58 | typedef result_type result_type_or_void; | |
59 | }; | |
60 | ||
61 | template <typename R> | |
62 | struct executor_binder_result_type<R(&)()> | |
63 | { | |
64 | typedef R result_type; | |
65 | protected: | |
66 | typedef result_type result_type_or_void; | |
67 | }; | |
68 | ||
69 | template <typename R, typename A1> | |
70 | struct executor_binder_result_type<R(*)(A1)> | |
71 | { | |
72 | typedef R result_type; | |
73 | protected: | |
74 | typedef result_type result_type_or_void; | |
75 | }; | |
76 | ||
77 | template <typename R, typename A1> | |
78 | struct executor_binder_result_type<R(&)(A1)> | |
79 | { | |
80 | typedef R result_type; | |
81 | protected: | |
82 | typedef result_type result_type_or_void; | |
83 | }; | |
84 | ||
85 | template <typename R, typename A1, typename A2> | |
86 | struct executor_binder_result_type<R(*)(A1, A2)> | |
87 | { | |
88 | typedef R result_type; | |
89 | protected: | |
90 | typedef result_type result_type_or_void; | |
91 | }; | |
92 | ||
93 | template <typename R, typename A1, typename A2> | |
94 | struct executor_binder_result_type<R(&)(A1, A2)> | |
95 | { | |
96 | typedef R result_type; | |
97 | protected: | |
98 | typedef result_type result_type_or_void; | |
99 | }; | |
100 | ||
101 | // Helper to automatically define nested typedef argument_type. | |
102 | ||
103 | template <typename T, typename = void> | |
104 | struct executor_binder_argument_type {}; | |
105 | ||
106 | template <typename T> | |
107 | struct executor_binder_argument_type<T, | |
20effc67 | 108 | typename void_type<typename T::argument_type>::type> |
b32b8144 FG |
109 | { |
110 | typedef typename T::argument_type argument_type; | |
111 | }; | |
112 | ||
113 | template <typename R, typename A1> | |
114 | struct executor_binder_argument_type<R(*)(A1)> | |
115 | { | |
116 | typedef A1 argument_type; | |
117 | }; | |
118 | ||
119 | template <typename R, typename A1> | |
120 | struct executor_binder_argument_type<R(&)(A1)> | |
121 | { | |
122 | typedef A1 argument_type; | |
123 | }; | |
124 | ||
125 | // Helper to automatically define nested typedefs first_argument_type and | |
126 | // second_argument_type. | |
127 | ||
128 | template <typename T, typename = void> | |
129 | struct executor_binder_argument_types {}; | |
130 | ||
131 | template <typename T> | |
132 | struct executor_binder_argument_types<T, | |
20effc67 | 133 | typename void_type<typename T::first_argument_type>::type> |
b32b8144 FG |
134 | { |
135 | typedef typename T::first_argument_type first_argument_type; | |
136 | typedef typename T::second_argument_type second_argument_type; | |
137 | }; | |
138 | ||
139 | template <typename R, typename A1, typename A2> | |
140 | struct executor_binder_argument_type<R(*)(A1, A2)> | |
141 | { | |
142 | typedef A1 first_argument_type; | |
143 | typedef A2 second_argument_type; | |
144 | }; | |
145 | ||
146 | template <typename R, typename A1, typename A2> | |
147 | struct executor_binder_argument_type<R(&)(A1, A2)> | |
148 | { | |
149 | typedef A1 first_argument_type; | |
150 | typedef A2 second_argument_type; | |
151 | }; | |
152 | ||
20effc67 TL |
153 | // Helper to perform uses_executor construction of the target type, if |
154 | // required. | |
b32b8144 FG |
155 | |
156 | template <typename T, typename Executor, bool UsesExecutor> | |
157 | class executor_binder_base; | |
158 | ||
159 | template <typename T, typename Executor> | |
160 | class executor_binder_base<T, Executor, true> | |
b32b8144 FG |
161 | { |
162 | protected: | |
163 | template <typename E, typename U> | |
164 | executor_binder_base(BOOST_ASIO_MOVE_ARG(E) e, BOOST_ASIO_MOVE_ARG(U) u) | |
165 | : executor_(BOOST_ASIO_MOVE_CAST(E)(e)), | |
166 | target_(executor_arg_t(), executor_, BOOST_ASIO_MOVE_CAST(U)(u)) | |
167 | { | |
168 | } | |
169 | ||
170 | Executor executor_; | |
171 | T target_; | |
172 | }; | |
173 | ||
174 | template <typename T, typename Executor> | |
175 | class executor_binder_base<T, Executor, false> | |
176 | { | |
177 | protected: | |
178 | template <typename E, typename U> | |
179 | executor_binder_base(BOOST_ASIO_MOVE_ARG(E) e, BOOST_ASIO_MOVE_ARG(U) u) | |
180 | : executor_(BOOST_ASIO_MOVE_CAST(E)(e)), | |
181 | target_(BOOST_ASIO_MOVE_CAST(U)(u)) | |
182 | { | |
183 | } | |
184 | ||
185 | Executor executor_; | |
186 | T target_; | |
187 | }; | |
188 | ||
189 | // Helper to enable SFINAE on zero-argument operator() below. | |
190 | ||
191 | template <typename T, typename = void> | |
192 | struct executor_binder_result_of0 | |
193 | { | |
194 | typedef void type; | |
195 | }; | |
196 | ||
197 | template <typename T> | |
198 | struct executor_binder_result_of0<T, | |
20effc67 | 199 | typename void_type<typename result_of<T()>::type>::type> |
b32b8144 FG |
200 | { |
201 | typedef typename result_of<T()>::type type; | |
202 | }; | |
203 | ||
204 | } // namespace detail | |
205 | ||
206 | /// A call wrapper type to bind an executor of type @c Executor to an object of | |
207 | /// type @c T. | |
208 | template <typename T, typename Executor> | |
209 | class executor_binder | |
210 | #if !defined(GENERATING_DOCUMENTATION) | |
211 | : public detail::executor_binder_result_type<T>, | |
212 | public detail::executor_binder_argument_type<T>, | |
213 | public detail::executor_binder_argument_types<T>, | |
214 | private detail::executor_binder_base< | |
215 | T, Executor, uses_executor<T, Executor>::value> | |
216 | #endif // !defined(GENERATING_DOCUMENTATION) | |
217 | { | |
218 | public: | |
219 | /// The type of the target object. | |
220 | typedef T target_type; | |
221 | ||
222 | /// The type of the associated executor. | |
223 | typedef Executor executor_type; | |
224 | ||
225 | #if defined(GENERATING_DOCUMENTATION) | |
226 | /// The return type if a function. | |
227 | /** | |
228 | * The type of @c result_type is based on the type @c T of the wrapper's | |
229 | * target object: | |
230 | * | |
231 | * @li if @c T is a pointer to function type, @c result_type is a synonym for | |
232 | * the return type of @c T; | |
233 | * | |
234 | * @li if @c T is a class type with a member type @c result_type, then @c | |
235 | * result_type is a synonym for @c T::result_type; | |
236 | * | |
237 | * @li otherwise @c result_type is not defined. | |
238 | */ | |
239 | typedef see_below result_type; | |
240 | ||
241 | /// The type of the function's argument. | |
242 | /** | |
243 | * The type of @c argument_type is based on the type @c T of the wrapper's | |
244 | * target object: | |
245 | * | |
246 | * @li if @c T is a pointer to a function type accepting a single argument, | |
247 | * @c argument_type is a synonym for the return type of @c T; | |
248 | * | |
249 | * @li if @c T is a class type with a member type @c argument_type, then @c | |
250 | * argument_type is a synonym for @c T::argument_type; | |
251 | * | |
252 | * @li otherwise @c argument_type is not defined. | |
253 | */ | |
254 | typedef see_below argument_type; | |
255 | ||
256 | /// The type of the function's first argument. | |
257 | /** | |
258 | * The type of @c first_argument_type is based on the type @c T of the | |
259 | * wrapper's target object: | |
260 | * | |
261 | * @li if @c T is a pointer to a function type accepting two arguments, @c | |
262 | * first_argument_type is a synonym for the return type of @c T; | |
263 | * | |
264 | * @li if @c T is a class type with a member type @c first_argument_type, | |
265 | * then @c first_argument_type is a synonym for @c T::first_argument_type; | |
266 | * | |
267 | * @li otherwise @c first_argument_type is not defined. | |
268 | */ | |
269 | typedef see_below first_argument_type; | |
270 | ||
271 | /// The type of the function's second argument. | |
272 | /** | |
273 | * The type of @c second_argument_type is based on the type @c T of the | |
274 | * wrapper's target object: | |
275 | * | |
276 | * @li if @c T is a pointer to a function type accepting two arguments, @c | |
277 | * second_argument_type is a synonym for the return type of @c T; | |
278 | * | |
279 | * @li if @c T is a class type with a member type @c first_argument_type, | |
280 | * then @c second_argument_type is a synonym for @c T::second_argument_type; | |
281 | * | |
282 | * @li otherwise @c second_argument_type is not defined. | |
283 | */ | |
284 | typedef see_below second_argument_type; | |
285 | #endif // defined(GENERATING_DOCUMENTATION) | |
286 | ||
287 | /// Construct an executor wrapper for the specified object. | |
288 | /** | |
289 | * This constructor is only valid if the type @c T is constructible from type | |
290 | * @c U. | |
291 | */ | |
292 | template <typename U> | |
293 | executor_binder(executor_arg_t, const executor_type& e, | |
294 | BOOST_ASIO_MOVE_ARG(U) u) | |
295 | : base_type(e, BOOST_ASIO_MOVE_CAST(U)(u)) | |
296 | { | |
297 | } | |
298 | ||
299 | /// Copy constructor. | |
300 | executor_binder(const executor_binder& other) | |
301 | : base_type(other.get_executor(), other.get()) | |
302 | { | |
303 | } | |
304 | ||
305 | /// Construct a copy, but specify a different executor. | |
306 | executor_binder(executor_arg_t, const executor_type& e, | |
307 | const executor_binder& other) | |
308 | : base_type(e, other.get()) | |
309 | { | |
310 | } | |
311 | ||
312 | /// Construct a copy of a different executor wrapper type. | |
313 | /** | |
314 | * This constructor is only valid if the @c Executor type is constructible | |
315 | * from type @c OtherExecutor, and the type @c T is constructible from type | |
316 | * @c U. | |
317 | */ | |
318 | template <typename U, typename OtherExecutor> | |
319 | executor_binder(const executor_binder<U, OtherExecutor>& other) | |
320 | : base_type(other.get_executor(), other.get()) | |
321 | { | |
322 | } | |
323 | ||
324 | /// Construct a copy of a different executor wrapper type, but specify a | |
325 | /// different executor. | |
326 | /** | |
327 | * This constructor is only valid if the type @c T is constructible from type | |
328 | * @c U. | |
329 | */ | |
330 | template <typename U, typename OtherExecutor> | |
331 | executor_binder(executor_arg_t, const executor_type& e, | |
332 | const executor_binder<U, OtherExecutor>& other) | |
333 | : base_type(e, other.get()) | |
334 | { | |
335 | } | |
336 | ||
337 | #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
338 | ||
339 | /// Move constructor. | |
340 | executor_binder(executor_binder&& other) | |
341 | : base_type(BOOST_ASIO_MOVE_CAST(executor_type)(other.get_executor()), | |
342 | BOOST_ASIO_MOVE_CAST(T)(other.get())) | |
343 | { | |
344 | } | |
345 | ||
346 | /// Move construct the target object, but specify a different executor. | |
347 | executor_binder(executor_arg_t, const executor_type& e, | |
348 | executor_binder&& other) | |
349 | : base_type(e, BOOST_ASIO_MOVE_CAST(T)(other.get())) | |
350 | { | |
351 | } | |
352 | ||
353 | /// Move construct from a different executor wrapper type. | |
354 | template <typename U, typename OtherExecutor> | |
355 | executor_binder(executor_binder<U, OtherExecutor>&& other) | |
356 | : base_type(BOOST_ASIO_MOVE_CAST(OtherExecutor)(other.get_executor()), | |
357 | BOOST_ASIO_MOVE_CAST(U)(other.get())) | |
358 | { | |
359 | } | |
360 | ||
361 | /// Move construct from a different executor wrapper type, but specify a | |
362 | /// different executor. | |
363 | template <typename U, typename OtherExecutor> | |
364 | executor_binder(executor_arg_t, const executor_type& e, | |
365 | executor_binder<U, OtherExecutor>&& other) | |
366 | : base_type(e, BOOST_ASIO_MOVE_CAST(U)(other.get())) | |
367 | { | |
368 | } | |
369 | ||
370 | #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) | |
371 | ||
372 | /// Destructor. | |
373 | ~executor_binder() | |
374 | { | |
375 | } | |
376 | ||
377 | /// Obtain a reference to the target object. | |
378 | target_type& get() BOOST_ASIO_NOEXCEPT | |
379 | { | |
380 | return this->target_; | |
381 | } | |
382 | ||
383 | /// Obtain a reference to the target object. | |
384 | const target_type& get() const BOOST_ASIO_NOEXCEPT | |
385 | { | |
386 | return this->target_; | |
387 | } | |
388 | ||
389 | /// Obtain the associated executor. | |
390 | executor_type get_executor() const BOOST_ASIO_NOEXCEPT | |
391 | { | |
392 | return this->executor_; | |
393 | } | |
394 | ||
395 | #if defined(GENERATING_DOCUMENTATION) | |
396 | ||
397 | template <typename... Args> auto operator()(Args&& ...); | |
398 | template <typename... Args> auto operator()(Args&& ...) const; | |
399 | ||
400 | #elif defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) | |
401 | ||
402 | /// Forwarding function call operator. | |
403 | template <typename... Args> | |
404 | typename result_of<T(Args...)>::type operator()( | |
405 | BOOST_ASIO_MOVE_ARG(Args)... args) | |
406 | { | |
407 | return this->target_(BOOST_ASIO_MOVE_CAST(Args)(args)...); | |
408 | } | |
409 | ||
410 | /// Forwarding function call operator. | |
411 | template <typename... Args> | |
412 | typename result_of<T(Args...)>::type operator()( | |
413 | BOOST_ASIO_MOVE_ARG(Args)... args) const | |
414 | { | |
415 | return this->target_(BOOST_ASIO_MOVE_CAST(Args)(args)...); | |
416 | } | |
417 | ||
418 | #elif defined(BOOST_ASIO_HAS_STD_TYPE_TRAITS) && !defined(_MSC_VER) | |
419 | ||
420 | typename detail::executor_binder_result_of0<T>::type operator()() | |
421 | { | |
422 | return this->target_(); | |
423 | } | |
424 | ||
425 | typename detail::executor_binder_result_of0<T>::type operator()() const | |
426 | { | |
427 | return this->target_(); | |
428 | } | |
429 | ||
430 | #define BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF(n) \ | |
431 | template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \ | |
432 | typename result_of<T(BOOST_ASIO_VARIADIC_TARGS(n))>::type operator()( \ | |
433 | BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \ | |
434 | { \ | |
435 | return this->target_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \ | |
436 | } \ | |
437 | \ | |
438 | template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \ | |
439 | typename result_of<T(BOOST_ASIO_VARIADIC_TARGS(n))>::type operator()( \ | |
440 | BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) const \ | |
441 | { \ | |
442 | return this->target_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \ | |
443 | } \ | |
444 | /**/ | |
445 | BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF) | |
446 | #undef BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF | |
447 | ||
448 | #else // defined(BOOST_ASIO_HAS_STD_TYPE_TRAITS) && !defined(_MSC_VER) | |
449 | ||
450 | typedef typename detail::executor_binder_result_type<T>::result_type_or_void | |
451 | result_type_or_void; | |
452 | ||
453 | result_type_or_void operator()() | |
454 | { | |
455 | return this->target_(); | |
456 | } | |
457 | ||
458 | result_type_or_void operator()() const | |
459 | { | |
460 | return this->target_(); | |
461 | } | |
462 | ||
463 | #define BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF(n) \ | |
464 | template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \ | |
465 | result_type_or_void operator()( \ | |
466 | BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \ | |
467 | { \ | |
468 | return this->target_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \ | |
469 | } \ | |
470 | \ | |
471 | template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \ | |
472 | result_type_or_void operator()( \ | |
473 | BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) const \ | |
474 | { \ | |
475 | return this->target_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \ | |
476 | } \ | |
477 | /**/ | |
478 | BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF) | |
479 | #undef BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF | |
480 | ||
481 | #endif // defined(BOOST_ASIO_HAS_STD_TYPE_TRAITS) && !defined(_MSC_VER) | |
482 | ||
483 | private: | |
484 | typedef detail::executor_binder_base<T, Executor, | |
485 | uses_executor<T, Executor>::value> base_type; | |
486 | }; | |
487 | ||
488 | /// Associate an object of type @c T with an executor of type @c Executor. | |
489 | template <typename Executor, typename T> | |
1e59de90 | 490 | BOOST_ASIO_NODISCARD inline executor_binder<typename decay<T>::type, Executor> |
b32b8144 | 491 | bind_executor(const Executor& ex, BOOST_ASIO_MOVE_ARG(T) t, |
1e59de90 | 492 | typename constraint< |
20effc67 | 493 | is_executor<Executor>::value || execution::is_executor<Executor>::value |
1e59de90 | 494 | >::type = 0) |
b32b8144 FG |
495 | { |
496 | return executor_binder<typename decay<T>::type, Executor>( | |
497 | executor_arg_t(), ex, BOOST_ASIO_MOVE_CAST(T)(t)); | |
498 | } | |
499 | ||
500 | /// Associate an object of type @c T with an execution context's executor. | |
501 | template <typename ExecutionContext, typename T> | |
1e59de90 | 502 | BOOST_ASIO_NODISCARD inline executor_binder<typename decay<T>::type, |
b32b8144 FG |
503 | typename ExecutionContext::executor_type> |
504 | bind_executor(ExecutionContext& ctx, BOOST_ASIO_MOVE_ARG(T) t, | |
1e59de90 TL |
505 | typename constraint<is_convertible< |
506 | ExecutionContext&, execution_context&>::value>::type = 0) | |
b32b8144 FG |
507 | { |
508 | return executor_binder<typename decay<T>::type, | |
509 | typename ExecutionContext::executor_type>( | |
510 | executor_arg_t(), ctx.get_executor(), BOOST_ASIO_MOVE_CAST(T)(t)); | |
511 | } | |
512 | ||
513 | #if !defined(GENERATING_DOCUMENTATION) | |
514 | ||
515 | template <typename T, typename Executor> | |
516 | struct uses_executor<executor_binder<T, Executor>, Executor> | |
517 | : true_type {}; | |
518 | ||
1e59de90 TL |
519 | namespace detail { |
520 | ||
521 | template <typename TargetAsyncResult, typename Executor, typename = void> | |
522 | class executor_binder_completion_handler_async_result | |
523 | { | |
524 | public: | |
525 | template <typename T> | |
526 | explicit executor_binder_completion_handler_async_result(T&) | |
527 | { | |
528 | } | |
529 | }; | |
530 | ||
531 | template <typename TargetAsyncResult, typename Executor> | |
532 | class executor_binder_completion_handler_async_result< | |
533 | TargetAsyncResult, Executor, | |
534 | typename void_type< | |
535 | typename TargetAsyncResult::completion_handler_type | |
536 | >::type> | |
b32b8144 FG |
537 | { |
538 | public: | |
539 | typedef executor_binder< | |
1e59de90 | 540 | typename TargetAsyncResult::completion_handler_type, Executor> |
b32b8144 FG |
541 | completion_handler_type; |
542 | ||
1e59de90 TL |
543 | explicit executor_binder_completion_handler_async_result( |
544 | typename TargetAsyncResult::completion_handler_type& handler) | |
545 | : target_(handler) | |
546 | { | |
547 | } | |
548 | ||
549 | typename TargetAsyncResult::return_type get() | |
550 | { | |
551 | return target_.get(); | |
552 | } | |
b32b8144 | 553 | |
1e59de90 TL |
554 | private: |
555 | TargetAsyncResult target_; | |
556 | }; | |
557 | ||
558 | template <typename TargetAsyncResult, typename = void> | |
559 | struct executor_binder_async_result_return_type | |
560 | { | |
561 | }; | |
562 | ||
563 | template <typename TargetAsyncResult> | |
564 | struct executor_binder_async_result_return_type< | |
565 | TargetAsyncResult, | |
566 | typename void_type< | |
567 | typename TargetAsyncResult::return_type | |
568 | >::type> | |
569 | { | |
570 | typedef typename TargetAsyncResult::return_type return_type; | |
571 | }; | |
572 | ||
573 | } // namespace detail | |
574 | ||
575 | template <typename T, typename Executor, typename Signature> | |
576 | class async_result<executor_binder<T, Executor>, Signature> : | |
577 | public detail::executor_binder_completion_handler_async_result< | |
578 | async_result<T, Signature>, Executor>, | |
579 | public detail::executor_binder_async_result_return_type< | |
580 | async_result<T, Signature> > | |
581 | { | |
582 | public: | |
b32b8144 | 583 | explicit async_result(executor_binder<T, Executor>& b) |
1e59de90 TL |
584 | : detail::executor_binder_completion_handler_async_result< |
585 | async_result<T, Signature>, Executor>(b.get()) | |
b32b8144 FG |
586 | { |
587 | } | |
588 | ||
1e59de90 TL |
589 | template <typename Initiation> |
590 | struct init_wrapper | |
b32b8144 | 591 | { |
1e59de90 TL |
592 | template <typename Init> |
593 | init_wrapper(const Executor& ex, BOOST_ASIO_MOVE_ARG(Init) init) | |
594 | : ex_(ex), | |
595 | initiation_(BOOST_ASIO_MOVE_CAST(Init)(init)) | |
596 | { | |
597 | } | |
598 | ||
599 | #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) | |
600 | ||
601 | template <typename Handler, typename... Args> | |
602 | void operator()( | |
603 | BOOST_ASIO_MOVE_ARG(Handler) handler, | |
604 | BOOST_ASIO_MOVE_ARG(Args)... args) | |
605 | { | |
606 | BOOST_ASIO_MOVE_CAST(Initiation)(initiation_)( | |
607 | executor_binder<typename decay<Handler>::type, Executor>( | |
608 | executor_arg_t(), ex_, BOOST_ASIO_MOVE_CAST(Handler)(handler)), | |
609 | BOOST_ASIO_MOVE_CAST(Args)(args)...); | |
610 | } | |
611 | ||
612 | template <typename Handler, typename... Args> | |
613 | void operator()( | |
614 | BOOST_ASIO_MOVE_ARG(Handler) handler, | |
615 | BOOST_ASIO_MOVE_ARG(Args)... args) const | |
616 | { | |
617 | initiation_( | |
618 | executor_binder<typename decay<Handler>::type, Executor>( | |
619 | executor_arg_t(), ex_, BOOST_ASIO_MOVE_CAST(Handler)(handler)), | |
620 | BOOST_ASIO_MOVE_CAST(Args)(args)...); | |
621 | } | |
622 | ||
623 | #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) | |
624 | ||
625 | template <typename Handler> | |
626 | void operator()( | |
627 | BOOST_ASIO_MOVE_ARG(Handler) handler) | |
628 | { | |
629 | BOOST_ASIO_MOVE_CAST(Initiation)(initiation_)( | |
630 | executor_binder<typename decay<Handler>::type, Executor>( | |
631 | executor_arg_t(), ex_, BOOST_ASIO_MOVE_CAST(Handler)(handler))); | |
632 | } | |
633 | ||
634 | template <typename Handler> | |
635 | void operator()( | |
636 | BOOST_ASIO_MOVE_ARG(Handler) handler) const | |
637 | { | |
638 | initiation_( | |
639 | executor_binder<typename decay<Handler>::type, Executor>( | |
640 | executor_arg_t(), ex_, BOOST_ASIO_MOVE_CAST(Handler)(handler))); | |
641 | } | |
642 | ||
643 | #define BOOST_ASIO_PRIVATE_INIT_WRAPPER_DEF(n) \ | |
644 | template <typename Handler, BOOST_ASIO_VARIADIC_TPARAMS(n)> \ | |
645 | void operator()( \ | |
646 | BOOST_ASIO_MOVE_ARG(Handler) handler, \ | |
647 | BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \ | |
648 | { \ | |
649 | BOOST_ASIO_MOVE_CAST(Initiation)(initiation_)( \ | |
650 | executor_binder<typename decay<Handler>::type, Executor>( \ | |
651 | executor_arg_t(), ex_, BOOST_ASIO_MOVE_CAST(Handler)(handler)), \ | |
652 | BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \ | |
653 | } \ | |
654 | \ | |
655 | template <typename Handler, BOOST_ASIO_VARIADIC_TPARAMS(n)> \ | |
656 | void operator()( \ | |
657 | BOOST_ASIO_MOVE_ARG(Handler) handler, \ | |
658 | BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) const \ | |
659 | { \ | |
660 | initiation_( \ | |
661 | executor_binder<typename decay<Handler>::type, Executor>( \ | |
662 | executor_arg_t(), ex_, BOOST_ASIO_MOVE_CAST(Handler)(handler)), \ | |
663 | BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \ | |
664 | } \ | |
665 | /**/ | |
666 | BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_INIT_WRAPPER_DEF) | |
667 | #undef BOOST_ASIO_PRIVATE_INIT_WRAPPER_DEF | |
668 | ||
669 | #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) | |
670 | ||
671 | Executor ex_; | |
672 | Initiation initiation_; | |
673 | }; | |
674 | ||
675 | #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) | |
676 | ||
677 | template <typename Initiation, typename RawCompletionToken, typename... Args> | |
678 | static BOOST_ASIO_INITFN_DEDUCED_RESULT_TYPE(T, Signature, | |
679 | (async_initiate<T, Signature>( | |
680 | declval<init_wrapper<typename decay<Initiation>::type> >(), | |
681 | declval<RawCompletionToken>().get(), | |
682 | declval<BOOST_ASIO_MOVE_ARG(Args)>()...))) | |
683 | initiate( | |
684 | BOOST_ASIO_MOVE_ARG(Initiation) initiation, | |
685 | BOOST_ASIO_MOVE_ARG(RawCompletionToken) token, | |
686 | BOOST_ASIO_MOVE_ARG(Args)... args) | |
687 | { | |
688 | return async_initiate<T, Signature>( | |
689 | init_wrapper<typename decay<Initiation>::type>( | |
690 | token.get_executor(), BOOST_ASIO_MOVE_CAST(Initiation)(initiation)), | |
691 | token.get(), BOOST_ASIO_MOVE_CAST(Args)(args)...); | |
b32b8144 FG |
692 | } |
693 | ||
1e59de90 TL |
694 | #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) |
695 | ||
696 | template <typename Initiation, typename RawCompletionToken> | |
697 | static BOOST_ASIO_INITFN_DEDUCED_RESULT_TYPE(T, Signature, | |
698 | (async_initiate<T, Signature>( | |
699 | declval<init_wrapper<typename decay<Initiation>::type> >(), | |
700 | declval<RawCompletionToken>().get()))) | |
701 | initiate( | |
702 | BOOST_ASIO_MOVE_ARG(Initiation) initiation, | |
703 | BOOST_ASIO_MOVE_ARG(RawCompletionToken) token) | |
704 | { | |
705 | return async_initiate<T, Signature>( | |
706 | init_wrapper<typename decay<Initiation>::type>( | |
707 | token.get_executor(), BOOST_ASIO_MOVE_CAST(Initiation)(initiation)), | |
708 | token.get()); | |
709 | } | |
710 | ||
711 | #define BOOST_ASIO_PRIVATE_INITIATE_DEF(n) \ | |
712 | template <typename Initiation, typename RawCompletionToken, \ | |
713 | BOOST_ASIO_VARIADIC_TPARAMS(n)> \ | |
714 | static BOOST_ASIO_INITFN_DEDUCED_RESULT_TYPE(T, Signature, \ | |
715 | (async_initiate<T, Signature>( \ | |
716 | declval<init_wrapper<typename decay<Initiation>::type> >(), \ | |
717 | declval<RawCompletionToken>().get(), \ | |
718 | BOOST_ASIO_VARIADIC_MOVE_DECLVAL(n)))) \ | |
719 | initiate( \ | |
720 | BOOST_ASIO_MOVE_ARG(Initiation) initiation, \ | |
721 | BOOST_ASIO_MOVE_ARG(RawCompletionToken) token, \ | |
722 | BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \ | |
723 | { \ | |
724 | return async_initiate<T, Signature>( \ | |
725 | init_wrapper<typename decay<Initiation>::type>( \ | |
726 | token.get_executor(), BOOST_ASIO_MOVE_CAST(Initiation)(initiation)), \ | |
727 | token.get(), BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \ | |
728 | } \ | |
729 | /**/ | |
730 | BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_INITIATE_DEF) | |
731 | #undef BOOST_ASIO_PRIVATE_INITIATE_DEF | |
732 | ||
733 | #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) | |
734 | ||
b32b8144 FG |
735 | private: |
736 | async_result(const async_result&) BOOST_ASIO_DELETED; | |
737 | async_result& operator=(const async_result&) BOOST_ASIO_DELETED; | |
b32b8144 FG |
738 | }; |
739 | ||
1e59de90 TL |
740 | template <template <typename, typename> class Associator, |
741 | typename T, typename Executor, typename DefaultCandidate> | |
742 | struct associator<Associator, executor_binder<T, Executor>, DefaultCandidate> | |
b32b8144 | 743 | { |
1e59de90 | 744 | typedef typename Associator<T, DefaultCandidate>::type type; |
b32b8144 FG |
745 | |
746 | static type get(const executor_binder<T, Executor>& b, | |
1e59de90 | 747 | const DefaultCandidate& c = DefaultCandidate()) BOOST_ASIO_NOEXCEPT |
b32b8144 | 748 | { |
1e59de90 | 749 | return Associator<T, DefaultCandidate>::get(b.get(), c); |
b32b8144 FG |
750 | } |
751 | }; | |
752 | ||
753 | template <typename T, typename Executor, typename Executor1> | |
754 | struct associated_executor<executor_binder<T, Executor>, Executor1> | |
755 | { | |
756 | typedef Executor type; | |
757 | ||
758 | static type get(const executor_binder<T, Executor>& b, | |
759 | const Executor1& = Executor1()) BOOST_ASIO_NOEXCEPT | |
760 | { | |
761 | return b.get_executor(); | |
762 | } | |
763 | }; | |
764 | ||
765 | #endif // !defined(GENERATING_DOCUMENTATION) | |
766 | ||
767 | } // namespace asio | |
768 | } // namespace boost | |
769 | ||
770 | #include <boost/asio/detail/pop_options.hpp> | |
771 | ||
772 | #endif // BOOST_ASIO_BIND_EXECUTOR_HPP |