Chris@16
|
1 //
|
Chris@16
|
2 // detail/wrapped_handler.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_DETAIL_WRAPPED_HANDLER_HPP
|
Chris@16
|
12 #define BOOST_ASIO_DETAIL_WRAPPED_HANDLER_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/bind_handler.hpp>
|
Chris@16
|
19 #include <boost/asio/detail/handler_alloc_helpers.hpp>
|
Chris@16
|
20 #include <boost/asio/detail/handler_cont_helpers.hpp>
|
Chris@16
|
21 #include <boost/asio/detail/handler_invoke_helpers.hpp>
|
Chris@16
|
22
|
Chris@16
|
23 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost {
|
Chris@16
|
26 namespace asio {
|
Chris@16
|
27 namespace detail {
|
Chris@16
|
28
|
Chris@16
|
29 struct is_continuation_delegated
|
Chris@16
|
30 {
|
Chris@16
|
31 template <typename Dispatcher, typename Handler>
|
Chris@16
|
32 bool operator()(Dispatcher&, Handler& handler) const
|
Chris@16
|
33 {
|
Chris@16
|
34 return boost_asio_handler_cont_helpers::is_continuation(handler);
|
Chris@16
|
35 }
|
Chris@16
|
36 };
|
Chris@16
|
37
|
Chris@16
|
38 struct is_continuation_if_running
|
Chris@16
|
39 {
|
Chris@16
|
40 template <typename Dispatcher, typename Handler>
|
Chris@16
|
41 bool operator()(Dispatcher& dispatcher, Handler&) const
|
Chris@16
|
42 {
|
Chris@16
|
43 return dispatcher.running_in_this_thread();
|
Chris@16
|
44 }
|
Chris@16
|
45 };
|
Chris@16
|
46
|
Chris@16
|
47 template <typename Dispatcher, typename Handler,
|
Chris@16
|
48 typename IsContinuation = is_continuation_delegated>
|
Chris@16
|
49 class wrapped_handler
|
Chris@16
|
50 {
|
Chris@16
|
51 public:
|
Chris@16
|
52 typedef void result_type;
|
Chris@16
|
53
|
Chris@16
|
54 wrapped_handler(Dispatcher dispatcher, Handler& handler)
|
Chris@16
|
55 : dispatcher_(dispatcher),
|
Chris@16
|
56 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
|
Chris@16
|
57 {
|
Chris@16
|
58 }
|
Chris@16
|
59
|
Chris@16
|
60 #if defined(BOOST_ASIO_HAS_MOVE)
|
Chris@16
|
61 wrapped_handler(const wrapped_handler& other)
|
Chris@16
|
62 : dispatcher_(other.dispatcher_),
|
Chris@16
|
63 handler_(other.handler_)
|
Chris@16
|
64 {
|
Chris@16
|
65 }
|
Chris@16
|
66
|
Chris@16
|
67 wrapped_handler(wrapped_handler&& other)
|
Chris@16
|
68 : dispatcher_(other.dispatcher_),
|
Chris@16
|
69 handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_))
|
Chris@16
|
70 {
|
Chris@16
|
71 }
|
Chris@16
|
72 #endif // defined(BOOST_ASIO_HAS_MOVE)
|
Chris@16
|
73
|
Chris@16
|
74 void operator()()
|
Chris@16
|
75 {
|
Chris@16
|
76 dispatcher_.dispatch(BOOST_ASIO_MOVE_CAST(Handler)(handler_));
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 void operator()() const
|
Chris@16
|
80 {
|
Chris@16
|
81 dispatcher_.dispatch(handler_);
|
Chris@16
|
82 }
|
Chris@16
|
83
|
Chris@16
|
84 template <typename Arg1>
|
Chris@16
|
85 void operator()(const Arg1& arg1)
|
Chris@16
|
86 {
|
Chris@16
|
87 dispatcher_.dispatch(detail::bind_handler(handler_, arg1));
|
Chris@16
|
88 }
|
Chris@16
|
89
|
Chris@16
|
90 template <typename Arg1>
|
Chris@16
|
91 void operator()(const Arg1& arg1) const
|
Chris@16
|
92 {
|
Chris@16
|
93 dispatcher_.dispatch(detail::bind_handler(handler_, arg1));
|
Chris@16
|
94 }
|
Chris@16
|
95
|
Chris@16
|
96 template <typename Arg1, typename Arg2>
|
Chris@16
|
97 void operator()(const Arg1& arg1, const Arg2& arg2)
|
Chris@16
|
98 {
|
Chris@16
|
99 dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2));
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 template <typename Arg1, typename Arg2>
|
Chris@16
|
103 void operator()(const Arg1& arg1, const Arg2& arg2) const
|
Chris@16
|
104 {
|
Chris@16
|
105 dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2));
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 template <typename Arg1, typename Arg2, typename Arg3>
|
Chris@16
|
109 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3)
|
Chris@16
|
110 {
|
Chris@16
|
111 dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3));
|
Chris@16
|
112 }
|
Chris@16
|
113
|
Chris@16
|
114 template <typename Arg1, typename Arg2, typename Arg3>
|
Chris@16
|
115 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) const
|
Chris@16
|
116 {
|
Chris@16
|
117 dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3));
|
Chris@16
|
118 }
|
Chris@16
|
119
|
Chris@16
|
120 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
|
Chris@16
|
121 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
|
Chris@16
|
122 const Arg4& arg4)
|
Chris@16
|
123 {
|
Chris@16
|
124 dispatcher_.dispatch(
|
Chris@16
|
125 detail::bind_handler(handler_, arg1, arg2, arg3, arg4));
|
Chris@16
|
126 }
|
Chris@16
|
127
|
Chris@16
|
128 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
|
Chris@16
|
129 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
|
Chris@16
|
130 const Arg4& arg4) const
|
Chris@16
|
131 {
|
Chris@16
|
132 dispatcher_.dispatch(
|
Chris@16
|
133 detail::bind_handler(handler_, arg1, arg2, arg3, arg4));
|
Chris@16
|
134 }
|
Chris@16
|
135
|
Chris@16
|
136 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
|
Chris@16
|
137 typename Arg5>
|
Chris@16
|
138 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
|
Chris@16
|
139 const Arg4& arg4, const Arg5& arg5)
|
Chris@16
|
140 {
|
Chris@16
|
141 dispatcher_.dispatch(
|
Chris@16
|
142 detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5));
|
Chris@16
|
143 }
|
Chris@16
|
144
|
Chris@16
|
145 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
|
Chris@16
|
146 typename Arg5>
|
Chris@16
|
147 void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
|
Chris@16
|
148 const Arg4& arg4, const Arg5& arg5) const
|
Chris@16
|
149 {
|
Chris@16
|
150 dispatcher_.dispatch(
|
Chris@16
|
151 detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5));
|
Chris@16
|
152 }
|
Chris@16
|
153
|
Chris@16
|
154 //private:
|
Chris@16
|
155 Dispatcher dispatcher_;
|
Chris@16
|
156 Handler handler_;
|
Chris@16
|
157 };
|
Chris@16
|
158
|
Chris@16
|
159 template <typename Handler, typename Context>
|
Chris@16
|
160 class rewrapped_handler
|
Chris@16
|
161 {
|
Chris@16
|
162 public:
|
Chris@16
|
163 explicit rewrapped_handler(Handler& handler, const Context& context)
|
Chris@16
|
164 : context_(context),
|
Chris@16
|
165 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
|
Chris@16
|
166 {
|
Chris@16
|
167 }
|
Chris@16
|
168
|
Chris@16
|
169 explicit rewrapped_handler(const Handler& handler, const Context& context)
|
Chris@16
|
170 : context_(context),
|
Chris@16
|
171 handler_(handler)
|
Chris@16
|
172 {
|
Chris@16
|
173 }
|
Chris@16
|
174
|
Chris@16
|
175 #if defined(BOOST_ASIO_HAS_MOVE)
|
Chris@16
|
176 rewrapped_handler(const rewrapped_handler& other)
|
Chris@16
|
177 : context_(other.context_),
|
Chris@16
|
178 handler_(other.handler_)
|
Chris@16
|
179 {
|
Chris@16
|
180 }
|
Chris@16
|
181
|
Chris@16
|
182 rewrapped_handler(rewrapped_handler&& other)
|
Chris@16
|
183 : context_(BOOST_ASIO_MOVE_CAST(Context)(other.context_)),
|
Chris@16
|
184 handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_))
|
Chris@16
|
185 {
|
Chris@16
|
186 }
|
Chris@16
|
187 #endif // defined(BOOST_ASIO_HAS_MOVE)
|
Chris@16
|
188
|
Chris@16
|
189 void operator()()
|
Chris@16
|
190 {
|
Chris@16
|
191 handler_();
|
Chris@16
|
192 }
|
Chris@16
|
193
|
Chris@16
|
194 void operator()() const
|
Chris@16
|
195 {
|
Chris@16
|
196 handler_();
|
Chris@16
|
197 }
|
Chris@16
|
198
|
Chris@16
|
199 //private:
|
Chris@16
|
200 Context context_;
|
Chris@16
|
201 Handler handler_;
|
Chris@16
|
202 };
|
Chris@16
|
203
|
Chris@16
|
204 template <typename Dispatcher, typename Handler, typename IsContinuation>
|
Chris@16
|
205 inline void* asio_handler_allocate(std::size_t size,
|
Chris@16
|
206 wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
|
Chris@16
|
207 {
|
Chris@16
|
208 return boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
209 size, this_handler->handler_);
|
Chris@16
|
210 }
|
Chris@16
|
211
|
Chris@16
|
212 template <typename Dispatcher, typename Handler, typename IsContinuation>
|
Chris@16
|
213 inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
Chris@16
|
214 wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
|
Chris@16
|
215 {
|
Chris@16
|
216 boost_asio_handler_alloc_helpers::deallocate(
|
Chris@16
|
217 pointer, size, this_handler->handler_);
|
Chris@16
|
218 }
|
Chris@16
|
219
|
Chris@16
|
220 template <typename Dispatcher, typename Handler, typename IsContinuation>
|
Chris@16
|
221 inline bool asio_handler_is_continuation(
|
Chris@16
|
222 wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
|
Chris@16
|
223 {
|
Chris@16
|
224 return IsContinuation()(this_handler->dispatcher_, this_handler->handler_);
|
Chris@16
|
225 }
|
Chris@16
|
226
|
Chris@16
|
227 template <typename Function, typename Dispatcher,
|
Chris@16
|
228 typename Handler, typename IsContinuation>
|
Chris@16
|
229 inline void asio_handler_invoke(Function& function,
|
Chris@16
|
230 wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
|
Chris@16
|
231 {
|
Chris@16
|
232 this_handler->dispatcher_.dispatch(
|
Chris@16
|
233 rewrapped_handler<Function, Handler>(
|
Chris@16
|
234 function, this_handler->handler_));
|
Chris@16
|
235 }
|
Chris@16
|
236
|
Chris@16
|
237 template <typename Function, typename Dispatcher,
|
Chris@16
|
238 typename Handler, typename IsContinuation>
|
Chris@16
|
239 inline void asio_handler_invoke(const Function& function,
|
Chris@16
|
240 wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
|
Chris@16
|
241 {
|
Chris@16
|
242 this_handler->dispatcher_.dispatch(
|
Chris@16
|
243 rewrapped_handler<Function, Handler>(
|
Chris@16
|
244 function, this_handler->handler_));
|
Chris@16
|
245 }
|
Chris@16
|
246
|
Chris@16
|
247 template <typename Handler, typename Context>
|
Chris@16
|
248 inline void* asio_handler_allocate(std::size_t size,
|
Chris@16
|
249 rewrapped_handler<Handler, Context>* this_handler)
|
Chris@16
|
250 {
|
Chris@16
|
251 return boost_asio_handler_alloc_helpers::allocate(
|
Chris@16
|
252 size, this_handler->context_);
|
Chris@16
|
253 }
|
Chris@16
|
254
|
Chris@16
|
255 template <typename Handler, typename Context>
|
Chris@16
|
256 inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
Chris@16
|
257 rewrapped_handler<Handler, Context>* this_handler)
|
Chris@16
|
258 {
|
Chris@16
|
259 boost_asio_handler_alloc_helpers::deallocate(
|
Chris@16
|
260 pointer, size, this_handler->context_);
|
Chris@16
|
261 }
|
Chris@16
|
262
|
Chris@16
|
263 template <typename Dispatcher, typename Context>
|
Chris@16
|
264 inline bool asio_handler_is_continuation(
|
Chris@16
|
265 rewrapped_handler<Dispatcher, Context>* this_handler)
|
Chris@16
|
266 {
|
Chris@16
|
267 return boost_asio_handler_cont_helpers::is_continuation(
|
Chris@101
|
268 this_handler->context_);
|
Chris@16
|
269 }
|
Chris@16
|
270
|
Chris@16
|
271 template <typename Function, typename Handler, typename Context>
|
Chris@16
|
272 inline void asio_handler_invoke(Function& function,
|
Chris@16
|
273 rewrapped_handler<Handler, Context>* this_handler)
|
Chris@16
|
274 {
|
Chris@16
|
275 boost_asio_handler_invoke_helpers::invoke(
|
Chris@16
|
276 function, this_handler->context_);
|
Chris@16
|
277 }
|
Chris@16
|
278
|
Chris@16
|
279 template <typename Function, typename Handler, typename Context>
|
Chris@16
|
280 inline void asio_handler_invoke(const Function& function,
|
Chris@16
|
281 rewrapped_handler<Handler, Context>* this_handler)
|
Chris@16
|
282 {
|
Chris@16
|
283 boost_asio_handler_invoke_helpers::invoke(
|
Chris@16
|
284 function, this_handler->context_);
|
Chris@16
|
285 }
|
Chris@16
|
286
|
Chris@16
|
287 } // namespace detail
|
Chris@16
|
288 } // namespace asio
|
Chris@16
|
289 } // namespace boost
|
Chris@16
|
290
|
Chris@16
|
291 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
292
|
Chris@16
|
293 #endif // BOOST_ASIO_DETAIL_WRAPPED_HANDLER_HPP
|