wrapped_handler.hpp 9.3 KB

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