-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp_function_binder.hpp
219 lines (183 loc) · 5.48 KB
/
exp_function_binder.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
#pragma once
#include"select_type.hpp"
#include"tuple_iterator_metainfo.hpp"
namespace exp_bind{
template<class F>
struct exp_function_binder {};
template<class R>
struct exp_function_binder<R()>
{
using func_type = R();
using type = std::function<R()>;
using return_type = R;
using argument_list_type = std::tuple<void*>;
std::tuple<void*> args_stack{};
type func_binder;
exp_function_binder(func_type _f) :func_binder(_f) {}
exp_function_binder(type stdfunc) :func_binder(stdfunc) {};
size_t argc_count() { return 0; }
R apply_func() { return func_binder(); }
R operator()() { return apply_func(); }
};
template<class R, class ...L>
struct exp_function_binder<R(L...)>
{
using type = std::function<R(L...)>;
using argument_list_type = exp_list<L...>;
using func_type = R(L...);
using return_type = R;
using argc_stack_type = tuple_preprocess::preprocess_tuple<std::tuple<L...>>;
exp_function_binder(func_type _f) :func_binder(_f) {}
exp_function_binder(type stdfunc) :func_binder(stdfunc) {}
exp_function_binder() {}
//Since EFB uses reference to arg_stack
//A default copy operation causes error
exp_function_binder(exp_function_binder&& efb) noexcept:
args_stack(efb.args_stack),
current_index(efb.current_index),
func_binder(efb.func_binder),
args_iter_keep(args_stack)
{}
exp_function_binder(const exp_function_binder& efb) :
args_stack(efb.args_stack),
current_index(efb.current_index),
func_binder(efb.func_binder),
args_iter_keep(args_stack)
{}
exp_function_binder& operator=(const exp_function_binder& efb)
{
func_binder = efb.func_binder;
return *this;
}
argc_stack_type args_stack{};
tuple_iterator<argc_stack_type> args_iter_keep{ args_stack };
type func_binder;
size_t current_index{ 0 };
size_t argc_count() { return args_iter_keep.iterator().size(); }
void reset_argc_stack() { current_index = 0; }
void rebind_back(size_t back_offset)
{
if (back_offset > current_index) throw std::exception("argument stack overflow!");
current_index -= back_offset;
}
typename tuple_iterator<argc_stack_type>::iterator_type& operator[](size_t index)
{
return args_iter_keep.iterator()[index];//return iterator of function stack
}
friend std::istream& operator>>(std::istream& ins, struct exp_function_binder<R(L...)>& efb)
{
ins >> efb[efb.current_index++];
return ins;
}
template<class T>
void bind(T &&val)
{
auto& iter = args_iter_keep.iterator();
if (current_index >= iter.size()) return;
iter[current_index++] = std::move(val);
}
template<class...L>
void bind_a_lot(L...l)
{
(bind(std::forward<L>(l)), ...);
}
R apply_func()
{
current_index = 0;
return std::apply([*this](auto&& ...l) { return func_binder(l...); }, std::move(args_stack));
}
template<class ...L>
R operator()(L...l)
{
bind_a_lot(l...);
return apply_func();
}
};
template<class T, class F>
struct member_function_wrapper
{};
template<class T, class R, class ...L>
struct member_function_wrapper<T, R(T::*)(L...)>
{
using func_type = R(T::*)(L...);
T& obj_ref;
func_type call;
member_function_wrapper(T& obj, func_type mf) :obj_ref(obj), call(mf) {}
template<class ...AL>
R apply(AL...al)
{
return (obj_ref.*call)(al...);
}
template<class ...AL>
R operator()(AL ...al)
{
return apply(al...);
}
};
template<class T, class R, class ...L>
using decl_mem_type = R(T::*)(L...);
template<class R, class ...L>
using decl_fn_type = R(L...);
template<class F>
struct fn_return_type
{
using type = exp_function_binder<F>::return_type;
};
template<class _std_fn>
struct _std_fn_unwrapper
{};
template<class fn>
struct _std_fn_unwrapper<std::function<fn>>
{
using fn_type = fn;
std::function<fn> fnc;
};
template<class T>
struct rem_ptr_impl{
using type = T;
};
template<class T>
struct rem_ptr_impl<T*>
{
using type = T;
};
template<class T>
using remove_pointer = typename rem_ptr_impl<T>::type;
template<auto f>
using fn_return = typename fn_return_type<
remove_pointer<decltype(f)>
>::type;
template<auto f, typename R>
using fn_return_is = std::is_same<fn_return<f>, R>;
template<class R, class ...L>
exp_function_binder<decl_fn_type<R, L...>> bind(decl_fn_type<R, L...> f)
{
return { f };
}
//lambda support, use std::function to convert lambda
template<class F>
auto bind(F && f)
{
std::function ff = f;
using func_form = _std_fn_unwrapper<decltype(ff)>::fn_type;
return exp_function_binder<func_form>{ff};
}
template<class T, class R, class ...L>
auto bind_mfw(T& obj, decl_mem_type<T, R, L...> mf)
{
return [&obj, mf](L...l)->R {return member_function_wrapper{ obj, mf }(l...); };
}
template<class T, class F>
auto bind(T& obj, F f)
{
return bind(bind_mfw(obj, f));
}
template<auto x>
using make_binder = exp_function_binder<remove_pointer<decltype(x)>>;
template<class T, class R, class ...L>
member_function_wrapper(T& obj, decl_mem_type<T, R, L...> mf) -> member_function_wrapper<T, R(T::*)(L...)>;
template<class R, class ...L>
exp_function_binder(decl_fn_type<R, L...> fn) -> exp_function_binder<decl_fn_type<R, L...>>;
template<class F>
exp_function_binder(std::function<F> f) -> exp_function_binder<typename _std_fn_unwrapper<typename decltype(f)>::fn_type>;
}