-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoke.hpp
82 lines (72 loc) · 2.99 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
#pragma once
#include <type_traits>
#include <utility>
#include "is_reference_wrapper.hpp"
template<class>
struct invoke_impl {
template<class F, class... Args>
static constexpr auto call(F&& f, Args&&... args)
noexcept(noexcept(static_cast<F&&>(f)(static_cast<Args&&>(args)...)))
-> decltype(static_cast<F&&>(f)(static_cast<Args&&>(args)...))
{ return static_cast<F&&>(f)(static_cast<Args&&>(args)...); }
};
template<class A, class B>
struct invoke_impl<A B::*> {
template<class T,
class = std::enable_if_t<std::is_base_of_v<B, std::decay_t<T>>>
>
static constexpr auto get(T&& t) noexcept -> T&&
{ return static_cast<T&&>(t); }
template<class T,
class = std::enable_if_t<is_reference_wrapper_v<std::decay_t<T>>>
>
static constexpr auto get(T&& t) noexcept -> decltype(t.get())
{ return t.get(); }
template<class T,
class = std::enable_if_t<!std::is_base_of_v<B, std::decay_t<T>>>,
class = std::enable_if_t<!is_reference_wrapper_v<std::decay_t<T>>>
>
static constexpr auto get(T&& t)
noexcept(noexcept(*static_cast<T&&>(t)))
-> decltype(*static_cast<T&&>(t))
{ return *static_cast<T&&>(t); }
template<class AA, class T, class... Args,
class = std::enable_if_t<std::is_function_v<AA>>
>
static constexpr auto call(AA B::*pmf, T&& t, Args&&... args)
noexcept(noexcept(((get)(static_cast<T&&>(t)).*pmf)(static_cast<Args&&>(args)...)))
-> decltype(((get)(static_cast<T&&>(t)).*pmf)(static_cast<Args&&>(args)...))
{ return ((get)(static_cast<T&&>(t)).*pmf)(static_cast<Args&&>(args)...); }
template<class T>
static constexpr auto call(A B::*pmd, T&& t)
noexcept(noexcept((get)(static_cast<T&&>(t)).*pmd))
-> decltype((get)(static_cast<T&&>(t)).*pmd)
{ return (get)(static_cast<T&&>(t)).*pmd; }
};
template<class F, class... Args>
constexpr auto invoke(F&& f, Args&&... args)
noexcept(noexcept(invoke_impl<std::decay_t<F>>::call(
static_cast<F&&>(f), static_cast<Args&&>(args)...)))
-> decltype(invoke_impl<std::decay_t<F>>::call(
static_cast<F&&>(f), static_cast<Args&&>(args)...))
{
return invoke_impl<std::decay_t<F>>::call(
static_cast<F&&>(f), static_cast<Args&&>(args)...);
}
template<class R, class F, class... Args,
class E = decltype(invoke_impl<std::decay_t<F>>::call(
std::declval<F>(), std::declval<Args>()...)),
class = std::enable_if_t<std::is_void_v<R> || std::is_convertible_v<E,R>>
>
constexpr R invoke(F&& f, Args&&... args)
noexcept(noexcept(invoke_impl<std::decay_t<F>>::call(
static_cast<F&&>(f), static_cast<Args&&>(args)...)) &&
(std::is_void_v<R> || std::is_nothrow_convertible_v<E,R>))
{
if constexpr (!std::is_void_v<R>)
return invoke_impl<std::decay_t<F>>::call(
static_cast<F&&>(f), static_cast<Args&&>(args)...);
else
invoke_impl<std::decay_t<F>>::call(
static_cast<F&&>(f), static_cast<Args&&>(args)...);
}