as_tuple.hpp 3.8 KB

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