ROS 2 rclcpp + rcl - kilted  kilted
ROS 2 C++ Client Library with ROS Client Library
function_traits.hpp
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<
52  typename function_traits<decltype( &FunctionT::operator())>::arguments>::type;
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 
59  using return_type = typename function_traits<decltype( &FunctionT::operator())>::return_type;
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 DOXYGEN_ONLY
84 struct function_traits<std::bind<ReturnTypeT (ClassT::*)(Args ...), FArgs ...>>
85 #elif defined _LIBCPP_VERSION // libc++ (Clang)
86 struct function_traits<std::__bind<ReturnTypeT (ClassT::*)(Args ...), FArgs ...>>
87 #elif defined _GLIBCXX_RELEASE // glibc++ (GNU C++ >= 7.1)
88 struct function_traits<std::_Bind<ReturnTypeT(ClassT::* (FArgs ...))(Args ...)>>
89 #elif defined __GLIBCXX__ // glibc++ (GNU C++)
90 struct function_traits<std::_Bind<std::_Mem_fn<ReturnTypeT (ClassT::*)(Args ...)>(FArgs ...)>>
91 #elif defined _MSC_VER // MS Visual Studio
92 struct function_traits<
93  std::_Binder<std::_Unforced, ReturnTypeT (ClassT::*)(Args ...), FArgs ...>>
94 #else
95 #error "Unsupported C++ compiler / standard library"
96 #endif
97  : function_traits<ReturnTypeT(Args ...)>
98 {};
99 
100 // std::bind for object const methods
101 template<typename ClassT, typename ReturnTypeT, typename ... Args, typename ... FArgs>
102 #if defined DOXYGEN_ONLY
103 struct function_traits<std::bind<ReturnTypeT (ClassT::*)(Args ...) const, FArgs ...>>
104 #elif defined _LIBCPP_VERSION // libc++ (Clang)
105 struct function_traits<std::__bind<ReturnTypeT (ClassT::*)(Args ...) const, FArgs ...>>
106 #elif defined _GLIBCXX_RELEASE // glibc++ (GNU C++ >= 7.1)
107 struct function_traits<std::_Bind<ReturnTypeT(ClassT::* (FArgs ...))(Args ...) const>>
108 #elif defined __GLIBCXX__ // glibc++ (GNU C++)
109 struct function_traits<std::_Bind<std::_Mem_fn<ReturnTypeT (ClassT::*)(Args ...) const>(FArgs ...)>>
110 #elif defined _MSC_VER // MS Visual Studio
111 struct function_traits<
112  std::_Binder<std::_Unforced, ReturnTypeT (ClassT::*)(Args ...) const, FArgs ...>>
113 #else
114 #error "Unsupported C++ compiler / standard library"
115 #endif
116  : function_traits<ReturnTypeT(Args ...)>
117 {};
118 
119 // std::bind for free functions
120 template<typename ReturnTypeT, typename ... Args, typename ... FArgs>
121 #if defined DOXYGEN_ONLY
122 struct function_traits<std::bind<ReturnTypeT( &)(Args ...), FArgs ...>>
123 #elif defined _LIBCPP_VERSION // libc++ (Clang)
124 struct function_traits<std::__bind<ReturnTypeT( &)(Args ...), FArgs ...>>
125 #elif defined __GLIBCXX__ // glibc++ (GNU C++)
126 struct function_traits<std::_Bind<ReturnTypeT(*(FArgs ...))(Args ...)>>
127 #elif defined _MSC_VER // MS Visual Studio
128 struct function_traits<std::_Binder<std::_Unforced, ReturnTypeT( &)(Args ...), FArgs ...>>
129 #else
130 #error "Unsupported C++ compiler / standard library"
131 #endif
132  : function_traits<ReturnTypeT(Args ...)>
133 {};
134 
135 // Lambdas
136 template<typename ClassT, typename ReturnTypeT, typename ... Args>
137 struct function_traits<ReturnTypeT (ClassT::*)(Args ...) const>
138  : function_traits<ReturnTypeT(ClassT &, Args ...)>
139 {};
140 
141 template<typename FunctionT>
143 {};
144 
145 template<typename FunctionT>
147 {};
148 
149 /* NOTE(esteve):
150  * VS2015 does not support expression SFINAE, so we're using this template to evaluate
151  * the arity of a function.
152  */
153 template<std::size_t Arity, typename FunctorT>
154 struct arity_comparator : std::integral_constant<
155  bool, (Arity == function_traits<FunctorT>::arity)> {};
156 
157 template<typename FunctorT, typename ... Args>
158 struct check_arguments : std::is_same<
159  typename function_traits<FunctorT>::arguments,
160  std::tuple<Args ...>
161 >
162 {};
163 
164 template<typename FunctorAT, typename FunctorBT>
165 struct same_arguments : std::is_same<
166  typename function_traits<FunctorAT>::arguments,
167  typename function_traits<FunctorBT>::arguments
168 >
169 {};
170 
171 namespace detail
172 {
173 
174 template<typename ReturnTypeT, typename ... Args>
176 
177 template<typename ReturnTypeT, typename ... Args>
178 struct as_std_function_helper<ReturnTypeT, std::tuple<Args ...>>
179 {
180  using type = std::function<ReturnTypeT(Args ...)>;
181 };
182 
183 } // namespace detail
184 
185 template<
186  typename FunctorT,
187  typename FunctionTraits = function_traits<FunctorT>
188 >
190 {
191  using type = typename detail::as_std_function_helper<
192  typename FunctionTraits::return_type,
193  typename FunctionTraits::arguments
194  >::type;
195 };
196 
197 } // namespace function_traits
198 
199 } // namespace rclcpp
200 
201 #endif // RCLCPP__FUNCTION_TRAITS_HPP_
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.