-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoke.hpp
473 lines (418 loc) · 16.4 KB
/
invoke.hpp
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! \file eggs/invoke.hpp
// Eggs.Invoke
//
// Copyright Agustin K-ballo Berge, Fusion Fenix 2017-2020
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef EGGS_INVOKE_HPP
#define EGGS_INVOKE_HPP
#include <functional>
#include <type_traits>
#include <utility>
namespace eggs { namespace detail
{
#define EGGS_FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)
///////////////////////////////////////////////////////////////////////////
template <typename C, typename T, bool Ref, bool RefWrapper,
bool IsFunction = std::is_function<T>::value>
struct invoke_mem_ptr;
// when `pm` is a pointer to member of a class `C` and
// `is_base_of_v<C, remove_reference_t<T>>` is `true`;
template <typename C, typename T>
struct invoke_mem_ptr<C, T, /*Ref=*/true, /*RefWrapper=*/false, /*IsFunction=*/false>
{
T C::*pm;
#if !__cpp_aggregate_paren_init
constexpr invoke_mem_ptr(T C::*pm) noexcept
: pm(pm)
{}
#endif
template <typename T1>
constexpr auto operator()(T1&& t1) const
noexcept(noexcept(EGGS_FWD(t1).*pm))
-> decltype(EGGS_FWD(t1).*pm)
{
return EGGS_FWD(t1).*pm;
}
};
template <typename C, typename T>
struct invoke_mem_ptr<C, T, /*Ref=*/true, /*RefWrapper=*/false, /*IsFunction=*/true>
{
T C::*pm;
#if !__cpp_aggregate_paren_init
constexpr invoke_mem_ptr(T C::*pm) noexcept
: pm(pm)
{}
#endif
template <typename T1, typename... Tn>
constexpr auto operator()(T1&& t1, Tn&&... tn) const
noexcept(noexcept((EGGS_FWD(t1).*pm)(EGGS_FWD(tn)...)))
-> decltype((EGGS_FWD(t1).*pm)(EGGS_FWD(tn)...))
{
return (EGGS_FWD(t1).*pm)(EGGS_FWD(tn)...);
}
};
// when `pm` is a pointer to member of a class `C` and
// `remove_cvref_t<T>` is a specialization of `reference_wrapper`;
template <typename C, typename T>
struct invoke_mem_ptr<C, T, /*Ref=*/false, /*RefWrapper=*/true, /*IsFunction=*/false>
{
T C::*pm;
#if !__cpp_aggregate_paren_init
constexpr invoke_mem_ptr(T C::*pm) noexcept
: pm(pm)
{}
#endif
template <typename T1>
constexpr auto operator()(T1&& t1) const
noexcept(noexcept(t1.get().*pm))
-> decltype(t1.get().*pm)
{
return t1.get().*pm;
}
};
template <typename C, typename T>
struct invoke_mem_ptr<C, T, /*Ref=*/false, /*RefWrapper=*/true, /*IsFunction=*/true>
{
T C::*pm;
#if !__cpp_aggregate_paren_init
constexpr invoke_mem_ptr(T C::*pm) noexcept
: pm(pm)
{}
#endif
template <typename T1, typename... Tn>
constexpr auto operator()(T1&& t1, Tn&&... tn) const
noexcept(noexcept((t1.get().*pm)(EGGS_FWD(tn)...)))
-> decltype((t1.get().*pm)(EGGS_FWD(tn)...))
{
return (t1.get().*pm)(EGGS_FWD(tn)...);
}
};
// when `pm` is a pointer to member of a class `C` and `T` does not
// satisfy the previous two items;
template <typename C, typename T>
struct invoke_mem_ptr<C, T, /*Ref=*/false, /*RefWrapper=*/false, /*IsFunction=*/false>
{
T C::*pm;
#if !__cpp_aggregate_paren_init
constexpr invoke_mem_ptr(T C::*pm) noexcept
: pm(pm)
{}
#endif
template <typename T1>
constexpr auto operator()(T1&& t1) const
noexcept(noexcept((*EGGS_FWD(t1)).*pm))
-> decltype((*EGGS_FWD(t1)).*pm)
{
return (*EGGS_FWD(t1)).*pm;
}
};
template <typename C, typename T>
struct invoke_mem_ptr<C, T, /*Ref=*/false, /*RefWrapper=*/false, /*IsFunction=*/true>
{
T C::*pm;
#if !__cpp_aggregate_paren_init
constexpr invoke_mem_ptr(T C::*pm) noexcept
: pm(pm)
{}
#endif
template <typename T1, typename... Tn>
constexpr auto operator()(T1&& t1, Tn&&... tn) const
noexcept(noexcept(((*EGGS_FWD(t1)).*pm)(EGGS_FWD(tn)...)))
-> decltype(((*EGGS_FWD(t1)).*pm)(EGGS_FWD(tn)...))
{
return ((*EGGS_FWD(t1)).*pm)(EGGS_FWD(tn)...);
}
};
///////////////////////////////////////////////////////////////////////////
template <typename F>
auto invoke(F&&, ...)
-> F&&;
template <typename T, typename C, typename T1>
auto invoke(T C::*, T1 const&, ...)
-> invoke_mem_ptr<C, T,
/*Ref=*/std::is_base_of<C, T1>::value,
/*RefWrapper=*/false>;
template <typename T, typename C, typename X>
auto invoke(T C::*, std::reference_wrapper<X>, ...)
-> invoke_mem_ptr<C, T,
/*Ref=*/false,
/*RefWrapper=*/true>;
//! EGGS_INVOKE(F, ...)
//!
//! - _Returns_: `INVOKE(F __VA_OPT__(,) __VA_ARGS__)`.
#if __cplusplus > 201703L // C++20: P0306
#define EGGS_INVOKE(F, ...) \
(static_cast<decltype(::eggs::detail::invoke(F __VA_OPT__(,) __VA_ARGS__))>(F)(__VA_ARGS__))
#elif _MSVC_TRADITIONAL
#define EGGS_INVOKE(F, ...) \
(static_cast<decltype(::eggs::detail::invoke(F, __VA_ARGS__))>(F)(__VA_ARGS__))
#else
#define EGGS_INVOKE(F, ...) \
(static_cast<decltype(::eggs::detail::invoke(F, ##__VA_ARGS__))>(F)(__VA_ARGS__))
#endif
///////////////////////////////////////////////////////////////////////////
// `INVOKE(f, t1, t2, ..., tN)` implicitly converted to `R`.
template <typename R, typename RD = typename std::remove_cv<R>::type>
struct invoke_r
{
private:
static R conversion(R) noexcept;
public:
template <typename F, typename... Args>
static constexpr auto call(F&& f, Args&&... args)
noexcept(noexcept(conversion(
EGGS_INVOKE(EGGS_FWD(f), EGGS_FWD(args)...))))
-> decltype(conversion(
EGGS_INVOKE(EGGS_FWD(f), EGGS_FWD(args)...)))
{
return EGGS_INVOKE(EGGS_FWD(f), EGGS_FWD(args)...);
}
};
// `static_cast<void>(INVOKE(f, t1, t2, ..., tN))` if `R` is _cv_ `void`.
template <typename R>
struct invoke_r<R, void>
{
template <typename F, typename... Args>
static constexpr auto call(F&& f, Args&&... args)
noexcept(noexcept(
EGGS_INVOKE(EGGS_FWD(f), EGGS_FWD(args)...)))
-> decltype(static_cast<void>(
EGGS_INVOKE(EGGS_FWD(f), EGGS_FWD(args)...)))
{
return static_cast<void>(
EGGS_INVOKE(EGGS_FWD(f), EGGS_FWD(args)...));
}
};
//! EGGS_INVOKE(R, F, ...)
//!
//! - _Returns_: `INVOKE<R>(F __VA_OPT__(,) __VA_ARGS__)`.
#define EGGS_INVOKE_R(R, ...) \
(::eggs::detail::invoke_r<R>::call(__VA_ARGS__))
}} // namespace eggs::detail
namespace eggs
{
///////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename T, typename Enable = void>
struct invoke_result_impl
{};
template <typename F, typename... Ts>
struct invoke_result_impl<F(Ts...), decltype((void)
EGGS_INVOKE(std::declval<F>(), std::declval<Ts>()...))>
{
using type = decltype(
EGGS_INVOKE(std::declval<F>(), std::declval<Ts>()...));
};
} // namespace detail
//! template <class Fn, class... ArgTypes> struct invoke_result;
//!
//! - _Comments_: If the expression `INVOKE(std::declval<Fn>(),
//! std::declval<ArgTypes>()...)` is well-formed when treated as an
//! unevaluated operand, the member typedef `type` names the type
//! `decltype(INVOKE(std::declval<Fn>(), std::declval<ArgTypes>()...))`;
//! otherwise, there shall be no member `type`. Access checking is
//! performed as if in a context unrelated to `Fn` and `ArgTypes`. Only
//! the validity of the immediate context of the expression is considered.
//!
//! - _Preconditions_: `Fn` and all types in the template parameter pack
//! `ArgTypes` are complete types, _cv_ `void`, or arrays of unknown
//! bound.
template <typename Fn, typename... ArgTypes>
struct invoke_result
: detail::invoke_result_impl<Fn&&(ArgTypes&&...)>
{};
//! template <class Fn, class... ArgTypes>
//! using invoke_result_t = typename invoke_result<Fn, ArgTypes...>::type;
template <typename Fn, typename... ArgTypes>
using invoke_result_t =
typename invoke_result<Fn, ArgTypes...>::type;
///////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename T, typename Enable = void>
struct is_invocable_impl
: std::false_type
{};
template <typename F, typename... Ts>
struct is_invocable_impl<F(Ts...), decltype((void)
EGGS_INVOKE(std::declval<F>(), std::declval<Ts>()...))>
: std::true_type
{};
}
//! template <class Fn, class... ArgTypes> struct is_invocable;
//!
//! - _Condition_: The expression `INVOKE(std::declval<Fn>(),
//! std::declval<ArgTypes>()...)` is well-formed when treated as an
//! unevaluated operand.
//!
//! - _Comments_: `Fn` and all types in the template parameter pack
//! `ArgTypes` shall be complete types, _cv_ `void`, or arrays of
//! unknown bound.
template <typename Fn, typename... ArgTypes>
struct is_invocable
: detail::is_invocable_impl<Fn&&(ArgTypes&&...)>::type
{};
#if __cpp_variable_templates
//! template <class Fn, class... ArgTypes> // (C++14)
//! inline constexpr bool is_invocable_v =
//! eggs::is_invocable<Fn, ArgTypes...>::value;
template <typename Fn, typename... ArgTypes>
#if __cpp_inline_variables
inline
#endif
constexpr bool is_invocable_v =
is_invocable<Fn, ArgTypes...>::value;
#endif
///////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename T, typename R, typename Enable = void>
struct is_invocable_r_impl
: std::false_type
{};
template <typename F, typename... Ts, typename R>
struct is_invocable_r_impl<F(Ts...), R, decltype((void)
EGGS_INVOKE_R(R, std::declval<F>(), std::declval<Ts>()...))>
: std::true_type
{};
}
//! template <class R, class Fn, class... ArgTypes> struct is_invocable_r;
//!
//! - _Condition_: The expression `INVOKE<R>(std::declval<Fn>(),
//! std::declval<ArgTypes>()...)` is well-formed when treated as an
//! unevaluated operand.
//!
//! - _Comments_: `Fn`, `R`, and all types in the template parameter pack
//! `ArgTypes` shall be complete types, _cv_ `void`, or arrays of
//! unknown bound.
template <typename R, typename Fn, typename... ArgTypes>
struct is_invocable_r
: detail::is_invocable_r_impl<Fn&&(ArgTypes&&...), R>::type
{};
#if __cpp_variable_templates
//! template <class R, class Fn, class... ArgTypes> // (C++14)
//! inline constexpr bool is_invocable_r_v =
//! eggs::is_invocable_r<R, Fn, ArgTypes...>::value;
template <typename R, typename Fn, typename... ArgTypes>
#if __cpp_inline_variables
inline
#endif
constexpr bool is_invocable_r_v =
is_invocable_r<R, Fn, ArgTypes...>::value;
#endif
///////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename T, typename Enable = void>
struct is_nothrow_invocable_impl
: std::false_type
{};
template <typename F, typename... Ts>
struct is_nothrow_invocable_impl<F(Ts...), decltype((void)
EGGS_INVOKE(std::declval<F>(), std::declval<Ts>()...))>
: std::integral_constant<bool, noexcept(
EGGS_INVOKE(std::declval<F>(), std::declval<Ts>()...))>
{};
}
//! template <class Fn, class... ArgTypes> struct is_nothrow_invocable;
//!
//! - _Condition_: `eggs::is_invocable_v<Fn, ArgTypes...>` is `true` and
//! the expression `INVOKE(std::declval<Fn>(), std::declval<ArgTypes>()...)`
//! is known not to throw any exceptions.
//!
//! - _Comments_: `Fn` and all types in the template parameter pack
//! `ArgTypes` shall be complete types, _cv_ `void`, or arrays of
//! unknown bound.
template <typename Fn, typename... ArgTypes>
struct is_nothrow_invocable
: detail::is_nothrow_invocable_impl<Fn&&(ArgTypes&&...)>::type
{};
#if __cpp_variable_templates
//! template <class Fn, class... ArgTypes> // (C++14)
//! inline constexpr bool is_nothrow_invocable_v =
//! eggs::is_nothrow_invocable<Fn, ArgTypes...>::value;
template <typename Fn, typename... ArgTypes>
#if __cpp_inline_variables
inline
#endif
constexpr bool is_nothrow_invocable_v =
is_nothrow_invocable<Fn, ArgTypes...>::value;
#endif
///////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename T, typename R, typename Enable = void>
struct is_nothrow_invocable_r_impl
: std::false_type
{};
template <typename F, typename... Ts, typename R>
struct is_nothrow_invocable_r_impl<F(Ts...), R, decltype((void)
EGGS_INVOKE_R(R, std::declval<F>(), std::declval<Ts>()...))>
: std::integral_constant<bool, noexcept(
EGGS_INVOKE_R(R, std::declval<F>(), std::declval<Ts>()...))>
{};
}
//! template <class R, class Fn, class... ArgTypes> struct is_nothrow_invocable_r;
//!
//! - _Condition_: `eggs::is_invocable_r_v<R, Fn, ArgTypes...>` is `true`
//! and the expression `INVOKE(std::declval<Fn>(), std::declval<ArgTypes>()...)`
//! is known not to throw any exceptions.
//!
//! - _Comments_: `Fn`, `R`, and all types in the template parameter pack
//! `ArgTypes` shall be complete types, _cv_ `void`, or arrays of
//! unknown bound.
template <typename R, typename Fn, typename... ArgTypes>
struct is_nothrow_invocable_r
: detail::is_nothrow_invocable_r_impl<Fn&&(ArgTypes&&...), R>::type
{};
#if __cpp_variable_templates
//! template <class R, class Fn, class... ArgTypes> // (C++14)
//! inline constexpr bool is_nothrow_invocable_r_v =
//! eggs::is_nothrow_invocable_r<R, Fn, ArgTypes...>::value;
template <typename R, typename Fn, typename... ArgTypes>
#if __cpp_inline_variables
inline
#endif
constexpr bool is_nothrow_invocable_r_v =
is_nothrow_invocable_r<R, Fn, ArgTypes...>::value;
#endif
///////////////////////////////////////////////////////////////////////////
//! template <class F, class... Args>
//! constexpr eggs::invoke_result_t<F, Args...> invoke(F&& f, Args&&... args)
//! noexcept(eggs::is_nothrow_invocable_v<F, Args...>);
//!
//! - _Returns_: `INVOKE(std::forward<F>(f), std::forward<Args>(args)...)`.
//!
//! - _Remarks_: This function shall not participate in overload resolution
//! unless `eggs::is_invocable_v<F, Args...>` is `true`.
template <typename Fn, typename... ArgTypes>
constexpr auto
invoke(Fn&& f, ArgTypes&&... args)
noexcept(noexcept(EGGS_INVOKE(EGGS_FWD(f), EGGS_FWD(args)...)))
-> decltype(EGGS_INVOKE(EGGS_FWD(f), EGGS_FWD(args)...))
{
return EGGS_INVOKE(EGGS_FWD(f), EGGS_FWD(args)...);
}
///////////////////////////////////////////////////////////////////////////
//! template <class R, class F, class... Args> // (extension)
//! constexpr R eggs::invoke_r(F&& f, Args&&... args)
//! noexcept(eggs::is_nothrow_invocable_r_v<R, F, Args...>);
//!
//! - _Returns_: `INVOKE<R>(std::forward<F>(f), std::forward<Args>(args)...)`.
//!
//! - _Remarks_: This function shall not participate in overload resolution
//! unless `eggs::is_invocable_r_v<R, F, Args...>` is `true`.
template <typename R, typename Fn, typename... ArgTypes>
constexpr auto
invoke_r(Fn&& f, ArgTypes&&... args)
noexcept(noexcept(EGGS_INVOKE_R(R, EGGS_FWD(f), EGGS_FWD(args)...)))
-> decltype(EGGS_INVOKE_R(R, EGGS_FWD(f), EGGS_FWD(args)...))
{
return EGGS_INVOKE_R(R, EGGS_FWD(f), EGGS_FWD(args)...);
}
#undef EGGS_FWD
} // namespace eggs
#endif /*EGGS_INVOKE_HPP*/