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::__1::__bind<ReturnTypeT (ClassT::*)(Args ...), FArgs ...>>
85 #elif defined __GLIBCXX__ // glibc++ (GNU C++)
86 struct function_traits<std::_Bind<std::_Mem_fn<ReturnTypeT (ClassT::*)(Args ...)>(FArgs ...)>>
87 #elif defined _MSC_VER // MS Visual Studio
88 struct function_traits<
89  std::_Binder<std::_Unforced, ReturnTypeT(__cdecl ClassT::*)(Args ...), FArgs ...>
90 >
91 #else
92 #error "Unsupported C++ compiler / standard library"
93 #endif
94  : function_traits<ReturnTypeT(Args ...)>
95 {};
96 
97 // std::bind for free functions
98 template<typename ReturnTypeT, typename ... Args, typename ... FArgs>
99 #if defined _LIBCPP_VERSION // libc++ (Clang)
100 struct function_traits<std::__1::__bind<ReturnTypeT( &)(Args ...), FArgs ...>>
101 #elif defined __GLIBCXX__ // glibc++ (GNU C++)
102 struct function_traits<std::_Bind<ReturnTypeT(*(FArgs ...))(Args ...)>>
103 #elif defined _MSC_VER // MS Visual Studio
104 struct function_traits<std::_Binder<std::_Unforced, ReturnTypeT(__cdecl &)(Args ...), FArgs ...>>
105 #else
106 #error "Unsupported C++ compiler / standard library"
107 #endif
108  : function_traits<ReturnTypeT(Args ...)>
109 {};
110 
111 // Lambdas
112 template<typename ClassT, typename ReturnTypeT, typename ... Args>
113 struct function_traits<ReturnTypeT (ClassT::*)(Args ...) const>
114  : function_traits<ReturnTypeT(ClassT &, Args ...)>
115 {};
116 
117 template<typename FunctionT>
119 {};
120 
121 template<typename FunctionT>
123 {};
124 
125 /* NOTE(esteve):
126  * VS2015 does not support expression SFINAE, so we're using this template to evaluate
127  * the arity of a function.
128  */
129 template<std::size_t Arity, typename FunctorT>
130 struct arity_comparator : std::integral_constant<
131  bool, (Arity == function_traits<FunctorT>::arity)>{};
132 
133 template<typename FunctorT, typename ... Args>
134 struct check_arguments : std::is_same<
135  typename function_traits<FunctorT>::arguments,
136  std::tuple<Args ...>
137  >
138 {};
139 
140 template<typename FunctorAT, typename FunctorBT>
141 struct same_arguments : std::is_same<
142  typename function_traits<FunctorAT>::arguments,
143  typename function_traits<FunctorBT>::arguments
144  >
145 {};
146 
147 } // namespace function_traits
148 
149 } // namespace rclcpp
150 
151 #endif // RCLCPP__FUNCTION_TRAITS_HPP_
Definition: function_traits.hpp:134
typename std::tuple_element< N, arguments >::type argument_type
Definition: function_traits.hpp:71
Definition: function_traits.hpp:49
Definition: allocator_common.hpp:24
Definition: parameter.hpp:235
ReturnTypeT return_type
Definition: function_traits.hpp:73
std::tuple< Tail ... > type
Definition: function_traits.hpp:44
Definition: function_traits.hpp:141
typename function_traits< decltype(&ReturnTypeT(ClassT &, Args ...) ::operator())>::return_type return_type
Definition: function_traits.hpp:59
Definition: function_traits.hpp:130
typename tuple_tail< typename function_traits< decltype(&FunctionT::operator())>::arguments >::type arguments
Definition: function_traits.hpp:52
Definition: function_traits.hpp:39
typename std::tuple_element< N, arguments >::type argument_type
Definition: function_traits.hpp:57
std::tuple< Args ... > arguments
Definition: function_traits.hpp:66