// // bind_allocator.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 ASIO_BIND_ALLOCATOR_HPP #define ASIO_BIND_ALLOCATOR_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include "asio/detail/type_traits.hpp" #include "asio/associated_allocator.hpp" #include "asio/associator.hpp" #include "asio/async_result.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { // Helper to automatically define nested typedef result_type. template struct allocator_binder_result_type { protected: typedef void result_type_or_void; }; template struct allocator_binder_result_type> { typedef typename T::result_type result_type; protected: typedef result_type result_type_or_void; }; template struct allocator_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct allocator_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct allocator_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct allocator_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct allocator_binder_result_type { typedef R result_type; protected: typedef result_type result_type_or_void; }; template struct allocator_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 allocator_binder_argument_type {}; template struct allocator_binder_argument_type> { typedef typename T::argument_type argument_type; }; template struct allocator_binder_argument_type { typedef A1 argument_type; }; template struct allocator_binder_argument_type { typedef A1 argument_type; }; // Helper to automatically define nested typedefs first_argument_type and // second_argument_type. template struct allocator_binder_argument_types {}; template struct allocator_binder_argument_types> { typedef typename T::first_argument_type first_argument_type; typedef typename T::second_argument_type second_argument_type; }; template struct allocator_binder_argument_type { typedef A1 first_argument_type; typedef A2 second_argument_type; }; template struct allocator_binder_argument_type { typedef A1 first_argument_type; typedef A2 second_argument_type; }; } // namespace detail /// A call wrapper type to bind an allocator of type @c Allocator /// to an object of type @c T. template class allocator_binder #if !defined(GENERATING_DOCUMENTATION) : public detail::allocator_binder_result_type, public detail::allocator_binder_argument_type, public detail::allocator_binder_argument_types #endif // !defined(GENERATING_DOCUMENTATION) { public: /// The type of the target object. typedef T target_type; /// The type of the associated allocator. typedef Allocator allocator_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 allocator wrapper for the specified object. /** * This constructor is only valid if the type @c T is constructible from type * @c U. */ template allocator_binder(const allocator_type& s, U&& u) : allocator_(s), target_(static_cast(u)) { } /// Copy constructor. allocator_binder(const allocator_binder& other) : allocator_(other.get_allocator()), target_(other.get()) { } /// Construct a copy, but specify a different allocator. allocator_binder(const allocator_type& s, const allocator_binder& other) : allocator_(s), target_(other.get()) { } /// Construct a copy of a different allocator wrapper type. /** * This constructor is only valid if the @c Allocator type is * constructible from type @c OtherAllocator, and the type @c T is * constructible from type @c U. */ template allocator_binder(const allocator_binder& other, constraint_t::value> = 0, constraint_t::value> = 0) : allocator_(other.get_allocator()), target_(other.get()) { } /// Construct a copy of a different allocator wrapper type, but /// specify a different allocator. /** * This constructor is only valid if the type @c T is constructible from type * @c U. */ template allocator_binder(const allocator_type& s, const allocator_binder& other, constraint_t::value> = 0) : allocator_(s), target_(other.get()) { } /// Move constructor. allocator_binder(allocator_binder&& other) : allocator_(static_cast( other.get_allocator())), target_(static_cast(other.get())) { } /// Move construct the target object, but specify a different allocator. allocator_binder(const allocator_type& s, allocator_binder&& other) : allocator_(s), target_(static_cast(other.get())) { } /// Move construct from a different allocator wrapper type. template allocator_binder( allocator_binder&& other, constraint_t::value> = 0, constraint_t::value> = 0) : allocator_(static_cast( other.get_allocator())), target_(static_cast(other.get())) { } /// Move construct from a different allocator wrapper type, but /// specify a different allocator. template allocator_binder(const allocator_type& s, allocator_binder&& other, constraint_t::value> = 0) : allocator_(s), target_(static_cast(other.get())) { } /// Destructor. ~allocator_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 allocator. allocator_type get_allocator() const noexcept { return allocator_; } /// 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: Allocator allocator_; T target_; }; /// Associate an object of type @c T with an allocator of type /// @c Allocator. template ASIO_NODISCARD inline allocator_binder, Allocator> bind_allocator(const Allocator& s, T&& t) { return allocator_binder, Allocator>(s, static_cast(t)); } #if !defined(GENERATING_DOCUMENTATION) namespace detail { template class allocator_binder_completion_handler_async_result { public: template explicit allocator_binder_completion_handler_async_result(T&) { } }; template class allocator_binder_completion_handler_async_result< TargetAsyncResult, Allocator, void_t> { private: TargetAsyncResult target_; public: typedef allocator_binder< typename TargetAsyncResult::completion_handler_type, Allocator> completion_handler_type; explicit allocator_binder_completion_handler_async_result( typename TargetAsyncResult::completion_handler_type& handler) : target_(handler) { } auto get() -> decltype(target_.get()) { return target_.get(); } }; template struct allocator_binder_async_result_return_type { }; template struct allocator_binder_async_result_return_type< TargetAsyncResult, void_type> { typedef typename TargetAsyncResult::return_type return_type; }; } // namespace detail template class async_result, Signature> : public detail::allocator_binder_completion_handler_async_result< async_result, Allocator>, public detail::allocator_binder_async_result_return_type< async_result> { public: explicit async_result(allocator_binder& b) : detail::allocator_binder_completion_handler_async_result< async_result, Allocator>(b.get()) { } template struct init_wrapper { template init_wrapper(const Allocator& allocator, Init&& init) : allocator_(allocator), initiation_(static_cast(init)) { } template void operator()(Handler&& handler, Args&&... args) { static_cast(initiation_)( allocator_binder, Allocator>( allocator_, static_cast(handler)), static_cast(args)...); } template void operator()(Handler&& handler, Args&&... args) const { initiation_( allocator_binder, Allocator>( allocator_, static_cast(handler)), static_cast(args)...); } Allocator allocator_; Initiation initiation_; }; template static auto initiate(Initiation&& initiation, RawCompletionToken&& token, Args&&... args) -> decltype( async_initiate( declval>>(), token.get(), static_cast(args)...)) { return async_initiate( init_wrapper>(token.get_allocator(), static_cast(initiation)), token.get(), static_cast(args)...); } private: async_result(const async_result&) = delete; async_result& operator=(const async_result&) = delete; async_result target_; }; template