as_single.hpp 4.1 KB

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