rclcpp  master
C++ ROS Client Library API
function_traits.hpp
Go to the documentation of this file.
1 // Copyright 2014 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__FUNCTION_TRAITS_HPP_
16 #define RCLCPP__FUNCTION_TRAITS_HPP_
17 
18 #include <functional>
19 #include <memory>
20 #include <tuple>
21 
22 namespace rclcpp
23 {
24 
25 namespace function_traits
26 {
27 
28 /* NOTE(esteve):
29  * We support service callbacks that can optionally take the request id,
30  * which should be possible with two overloaded create_service methods,
31  * but unfortunately std::function's constructor on VS2015 is too greedy,
32  * so we need a mechanism for checking the arity and the type of each argument
33  * in a callback function.
34  * See http://blogs.msdn.com/b/vcblog/archive/2015/06/19/c-11-14-17-features-in-vs-2015-rtm.aspx
35  */
36 
37 // Remove the first item in a tuple
38 template<typename T>
39 struct tuple_tail;
40 
41 template<typename Head, typename ... Tail>
42 struct tuple_tail<std::tuple<Head, Tail ...>>
43 {
44  using type = std::tuple<Tail ...>;
45 };
46 
47 // std::function
48 template<typename FunctionT>
50 {
51  using arguments = typename tuple_tail<
53 
54  static constexpr std::size_t arity = std::tuple_size<arguments>::value;
55 
56  template<std::size_t N>
57  using argument_type = typename std::tuple_element<N, arguments>::type;
58 
60 };
61 
62 // Free functions
63 template<typename ReturnTypeT, typename ... Args>
64 struct function_traits<ReturnTypeT(Args ...)>
65 {
66  using arguments = std::tuple<Args ...>;
67 
68  static constexpr std::size_t arity = std::tuple_size<arguments>::value;
69 
70  template<std::size_t N>
71  using argument_type = typename std::tuple_element<N, arguments>::type;
72 
73  using return_type = ReturnTypeT;
74 };
75 
76 // Function pointers
77 template<typename ReturnTypeT, typename ... Args>
78 struct function_traits<ReturnTypeT (*)(Args ...)>: function_traits<ReturnTypeT(Args ...)>
79 {};
80 
81 // std::bind for object methods
82 template<typename ClassT, typename ReturnTypeT, typename ... Args, typename ... FArgs>
83 #if defined _LIBCPP_VERSION // libc++ (Clang)
84 struct function_traits<std::__bind<ReturnTypeT (ClassT::*)(Args ...), FArgs ...>>
85 #elif defined _GLIBCXX_RELEASE // glibc++ (GNU C++ >= 7.1)
86 struct function_traits<std::_Bind<ReturnTypeT(ClassT::*(FArgs ...))(Args ...)>>
87 #elif defined __GLIBCXX__ // glibc++ (GNU C++)
88 struct function_traits<std::_Bind<std::_Mem_fn<ReturnTypeT (ClassT::*)(Args ...)>(FArgs ...)>>
89 #elif defined _MSC_VER // MS Visual Studio
90 struct function_traits<
91  std::_Binder<std::_Unforced, ReturnTypeT(__cdecl ClassT::*)(Args ...), FArgs ...>
92 >
93 #else
94 #error "Unsupported C++ compiler / standard library"
95 #endif
96  : function_traits<ReturnTypeT(Args ...)>
97 {};
98 
99 // std::bind for object const methods
100 template<typename ClassT, typename ReturnTypeT, typename ... Args, typename ... FArgs>
101 #if defined _LIBCPP_VERSION // libc++ (Clang)
102 struct function_traits<std::__bind<ReturnTypeT (ClassT::*)(Args ...) const, FArgs ...>>
103 #elif defined _GLIBCXX_RELEASE // glibc++ (GNU C++ >= 7.1)
104 struct function_traits<std::_Bind<ReturnTypeT(ClassT::*(FArgs ...))(Args ...) const>>
105 #elif defined __GLIBCXX__ // glibc++ (GNU C++)
106 struct function_traits<std::_Bind<std::_Mem_fn<ReturnTypeT (ClassT::*)(Args ...) const>(FArgs ...)>>
107 #elif defined _MSC_VER // MS Visual Studio
108 struct function_traits<
109  std::_Binder<std::_Unforced, ReturnTypeT(__cdecl ClassT::*)(Args ...) const, FArgs ...>
110 >
111 #else
112 #error "Unsupported C++ compiler / standard library"
113 #endif
114  : function_traits<ReturnTypeT(Args ...)>
115 {};
116 
117 // std::bind for free functions
118 template<typename ReturnTypeT, typename ... Args, typename ... FArgs>
119 #if defined _LIBCPP_VERSION // libc++ (Clang)
120 struct function_traits<std::__bind<ReturnTypeT( &)(Args ...), FArgs ...>>
121 #elif defined __GLIBCXX__ // glibc++ (GNU C++)
122 struct function_traits<std::_Bind<ReturnTypeT(*(FArgs ...))(Args ...)>>
123 #elif defined _MSC_VER // MS Visual Studio
124 struct function_traits<std::_Binder<std::_Unforced, ReturnTypeT(__cdecl &)(Args ...), FArgs ...>>
125 #else
126 #error "Unsupported C++ compiler / standard library"
127 #endif
128  : function_traits<ReturnTypeT(Args ...)>
129 {};
130 
131 // Lambdas
132 template<typename ClassT, typename ReturnTypeT, typename ... Args>
133 struct function_traits<ReturnTypeT (ClassT::*)(Args ...) const>
134  : function_traits<ReturnTypeT(ClassT &, Args ...)>
135 {};
136 
137 template<typename FunctionT>
139 {};
140 
141 template<typename FunctionT>
143 {};
144 
145 /* NOTE(esteve):
146  * VS2015 does not support expression SFINAE, so we're using this template to evaluate
147  * the arity of a function.
148  */
149 template<std::size_t Arity, typename FunctorT>
151  bool, (Arity == function_traits<FunctorT>::arity)> {};
152 
153 template<typename FunctorT, typename ... Args>
155  typename function_traits<FunctorT>::arguments,
156  std::tuple<Args ...>
157 >
158 {};
159 
160 template<typename FunctorAT, typename FunctorBT>
162  typename function_traits<FunctorAT>::arguments,
163  typename function_traits<FunctorBT>::arguments
164 >
165 {};
166 
167 } // namespace function_traits
168 
169 } // namespace rclcpp
170 
171 #endif // RCLCPP__FUNCTION_TRAITS_HPP_
Definition: function_traits.hpp:154
typename std::tuple_element< N, arguments >::type argument_type
Definition: function_traits.hpp:71
typename tuple_tail< typename function_traits< decltype(&FunctionT::operator())>::arguments >::type arguments
Definition: function_traits.hpp:52
Definition: function_traits.hpp:49
typename function_traits< decltype(&ReturnTypeT(ClassT &, Args ...) ::operator())>::return_type return_type
Definition: function_traits.hpp:59
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
ReturnTypeT return_type
Definition: function_traits.hpp:73
Definition: function_traits.hpp:161
Definition: function_traits.hpp:150
Definition: function_traits.hpp:39
typename std::tuple_element< N, arguments >::type argument_type
Definition: function_traits.hpp:57