ROS 2 rclcpp + rcl - rolling  rolling-4d14414d
ROS 2 C++ Client Library with ROS Client Library
type_adapter.hpp
1 // Copyright 2021 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__TYPE_ADAPTER_HPP_
16 #define RCLCPP__TYPE_ADAPTER_HPP_
17 
18 #include <type_traits>
19 #include <new>
20 
21 namespace rclcpp
22 {
23 
25 
98 template<typename CustomType, typename ROSMessageType = void, class Enable = void>
100 {
101  using is_specialized = std::false_type;
102  using custom_type = CustomType;
103  // In this case, the CustomType is the only thing given, or there is no specialization.
104  // Assign ros_message_type to CustomType for the former case.
105  using ros_message_type = CustomType;
106 };
107 
109 template<typename T>
110 struct is_type_adapter : std::false_type {};
111 
113 template<typename ... Ts>
114 struct is_type_adapter<TypeAdapter<Ts...>>: std::true_type {};
115 
117 template<typename T>
118 struct TypeAdapter<T, void, std::enable_if_t<is_type_adapter<T>::value>>: T {};
119 
120 namespace detail
121 {
122 
123 template<typename CustomType, typename ROSMessageType>
125 {
127  static_assert(
128  type_adapter::is_specialized::value,
129  "No type adapter for this custom type/ros message type pair");
130 };
131 
132 template<typename, typename = void>
133 struct has_overloaded_operator_new : std::false_type {};
134 template<typename T>
135 struct has_overloaded_operator_new<T, std::void_t<
136  decltype(T::operator new(std::size_t()))
137  >>: std::true_type {};
138 
139 template<typename, typename = void>
140 struct has_overloaded_aligned_operator_new : std::false_type {};
141 template<typename T>
143  std::void_t<decltype( T::operator new(std::size_t(), std::align_val_t()) )>>
144  : std::true_type {};
145 
146 template<typename T>
147 inline constexpr bool has_overloaded_operator_new_v = has_overloaded_operator_new<T>::value ||
149 
150 } // namespace detail
151 
153 template<typename CustomType>
155 {
156  template<typename ROSMessageType>
157  using as = typename ::rclcpp::detail::assert_type_pair_is_specialized_type_adapter<
158  CustomType,
159  ROSMessageType
160  >::type_adapter;
161 };
162 
164 
180 template<typename CustomType>
182 {
183  using is_specialized = std::false_type;
184 };
185 
187 
194 template<typename T>
195 struct TypeAdapter<T, void, std::enable_if_t<ImplicitTypeAdapter<T>::is_specialized::value>>
197 {};
198 
200 
208 #define RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE(CustomType, ROSMessageType) \
209  template<> \
210  struct rclcpp::ImplicitTypeAdapter<CustomType> \
211  : public rclcpp::TypeAdapter<CustomType, ROSMessageType> \
212  { \
213  static_assert( \
214  is_specialized::value, \
215  "Cannot use custom type as ros type when there is no TypeAdapter for that pair"); \
216  }
217 
218 } // namespace rclcpp
219 
220 #endif // RCLCPP__TYPE_ADAPTER_HPP_
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Implicit type adapter used as a short hand way to create something with just the custom type.
Template structure used to adapt custom, user-defined types to ROS types.
Template metafunction that can make the type being adapted explicit.
Helper template to determine if a type is a TypeAdapter, false specialization.