-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfunction_traits.hpp
More file actions
180 lines (144 loc) · 6.04 KB
/
function_traits.hpp
File metadata and controls
180 lines (144 loc) · 6.04 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
#pragma once
#include <stdx/type_traits.hpp>
#include <boost/mp11/algorithm.hpp>
#include <boost/mp11/utility.hpp>
#include <type_traits>
#include <utility>
namespace stdx {
inline namespace v1 {
namespace detail {
template <auto> struct any_type {
// NOLINTNEXTLINE(google-explicit-constructor)
template <typename T> operator T();
};
template <typename F> struct function_traits;
template <typename R, typename... Args> struct function_traits<R(Args...)> {
using return_type = R;
template <template <typename...> typename List> using args = List<Args...>;
template <template <typename...> typename List>
using decayed_args = List<std::decay_t<Args>...>;
using arity = std::integral_constant<std::size_t, sizeof...(Args)>;
using obj_t = void;
template <auto N> using nth_arg = nth_t<N, Args...>;
template <auto N> using decayed_nth_arg = std::decay_t<nth_arg<N>>;
template <std::size_t... Is>
constexpr static auto invoke(std::index_sequence<Is...>)
-> std::invoke_result_t<R(Args...), any_type<Is>...>;
};
template <typename R, typename... Args>
struct function_traits<R (*)(Args...)> : function_traits<R(Args...)> {};
template <typename R, typename... Args>
struct function_traits<R (*const)(Args...)> : function_traits<R(Args...)> {};
template <typename R, typename... Args>
struct function_traits<R (*volatile)(Args...)> : function_traits<R(Args...)> {};
template <typename R, typename... Args>
struct function_traits<R (*const volatile)(Args...)>
: function_traits<R(Args...)> {};
template <typename C, typename R, typename... Args>
struct function_traits<R (C::*)(Args...)> : function_traits<R(Args...)> {
using obj_t = C;
};
template <typename C, typename R, typename... Args>
struct function_traits<R (C::*)(Args...) &> : function_traits<R(Args...)> {
using obj_t = C &;
};
template <typename C, typename R, typename... Args>
struct function_traits<R (C::*)(Args...) const &>
: function_traits<R(Args...)> {
using obj_t = C const &;
};
template <typename C, typename R, typename... Args>
struct function_traits<R (C::*)(Args...) volatile &>
: function_traits<R(Args...)> {
using obj_t = C volatile &;
};
template <typename C, typename R, typename... Args>
struct function_traits<R (C::*)(Args...) const volatile &>
: function_traits<R(Args...)> {
using obj_t = C const volatile &;
};
template <typename C, typename R, typename... Args>
struct function_traits<R (C::*)(Args...) &&> : function_traits<R(Args...)> {
using obj_t = C &&;
};
template <typename C, typename R, typename... Args>
struct function_traits<R (C::*)(Args...) const &&>
: function_traits<R(Args...)> {
using obj_t = C const &&;
};
template <typename C, typename R, typename... Args>
struct function_traits<R (C::*)(Args...) volatile &&>
: function_traits<R(Args...)> {
using obj_t = C volatile &&;
};
template <typename C, typename R, typename... Args>
struct function_traits<R (C::*)(Args...) const volatile &&>
: function_traits<R(Args...)> {
using obj_t = C const volatile &&;
};
template <typename C, typename R, typename... Args>
struct function_traits<R (C::*)(Args...) const> : function_traits<R(Args...)> {
using obj_t = C const;
};
template <typename C, typename R, typename... Args>
struct function_traits<R (C::*)(Args...) volatile>
: function_traits<R(Args...)> {
using obj_t = C volatile;
};
template <typename C, typename R, typename... Args>
struct function_traits<R (C::*)(Args...) const volatile>
: function_traits<R(Args...)> {
using obj_t = C const volatile;
};
template <typename F, typename = void> struct detect_call_operator {
template <std::size_t... Is>
constexpr static auto invoke(std::index_sequence<Is...>) ->
typename boost::mp11::mp_cond<
boost::mp11::mp_valid<std::invoke_result_t, F &&, any_type<Is>...>,
boost::mp11::mp_defer<std::invoke_result_t, F &&, any_type<Is>...>,
boost::mp11::mp_valid<std::invoke_result_t, F &, any_type<Is>...>,
boost::mp11::mp_defer<std::invoke_result_t, F &,
any_type<Is>...>>::type;
};
template <typename F>
struct detect_call_operator<
F, std::void_t<decltype(&remove_cvref_t<F>::operator())>>
: function_traits<decltype(&remove_cvref_t<F>::operator())> {};
template <typename F> struct function_traits : detect_call_operator<F> {};
} // namespace detail
template <typename F> using function_traits = detail::function_traits<F>;
template <typename F> using return_t = typename function_traits<F>::return_type;
template <typename F, template <typename...> typename List>
using args_t = typename function_traits<F>::template args<List>;
template <typename F, template <typename...> typename List>
using decayed_args_t = typename function_traits<F>::template decayed_args<List>;
template <typename F>
using nongeneric_arity_t = typename function_traits<F>::arity;
template <typename F> using obj_arg_t = typename function_traits<F>::obj_t;
template <typename F, auto N>
using nth_arg_t = typename function_traits<F>::template nth_arg<N>;
template <typename F, auto N>
using decayed_nth_arg_t =
typename function_traits<F>::template decayed_nth_arg<N>;
namespace detail {
template <typename F, typename N>
using try_invoke =
decltype(function_traits<F>::invoke(std::make_index_sequence<N::value>{}));
template <typename F, typename N>
using has_arg_count = boost::mp11::mp_valid<try_invoke, F, N>;
template <typename F, typename N> struct generic_arity;
template <typename F, typename N>
using generic_arity_t = typename generic_arity<F, N>::type;
template <typename F, typename N> struct generic_arity {
using type = boost::mp11::mp_eval_if<
has_arg_count<F, N>, N, generic_arity_t, F,
std::integral_constant<std::size_t, N::value + 1u>>;
};
} // namespace detail
template <typename F>
using arity_t = boost::mp11::mp_eval_or<
detail::generic_arity_t<F, std::integral_constant<std::size_t, 0u>>,
nongeneric_arity_t, F>;
template <typename F> constexpr auto arity_v = arity_t<F>::value;
} // namespace v1
} // namespace stdx