as_tuple.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // as_tuple.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_AS_TUPLE_HPP
  11. #define BOOST_ASIO_AS_TUPLE_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/detail/type_traits.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. /// A @ref completion_token adapter used to specify that the completion handler
  21. /// arguments should be combined into a single tuple argument.
  22. /**
  23. * The as_tuple_t class is used to indicate that any arguments to the
  24. * completion handler should be combined and passed as a single tuple argument.
  25. * The arguments are first moved into a @c std::tuple and that tuple is then
  26. * passed to the completion handler.
  27. */
  28. template <typename CompletionToken>
  29. class as_tuple_t
  30. {
  31. public:
  32. /// Tag type used to prevent the "default" constructor from being used for
  33. /// conversions.
  34. struct default_constructor_tag {};
  35. /// Default constructor.
  36. /**
  37. * This constructor is only valid if the underlying completion token is
  38. * default constructible and move constructible. The underlying completion
  39. * token is itself defaulted as an argument to allow it to capture a source
  40. * location.
  41. */
  42. constexpr as_tuple_t(
  43. default_constructor_tag = default_constructor_tag(),
  44. CompletionToken token = CompletionToken())
  45. : token_(static_cast<CompletionToken&&>(token))
  46. {
  47. }
  48. /// Constructor.
  49. template <typename T>
  50. constexpr explicit as_tuple_t(
  51. T&& completion_token)
  52. : token_(static_cast<T&&>(completion_token))
  53. {
  54. }
  55. /// Adapts an executor to add the @c as_tuple_t completion token as the
  56. /// default.
  57. template <typename InnerExecutor>
  58. struct executor_with_default : InnerExecutor
  59. {
  60. /// Specify @c as_tuple_t as the default completion token type.
  61. typedef as_tuple_t default_completion_token_type;
  62. /// Construct the adapted executor from the inner executor type.
  63. template <typename InnerExecutor1>
  64. executor_with_default(const InnerExecutor1& ex,
  65. constraint_t<
  66. conditional_t<
  67. !is_same<InnerExecutor1, executor_with_default>::value,
  68. is_convertible<InnerExecutor1, InnerExecutor>,
  69. false_type
  70. >::value
  71. > = 0) noexcept
  72. : InnerExecutor(ex)
  73. {
  74. }
  75. };
  76. /// Type alias to adapt an I/O object to use @c as_tuple_t as its
  77. /// default completion token type.
  78. template <typename T>
  79. using as_default_on_t = typename T::template rebind_executor<
  80. executor_with_default<typename T::executor_type>>::other;
  81. /// Function helper to adapt an I/O object to use @c as_tuple_t as its
  82. /// default completion token type.
  83. template <typename T>
  84. static typename decay_t<T>::template rebind_executor<
  85. executor_with_default<typename decay_t<T>::executor_type>
  86. >::other
  87. as_default_on(T&& object)
  88. {
  89. return typename decay_t<T>::template rebind_executor<
  90. executor_with_default<typename decay_t<T>::executor_type>
  91. >::other(static_cast<T&&>(object));
  92. }
  93. //private:
  94. CompletionToken token_;
  95. };
  96. /// Adapt a @ref completion_token to specify that the completion handler
  97. /// arguments should be combined into a single tuple argument.
  98. template <typename CompletionToken>
  99. BOOST_ASIO_NODISCARD inline
  100. constexpr as_tuple_t<decay_t<CompletionToken>>
  101. as_tuple(CompletionToken&& completion_token)
  102. {
  103. return as_tuple_t<decay_t<CompletionToken>>(
  104. static_cast<CompletionToken&&>(completion_token));
  105. }
  106. } // namespace asio
  107. } // namespace boost
  108. #include <boost/asio/detail/pop_options.hpp>
  109. #include <boost/asio/impl/as_tuple.hpp>
  110. #endif // BOOST_ASIO_AS_TUPLE_HPP