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