wrapped_handler.hpp 5.6 KB

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