consign.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // impl/consign.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_IMPL_CONSIGN_HPP
  11. #define BOOST_ASIO_IMPL_CONSIGN_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/config.hpp>
  16. #include <boost/asio/associator.hpp>
  17. #include <boost/asio/async_result.hpp>
  18. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  19. #include <boost/asio/detail/handler_cont_helpers.hpp>
  20. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  21. #include <boost/asio/detail/type_traits.hpp>
  22. #include <boost/asio/detail/utility.hpp>
  23. #include <boost/asio/detail/variadic_templates.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. // Class to adapt a consign_t as a completion handler.
  29. template <typename Handler, typename... Values>
  30. class consign_handler
  31. {
  32. public:
  33. typedef void result_type;
  34. template <typename H>
  35. consign_handler(BOOST_ASIO_MOVE_ARG(H) handler, std::tuple<Values...> values)
  36. : handler_(BOOST_ASIO_MOVE_CAST(H)(handler)),
  37. values_(BOOST_ASIO_MOVE_CAST(std::tuple<Values...>)(values))
  38. {
  39. }
  40. template <typename... Args>
  41. void operator()(BOOST_ASIO_MOVE_ARG(Args)... args)
  42. {
  43. BOOST_ASIO_MOVE_OR_LVALUE(Handler)(handler_)(
  44. BOOST_ASIO_MOVE_CAST(Args)(args)...);
  45. }
  46. //private:
  47. Handler handler_;
  48. std::tuple<Values...> values_;
  49. };
  50. template <typename Handler>
  51. inline asio_handler_allocate_is_deprecated
  52. asio_handler_allocate(std::size_t size,
  53. consign_handler<Handler>* this_handler)
  54. {
  55. #if defined(BOOST_ASIO_NO_DEPRECATED)
  56. boost_asio_handler_alloc_helpers::allocate(size, this_handler->handler_);
  57. return asio_handler_allocate_is_no_longer_used();
  58. #else // defined(BOOST_ASIO_NO_DEPRECATED)
  59. return boost_asio_handler_alloc_helpers::allocate(
  60. size, this_handler->handler_);
  61. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  62. }
  63. template <typename Handler>
  64. inline asio_handler_deallocate_is_deprecated
  65. asio_handler_deallocate(void* pointer, std::size_t size,
  66. consign_handler<Handler>* this_handler)
  67. {
  68. boost_asio_handler_alloc_helpers::deallocate(
  69. pointer, size, this_handler->handler_);
  70. #if defined(BOOST_ASIO_NO_DEPRECATED)
  71. return asio_handler_deallocate_is_no_longer_used();
  72. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  73. }
  74. template <typename Handler>
  75. inline bool asio_handler_is_continuation(
  76. consign_handler<Handler>* this_handler)
  77. {
  78. return boost_asio_handler_cont_helpers::is_continuation(
  79. this_handler->handler_);
  80. }
  81. template <typename Function, typename Handler>
  82. inline asio_handler_invoke_is_deprecated
  83. asio_handler_invoke(Function& function,
  84. consign_handler<Handler>* this_handler)
  85. {
  86. boost_asio_handler_invoke_helpers::invoke(
  87. function, this_handler->handler_);
  88. #if defined(BOOST_ASIO_NO_DEPRECATED)
  89. return asio_handler_invoke_is_no_longer_used();
  90. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  91. }
  92. template <typename Function, typename Handler>
  93. inline asio_handler_invoke_is_deprecated
  94. asio_handler_invoke(const Function& function,
  95. consign_handler<Handler>* this_handler)
  96. {
  97. boost_asio_handler_invoke_helpers::invoke(
  98. function, this_handler->handler_);
  99. #if defined(BOOST_ASIO_NO_DEPRECATED)
  100. return asio_handler_invoke_is_no_longer_used();
  101. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  102. }
  103. } // namespace detail
  104. #if !defined(GENERATING_DOCUMENTATION)
  105. template <typename CompletionToken, typename... Values, typename... Signatures>
  106. struct async_result<
  107. consign_t<CompletionToken, Values...>, Signatures...>
  108. : async_result<CompletionToken, Signatures...>
  109. {
  110. template <typename Initiation>
  111. struct init_wrapper
  112. {
  113. init_wrapper(Initiation init)
  114. : initiation_(BOOST_ASIO_MOVE_CAST(Initiation)(init))
  115. {
  116. }
  117. template <typename Handler, typename... Args>
  118. void operator()(
  119. BOOST_ASIO_MOVE_ARG(Handler) handler,
  120. std::tuple<Values...> values,
  121. BOOST_ASIO_MOVE_ARG(Args)... args)
  122. {
  123. BOOST_ASIO_MOVE_CAST(Initiation)(initiation_)(
  124. detail::consign_handler<
  125. typename decay<Handler>::type, Values...>(
  126. BOOST_ASIO_MOVE_CAST(Handler)(handler),
  127. BOOST_ASIO_MOVE_CAST(std::tuple<Values...>)(values)),
  128. BOOST_ASIO_MOVE_CAST(Args)(args)...);
  129. }
  130. Initiation initiation_;
  131. };
  132. template <typename Initiation, typename RawCompletionToken, typename... Args>
  133. static BOOST_ASIO_INITFN_DEDUCED_RESULT_TYPE(CompletionToken, Signatures...,
  134. (async_initiate<CompletionToken, Signatures...>(
  135. declval<init_wrapper<typename decay<Initiation>::type> >(),
  136. declval<CompletionToken&>(),
  137. declval<std::tuple<Values...> >(),
  138. declval<BOOST_ASIO_MOVE_ARG(Args)>()...)))
  139. initiate(
  140. BOOST_ASIO_MOVE_ARG(Initiation) initiation,
  141. BOOST_ASIO_MOVE_ARG(RawCompletionToken) token,
  142. BOOST_ASIO_MOVE_ARG(Args)... args)
  143. {
  144. return async_initiate<CompletionToken, Signatures...>(
  145. init_wrapper<typename decay<Initiation>::type>(
  146. BOOST_ASIO_MOVE_CAST(Initiation)(initiation)),
  147. token.token_,
  148. BOOST_ASIO_MOVE_CAST(std::tuple<Values...>)(token.values_),
  149. BOOST_ASIO_MOVE_CAST(Args)(args)...);
  150. }
  151. };
  152. template <template <typename, typename> class Associator,
  153. typename Handler, typename... Values, typename DefaultCandidate>
  154. struct associator<Associator,
  155. detail::consign_handler<Handler, Values...>, DefaultCandidate>
  156. : Associator<Handler, DefaultCandidate>
  157. {
  158. static typename Associator<Handler, DefaultCandidate>::type
  159. get(const detail::consign_handler<Handler, Values...>& h) BOOST_ASIO_NOEXCEPT
  160. {
  161. return Associator<Handler, DefaultCandidate>::get(h.handler_);
  162. }
  163. static BOOST_ASIO_AUTO_RETURN_TYPE_PREFIX2(
  164. typename Associator<Handler, DefaultCandidate>::type)
  165. get(const detail::consign_handler<Handler, Values...>& h,
  166. const DefaultCandidate& c) BOOST_ASIO_NOEXCEPT
  167. BOOST_ASIO_AUTO_RETURN_TYPE_SUFFIX((
  168. Associator<Handler, DefaultCandidate>::get(h.handler_, c)))
  169. {
  170. return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
  171. }
  172. };
  173. #endif // !defined(GENERATING_DOCUMENTATION)
  174. } // namespace asio
  175. } // namespace boost
  176. #include <boost/asio/detail/pop_options.hpp>
  177. #endif // BOOST_ASIO_IMPL_CONSIGN_HPP