prepend.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // impl/prepend.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_PREPEND_HPP
  11. #define ASIO_IMPL_PREPEND_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 a prepend_t as a completion handler.
  25. template <typename Handler, typename... Values>
  26. class prepend_handler
  27. {
  28. public:
  29. typedef void result_type;
  30. template <typename H>
  31. prepend_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<Values&&>(std::get<I>(values_))...,
  48. static_cast<Args&&>(args)...);
  49. }
  50. //private:
  51. Handler handler_;
  52. std::tuple<Values...> values_;
  53. };
  54. template <typename Handler>
  55. inline bool asio_handler_is_continuation(
  56. prepend_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 prepend_signature;
  63. template <typename R, typename... Args, typename... Values>
  64. struct prepend_signature<R(Args...), Values...>
  65. {
  66. typedef R type(Values..., decay_t<Args>...);
  67. };
  68. } // namespace detail
  69. #if !defined(GENERATING_DOCUMENTATION)
  70. template <typename CompletionToken, typename... Values, typename Signature>
  71. struct async_result<
  72. prepend_t<CompletionToken, Values...>, Signature>
  73. : async_result<CompletionToken,
  74. typename detail::prepend_signature<
  75. Signature, Values...>::type>
  76. {
  77. typedef typename detail::prepend_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::prepend_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::prepend_handler<Handler, Values...>, DefaultCandidate>
  120. : Associator<Handler, DefaultCandidate>
  121. {
  122. static typename Associator<Handler, DefaultCandidate>::type get(
  123. const detail::prepend_handler<Handler, Values...>& h) noexcept
  124. {
  125. return Associator<Handler, DefaultCandidate>::get(h.handler_);
  126. }
  127. static auto get(const detail::prepend_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. #include "asio/detail/pop_options.hpp"
  137. #endif // ASIO_IMPL_PREPEND_HPP