append.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // impl/append.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_IMPL_APPEND_HPP
  11. #define BOOST_ASIO_IMPL_APPEND_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_cont_helpers.hpp>
  19. #include <boost/asio/detail/type_traits.hpp>
  20. #include <boost/asio/detail/utility.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. // Class to adapt an append_t as a completion handler.
  26. template <typename Handler, typename... Values>
  27. class append_handler
  28. {
  29. public:
  30. typedef void result_type;
  31. template <typename H>
  32. append_handler(H&& handler, std::tuple<Values...> values)
  33. : handler_(static_cast<H&&>(handler)),
  34. values_(static_cast<std::tuple<Values...>&&>(values))
  35. {
  36. }
  37. template <typename... Args>
  38. void operator()(Args&&... args)
  39. {
  40. this->invoke(
  41. index_sequence_for<Values...>{},
  42. static_cast<Args&&>(args)...);
  43. }
  44. template <std::size_t... I, typename... Args>
  45. void invoke(index_sequence<I...>, Args&&... args)
  46. {
  47. static_cast<Handler&&>(handler_)(
  48. static_cast<Args&&>(args)...,
  49. static_cast<Values&&>(std::get<I>(values_))...);
  50. }
  51. //private:
  52. Handler handler_;
  53. std::tuple<Values...> values_;
  54. };
  55. template <typename Handler>
  56. inline bool asio_handler_is_continuation(
  57. append_handler<Handler>* this_handler)
  58. {
  59. return boost_asio_handler_cont_helpers::is_continuation(
  60. this_handler->handler_);
  61. }
  62. template <typename Signature, typename... Values>
  63. struct append_signature;
  64. template <typename R, typename... Args, typename... Values>
  65. struct append_signature<R(Args...), Values...>
  66. {
  67. typedef R type(decay_t<Args>..., Values...);
  68. };
  69. } // namespace detail
  70. #if !defined(GENERATING_DOCUMENTATION)
  71. template <typename CompletionToken, typename... Values, typename Signature>
  72. struct async_result<append_t<CompletionToken, Values...>, Signature>
  73. : async_result<CompletionToken,
  74. typename detail::append_signature<
  75. Signature, Values...>::type>
  76. {
  77. typedef typename detail::append_signature<
  78. Signature, Values...>::type signature;
  79. template <typename Initiation>
  80. struct init_wrapper
  81. {
  82. init_wrapper(Initiation init)
  83. : initiation_(static_cast<Initiation&&>(init))
  84. {
  85. }
  86. template <typename Handler, typename... Args>
  87. void operator()(Handler&& handler,
  88. std::tuple<Values...> values, Args&&... args)
  89. {
  90. static_cast<Initiation&&>(initiation_)(
  91. detail::append_handler<decay_t<Handler>, Values...>(
  92. static_cast<Handler&&>(handler),
  93. static_cast<std::tuple<Values...>&&>(values)),
  94. static_cast<Args&&>(args)...);
  95. }
  96. Initiation initiation_;
  97. };
  98. template <typename Initiation, typename RawCompletionToken, typename... Args>
  99. static auto initiate(Initiation&& initiation,
  100. RawCompletionToken&& token, Args&&... args)
  101. -> decltype(
  102. async_initiate<CompletionToken, signature>(
  103. declval<init_wrapper<decay_t<Initiation>>>(),
  104. token.token_,
  105. static_cast<std::tuple<Values...>&&>(token.values_),
  106. static_cast<Args&&>(args)...))
  107. {
  108. return async_initiate<CompletionToken, signature>(
  109. init_wrapper<decay_t<Initiation>>(
  110. static_cast<Initiation&&>(initiation)),
  111. token.token_,
  112. static_cast<std::tuple<Values...>&&>(token.values_),
  113. static_cast<Args&&>(args)...);
  114. }
  115. };
  116. template <template <typename, typename> class Associator,
  117. typename Handler, typename... Values, typename DefaultCandidate>
  118. struct associator<Associator,
  119. detail::append_handler<Handler, Values...>, DefaultCandidate>
  120. : Associator<Handler, DefaultCandidate>
  121. {
  122. static typename Associator<Handler, DefaultCandidate>::type get(
  123. const detail::append_handler<Handler, Values...>& h) noexcept
  124. {
  125. return Associator<Handler, DefaultCandidate>::get(h.handler_);
  126. }
  127. static auto get(const detail::append_handler<Handler, Values...>& h,
  128. const DefaultCandidate& c) noexcept
  129. -> decltype(Associator<Handler, DefaultCandidate>::get(h.handler_, c))
  130. {
  131. return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
  132. }
  133. };
  134. #endif // !defined(GENERATING_DOCUMENTATION)
  135. } // namespace asio
  136. } // namespace boost
  137. #include <boost/asio/detail/pop_options.hpp>
  138. #endif // BOOST_ASIO_IMPL_APPEND_HPP