5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_IMPL_COMPOSE_HPP
12 #define BOOST_ASIO_IMPL_COMPOSE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.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 #include <boost/asio/detail/type_traits.hpp>
23 #include <boost/asio/detail/variadic_templates.hpp>
24 #include <boost/asio/executor_work_guard.hpp>
25 #include <boost/asio/is_executor.hpp>
26 #include <boost/asio/system_executor.hpp>
28 #include <boost/asio/detail/push_options.hpp>
36 struct composed_io_executors;
39 struct composed_io_executors<void()>
41 composed_io_executors() BOOST_ASIO_NOEXCEPT
42 : head_(system_executor())
46 typedef system_executor head_type;
47 system_executor head_;
50 inline composed_io_executors<void()> make_composed_io_executors()
52 return composed_io_executors<void()>();
55 template <typename Head>
56 struct composed_io_executors<void(Head)>
58 explicit composed_io_executors(const Head& ex) BOOST_ASIO_NOEXCEPT
63 typedef Head head_type;
67 template <typename Head>
68 inline composed_io_executors<void(Head)>
69 make_composed_io_executors(const Head& head)
71 return composed_io_executors<void(Head)>(head);
74 #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
76 template <typename Head, typename... Tail>
77 struct composed_io_executors<void(Head, Tail...)>
79 explicit composed_io_executors(const Head& head,
80 const Tail&... tail) BOOST_ASIO_NOEXCEPT
92 typedef Head head_type;
94 composed_io_executors<void(Tail...)> tail_;
97 template <typename Head, typename... Tail>
98 inline composed_io_executors<void(Head, Tail...)>
99 make_composed_io_executors(const Head& head, const Tail&... tail)
101 return composed_io_executors<void(Head, Tail...)>(head, tail...);
104 #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
106 #define BOOST_ASIO_PRIVATE_COMPOSED_IO_EXECUTORS_DEF(n) \
107 template <typename Head, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
108 struct composed_io_executors<void(Head, BOOST_ASIO_VARIADIC_TARGS(n))> \
110 explicit composed_io_executors(const Head& head, \
111 BOOST_ASIO_VARIADIC_CONSTREF_PARAMS(n)) BOOST_ASIO_NOEXCEPT \
113 tail_(BOOST_ASIO_VARIADIC_BYVAL_ARGS(n)) \
123 typedef Head head_type; \
125 composed_io_executors<void(BOOST_ASIO_VARIADIC_TARGS(n))> tail_; \
128 template <typename Head, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
129 inline composed_io_executors<void(Head, BOOST_ASIO_VARIADIC_TARGS(n))> \
130 make_composed_io_executors(const Head& head, \
131 BOOST_ASIO_VARIADIC_CONSTREF_PARAMS(n)) \
133 return composed_io_executors< \
134 void(Head, BOOST_ASIO_VARIADIC_TARGS(n))>( \
135 head, BOOST_ASIO_VARIADIC_BYVAL_ARGS(n)); \
138 BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_COMPOSED_IO_EXECUTORS_DEF)
139 #undef BOOST_ASIO_PRIVATE_COMPOSED_IO_EXECUTORS_DEF
141 #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
144 struct composed_work;
147 struct composed_work<void()>
149 typedef composed_io_executors<void()> executors_type;
151 composed_work(const executors_type&) BOOST_ASIO_NOEXCEPT
152 : head_(system_executor())
161 typedef system_executor head_type;
162 executor_work_guard<system_executor> head_;
165 template <typename Head>
166 struct composed_work<void(Head)>
168 typedef composed_io_executors<void(Head)> executors_type;
170 explicit composed_work(const executors_type& ex) BOOST_ASIO_NOEXCEPT
180 typedef Head head_type;
181 executor_work_guard<Head> head_;
184 #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
186 template <typename Head, typename... Tail>
187 struct composed_work<void(Head, Tail...)>
189 typedef composed_io_executors<void(Head, Tail...)> executors_type;
191 explicit composed_work(const executors_type& ex) BOOST_ASIO_NOEXCEPT
203 typedef Head head_type;
204 executor_work_guard<Head> head_;
205 composed_work<void(Tail...)> tail_;
208 #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
210 #define BOOST_ASIO_PRIVATE_COMPOSED_WORK_DEF(n) \
211 template <typename Head, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
212 struct composed_work<void(Head, BOOST_ASIO_VARIADIC_TARGS(n))> \
214 typedef composed_io_executors<void(Head, \
215 BOOST_ASIO_VARIADIC_TARGS(n))> executors_type; \
217 explicit composed_work(const executors_type& ex) BOOST_ASIO_NOEXCEPT \
229 typedef Head head_type; \
230 executor_work_guard<Head> head_; \
231 composed_work<void(BOOST_ASIO_VARIADIC_TARGS(n))> tail_; \
234 BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_COMPOSED_WORK_DEF)
235 #undef BOOST_ASIO_PRIVATE_COMPOSED_WORK_DEF
237 #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
239 #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
240 template <typename Impl, typename Work, typename Handler, typename Signature>
243 template <typename Impl, typename Work, typename Handler,
244 typename R, typename... Args>
245 class composed_op<Impl, Work, Handler, R(Args...)>
246 #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
247 template <typename Impl, typename Work, typename Handler, typename Signature>
249 #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
252 composed_op(BOOST_ASIO_MOVE_ARG(Impl) impl,
253 BOOST_ASIO_MOVE_ARG(Work) work,
254 BOOST_ASIO_MOVE_ARG(Handler) handler)
255 : impl_(BOOST_ASIO_MOVE_CAST(Impl)(impl)),
256 work_(BOOST_ASIO_MOVE_CAST(Work)(work)),
257 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)),
262 #if defined(BOOST_ASIO_HAS_MOVE)
263 composed_op(composed_op&& other)
264 : impl_(BOOST_ASIO_MOVE_CAST(Impl)(other.impl_)),
265 work_(BOOST_ASIO_MOVE_CAST(Work)(other.work_)),
266 handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_)),
267 invocations_(other.invocations_)
270 #endif // defined(BOOST_ASIO_HAS_MOVE)
272 typedef typename associated_executor<Handler,
273 typename Work::head_type>::type executor_type;
275 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
277 return (get_associated_executor)(handler_, work_.head_.get_executor());
280 typedef typename associated_allocator<Handler,
281 std::allocator<void> >::type allocator_type;
283 allocator_type get_allocator() const BOOST_ASIO_NOEXCEPT
285 return (get_associated_allocator)(handler_, std::allocator<void>());
288 #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
290 template<typename... T>
291 void operator()(BOOST_ASIO_MOVE_ARG(T)... t)
293 if (invocations_ < ~unsigned(0))
295 impl_(*this, BOOST_ASIO_MOVE_CAST(T)(t)...);
298 void complete(Args... args)
301 this->handler_(BOOST_ASIO_MOVE_CAST(Args)(args)...);
304 #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
308 if (invocations_ < ~unsigned(0))
319 #define BOOST_ASIO_PRIVATE_COMPOSED_OP_DEF(n) \
320 template<BOOST_ASIO_VARIADIC_TPARAMS(n)> \
321 void operator()(BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
323 if (invocations_ < ~unsigned(0)) \
325 impl_(*this, BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
328 template<BOOST_ASIO_VARIADIC_TPARAMS(n)> \
329 void complete(BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
331 this->work_.reset(); \
332 this->handler_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
335 BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_COMPOSED_OP_DEF)
336 #undef BOOST_ASIO_PRIVATE_COMPOSED_OP_DEF
338 #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
344 unsigned invocations_;
347 template <typename Impl, typename Work, typename Handler, typename Signature>
348 inline void* asio_handler_allocate(std::size_t size,
349 composed_op<Impl, Work, Handler, Signature>* this_handler)
351 return boost_asio_handler_alloc_helpers::allocate(
352 size, this_handler->handler_);
355 template <typename Impl, typename Work, typename Handler, typename Signature>
356 inline void asio_handler_deallocate(void* pointer, std::size_t size,
357 composed_op<Impl, Work, Handler, Signature>* this_handler)
359 boost_asio_handler_alloc_helpers::deallocate(
360 pointer, size, this_handler->handler_);
363 template <typename Impl, typename Work, typename Handler, typename Signature>
364 inline bool asio_handler_is_continuation(
365 composed_op<Impl, Work, Handler, Signature>* this_handler)
367 return this_handler->invocations_ > 1 ? true
368 : boost_asio_handler_cont_helpers::is_continuation(
369 this_handler->handler_);
372 template <typename Function, typename Impl,
373 typename Work, typename Handler, typename Signature>
374 inline void asio_handler_invoke(Function& function,
375 composed_op<Impl, Work, Handler, Signature>* this_handler)
377 boost_asio_handler_invoke_helpers::invoke(
378 function, this_handler->handler_);
381 template <typename Function, typename Impl,
382 typename Work, typename Handler, typename Signature>
383 inline void asio_handler_invoke(const Function& function,
384 composed_op<Impl, Work, Handler, Signature>* this_handler)
386 boost_asio_handler_invoke_helpers::invoke(
387 function, this_handler->handler_);
390 template <typename Signature, typename Executors>
391 class initiate_composed_op
394 typedef typename composed_io_executors<Executors>::head_type executor_type;
396 template <typename T>
397 explicit initiate_composed_op(BOOST_ASIO_MOVE_ARG(T) executors)
398 : executors_(BOOST_ASIO_MOVE_CAST(T)(executors))
402 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
404 return executors_.head_;
407 template <typename Handler, typename Impl>
408 void operator()(BOOST_ASIO_MOVE_ARG(Handler) handler,
409 BOOST_ASIO_MOVE_ARG(Impl) impl) const
411 composed_op<typename decay<Impl>::type, composed_work<Executors>,
412 typename decay<Handler>::type, Signature>(
413 BOOST_ASIO_MOVE_CAST(Impl)(impl),
414 composed_work<Executors>(executors_),
415 BOOST_ASIO_MOVE_CAST(Handler)(handler))();
419 composed_io_executors<Executors> executors_;
422 template <typename Signature, typename Executors>
423 inline initiate_composed_op<Signature, Executors> make_initiate_composed_op(
424 BOOST_ASIO_MOVE_ARG(composed_io_executors<Executors>) executors)
426 return initiate_composed_op<Signature, Executors>(
427 BOOST_ASIO_MOVE_CAST(composed_io_executors<Executors>)(executors));
430 template <typename IoObject>
431 inline typename IoObject::executor_type
432 get_composed_io_executor(IoObject& io_object)
434 return io_object.get_executor();
437 template <typename Executor>
438 inline const Executor& get_composed_io_executor(const Executor& ex,
439 typename enable_if<is_executor<Executor>::value>::type* = 0)
443 } // namespace detail
445 #if !defined(GENERATING_DOCUMENTATION)
446 #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
448 template <typename CompletionToken, typename Signature,
449 typename Implementation, typename... IoObjectsOrExecutors>
450 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, Signature)
451 async_compose(BOOST_ASIO_MOVE_ARG(Implementation) implementation,
452 BOOST_ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token,
453 BOOST_ASIO_MOVE_ARG(IoObjectsOrExecutors)... io_objects_or_executors)
455 return async_initiate<CompletionToken, Signature>(
456 detail::make_initiate_composed_op<Signature>(
457 detail::make_composed_io_executors(
458 detail::get_composed_io_executor(
459 BOOST_ASIO_MOVE_CAST(IoObjectsOrExecutors)(
460 io_objects_or_executors))...)),
461 token, BOOST_ASIO_MOVE_CAST(Implementation)(implementation));
464 #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
466 template <typename CompletionToken, typename Signature, typename Implementation>
467 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, Signature)
468 async_compose(BOOST_ASIO_MOVE_ARG(Implementation) implementation,
469 BOOST_ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token)
471 return async_initiate<CompletionToken, Signature>(
472 detail::make_initiate_composed_op<Signature>(
473 detail::make_composed_io_executors()),
474 token, BOOST_ASIO_MOVE_CAST(Implementation)(implementation));
477 # define BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR(n) \
478 BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_##n
480 # define BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_1 \
481 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T1)(x1))
482 # define BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_2 \
483 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T1)(x1)), \
484 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T2)(x2))
485 # define BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_3 \
486 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T1)(x1)), \
487 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T2)(x2)), \
488 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T3)(x3))
489 # define BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_4 \
490 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T1)(x1)), \
491 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T2)(x2)), \
492 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T3)(x3)), \
493 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T4)(x4))
494 # define BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_5 \
495 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T1)(x1)), \
496 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T2)(x2)), \
497 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T3)(x3)), \
498 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T4)(x4)), \
499 detail::get_composed_io_executor(BOOST_ASIO_MOVE_CAST(T5)(x5))
501 #define BOOST_ASIO_PRIVATE_ASYNC_COMPOSE_DEF(n) \
502 template <typename CompletionToken, typename Signature, \
503 typename Implementation, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
504 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, Signature) \
505 async_compose(BOOST_ASIO_MOVE_ARG(Implementation) implementation, \
506 BOOST_ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token, \
507 BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
509 return async_initiate<CompletionToken, Signature>( \
510 detail::make_initiate_composed_op<Signature>( \
511 detail::make_composed_io_executors( \
512 BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR(n))), \
513 token, BOOST_ASIO_MOVE_CAST(Implementation)(implementation)); \
516 BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_ASYNC_COMPOSE_DEF)
517 #undef BOOST_ASIO_PRIVATE_ASYNC_COMPOSE_DEF
519 #undef BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR
520 #undef BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_1
521 #undef BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_2
522 #undef BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_3
523 #undef BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_4
524 #undef BOOST_ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_5
526 #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
527 #endif // !defined(GENERATING_DOCUMENTATION)
532 #include <boost/asio/detail/pop_options.hpp>
534 #endif // BOOST_ASIO_IMPL_COMPOSE_HPP