ROS 2 rclcpp + rcl - humble  humble
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 
20 namespace rclcpp
21 {
22 
24 
97 template<typename CustomType, typename ROSMessageType = void, class Enable = void>
99 {
100  using is_specialized = std::false_type;
101  using custom_type = CustomType;
102  // In this case, the CustomType is the only thing given, or there is no specialization.
103  // Assign ros_message_type to CustomType for the former case.
104  using ros_message_type = CustomType;
105 };
106 
108 template<typename T>
109 struct is_type_adapter : std::false_type {};
110 
112 template<typename ... Ts>
113 struct is_type_adapter<TypeAdapter<Ts...>>: std::true_type {};
114 
116 template<typename T>
117 struct TypeAdapter<T, void, std::enable_if_t<is_type_adapter<T>::value>>: T {};
118 
119 namespace detail
120 {
121 
122 template<typename CustomType, typename ROSMessageType>
124 {
126  static_assert(
127  type_adapter::is_specialized::value,
128  "No type adapter for this custom type/ros message type pair");
129 };
130 
131 } // namespace detail
132 
134 template<typename CustomType>
136 {
137  template<typename ROSMessageType>
138  using as = typename ::rclcpp::detail::assert_type_pair_is_specialized_type_adapter<
139  CustomType,
140  ROSMessageType
141  >::type_adapter;
142 };
143 
145 
161 template<typename CustomType>
163 {
164  using is_specialized = std::false_type;
165 };
166 
168 
175 template<typename T>
176 struct TypeAdapter<T, void, std::enable_if_t<ImplicitTypeAdapter<T>::is_specialized::value>>
178 {};
179 
181 
189 #define RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE(CustomType, ROSMessageType) \
190  template<> \
191  struct rclcpp::ImplicitTypeAdapter<CustomType> \
192  : public rclcpp::TypeAdapter<CustomType, ROSMessageType> \
193  { \
194  static_assert( \
195  is_specialized::value, \
196  "Cannot use custom type as ros type when there is no TypeAdapter for that pair"); \
197  }
198 
199 } // namespace rclcpp
200 
201 #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.