wrapped_handler.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // detail/wrapped_handler.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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 BOOST_ASIO_DETAIL_WRAPPED_HANDLER_HPP
  11. #define BOOST_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 <boost/asio/detail/bind_handler.hpp>
  16. #include <boost/asio/detail/handler_cont_helpers.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. namespace detail {
  21. struct is_continuation_delegated
  22. {
  23. template <typename Dispatcher, typename Handler>
  24. bool operator()(Dispatcher&, Handler& handler) const
  25. {
  26. return boost_asio_handler_cont_helpers::is_continuation(handler);
  27. }
  28. };
  29. struct is_continuation_if_running
  30. {
  31. template <typename Dispatcher, typename Handler>
  32. bool operator()(Dispatcher& dispatcher, Handler&) const
  33. {
  34. return dispatcher.running_in_this_thread();
  35. }
  36. };
  37. template <typename Dispatcher, typename Handler,
  38. typename IsContinuation = is_continuation_delegated>
  39. class wrapped_handler
  40. {
  41. public:
  42. typedef void result_type;
  43. wrapped_handler(Dispatcher dispatcher, Handler& handler)
  44. : dispatcher_(dispatcher),
  45. handler_(static_cast<Handler&&>(handler))
  46. {
  47. }
  48. wrapped_handler(const wrapped_handler& other)
  49. : dispatcher_(other.dispatcher_),
  50. handler_(other.handler_)
  51. {
  52. }
  53. wrapped_handler(wrapped_handler&& other)
  54. : dispatcher_(other.dispatcher_),
  55. handler_(static_cast<Handler&&>(other.handler_))
  56. {
  57. }
  58. void operator()()
  59. {
  60. dispatcher_.dispatch(static_cast<Handler&&>(handler_));
  61. }
  62. void operator()() const
  63. {
  64. dispatcher_.dispatch(handler_);
  65. }
  66. template <typename Arg1>
  67. void operator()(const Arg1& arg1)
  68. {
  69. dispatcher_.dispatch(detail::bind_handler(handler_, arg1));
  70. }
  71. template <typename Arg1>
  72. void operator()(const Arg1& arg1) const
  73. {
  74. dispatcher_.dispatch(detail::bind_handler(handler_, arg1));
  75. }
  76. template <typename Arg1, typename Arg2>
  77. void operator()(const Arg1& arg1, const Arg2& arg2)
  78. {
  79. dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2));
  80. }
  81. template <typename Arg1, typename Arg2>
  82. void operator()(const Arg1& arg1, const Arg2& arg2) const
  83. {
  84. dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2));
  85. }
  86. template <typename Arg1, typename Arg2, typename Arg3>
  87. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3)
  88. {
  89. dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3));
  90. }
  91. template <typename Arg1, typename Arg2, typename Arg3>
  92. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) const
  93. {
  94. dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3));
  95. }
  96. template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
  97. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
  98. const Arg4& arg4)
  99. {
  100. dispatcher_.dispatch(
  101. detail::bind_handler(handler_, arg1, arg2, arg3, arg4));
  102. }
  103. template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
  104. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
  105. const Arg4& arg4) const
  106. {
  107. dispatcher_.dispatch(
  108. detail::bind_handler(handler_, arg1, arg2, arg3, arg4));
  109. }
  110. template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
  111. typename Arg5>
  112. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
  113. const Arg4& arg4, const Arg5& arg5)
  114. {
  115. dispatcher_.dispatch(
  116. detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5));
  117. }
  118. template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
  119. typename Arg5>
  120. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
  121. const Arg4& arg4, const Arg5& arg5) const
  122. {
  123. dispatcher_.dispatch(
  124. detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5));
  125. }
  126. //private:
  127. Dispatcher dispatcher_;
  128. Handler handler_;
  129. };
  130. template <typename Handler, typename Context>
  131. class rewrapped_handler
  132. {
  133. public:
  134. explicit rewrapped_handler(Handler& handler, const Context& context)
  135. : context_(context),
  136. handler_(static_cast<Handler&&>(handler))
  137. {
  138. }
  139. explicit rewrapped_handler(const Handler& handler, const Context& context)
  140. : context_(context),
  141. handler_(handler)
  142. {
  143. }
  144. rewrapped_handler(const rewrapped_handler& other)
  145. : context_(other.context_),
  146. handler_(other.handler_)
  147. {
  148. }
  149. rewrapped_handler(rewrapped_handler&& other)
  150. : context_(static_cast<Context&&>(other.context_)),
  151. handler_(static_cast<Handler&&>(other.handler_))
  152. {
  153. }
  154. void operator()()
  155. {
  156. handler_();
  157. }
  158. void operator()() const
  159. {
  160. handler_();
  161. }
  162. //private:
  163. Context context_;
  164. Handler handler_;
  165. };
  166. template <typename Dispatcher, typename Handler, typename IsContinuation>
  167. inline bool asio_handler_is_continuation(
  168. wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
  169. {
  170. return IsContinuation()(this_handler->dispatcher_, this_handler->handler_);
  171. }
  172. template <typename Dispatcher, typename Context>
  173. inline bool asio_handler_is_continuation(
  174. rewrapped_handler<Dispatcher, Context>* this_handler)
  175. {
  176. return boost_asio_handler_cont_helpers::is_continuation(
  177. this_handler->context_);
  178. }
  179. } // namespace detail
  180. } // namespace asio
  181. } // namespace boost
  182. #include <boost/asio/detail/pop_options.hpp>
  183. #endif // BOOST_ASIO_DETAIL_WRAPPED_HANDLER_HPP