wrapped_handler.hpp 5.4 KB

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