consign.hpp 4.0 KB

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