wrapped_handler.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // detail/wrapped_handler.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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 = void>
  38. struct wrapped_executor
  39. {
  40. typedef Dispatcher executor_type;
  41. static const Dispatcher& get(const Dispatcher& dispatcher) noexcept
  42. {
  43. return dispatcher;
  44. }
  45. };
  46. template <typename Dispatcher>
  47. struct wrapped_executor<Dispatcher,
  48. void_type<typename Dispatcher::executor_type>>
  49. {
  50. typedef typename Dispatcher::executor_type executor_type;
  51. static executor_type get(const Dispatcher& dispatcher) noexcept
  52. {
  53. return dispatcher.get_executor();
  54. }
  55. };
  56. template <typename Dispatcher, typename Handler,
  57. typename IsContinuation = is_continuation_delegated>
  58. class wrapped_handler
  59. {
  60. public:
  61. typedef void result_type;
  62. typedef typename wrapped_executor<Dispatcher>::executor_type executor_type;
  63. wrapped_handler(Dispatcher dispatcher, Handler& handler)
  64. : dispatcher_(dispatcher),
  65. handler_(static_cast<Handler&&>(handler))
  66. {
  67. }
  68. wrapped_handler(const wrapped_handler& other)
  69. : dispatcher_(other.dispatcher_),
  70. handler_(other.handler_)
  71. {
  72. }
  73. wrapped_handler(wrapped_handler&& other)
  74. : dispatcher_(other.dispatcher_),
  75. handler_(static_cast<Handler&&>(other.handler_))
  76. {
  77. }
  78. executor_type get_executor() const noexcept
  79. {
  80. return wrapped_executor<Dispatcher>::get(dispatcher_);
  81. }
  82. void operator()()
  83. {
  84. dispatcher_.dispatch(static_cast<Handler&&>(handler_));
  85. }
  86. void operator()() const
  87. {
  88. dispatcher_.dispatch(handler_);
  89. }
  90. template <typename Arg1>
  91. void operator()(const Arg1& arg1)
  92. {
  93. dispatcher_.dispatch(detail::bind_handler(handler_, arg1));
  94. }
  95. template <typename Arg1>
  96. void operator()(const Arg1& arg1) const
  97. {
  98. dispatcher_.dispatch(detail::bind_handler(handler_, arg1));
  99. }
  100. template <typename Arg1, typename Arg2>
  101. void operator()(const Arg1& arg1, const Arg2& arg2)
  102. {
  103. dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2));
  104. }
  105. template <typename Arg1, typename Arg2>
  106. void operator()(const Arg1& arg1, const Arg2& arg2) const
  107. {
  108. dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2));
  109. }
  110. template <typename Arg1, typename Arg2, typename Arg3>
  111. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3)
  112. {
  113. dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3));
  114. }
  115. template <typename Arg1, typename Arg2, typename Arg3>
  116. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) const
  117. {
  118. dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3));
  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. template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
  128. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
  129. const Arg4& arg4) const
  130. {
  131. dispatcher_.dispatch(
  132. detail::bind_handler(handler_, arg1, arg2, arg3, arg4));
  133. }
  134. template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
  135. typename Arg5>
  136. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
  137. const Arg4& arg4, const Arg5& arg5)
  138. {
  139. dispatcher_.dispatch(
  140. detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5));
  141. }
  142. template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
  143. typename Arg5>
  144. void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3,
  145. const Arg4& arg4, const Arg5& arg5) const
  146. {
  147. dispatcher_.dispatch(
  148. detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5));
  149. }
  150. //private:
  151. Dispatcher dispatcher_;
  152. Handler handler_;
  153. };
  154. template <typename Dispatcher, typename Handler, typename IsContinuation>
  155. inline bool asio_handler_is_continuation(
  156. wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler)
  157. {
  158. return IsContinuation()(this_handler->dispatcher_, this_handler->handler_);
  159. }
  160. } // namespace detail
  161. } // namespace asio
  162. } // namespace boost
  163. #include <boost/asio/detail/pop_options.hpp>
  164. #endif // BOOST_ASIO_DETAIL_WRAPPED_HANDLER_HPP