forked from ros2/rclcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_adapter.hpp
More file actions
201 lines (185 loc) · 6.85 KB
/
type_adapter.hpp
File metadata and controls
201 lines (185 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2021 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef RCLCPP__TYPE_ADAPTER_HPP_
#define RCLCPP__TYPE_ADAPTER_HPP_
#include <type_traits>
namespace rclcpp
{
/// Template structure used to adapt custom, user-defined types to ROS types.
/**
* Adapting a custom, user-defined type to a ROS type allows that custom type
* to be used when publishing and subscribing in ROS.
*
* In order to adapt a custom type to a ROS type, the user must create a
* template specialization of this structure for the custom type.
* In that specialization they must:
*
* - change `is_specialized` to `std::true_type`,
* - specify the custom type with `using custom_type = ...`,
* - specify the ROS type with `using ros_message_type = ...`,
* - provide static convert functions with the signatures:
* - static void convert_to_ros(const custom_type &, ros_message_type &)
* - static void convert_to_custom(const ros_message_type &, custom_type &)
*
* The convert functions must convert from one type to the other.
*
* For example, here is a theoretical example for adapting `std::string` to the
* `std_msgs::msg::String` ROS message type:
*
* template<>
* struct rclcpp::TypeAdapter<std::string, std_msgs::msg::String>
* {
* using is_specialized = std::true_type;
* using custom_type = std::string;
* using ros_message_type = std_msgs::msg::String;
*
* static
* void
* convert_to_ros_message(
* const custom_type & source,
* ros_message_type & destination)
* {
* destination.data = source;
* }
*
* static
* void
* convert_to_custom(
* const ros_message_type & source,
* custom_type & destination)
* {
* destination = source.data;
* }
* };
*
* The adapter can then be used when creating a publisher or subscription,
* e.g.:
*
* using MyAdaptedType = TypeAdapter<std::string, std_msgs::msg::String>;
* auto pub = node->create_publisher<MyAdaptedType>("topic", 10);
* auto sub = node->create_subscription<MyAdaptedType>(
* "topic",
* 10,
* [](const std::string & msg) {...});
*
* You can also be more declarative by using the adapt_type::as metafunctions,
* which are a bit less ambiguous to read:
*
* using AdaptedType = rclcpp::adapt_type<std::string>::as<std_msgs::msg::String>;
* auto pub = node->create_publisher<AdaptedType>(...);
*
* If you wish, you may associate a custom type with a single ROS message type,
* allowing you to be a bit more brief when creating entities, e.g.:
*
* // First you must declare the association, this is similar to how you
* // would avoid using the namespace in C++ by doing `using std::vector;`.
* RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE(std::string, std_msgs::msg::String);
*
* // Then you can create things with just the custom type, and the ROS
* // message type is implied based on the previous statement.
* auto pub = node->create_publisher<std::string>(...);
*/
template<typename CustomType, typename ROSMessageType = void, class Enable = void>
struct TypeAdapter
{
using is_specialized = std::false_type;
using custom_type = CustomType;
// In this case, the CustomType is the only thing given, or there is no specialization.
// Assign ros_message_type to CustomType for the former case.
using ros_message_type = CustomType;
};
/// Helper template to determine if a type is a TypeAdapter, false specialization.
template<typename T>
struct is_type_adapter : std::false_type {};
/// Helper template to determine if a type is a TypeAdapter, true specialization.
template<typename ... Ts>
struct is_type_adapter<TypeAdapter<Ts...>>: std::true_type {};
/// Identity specialization for TypeAdapter.
template<typename T>
struct TypeAdapter<T, void, std::enable_if_t<is_type_adapter<T>::value>>: T {};
namespace detail
{
template<typename CustomType, typename ROSMessageType>
struct assert_type_pair_is_specialized_type_adapter
{
using type_adapter = TypeAdapter<CustomType, ROSMessageType>;
static_assert(
type_adapter::is_specialized::value,
"No type adapter for this custom type/ros message type pair");
};
} // namespace detail
/// Template metafunction that can make the type being adapted explicit.
template<typename CustomType>
struct adapt_type
{
template<typename ROSMessageType>
using as = typename ::rclcpp::detail::assert_type_pair_is_specialized_type_adapter<
CustomType,
ROSMessageType
>::type_adapter;
};
/// Implicit type adapter used as a short hand way to create something with just the custom type.
/**
* This is used when creating a publisher or subscription using just the custom
* type in conjunction with RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE().
* For example:
*
* #include "type_adapter_for_std_string_to_std_msgs_String.hpp"
*
* RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE(std::string, std_msgs::msg::String);
*
* int main(...) {
* // ...
* auto pub = node->create_publisher<std::string>(...);
* }
*
* \sa TypeAdapter for more examples.
*/
template<typename CustomType>
struct ImplicitTypeAdapter
{
using is_specialized = std::false_type;
};
/// Specialization of TypeAdapter for ImplicitTypeAdapter.
/**
* This allows for things like this:
*
* RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE(std::string, std_msgs::msg::String);
* auto pub = node->create_publisher<std::string>("topic", 10);
*
*/
template<typename T>
struct TypeAdapter<T, void, std::enable_if_t<ImplicitTypeAdapter<T>::is_specialized::value>>
: ImplicitTypeAdapter<T>
{};
/// Assigns the custom type implicitly to the given custom type/ros message type pair.
/**
* Note: this macro needs to be used in the root namespace.
* We cannot use ::rclcpp to protect against this, due to how GCC interprets the
* syntax, see: https://stackoverflow.com/a/2781537
*
* \sa TypeAdapter
* \sa ImplicitTypeAdapter
*/
#define RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE(CustomType, ROSMessageType) \
template<> \
struct rclcpp::ImplicitTypeAdapter<CustomType> \
: public rclcpp::TypeAdapter<CustomType, ROSMessageType> \
{ \
static_assert( \
is_specialized::value, \
"Cannot use custom type as ros type when there is no TypeAdapter for that pair"); \
}
} // namespace rclcpp
#endif // RCLCPP__TYPE_ADAPTER_HPP_