as_tuple.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // impl/as_tuple.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_AS_TUPLE_HPP
  11. #define ASIO_IMPL_AS_TUPLE_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 <tuple>
  17. #include "asio/associator.hpp"
  18. #include "asio/async_result.hpp"
  19. #include "asio/detail/handler_cont_helpers.hpp"
  20. #include "asio/detail/type_traits.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace detail {
  24. // Class to adapt a as_tuple_t as a completion handler.
  25. template <typename Handler>
  26. class as_tuple_handler
  27. {
  28. public:
  29. typedef void result_type;
  30. template <typename CompletionToken>
  31. as_tuple_handler(as_tuple_t<CompletionToken> e)
  32. : handler_(static_cast<CompletionToken&&>(e.token_))
  33. {
  34. }
  35. template <typename RedirectedHandler>
  36. as_tuple_handler(RedirectedHandler&& h)
  37. : handler_(static_cast<RedirectedHandler&&>(h))
  38. {
  39. }
  40. template <typename... Args>
  41. void operator()(Args&&... args)
  42. {
  43. static_cast<Handler&&>(handler_)(
  44. std::make_tuple(static_cast<Args&&>(args)...));
  45. }
  46. //private:
  47. Handler handler_;
  48. };
  49. template <typename Handler>
  50. inline bool asio_handler_is_continuation(
  51. as_tuple_handler<Handler>* this_handler)
  52. {
  53. return asio_handler_cont_helpers::is_continuation(
  54. this_handler->handler_);
  55. }
  56. template <typename Signature>
  57. struct as_tuple_signature;
  58. template <typename R, typename... Args>
  59. struct as_tuple_signature<R(Args...)>
  60. {
  61. typedef R type(std::tuple<decay_t<Args>...>);
  62. };
  63. template <typename R, typename... Args>
  64. struct as_tuple_signature<R(Args...) &>
  65. {
  66. typedef R type(std::tuple<decay_t<Args>...>) &;
  67. };
  68. template <typename R, typename... Args>
  69. struct as_tuple_signature<R(Args...) &&>
  70. {
  71. typedef R type(std::tuple<decay_t<Args>...>) &&;
  72. };
  73. #if defined(ASIO_HAS_NOEXCEPT_FUNCTION_TYPE)
  74. template <typename R, typename... Args>
  75. struct as_tuple_signature<R(Args...) noexcept>
  76. {
  77. typedef R type(std::tuple<decay_t<Args>...>) noexcept;
  78. };
  79. template <typename R, typename... Args>
  80. struct as_tuple_signature<R(Args...) & noexcept>
  81. {
  82. typedef R type(std::tuple<decay_t<Args>...>) & noexcept;
  83. };
  84. template <typename R, typename... Args>
  85. struct as_tuple_signature<R(Args...) && noexcept>
  86. {
  87. typedef R type(std::tuple<decay_t<Args>...>) && noexcept;
  88. };
  89. #endif // defined(ASIO_HAS_NOEXCEPT_FUNCTION_TYPE)
  90. } // namespace detail
  91. #if !defined(GENERATING_DOCUMENTATION)
  92. template <typename CompletionToken, typename... Signatures>
  93. struct async_result<as_tuple_t<CompletionToken>, Signatures...>
  94. : async_result<CompletionToken,
  95. typename detail::as_tuple_signature<Signatures>::type...>
  96. {
  97. template <typename Initiation>
  98. struct init_wrapper
  99. {
  100. init_wrapper(Initiation init)
  101. : initiation_(static_cast<Initiation&&>(init))
  102. {
  103. }
  104. template <typename Handler, typename... Args>
  105. void operator()(Handler&& handler, Args&&... args)
  106. {
  107. static_cast<Initiation&&>(initiation_)(
  108. detail::as_tuple_handler<decay_t<Handler>>(
  109. static_cast<Handler&&>(handler)),
  110. static_cast<Args&&>(args)...);
  111. }
  112. Initiation initiation_;
  113. };
  114. template <typename Initiation, typename RawCompletionToken, typename... Args>
  115. static auto initiate(Initiation&& initiation,
  116. RawCompletionToken&& token, Args&&... args)
  117. -> decltype(
  118. async_initiate<
  119. conditional_t<
  120. is_const<remove_reference_t<RawCompletionToken>>::value,
  121. const CompletionToken, CompletionToken>,
  122. typename detail::as_tuple_signature<Signatures>::type...>(
  123. init_wrapper<decay_t<Initiation>>(
  124. static_cast<Initiation&&>(initiation)),
  125. token.token_, static_cast<Args&&>(args)...))
  126. {
  127. return async_initiate<
  128. conditional_t<
  129. is_const<remove_reference_t<RawCompletionToken>>::value,
  130. const CompletionToken, CompletionToken>,
  131. typename detail::as_tuple_signature<Signatures>::type...>(
  132. init_wrapper<decay_t<Initiation>>(
  133. static_cast<Initiation&&>(initiation)),
  134. token.token_, static_cast<Args&&>(args)...);
  135. }
  136. };
  137. #if defined(ASIO_MSVC)
  138. // Workaround for MSVC internal compiler error.
  139. template <typename CompletionToken, typename Signature>
  140. struct async_result<as_tuple_t<CompletionToken>, Signature>
  141. : async_result<CompletionToken,
  142. typename detail::as_tuple_signature<Signature>::type>
  143. {
  144. template <typename Initiation>
  145. struct init_wrapper
  146. {
  147. init_wrapper(Initiation init)
  148. : initiation_(static_cast<Initiation&&>(init))
  149. {
  150. }
  151. template <typename Handler, typename... Args>
  152. void operator()(Handler&& handler, Args&&... args)
  153. {
  154. static_cast<Initiation&&>(initiation_)(
  155. detail::as_tuple_handler<decay_t<Handler>>(
  156. static_cast<Handler&&>(handler)),
  157. static_cast<Args&&>(args)...);
  158. }
  159. Initiation initiation_;
  160. };
  161. template <typename Initiation, typename RawCompletionToken, typename... Args>
  162. static auto initiate(Initiation&& initiation,
  163. RawCompletionToken&& token, Args&&... args)
  164. -> decltype(
  165. async_initiate<
  166. conditional_t<
  167. is_const<remove_reference_t<RawCompletionToken>>::value,
  168. const CompletionToken, CompletionToken>,
  169. typename detail::as_tuple_signature<Signature>::type>(
  170. init_wrapper<decay_t<Initiation>>(
  171. static_cast<Initiation&&>(initiation)),
  172. token.token_, static_cast<Args&&>(args)...))
  173. {
  174. return async_initiate<
  175. conditional_t<
  176. is_const<remove_reference_t<RawCompletionToken>>::value,
  177. const CompletionToken, CompletionToken>,
  178. typename detail::as_tuple_signature<Signature>::type>(
  179. init_wrapper<decay_t<Initiation>>(
  180. static_cast<Initiation&&>(initiation)),
  181. token.token_, static_cast<Args&&>(args)...);
  182. }
  183. };
  184. #endif // defined(ASIO_MSVC)
  185. template <template <typename, typename> class Associator,
  186. typename Handler, typename DefaultCandidate>
  187. struct associator<Associator,
  188. detail::as_tuple_handler<Handler>, DefaultCandidate>
  189. : Associator<Handler, DefaultCandidate>
  190. {
  191. static typename Associator<Handler, DefaultCandidate>::type get(
  192. const detail::as_tuple_handler<Handler>& h) noexcept
  193. {
  194. return Associator<Handler, DefaultCandidate>::get(h.handler_);
  195. }
  196. static auto get(const detail::as_tuple_handler<Handler>& h,
  197. const DefaultCandidate& c) noexcept
  198. -> decltype(Associator<Handler, DefaultCandidate>::get(h.handler_, c))
  199. {
  200. return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
  201. }
  202. };
  203. #endif // !defined(GENERATING_DOCUMENTATION)
  204. } // namespace asio
  205. #include "asio/detail/pop_options.hpp"
  206. #endif // ASIO_IMPL_AS_TUPLE_HPP