Repository navigation
Expand file tree
/
Copy pathfunction_traits.hpp
More file actions
88 lines (74 loc) · 2.98 KB
/
Copy pathfunction_traits.hpp
File metadata and controls
88 lines (74 loc) · 2.98 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
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
#pragma once
#include <type_traits>
#include <functional>
#include <tuple>
//member function
#define TIMAX_FUNCTION_TRAITS(...)\
template <typename ReturnType, typename ClassType, typename... Args>\
struct function_traits_impl<ReturnType(ClassType::*)(Args...) __VA_ARGS__> : function_traits_impl<ReturnType(Args...)>{};\
namespace timax
{
/*
* 1. function type ==> Ret(Args...)
* 2. function pointer ==> Ret(*)(Args...)
* 3. function reference ==> Ret(&)(Args...)
* 4. pointer to non-static member function ==> Ret(T::*)(Args...)
* 5. function object and functor ==> &T::operator()
* 6. function with generic operator call ==> template <typeanme ... Args> &T::operator()
*/
template <typename T>
struct function_traits_impl;
template<typename T>
struct function_traits : function_traits_impl<
std::remove_cv_t<std::remove_reference_t<T>>>
{};
template<typename Ret, typename... Args>
struct function_traits_impl<Ret(Args...)>
{
public:
enum { arity = sizeof...(Args) };
typedef Ret function_type(Args...);
typedef Ret result_type;
using stl_function_type = std::function<function_type>;
typedef Ret(*pointer)(Args...);
template<size_t I>
struct args
{
static_assert(I < arity, "index is out of range, index must less than sizeof Args");
using type = typename std::tuple_element<I, std::tuple<Args...>>::type;
};
typedef std::tuple<std::remove_cv_t<std::remove_reference_t<Args>>...> tuple_type;
using args_type_t = std::tuple<Args...>;
};
template<size_t I, typename Function>
using arg_type = typename function_traits<Function>::template args<I>::type;
// function pointer
template<typename Ret, typename... Args>
struct function_traits_impl<Ret(*)(Args...)> : function_traits<Ret(Args...)> {};
// std::function
template <typename Ret, typename... Args>
struct function_traits_impl<std::function<Ret(Args...)>> : function_traits_impl<Ret(Args...)> {};
// pointer of non-static member function
TIMAX_FUNCTION_TRAITS()
TIMAX_FUNCTION_TRAITS(const)
TIMAX_FUNCTION_TRAITS(volatile)
TIMAX_FUNCTION_TRAITS(const volatile)
// functor
template<typename Callable>
struct function_traits_impl : function_traits_impl<decltype(&Callable::operator())> {};
template <typename Function>
typename function_traits<Function>::stl_function_type to_function(const Function& lambda)
{
return static_cast<typename function_traits<Function>::stl_function_type>(lambda);
}
template <typename Function>
typename function_traits<Function>::stl_function_type to_function(Function&& lambda)
{
return static_cast<typename function_traits<Function>::stl_function_type>(std::forward<Function>(lambda));
}
template <typename Function>
typename function_traits<Function>::pointer to_function_pointer(const Function& lambda)
{
return static_cast<typename function_traits<Function>::pointer>(lambda);
}
}