|
| 1 | +// Copyright 2023 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__DYNAMIC_TYPESUPPORT__DETAIL__DYNAMIC_MESSAGE_IMPL_HPP_ |
| 16 | +#define RCLCPP__DYNAMIC_TYPESUPPORT__DETAIL__DYNAMIC_MESSAGE_IMPL_HPP_ |
| 17 | + |
| 18 | +#include <rosidl_dynamic_typesupport/types.h> |
| 19 | +#include <rosidl_dynamic_typesupport/api/dynamic_data.h> |
| 20 | + |
| 21 | +#include <cstdint> |
| 22 | +#include <cstddef> |
| 23 | +#include <memory> |
| 24 | +#include <string> |
| 25 | + |
| 26 | +#include "rclcpp/exceptions.hpp" |
| 27 | + |
| 28 | +#ifndef RCLCPP__DYNAMIC_TYPESUPPORT__DYNAMIC_MESSAGE_HPP_ |
| 29 | +#include "rclcpp/dynamic_typesupport/dynamic_message.hpp" |
| 30 | +#endif |
| 31 | + |
| 32 | + |
| 33 | +#define __DYNAMIC_MESSAGE_GET_VALUE_BY_ID_FN(ValueT, FunctionT) \ |
| 34 | + template<> \ |
| 35 | + ValueT \ |
| 36 | + DynamicMessage::get_value<ValueT>(rosidl_dynamic_typesupport_member_id_t id) \ |
| 37 | + { \ |
| 38 | + ValueT out; \ |
| 39 | + rosidl_dynamic_typesupport_dynamic_data_get_ ## FunctionT ## _value( \ |
| 40 | + &rosidl_dynamic_data_, id, &out); \ |
| 41 | + return out; \ |
| 42 | + } |
| 43 | + |
| 44 | +#define __DYNAMIC_MESSAGE_GET_VALUE_BY_NAME_FN(ValueT, FunctionT) \ |
| 45 | + template<> \ |
| 46 | + ValueT \ |
| 47 | + DynamicMessage::get_value<ValueT>(const std::string & name) \ |
| 48 | + { \ |
| 49 | + return get_value<ValueT>(get_member_id(name)); \ |
| 50 | + } |
| 51 | + |
| 52 | +#define __DYNAMIC_MESSAGE_SET_VALUE_BY_ID_FN(ValueT, FunctionT) \ |
| 53 | + template<> \ |
| 54 | + void \ |
| 55 | + DynamicMessage::set_value<ValueT>(rosidl_dynamic_typesupport_member_id_t id, ValueT value) \ |
| 56 | + { \ |
| 57 | + rosidl_dynamic_typesupport_dynamic_data_set_ ## FunctionT ## _value( \ |
| 58 | + &rosidl_dynamic_data_, id, value); \ |
| 59 | + } |
| 60 | + |
| 61 | +#define __DYNAMIC_MESSAGE_SET_VALUE_BY_NAME_FN(ValueT, FunctionT) \ |
| 62 | + template<> \ |
| 63 | + void \ |
| 64 | + DynamicMessage::set_value<ValueT>(const std::string & name, ValueT value) \ |
| 65 | + { \ |
| 66 | + set_value<ValueT>(get_member_id(name), value); \ |
| 67 | + } |
| 68 | + |
| 69 | +#define __DYNAMIC_MESSAGE_INSERT_VALUE(ValueT, FunctionT) \ |
| 70 | + template<> \ |
| 71 | + rosidl_dynamic_typesupport_member_id_t \ |
| 72 | + DynamicMessage::insert_value<ValueT>(ValueT value) \ |
| 73 | + { \ |
| 74 | + rosidl_dynamic_typesupport_member_id_t out; \ |
| 75 | + rosidl_dynamic_typesupport_dynamic_data_insert_ ## FunctionT ## _value( \ |
| 76 | + &rosidl_dynamic_data_, value, &out); \ |
| 77 | + return out; \ |
| 78 | + } |
| 79 | + |
| 80 | +#define DYNAMIC_MESSAGE_DEFINITIONS(ValueT, FunctionT) \ |
| 81 | + __DYNAMIC_MESSAGE_GET_VALUE_BY_ID_FN(ValueT, FunctionT) \ |
| 82 | + __DYNAMIC_MESSAGE_GET_VALUE_BY_NAME_FN(ValueT, FunctionT) \ |
| 83 | + __DYNAMIC_MESSAGE_SET_VALUE_BY_ID_FN(ValueT, FunctionT) \ |
| 84 | + __DYNAMIC_MESSAGE_SET_VALUE_BY_NAME_FN(ValueT, FunctionT) \ |
| 85 | + __DYNAMIC_MESSAGE_INSERT_VALUE(ValueT, FunctionT) |
| 86 | + |
| 87 | + |
| 88 | +namespace rclcpp |
| 89 | +{ |
| 90 | +namespace dynamic_typesupport |
| 91 | +{ |
| 92 | + |
| 93 | +/** |
| 94 | + * Since we're in a ROS layer, these should support all ROS interface C++ types as found in: |
| 95 | + * https://docs.ros.org/en/rolling/Concepts/About-ROS-Interfaces.html |
| 96 | + * |
| 97 | + * Explicitly: |
| 98 | + * - Basic types: bool, byte, char |
| 99 | + * - Float types: float, double |
| 100 | + * - Int types: int8_t, int16_t, int32_t, int64_t |
| 101 | + * - Unsigned int types: uint8_t, uint16_t, uint32_t, uint64_t |
| 102 | + * - String types: std::string, std::u16string |
| 103 | + */ |
| 104 | + |
| 105 | +DYNAMIC_MESSAGE_DEFINITIONS(bool, bool); |
| 106 | +// DYNAMIC_MESSAGE_DEFINITIONS(std::byte, byte); |
| 107 | +DYNAMIC_MESSAGE_DEFINITIONS(char, char); |
| 108 | +DYNAMIC_MESSAGE_DEFINITIONS(float, float32); |
| 109 | +DYNAMIC_MESSAGE_DEFINITIONS(double, float64); |
| 110 | +DYNAMIC_MESSAGE_DEFINITIONS(int8_t, int8); |
| 111 | +DYNAMIC_MESSAGE_DEFINITIONS(int16_t, int16); |
| 112 | +DYNAMIC_MESSAGE_DEFINITIONS(int32_t, int32); |
| 113 | +DYNAMIC_MESSAGE_DEFINITIONS(int64_t, int64); |
| 114 | +DYNAMIC_MESSAGE_DEFINITIONS(uint8_t, uint8); |
| 115 | +DYNAMIC_MESSAGE_DEFINITIONS(uint16_t, uint16); |
| 116 | +DYNAMIC_MESSAGE_DEFINITIONS(uint32_t, uint32); |
| 117 | +DYNAMIC_MESSAGE_DEFINITIONS(uint64_t, uint64); |
| 118 | +// DYNAMIC_MESSAGE_DEFINITIONS(std::string, std::string); |
| 119 | +// DYNAMIC_MESSAGE_DEFINITIONS(std::u16string, std::u16string); |
| 120 | + |
| 121 | +// Byte and String getters have a different implementation and are defined below |
| 122 | + |
| 123 | + |
| 124 | +// BYTE ============================================================================================ |
| 125 | +template<> |
| 126 | +std::byte |
| 127 | +DynamicMessage::get_value<std::byte>(rosidl_dynamic_typesupport_member_id_t id) |
| 128 | +{ |
| 129 | + unsigned char out; |
| 130 | + rosidl_dynamic_typesupport_dynamic_data_get_byte_value(&get_rosidl_dynamic_data(), id, &out); |
| 131 | + return static_cast<std::byte>(out); |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +template<> |
| 136 | +std::byte |
| 137 | +DynamicMessage::get_value<std::byte>(const std::string & name) |
| 138 | +{ |
| 139 | + return get_value<std::byte>(get_member_id(name)); |
| 140 | +} |
| 141 | + |
| 142 | + |
| 143 | +template<> |
| 144 | +void |
| 145 | +DynamicMessage::set_value<std::byte>( |
| 146 | + rosidl_dynamic_typesupport_member_id_t id, const std::byte value) |
| 147 | +{ |
| 148 | + rosidl_dynamic_typesupport_dynamic_data_set_byte_value( |
| 149 | + &rosidl_dynamic_data_, id, static_cast<unsigned char>(value)); |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | +template<> |
| 154 | +void |
| 155 | +DynamicMessage::set_value<std::byte>(const std::string & name, const std::byte value) |
| 156 | +{ |
| 157 | + set_value<std::byte>(get_member_id(name), value); |
| 158 | +} |
| 159 | + |
| 160 | + |
| 161 | +template<> |
| 162 | +rosidl_dynamic_typesupport_member_id_t |
| 163 | +DynamicMessage::insert_value<std::byte>(const std::byte value) |
| 164 | +{ |
| 165 | + rosidl_dynamic_typesupport_member_id_t out; |
| 166 | + rosidl_dynamic_typesupport_dynamic_data_insert_byte_value( |
| 167 | + &rosidl_dynamic_data_, static_cast<unsigned char>(value), &out); |
| 168 | + return out; |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +// STRINGS ========================================================================================= |
| 173 | +template<> |
| 174 | +std::string |
| 175 | +DynamicMessage::get_value<std::string>(rosidl_dynamic_typesupport_member_id_t id) |
| 176 | +{ |
| 177 | + size_t buf_length; |
| 178 | + char * buf = nullptr; |
| 179 | + rosidl_dynamic_typesupport_dynamic_data_get_string_value( |
| 180 | + &get_rosidl_dynamic_data(), id, &buf, &buf_length); |
| 181 | + auto out = std::string(buf, buf_length); |
| 182 | + delete buf; |
| 183 | + return out; |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | +template<> |
| 188 | +std::u16string |
| 189 | +DynamicMessage::get_value<std::u16string>(rosidl_dynamic_typesupport_member_id_t id) |
| 190 | +{ |
| 191 | + size_t buf_length; |
| 192 | + char16_t * buf = nullptr; |
| 193 | + rosidl_dynamic_typesupport_dynamic_data_get_wstring_value( |
| 194 | + &get_rosidl_dynamic_data(), id, &buf, &buf_length); |
| 195 | + auto out = std::u16string(buf, buf_length); |
| 196 | + delete buf; |
| 197 | + return out; |
| 198 | +} |
| 199 | + |
| 200 | + |
| 201 | +template<> |
| 202 | +std::string |
| 203 | +DynamicMessage::get_value<std::string>(const std::string & name) |
| 204 | +{ |
| 205 | + return get_value<std::string>(get_member_id(name)); |
| 206 | +} |
| 207 | + |
| 208 | + |
| 209 | +template<> |
| 210 | +std::u16string |
| 211 | +DynamicMessage::get_value<std::u16string>(const std::string & name) |
| 212 | +{ |
| 213 | + return get_value<std::u16string>(get_member_id(name)); |
| 214 | +} |
| 215 | + |
| 216 | + |
| 217 | +template<> |
| 218 | +void |
| 219 | +DynamicMessage::set_value<std::string>( |
| 220 | + rosidl_dynamic_typesupport_member_id_t id, const std::string value) |
| 221 | +{ |
| 222 | + rosidl_dynamic_typesupport_dynamic_data_set_string_value( |
| 223 | + &rosidl_dynamic_data_, id, value.c_str(), value.size()); |
| 224 | +} |
| 225 | + |
| 226 | + |
| 227 | +template<> |
| 228 | +void |
| 229 | +DynamicMessage::set_value<std::u16string>( |
| 230 | + rosidl_dynamic_typesupport_member_id_t id, const std::u16string value) |
| 231 | +{ |
| 232 | + rosidl_dynamic_typesupport_dynamic_data_set_wstring_value( |
| 233 | + &rosidl_dynamic_data_, id, value.c_str(), value.size()); |
| 234 | +} |
| 235 | + |
| 236 | + |
| 237 | +template<> |
| 238 | +void |
| 239 | +DynamicMessage::set_value<std::string>(const std::string & name, const std::string value) |
| 240 | +{ |
| 241 | + set_value<std::string>(get_member_id(name), value); |
| 242 | +} |
| 243 | + |
| 244 | + |
| 245 | +template<> |
| 246 | +void |
| 247 | +DynamicMessage::set_value<std::u16string>(const std::string & name, const std::u16string value) |
| 248 | +{ |
| 249 | + set_value<std::u16string>(get_member_id(name), value); |
| 250 | +} |
| 251 | + |
| 252 | + |
| 253 | +template<> |
| 254 | +rosidl_dynamic_typesupport_member_id_t |
| 255 | +DynamicMessage::insert_value<std::string>(const std::string value) |
| 256 | +{ |
| 257 | + rosidl_dynamic_typesupport_member_id_t out; |
| 258 | + rosidl_dynamic_typesupport_dynamic_data_insert_string_value( |
| 259 | + &rosidl_dynamic_data_, value.c_str(), value.size(), &out); |
| 260 | + return out; |
| 261 | +} |
| 262 | + |
| 263 | + |
| 264 | +template<> |
| 265 | +rosidl_dynamic_typesupport_member_id_t |
| 266 | +DynamicMessage::insert_value<std::u16string>(const std::u16string value) |
| 267 | +{ |
| 268 | + rosidl_dynamic_typesupport_member_id_t out; |
| 269 | + rosidl_dynamic_typesupport_dynamic_data_insert_wstring_value( |
| 270 | + &rosidl_dynamic_data_, value.c_str(), value.size(), &out); |
| 271 | + return out; |
| 272 | +} |
| 273 | + |
| 274 | + |
| 275 | +// THROW FOR UNSUPPORTED TYPES ===================================================================== |
| 276 | +template<typename ValueT> |
| 277 | +ValueT |
| 278 | +DynamicMessage::get_value(rosidl_dynamic_typesupport_member_id_t id) |
| 279 | +{ |
| 280 | + throw rclcpp::exceptions::UnimplementedError("get_value is not implemented for input type"); |
| 281 | +} |
| 282 | + |
| 283 | + |
| 284 | +template<typename ValueT> |
| 285 | +ValueT |
| 286 | +DynamicMessage::get_value(const std::string & name) |
| 287 | +{ |
| 288 | + throw rclcpp::exceptions::UnimplementedError("get_value is not implemented for input type"); |
| 289 | +} |
| 290 | + |
| 291 | + |
| 292 | +template<typename ValueT> |
| 293 | +void |
| 294 | +DynamicMessage::set_value( |
| 295 | + rosidl_dynamic_typesupport_member_id_t id, ValueT value) |
| 296 | +{ |
| 297 | + throw rclcpp::exceptions::UnimplementedError("set_value is not implemented for input type"); |
| 298 | +} |
| 299 | + |
| 300 | + |
| 301 | +template<typename ValueT> |
| 302 | +void |
| 303 | +DynamicMessage::set_value(const std::string & name, ValueT value) |
| 304 | +{ |
| 305 | + throw rclcpp::exceptions::UnimplementedError("set_value is not implemented for input type"); |
| 306 | +} |
| 307 | + |
| 308 | + |
| 309 | +template<typename ValueT> |
| 310 | +rosidl_dynamic_typesupport_member_id_t |
| 311 | +DynamicMessage::insert_value(ValueT value) |
| 312 | +{ |
| 313 | + throw rclcpp::exceptions::UnimplementedError("insert_value is not implemented for input type"); |
| 314 | +} |
| 315 | + |
| 316 | + |
| 317 | +} // namespace dynamic_typesupport |
| 318 | +} // namespace rclcpp |
| 319 | + |
| 320 | +#undef __DYNAMIC_MESSAGE_GET_VALUE_BY_ID_FN |
| 321 | +#undef __DYNAMIC_MESSAGE_GET_VALUE_BY_NAME_FN |
| 322 | +#undef __DYNAMIC_MESSAGE_SET_VALUE_BY_ID_FN |
| 323 | +#undef __DYNAMIC_MESSAGE_SET_VALUE_BY_NAME_FN |
| 324 | +#undef __DYNAMIC_MESSAGE_INSERT_VALUE |
| 325 | +#undef DYNAMIC_MESSAGE_DEFINITIONS |
| 326 | + |
| 327 | +#endif // RCLCPP__DYNAMIC_TYPESUPPORT__DETAIL__DYNAMIC_MESSAGE_IMPL_HPP_ |
0 commit comments