]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/wrapped_handler.hpp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
b32b8144 | 5 | // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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> | |
32 | 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> | |
41 | 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 | ||
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) | |
61 | wrapped_handler(const wrapped_handler& other) | |
62 | : dispatcher_(other.dispatcher_), | |
63 | handler_(other.handler_) | |
64 | { | |
65 | } | |
66 | ||
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 | ||
74 | void operator()() | |
75 | { | |
76 | dispatcher_.dispatch(BOOST_ASIO_MOVE_CAST(Handler)(handler_)); | |
77 | } | |
78 | ||
79 | void operator()() const | |
80 | { | |
81 | dispatcher_.dispatch(handler_); | |
82 | } | |
83 | ||
84 | template <typename Arg1> | |
85 | void operator()(const Arg1& arg1) | |
86 | { | |
87 | dispatcher_.dispatch(detail::bind_handler(handler_, arg1)); | |
88 | } | |
89 | ||
90 | template <typename Arg1> | |
91 | void operator()(const Arg1& arg1) const | |
92 | { | |
93 | dispatcher_.dispatch(detail::bind_handler(handler_, arg1)); | |
94 | } | |
95 | ||
96 | template <typename Arg1, typename 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> | |
103 | 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> | |
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> | |
115 | 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> | |
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> | |
129 | 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> | |
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> | |
147 | 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: | |
163 | explicit rewrapped_handler(Handler& handler, const Context& context) | |
164 | : context_(context), | |
165 | handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)) | |
166 | { | |
167 | } | |
168 | ||
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) | |
176 | rewrapped_handler(const rewrapped_handler& other) | |
177 | : context_(other.context_), | |
178 | handler_(other.handler_) | |
179 | { | |
180 | } | |
181 | ||
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 | ||
189 | void operator()() | |
190 | { | |
191 | handler_(); | |
192 | } | |
193 | ||
194 | 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 void* asio_handler_allocate(std::size_t size, | |
206 | wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler) | |
207 | { | |
208 | return boost_asio_handler_alloc_helpers::allocate( | |
209 | size, this_handler->handler_); | |
210 | } | |
211 | ||
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) | |
215 | { | |
216 | boost_asio_handler_alloc_helpers::deallocate( | |
217 | pointer, size, this_handler->handler_); | |
218 | } | |
219 | ||
220 | template <typename Dispatcher, typename Handler, typename IsContinuation> | |
221 | inline bool asio_handler_is_continuation( | |
222 | wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler) | |
223 | { | |
224 | return IsContinuation()(this_handler->dispatcher_, this_handler->handler_); | |
225 | } | |
226 | ||
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) | |
231 | { | |
232 | this_handler->dispatcher_.dispatch( | |
233 | rewrapped_handler<Function, Handler>( | |
234 | function, this_handler->handler_)); | |
235 | } | |
236 | ||
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) | |
241 | { | |
242 | this_handler->dispatcher_.dispatch( | |
243 | rewrapped_handler<Function, Handler>( | |
244 | function, this_handler->handler_)); | |
245 | } | |
246 | ||
247 | template <typename Handler, typename Context> | |
248 | inline void* asio_handler_allocate(std::size_t size, | |
249 | rewrapped_handler<Handler, Context>* this_handler) | |
250 | { | |
251 | return boost_asio_handler_alloc_helpers::allocate( | |
252 | size, this_handler->context_); | |
253 | } | |
254 | ||
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) | |
258 | { | |
259 | boost_asio_handler_alloc_helpers::deallocate( | |
260 | pointer, size, this_handler->context_); | |
261 | } | |
262 | ||
263 | template <typename Dispatcher, typename Context> | |
264 | inline bool asio_handler_is_continuation( | |
265 | rewrapped_handler<Dispatcher, Context>* this_handler) | |
266 | { | |
267 | return boost_asio_handler_cont_helpers::is_continuation( | |
268 | this_handler->context_); | |
269 | } | |
270 | ||
271 | template <typename Function, typename Handler, typename Context> | |
272 | inline void asio_handler_invoke(Function& function, | |
273 | rewrapped_handler<Handler, Context>* this_handler) | |
274 | { | |
275 | boost_asio_handler_invoke_helpers::invoke( | |
276 | function, this_handler->context_); | |
277 | } | |
278 | ||
279 | template <typename Function, typename Handler, typename Context> | |
280 | inline void asio_handler_invoke(const Function& function, | |
281 | rewrapped_handler<Handler, Context>* this_handler) | |
282 | { | |
283 | boost_asio_handler_invoke_helpers::invoke( | |
284 | function, this_handler->context_); | |
285 | } | |
286 | ||
287 | } // namespace detail | |
288 | } // namespace asio | |
289 | } // namespace boost | |
290 | ||
291 | #include <boost/asio/detail/pop_options.hpp> | |
292 | ||
293 | #endif // BOOST_ASIO_DETAIL_WRAPPED_HANDLER_HPP |