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