-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathbind.hh
More file actions
49 lines (40 loc) · 1.3 KB
/
Copy pathbind.hh
File metadata and controls
49 lines (40 loc) · 1.3 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
// Simple version of std::bind that doesn't support placeholders
#pragma once
#include <tuple>
#include <utility>
// __bind_call generates a list of integers 0 .. N-1 ...
template <int N, typename F, typename Res, typename TArgs, int... list>
struct __bind_call {
static Res call(F& f, TArgs& t) {
return __bind_call<N - 1, F, Res, TArgs, N - 1, list...>::call(f, t);
}
};
// ... which the __bind_call base case unpacks to get a sequence of
// tuple indexes to pass to std::get to extract the saved arguments
// from the tuple t.
template <typename F, typename Res, typename TArgs, int... list>
struct __bind_call<0, F, Res, TArgs, list...> {
static Res call(F& f, TArgs& t) {
return f(std::get<list>(t)...);
}
};
template <typename F, typename Res, typename... Args>
struct __bind_helper
{
typedef std::tuple<typename std::decay<Args>::type...> TArgs;
F f_;
TArgs args_;
__bind_helper(F&& f, Args&&... args) : f_(f), args_(args...) { }
Res operator()()
{
return __bind_call<sizeof...(Args), F, Res, TArgs>::
call(f_, args_);
}
};
template <typename F, typename... Args>
auto bind_simple(F&& f, Args&&... args)
-> __bind_helper<F, decltype(f(args...)), Args...>
{
return __bind_helper<F, decltype(f(args...)), Args...>(
std::forward<F>(f), std::forward<Args>(args)...);
}