-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
182 lines (146 loc) · 5.45 KB
/
main.cpp
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
#include <iostream>
#include <utility>
#include <regex>
namespace naive
{
template <int INDEX, typename C, typename ...TYPES>
struct ArgAtIndex : public ArgAtIndex<INDEX - 1, TYPES...>
{};
template <typename C, typename ...TYPES>
struct ArgAtIndex<0, C, TYPES...>
{
using type = C;
};
template <size_t INDEX, typename T>
struct Argument
{
Argument(T&& v): value(v)
{}
T value;
};
template <typename T, typename ...ARGS>
struct ArgHolderBuilder
{};
template <typename ...ARGS, size_t ...INDEXES>
struct ArgHolderBuilder<std::index_sequence<INDEXES...>, ARGS...>
{
struct ArgsHolder: public Argument<INDEXES, ARGS>...
{
ArgsHolder(ARGS&& ...args): Argument<INDEXES, ARGS>(std::forward<ARGS>(args))...
{}
};
};
//-----------------------------------------------------------------------
struct FunctorInvokeHelper
{
template <typename FUNC, typename ...ARGS>
static auto invoke(FUNC& func, ARGS&& ...args) -> void /* decltype(func(std::forward<ARGS>(args)...))*/
{
return func(std::forward<ARGS>(args)...);
}
};
struct MemberInvokeHelper
{
template <typename FUNC, typename OBJ, typename ...ARGS>
static auto invoke(FUNC& fn, OBJ&& obj, ARGS&& ...args) -> decltype((obj.*fn)(std::forward<ARGS>(args)...))
{
return (obj.*fn)(std::forward<ARGS>(args)...);
}
};
//-----------------------------------------------------------------------
template <size_t INDEX, typename T, typename ...ARGS>
struct TakeValue : public TakeValue<INDEX - 1, ARGS...>
{
using superclass_t = TakeValue<INDEX - 1, ARGS...>;
using result_t = typename superclass_t::result_t;
static result_t take(T&& t, ARGS&& ...other)
{
return superclass_t::take(std::forward<ARGS>(other)...);
}
};
template <typename T, typename ...ARGS>
struct TakeValue<0, T, ARGS...>
{
using result_t = T;
static T take(T&& v, ARGS&& ...other)
{
return std::forward<T>(v);
}
};
//-----------------------------------------------------------------------
template <typename T>
struct PlaceHolderIndex
{};
template <size_t INDEX>
struct PlaceHolderIndex<std::placeholders::__ph<INDEX>> : public std::integral_constant<size_t, INDEX>
{};
//-----------------------------------------------------------------------
template <size_t INDEX>
struct TakePlaceholder
{
template <typename DUMMY, typename ...PHS>
static auto take(const DUMMY&, PHS&& ...phs) -> typename ArgAtIndex<INDEX, PHS...>::type
{
return TakeValue<INDEX, PHS...>::take(std::forward<PHS>(phs)...);
}
};
template <size_t INDEX, typename ...ARGS>
struct TakeArgument
{
using args_holder_t = typename ArgHolderBuilder<std::make_index_sequence<sizeof...(ARGS)>, ARGS...>::ArgsHolder;
using result_t = typename ArgAtIndex<INDEX, ARGS...>::type;
template <typename ...DUMMY>
static auto take(args_holder_t& holder, DUMMY&& ...) -> result_t
{
using arg_t = Argument<INDEX, result_t>;
return static_cast<arg_t *>(&holder)->value;
}
};
template <typename T, size_t INDEX, typename ...ARGS>
struct Take : public std::conditional<std::is_placeholder<std::remove_reference_t<T>>::value,
TakePlaceholder<PlaceHolderIndex<std::remove_reference_t<T>>::value - 1>, TakeArgument<INDEX, ARGS...>>::type
{
};
//-----------------------------------------------------------------------
template <typename FUNC, typename ...ARGS>
struct Binder
{
using args_holder_t = typename ArgHolderBuilder<std::make_index_sequence<sizeof...(ARGS)>, ARGS...>::ArgsHolder;
using invoker_t = typename std::conditional<std::is_member_pointer<FUNC>::value, MemberInvokeHelper, FunctorInvokeHelper>::type;
Binder(FUNC& fn, ARGS&& ...args): m_func(fn), m_argsHolder(std::forward<ARGS>(args)...)
{}
template <typename ...PHS>
void operator() (PHS&& ...phs)
{
invoke_helper(std::make_index_sequence<sizeof...(ARGS)>(), std::forward<PHS>(phs)...);
}
template <size_t ...ARGIDX, typename ...PHS>
auto invoke_helper(std::index_sequence<ARGIDX...>, PHS&& ...phs) -> void
{
invoker_t::invoke(m_func, Take<typename ArgAtIndex<ARGIDX, ARGS...>::type, ARGIDX, ARGS...>::take(m_argsHolder, std::forward<PHS>(phs)...)...);
};
private:
FUNC m_func;
args_holder_t m_argsHolder;
};
template <typename FUNC, typename ...ARGS>
auto bind(FUNC& fn, ARGS&& ...args) -> Binder<FUNC, ARGS...>
{
return Binder<FUNC, ARGS...>(fn, std::forward<ARGS>(args)...);
};
}
int main()
{
// auto foo = [] (int a, char b, std::string s) {
// std::cout << "a=" << a << " b=" << b << " s=" << s << std::endl;
// };
// auto bar = naive::bind(foo, 42, 'b', "blah-blah-blah");
// bar(1234);
auto foo = [] (std::string a, std::string b, std::string c) {
std::cout << "a=" << a << ", b=" << b << ", c=" << c << std::endl;
};
auto baz = naive::bind(foo, std::placeholders::_1, std::placeholders::_1, std::placeholders::_1);
baz("zzzz");
std::cout << "Hello, World!" << std::endl;
return 0;
}