SMACC2
type_adapter.hpp
Go to the documentation of this file.
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
20namespace rclcpp
21{
22
24
97template<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
108template<typename T>
109struct is_type_adapter : std::false_type {};
110
112template<typename ... Ts>
113struct is_type_adapter<TypeAdapter<Ts...>>: std::true_type {};
114
116template<typename T>
117struct TypeAdapter<T, void, std::enable_if_t<is_type_adapter<T>::value>>: T {};
118
119namespace detail
120{
121
122template<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
134template<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
161template<typename CustomType>
163{
164 using is_specialized = std::false_type;
165};
166
168
175template<typename T>
176struct 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_
Implicit type adapter used as a short hand way to create something with just the custom type.
std::false_type is_specialized
Template structure used to adapt custom, user-defined types to ROS types.
std::false_type is_specialized
CustomType ros_message_type
Template metafunction that can make the type being adapted explicit.
typename ::rclcpp::detail::assert_type_pair_is_specialized_type_adapter< CustomType, ROSMessageType >::type_adapter as
Helper template to determine if a type is a TypeAdapter, false specialization.