5 // Copyright (c) 2003-2020 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_BIND_EXECUTOR_HPP
12 #define BOOST_ASIO_BIND_EXECUTOR_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/type_traits.hpp>
20 #include <boost/asio/detail/variadic_templates.hpp>
21 #include <boost/asio/associated_executor.hpp>
22 #include <boost/asio/associated_allocator.hpp>
23 #include <boost/asio/async_result.hpp>
24 #include <boost/asio/execution/executor.hpp>
25 #include <boost/asio/execution_context.hpp>
26 #include <boost/asio/is_executor.hpp>
27 #include <boost/asio/uses_executor.hpp>
29 #include <boost/asio/detail/push_options.hpp>
35 // Helper to automatically define nested typedef result_type.
37 template <typename T, typename = void>
38 struct executor_binder_result_type
41 typedef void result_type_or_void;
45 struct executor_binder_result_type<T,
46 typename void_type<typename T::result_type>::type>
48 typedef typename T::result_type result_type;
50 typedef result_type result_type_or_void;
54 struct executor_binder_result_type<R(*)()>
56 typedef R result_type;
58 typedef result_type result_type_or_void;
62 struct executor_binder_result_type<R(&)()>
64 typedef R result_type;
66 typedef result_type result_type_or_void;
69 template <typename R, typename A1>
70 struct executor_binder_result_type<R(*)(A1)>
72 typedef R result_type;
74 typedef result_type result_type_or_void;
77 template <typename R, typename A1>
78 struct executor_binder_result_type<R(&)(A1)>
80 typedef R result_type;
82 typedef result_type result_type_or_void;
85 template <typename R, typename A1, typename A2>
86 struct executor_binder_result_type<R(*)(A1, A2)>
88 typedef R result_type;
90 typedef result_type result_type_or_void;
93 template <typename R, typename A1, typename A2>
94 struct executor_binder_result_type<R(&)(A1, A2)>
96 typedef R result_type;
98 typedef result_type result_type_or_void;
101 // Helper to automatically define nested typedef argument_type.
103 template <typename T, typename = void>
104 struct executor_binder_argument_type {};
106 template <typename T>
107 struct executor_binder_argument_type<T,
108 typename void_type<typename T::argument_type>::type>
110 typedef typename T::argument_type argument_type;
113 template <typename R, typename A1>
114 struct executor_binder_argument_type<R(*)(A1)>
116 typedef A1 argument_type;
119 template <typename R, typename A1>
120 struct executor_binder_argument_type<R(&)(A1)>
122 typedef A1 argument_type;
125 // Helper to automatically define nested typedefs first_argument_type and
126 // second_argument_type.
128 template <typename T, typename = void>
129 struct executor_binder_argument_types {};
131 template <typename T>
132 struct executor_binder_argument_types<T,
133 typename void_type<typename T::first_argument_type>::type>
135 typedef typename T::first_argument_type first_argument_type;
136 typedef typename T::second_argument_type second_argument_type;
139 template <typename R, typename A1, typename A2>
140 struct executor_binder_argument_type<R(*)(A1, A2)>
142 typedef A1 first_argument_type;
143 typedef A2 second_argument_type;
146 template <typename R, typename A1, typename A2>
147 struct executor_binder_argument_type<R(&)(A1, A2)>
149 typedef A1 first_argument_type;
150 typedef A2 second_argument_type;
153 // Helper to perform uses_executor construction of the target type, if
156 template <typename T, typename Executor, bool UsesExecutor>
157 class executor_binder_base;
159 template <typename T, typename Executor>
160 class executor_binder_base<T, Executor, true>
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))
174 template <typename T, typename Executor>
175 class executor_binder_base<T, Executor, false>
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))
189 // Helper to enable SFINAE on zero-argument operator() below.
191 template <typename T, typename = void>
192 struct executor_binder_result_of0
197 template <typename T>
198 struct executor_binder_result_of0<T,
199 typename void_type<typename result_of<T()>::type>::type>
201 typedef typename result_of<T()>::type type;
204 } // namespace detail
206 /// A call wrapper type to bind an executor of type @c Executor to an object of
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)
219 /// The type of the target object.
220 typedef T target_type;
222 /// The type of the associated executor.
223 typedef Executor executor_type;
225 #if defined(GENERATING_DOCUMENTATION)
226 /// The return type if a function.
228 * The type of @c result_type is based on the type @c T of the wrapper's
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;
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;
237 * @li otherwise @c result_type is not defined.
239 typedef see_below result_type;
241 /// The type of the function's argument.
243 * The type of @c argument_type is based on the type @c T of the wrapper's
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;
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;
252 * @li otherwise @c argument_type is not defined.
254 typedef see_below argument_type;
256 /// The type of the function's first argument.
258 * The type of @c first_argument_type is based on the type @c T of the
259 * wrapper's target object:
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;
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;
267 * @li otherwise @c first_argument_type is not defined.
269 typedef see_below first_argument_type;
271 /// The type of the function's second argument.
273 * The type of @c second_argument_type is based on the type @c T of the
274 * wrapper's target object:
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;
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;
282 * @li otherwise @c second_argument_type is not defined.
284 typedef see_below second_argument_type;
285 #endif // defined(GENERATING_DOCUMENTATION)
287 /// Construct an executor wrapper for the specified object.
289 * This constructor is only valid if the type @c T is constructible from type
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))
299 /// Copy constructor.
300 executor_binder(const executor_binder& other)
301 : base_type(other.get_executor(), other.get())
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())
312 /// Construct a copy of a different executor wrapper type.
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
318 template <typename U, typename OtherExecutor>
319 executor_binder(const executor_binder<U, OtherExecutor>& other)
320 : base_type(other.get_executor(), other.get())
324 /// Construct a copy of a different executor wrapper type, but specify a
325 /// different executor.
327 * This constructor is only valid if the type @c T is constructible from type
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())
337 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
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()))
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()))
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()))
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()))
370 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
377 /// Obtain a reference to the target object.
378 target_type& get() BOOST_ASIO_NOEXCEPT
380 return this->target_;
383 /// Obtain a reference to the target object.
384 const target_type& get() const BOOST_ASIO_NOEXCEPT
386 return this->target_;
389 /// Obtain the associated executor.
390 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
392 return this->executor_;
395 #if defined(GENERATING_DOCUMENTATION)
397 template <typename... Args> auto operator()(Args&& ...);
398 template <typename... Args> auto operator()(Args&& ...) const;
400 #elif defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
402 /// Forwarding function call operator.
403 template <typename... Args>
404 typename result_of<T(Args...)>::type operator()(
405 BOOST_ASIO_MOVE_ARG(Args)... args)
407 return this->target_(BOOST_ASIO_MOVE_CAST(Args)(args)...);
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
415 return this->target_(BOOST_ASIO_MOVE_CAST(Args)(args)...);
418 #elif defined(BOOST_ASIO_HAS_STD_TYPE_TRAITS) && !defined(_MSC_VER)
420 typename detail::executor_binder_result_of0<T>::type operator()()
422 return this->target_();
425 typename detail::executor_binder_result_of0<T>::type operator()() const
427 return this->target_();
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)) \
435 return this->target_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
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 \
442 return this->target_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
445 BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF)
446 #undef BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF
448 #else // defined(BOOST_ASIO_HAS_STD_TYPE_TRAITS) && !defined(_MSC_VER)
450 typedef typename detail::executor_binder_result_type<T>::result_type_or_void
453 result_type_or_void operator()()
455 return this->target_();
458 result_type_or_void operator()() const
460 return this->target_();
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)) \
468 return this->target_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
471 template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
472 result_type_or_void operator()( \
473 BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) const \
475 return this->target_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
478 BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF)
479 #undef BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF
481 #endif // defined(BOOST_ASIO_HAS_STD_TYPE_TRAITS) && !defined(_MSC_VER)
484 typedef detail::executor_binder_base<T, Executor,
485 uses_executor<T, Executor>::value> base_type;
488 /// Associate an object of type @c T with an executor of type @c Executor.
489 template <typename Executor, typename T>
490 inline executor_binder<typename decay<T>::type, Executor>
491 bind_executor(const Executor& ex, BOOST_ASIO_MOVE_ARG(T) t,
493 is_executor<Executor>::value || execution::is_executor<Executor>::value
496 return executor_binder<typename decay<T>::type, Executor>(
497 executor_arg_t(), ex, BOOST_ASIO_MOVE_CAST(T)(t));
500 /// Associate an object of type @c T with an execution context's executor.
501 template <typename ExecutionContext, typename T>
502 inline executor_binder<typename decay<T>::type,
503 typename ExecutionContext::executor_type>
504 bind_executor(ExecutionContext& ctx, BOOST_ASIO_MOVE_ARG(T) t,
505 typename enable_if<is_convertible<
506 ExecutionContext&, execution_context&>::value>::type* = 0)
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));
513 #if !defined(GENERATING_DOCUMENTATION)
515 template <typename T, typename Executor>
516 struct uses_executor<executor_binder<T, Executor>, Executor>
519 template <typename T, typename Executor, typename Signature>
520 class async_result<executor_binder<T, Executor>, Signature>
523 typedef executor_binder<
524 typename async_result<T, Signature>::completion_handler_type, Executor>
525 completion_handler_type;
527 typedef typename async_result<T, Signature>::return_type return_type;
529 explicit async_result(executor_binder<T, Executor>& b)
536 return target_.get();
540 async_result(const async_result&) BOOST_ASIO_DELETED;
541 async_result& operator=(const async_result&) BOOST_ASIO_DELETED;
543 async_result<T, Signature> target_;
546 template <typename T, typename Executor, typename Allocator>
547 struct associated_allocator<executor_binder<T, Executor>, Allocator>
549 typedef typename associated_allocator<T, Allocator>::type type;
551 static type get(const executor_binder<T, Executor>& b,
552 const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
554 return associated_allocator<T, Allocator>::get(b.get(), a);
558 template <typename T, typename Executor, typename Executor1>
559 struct associated_executor<executor_binder<T, Executor>, Executor1>
561 typedef Executor type;
563 static type get(const executor_binder<T, Executor>& b,
564 const Executor1& = Executor1()) BOOST_ASIO_NOEXCEPT
566 return b.get_executor();
570 #endif // !defined(GENERATING_DOCUMENTATION)
575 #include <boost/asio/detail/pop_options.hpp>
577 #endif // BOOST_ASIO_BIND_EXECUTOR_HPP