2 // detail/wrapped_handler.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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_DETAIL_WRAPPED_HANDLER_HPP
12 #define BOOST_ASIO_DETAIL_WRAPPED_HANDLER_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/bind_handler.hpp>
19 #include <boost/asio/detail/handler_alloc_helpers.hpp>
20 #include <boost/asio/detail/handler_cont_helpers.hpp>
21 #include <boost/asio/detail/handler_invoke_helpers.hpp>
23 #include <boost/asio/detail/push_options.hpp>
29 struct is_continuation_delegated
31 template <typename Dispatcher, typename Handler>
32 bool operator()(Dispatcher&, Handler& handler) const
34 return boost_asio_handler_cont_helpers::is_continuation(handler);
38 struct is_continuation_if_running
40 template <typename Dispatcher, typename Handler>
41 bool operator()(Dispatcher& dispatcher, Handler&) const
43 return dispatcher.running_in_this_thread();
47 template <typename Dispatcher, typename Handler,
48 typename IsContinuation = is_continuation_delegated>
52 typedef void result_type;
54 wrapped_handler(Dispatcher dispatcher, Handler& handler)
55 : dispatcher_(dispatcher),
56 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
60 #if defined(BOOST_ASIO_HAS_MOVE)
61 wrapped_handler(const wrapped_handler& other)
62 : dispatcher_(other.dispatcher_),
63 handler_(other.handler_)
67 wrapped_handler(wrapped_handler&& other)
68 : dispatcher_(other.dispatcher_),
69 handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_))
72 #endif // defined(BOOST_ASIO_HAS_MOVE)
76 dispatcher_.dispatch(BOOST_ASIO_MOVE_CAST(Handler)(handler_));
79 void operator()() const
81 dispatcher_.dispatch(handler_);
84 template <typename Arg1>
85 void operator()(const Arg1& arg1)
87 dispatcher_.dispatch(detail::bind_handler(handler_, arg1));
90 template <typename Arg1>
91 void operator()(const Arg1& arg1) const
93 dispatcher_.dispatch(detail::bind_handler(handler_, arg1));
96 template <typename Arg1, typename Arg2>
97 void operator()(const Arg1& arg1, const Arg2& arg2)
99 dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2));
102 template <typename Arg1, typename Arg2>
103 void operator()(const Arg1& arg1, const Arg2& arg2) const
105 dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2));
108 template <typename Arg1, typename Arg2, typename Arg3>
109 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3)
111 dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3));
114 template <typename Arg1, typename Arg2, typename Arg3>
115 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) const
117 dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3));
120 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
121 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
124 dispatcher_.dispatch(
125 detail::bind_handler(handler_, arg1, arg2, arg3, arg4));
128 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
129 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
130 const Arg4& arg4) const
132 dispatcher_.dispatch(
133 detail::bind_handler(handler_, arg1, arg2, arg3, arg4));
136 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
138 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
139 const Arg4& arg4, const Arg5& arg5)
141 dispatcher_.dispatch(
142 detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5));
145 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
147 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
148 const Arg4& arg4, const Arg5& arg5) const
150 dispatcher_.dispatch(
151 detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5));
155 Dispatcher dispatcher_;
159 template <typename Handler, typename Context>
160 class rewrapped_handler
163 explicit rewrapped_handler(Handler& handler, const Context& context)
165 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
169 explicit rewrapped_handler(const Handler& handler, const Context& context)
175 #if defined(BOOST_ASIO_HAS_MOVE)
176 rewrapped_handler(const rewrapped_handler& other)
177 : context_(other.context_),
178 handler_(other.handler_)
182 rewrapped_handler(rewrapped_handler&& other)
183 : context_(BOOST_ASIO_MOVE_CAST(Context)(other.context_)),
184 handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_))
187 #endif // defined(BOOST_ASIO_HAS_MOVE)
194 void operator()() const
204 template <typename Dispatcher, typename Handler, typename IsContinuation>
205 inline void* asio_handler_allocate(std::size_t size,
206 wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
208 return boost_asio_handler_alloc_helpers::allocate(
209 size, this_handler->handler_);
212 template <typename Dispatcher, typename Handler, typename IsContinuation>
213 inline void asio_handler_deallocate(void* pointer, std::size_t size,
214 wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
216 boost_asio_handler_alloc_helpers::deallocate(
217 pointer, size, this_handler->handler_);
220 template <typename Dispatcher, typename Handler, typename IsContinuation>
221 inline bool asio_handler_is_continuation(
222 wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
224 return IsContinuation()(this_handler->dispatcher_, this_handler->handler_);
227 template <typename Function, typename Dispatcher,
228 typename Handler, typename IsContinuation>
229 inline void asio_handler_invoke(Function& function,
230 wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
232 this_handler->dispatcher_.dispatch(
233 rewrapped_handler<Function, Handler>(
234 function, this_handler->handler_));
237 template <typename Function, typename Dispatcher,
238 typename Handler, typename IsContinuation>
239 inline void asio_handler_invoke(const Function& function,
240 wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
242 this_handler->dispatcher_.dispatch(
243 rewrapped_handler<Function, Handler>(
244 function, this_handler->handler_));
247 template <typename Handler, typename Context>
248 inline void* asio_handler_allocate(std::size_t size,
249 rewrapped_handler<Handler, Context>* this_handler)
251 return boost_asio_handler_alloc_helpers::allocate(
252 size, this_handler->context_);
255 template <typename Handler, typename Context>
256 inline void asio_handler_deallocate(void* pointer, std::size_t size,
257 rewrapped_handler<Handler, Context>* this_handler)
259 boost_asio_handler_alloc_helpers::deallocate(
260 pointer, size, this_handler->context_);
263 template <typename Dispatcher, typename Context>
264 inline bool asio_handler_is_continuation(
265 rewrapped_handler<Dispatcher, Context>* this_handler)
267 return boost_asio_handler_cont_helpers::is_continuation(
268 this_handler->context_);
271 template <typename Function, typename Handler, typename Context>
272 inline void asio_handler_invoke(Function& function,
273 rewrapped_handler<Handler, Context>* this_handler)
275 boost_asio_handler_invoke_helpers::invoke(
276 function, this_handler->context_);
279 template <typename Function, typename Handler, typename Context>
280 inline void asio_handler_invoke(const Function& function,
281 rewrapped_handler<Handler, Context>* this_handler)
283 boost_asio_handler_invoke_helpers::invoke(
284 function, this_handler->context_);
287 } // namespace detail
291 #include <boost/asio/detail/pop_options.hpp>
293 #endif // BOOST_ASIO_DETAIL_WRAPPED_HANDLER_HPP