-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfunc.h
225 lines (189 loc) · 5.09 KB
/
func.h
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
#pragma once
#include <tuple>
#include <type_traits>
#include <algorithm>
template <typename TF,
typename TArgs,
typename TBound>
struct Func{};
template <typename TF, typename... TArgs>
struct Func<TF, std::tuple<TArgs...>, std::tuple<TF*, TArgs...>>
{
std::tuple<TF*, TArgs...> bound;
auto operator() ()
{
return std::apply(fwd, bound);
}
static auto fwd(TF* f, TArgs... args)
{
return f(args...);
}
};
template <typename TF,
typename... TArgs,
typename... TBounds>
struct Func<TF, std::tuple<TArgs...>, std::tuple<TF*, TBounds...>>
{
using result_of = std::invoke_result_t<TF, TArgs...>;
using tuple_args = std::tuple<TArgs...>;
using tuple_bound = std::tuple<TF*, TBounds...>;
constexpr static size_t sizeofArgs = sizeof...(TArgs);
constexpr static size_t sizeofBounds = sizeof...(TBounds);
using next_argument = std::tuple_element_t<sizeofBounds, tuple_args>;
tuple_bound bound;
template <size_t OFFSET,
typename TAs,
typename TBs,
typename SQ>
struct types_match {};
template <size_t OFFSET,
typename TAs,
typename TBs,
size_t... IA>
struct types_match<OFFSET,
TAs,
TBs,
std::index_sequence<IA...>
>
{
using type = std::conjunction<
std::is_same<
std::tuple_element_t<OFFSET + IA, TAs>,
std::tuple_element_t<IA, TBs>
>...
>;
};
template <
typename... TBinds,
size_t QTD = sizeof...(TBinds)
>
using can_invoke_with = std::enable_if_t
<
(QTD == (sizeofArgs - sizeofBounds)) &&
(types_match< sizeofBounds, tuple_args, std::tuple<TBinds...>, std::make_index_sequence<sizeof...(TBinds)>>::type::value)
>;
template <
typename... TBinds,
size_t QTD = sizeof...(TBinds)
>
using can_bind_with = std::enable_if_t
<
(QTD <= (sizeofArgs - sizeofBounds)) &&
(types_match< sizeofBounds, tuple_args, std::tuple<TBinds...>, std::make_index_sequence<sizeof...(TBinds)>>::type::value)
>;
template <
typename... TBinds,
size_t QTD = sizeof...(TBinds),
typename = can_bind_with<TBinds...>
>
auto operator() (TBinds... binds) const
{
auto newf = Func<TF, tuple_args,
std::tuple<TF*, TBounds..., TBinds...>
>
{
std::tuple_cat(bound, std::make_tuple(binds...))
};
if constexpr (QTD == (sizeofArgs - sizeofBounds))
return newf();
else
return newf;
}
template <
typename... TBinds,
size_t QTD = sizeof...(TBinds),
typename = std::enable_if_t<QTD <= (sizeofArgs - sizeofBounds)>,
typename = std::enable_if_t<
types_match<
sizeofBounds,
tuple_args,
std::tuple<TBinds...>,
std::make_index_sequence<sizeof...(TBinds)>
>::type::value
>
>
auto operator << (TBinds... binds)
{
auto newf = Func<TF, tuple_args,
std::tuple<TF*, TBounds..., TBinds...>
>
{
std::tuple_cat(bound, std::make_tuple(binds...))
};
return newf;
}
};
template <typename TF>
struct make_f
{
template<typename... TArgs> struct types { using type = std::tuple<TArgs...>; };
template<class Sig>
struct args;
template<class R, class...Args>
struct args<R (Args...)> : types<Args...>
{
};
template<class R, class...Args>
struct args<R (*)(Args...)> : types<Args...>
{
};
template<class Sig> using args_t = typename args<Sig>::type;
using type = Func<TF, args_t<TF>, std::tuple<TF*>>;
};
template <typename Fs1, typename... Fs>
struct pipeline
{
using functions_type = std::tuple<
Fs1,
Fs...
>;
functions_type fs;
using data_type = std::tuple<
typename Fs1::next_argument,
typename Fs1::result_of,
typename Fs::result_of...
>;
data_type t;
pipeline(Fs1 fs1, Fs... fs) : fs{fs1, fs...}, t{}
{
}
auto operator() (typename Fs1::next_argument arg)
{
std::get<0>(t) = arg;
call_pipe(t, fs,
std::make_index_sequence<sizeof...(Fs) + 1>{}
);
return std::get<std::tuple_size_v<data_type> - 1>(t);
}
template <size_t... Is>
void call_pipe(data_type& t,
functions_type& fs,
std::index_sequence<Is...> s)
{
((
std::get<Is + 1>(t) =
std::get<Is>(fs)
( std::get<Is>(t) )
),...);
}
};
#ifdef FUNC_BODY
template <typename TF>
auto $(TF* f)
{
return typename make_f<TF>::type
{
std::make_tuple(f)
};
}
template <typename Fs1, typename... Fs>
auto $$(Fs1 fs1, Fs... fs)
{
return pipeline{fs1, fs...};
}
#else
template <typename TF>
auto $(TF* f);
template <typename Fs1, typename... Fs>
auto $$(Fs1 fs1, Fs... fs);
#endif