append.hpp 4.5 KB

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