prepend.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 BOOST_ASIO_IMPL_PREPEND_HPP
  11. #define BOOST_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 <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 a prepend_t as a completion handler.
  26. template <typename Handler, typename... Values>
  27. class prepend_handler
  28. {
  29. public:
  30. typedef void result_type;
  31. template <typename H>
  32. prepend_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<Values&&>(std::get<I>(values_))...,
  49. static_cast<Args&&>(args)...);
  50. }
  51. //private:
  52. Handler handler_;
  53. std::tuple<Values...> values_;
  54. };
  55. template <typename Handler>
  56. inline bool asio_handler_is_continuation(
  57. prepend_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 prepend_signature;
  64. template <typename R, typename... Args, typename... Values>
  65. struct prepend_signature<R(Args...), Values...>
  66. {
  67. typedef R type(Values..., decay_t<Args>...);
  68. };
  69. } // namespace detail
  70. #if !defined(GENERATING_DOCUMENTATION)
  71. template <typename CompletionToken, typename... Values, typename Signature>
  72. struct async_result<
  73. prepend_t<CompletionToken, Values...>, Signature>
  74. : async_result<CompletionToken,
  75. typename detail::prepend_signature<
  76. Signature, Values...>::type>
  77. {
  78. typedef typename detail::prepend_signature<
  79. Signature, Values...>::type signature;
  80. template <typename Initiation>
  81. struct init_wrapper
  82. {
  83. init_wrapper(Initiation init)
  84. : initiation_(static_cast<Initiation&&>(init))
  85. {
  86. }
  87. template <typename Handler, typename... Args>
  88. void operator()(Handler&& handler,
  89. std::tuple<Values...> values, Args&&... args)
  90. {
  91. static_cast<Initiation&&>(initiation_)(
  92. detail::prepend_handler<decay_t<Handler>, Values...>(
  93. static_cast<Handler&&>(handler),
  94. static_cast<std::tuple<Values...>&&>(values)),
  95. static_cast<Args&&>(args)...);
  96. }
  97. Initiation initiation_;
  98. };
  99. template <typename Initiation, typename RawCompletionToken, typename... Args>
  100. static auto initiate(Initiation&& initiation,
  101. RawCompletionToken&& token, Args&&... args)
  102. -> decltype(
  103. async_initiate<CompletionToken, signature>(
  104. declval<init_wrapper<decay_t<Initiation>>>(),
  105. token.token_,
  106. static_cast<std::tuple<Values...>&&>(token.values_),
  107. static_cast<Args&&>(args)...))
  108. {
  109. return async_initiate<CompletionToken, signature>(
  110. init_wrapper<decay_t<Initiation>>(
  111. static_cast<Initiation&&>(initiation)),
  112. token.token_,
  113. static_cast<std::tuple<Values...>&&>(token.values_),
  114. static_cast<Args&&>(args)...);
  115. }
  116. };
  117. template <template <typename, typename> class Associator,
  118. typename Handler, typename... Values, typename DefaultCandidate>
  119. struct associator<Associator,
  120. detail::prepend_handler<Handler, Values...>, DefaultCandidate>
  121. : Associator<Handler, DefaultCandidate>
  122. {
  123. static typename Associator<Handler, DefaultCandidate>::type get(
  124. const detail::prepend_handler<Handler, Values...>& h) noexcept
  125. {
  126. return Associator<Handler, DefaultCandidate>::get(h.handler_);
  127. }
  128. static auto get(const detail::prepend_handler<Handler, Values...>& h,
  129. const DefaultCandidate& c) noexcept
  130. -> decltype(Associator<Handler, DefaultCandidate>::get(h.handler_, c))
  131. {
  132. return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
  133. }
  134. };
  135. #endif // !defined(GENERATING_DOCUMENTATION)
  136. } // namespace asio
  137. } // namespace boost
  138. #include <boost/asio/detail/pop_options.hpp>
  139. #endif // BOOST_ASIO_IMPL_PREPEND_HPP