prefer_only.hpp
8.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//
// execution/prefer_only.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef ASIO_EXECUTION_PREFER_ONLY_HPP
#define ASIO_EXECUTION_PREFER_ONLY_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
#include "asio/detail/type_traits.hpp"
#include "asio/is_applicable_property.hpp"
#include "asio/prefer.hpp"
#include "asio/query.hpp"
#include "asio/traits/static_query.hpp"
#include "asio/detail/push_options.hpp"
namespace asio {
#if defined(GENERATING_DOCUMENTATION)
namespace execution {
/// A property adapter that is used with the polymorphic executor wrapper
/// to mark properties as preferable, but not requirable.
template <typename Property>
struct prefer_only
{
/// The prefer_only adapter applies to the same types as the nested property.
template <typename T>
static constexpr bool is_applicable_property_v =
is_applicable_property<T, Property>::value;
/// The context_t property cannot be required.
static constexpr bool is_requirable = false;
/// The context_t property can be preferred, it the underlying property can
/// be preferred.
/**
* @c true if @c Property::is_preferable is @c true, otherwise @c false.
*/
static constexpr bool is_preferable = automatically_determined;
/// The type returned by queries against an @c any_executor.
typedef typename Property::polymorphic_query_result_type
polymorphic_query_result_type;
};
} // namespace execution
#else // defined(GENERATING_DOCUMENTATION)
namespace execution {
namespace detail {
template <typename InnerProperty, typename = void>
struct prefer_only_is_preferable
{
ASIO_STATIC_CONSTEXPR(bool, is_preferable = false);
};
template <typename InnerProperty>
struct prefer_only_is_preferable<InnerProperty,
typename enable_if<
InnerProperty::is_preferable
>::type>
{
ASIO_STATIC_CONSTEXPR(bool, is_preferable = true);
};
template <typename InnerProperty, typename = void>
struct prefer_only_polymorphic_query_result_type
{
};
template <typename InnerProperty>
struct prefer_only_polymorphic_query_result_type<InnerProperty,
typename void_type<
typename InnerProperty::polymorphic_query_result_type
>::type>
{
typedef typename InnerProperty::polymorphic_query_result_type
polymorphic_query_result_type;
};
template <typename InnerProperty, typename = void>
struct prefer_only_property
{
InnerProperty property;
prefer_only_property(const InnerProperty& p)
: property(p)
{
}
};
#if defined(ASIO_HAS_DECLTYPE) \
&& defined(ASIO_HAS_WORKING_EXPRESSION_SFINAE)
template <typename InnerProperty>
struct prefer_only_property<InnerProperty,
typename void_type<
decltype(asio::declval<const InnerProperty>().value())
>::type>
{
InnerProperty property;
prefer_only_property(const InnerProperty& p)
: property(p)
{
}
ASIO_CONSTEXPR auto value() const
ASIO_NOEXCEPT_IF((
noexcept(asio::declval<const InnerProperty>().value())))
-> decltype(asio::declval<const InnerProperty>().value())
{
return property.value();
}
};
#else // defined(ASIO_HAS_DECLTYPE)
// && defined(ASIO_HAS_WORKING_EXPRESSION_SFINAE)
struct prefer_only_memfns_base
{
void value();
};
template <typename T>
struct prefer_only_memfns_derived
: T, prefer_only_memfns_base
{
};
template <typename T, T>
struct prefer_only_memfns_check
{
};
template <typename>
char (&prefer_only_value_memfn_helper(...))[2];
template <typename T>
char prefer_only_value_memfn_helper(
prefer_only_memfns_check<
void (prefer_only_memfns_base::*)(),
&prefer_only_memfns_derived<T>::value>*);
template <typename InnerProperty>
struct prefer_only_property<InnerProperty,
typename enable_if<
sizeof(prefer_only_value_memfn_helper<InnerProperty>(0)) != 1
&& !is_same<typename InnerProperty::polymorphic_query_result_type,
void>::value
>::type>
{
InnerProperty property;
prefer_only_property(const InnerProperty& p)
: property(p)
{
}
ASIO_CONSTEXPR typename InnerProperty::polymorphic_query_result_type
value() const
{
return property.value();
}
};
#endif // defined(ASIO_HAS_DECLTYPE)
// && defined(ASIO_HAS_WORKING_EXPRESSION_SFINAE)
} // namespace detail
template <typename InnerProperty>
struct prefer_only :
detail::prefer_only_is_preferable<InnerProperty>,
detail::prefer_only_polymorphic_query_result_type<InnerProperty>,
detail::prefer_only_property<InnerProperty>
{
ASIO_STATIC_CONSTEXPR(bool, is_requirable = false);
ASIO_CONSTEXPR prefer_only(const InnerProperty& p)
: detail::prefer_only_property<InnerProperty>(p)
{
}
#if defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
&& defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
template <typename T>
static ASIO_CONSTEXPR
typename traits::static_query<T, InnerProperty>::result_type
static_query()
ASIO_NOEXCEPT_IF((
traits::static_query<T, InnerProperty>::is_noexcept))
{
return traits::static_query<T, InnerProperty>::value();
}
template <typename E, typename T = decltype(prefer_only::static_query<E>())>
static ASIO_CONSTEXPR const T static_query_v
= prefer_only::static_query<E>();
#endif // defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
// && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
template <typename Executor, typename Property>
friend ASIO_CONSTEXPR
typename prefer_result<const Executor&, const InnerProperty&>::type
prefer(const Executor& ex, const prefer_only<Property>& p,
typename enable_if<
is_same<Property, InnerProperty>::value
&& can_prefer<const Executor&, const InnerProperty&>::value
>::type* = 0)
#if !defined(ASIO_MSVC) \
&& !defined(__clang__) // Clang crashes if noexcept is used here.
ASIO_NOEXCEPT_IF((
is_nothrow_prefer<const Executor&, const InnerProperty&>::value))
#endif // !defined(ASIO_MSVC)
// && !defined(__clang__)
{
return asio::prefer(ex, p.property);
}
template <typename Executor, typename Property>
friend ASIO_CONSTEXPR
typename query_result<const Executor&, const InnerProperty&>::type
query(const Executor& ex, const prefer_only<Property>& p,
typename enable_if<
is_same<Property, InnerProperty>::value
&& can_query<const Executor&, const InnerProperty&>::value
>::type* = 0)
#if !defined(ASIO_MSVC) \
&& !defined(__clang__) // Clang crashes if noexcept is used here.
ASIO_NOEXCEPT_IF((
is_nothrow_query<const Executor&, const InnerProperty&>::value))
#endif // !defined(ASIO_MSVC)
// && !defined(__clang__)
{
return asio::query(ex, p.property);
}
};
#if defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
&& defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
template <typename InnerProperty> template <typename E, typename T>
const T prefer_only<InnerProperty>::static_query_v;
#endif // defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
// && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
} // namespace execution
template <typename T, typename InnerProperty>
struct is_applicable_property<T, execution::prefer_only<InnerProperty> >
: is_applicable_property<T, InnerProperty>
{
};
namespace traits {
#if !defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
|| !defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
template <typename T, typename InnerProperty>
struct static_query<T, execution::prefer_only<InnerProperty> > :
static_query<T, const InnerProperty&>
{
};
#endif // !defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
// || !defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
#if !defined(ASIO_HAS_DEDUCED_PREFER_FREE_TRAIT)
template <typename T, typename InnerProperty>
struct prefer_free_default<T, execution::prefer_only<InnerProperty>,
typename enable_if<
can_prefer<const T&, const InnerProperty&>::value
>::type>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept =
(is_nothrow_prefer<const T&, const InnerProperty&>::value));
typedef typename prefer_result<const T&,
const InnerProperty&>::type result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_PREFER_FREE_TRAIT)
#if !defined(ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT)
template <typename T, typename InnerProperty>
struct query_free<T, execution::prefer_only<InnerProperty>,
typename enable_if<
can_query<const T&, const InnerProperty&>::value
>::type>
{
ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
ASIO_STATIC_CONSTEXPR(bool, is_noexcept =
(is_nothrow_query<const T&, const InnerProperty&>::value));
typedef typename query_result<const T&,
const InnerProperty&>::type result_type;
};
#endif // !defined(ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT)
} // namespace traits
#endif // defined(GENERATING_DOCUMENTATION)
} // namespace asio
#include "asio/detail/pop_options.hpp"
#endif // ASIO_EXECUTION_PREFER_ONLY_HPP