as_single.hpp 4.5 KB

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