rttr  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
function_traits.h
Go to the documentation of this file.
1 /************************************************************************************
2 * *
3 * Copyright (c) 2014 Axel Menzel <info@axelmenzel.de> *
4 * *
5 * This file is part of RTTR (Run Time Type Reflection) *
6 * License: MIT License *
7 * *
8 * Permission is hereby granted, free of charge, to any person obtaining *
9 * a copy of this software and associated documentation files (the "Software"), *
10 * to deal in the Software without restriction, including without limitation *
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
12 * and/or sell copies of the Software, and to permit persons to whom the *
13 * Software is furnished to do so, subject to the following conditions: *
14 * *
15 * The above copyright notice and this permission notice shall be included in *
16 * all copies or substantial portions of the Software. *
17 * *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
24 * SOFTWARE. *
25 * *
26 *************************************************************************************/
27 
28 #ifndef __RTTR_FUNCTION_TRAITS_H__
29 #define __RTTR_FUNCTION_TRAITS_H__
30 
32 
33 #include <type_traits>
34 #include <functional>
35 #include <tuple>
36 
37 namespace rttr
38 {
39 namespace detail
40 {
41 
43 
44  template<typename T>
45  struct is_std_function : std::false_type
46  {
47  typedef T signature;
48  };
49 
50  template<typename T>
51  struct is_std_function<std::function<T>> : std::true_type
52  {
53  typedef T signature;
54  };
55 
56  template<typename T>
57  struct get_std_function_signature
58  {
59  typedef T type;
60  };
61 
62  template<typename T>
63  struct get_std_function_signature<std::function<T>>
64  {
65  typedef T type;
66  };
67 
69  template<typename T>
70  struct is_function_ptr : std::integral_constant<bool, std::is_pointer<T>::value &&
71  std::is_function<typename std::remove_pointer<T>::type>::value>
72  {
73  };
74 
77 
79 
80  template<typename... Args>
81  struct function_args
82  {
83  typedef std::tuple<Args...> arg_types;
84  };
85 
87 
88  template<typename T>
89  struct function_traits_func_ptr;
90 
91  template<typename R, typename... Args>
92  struct function_traits_func_ptr<R (*)(Args...)> : function_args<Args...>
93  {
94  static const size_t arg_count = sizeof...(Args);
95  typedef R return_type;
96  };
97 
99 
100  template<typename T>
101  struct function_traits_mem_ptr;
102 
103  template<typename R, typename C, typename... Args>
104  struct function_traits_mem_ptr<R (C::*)(Args...)> : function_args<Args...>
105  {
106  static const size_t arg_count = sizeof...(Args);
107  typedef R return_type;
108  typedef C class_type;
109  };
110 
111  template<typename R, typename C, typename... Args>
112  struct function_traits_mem_ptr<R (C::*)(Args...) const> : function_args<Args...>
113  {
114  static const size_t arg_count = sizeof...(Args);
115  typedef R return_type;
116  typedef C class_type;
117  };
118 
119  template<typename R, typename C, typename... Args>
120  struct function_traits_mem_ptr<R (C::*)(Args...) const volatile> : function_args<Args...>
121  {
122  static const size_t arg_count = sizeof...(Args);
123  typedef R return_type;
124  typedef C class_type;
125  };
126 
130 
131  template<typename T>
132  struct function_traits : std::conditional<std::is_member_function_pointer<T>::value,
133  function_traits_mem_ptr<T>,
134  typename std::conditional<std::is_function<T>::value,
135  function_traits_func_ptr<typename std::add_pointer<T>::type>,
136  typename std::conditional<is_std_function<T>::value,
137  function_traits_func_ptr<typename std::add_pointer<typename get_std_function_signature<T>::type>::type>,
138  function_traits_func_ptr<T>
139  >::type
140  >::type
141  >::type
142  {
143  };
144 
146 
147  template<typename T, size_t Index>
148  struct param_types
149  {
150  typedef typename std::tuple_element<Index, typename function_traits<T>::arg_types>::type type;
151  };
152 
154  // use it like e.g:
155  // param_types<F, 0>::type
156 
157  template<typename T>
158  struct is_void_func : std::conditional<std::is_same<typename function_traits<T>::return_type, void>::value,
159  std::true_type,
160  std::false_type
161  >::type
162  {};
163 } // end namespace detail
164 } // end namespace rttr
165 
166 #endif // __RTTR_FUNCTION_TRAITS_H__