// // bind_executor.hpp // ~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef BOOST_ASIO_BIND_EXECUTOR_HPP #define BOOST_ASIO_BIND_EXECUTOR_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include #include #include #include #include #include #include #include #include #include namespace lslboost { namespace asio { namespace detail { template struct executor_binder_check { typedef void type; }; // Helper to automatically define nested typedef result_type. template struct executor_binder_result_type { protected: typedef void result_type_or_void; }; template struct executor_binder_result_type::type> { typedef typename T::result_type result_type; protected: typedef result_type result_type_or_void; }; template struct executor_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct executor_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct executor_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct executor_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct executor_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct executor_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; // Helper to automatically define nested typedef argument_type. template struct executor_binder_argument_type {}; template struct executor_binder_argument_type::type> { typedef typename T::argument_type argument_type; }; template struct executor_binder_argument_type { typedef A1 argument_type; }; template struct executor_binder_argument_type { typedef A1 argument_type; }; // Helper to automatically define nested typedefs first_argument_type and // second_argument_type. template struct executor_binder_argument_types {}; template struct executor_binder_argument_types::type> { typedef typename T::first_argument_type first_argument_type; typedef typename T::second_argument_type second_argument_type; }; template struct executor_binder_argument_type { typedef A1 first_argument_type; typedef A2 second_argument_type; }; template struct executor_binder_argument_type { typedef A1 first_argument_type; typedef A2 second_argument_type; }; // Helper to: // - Apply the empty base optimisation to the executor. // - Perform uses_executor construction of the target type, if required. template class executor_binder_base; template class executor_binder_base : protected Executor { protected: template executor_binder_base(BOOST_ASIO_MOVE_ARG(E) e, BOOST_ASIO_MOVE_ARG(U) u) : executor_(BOOST_ASIO_MOVE_CAST(E)(e)), target_(executor_arg_t(), executor_, BOOST_ASIO_MOVE_CAST(U)(u)) { } Executor executor_; T target_; }; template class executor_binder_base { protected: template executor_binder_base(BOOST_ASIO_MOVE_ARG(E) e, BOOST_ASIO_MOVE_ARG(U) u) : executor_(BOOST_ASIO_MOVE_CAST(E)(e)), target_(BOOST_ASIO_MOVE_CAST(U)(u)) { } Executor executor_; T target_; }; // Helper to enable SFINAE on zero-argument operator() below. template struct executor_binder_result_of0 { typedef void type; }; template struct executor_binder_result_of0::type>::type> { typedef typename result_of::type type; }; } // namespace detail /// A call wrapper type to bind an executor of type @c Executor to an object of /// type @c T. template class executor_binder #if !defined(GENERATING_DOCUMENTATION) : public detail::executor_binder_result_type, public detail::executor_binder_argument_type, public detail::executor_binder_argument_types, private detail::executor_binder_base< T, Executor, uses_executor::value> #endif // !defined(GENERATING_DOCUMENTATION) { public: /// The type of the target object. typedef T target_type; /// The type of the associated executor. typedef Executor executor_type; #if defined(GENERATING_DOCUMENTATION) /// The return type if a function. /** * The type of @c result_type is based on the type @c T of the wrapper's * target object: * * @li if @c T is a pointer to function type, @c result_type is a synonym for * the return type of @c T; * * @li if @c T is a class type with a member type @c result_type, then @c * result_type is a synonym for @c T::result_type; * * @li otherwise @c result_type is not defined. */ typedef see_below result_type; /// The type of the function's argument. /** * The type of @c argument_type is based on the type @c T of the wrapper's * target object: * * @li if @c T is a pointer to a function type accepting a single argument, * @c argument_type is a synonym for the return type of @c T; * * @li if @c T is a class type with a member type @c argument_type, then @c * argument_type is a synonym for @c T::argument_type; * * @li otherwise @c argument_type is not defined. */ typedef see_below argument_type; /// The type of the function's first argument. /** * The type of @c first_argument_type is based on the type @c T of the * wrapper's target object: * * @li if @c T is a pointer to a function type accepting two arguments, @c * first_argument_type is a synonym for the return type of @c T; * * @li if @c T is a class type with a member type @c first_argument_type, * then @c first_argument_type is a synonym for @c T::first_argument_type; * * @li otherwise @c first_argument_type is not defined. */ typedef see_below first_argument_type; /// The type of the function's second argument. /** * The type of @c second_argument_type is based on the type @c T of the * wrapper's target object: * * @li if @c T is a pointer to a function type accepting two arguments, @c * second_argument_type is a synonym for the return type of @c T; * * @li if @c T is a class type with a member type @c first_argument_type, * then @c second_argument_type is a synonym for @c T::second_argument_type; * * @li otherwise @c second_argument_type is not defined. */ typedef see_below second_argument_type; #endif // defined(GENERATING_DOCUMENTATION) /// Construct an executor wrapper for the specified object. /** * This constructor is only valid if the type @c T is constructible from type * @c U. */ template executor_binder(executor_arg_t, const executor_type& e, BOOST_ASIO_MOVE_ARG(U) u) : base_type(e, BOOST_ASIO_MOVE_CAST(U)(u)) { } /// Copy constructor. executor_binder(const executor_binder& other) : base_type(other.get_executor(), other.get()) { } /// Construct a copy, but specify a different executor. executor_binder(executor_arg_t, const executor_type& e, const executor_binder& other) : base_type(e, other.get()) { } /// Construct a copy of a different executor wrapper type. /** * This constructor is only valid if the @c Executor type is constructible * from type @c OtherExecutor, and the type @c T is constructible from type * @c U. */ template executor_binder(const executor_binder& other) : base_type(other.get_executor(), other.get()) { } /// Construct a copy of a different executor wrapper type, but specify a /// different executor. /** * This constructor is only valid if the type @c T is constructible from type * @c U. */ template executor_binder(executor_arg_t, const executor_type& e, const executor_binder& other) : base_type(e, other.get()) { } #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) /// Move constructor. executor_binder(executor_binder&& other) : base_type(BOOST_ASIO_MOVE_CAST(executor_type)(other.get_executor()), BOOST_ASIO_MOVE_CAST(T)(other.get())) { } /// Move construct the target object, but specify a different executor. executor_binder(executor_arg_t, const executor_type& e, executor_binder&& other) : base_type(e, BOOST_ASIO_MOVE_CAST(T)(other.get())) { } /// Move construct from a different executor wrapper type. template executor_binder(executor_binder&& other) : base_type(BOOST_ASIO_MOVE_CAST(OtherExecutor)(other.get_executor()), BOOST_ASIO_MOVE_CAST(U)(other.get())) { } /// Move construct from a different executor wrapper type, but specify a /// different executor. template executor_binder(executor_arg_t, const executor_type& e, executor_binder&& other) : base_type(e, BOOST_ASIO_MOVE_CAST(U)(other.get())) { } #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) /// Destructor. ~executor_binder() { } /// Obtain a reference to the target object. target_type& get() BOOST_ASIO_NOEXCEPT { return this->target_; } /// Obtain a reference to the target object. const target_type& get() const BOOST_ASIO_NOEXCEPT { return this->target_; } /// Obtain the associated executor. executor_type get_executor() const BOOST_ASIO_NOEXCEPT { return this->executor_; } #if defined(GENERATING_DOCUMENTATION) template auto operator()(Args&& ...); template auto operator()(Args&& ...) const; #elif defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) /// Forwarding function call operator. template typename result_of::type operator()( BOOST_ASIO_MOVE_ARG(Args)... args) { return this->target_(BOOST_ASIO_MOVE_CAST(Args)(args)...); } /// Forwarding function call operator. template typename result_of::type operator()( BOOST_ASIO_MOVE_ARG(Args)... args) const { return this->target_(BOOST_ASIO_MOVE_CAST(Args)(args)...); } #elif defined(BOOST_ASIO_HAS_STD_TYPE_TRAITS) && !defined(_MSC_VER) typename detail::executor_binder_result_of0::type operator()() { return this->target_(); } typename detail::executor_binder_result_of0::type operator()() const { return this->target_(); } #define BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF(n) \ template \ typename result_of::type operator()( \ BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \ { \ return this->target_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \ } \ \ template \ typename result_of::type operator()( \ BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) const \ { \ return this->target_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \ } \ /**/ BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF) #undef BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF #else // defined(BOOST_ASIO_HAS_STD_TYPE_TRAITS) && !defined(_MSC_VER) typedef typename detail::executor_binder_result_type::result_type_or_void result_type_or_void; result_type_or_void operator()() { return this->target_(); } result_type_or_void operator()() const { return this->target_(); } #define BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF(n) \ template \ result_type_or_void operator()( \ BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \ { \ return this->target_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \ } \ \ template \ result_type_or_void operator()( \ BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) const \ { \ return this->target_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \ } \ /**/ BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF) #undef BOOST_ASIO_PRIVATE_BIND_EXECUTOR_CALL_DEF #endif // defined(BOOST_ASIO_HAS_STD_TYPE_TRAITS) && !defined(_MSC_VER) private: typedef detail::executor_binder_base::value> base_type; }; /// Associate an object of type @c T with an executor of type @c Executor. template inline executor_binder::type, Executor> bind_executor(const Executor& ex, BOOST_ASIO_MOVE_ARG(T) t, typename enable_if::value>::type* = 0) { return executor_binder::type, Executor>( executor_arg_t(), ex, BOOST_ASIO_MOVE_CAST(T)(t)); } /// Associate an object of type @c T with an execution context's executor. template inline executor_binder::type, typename ExecutionContext::executor_type> bind_executor(ExecutionContext& ctx, BOOST_ASIO_MOVE_ARG(T) t, typename enable_if::value>::type* = 0) { return executor_binder::type, typename ExecutionContext::executor_type>( executor_arg_t(), ctx.get_executor(), BOOST_ASIO_MOVE_CAST(T)(t)); } #if !defined(GENERATING_DOCUMENTATION) template struct uses_executor, Executor> : true_type {}; template class async_result, Signature> { public: typedef executor_binder< typename async_result::completion_handler_type, Executor> completion_handler_type; typedef typename async_result::return_type return_type; explicit async_result(executor_binder& b) : target_(b.get()) { } return_type get() { return target_.get(); } private: async_result(const async_result&) BOOST_ASIO_DELETED; async_result& operator=(const async_result&) BOOST_ASIO_DELETED; async_result target_; }; #if !defined(BOOST_ASIO_NO_DEPRECATED) template struct handler_type, Signature> { typedef executor_binder< typename handler_type::type, Executor> type; }; template class async_result > { public: typedef typename async_result::type type; explicit async_result(executor_binder& b) : target_(b.get()) { } type get() { return target_.get(); } private: async_result target_; }; #endif // !defined(BOOST_ASIO_NO_DEPRECATED) template struct associated_allocator, Allocator> { typedef typename associated_allocator::type type; static type get(const executor_binder& b, const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT { return associated_allocator::get(b.get(), a); } }; template struct associated_executor, Executor1> { typedef Executor type; static type get(const executor_binder& b, const Executor1& = Executor1()) BOOST_ASIO_NOEXCEPT { return b.get_executor(); } }; #endif // !defined(GENERATING_DOCUMENTATION) } // namespace asio } // namespace lslboost #include #endif // BOOST_ASIO_BIND_EXECUTOR_HPP