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_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 free functions
100 template<typename ReturnTypeT, typename ... Args, typename ... FArgs>
101 #if defined _LIBCPP_VERSION // libc++ (Clang)
102 struct function_traits<std::__1::__bind<ReturnTypeT( &)(Args ...), FArgs ...>>
103 #elif defined __GLIBCXX__ // glibc++ (GNU C++)
104 struct function_traits<std::_Bind<ReturnTypeT(*(FArgs ...))(Args ...)>>
105 #elif defined _MSC_VER // MS Visual Studio
106 struct function_traits<std::_Binder<std::_Unforced, ReturnTypeT(__cdecl &)(Args ...), FArgs ...>>
107 #else
108 #error "Unsupported C++ compiler / standard library"
109 #endif
110  : function_traits<ReturnTypeT(Args ...)>
111 {};
112 
113 // Lambdas
114 template<typename ClassT, typename ReturnTypeT, typename ... Args>
115 struct function_traits<ReturnTypeT (ClassT::*)(Args ...) const>
116  : function_traits<ReturnTypeT(ClassT &, Args ...)>
117 {};
118 
119 template<typename FunctionT>
121 {};
122 
123 template<typename FunctionT>
125 {};
126 
127 /* NOTE(esteve):
128  * VS2015 does not support expression SFINAE, so we're using this template to evaluate
129  * the arity of a function.
130  */
131 template<std::size_t Arity, typename FunctorT>
133  bool, (Arity == function_traits<FunctorT>::arity)> {};
134 
135 template<typename FunctorT, typename ... Args>
137  typename function_traits<FunctorT>::arguments,
138  std::tuple<Args ...>
139 >
140 {};
141 
142 template<typename FunctorAT, typename FunctorBT>
144  typename function_traits<FunctorAT>::arguments,
145  typename function_traits<FunctorBT>::arguments
146 >
147 {};
148 
149 } // namespace function_traits
150 
151 } // namespace rclcpp
152 
153 #endif // RCLCPP__FUNCTION_TRAITS_HPP_
Definition: function_traits.hpp:136
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
Definition: allocator_common.hpp:24
ReturnTypeT return_type
Definition: function_traits.hpp:73
Definition: function_traits.hpp:143
Definition: function_traits.hpp:132
Definition: function_traits.hpp:39
typename std::tuple_element< N, arguments >::type argument_type
Definition: function_traits.hpp:57