// // bind_immediate_executor.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_IMMEDIATE_EXECUTOR_HPP #define BOOST_ASIO_BIND_IMMEDIATE_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 namespace boost { namespace asio { namespace detail { // Helper to automatically define nested typedef result_type. template struct immediate_executor_binder_result_type { protected: typedef void result_type_or_void; }; template struct immediate_executor_binder_result_type> { typedef typename T::result_type result_type; protected: typedef result_type result_type_or_void; }; template struct immediate_executor_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct immediate_executor_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct immediate_executor_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct immediate_executor_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct immediate_executor_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct immediate_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 immediate_executor_binder_argument_type {}; template struct immediate_executor_binder_argument_type> { typedef typename T::argument_type argument_type; }; template struct immediate_executor_binder_argument_type { typedef A1 argument_type; }; template struct immediate_executor_binder_argument_type { typedef A1 argument_type; }; // Helper to automatically define nested typedefs first_argument_type and // second_argument_type. template struct immediate_executor_binder_argument_types {}; template struct immediate_executor_binder_argument_types> { typedef typename T::first_argument_type first_argument_type; typedef typename T::second_argument_type second_argument_type; }; template struct immediate_executor_binder_argument_type { typedef A1 first_argument_type; typedef A2 second_argument_type; }; template struct immediate_executor_binder_argument_type { typedef A1 first_argument_type; typedef A2 second_argument_type; }; } // namespace detail /// A call wrapper type to bind a immediate executor of type @c Executor /// to an object of type @c T. template class immediate_executor_binder #if !defined(GENERATING_DOCUMENTATION) : public detail::immediate_executor_binder_result_type, public detail::immediate_executor_binder_argument_type, public detail::immediate_executor_binder_argument_types #endif // !defined(GENERATING_DOCUMENTATION) { public: /// The type of the target object. typedef T target_type; /// The type of the associated immediate executor. typedef Executor immediate_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 a immediate executor wrapper for the specified object. /** * This constructor is only valid if the type @c T is constructible from type * @c U. */ template immediate_executor_binder(const immediate_executor_type& e, U&& u) : executor_(e), target_(static_cast(u)) { } /// Copy constructor. immediate_executor_binder(const immediate_executor_binder& other) : executor_(other.get_immediate_executor()), target_(other.get()) { } /// Construct a copy, but specify a different immediate executor. immediate_executor_binder(const immediate_executor_type& e, const immediate_executor_binder& other) : executor_(e), target_(other.get()) { } /// Construct a copy of a different immediate 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 immediate_executor_binder( const immediate_executor_binder& other, constraint_t::value> = 0, constraint_t::value> = 0) : executor_(other.get_immediate_executor()), target_(other.get()) { } /// Construct a copy of a different immediate executor wrapper type, but /// specify a different immediate executor. /** * This constructor is only valid if the type @c T is constructible from type * @c U. */ template immediate_executor_binder(const immediate_executor_type& e, const immediate_executor_binder& other, constraint_t::value> = 0) : executor_(e), target_(other.get()) { } /// Move constructor. immediate_executor_binder(immediate_executor_binder&& other) : executor_(static_cast( other.get_immediate_executor())), target_(static_cast(other.get())) { } /// Move construct the target object, but specify a different immediate /// executor. immediate_executor_binder(const immediate_executor_type& e, immediate_executor_binder&& other) : executor_(e), target_(static_cast(other.get())) { } /// Move construct from a different immediate executor wrapper type. template immediate_executor_binder( immediate_executor_binder&& other, constraint_t::value> = 0, constraint_t::value> = 0) : executor_(static_cast( other.get_immediate_executor())), target_(static_cast(other.get())) { } /// Move construct from a different immediate executor wrapper type, but /// specify a different immediate executor. template immediate_executor_binder(const immediate_executor_type& e, immediate_executor_binder&& other, constraint_t::value> = 0) : executor_(e), target_(static_cast(other.get())) { } /// Destructor. ~immediate_executor_binder() { } /// Obtain a reference to the target object. target_type& get() noexcept { return target_; } /// Obtain a reference to the target object. const target_type& get() const noexcept { return target_; } /// Obtain the associated immediate executor. immediate_executor_type get_immediate_executor() const noexcept { return executor_; } /// Forwarding function call operator. template result_of_t operator()(Args&&... args) { return target_(static_cast(args)...); } /// Forwarding function call operator. template result_of_t operator()(Args&&... args) const { return target_(static_cast(args)...); } private: Executor executor_; T target_; }; /// A function object type that adapts a @ref completion_token to specify that /// the completion handler should have the supplied executor as its associated /// immediate executor. /** * May also be used directly as a completion token, in which case it adapts the * asynchronous operation's default completion token (or boost::asio::deferred * if no default is available). */ template struct partial_immediate_executor_binder { /// Constructor that specifies associated executor. explicit partial_immediate_executor_binder(const Executor& ex) : executor_(ex) { } /// Adapt a @ref completion_token to specify that the completion handler /// should have the executor as its associated immediate executor. template BOOST_ASIO_NODISCARD inline constexpr immediate_executor_binder, Executor> operator()(CompletionToken&& completion_token) const { return immediate_executor_binder, Executor>( static_cast(completion_token), executor_); } //private: Executor executor_; }; /// Create a partial completion token that associates an executor. template BOOST_ASIO_NODISCARD inline partial_immediate_executor_binder bind_immediate_executor(const Executor& ex) { return partial_immediate_executor_binder(ex); } /// Associate an object of type @c T with a immediate executor of type /// @c Executor. template BOOST_ASIO_NODISCARD inline immediate_executor_binder, Executor> bind_immediate_executor(const Executor& e, T&& t) { return immediate_executor_binder< decay_t, Executor>( e, static_cast(t)); } #if !defined(GENERATING_DOCUMENTATION) namespace detail { template class immediate_executor_binder_completion_handler_async_result { public: template explicit immediate_executor_binder_completion_handler_async_result(T&) { } }; template class immediate_executor_binder_completion_handler_async_result< TargetAsyncResult, Executor, void_t< typename TargetAsyncResult::completion_handler_type >> { private: TargetAsyncResult target_; public: typedef immediate_executor_binder< typename TargetAsyncResult::completion_handler_type, Executor> completion_handler_type; explicit immediate_executor_binder_completion_handler_async_result( typename TargetAsyncResult::completion_handler_type& handler) : target_(handler) { } auto get() -> decltype(target_.get()) { return target_.get(); } }; template struct immediate_executor_binder_async_result_return_type { }; template struct immediate_executor_binder_async_result_return_type< TargetAsyncResult, void_t< typename TargetAsyncResult::return_type >> { typedef typename TargetAsyncResult::return_type return_type; }; } // namespace detail template class async_result, Signature> : public detail::immediate_executor_binder_completion_handler_async_result< async_result, Executor>, public detail::immediate_executor_binder_async_result_return_type< async_result> { public: explicit async_result(immediate_executor_binder& b) : detail::immediate_executor_binder_completion_handler_async_result< async_result, Executor>(b.get()) { } template struct init_wrapper : detail::initiation_base { using detail::initiation_base::initiation_base; template void operator()(Handler&& handler, const Executor& e, Args&&... args) && { static_cast(*this)( immediate_executor_binder< decay_t, Executor>( e, static_cast(handler)), static_cast(args)...); } template void operator()(Handler&& handler, const Executor& e, Args&&... args) const & { static_cast(*this)( immediate_executor_binder< decay_t, Executor>( e, static_cast(handler)), static_cast(args)...); } }; template static auto initiate(Initiation&& initiation, RawCompletionToken&& token, Args&&... args) -> decltype( async_initiate< conditional_t< is_const>::value, const T, T>, Signature>( declval>>(), token.get(), token.get_immediate_executor(), static_cast(args)...)) { return async_initiate< conditional_t< is_const>::value, const T, T>, Signature>( init_wrapper>( static_cast(initiation)), token.get(), token.get_immediate_executor(), static_cast(args)...); } private: async_result(const async_result&) = delete; async_result& operator=(const async_result&) = delete; async_result target_; }; template struct async_result, Signatures...> { template static auto initiate(Initiation&& initiation, RawCompletionToken&& token, Args&&... args) -> decltype( async_initiate( static_cast(initiation), immediate_executor_binder< default_completion_token_t>, Executor>(token.executor_, default_completion_token_t>{}), static_cast(args)...)) { return async_initiate( static_cast(initiation), immediate_executor_binder< default_completion_token_t>, Executor>(token.executor_, default_completion_token_t>{}), static_cast(args)...); } }; template