as_tuple.hpp 4.5 KB

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