Chris@16: // Chris@16: // detail/wrapped_handler.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_DETAIL_WRAPPED_HANDLER_HPP Chris@16: #define BOOST_ASIO_DETAIL_WRAPPED_HANDLER_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace detail { Chris@16: Chris@16: struct is_continuation_delegated Chris@16: { Chris@16: template Chris@16: bool operator()(Dispatcher&, Handler& handler) const Chris@16: { Chris@16: return boost_asio_handler_cont_helpers::is_continuation(handler); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct is_continuation_if_running Chris@16: { Chris@16: template Chris@16: bool operator()(Dispatcher& dispatcher, Handler&) const Chris@16: { Chris@16: return dispatcher.running_in_this_thread(); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: class wrapped_handler Chris@16: { Chris@16: public: Chris@16: typedef void result_type; Chris@16: Chris@16: wrapped_handler(Dispatcher dispatcher, Handler& handler) Chris@16: : dispatcher_(dispatcher), Chris@16: handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)) Chris@16: { Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) Chris@16: wrapped_handler(const wrapped_handler& other) Chris@16: : dispatcher_(other.dispatcher_), Chris@16: handler_(other.handler_) Chris@16: { Chris@16: } Chris@16: Chris@16: wrapped_handler(wrapped_handler&& other) Chris@16: : dispatcher_(other.dispatcher_), Chris@16: handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_)) Chris@16: { Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) Chris@16: Chris@16: void operator()() Chris@16: { Chris@16: dispatcher_.dispatch(BOOST_ASIO_MOVE_CAST(Handler)(handler_)); Chris@16: } Chris@16: Chris@16: void operator()() const Chris@16: { Chris@16: dispatcher_.dispatch(handler_); Chris@16: } Chris@16: Chris@16: template Chris@16: void operator()(const Arg1& arg1) Chris@16: { Chris@16: dispatcher_.dispatch(detail::bind_handler(handler_, arg1)); Chris@16: } Chris@16: Chris@16: template Chris@16: void operator()(const Arg1& arg1) const Chris@16: { Chris@16: dispatcher_.dispatch(detail::bind_handler(handler_, arg1)); Chris@16: } Chris@16: Chris@16: template Chris@16: void operator()(const Arg1& arg1, const Arg2& arg2) Chris@16: { Chris@16: dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2)); Chris@16: } Chris@16: Chris@16: template Chris@16: void operator()(const Arg1& arg1, const Arg2& arg2) const Chris@16: { Chris@16: dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2)); Chris@16: } Chris@16: Chris@16: template Chris@16: void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) Chris@16: { Chris@16: dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3)); Chris@16: } Chris@16: Chris@16: template Chris@16: void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) const Chris@16: { Chris@16: dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3)); Chris@16: } Chris@16: Chris@16: template Chris@16: void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, Chris@16: const Arg4& arg4) Chris@16: { Chris@16: dispatcher_.dispatch( Chris@16: detail::bind_handler(handler_, arg1, arg2, arg3, arg4)); Chris@16: } Chris@16: Chris@16: template Chris@16: void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, Chris@16: const Arg4& arg4) const Chris@16: { Chris@16: dispatcher_.dispatch( Chris@16: detail::bind_handler(handler_, arg1, arg2, arg3, arg4)); Chris@16: } Chris@16: Chris@16: template Chris@16: void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, Chris@16: const Arg4& arg4, const Arg5& arg5) Chris@16: { Chris@16: dispatcher_.dispatch( Chris@16: detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5)); Chris@16: } Chris@16: Chris@16: template Chris@16: void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, Chris@16: const Arg4& arg4, const Arg5& arg5) const Chris@16: { Chris@16: dispatcher_.dispatch( Chris@16: detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5)); Chris@16: } Chris@16: Chris@16: //private: Chris@16: Dispatcher dispatcher_; Chris@16: Handler handler_; Chris@16: }; Chris@16: Chris@16: template Chris@16: class rewrapped_handler Chris@16: { Chris@16: public: Chris@16: explicit rewrapped_handler(Handler& handler, const Context& context) Chris@16: : context_(context), Chris@16: handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)) Chris@16: { Chris@16: } Chris@16: Chris@16: explicit rewrapped_handler(const Handler& handler, const Context& context) Chris@16: : context_(context), Chris@16: handler_(handler) Chris@16: { Chris@16: } Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) Chris@16: rewrapped_handler(const rewrapped_handler& other) Chris@16: : context_(other.context_), Chris@16: handler_(other.handler_) Chris@16: { Chris@16: } Chris@16: Chris@16: rewrapped_handler(rewrapped_handler&& other) Chris@16: : context_(BOOST_ASIO_MOVE_CAST(Context)(other.context_)), Chris@16: handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_)) Chris@16: { Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) Chris@16: Chris@16: void operator()() Chris@16: { Chris@16: handler_(); Chris@16: } Chris@16: Chris@16: void operator()() const Chris@16: { Chris@16: handler_(); Chris@16: } Chris@16: Chris@16: //private: Chris@16: Context context_; Chris@16: Handler handler_; Chris@16: }; Chris@16: Chris@16: template Chris@16: inline void* asio_handler_allocate(std::size_t size, Chris@16: wrapped_handler* this_handler) Chris@16: { Chris@16: return boost_asio_handler_alloc_helpers::allocate( Chris@16: size, this_handler->handler_); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void asio_handler_deallocate(void* pointer, std::size_t size, Chris@16: wrapped_handler* this_handler) Chris@16: { Chris@16: boost_asio_handler_alloc_helpers::deallocate( Chris@16: pointer, size, this_handler->handler_); Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool asio_handler_is_continuation( Chris@16: wrapped_handler* this_handler) Chris@16: { Chris@16: return IsContinuation()(this_handler->dispatcher_, this_handler->handler_); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void asio_handler_invoke(Function& function, Chris@16: wrapped_handler* this_handler) Chris@16: { Chris@16: this_handler->dispatcher_.dispatch( Chris@16: rewrapped_handler( Chris@16: function, this_handler->handler_)); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void asio_handler_invoke(const Function& function, Chris@16: wrapped_handler* this_handler) Chris@16: { Chris@16: this_handler->dispatcher_.dispatch( Chris@16: rewrapped_handler( Chris@16: function, this_handler->handler_)); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void* asio_handler_allocate(std::size_t size, Chris@16: rewrapped_handler* this_handler) Chris@16: { Chris@16: return boost_asio_handler_alloc_helpers::allocate( Chris@16: size, this_handler->context_); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void asio_handler_deallocate(void* pointer, std::size_t size, Chris@16: rewrapped_handler* this_handler) Chris@16: { Chris@16: boost_asio_handler_alloc_helpers::deallocate( Chris@16: pointer, size, this_handler->context_); Chris@16: } Chris@16: Chris@16: template Chris@16: inline bool asio_handler_is_continuation( Chris@16: rewrapped_handler* this_handler) Chris@16: { Chris@16: return boost_asio_handler_cont_helpers::is_continuation( Chris@101: this_handler->context_); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void asio_handler_invoke(Function& function, Chris@16: rewrapped_handler* this_handler) Chris@16: { Chris@16: boost_asio_handler_invoke_helpers::invoke( Chris@16: function, this_handler->context_); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void asio_handler_invoke(const Function& function, Chris@16: rewrapped_handler* this_handler) Chris@16: { Chris@16: boost_asio_handler_invoke_helpers::invoke( Chris@16: function, this_handler->context_); Chris@16: } Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_WRAPPED_HANDLER_HPP