-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfunction_traits.h
79 lines (63 loc) · 2.55 KB
/
function_traits.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
#ifndef FUNCTION_TRAITS
#define FUNCTION_TRAITS
#include <functional>
#include "VariadicTypedef.h"
namespace details
{
template <typename ReturnType, typename... Args>
struct function_traits_helper
{
typedef variadic_typedef<Args...> args;
enum { arity = sizeof...(Args) };
typedef ReturnType result_type;
template <size_t i>
struct arg
{
using type = typename std::tuple_element<i, std::tuple<Args...>>::type;
};
};
template<typename T>
struct lambda_traits: public lambda_traits<decltype(&T::operator())> {};
template<typename ClassType, typename ReturnType, typename... Args>
struct lambda_traits<ReturnType(ClassType::*)(Args...) const> : public function_traits_helper<ReturnType, Args...>
{
typedef std::function<ReturnType(Args...)> std_function;
typedef ReturnType (*function)(Args...);
static ReturnType call(function f, Args&&... a)
{
return (*f)(std::forward<Args>(a)...);
}
};
}
template <typename T>
struct function_traits
: public details::lambda_traits<T> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const>: public details::function_traits_helper<ReturnType, Args...>
{
typedef ClassType class_type;
typedef const ClassType object_type;
typedef ReturnType(ClassType::*function)(Args...) const;
static ReturnType call(object_type *object, function f, Args&&... a) {return (object->*f)(std::forward<Args>(a)...);}
static ReturnType call(object_type &object, function f, Args&&... a) {return (object.*f)(std::forward<Args>(a)...);}
};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...)>: public details::function_traits_helper<ReturnType, Args...>
{
typedef ClassType class_type;
typedef ClassType object_type;
typedef ReturnType(ClassType::*function)(Args...);
static ReturnType call(object_type *object, function f, Args&&... a) {return (object->*f)(std::forward<Args>(a)...);}
static ReturnType call(object_type &object, function f, Args&&... a) {return (object.*f)(std::forward<Args>(a)...);}
};
template <typename ReturnType, typename... Args>
struct function_traits<ReturnType (*)(Args...)>: public details::function_traits_helper<ReturnType, Args...>
{
typedef std::function<ReturnType(Args...)> std_function;
typedef ReturnType (*function)(Args...);
static ReturnType call(function f, Args&&... a)
{
(*f)(std::forward<Args>(a)...);
}
};
#endif // FUNCTION_TRAITS