consign.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // impl/consign.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_CONSIGN_HPP
  11. #define ASIO_IMPL_CONSIGN_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 consign_t as a completion handler.
  25. template <typename Handler, typename... Values>
  26. class consign_handler
  27. {
  28. public:
  29. typedef void result_type;
  30. template <typename H>
  31. consign_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. static_cast<Handler&&>(handler_)(static_cast<Args&&>(args)...);
  40. }
  41. //private:
  42. Handler handler_;
  43. std::tuple<Values...> values_;
  44. };
  45. template <typename Handler>
  46. inline bool asio_handler_is_continuation(
  47. consign_handler<Handler>* this_handler)
  48. {
  49. return asio_handler_cont_helpers::is_continuation(
  50. this_handler->handler_);
  51. }
  52. } // namespace detail
  53. #if !defined(GENERATING_DOCUMENTATION)
  54. template <typename CompletionToken, typename... Values, typename... Signatures>
  55. struct async_result<consign_t<CompletionToken, Values...>, Signatures...>
  56. : async_result<CompletionToken, Signatures...>
  57. {
  58. template <typename Initiation>
  59. struct init_wrapper
  60. {
  61. init_wrapper(Initiation init)
  62. : initiation_(static_cast<Initiation&&>(init))
  63. {
  64. }
  65. template <typename Handler, typename... Args>
  66. void operator()(Handler&& handler,
  67. std::tuple<Values...> values, Args&&... args)
  68. {
  69. static_cast<Initiation&&>(initiation_)(
  70. detail::consign_handler<decay_t<Handler>, Values...>(
  71. static_cast<Handler&&>(handler),
  72. static_cast<std::tuple<Values...>&&>(values)),
  73. static_cast<Args&&>(args)...);
  74. }
  75. Initiation initiation_;
  76. };
  77. template <typename Initiation, typename RawCompletionToken, typename... Args>
  78. static auto initiate(Initiation&& initiation,
  79. RawCompletionToken&& token, Args&&... args)
  80. -> decltype(
  81. async_initiate<CompletionToken, Signatures...>(
  82. init_wrapper<decay_t<Initiation>>(
  83. static_cast<Initiation&&>(initiation)),
  84. token.token_, static_cast<std::tuple<Values...>&&>(token.values_),
  85. static_cast<Args&&>(args)...))
  86. {
  87. return async_initiate<CompletionToken, Signatures...>(
  88. init_wrapper<decay_t<Initiation>>(
  89. static_cast<Initiation&&>(initiation)),
  90. token.token_, static_cast<std::tuple<Values...>&&>(token.values_),
  91. static_cast<Args&&>(args)...);
  92. }
  93. };
  94. template <template <typename, typename> class Associator,
  95. typename Handler, typename... Values, typename DefaultCandidate>
  96. struct associator<Associator,
  97. detail::consign_handler<Handler, Values...>, DefaultCandidate>
  98. : Associator<Handler, DefaultCandidate>
  99. {
  100. static typename Associator<Handler, DefaultCandidate>::type get(
  101. const detail::consign_handler<Handler, Values...>& h) noexcept
  102. {
  103. return Associator<Handler, DefaultCandidate>::get(h.handler_);
  104. }
  105. static auto get(const detail::consign_handler<Handler, Values...>& h,
  106. const DefaultCandidate& c) noexcept
  107. -> decltype(Associator<Handler, DefaultCandidate>::get(h.handler_, c))
  108. {
  109. return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
  110. }
  111. };
  112. #endif // !defined(GENERATING_DOCUMENTATION)
  113. } // namespace asio
  114. #include "asio/detail/pop_options.hpp"
  115. #endif // ASIO_IMPL_CONSIGN_HPP