From c52f250effbf8d2dbb9b76458f6e59fdb3e058ce Mon Sep 17 00:00:00 2001 From: methylDragon Date: Mon, 10 Apr 2023 06:01:49 -0700 Subject: [PATCH] Refactor dynamic type support structs to use allocators and refs Signed-off-by: methylDragon --- .../detail/dynamic_message_impl.hpp | 24 +- .../dynamic_message_type_builder_impl.hpp | 8 +- .../dynamic_typesupport/dynamic_message.hpp | 96 ++--- .../dynamic_message_type.hpp | 62 ++- .../dynamic_message_type_builder.hpp | 71 ++-- .../dynamic_message_type_support.hpp | 2 +- .../dynamic_serialization_support.hpp | 9 +- .../dynamic_typesupport/dynamic_message.cpp | 382 ++++++++---------- .../dynamic_message_type.cpp | 190 ++++----- .../dynamic_message_type_builder.cpp | 272 +++++-------- .../dynamic_message_type_support.cpp | 18 +- rclcpp/src/rclcpp/executor.cpp | 2 +- rclcpp/src/rclcpp/subscription_base.cpp | 2 +- 13 files changed, 480 insertions(+), 658 deletions(-) diff --git a/rclcpp/include/rclcpp/dynamic_typesupport/detail/dynamic_message_impl.hpp b/rclcpp/include/rclcpp/dynamic_typesupport/detail/dynamic_message_impl.hpp index eccdbccf4d..5ae6827692 100644 --- a/rclcpp/include/rclcpp/dynamic_typesupport/detail/dynamic_message_impl.hpp +++ b/rclcpp/include/rclcpp/dynamic_typesupport/detail/dynamic_message_impl.hpp @@ -37,7 +37,7 @@ { \ ValueT out; \ rosidl_dynamic_typesupport_dynamic_data_get_ ## FunctionT ## _value( \ - rosidl_dynamic_data_.get(), id, &out); \ + &rosidl_dynamic_data_, id, &out); \ return out; \ } @@ -55,7 +55,7 @@ DynamicMessage::set_value(rosidl_dynamic_typesupport_member_id_t id, ValueT value) \ { \ rosidl_dynamic_typesupport_dynamic_data_set_ ## FunctionT ## _value( \ - rosidl_dynamic_data_.get(), id, value); \ + &rosidl_dynamic_data_, id, value); \ } #define __DYNAMIC_MESSAGE_SET_VALUE_BY_NAME_FN(ValueT, FunctionT) \ @@ -73,7 +73,7 @@ { \ rosidl_dynamic_typesupport_member_id_t out; \ rosidl_dynamic_typesupport_dynamic_data_insert_ ## FunctionT ## _value( \ - rosidl_dynamic_data_.get(), value, &out); \ + &rosidl_dynamic_data_, value, &out); \ return out; \ } @@ -127,7 +127,7 @@ std::byte DynamicMessage::get_value(rosidl_dynamic_typesupport_member_id_t id) { unsigned char out; - rosidl_dynamic_typesupport_dynamic_data_get_byte_value(get_rosidl_dynamic_data(), id, &out); + rosidl_dynamic_typesupport_dynamic_data_get_byte_value(&get_rosidl_dynamic_data(), id, &out); return static_cast(out); } @@ -146,7 +146,7 @@ DynamicMessage::set_value( rosidl_dynamic_typesupport_member_id_t id, const std::byte value) { rosidl_dynamic_typesupport_dynamic_data_set_byte_value( - rosidl_dynamic_data_.get(), id, static_cast(value)); + &rosidl_dynamic_data_, id, static_cast(value)); } @@ -164,7 +164,7 @@ DynamicMessage::insert_value(const std::byte value) { rosidl_dynamic_typesupport_member_id_t out; rosidl_dynamic_typesupport_dynamic_data_insert_byte_value( - rosidl_dynamic_data_.get(), static_cast(value), &out); + &rosidl_dynamic_data_, static_cast(value), &out); return out; } @@ -177,7 +177,7 @@ DynamicMessage::get_value(rosidl_dynamic_typesupport_member_id_t id size_t buf_length; char * buf = nullptr; rosidl_dynamic_typesupport_dynamic_data_get_string_value( - get_rosidl_dynamic_data(), id, &buf, &buf_length); + &get_rosidl_dynamic_data(), id, &buf, &buf_length); auto out = std::string(buf, buf_length); delete buf; return out; @@ -191,7 +191,7 @@ DynamicMessage::get_value(rosidl_dynamic_typesupport_member_id_t size_t buf_length; char16_t * buf = nullptr; rosidl_dynamic_typesupport_dynamic_data_get_wstring_value( - get_rosidl_dynamic_data(), id, &buf, &buf_length); + &get_rosidl_dynamic_data(), id, &buf, &buf_length); auto out = std::u16string(buf, buf_length); delete buf; return out; @@ -220,7 +220,7 @@ DynamicMessage::set_value( rosidl_dynamic_typesupport_member_id_t id, const std::string value) { rosidl_dynamic_typesupport_dynamic_data_set_string_value( - rosidl_dynamic_data_.get(), id, value.c_str(), value.size()); + &rosidl_dynamic_data_, id, value.c_str(), value.size()); } @@ -230,7 +230,7 @@ DynamicMessage::set_value( rosidl_dynamic_typesupport_member_id_t id, const std::u16string value) { rosidl_dynamic_typesupport_dynamic_data_set_wstring_value( - rosidl_dynamic_data_.get(), id, value.c_str(), value.size()); + &rosidl_dynamic_data_, id, value.c_str(), value.size()); } @@ -256,7 +256,7 @@ DynamicMessage::insert_value(const std::string value) { rosidl_dynamic_typesupport_member_id_t out; rosidl_dynamic_typesupport_dynamic_data_insert_string_value( - rosidl_dynamic_data_.get(), value.c_str(), value.size(), &out); + &rosidl_dynamic_data_, value.c_str(), value.size(), &out); return out; } @@ -267,7 +267,7 @@ DynamicMessage::insert_value(const std::u16string value) { rosidl_dynamic_typesupport_member_id_t out; rosidl_dynamic_typesupport_dynamic_data_insert_wstring_value( - rosidl_dynamic_data_.get(), value.c_str(), value.size(), &out); + &rosidl_dynamic_data_, value.c_str(), value.size(), &out); return out; } diff --git a/rclcpp/include/rclcpp/dynamic_typesupport/detail/dynamic_message_type_builder_impl.hpp b/rclcpp/include/rclcpp/dynamic_typesupport/detail/dynamic_message_type_builder_impl.hpp index f28f8ce293..60188146e1 100644 --- a/rclcpp/include/rclcpp/dynamic_typesupport/detail/dynamic_message_type_builder_impl.hpp +++ b/rclcpp/include/rclcpp/dynamic_typesupport/detail/dynamic_message_type_builder_impl.hpp @@ -39,7 +39,7 @@ const std::string & default_value) \ { \ rosidl_dynamic_typesupport_dynamic_type_builder_add_ ## FunctionT ## _member( \ - rosidl_dynamic_type_builder_.get(), \ + &rosidl_dynamic_type_builder_, \ id, name.c_str(), name.size(), default_value.c_str(), default_value.size()); \ } @@ -52,7 +52,7 @@ const std::string & default_value) \ { \ rosidl_dynamic_typesupport_dynamic_type_builder_add_ ## FunctionT ## _array_member( \ - rosidl_dynamic_type_builder_.get(), \ + &rosidl_dynamic_type_builder_, \ id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), \ array_length); \ } @@ -67,7 +67,7 @@ { \ rosidl_dynamic_typesupport_dynamic_type_builder_add_ ## FunctionT ## \ _unbounded_sequence_member( \ - rosidl_dynamic_type_builder_.get(), \ + &rosidl_dynamic_type_builder_, \ id, name.c_str(), name.size(), default_value.c_str(), default_value.size()); \ } @@ -81,7 +81,7 @@ const std::string & default_value) \ { \ rosidl_dynamic_typesupport_dynamic_type_builder_add_ ## FunctionT ## _bounded_sequence_member( \ - rosidl_dynamic_type_builder_.get(), \ + &rosidl_dynamic_type_builder_, \ id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), \ sequence_bound); \ } diff --git a/rclcpp/include/rclcpp/dynamic_typesupport/dynamic_message.hpp b/rclcpp/include/rclcpp/dynamic_typesupport/dynamic_message.hpp index 2719df35b5..fef984a1e0 100644 --- a/rclcpp/include/rclcpp/dynamic_typesupport/dynamic_message.hpp +++ b/rclcpp/include/rclcpp/dynamic_typesupport/dynamic_message.hpp @@ -15,6 +15,7 @@ #ifndef RCLCPP__DYNAMIC_TYPESUPPORT__DYNAMIC_MESSAGE_HPP_ #define RCLCPP__DYNAMIC_TYPESUPPORT__DYNAMIC_MESSAGE_HPP_ +#include #include #include @@ -26,21 +27,18 @@ #include "rclcpp/macros.hpp" #include "rclcpp/visibility_control.hpp" - namespace rclcpp { namespace dynamic_typesupport { - class DynamicMessageType; class DynamicMessageTypeBuilder; -/// Utility wrapper class for rosidl_dynamic_typesupport_dynamic_data_t * +/// Utility wrapper class for rosidl_dynamic_typesupport_dynamic_data_t /** * This class: - * - Manages the lifetime of the raw pointer. - * - Exposes getter methods to get the raw pointer and shared pointers + * - Exposes getter methods for the struct * - Exposes the underlying serialization support API * * Ownership: @@ -67,50 +65,48 @@ class DynamicMessage : public std::enable_shared_from_this // the data should be the exact same object managed by the DynamicSerializationSupport, // otherwise the lifetime management will not work properly. - /// Construct a new DynamicMessage with the provided dynamic type builder + /// Construct a new DynamicMessage with the provided dynamic type builder, using its allocator RCLCPP_PUBLIC explicit DynamicMessage(std::shared_ptr dynamic_type_builder); - /// Construct a new DynamicMessage with the provided dynamic type + /// Construct a new DynamicMessage with the provided dynamic type builder and allocator + RCLCPP_PUBLIC + DynamicMessage( + std::shared_ptr dynamic_type_builder, + rcl_allocator_t allocator); + + /// Construct a new DynamicMessage with the provided dynamic type, using its allocator RCLCPP_PUBLIC explicit DynamicMessage(std::shared_ptr dynamic_type); - /// Assume ownership of raw pointer + /// Construct a new DynamicMessage with the provided dynamic type and allocator RCLCPP_PUBLIC DynamicMessage( - DynamicSerializationSupport::SharedPtr serialization_support, - rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data); + std::shared_ptr dynamic_type, + rcl_allocator_t allocator); - /// Copy shared pointer + /// Assume ownership of struct RCLCPP_PUBLIC DynamicMessage( DynamicSerializationSupport::SharedPtr serialization_support, - std::shared_ptr rosidl_dynamic_data); + rosidl_dynamic_typesupport_dynamic_data_t && rosidl_dynamic_data); /// Loaning constructor - /// Must only be called with raw ptr obtained from loaning! + /// Must only be called with a rosidl dynaimc data object obtained from loaning! // NOTE(methylDragon): I'd put this in protected, but I need this exposed to // enable_shared_from_this... RCLCPP_PUBLIC DynamicMessage( DynamicMessage::SharedPtr parent_data, - rosidl_dynamic_typesupport_dynamic_data_t * rosidl_loaned_data); + rosidl_dynamic_typesupport_dynamic_data_t && rosidl_loaned_data); // NOTE(methylDragon): Deliberately no constructor from description to nudge users towards using // construction from dynamic type/builder, which is more efficient - /// Copy constructor - RCLCPP_PUBLIC - DynamicMessage(const DynamicMessage & other); - /// Move constructor RCLCPP_PUBLIC DynamicMessage(DynamicMessage && other) noexcept; - /// Copy assignment - RCLCPP_PUBLIC - DynamicMessage & operator=(const DynamicMessage & other); - /// Move assignment RCLCPP_PUBLIC DynamicMessage & operator=(DynamicMessage && other) noexcept; @@ -129,21 +125,13 @@ class DynamicMessage : public std::enable_shared_from_this get_name() const; RCLCPP_PUBLIC - rosidl_dynamic_typesupport_dynamic_data_t * + rosidl_dynamic_typesupport_dynamic_data_t & get_rosidl_dynamic_data(); RCLCPP_PUBLIC - const rosidl_dynamic_typesupport_dynamic_data_t * + const rosidl_dynamic_typesupport_dynamic_data_t & get_rosidl_dynamic_data() const; - RCLCPP_PUBLIC - std::shared_ptr - get_shared_rosidl_dynamic_data(); - - RCLCPP_PUBLIC - std::shared_ptr - get_shared_rosidl_dynamic_data() const; - RCLCPP_PUBLIC DynamicSerializationSupport::SharedPtr get_shared_dynamic_serialization_support(); @@ -176,19 +164,21 @@ class DynamicMessage : public std::enable_shared_from_this // METHODS ======================================================================================= RCLCPP_PUBLIC DynamicMessage - clone() const; + clone(rcl_allocator_t allocator = rcl_get_default_allocator()); RCLCPP_PUBLIC DynamicMessage::SharedPtr - clone_shared() const; + clone_shared(rcl_allocator_t allocator = rcl_get_default_allocator()); RCLCPP_PUBLIC DynamicMessage - init_from_type(DynamicMessageType & type) const; + init_from_type( + DynamicMessageType & type, rcl_allocator_t allocator = rcl_get_default_allocator()) const; RCLCPP_PUBLIC DynamicMessage::SharedPtr - init_from_type_shared(DynamicMessageType & type) const; + init_from_type_shared( + DynamicMessageType & type, rcl_allocator_t allocator = rcl_get_default_allocator()) const; RCLCPP_PUBLIC bool @@ -196,11 +186,15 @@ class DynamicMessage : public std::enable_shared_from_this RCLCPP_PUBLIC DynamicMessage::SharedPtr - loan_value(rosidl_dynamic_typesupport_member_id_t id); + loan_value( + rosidl_dynamic_typesupport_member_id_t id, + rcl_allocator_t allocator = rcl_get_default_allocator()); RCLCPP_PUBLIC DynamicMessage::SharedPtr - loan_value(const std::string & name); + loan_value( + const std::string & name, + rcl_allocator_t allocator = rcl_get_default_allocator()); RCLCPP_PUBLIC void @@ -232,11 +226,11 @@ class DynamicMessage : public std::enable_shared_from_this RCLCPP_PUBLIC bool - serialize(rcl_serialized_message_t * buffer); + serialize(rcl_serialized_message_t & buffer); RCLCPP_PUBLIC bool - deserialize(rcl_serialized_message_t * buffer); + deserialize(rcl_serialized_message_t & buffer); // MEMBER ACCESS TEMPLATES ======================================================================= @@ -366,19 +360,27 @@ class DynamicMessage : public std::enable_shared_from_this // NESTED MEMBER ACCESS ========================================================================== RCLCPP_PUBLIC DynamicMessage - get_complex_value(rosidl_dynamic_typesupport_member_id_t id); + get_complex_value( + rosidl_dynamic_typesupport_member_id_t id, + rcl_allocator_t allocator = rcl_get_default_allocator()); RCLCPP_PUBLIC DynamicMessage - get_complex_value(const std::string & name); + get_complex_value( + const std::string & name, + rcl_allocator_t allocator = rcl_get_default_allocator()); RCLCPP_PUBLIC DynamicMessage::SharedPtr - get_complex_value_shared(rosidl_dynamic_typesupport_member_id_t id); + get_complex_value_shared( + rosidl_dynamic_typesupport_member_id_t id, + rcl_allocator_t allocator = rcl_get_default_allocator()); RCLCPP_PUBLIC DynamicMessage::SharedPtr - get_complex_value_shared(const std::string & name); + get_complex_value_shared( + const std::string & name, + rcl_allocator_t allocator = rcl_get_default_allocator()); RCLCPP_PUBLIC void @@ -405,14 +407,15 @@ class DynamicMessage : public std::enable_shared_from_this // DynamicSerializationSupport DynamicSerializationSupport::SharedPtr serialization_support_; - std::shared_ptr rosidl_dynamic_data_; - + rosidl_dynamic_typesupport_dynamic_data_t rosidl_dynamic_data_; bool is_loaned_; // Used for returning the loaned value, and lifetime management DynamicMessage::SharedPtr parent_data_; private: + RCLCPP_DISABLE_COPY(DynamicMessage) + RCLCPP_PUBLIC DynamicMessage(); @@ -423,7 +426,6 @@ class DynamicMessage : public std::enable_shared_from_this const rosidl_dynamic_typesupport_dynamic_data_t & dynamic_data); }; - } // namespace dynamic_typesupport } // namespace rclcpp diff --git a/rclcpp/include/rclcpp/dynamic_typesupport/dynamic_message_type.hpp b/rclcpp/include/rclcpp/dynamic_typesupport/dynamic_message_type.hpp index c2402aad7b..beaaee08a5 100644 --- a/rclcpp/include/rclcpp/dynamic_typesupport/dynamic_message_type.hpp +++ b/rclcpp/include/rclcpp/dynamic_typesupport/dynamic_message_type.hpp @@ -15,6 +15,7 @@ #ifndef RCLCPP__DYNAMIC_TYPESUPPORT__DYNAMIC_MESSAGE_TYPE_HPP_ #define RCLCPP__DYNAMIC_TYPESUPPORT__DYNAMIC_MESSAGE_TYPE_HPP_ +#include #include #include @@ -24,21 +25,18 @@ #include "rclcpp/macros.hpp" #include "rclcpp/visibility_control.hpp" - namespace rclcpp { namespace dynamic_typesupport { - class DynamicMessage; class DynamicMessageTypeBuilder; -/// Utility wrapper class for `rosidl_dynamic_typesupport_dynamic_type_t *` +/// Utility wrapper class for `rosidl_dynamic_typesupport_dynamic_type_t` /** * This class: - * - Manages the lifetime of the raw pointer. - * - Exposes getter methods to get the raw pointer and shared pointers + * - Exposes getter methods for the struct * - Exposes the underlying serialization support API * * Ownership: @@ -71,40 +69,34 @@ class DynamicMessageType : public std::enable_shared_from_this dynamic_type_builder); - /// Assume ownership of raw pointer + /// Construct a new `DynamicMessageType` with the provided dynamic type builder and allocator RCLCPP_PUBLIC DynamicMessageType( - DynamicSerializationSupport::SharedPtr serialization_support, - rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type); + std::shared_ptr dynamic_type_builder, + rcl_allocator_t allocator); - /// Copy shared pointer + /// Assume ownership of struct RCLCPP_PUBLIC DynamicMessageType( DynamicSerializationSupport::SharedPtr serialization_support, - std::shared_ptr rosidl_dynamic_type); + rosidl_dynamic_typesupport_dynamic_type_t && rosidl_dynamic_type); /// From description RCLCPP_PUBLIC DynamicMessageType( DynamicSerializationSupport::SharedPtr serialization_support, - const rosidl_runtime_c__type_description__TypeDescription & description); - - /// Copy constructor - RCLCPP_PUBLIC - DynamicMessageType(const DynamicMessageType & other); + const rosidl_runtime_c__type_description__TypeDescription & description, + rcl_allocator_t allocator = rcl_get_default_allocator()); /// Move constructor RCLCPP_PUBLIC DynamicMessageType(DynamicMessageType && other) noexcept; - /// Copy assignment - RCLCPP_PUBLIC - DynamicMessageType & operator=(const DynamicMessageType & other); - /// Move assignment RCLCPP_PUBLIC DynamicMessageType & operator=(DynamicMessageType && other) noexcept; @@ -113,10 +105,15 @@ class DynamicMessageType : public std::enable_shared_from_this - get_shared_rosidl_dynamic_type(); - - RCLCPP_PUBLIC - std::shared_ptr - get_shared_rosidl_dynamic_type() const; - RCLCPP_PUBLIC DynamicSerializationSupport::SharedPtr get_shared_dynamic_serialization_support(); @@ -160,11 +149,11 @@ class DynamicMessageType : public std::enable_shared_from_this - build_dynamic_message_shared(); + build_dynamic_message_shared(rcl_allocator_t allocator = rcl_get_default_allocator()); protected: // NOTE(methylDragon): @@ -187,9 +176,11 @@ class DynamicMessageType : public std::enable_shared_from_this rosidl_dynamic_type_; + rosidl_dynamic_typesupport_dynamic_type_t rosidl_dynamic_type_; private: + RCLCPP_DISABLE_COPY(DynamicMessageType) + RCLCPP_PUBLIC DynamicMessageType(); @@ -200,7 +191,6 @@ class DynamicMessageType : public std::enable_shared_from_this #include #include @@ -24,7 +25,6 @@ #include "rclcpp/macros.hpp" #include "rclcpp/visibility_control.hpp" - namespace rclcpp { namespace dynamic_typesupport @@ -69,54 +69,55 @@ class DynamicMessageTypeBuilder : public std::enable_shared_from_this dynamic_type_builder); + rosidl_dynamic_typesupport_dynamic_type_builder_t && dynamic_type_builder); - /// Copy constructor + /// From description RCLCPP_PUBLIC - DynamicMessageTypeBuilder(const DynamicMessageTypeBuilder & other); + DynamicMessageTypeBuilder( + DynamicSerializationSupport::SharedPtr serialization_support, + const rosidl_runtime_c__type_description__TypeDescription & description, + rcl_allocator_t allocator = rcl_get_default_allocator()); /// Move constructor RCLCPP_PUBLIC DynamicMessageTypeBuilder(DynamicMessageTypeBuilder && other) noexcept; - /// Copy assignment - RCLCPP_PUBLIC - DynamicMessageTypeBuilder & operator=(const DynamicMessageTypeBuilder & other); - /// Move assignment RCLCPP_PUBLIC DynamicMessageTypeBuilder & operator=(DynamicMessageTypeBuilder && other) noexcept; - /// From description - RCLCPP_PUBLIC - DynamicMessageTypeBuilder( - DynamicSerializationSupport::SharedPtr serialization_support, - const rosidl_runtime_c__type_description__TypeDescription & description); - RCLCPP_PUBLIC virtual ~DynamicMessageTypeBuilder(); /// Swaps the serialization support if serialization_support is populated + /** + * The user can call this with another description to reconfigure the type without changing the + * serialization support + */ RCLCPP_PUBLIC void init_from_description( const rosidl_runtime_c__type_description__TypeDescription & description, + rcl_allocator_t allocator = rcl_get_default_allocator(), DynamicSerializationSupport::SharedPtr serialization_support = nullptr); @@ -130,21 +131,13 @@ class DynamicMessageTypeBuilder : public std::enable_shared_from_this - get_shared_rosidl_dynamic_type_builder(); - - RCLCPP_PUBLIC - std::shared_ptr - get_shared_rosidl_dynamic_type_builder() const; - RCLCPP_PUBLIC DynamicSerializationSupport::SharedPtr get_shared_dynamic_serialization_support(); @@ -161,31 +154,31 @@ class DynamicMessageTypeBuilder : public std::enable_shared_from_this rosidl_dynamic_type_builder_; + rosidl_dynamic_typesupport_dynamic_type_builder_t rosidl_dynamic_type_builder_; private: + RCLCPP_DISABLE_COPY(DynamicMessageTypeBuilder) + RCLCPP_PUBLIC DynamicMessageTypeBuilder(); @@ -397,7 +392,8 @@ class DynamicMessageTypeBuilder : public std::enable_shared_from_this #include +#include "rcl/allocator.h" #include "rcl/types.h" #include "rcutils/logging_macros.h" @@ -43,111 +44,81 @@ using rclcpp::dynamic_typesupport::DynamicSerializationSupport; // CONSTRUCTION ================================================================================== DynamicMessage::DynamicMessage(const DynamicMessageTypeBuilder::SharedPtr dynamic_type_builder) +: DynamicMessage::DynamicMessage( + dynamic_type_builder, dynamic_type_builder->get_rosidl_dynamic_type_builder().allocator) {} + + +DynamicMessage::DynamicMessage( + const DynamicMessageTypeBuilder::SharedPtr dynamic_type_builder, + rcl_allocator_t allocator) : serialization_support_(dynamic_type_builder->get_shared_dynamic_serialization_support()), - rosidl_dynamic_data_(nullptr), + rosidl_dynamic_data_(rosidl_dynamic_typesupport_get_zero_initialized_dynamic_data()), is_loaned_(false), parent_data_(nullptr) { if (!serialization_support_) { - throw std::runtime_error("dynamic type could not bind serialization support!"); + throw std::runtime_error("dynamic message could not bind serialization support!"); + } + if (!dynamic_type_builder) { + throw std::runtime_error("dynamic message type builder cannot be nullptr!"); } - rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder = + rosidl_dynamic_typesupport_dynamic_type_builder_t rosidl_dynamic_type_builder = dynamic_type_builder->get_rosidl_dynamic_type_builder(); - if (!rosidl_dynamic_type_builder) { - throw std::runtime_error("dynamic type builder cannot be nullptr!"); - } - rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data = nullptr; - rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_create_from_dynamic_type_builder( - rosidl_dynamic_type_builder, &rosidl_dynamic_data); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_data) { - throw std::runtime_error("could not create new dynamic data object from dynamic type builder"); + rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_init_from_dynamic_type_builder( + &rosidl_dynamic_type_builder, &allocator, &get_rosidl_dynamic_data()); + if (ret != RCUTILS_RET_OK) { + throw std::runtime_error("could not init new dynamic data object from dynamic type builder"); } - - rosidl_dynamic_data_.reset( - rosidl_dynamic_data, - // Custom deleter - [](rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data)->void { - rosidl_dynamic_typesupport_dynamic_data_destroy(rosidl_dynamic_data); - }); } DynamicMessage::DynamicMessage(const DynamicMessageType::SharedPtr dynamic_type) +: DynamicMessage::DynamicMessage( + dynamic_type, dynamic_type->get_rosidl_dynamic_type().allocator) {} + + +DynamicMessage::DynamicMessage( + const DynamicMessageType::SharedPtr dynamic_type, + rcl_allocator_t allocator) : serialization_support_(dynamic_type->get_shared_dynamic_serialization_support()), - rosidl_dynamic_data_(nullptr), + rosidl_dynamic_data_(rosidl_dynamic_typesupport_get_zero_initialized_dynamic_data()), is_loaned_(false), parent_data_(nullptr) { if (!serialization_support_) { throw std::runtime_error("dynamic type could not bind serialization support!"); } + if (!dynamic_type) { + throw std::runtime_error("dynamic message type cannot be nullptr!"); + } - rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type = + rosidl_dynamic_typesupport_dynamic_type_t rosidl_dynamic_type = dynamic_type->get_rosidl_dynamic_type(); - if (!rosidl_dynamic_type) { - throw std::runtime_error("dynamic type cannot be nullptr!"); - } - rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data = nullptr; - rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_create_from_dynamic_type( - rosidl_dynamic_type, &rosidl_dynamic_data); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_data) { + rosidl_dynamic_typesupport_dynamic_data_t rosidl_dynamic_data = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_data(); + rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_init_from_dynamic_type( + &rosidl_dynamic_type, &allocator, &rosidl_dynamic_data); + if (ret != RCUTILS_RET_OK) { throw std::runtime_error( - std::string("could not create new dynamic data object from dynamic type") + + std::string("could not init new dynamic data object from dynamic type") + rcl_get_error_string().str); } - - rosidl_dynamic_data_.reset( - rosidl_dynamic_data, - // Custom deleter - [](rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data)->void { - rosidl_dynamic_typesupport_dynamic_data_destroy(rosidl_dynamic_data); - }); -} - - -DynamicMessage::DynamicMessage( - DynamicSerializationSupport::SharedPtr serialization_support, - rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data) -: serialization_support_(serialization_support), - rosidl_dynamic_data_(nullptr), - is_loaned_(false), - parent_data_(nullptr) -{ - if (!rosidl_dynamic_data) { - throw std::runtime_error("rosidl dynamic data cannot be nullptr!"); - } - if (serialization_support) { - if (!match_serialization_support_(*serialization_support, *rosidl_dynamic_data)) { - throw std::runtime_error( - "serialization support library identifier does not match dynamic data's!"); - } - } - - rosidl_dynamic_data_.reset( - rosidl_dynamic_data, - // Custom deleter - [](rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data)->void { - rosidl_dynamic_typesupport_dynamic_data_destroy(rosidl_dynamic_data); - }); } DynamicMessage::DynamicMessage( DynamicSerializationSupport::SharedPtr serialization_support, - std::shared_ptr rosidl_dynamic_data) + rosidl_dynamic_typesupport_dynamic_data_t && rosidl_dynamic_data) : serialization_support_(serialization_support), - rosidl_dynamic_data_(rosidl_dynamic_data), + rosidl_dynamic_data_(std::move(rosidl_dynamic_data)), is_loaned_(false), parent_data_(nullptr) { - if (!rosidl_dynamic_data) { - throw std::runtime_error("rosidl dynamic data cannot be nullptr!"); - } if (serialization_support) { - if (!match_serialization_support_(*serialization_support, *rosidl_dynamic_data)) { + if (!match_serialization_support_(*serialization_support, rosidl_dynamic_data)) { throw std::runtime_error( "serialization support library identifier does not match dynamic data's!"); } @@ -157,66 +128,35 @@ DynamicMessage::DynamicMessage( DynamicMessage::DynamicMessage( DynamicMessage::SharedPtr parent_data, - rosidl_dynamic_typesupport_dynamic_data_t * rosidl_loaned_data) + rosidl_dynamic_typesupport_dynamic_data_t && rosidl_loaned_data) : serialization_support_(parent_data->get_shared_dynamic_serialization_support()), - rosidl_dynamic_data_(nullptr), + rosidl_dynamic_data_(std::move(rosidl_loaned_data)), is_loaned_(true), parent_data_(nullptr) { if (!parent_data) { throw std::runtime_error("parent dynamic data cannot be nullptr!"); } - if (!rosidl_loaned_data) { - throw std::runtime_error("loaned rosidl dynamic data cannot be nullptr!"); - } - if (serialization_support_) { - if (!match_serialization_support_(*serialization_support_, *rosidl_loaned_data)) { + if (!match_serialization_support_(*serialization_support_, rosidl_loaned_data)) { throw std::runtime_error( "serialization support library identifier does not match loaned dynamic data's!"); } } - - rosidl_dynamic_data_.reset( - rosidl_loaned_data, - // Custom no-op deleter - [](rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data)->void { - // Data fini and destruction is deferred to return_loaned_value() - (void) rosidl_dynamic_data; - }); parent_data_ = parent_data; } -DynamicMessage::DynamicMessage(const DynamicMessage & other) -: enable_shared_from_this(), - serialization_support_(nullptr), - rosidl_dynamic_data_(nullptr), - is_loaned_(false), - parent_data_(nullptr) -{ - DynamicMessage out = other.clone(); - // We don't copy is_loaned_ or parent_data_ because it's a fresh copy now - std::swap(serialization_support_, out.serialization_support_); - std::swap(rosidl_dynamic_data_, out.rosidl_dynamic_data_); -} - - DynamicMessage::DynamicMessage(DynamicMessage && other) noexcept : serialization_support_(std::exchange(other.serialization_support_, nullptr)), - rosidl_dynamic_data_(std::exchange(other.rosidl_dynamic_data_, nullptr)), + rosidl_dynamic_data_(std::exchange( + other.rosidl_dynamic_data_, + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_data())), is_loaned_(other.is_loaned_), parent_data_(std::exchange(other.parent_data_, nullptr)) {} -DynamicMessage & -DynamicMessage::operator=(const DynamicMessage & other) -{ - return *this = DynamicMessage(other); -} - - DynamicMessage & DynamicMessage::operator=(DynamicMessage && other) noexcept { @@ -230,13 +170,21 @@ DynamicMessage::operator=(DynamicMessage && other) noexcept DynamicMessage::~DynamicMessage() { - if (is_loaned_) { - if (!parent_data_) { - RCUTILS_LOG_ERROR("dynamic data is loaned, but parent is missing!!"); - } else { - rosidl_dynamic_typesupport_dynamic_data_return_loaned_value( - parent_data_->get_rosidl_dynamic_data(), get_rosidl_dynamic_data()); + if (!is_loaned_) { + if (rosidl_dynamic_typesupport_dynamic_data_fini(&get_rosidl_dynamic_data()) != + RCUTILS_RET_OK) + { + RCUTILS_LOG_ERROR("could not fini rosidl dynamic data"); } + return; + } + + // Loaned case + if (!parent_data_) { + RCUTILS_LOG_ERROR("dynamic data is loaned, but parent is missing!!"); + } else { + rosidl_dynamic_typesupport_dynamic_data_return_loaned_value( + &parent_data_->get_rosidl_dynamic_data(), &get_rosidl_dynamic_data()); } } @@ -246,16 +194,13 @@ DynamicMessage::match_serialization_support_( const DynamicSerializationSupport & serialization_support, const rosidl_dynamic_typesupport_dynamic_data_t & rosidl_dynamic_type_data) { - bool out = true; - if (serialization_support.get_serialization_library_identifier() != std::string( rosidl_dynamic_type_data.serialization_support->serialization_library_identifier)) { RCUTILS_LOG_ERROR("serialization support library identifier does not match dynamic data's"); - out = false; + return false; } - - return out; + return true; } @@ -263,7 +208,8 @@ DynamicMessage::match_serialization_support_( const std::string DynamicMessage::get_serialization_library_identifier() const { - return std::string(rosidl_dynamic_data_->serialization_support->serialization_library_identifier); + return std::string( + get_rosidl_dynamic_data().serialization_support->serialization_library_identifier); } @@ -274,7 +220,7 @@ DynamicMessage::get_name() const const char * buf; if ( rosidl_dynamic_typesupport_dynamic_data_get_name( - get_rosidl_dynamic_data(), &buf, + &get_rosidl_dynamic_data(), &buf, &buf_length) != RCUTILS_RET_OK) { @@ -285,33 +231,17 @@ DynamicMessage::get_name() const } -rosidl_dynamic_typesupport_dynamic_data_t * +rosidl_dynamic_typesupport_dynamic_data_t & DynamicMessage::get_rosidl_dynamic_data() { - return rosidl_dynamic_data_.get(); + return rosidl_dynamic_data_; } -const rosidl_dynamic_typesupport_dynamic_data_t * +const rosidl_dynamic_typesupport_dynamic_data_t & DynamicMessage::get_rosidl_dynamic_data() const { - return rosidl_dynamic_data_.get(); -} - - -std::shared_ptr -DynamicMessage::get_shared_rosidl_dynamic_data() -{ - return std::shared_ptr( - shared_from_this(), rosidl_dynamic_data_.get()); -} - - -std::shared_ptr -DynamicMessage::get_shared_rosidl_dynamic_data() const -{ - return std::shared_ptr( - shared_from_this(), rosidl_dynamic_data_.get()); + return rosidl_dynamic_data_; } @@ -334,7 +264,7 @@ DynamicMessage::get_item_count() const { size_t item_count; rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_get_item_count( - get_rosidl_dynamic_data(), &item_count); + &get_rosidl_dynamic_data(), &item_count); if (ret != RCUTILS_RET_OK) { throw std::runtime_error("could not get item count of dynamic data"); } @@ -347,7 +277,7 @@ DynamicMessage::get_member_id(size_t index) const { rosidl_dynamic_typesupport_member_id_t member_id; rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_get_member_id_at_index( - get_rosidl_dynamic_data(), index, &member_id); + &get_rosidl_dynamic_data(), index, &member_id); if (ret != RCUTILS_RET_OK) { throw std::runtime_error("could not member id of dynamic data element by index"); } @@ -360,7 +290,7 @@ DynamicMessage::get_member_id(const std::string & name) const { rosidl_dynamic_typesupport_member_id_t member_id; rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_get_member_id_by_name( - get_rosidl_dynamic_data(), name.c_str(), name.size(), &member_id); + &get_rosidl_dynamic_data(), name.c_str(), name.size(), &member_id); if (ret != RCUTILS_RET_OK) { throw std::runtime_error("could not member id of dynamic data element by name"); } @@ -373,7 +303,7 @@ DynamicMessage::get_array_index(size_t index) const { rosidl_dynamic_typesupport_member_id_t array_index; rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_get_array_index( - get_rosidl_dynamic_data(), index, &array_index); + &get_rosidl_dynamic_data(), index, &array_index); if (ret != RCUTILS_RET_OK) { throw std::runtime_error("could not array index of dynamic data element by index"); } @@ -390,56 +320,61 @@ DynamicMessage::get_array_index(const std::string & name) const // METHODS ======================================================================================= DynamicMessage -DynamicMessage::clone() const +DynamicMessage::clone(rcl_allocator_t allocator) { - rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data = nullptr; + rosidl_dynamic_typesupport_dynamic_data_t rosidl_dynamic_data = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_data(); rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_clone( - get_rosidl_dynamic_data(), &rosidl_dynamic_data); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_data) { + &get_rosidl_dynamic_data(), &allocator, &rosidl_dynamic_data); + if (ret != RCUTILS_RET_OK) { throw std::runtime_error( std::string("could not clone dynamic data: ") + rcl_get_error_string().str); } - return DynamicMessage(serialization_support_, rosidl_dynamic_data); + return DynamicMessage(get_shared_dynamic_serialization_support(), std::move(rosidl_dynamic_data)); } DynamicMessage::SharedPtr -DynamicMessage::clone_shared() const +DynamicMessage::clone_shared(rcl_allocator_t allocator) { - rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data = nullptr; + rosidl_dynamic_typesupport_dynamic_data_t rosidl_dynamic_data = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_data(); rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_clone( - get_rosidl_dynamic_data(), &rosidl_dynamic_data); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_data) { + &get_rosidl_dynamic_data(), &allocator, &rosidl_dynamic_data); + if (ret != RCUTILS_RET_OK) { throw std::runtime_error( std::string("could not clone dynamic data: ") + rcl_get_error_string().str); } - return DynamicMessage::make_shared(serialization_support_, rosidl_dynamic_data); + return DynamicMessage::make_shared( + get_shared_dynamic_serialization_support(), std::move(rosidl_dynamic_data)); } DynamicMessage -DynamicMessage::init_from_type(DynamicMessageType & type) const +DynamicMessage::init_from_type(DynamicMessageType & type, rcl_allocator_t allocator) const { - rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data = nullptr; - rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_create_from_dynamic_type( - type.get_rosidl_dynamic_type(), &rosidl_dynamic_data); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_data) { - throw std::runtime_error("could not create new dynamic data object from dynamic type"); + rosidl_dynamic_typesupport_dynamic_data_t rosidl_dynamic_data = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_data(); + rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_init_from_dynamic_type( + &type.get_rosidl_dynamic_type(), &allocator, &rosidl_dynamic_data); + if (ret != RCUTILS_RET_OK) { + throw std::runtime_error("could not init new dynamic data object from dynamic type"); } - return DynamicMessage(serialization_support_, rosidl_dynamic_data); + return DynamicMessage(serialization_support_, std::move(rosidl_dynamic_data)); } DynamicMessage::SharedPtr -DynamicMessage::init_from_type_shared(DynamicMessageType & type) const +DynamicMessage::init_from_type_shared(DynamicMessageType & type, rcl_allocator_t allocator) const { - rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data = nullptr; - rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_create_from_dynamic_type( - type.get_rosidl_dynamic_type(), &rosidl_dynamic_data); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_data) { - throw std::runtime_error("could not create new dynamic data object from dynamic type"); + rosidl_dynamic_typesupport_dynamic_data_t rosidl_dynamic_data = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_data(); + rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_init_from_dynamic_type( + &type.get_rosidl_dynamic_type(), &allocator, &rosidl_dynamic_data); + if (ret != RCUTILS_RET_OK) { + throw std::runtime_error("could not init new dynamic data object from dynamic type"); } - return DynamicMessage::make_shared(serialization_support_, rosidl_dynamic_data); + return DynamicMessage::make_shared(serialization_support_, std::move(rosidl_dynamic_data)); } @@ -451,7 +386,7 @@ DynamicMessage::equals(const DynamicMessage & other) const } bool equals; rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_equals( - get_rosidl_dynamic_data(), other.get_rosidl_dynamic_data(), &equals); + &get_rosidl_dynamic_data(), &other.get_rosidl_dynamic_data(), &equals); if (ret != RCUTILS_RET_OK) { throw std::runtime_error("could not equate dynamic messages"); } @@ -460,44 +395,47 @@ DynamicMessage::equals(const DynamicMessage & other) const DynamicMessage::SharedPtr -DynamicMessage::loan_value(rosidl_dynamic_typesupport_member_id_t id) +DynamicMessage::loan_value( + rosidl_dynamic_typesupport_member_id_t id, + rcl_allocator_t allocator) { - rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data = nullptr; + rosidl_dynamic_typesupport_dynamic_data_t rosidl_dynamic_data = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_data(); rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_loan_value( - get_rosidl_dynamic_data(), id, &rosidl_dynamic_data); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_data) { + &get_rosidl_dynamic_data(), id, &allocator, &rosidl_dynamic_data); + if (ret != RCUTILS_RET_OK) { throw std::runtime_error( std::string("could not loan dynamic data: ") + rcl_get_error_string().str); } - return DynamicMessage::make_shared(shared_from_this(), rosidl_dynamic_data); + return DynamicMessage::make_shared(shared_from_this(), std::move(rosidl_dynamic_data)); } DynamicMessage::SharedPtr -DynamicMessage::loan_value(const std::string & name) +DynamicMessage::loan_value(const std::string & name, rcl_allocator_t allocator) { - return loan_value(get_member_id(name)); + return loan_value(get_member_id(name), allocator); } void DynamicMessage::clear_all_values() { - rosidl_dynamic_typesupport_dynamic_data_clear_all_values(get_rosidl_dynamic_data()); + rosidl_dynamic_typesupport_dynamic_data_clear_all_values(&get_rosidl_dynamic_data()); } void DynamicMessage::clear_nonkey_values() { - rosidl_dynamic_typesupport_dynamic_data_clear_nonkey_values(get_rosidl_dynamic_data()); + rosidl_dynamic_typesupport_dynamic_data_clear_nonkey_values(&get_rosidl_dynamic_data()); } void DynamicMessage::clear_value(rosidl_dynamic_typesupport_member_id_t id) { - rosidl_dynamic_typesupport_dynamic_data_clear_value(get_rosidl_dynamic_data(), id); + rosidl_dynamic_typesupport_dynamic_data_clear_value(&get_rosidl_dynamic_data(), id); } @@ -511,7 +449,7 @@ DynamicMessage::clear_value(const std::string & name) void DynamicMessage::clear_sequence() { - rosidl_dynamic_typesupport_dynamic_data_clear_sequence_data(get_rosidl_dynamic_data()); + rosidl_dynamic_typesupport_dynamic_data_clear_sequence_data(&get_rosidl_dynamic_data()); } @@ -519,7 +457,7 @@ rosidl_dynamic_typesupport_member_id_t DynamicMessage::insert_sequence_data() { rosidl_dynamic_typesupport_member_id_t out; - rosidl_dynamic_typesupport_dynamic_data_insert_sequence_data(get_rosidl_dynamic_data(), &out); + rosidl_dynamic_typesupport_dynamic_data_insert_sequence_data(&get_rosidl_dynamic_data(), &out); return out; } @@ -528,37 +466,33 @@ void DynamicMessage::remove_sequence_data(rosidl_dynamic_typesupport_member_id_t index) { rosidl_dynamic_typesupport_dynamic_data_remove_sequence_data( - get_rosidl_dynamic_data(), index); + &get_rosidl_dynamic_data(), index); } bool -DynamicMessage::serialize(rcl_serialized_message_t * buffer) +DynamicMessage::serialize(rcl_serialized_message_t & buffer) { - bool success; - rcutils_ret_t ret = - rosidl_dynamic_typesupport_dynamic_data_serialize(get_rosidl_dynamic_data(), buffer, &success); - if (ret != RCUTILS_RET_OK || !success) { + rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_serialize( + &get_rosidl_dynamic_data(), &buffer); + if (ret != RCUTILS_RET_OK) { throw std::runtime_error( std::string("could serialize loan dynamic data: ") + rcl_get_error_string().str); } - return success; + return true; } bool -DynamicMessage::deserialize(rcl_serialized_message_t * buffer) -{ - bool success; - rcutils_ret_t ret = - rosidl_dynamic_typesupport_dynamic_data_deserialize( - get_rosidl_dynamic_data(), buffer, - &success); - if (ret != RCUTILS_RET_OK || !success) { +DynamicMessage::deserialize(rcl_serialized_message_t & buffer) +{ + rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_deserialize( + &get_rosidl_dynamic_data(), &buffer); + if (ret != RCUTILS_RET_OK) { throw std::runtime_error( std::string("could deserialize loan dynamic data: ") + rcl_get_error_string().str); } - return success; + return true; } @@ -574,7 +508,7 @@ DynamicMessage::get_fixed_string_value( size_t buf_length; char * buf = nullptr; rosidl_dynamic_typesupport_dynamic_data_get_fixed_string_value( - get_rosidl_dynamic_data(), id, &buf, &buf_length, string_length); + &get_rosidl_dynamic_data(), id, &buf, &buf_length, string_length); auto out = std::string(buf, buf_length); delete buf; return out; @@ -595,7 +529,7 @@ DynamicMessage::get_fixed_wstring_value( size_t buf_length; char16_t * buf = nullptr; rosidl_dynamic_typesupport_dynamic_data_get_fixed_wstring_value( - get_rosidl_dynamic_data(), id, &buf, &buf_length, wstring_length); + &get_rosidl_dynamic_data(), id, &buf, &buf_length, wstring_length); auto out = std::u16string(buf, buf_length); delete buf; return out; @@ -614,7 +548,7 @@ DynamicMessage::set_fixed_string_value( rosidl_dynamic_typesupport_member_id_t id, const std::string value, size_t string_length) { rosidl_dynamic_typesupport_dynamic_data_set_fixed_string_value( - get_rosidl_dynamic_data(), id, value.c_str(), value.size(), string_length); + &get_rosidl_dynamic_data(), id, value.c_str(), value.size(), string_length); } @@ -631,7 +565,7 @@ DynamicMessage::set_fixed_wstring_value( rosidl_dynamic_typesupport_member_id_t id, const std::u16string value, size_t wstring_length) { rosidl_dynamic_typesupport_dynamic_data_set_fixed_wstring_value( - get_rosidl_dynamic_data(), id, value.c_str(), value.size(), wstring_length); + &get_rosidl_dynamic_data(), id, value.c_str(), value.size(), wstring_length); } @@ -648,7 +582,7 @@ DynamicMessage::insert_fixed_string_value(const std::string value, size_t string { rosidl_dynamic_typesupport_member_id_t out; rosidl_dynamic_typesupport_dynamic_data_insert_fixed_string_value( - get_rosidl_dynamic_data(), value.c_str(), value.size(), string_length, &out); + &get_rosidl_dynamic_data(), value.c_str(), value.size(), string_length, &out); return out; } @@ -658,7 +592,7 @@ DynamicMessage::insert_fixed_wstring_value(const std::u16string value, size_t ws { rosidl_dynamic_typesupport_member_id_t out; rosidl_dynamic_typesupport_dynamic_data_insert_fixed_wstring_value( - get_rosidl_dynamic_data(), value.c_str(), value.size(), wstring_length, &out); + &get_rosidl_dynamic_data(), value.c_str(), value.size(), wstring_length, &out); return out; } @@ -671,7 +605,7 @@ DynamicMessage::get_bounded_string_value( size_t buf_length; char * buf = nullptr; rosidl_dynamic_typesupport_dynamic_data_get_bounded_string_value( - get_rosidl_dynamic_data(), id, &buf, &buf_length, string_bound); + &get_rosidl_dynamic_data(), id, &buf, &buf_length, string_bound); auto out = std::string(buf, buf_length); delete buf; return out; @@ -692,7 +626,7 @@ DynamicMessage::get_bounded_wstring_value( size_t buf_length; char16_t * buf = nullptr; rosidl_dynamic_typesupport_dynamic_data_get_bounded_wstring_value( - get_rosidl_dynamic_data(), id, &buf, &buf_length, wstring_bound); + &get_rosidl_dynamic_data(), id, &buf, &buf_length, wstring_bound); auto out = std::u16string(buf, buf_length); delete buf; return out; @@ -711,7 +645,7 @@ DynamicMessage::set_bounded_string_value( rosidl_dynamic_typesupport_member_id_t id, const std::string value, size_t string_bound) { rosidl_dynamic_typesupport_dynamic_data_set_bounded_string_value( - get_rosidl_dynamic_data(), id, value.c_str(), value.size(), string_bound); + &get_rosidl_dynamic_data(), id, value.c_str(), value.size(), string_bound); } @@ -728,7 +662,7 @@ DynamicMessage::set_bounded_wstring_value( rosidl_dynamic_typesupport_member_id_t id, const std::u16string value, size_t wstring_bound) { rosidl_dynamic_typesupport_dynamic_data_set_bounded_wstring_value( - get_rosidl_dynamic_data(), id, value.c_str(), value.size(), wstring_bound); + &get_rosidl_dynamic_data(), id, value.c_str(), value.size(), wstring_bound); } @@ -745,7 +679,7 @@ DynamicMessage::insert_bounded_string_value(const std::string value, size_t stri { rosidl_dynamic_typesupport_member_id_t out; rosidl_dynamic_typesupport_dynamic_data_insert_bounded_string_value( - get_rosidl_dynamic_data(), value.c_str(), value.size(), string_bound, &out); + &get_rosidl_dynamic_data(), value.c_str(), value.size(), string_bound, &out); return out; } @@ -755,43 +689,47 @@ DynamicMessage::insert_bounded_wstring_value(const std::u16string value, size_t { rosidl_dynamic_typesupport_member_id_t out; rosidl_dynamic_typesupport_dynamic_data_insert_bounded_wstring_value( - get_rosidl_dynamic_data(), value.c_str(), value.size(), wstring_bound, &out); + &get_rosidl_dynamic_data(), value.c_str(), value.size(), wstring_bound, &out); return out; } // NESTED MEMBER ACCESS ============================================================================ DynamicMessage -DynamicMessage::get_complex_value(rosidl_dynamic_typesupport_member_id_t id) +DynamicMessage::get_complex_value( + rosidl_dynamic_typesupport_member_id_t id, rcl_allocator_t allocator) { - rosidl_dynamic_typesupport_dynamic_data_t * out_ptr = nullptr; + rosidl_dynamic_typesupport_dynamic_data_t out = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_data(); rosidl_dynamic_typesupport_dynamic_data_get_complex_value( - get_rosidl_dynamic_data(), id, &out_ptr); - return DynamicMessage(get_shared_dynamic_serialization_support(), out_ptr); + &get_rosidl_dynamic_data(), id, &allocator, &out); + return DynamicMessage(get_shared_dynamic_serialization_support(), std::move(out)); } DynamicMessage -DynamicMessage::get_complex_value(const std::string & name) +DynamicMessage::get_complex_value(const std::string & name, rcl_allocator_t allocator) { - return get_complex_value(get_member_id(name)); + return get_complex_value(get_member_id(name), allocator); } DynamicMessage::SharedPtr -DynamicMessage::get_complex_value_shared(rosidl_dynamic_typesupport_member_id_t id) +DynamicMessage::get_complex_value_shared( + rosidl_dynamic_typesupport_member_id_t id, rcl_allocator_t allocator) { - rosidl_dynamic_typesupport_dynamic_data_t * out_ptr = nullptr; + rosidl_dynamic_typesupport_dynamic_data_t out = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_data(); rosidl_dynamic_typesupport_dynamic_data_get_complex_value( - get_rosidl_dynamic_data(), id, &out_ptr); - return DynamicMessage::make_shared(get_shared_dynamic_serialization_support(), out_ptr); + &get_rosidl_dynamic_data(), id, &allocator, &out); + return DynamicMessage::make_shared(get_shared_dynamic_serialization_support(), std::move(out)); } DynamicMessage::SharedPtr -DynamicMessage::get_complex_value_shared(const std::string & name) +DynamicMessage::get_complex_value_shared(const std::string & name, rcl_allocator_t allocator) { - return get_complex_value_shared(get_member_id(name)); + return get_complex_value_shared(get_member_id(name), allocator); } @@ -800,7 +738,7 @@ DynamicMessage::set_complex_value( rosidl_dynamic_typesupport_member_id_t id, DynamicMessage & value) { rosidl_dynamic_typesupport_dynamic_data_set_complex_value( - get_rosidl_dynamic_data(), id, value.get_rosidl_dynamic_data()); + &get_rosidl_dynamic_data(), id, &value.get_rosidl_dynamic_data()); } @@ -816,7 +754,7 @@ DynamicMessage::insert_complex_value_copy(const DynamicMessage & value) { rosidl_dynamic_typesupport_member_id_t out; rosidl_dynamic_typesupport_dynamic_data_insert_complex_value_copy( - get_rosidl_dynamic_data(), value.get_rosidl_dynamic_data(), &out); + &get_rosidl_dynamic_data(), &value.get_rosidl_dynamic_data(), &out); return out; } @@ -826,6 +764,6 @@ DynamicMessage::insert_complex_value(DynamicMessage & value) { rosidl_dynamic_typesupport_member_id_t out; rosidl_dynamic_typesupport_dynamic_data_insert_complex_value( - get_rosidl_dynamic_data(), value.get_rosidl_dynamic_data(), &out); + &get_rosidl_dynamic_data(), &value.get_rosidl_dynamic_data(), &out); return out; } diff --git a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type.cpp b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type.cpp index 4daa44c35a..ee3efd7cfb 100644 --- a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type.cpp +++ b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type.cpp @@ -37,69 +37,39 @@ using rclcpp::dynamic_typesupport::DynamicSerializationSupport; // CONSTRUCTION ==================================================================================== DynamicMessageType::DynamicMessageType(DynamicMessageTypeBuilder::SharedPtr dynamic_type_builder) +: DynamicMessageType::DynamicMessageType( + dynamic_type_builder, dynamic_type_builder->get_rosidl_dynamic_type_builder().allocator) {} + + +DynamicMessageType::DynamicMessageType( + DynamicMessageTypeBuilder::SharedPtr dynamic_type_builder, + rcl_allocator_t allocator) : serialization_support_(dynamic_type_builder->get_shared_dynamic_serialization_support()), - rosidl_dynamic_type_(nullptr) + rosidl_dynamic_type_(rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type()) { if (!serialization_support_) { throw std::runtime_error("dynamic type could not bind serialization support!"); } - rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder = + rosidl_dynamic_typesupport_dynamic_type_builder_t rosidl_dynamic_type_builder = dynamic_type_builder->get_rosidl_dynamic_type_builder(); - if (!rosidl_dynamic_type_builder) { - throw std::runtime_error("dynamic type builder cannot be nullptr!"); - } - - rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type = nullptr; - rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_create_from_dynamic_type_builder( - rosidl_dynamic_type_builder, &rosidl_dynamic_type); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_type) { - throw std::runtime_error("could not create new dynamic type object"); - } - rosidl_dynamic_type_.reset( - rosidl_dynamic_type, - // Custom deleter - [](rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type)->void { - rosidl_dynamic_typesupport_dynamic_type_destroy(rosidl_dynamic_type); - }); -} - - -DynamicMessageType::DynamicMessageType( - DynamicSerializationSupport::SharedPtr serialization_support, - rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type) -: serialization_support_(serialization_support), rosidl_dynamic_type_(nullptr) -{ - if (!rosidl_dynamic_type) { - throw std::runtime_error("rosidl dynamic type cannot be nullptr!"); - } - if (serialization_support) { - if (!match_serialization_support_(*serialization_support, *rosidl_dynamic_type)) { - throw std::runtime_error( - "serialization support library identifier does not match dynamic type's!"); - } + rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_init_from_dynamic_type_builder( + &rosidl_dynamic_type_builder, &allocator, &get_rosidl_dynamic_type()); + if (ret != RCUTILS_RET_OK) { + throw std::runtime_error("could not init new dynamic type object"); } - - rosidl_dynamic_type_.reset( - rosidl_dynamic_type, - // Custom deleter - [](rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type)->void { - rosidl_dynamic_typesupport_dynamic_type_destroy(rosidl_dynamic_type); - }); } DynamicMessageType::DynamicMessageType( DynamicSerializationSupport::SharedPtr serialization_support, - std::shared_ptr rosidl_dynamic_type) -: serialization_support_(serialization_support), rosidl_dynamic_type_(rosidl_dynamic_type) + rosidl_dynamic_typesupport_dynamic_type_t && rosidl_dynamic_type) +: serialization_support_(serialization_support), + rosidl_dynamic_type_(std::move(rosidl_dynamic_type)) { - if (!rosidl_dynamic_type) { - throw std::runtime_error("rosidl dynamic type cannot be nullptr!"); - } if (serialization_support) { - if (!match_serialization_support_(*serialization_support, *rosidl_dynamic_type)) { + if (!match_serialization_support_(*serialization_support, rosidl_dynamic_type)) { throw std::runtime_error( "serialization support library identifier does not match dynamic type's!"); } @@ -109,32 +79,20 @@ DynamicMessageType::DynamicMessageType( DynamicMessageType::DynamicMessageType( DynamicSerializationSupport::SharedPtr serialization_support, - const rosidl_runtime_c__type_description__TypeDescription & description) -: serialization_support_(serialization_support), rosidl_dynamic_type_(nullptr) -{ - init_from_description(description, serialization_support); -} - - -DynamicMessageType::DynamicMessageType(const DynamicMessageType & other) -: enable_shared_from_this(), serialization_support_(nullptr), rosidl_dynamic_type_(nullptr) + const rosidl_runtime_c__type_description__TypeDescription & description, + rcl_allocator_t allocator) +: serialization_support_(serialization_support), + rosidl_dynamic_type_(rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type()) { - DynamicMessageType out = other.clone(); - std::swap(serialization_support_, out.serialization_support_); - std::swap(rosidl_dynamic_type_, out.rosidl_dynamic_type_); + init_from_description(description, allocator, serialization_support); } DynamicMessageType::DynamicMessageType(DynamicMessageType && other) noexcept : serialization_support_(std::exchange(other.serialization_support_, nullptr)), - rosidl_dynamic_type_(std::exchange(other.rosidl_dynamic_type_, nullptr)) {} - - -DynamicMessageType & -DynamicMessageType::operator=(const DynamicMessageType & other) -{ - return *this = DynamicMessageType(other); -} + rosidl_dynamic_type_(std::exchange( + other.rosidl_dynamic_type_, rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type())) +{} DynamicMessageType & @@ -146,12 +104,19 @@ DynamicMessageType::operator=(DynamicMessageType && other) noexcept } -DynamicMessageType::~DynamicMessageType() {} +DynamicMessageType::~DynamicMessageType() +{ + if (rosidl_dynamic_typesupport_dynamic_type_fini(&get_rosidl_dynamic_type()) != RCUTILS_RET_OK) + { + RCUTILS_LOG_ERROR("could not fini rosidl dynamic type"); + } +} void DynamicMessageType::init_from_description( const rosidl_runtime_c__type_description__TypeDescription & description, + rcl_allocator_t allocator, DynamicSerializationSupport::SharedPtr serialization_support) { if (serialization_support) { @@ -159,21 +124,18 @@ DynamicMessageType::init_from_description( serialization_support_ = serialization_support; } - rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type = nullptr; - rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_create_from_description( + rosidl_dynamic_typesupport_dynamic_type_t rosidl_dynamic_type = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type(); + rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_init_from_description( &serialization_support_->get_rosidl_serialization_support(), &description, + &allocator, &rosidl_dynamic_type); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_type) { - throw std::runtime_error("could not create new dynamic type object"); + if (ret != RCUTILS_RET_OK) { + throw std::runtime_error("could not init new dynamic type object"); } - rosidl_dynamic_type_.reset( - rosidl_dynamic_type, - // Custom deleter - [](rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type)->void { - rosidl_dynamic_typesupport_dynamic_type_destroy(rosidl_dynamic_type); - }); + rosidl_dynamic_type_ = std::move(rosidl_dynamic_type); } @@ -182,8 +144,6 @@ DynamicMessageType::match_serialization_support_( const DynamicSerializationSupport & serialization_support, const rosidl_dynamic_typesupport_dynamic_type_t & rosidl_dynamic_type) { - bool out = true; - if (serialization_support.get_serialization_library_identifier() != std::string( rosidl_dynamic_type.serialization_support->serialization_library_identifier)) { @@ -191,10 +151,9 @@ DynamicMessageType::match_serialization_support_( "serialization support library identifier does not match dynamic type's (%s vs %s)", serialization_support.get_serialization_library_identifier().c_str(), rosidl_dynamic_type.serialization_support->serialization_library_identifier); - out = false; + return false; } - - return out; + return true; } @@ -202,7 +161,8 @@ DynamicMessageType::match_serialization_support_( const std::string DynamicMessageType::get_serialization_library_identifier() const { - return std::string(rosidl_dynamic_type_->serialization_support->serialization_library_identifier); + return std::string( + get_rosidl_dynamic_type().serialization_support->serialization_library_identifier); } @@ -211,7 +171,7 @@ DynamicMessageType::get_name() const { size_t buf_length; const char * buf; - rosidl_dynamic_typesupport_dynamic_type_get_name(get_rosidl_dynamic_type(), &buf, &buf_length); + rosidl_dynamic_typesupport_dynamic_type_get_name(&get_rosidl_dynamic_type(), &buf, &buf_length); return std::string(buf, buf_length); } @@ -221,7 +181,7 @@ DynamicMessageType::get_member_count() const { size_t out; rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_get_member_count( - rosidl_dynamic_type_.get(), &out); + &get_rosidl_dynamic_type(), &out); if (ret != RCUTILS_RET_OK) { throw std::runtime_error( std::string("could not get member count: ") + rcl_get_error_string().str); @@ -230,33 +190,17 @@ DynamicMessageType::get_member_count() const } -rosidl_dynamic_typesupport_dynamic_type_t * +rosidl_dynamic_typesupport_dynamic_type_t & DynamicMessageType::get_rosidl_dynamic_type() { - return rosidl_dynamic_type_.get(); + return rosidl_dynamic_type_; } -const rosidl_dynamic_typesupport_dynamic_type_t * +const rosidl_dynamic_typesupport_dynamic_type_t & DynamicMessageType::get_rosidl_dynamic_type() const { - return rosidl_dynamic_type_.get(); -} - - -std::shared_ptr -DynamicMessageType::get_shared_rosidl_dynamic_type() -{ - return std::shared_ptr( - shared_from_this(), rosidl_dynamic_type_.get()); -} - - -std::shared_ptr -DynamicMessageType::get_shared_rosidl_dynamic_type() const -{ - return std::shared_ptr( - shared_from_this(), rosidl_dynamic_type_.get()); + return rosidl_dynamic_type_; } @@ -276,30 +220,34 @@ DynamicMessageType::get_shared_dynamic_serialization_support() const // METHODS ========================================================================================= DynamicMessageType -DynamicMessageType::clone() const +DynamicMessageType::clone(rcl_allocator_t allocator) { - rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type = nullptr; + rosidl_dynamic_typesupport_dynamic_type_t rosidl_dynamic_type = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type(); rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_clone( - get_rosidl_dynamic_type(), &rosidl_dynamic_type); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_type) { + &get_rosidl_dynamic_type(), &allocator, &rosidl_dynamic_type); + if (ret != RCUTILS_RET_OK) { throw std::runtime_error( std::string("could not clone dynamic type: ") + rcl_get_error_string().str); } - return DynamicMessageType(serialization_support_, rosidl_dynamic_type); + return DynamicMessageType( + get_shared_dynamic_serialization_support(), std::move(rosidl_dynamic_type)); } DynamicMessageType::SharedPtr -DynamicMessageType::clone_shared() const +DynamicMessageType::clone_shared(rcl_allocator_t allocator) { - rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type = nullptr; + rosidl_dynamic_typesupport_dynamic_type_t rosidl_dynamic_type = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type(); rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_clone( - get_rosidl_dynamic_type(), &rosidl_dynamic_type); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_type) { + &get_rosidl_dynamic_type(), &allocator, &rosidl_dynamic_type); + if (ret != RCUTILS_RET_OK) { throw std::runtime_error( std::string("could not clone dynamic type: ") + rcl_get_error_string().str); } - return DynamicMessageType::make_shared(serialization_support_, rosidl_dynamic_type); + return DynamicMessageType::make_shared( + get_shared_dynamic_serialization_support(), std::move(rosidl_dynamic_type)); } @@ -311,7 +259,7 @@ DynamicMessageType::equals(const DynamicMessageType & other) const } bool out; rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_equals( - get_rosidl_dynamic_type(), other.get_rosidl_dynamic_type(), &out); + &get_rosidl_dynamic_type(), &other.get_rosidl_dynamic_type(), &out); if (ret != RCUTILS_RET_OK) { throw std::runtime_error( std::string("could not equate dynamic message types: ") + rcl_get_error_string().str); @@ -321,14 +269,14 @@ DynamicMessageType::equals(const DynamicMessageType & other) const DynamicMessage -DynamicMessageType::build_dynamic_message() +DynamicMessageType::build_dynamic_message(rcl_allocator_t allocator) { - return DynamicMessage(shared_from_this()); + return DynamicMessage(shared_from_this(), allocator); } DynamicMessage::SharedPtr -DynamicMessageType::build_dynamic_message_shared() +DynamicMessageType::build_dynamic_message_shared(rcl_allocator_t allocator) { - return DynamicMessage::make_shared(shared_from_this()); + return DynamicMessage::make_shared(shared_from_this(), allocator); } diff --git a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_builder.cpp b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_builder.cpp index a37fa3d674..ca225cf107 100644 --- a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_builder.cpp +++ b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_builder.cpp @@ -28,69 +28,48 @@ #include "rclcpp/dynamic_typesupport/dynamic_serialization_support.hpp" #include "rclcpp/exceptions.hpp" - using rclcpp::dynamic_typesupport::DynamicMessage; using rclcpp::dynamic_typesupport::DynamicMessageType; using rclcpp::dynamic_typesupport::DynamicMessageTypeBuilder; using rclcpp::dynamic_typesupport::DynamicSerializationSupport; - #ifndef RCLCPP__DYNAMIC_TYPESUPPORT__DETAIL__DYNAMIC_MESSAGE_TYPE_BUILDER_IMPL_HPP_ // Template specialization implementations #include "rclcpp/dynamic_typesupport/detail/dynamic_message_type_builder_impl.hpp" #endif - // CONSTRUCTION ==================================================================================== DynamicMessageTypeBuilder::DynamicMessageTypeBuilder( DynamicSerializationSupport::SharedPtr serialization_support, const std::string & name) -: serialization_support_(serialization_support), rosidl_dynamic_type_builder_(nullptr) -{ - init_from_serialization_support_(serialization_support, name); - if (!rosidl_dynamic_type_builder_) { - throw std::runtime_error("could not create new dynamic type builder object"); - } -} +: DynamicMessageTypeBuilder::DynamicMessageTypeBuilder( + serialization_support, + name, + serialization_support->get_rosidl_serialization_support().allocator) {} DynamicMessageTypeBuilder::DynamicMessageTypeBuilder( DynamicSerializationSupport::SharedPtr serialization_support, - rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder) -: serialization_support_(serialization_support), rosidl_dynamic_type_builder_(nullptr) + const std::string & name, + rcl_allocator_t allocator) +: serialization_support_(serialization_support), + rosidl_dynamic_type_builder_( + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type_builder()) { - if (!serialization_support) { - throw std::runtime_error("serialization support cannot be nullptr!"); - } - if (!rosidl_dynamic_type_builder) { - throw std::runtime_error("rosidl dynamic type builder cannot be nullptr!"); - } - if (!match_serialization_support_(*serialization_support, *rosidl_dynamic_type_builder)) { - throw std::runtime_error( - "serialization support library does not match dynamic type builder's!"); - } - - rosidl_dynamic_type_builder_.reset( - rosidl_dynamic_type_builder, - // Custom deleter - [](rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder)->void { - rosidl_dynamic_typesupport_dynamic_type_builder_destroy(rosidl_dynamic_type_builder); - }); + init_from_serialization_support_(serialization_support, name, allocator); } DynamicMessageTypeBuilder::DynamicMessageTypeBuilder( DynamicSerializationSupport::SharedPtr serialization_support, - std::shared_ptr rosidl_dynamic_type_builder) + rosidl_dynamic_typesupport_dynamic_type_builder_t && rosidl_dynamic_type_builder) : serialization_support_(serialization_support), - rosidl_dynamic_type_builder_(rosidl_dynamic_type_builder) + rosidl_dynamic_type_builder_( + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type_builder()) { if (!serialization_support) { throw std::runtime_error("serialization support cannot be nullptr!"); } - if (!rosidl_dynamic_type_builder) { - throw std::runtime_error("rosidl dynamic type builder cannot be nullptr!"); - } - if (!match_serialization_support_(*serialization_support, *rosidl_dynamic_type_builder.get())) { + if (!match_serialization_support_(*serialization_support, rosidl_dynamic_type_builder)) { throw std::runtime_error( "serialization support library does not match dynamic type builder's!"); } @@ -99,36 +78,24 @@ DynamicMessageTypeBuilder::DynamicMessageTypeBuilder( DynamicMessageTypeBuilder::DynamicMessageTypeBuilder( DynamicSerializationSupport::SharedPtr serialization_support, - const rosidl_runtime_c__type_description__TypeDescription & description) + const rosidl_runtime_c__type_description__TypeDescription & description, + rcl_allocator_t allocator) : serialization_support_(serialization_support), - rosidl_dynamic_type_builder_(nullptr) + rosidl_dynamic_type_builder_( + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type_builder()) { if (!serialization_support) { throw std::runtime_error("serialization support cannot be nullptr!"); } - init_from_description(description, serialization_support); -} - - -DynamicMessageTypeBuilder::DynamicMessageTypeBuilder(const DynamicMessageTypeBuilder & other) -: enable_shared_from_this(), serialization_support_(nullptr), rosidl_dynamic_type_builder_(nullptr) -{ - DynamicMessageTypeBuilder out = other.clone(); - std::swap(serialization_support_, out.serialization_support_); - std::swap(rosidl_dynamic_type_builder_, out.rosidl_dynamic_type_builder_); + init_from_description(description, allocator, serialization_support); } DynamicMessageTypeBuilder::DynamicMessageTypeBuilder(DynamicMessageTypeBuilder && other) noexcept : serialization_support_(std::exchange(other.serialization_support_, nullptr)), - rosidl_dynamic_type_builder_(std::exchange(other.rosidl_dynamic_type_builder_, nullptr)) {} - - -DynamicMessageTypeBuilder & -DynamicMessageTypeBuilder::operator=(const DynamicMessageTypeBuilder & other) -{ - return *this = DynamicMessageTypeBuilder(other); -} + rosidl_dynamic_type_builder_(std::exchange( + other.rosidl_dynamic_type_builder_, + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type_builder())) {} DynamicMessageTypeBuilder & @@ -140,12 +107,20 @@ DynamicMessageTypeBuilder::operator=(DynamicMessageTypeBuilder && other) noexcep } -DynamicMessageTypeBuilder::~DynamicMessageTypeBuilder() {} +DynamicMessageTypeBuilder::~DynamicMessageTypeBuilder() +{ + if (rosidl_dynamic_typesupport_dynamic_type_builder_fini(&get_rosidl_dynamic_type_builder()) != + RCUTILS_RET_OK) + { + RCUTILS_LOG_ERROR("could not fini rosidl dynamic type builder"); + } +} void DynamicMessageTypeBuilder::init_from_description( const rosidl_runtime_c__type_description__TypeDescription & description, + rcl_allocator_t allocator, DynamicSerializationSupport::SharedPtr serialization_support) { if (serialization_support) { @@ -153,28 +128,26 @@ DynamicMessageTypeBuilder::init_from_description( serialization_support_ = serialization_support; } - rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder = nullptr; - rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_builder_create_from_description( + rosidl_dynamic_typesupport_dynamic_type_builder_t rosidl_dynamic_type_builder = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type_builder(); + rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_builder_init_from_description( &serialization_support_->get_rosidl_serialization_support(), &description, + &allocator, &rosidl_dynamic_type_builder); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_type_builder) { - throw std::runtime_error("could not create new dynamic type builder object"); + if (ret != RCUTILS_RET_OK) { + throw std::runtime_error("could not init new dynamic type builder object"); } - rosidl_dynamic_type_builder_.reset( - rosidl_dynamic_type_builder, - // Custom deleter - [](rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder)->void { - rosidl_dynamic_typesupport_dynamic_type_builder_destroy(rosidl_dynamic_type_builder); - }); + rosidl_dynamic_type_builder_ = std::move(rosidl_dynamic_type_builder); } void DynamicMessageTypeBuilder::init_from_serialization_support_( DynamicSerializationSupport::SharedPtr serialization_support, - const std::string & name) + const std::string & name, + rcl_allocator_t allocator) { if (!serialization_support) { throw std::runtime_error("serialization support cannot be nullptr!"); @@ -183,26 +156,17 @@ DynamicMessageTypeBuilder::init_from_serialization_support_( throw std::runtime_error("serialization support raw pointer cannot be nullptr!"); } - - rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder = nullptr; - rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_builder_create( + rosidl_dynamic_typesupport_dynamic_type_builder_t rosidl_dynamic_type_builder = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type_builder(); + rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_builder_init( &serialization_support->get_rosidl_serialization_support(), name.c_str(), name.size(), + &allocator, &rosidl_dynamic_type_builder); if (ret != RCUTILS_RET_OK) { throw std::runtime_error( std::string("could not init dynamic type builder: ") + rcl_get_error_string().str); } - if (!rosidl_dynamic_type_builder) { - throw std::runtime_error("could not init dynamic type builder object"); - } - - rosidl_dynamic_type_builder_.reset( - rosidl_dynamic_type_builder, - // Custom deleter - [](rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder)->void { - rosidl_dynamic_typesupport_dynamic_type_builder_destroy(rosidl_dynamic_type_builder); - }); } @@ -211,17 +175,14 @@ DynamicMessageTypeBuilder::match_serialization_support_( const DynamicSerializationSupport & serialization_support, const rosidl_dynamic_typesupport_dynamic_type_builder_t & rosidl_dynamic_type_builder) { - bool out = true; - if (serialization_support.get_serialization_library_identifier() != std::string( rosidl_dynamic_type_builder.serialization_support->serialization_library_identifier)) { RCUTILS_LOG_ERROR( "serialization support library identifier does not match dynamic type builder's"); - out = false; + return false; } - - return out; + return true; } @@ -229,7 +190,8 @@ DynamicMessageTypeBuilder::match_serialization_support_( const std::string DynamicMessageTypeBuilder::get_serialization_library_identifier() const { - return std::string(rosidl_dynamic_type_builder_->serialization_support->serialization_library_identifier); + return std::string( + get_rosidl_dynamic_type_builder().serialization_support->serialization_library_identifier); } @@ -239,38 +201,22 @@ DynamicMessageTypeBuilder::get_name() const size_t buf_length; const char * buf; rosidl_dynamic_typesupport_dynamic_type_builder_get_name( - get_rosidl_dynamic_type_builder(), &buf, &buf_length); + &get_rosidl_dynamic_type_builder(), &buf, &buf_length); return std::string(buf, buf_length); } -rosidl_dynamic_typesupport_dynamic_type_builder_t * +rosidl_dynamic_typesupport_dynamic_type_builder_t & DynamicMessageTypeBuilder::get_rosidl_dynamic_type_builder() { - return rosidl_dynamic_type_builder_.get(); + return rosidl_dynamic_type_builder_; } -const rosidl_dynamic_typesupport_dynamic_type_builder_t * +const rosidl_dynamic_typesupport_dynamic_type_builder_t & DynamicMessageTypeBuilder::get_rosidl_dynamic_type_builder() const { - return rosidl_dynamic_type_builder_.get(); -} - - -std::shared_ptr -DynamicMessageTypeBuilder::get_shared_rosidl_dynamic_type_builder() -{ - return std::shared_ptr( - shared_from_this(), rosidl_dynamic_type_builder_.get()); -} - - -std::shared_ptr -DynamicMessageTypeBuilder::get_shared_rosidl_dynamic_type_builder() const -{ - return std::shared_ptr( - shared_from_this(), rosidl_dynamic_type_builder_.get()); + return rosidl_dynamic_type_builder_; } @@ -293,41 +239,44 @@ void DynamicMessageTypeBuilder::set_name(const std::string & name) { rosidl_dynamic_typesupport_dynamic_type_builder_set_name( - get_rosidl_dynamic_type_builder(), name.c_str(), name.size()); + &get_rosidl_dynamic_type_builder(), name.c_str(), name.size()); } DynamicMessageTypeBuilder -DynamicMessageTypeBuilder::clone() const +DynamicMessageTypeBuilder::clone(rcl_allocator_t allocator) { - rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder = nullptr; + rosidl_dynamic_typesupport_dynamic_type_builder_t rosidl_dynamic_type_builder = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type_builder(); rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_builder_clone( - get_rosidl_dynamic_type_builder(), &rosidl_dynamic_type_builder); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_type_builder) { + &get_rosidl_dynamic_type_builder(), &allocator, &rosidl_dynamic_type_builder); + if (ret != RCUTILS_RET_OK) { throw std::runtime_error( std::string("could not clone dynamic type builder: ") + rcl_get_error_string().str); } - return DynamicMessageTypeBuilder(serialization_support_, rosidl_dynamic_type_builder); + return DynamicMessageTypeBuilder( + get_shared_dynamic_serialization_support(), std::move(rosidl_dynamic_type_builder)); } DynamicMessageTypeBuilder::SharedPtr -DynamicMessageTypeBuilder::clone_shared() const +DynamicMessageTypeBuilder::clone_shared(rcl_allocator_t allocator) { - rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder = nullptr; + rosidl_dynamic_typesupport_dynamic_type_builder_t rosidl_dynamic_type_builder = + rosidl_dynamic_typesupport_get_zero_initialized_dynamic_type_builder(); rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_builder_clone( - get_rosidl_dynamic_type_builder(), &rosidl_dynamic_type_builder); - if (ret != RCUTILS_RET_OK || !rosidl_dynamic_type_builder) { + &get_rosidl_dynamic_type_builder(), &allocator, &rosidl_dynamic_type_builder); + if (ret != RCUTILS_RET_OK) { throw std::runtime_error( std::string("could not clone dynamic type builder: ") + rcl_get_error_string().str); } return DynamicMessageTypeBuilder::make_shared( - serialization_support_, rosidl_dynamic_type_builder); + get_shared_dynamic_serialization_support(), std::move(rosidl_dynamic_type_builder)); } void -DynamicMessageTypeBuilder::clear() +DynamicMessageTypeBuilder::clear(rcl_allocator_t allocator) { if (!serialization_support_) { throw std::runtime_error( @@ -336,38 +285,35 @@ DynamicMessageTypeBuilder::clear() } const std::string & name = get_name(); - init_from_serialization_support_(serialization_support_, name); - if (!rosidl_dynamic_type_builder_) { - throw std::runtime_error("could not create new dynamic type builder object"); - } + init_from_serialization_support_(serialization_support_, name, allocator); } DynamicMessage -DynamicMessageTypeBuilder::build_dynamic_message() +DynamicMessageTypeBuilder::build_dynamic_message(rcl_allocator_t allocator) { - return DynamicMessage(shared_from_this()); + return DynamicMessage(shared_from_this(), allocator); } DynamicMessage::SharedPtr -DynamicMessageTypeBuilder::build_dynamic_message_shared() +DynamicMessageTypeBuilder::build_dynamic_message_shared(rcl_allocator_t allocator) { - return DynamicMessage::make_shared(shared_from_this()); + return DynamicMessage::make_shared(shared_from_this(), allocator); } DynamicMessageType -DynamicMessageTypeBuilder::build_dynamic_message_type() +DynamicMessageTypeBuilder::build_dynamic_message_type(rcl_allocator_t allocator) { - return DynamicMessageType(shared_from_this()); + return DynamicMessageType(shared_from_this(), allocator); } DynamicMessageType::SharedPtr -DynamicMessageTypeBuilder::build_dynamic_message_type_shared() +DynamicMessageTypeBuilder::build_dynamic_message_type_shared(rcl_allocator_t allocator) { - return DynamicMessageType::make_shared(shared_from_this()); + return DynamicMessageType::make_shared(shared_from_this(), allocator); } @@ -382,7 +328,7 @@ DynamicMessageTypeBuilder::add_fixed_string_member( const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_fixed_string_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), string_length); } @@ -394,7 +340,7 @@ DynamicMessageTypeBuilder::add_fixed_wstring_member( const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_fixed_wstring_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), wstring_length); } @@ -406,7 +352,7 @@ DynamicMessageTypeBuilder::add_fixed_string_array_member( size_t string_length, size_t array_length, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_fixed_string_array_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), string_length, array_length); } @@ -418,7 +364,7 @@ DynamicMessageTypeBuilder::add_fixed_wstring_array_member( size_t wstring_length, size_t array_length, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_fixed_wstring_array_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), wstring_length, array_length); } @@ -430,7 +376,7 @@ DynamicMessageTypeBuilder::add_fixed_string_unbounded_sequence_member( const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_fixed_string_unbounded_sequence_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), string_length); } @@ -442,7 +388,7 @@ DynamicMessageTypeBuilder::add_fixed_wstring_unbounded_sequence_member( const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_fixed_wstring_unbounded_sequence_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), wstring_length); } @@ -454,7 +400,7 @@ DynamicMessageTypeBuilder::add_fixed_string_bounded_sequence_member( size_t string_length, size_t sequence_bound, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_fixed_string_bounded_sequence_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), string_length, sequence_bound); } @@ -466,7 +412,7 @@ DynamicMessageTypeBuilder::add_fixed_wstring_bounded_sequence_member( size_t wstring_length, size_t sequence_bound, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_fixed_wstring_bounded_sequence_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), wstring_length, sequence_bound); } @@ -479,7 +425,7 @@ DynamicMessageTypeBuilder::add_bounded_string_member( const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_bounded_string_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), string_bound); } @@ -491,7 +437,7 @@ DynamicMessageTypeBuilder::add_bounded_wstring_member( const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_bounded_wstring_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), wstring_bound); } @@ -503,7 +449,7 @@ DynamicMessageTypeBuilder::add_bounded_string_array_member( size_t string_bound, size_t array_length, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_bounded_string_array_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), string_bound, array_length); } @@ -515,7 +461,7 @@ DynamicMessageTypeBuilder::add_bounded_wstring_array_member( size_t wstring_bound, size_t array_length, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_bounded_wstring_array_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), wstring_bound, array_length); } @@ -527,7 +473,7 @@ DynamicMessageTypeBuilder::add_bounded_string_unbounded_sequence_member( const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_bounded_string_unbounded_sequence_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), string_bound); } @@ -539,7 +485,7 @@ DynamicMessageTypeBuilder::add_bounded_wstring_unbounded_sequence_member( const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_bounded_wstring_unbounded_sequence_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), wstring_bound); } @@ -551,7 +497,7 @@ DynamicMessageTypeBuilder::add_bounded_string_bounded_sequence_member( size_t string_bound, size_t sequence_bound, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_bounded_string_bounded_sequence_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), string_bound, sequence_bound); } @@ -563,7 +509,7 @@ DynamicMessageTypeBuilder::add_bounded_wstring_bounded_sequence_member( size_t wstring_bound, size_t sequence_bound, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_bounded_wstring_bounded_sequence_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), wstring_bound, sequence_bound); } @@ -576,9 +522,9 @@ DynamicMessageTypeBuilder::add_complex_member( DynamicMessageType & nested_type, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_complex_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), - nested_type.get_rosidl_dynamic_type()); + &nested_type.get_rosidl_dynamic_type()); } @@ -588,9 +534,9 @@ DynamicMessageTypeBuilder::add_complex_array_member( DynamicMessageType & nested_type, size_t array_length, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_complex_array_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), - nested_type.get_rosidl_dynamic_type(), array_length); + &nested_type.get_rosidl_dynamic_type(), array_length); } @@ -600,9 +546,9 @@ DynamicMessageTypeBuilder::add_complex_unbounded_sequence_member( DynamicMessageType & nested_type, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_complex_unbounded_sequence_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), - nested_type.get_rosidl_dynamic_type()); + &nested_type.get_rosidl_dynamic_type()); } @@ -612,9 +558,9 @@ DynamicMessageTypeBuilder::add_complex_bounded_sequence_member( DynamicMessageType & nested_type, size_t sequence_bound, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_complex_bounded_sequence_member( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), - nested_type.get_rosidl_dynamic_type(), sequence_bound); + &nested_type.get_rosidl_dynamic_type(), sequence_bound); } @@ -624,9 +570,9 @@ DynamicMessageTypeBuilder::add_complex_member_builder( DynamicMessageTypeBuilder & nested_type_builder, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_complex_member_builder( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), - nested_type_builder.get_rosidl_dynamic_type_builder()); + &nested_type_builder.get_rosidl_dynamic_type_builder()); } @@ -637,9 +583,9 @@ DynamicMessageTypeBuilder::add_complex_array_member_builder( const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_complex_array_member_builder( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), - nested_type_builder.get_rosidl_dynamic_type_builder(), array_length); + &nested_type_builder.get_rosidl_dynamic_type_builder(), array_length); } @@ -649,9 +595,9 @@ DynamicMessageTypeBuilder::add_complex_unbounded_sequence_member_builder( DynamicMessageTypeBuilder & nested_type_builder, const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_complex_unbounded_sequence_member_builder( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), - nested_type_builder.get_rosidl_dynamic_type_builder()); + &nested_type_builder.get_rosidl_dynamic_type_builder()); } @@ -662,7 +608,7 @@ DynamicMessageTypeBuilder::add_complex_bounded_sequence_member_builder( const std::string & default_value) { rosidl_dynamic_typesupport_dynamic_type_builder_add_complex_bounded_sequence_member_builder( - get_rosidl_dynamic_type_builder(), + &get_rosidl_dynamic_type_builder(), id, name.c_str(), name.size(), default_value.c_str(), default_value.size(), - nested_type_builder.get_rosidl_dynamic_type_builder(), sequence_bound); + &nested_type_builder.get_rosidl_dynamic_type_builder(), sequence_bound); } diff --git a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_support.cpp b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_support.cpp index d8a2a2be12..b840ab52c3 100644 --- a/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_support.cpp +++ b/rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message_type_support.cpp @@ -82,10 +82,10 @@ DynamicMessageTypeSupport::DynamicMessageTypeSupport( std::move(ts_impl->serialization_support)); dynamic_message_type_ = DynamicMessageType::make_shared( - get_shared_dynamic_serialization_support(), std::move(ts_impl->dynamic_message_type)); + get_shared_dynamic_serialization_support(), std::move(*ts_impl->dynamic_message_type)); dynamic_message_ = DynamicMessage::make_shared( - get_shared_dynamic_serialization_support(), std::move(ts_impl->dynamic_message)); + get_shared_dynamic_serialization_support(), std::move(*ts_impl->dynamic_message)); } DynamicMessageTypeSupport::DynamicMessageTypeSupport( @@ -131,10 +131,10 @@ DynamicMessageTypeSupport::DynamicMessageTypeSupport( rosidl_message_type_support_.data); dynamic_message_type_ = DynamicMessageType::make_shared( - get_shared_dynamic_serialization_support(), ts_impl->dynamic_message_type); + get_shared_dynamic_serialization_support(), std::move(*ts_impl->dynamic_message_type)); dynamic_message_ = DynamicMessage::make_shared( - get_shared_dynamic_serialization_support(), ts_impl->dynamic_message); + get_shared_dynamic_serialization_support(), std::move(*ts_impl->dynamic_message)); } DynamicMessageTypeSupport::DynamicMessageTypeSupport( @@ -204,8 +204,8 @@ DynamicMessageTypeSupport::DynamicMessageTypeSupport( // ts_impl->type_description_sources = // Not used ts_impl->serialization_support = serialization_support->get_rosidl_serialization_support(); - ts_impl->dynamic_message_type = dynamic_message_type->get_rosidl_dynamic_type(); - ts_impl->dynamic_message = dynamic_message->get_rosidl_dynamic_data(); + ts_impl->dynamic_message_type = &dynamic_message_type->get_rosidl_dynamic_type(); + ts_impl->dynamic_message = &dynamic_message->get_rosidl_dynamic_data(); rosidl_message_type_support_ = { rosidl_get_dynamic_typesupport_identifier(), // typesupport_identifier @@ -222,6 +222,12 @@ DynamicMessageTypeSupport::DynamicMessageTypeSupport( DynamicMessageTypeSupport::~DynamicMessageTypeSupport() { + // These must go first + serialization_support_.reset(); + dynamic_message_type_.reset(); + dynamic_message_.reset(); + + // Early return if type support isn't populated to avoid segfaults if (!rosidl_message_type_support_.data) { return; } diff --git a/rclcpp/src/rclcpp/executor.cpp b/rclcpp/src/rclcpp/executor.cpp index fdb28c26bb..08b023e8e9 100644 --- a/rclcpp/src/rclcpp/executor.cpp +++ b/rclcpp/src/rclcpp/executor.cpp @@ -716,7 +716,7 @@ Executor::execute_subscription(rclcpp::SubscriptionBase::SharedPtr subscription) [&]() {return subscription->take_serialized(*serialized_msg.get(), message_info);}, [&]() { - bool ret = dynamic_message->deserialize(&serialized_msg->get_rcl_serialized_message()); + bool ret = dynamic_message->deserialize(serialized_msg->get_rcl_serialized_message()); if (!ret) { throw_from_rcl_error(ret, "Couldn't convert serialized message to dynamic data!"); } diff --git a/rclcpp/src/rclcpp/subscription_base.cpp b/rclcpp/src/rclcpp/subscription_base.cpp index f9fcd5628f..bd48efcb8c 100644 --- a/rclcpp/src/rclcpp/subscription_base.cpp +++ b/rclcpp/src/rclcpp/subscription_base.cpp @@ -536,7 +536,7 @@ SubscriptionBase::take_dynamic_message( { rcl_ret_t ret = rcl_take_dynamic_message( this->get_subscription_handle().get(), - message_out.get_rosidl_dynamic_data(), + &message_out.get_rosidl_dynamic_data(), &message_info_out.get_rmw_message_info(), nullptr); if (RCL_RET_SUBSCRIPTION_TAKE_FAILED == ret) {