as_tuple.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // experimental/as_tuple.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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_EXPERIMENTAL_AS_TUPLE_HPP
  11. #define ASIO_EXPERIMENTAL_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. namespace experimental {
  20. /// Completion token type 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. ASIO_CONSTEXPR as_tuple_t(
  43. default_constructor_tag = default_constructor_tag(),
  44. CompletionToken token = CompletionToken())
  45. : token_(ASIO_MOVE_CAST(CompletionToken)(token))
  46. {
  47. }
  48. /// Constructor.
  49. template <typename T>
  50. ASIO_CONSTEXPR explicit as_tuple_t(
  51. ASIO_MOVE_ARG(T) completion_token)
  52. : token_(ASIO_MOVE_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. typename constraint<
  66. conditional<
  67. !is_same<InnerExecutor1, executor_with_default>::value,
  68. is_convertible<InnerExecutor1, InnerExecutor>,
  69. false_type
  70. >::type::value
  71. >::type = 0) ASIO_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. #if defined(ASIO_HAS_ALIAS_TEMPLATES) \
  79. || defined(GENERATING_DOCUMENTATION)
  80. template <typename T>
  81. using as_default_on_t = typename T::template rebind_executor<
  82. executor_with_default<typename T::executor_type> >::other;
  83. #endif // defined(ASIO_HAS_ALIAS_TEMPLATES)
  84. // || defined(GENERATING_DOCUMENTATION)
  85. /// Function helper to adapt an I/O object to use @c as_tuple_t as its
  86. /// default completion token type.
  87. template <typename T>
  88. static typename decay<T>::type::template rebind_executor<
  89. executor_with_default<typename decay<T>::type::executor_type>
  90. >::other
  91. as_default_on(ASIO_MOVE_ARG(T) object)
  92. {
  93. return typename decay<T>::type::template rebind_executor<
  94. executor_with_default<typename decay<T>::type::executor_type>
  95. >::other(ASIO_MOVE_CAST(T)(object));
  96. }
  97. //private:
  98. CompletionToken token_;
  99. };
  100. /// Create a completion token to specify that the completion handler arguments
  101. /// should be combined into a single tuple argument.
  102. template <typename CompletionToken>
  103. ASIO_NODISCARD inline
  104. ASIO_CONSTEXPR as_tuple_t<typename decay<CompletionToken>::type>
  105. as_tuple(ASIO_MOVE_ARG(CompletionToken) completion_token)
  106. {
  107. return as_tuple_t<typename decay<CompletionToken>::type>(
  108. ASIO_MOVE_CAST(CompletionToken)(completion_token));
  109. }
  110. } // namespace experimental
  111. } // namespace asio
  112. #include "asio/detail/pop_options.hpp"
  113. #include "asio/experimental/impl/as_tuple.hpp"
  114. #endif // ASIO_EXPERIMENTAL_AS_TUPLE_HPP