1 | // |
2 | // execution/context_as.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_EXECUTION_CONTEXT_AS_HPP |
12 | #define BOOST_ASIO_EXECUTION_CONTEXT_AS_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 | #include <boost/asio/execution/context.hpp> |
21 | #include <boost/asio/execution/executor.hpp> |
22 | #include <boost/asio/is_applicable_property.hpp> |
23 | #include <boost/asio/query.hpp> |
24 | #include <boost/asio/traits/query_static_constexpr_member.hpp> |
25 | #include <boost/asio/traits/static_query.hpp> |
26 | |
27 | #include <boost/asio/detail/push_options.hpp> |
28 | |
29 | namespace boost { |
30 | namespace asio { |
31 | |
32 | #if defined(GENERATING_DOCUMENTATION) |
33 | |
34 | namespace execution { |
35 | |
36 | /// A property that is used to obtain the execution context that is associated |
37 | /// with an executor. |
38 | template <typename U> |
39 | struct context_as_t |
40 | { |
41 | /// The context_as_t property applies to executors. |
42 | template <typename T> |
43 | static constexpr bool is_applicable_property_v = is_executor_v<T>; |
44 | |
45 | /// The context_t property cannot be required. |
46 | static constexpr bool is_requirable = false; |
47 | |
48 | /// The context_t property cannot be preferred. |
49 | static constexpr bool is_preferable = false; |
50 | |
51 | /// The type returned by queries against an @c any_executor. |
52 | typedef T polymorphic_query_result_type; |
53 | }; |
54 | |
55 | /// A special value used for accessing the context_as_t property. |
56 | template <typename U> |
57 | constexpr context_as_t context_as; |
58 | |
59 | } // namespace execution |
60 | |
61 | #else // defined(GENERATING_DOCUMENTATION) |
62 | |
63 | namespace execution { |
64 | |
65 | template <typename T> |
66 | struct context_as_t |
67 | { |
68 | #if defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES) |
69 | template <typename U> |
70 | static constexpr bool is_applicable_property_v = is_executor<U>::value; |
71 | #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES) |
72 | |
73 | static constexpr bool is_requirable = false; |
74 | static constexpr bool is_preferable = false; |
75 | |
76 | typedef T polymorphic_query_result_type; |
77 | |
78 | constexpr context_as_t() |
79 | { |
80 | } |
81 | |
82 | constexpr context_as_t(context_t) |
83 | { |
84 | } |
85 | |
86 | #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \ |
87 | && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES) |
88 | template <typename E> |
89 | static constexpr |
90 | typename context_t::query_static_constexpr_member<E>::result_type |
91 | static_query() |
92 | noexcept(context_t::query_static_constexpr_member<E>::is_noexcept) |
93 | { |
94 | return context_t::query_static_constexpr_member<E>::value(); |
95 | } |
96 | |
97 | template <typename E, typename U = decltype(context_as_t::static_query<E>())> |
98 | static constexpr const U static_query_v |
99 | = context_as_t::static_query<E>(); |
100 | #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) |
101 | // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES) |
102 | |
103 | template <typename Executor, typename U> |
104 | friend constexpr U query( |
105 | const Executor& ex, const context_as_t<U>&, |
106 | enable_if_t< |
107 | is_same<T, U>::value |
108 | >* = 0, |
109 | enable_if_t< |
110 | can_query<const Executor&, const context_t&>::value |
111 | >* = 0) |
112 | #if !defined(__clang__) // Clang crashes if noexcept is used here. |
113 | #if defined(BOOST_ASIO_MSVC) // Visual C++ wants the type to be qualified. |
114 | noexcept(is_nothrow_query<const Executor&, const context_t&>::value) |
115 | #else // defined(BOOST_ASIO_MSVC) |
116 | noexcept(is_nothrow_query<const Executor&, const context_t&>::value) |
117 | #endif // defined(BOOST_ASIO_MSVC) |
118 | #endif // !defined(__clang__) |
119 | { |
120 | return boost::asio::query(ex, context); |
121 | } |
122 | }; |
123 | |
124 | #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \ |
125 | && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES) |
126 | template <typename T> template <typename E, typename U> |
127 | const U context_as_t<T>::static_query_v; |
128 | #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) |
129 | // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES) |
130 | |
131 | #if defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES) \ |
132 | || defined(GENERATING_DOCUMENTATION) |
133 | template <typename T> |
134 | constexpr context_as_t<T> context_as{}; |
135 | #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES) |
136 | // || defined(GENERATING_DOCUMENTATION) |
137 | |
138 | } // namespace execution |
139 | |
140 | #if !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES) |
141 | |
142 | template <typename T, typename U> |
143 | struct is_applicable_property<T, execution::context_as_t<U>> |
144 | : integral_constant<bool, execution::is_executor<T>::value> |
145 | { |
146 | }; |
147 | |
148 | #endif // !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES) |
149 | |
150 | namespace traits { |
151 | |
152 | #if !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \ |
153 | || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES) |
154 | |
155 | template <typename T, typename U> |
156 | struct static_query<T, execution::context_as_t<U>, |
157 | enable_if_t< |
158 | static_query<T, execution::context_t>::is_valid |
159 | >> : static_query<T, execution::context_t> |
160 | { |
161 | }; |
162 | |
163 | #endif // !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) |
164 | // || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES) |
165 | |
166 | #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT) |
167 | |
168 | template <typename T, typename U> |
169 | struct query_free<T, execution::context_as_t<U>, |
170 | enable_if_t< |
171 | can_query<const T&, const execution::context_t&>::value |
172 | >> |
173 | { |
174 | static constexpr bool is_valid = true; |
175 | static constexpr bool is_noexcept = |
176 | is_nothrow_query<const T&, const execution::context_t&>::value; |
177 | |
178 | typedef U result_type; |
179 | }; |
180 | |
181 | #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT) |
182 | |
183 | } // namespace traits |
184 | |
185 | #endif // defined(GENERATING_DOCUMENTATION) |
186 | |
187 | } // namespace asio |
188 | } // namespace boost |
189 | |
190 | #include <boost/asio/detail/pop_options.hpp> |
191 | |
192 | #endif // BOOST_ASIO_EXECUTION_CONTEXT_AS_HPP |
193 | |