From 21bc31c7f8d5dc0fd69580bd78067676521dd549 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Mon, 24 Feb 2020 11:19:19 +0100 Subject: [PATCH 01/20] Making TypeSupport interface methods const. Signed-off-by: Miguel Company --- .../include/rmw_fastrtps_cpp/TypeSupport.hpp | 6 +++--- rmw_fastrtps_cpp/src/type_support_common.cpp | 6 +++--- .../rmw_fastrtps_dynamic_cpp/TypeSupport.hpp | 20 ++++++++++++------- .../TypeSupport_impl.hpp | 19 ++++++++++++------ .../rmw_fastrtps_shared_cpp/TypeSupport.hpp | 6 +++--- 5 files changed, 35 insertions(+), 22 deletions(-) diff --git a/rmw_fastrtps_cpp/include/rmw_fastrtps_cpp/TypeSupport.hpp b/rmw_fastrtps_cpp/include/rmw_fastrtps_cpp/TypeSupport.hpp index a276d48df..1f5bb641a 100644 --- a/rmw_fastrtps_cpp/include/rmw_fastrtps_cpp/TypeSupport.hpp +++ b/rmw_fastrtps_cpp/include/rmw_fastrtps_cpp/TypeSupport.hpp @@ -35,13 +35,13 @@ namespace rmw_fastrtps_cpp class TypeSupport : public rmw_fastrtps_shared_cpp::TypeSupport { public: - size_t getEstimatedSerializedSize(const void * ros_message, const void * impl); + size_t getEstimatedSerializedSize(const void * ros_message, const void * impl) const override; bool serializeROSmessage( - const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl); + const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) const override; bool deserializeROSmessage( - eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl); + eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const override; protected: TypeSupport(); diff --git a/rmw_fastrtps_cpp/src/type_support_common.cpp b/rmw_fastrtps_cpp/src/type_support_common.cpp index 981dd518f..9cc671b29 100644 --- a/rmw_fastrtps_cpp/src/type_support_common.cpp +++ b/rmw_fastrtps_cpp/src/type_support_common.cpp @@ -47,7 +47,7 @@ void TypeSupport::set_members(const message_type_support_callbacks_t * members) m_typeSize = 4 + data_size; } -size_t TypeSupport::getEstimatedSerializedSize(const void * ros_message, const void * impl) +size_t TypeSupport::getEstimatedSerializedSize(const void * ros_message, const void * impl) const { if (max_size_bound_) { return m_typeSize; @@ -63,7 +63,7 @@ size_t TypeSupport::getEstimatedSerializedSize(const void * ros_message, const v } bool TypeSupport::serializeROSmessage( - const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) + const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) const { assert(ros_message); assert(impl); @@ -83,7 +83,7 @@ bool TypeSupport::serializeROSmessage( } bool TypeSupport::deserializeROSmessage( - eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) + eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const { assert(ros_message); assert(impl); diff --git a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp index e0bd7400c..e4e2f7f84 100644 --- a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp +++ b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp @@ -134,13 +134,13 @@ template class TypeSupport : public rmw_fastrtps_shared_cpp::TypeSupport { public: - size_t getEstimatedSerializedSize(const void * ros_message, const void * impl); + size_t getEstimatedSerializedSize(const void * ros_message, const void * impl) const override; bool serializeROSmessage( - const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl); + const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) const override; bool deserializeROSmessage( - eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl); + eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const override; protected: TypeSupport(); @@ -151,14 +151,20 @@ class TypeSupport : public rmw_fastrtps_shared_cpp::TypeSupport private: size_t getEstimatedSerializedSize( - const MembersType * members, const void * ros_message, size_t current_alignment); + const MembersType * members, + const void * ros_message, + size_t current_alignment) const; bool serializeROSmessage( - eprosima::fastcdr::Cdr & ser, const MembersType * members, const void * ros_message); + eprosima::fastcdr::Cdr & ser, + const MembersType * members, + const void * ros_message) const; bool deserializeROSmessage( - eprosima::fastcdr::Cdr & deser, const MembersType * members, void * ros_message, - bool call_new); + eprosima::fastcdr::Cdr & deser, + const MembersType * members, + void * ros_message, + bool call_new) const; }; } // namespace rmw_fastrtps_dynamic_cpp diff --git a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp index 8a29b00a8..6031cbe8b 100644 --- a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp +++ b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp @@ -372,7 +372,9 @@ size_t get_array_size_and_assign_field( template bool TypeSupport::serializeROSmessage( - eprosima::fastcdr::Cdr & ser, const MembersType * members, const void * ros_message) + eprosima::fastcdr::Cdr & ser, + const MembersType * members, + const void * ros_message) const { assert(members); assert(ros_message); @@ -630,7 +632,9 @@ size_t next_field_align_string( template size_t TypeSupport::getEstimatedSerializedSize( - const MembersType * members, const void * ros_message, size_t current_alignment) + const MembersType * members, + const void * ros_message, + size_t current_alignment) const { assert(members); assert(ros_message); @@ -949,7 +953,10 @@ inline size_t get_submessage_array_deserialize( template bool TypeSupport::deserializeROSmessage( - eprosima::fastcdr::Cdr & deser, const MembersType * members, void * ros_message, bool call_new) + eprosima::fastcdr::Cdr & deser, + const MembersType * members, + void * ros_message, + bool call_new) const { assert(members); assert(ros_message); @@ -1117,7 +1124,7 @@ size_t TypeSupport::calculateMaxSerializedSize( template size_t TypeSupport::getEstimatedSerializedSize( - const void * ros_message, const void * impl) + const void * ros_message, const void * impl) const { if (max_size_bound_) { return m_typeSize; @@ -1141,7 +1148,7 @@ size_t TypeSupport::getEstimatedSerializedSize( template bool TypeSupport::serializeROSmessage( - const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) + const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) const { assert(ros_message); assert(impl); @@ -1161,7 +1168,7 @@ bool TypeSupport::serializeROSmessage( template bool TypeSupport::deserializeROSmessage( - eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) + eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const { assert(ros_message); assert(impl); diff --git a/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/TypeSupport.hpp b/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/TypeSupport.hpp index c670be90c..4515ba465 100644 --- a/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/TypeSupport.hpp +++ b/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/TypeSupport.hpp @@ -41,13 +41,13 @@ struct SerializedData class TypeSupport : public eprosima::fastrtps::TopicDataType { public: - virtual size_t getEstimatedSerializedSize(const void * ros_message, const void * impl) = 0; + virtual size_t getEstimatedSerializedSize(const void * ros_message, const void * impl) const = 0; virtual bool serializeROSmessage( - const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) = 0; + const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) const = 0; virtual bool deserializeROSmessage( - eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) = 0; + eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const = 0; RMW_FASTRTPS_SHARED_CPP_PUBLIC bool getKey( From 96b4c2c6a19952ae422a61eb22f65ed2312116aa Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Mon, 24 Feb 2020 09:43:41 +0100 Subject: [PATCH 02/20] Created TypeSupport proxy class Signed-off-by: Miguel Company --- .../rmw_fastrtps_dynamic_cpp/TypeSupport.hpp | 14 ++++++++ .../TypeSupport_impl.hpp | 24 ++++++------- .../src/type_support_common.cpp | 34 +++++++++++++++++++ 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp index e4e2f7f84..441a69255 100644 --- a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp +++ b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp @@ -130,6 +130,20 @@ struct StringHelper } }; +class TypeSupportProxy : public rmw_fastrtps_shared_cpp::TypeSupport +{ +public: + TypeSupportProxy(rmw_fastrtps_shared_cpp::TypeSupport * inner_type); + + size_t getEstimatedSerializedSize(const void * ros_message, const void * impl) const override; + + bool serializeROSmessage( + const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) const override; + + bool deserializeROSmessage( + eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const override; +}; + template class TypeSupport : public rmw_fastrtps_shared_cpp::TypeSupport { diff --git a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp index 6031cbe8b..6d2b6e5f7 100644 --- a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp +++ b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp @@ -1131,14 +1131,14 @@ size_t TypeSupport::getEstimatedSerializedSize( } assert(ros_message); - assert(impl); + assert(members_); // Encapsulation size size_t ret_val = 4; - auto members = static_cast(impl); - if (members->member_count_ != 0) { - ret_val += TypeSupport::getEstimatedSerializedSize(members, ros_message, 0); + (void)impl; + if (members_->member_count_ != 0) { + ret_val += TypeSupport::getEstimatedSerializedSize(members_, ros_message, 0); } else { ret_val += 1; } @@ -1151,14 +1151,14 @@ bool TypeSupport::serializeROSmessage( const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) const { assert(ros_message); - assert(impl); + assert(members_); // Serialize encapsulation ser.serialize_encapsulation(); - auto members = static_cast(impl); - if (members->member_count_ != 0) { - TypeSupport::serializeROSmessage(ser, members, ros_message); + (void)impl; + if (members_->member_count_ != 0) { + TypeSupport::serializeROSmessage(ser, members_, ros_message); } else { ser << (uint8_t)0; } @@ -1171,14 +1171,14 @@ bool TypeSupport::deserializeROSmessage( eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const { assert(ros_message); - assert(impl); + assert(members_); // Deserialize encapsulation. deser.read_encapsulation(); - auto members = static_cast(impl); - if (members->member_count_ != 0) { - TypeSupport::deserializeROSmessage(deser, members, ros_message, false); + (void)impl; + if (members_->member_count_ != 0) { + TypeSupport::deserializeROSmessage(deser, members_, ros_message, false); } else { uint8_t dump = 0; deser >> dump; diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp b/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp index 3d546a067..467118cf7 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp @@ -14,6 +14,8 @@ #include "rmw/error_handling.h" +#include "rmw_fastrtps_dynamic_cpp/TypeSupport.hpp" + #include "rosidl_typesupport_introspection_cpp/identifier.hpp" #include "rosidl_typesupport_introspection_c/identifier.h" @@ -88,3 +90,35 @@ _register_type( { eprosima::fastrtps::Domain::registerType(participant, typed_typesupport); } + +namespace rmw_fastrtps_dynamic_cpp +{ + +TypeSupportProxy::TypeSupportProxy(rmw_fastrtps_shared_cpp::TypeSupport * inner_type) +{ + setName(inner_type->getName()); + m_typeSize = inner_type->m_typeSize; +} + +size_t TypeSupportProxy::getEstimatedSerializedSize( + const void * ros_message, const void * impl) const +{ + auto type_impl = static_cast(impl); + return type_impl->getEstimatedSerializedSize(ros_message, impl); +} + +bool TypeSupportProxy::serializeROSmessage( + const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) const +{ + auto type_impl = static_cast(impl); + return type_impl->serializeROSmessage(ros_message, ser, impl); +} + +bool TypeSupportProxy::deserializeROSmessage( + eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const +{ + auto type_impl = static_cast(impl); + return type_impl->deserializeROSmessage(deser, ros_message, impl); +} + +} // namespace rmw_fastrtps_dynamic_cpp \ No newline at end of file From 4086469b73f2c6c19bf654d1510e687ecd282c24 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Mon, 24 Feb 2020 10:02:56 +0100 Subject: [PATCH 03/20] Changes on rmw_publisher Signed-off-by: Miguel Company --- .../src/rmw_publisher.cpp | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp index 3de1ba7bd..388790e02 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp @@ -32,6 +32,7 @@ using Domain = eprosima::fastrtps::Domain; using Participant = eprosima::fastrtps::Participant; using TopicDataType = eprosima::fastrtps::TopicDataType; +using TypeSupportProxy = rmw_fastrtps_dynamic_cpp::TypeSupportProxy; extern "C" { @@ -132,8 +133,17 @@ rmw_create_publisher( RMW_SET_ERROR_MSG("failed to allocate CustomPublisherInfo"); return nullptr; } + + auto type_impl = _create_message_type_support( + type_support->data, type_support->typesupport_identifier); + if(!type_impl) { + delete info; + RMW_SET_ERROR_MSG("failed to allocate type support"); + return nullptr; + } + info->typesupport_identifier_ = type_support->typesupport_identifier; - info->type_support_impl_ = type_support->data; + info->type_support_impl_ = type_impl; std::string type_name = _create_type_name( type_support->data, info->typesupport_identifier_); @@ -141,8 +151,11 @@ rmw_create_publisher( participant, type_name.c_str(), reinterpret_cast(&info->type_support_))) { - info->type_support_ = _create_message_type_support( - type_support->data, info->typesupport_identifier_); + info->type_support_ = new (std::nothrow) TypeSupportProxy(type_impl); + if(!info->type_support_) { + RMW_SET_ERROR_MSG("failed to allocate TypeSupportProxy"); + goto fail; + } _register_type(participant, info->type_support_); } @@ -226,6 +239,8 @@ rmw_create_publisher( } delete info; } + + delete type_impl; if (rmw_publisher) { rmw_publisher_free(rmw_publisher); @@ -289,6 +304,14 @@ rmw_return_loaned_message_from_publisher( rmw_ret_t rmw_destroy_publisher(rmw_node_t * node, rmw_publisher_t * publisher) { + auto info = static_cast(publisher->data); + RCUTILS_CHECK_FOR_NULL_WITH_MSG(info, "publisher info pointer is null", return RMW_RET_ERROR); + + auto impl = static_cast(const_cast(info->type_support_impl_)); + RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "publisher type support is null", return RMW_RET_ERROR); + + delete impl; + return rmw_fastrtps_shared_cpp::__rmw_destroy_publisher( eprosima_fastrtps_identifier, node, publisher); } From 91c52bce657ed0e6be10a1b6ad98d786469a3f56 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Mon, 24 Feb 2020 10:46:18 +0100 Subject: [PATCH 04/20] Changes on rmw_subscription. Signed-off-by: Miguel Company --- .../src/rmw_subscription.cpp | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp index 740ddfab1..85533bce7 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp @@ -36,6 +36,7 @@ using Domain = eprosima::fastrtps::Domain; using Participant = eprosima::fastrtps::Participant; using TopicDataType = eprosima::fastrtps::TopicDataType; +using TypeSupportProxy = rmw_fastrtps_dynamic_cpp::TypeSupportProxy; extern "C" { @@ -134,8 +135,17 @@ rmw_create_subscription( RMW_SET_ERROR_MSG("failed to allocate CustomSubscriberInfo"); return nullptr; } + + auto type_impl = _create_message_type_support( + type_support->data, type_support->typesupport_identifier); + if(!type_impl) { + delete info; + RMW_SET_ERROR_MSG("failed to allocate type support"); + return nullptr; + } + info->typesupport_identifier_ = type_support->typesupport_identifier; - info->type_support_impl_ = type_support->data; + info->type_support_impl_ = type_impl; std::string type_name = _create_type_name( type_support->data, info->typesupport_identifier_); @@ -143,8 +153,11 @@ rmw_create_subscription( participant, type_name.c_str(), reinterpret_cast(&info->type_support_))) { - info->type_support_ = _create_message_type_support( - type_support->data, info->typesupport_identifier_); + info->type_support_ = new (std::nothrow) TypeSupportProxy(type_impl); + if(!info->type_support_) { + RMW_SET_ERROR_MSG("failed to allocate TypeSupportProxy"); + goto fail; + } _register_type(participant, info->type_support_); } @@ -206,6 +219,8 @@ rmw_create_subscription( } delete info; } + + delete type_impl; if (rmw_subscription) { rmw_subscription_free(rmw_subscription); @@ -235,6 +250,14 @@ rmw_subscription_get_actual_qos( rmw_ret_t rmw_destroy_subscription(rmw_node_t * node, rmw_subscription_t * subscription) { + auto info = static_cast(subscription->data); + RCUTILS_CHECK_FOR_NULL_WITH_MSG(info, "subscription info pointer is null", return RMW_RET_ERROR); + + auto impl = static_cast(const_cast(info->type_support_impl_)); + RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "subscription type support is null", return RMW_RET_ERROR); + + delete impl; + return rmw_fastrtps_shared_cpp::__rmw_destroy_subscription( eprosima_fastrtps_identifier, node, subscription); } From 31e60bdaf5fb89d9e1106665992c27f62759aee1 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Mon, 24 Feb 2020 14:19:59 +0100 Subject: [PATCH 05/20] Changes on rmw_client Signed-off-by: Miguel Company --- rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp | 51 ++++++++++++++++++--- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp index 24c9c0e80..6e5f949a2 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp @@ -38,6 +38,7 @@ using Domain = eprosima::fastrtps::Domain; using Participant = eprosima::fastrtps::Participant; using TopicDataType = eprosima::fastrtps::TopicDataType; +using TypeSupportProxy = rmw_fastrtps_dynamic_cpp::TypeSupportProxy; extern "C" { @@ -101,6 +102,26 @@ rmw_create_client( info->request_publisher_matched_count_ = 0; info->response_subscriber_matched_count_ = 0; + auto request_type_impl = _create_request_type_support( + type_support->data, info->typesupport_identifier_); + if(!request_type_impl) { + delete info; + RMW_SET_ERROR_MSG("failed to allocate request type support"); + return nullptr; + } + + auto response_type_impl = _create_response_type_support( + type_support->data, info->typesupport_identifier_); + if(!response_type_impl) { + delete request_type_impl; + delete info; + RMW_SET_ERROR_MSG("failed to allocate response type support"); + return nullptr; + } + + info->request_type_support_impl_ = request_type_impl; + info->response_type_support_impl_ = response_type_impl; + const void * untyped_request_members; const void * untyped_response_members; @@ -109,9 +130,6 @@ rmw_create_client( untyped_response_members = get_response_ptr( type_support->data, info->typesupport_identifier_); - info->request_type_support_impl_ = untyped_request_members; - info->response_type_support_impl_ = untyped_response_members; - std::string request_type_name = _create_type_name( untyped_request_members, info->typesupport_identifier_); std::string response_type_name = _create_type_name( @@ -121,8 +139,11 @@ rmw_create_client( participant, request_type_name.c_str(), reinterpret_cast(&info->request_type_support_))) { - info->request_type_support_ = _create_request_type_support( - type_support->data, info->typesupport_identifier_); + info->request_type_support_ = new (std::nothrow) TypeSupportProxy(request_type_impl); + if(!info->request_type_support_) { + RMW_SET_ERROR_MSG("failed to allocate request TypeSupportProxy"); + goto fail; + } _register_type(participant, info->request_type_support_); } @@ -130,8 +151,11 @@ rmw_create_client( participant, response_type_name.c_str(), reinterpret_cast(&info->response_type_support_))) { - info->response_type_support_ = _create_response_type_support( - type_support->data, info->typesupport_identifier_); + info->response_type_support_ = new (std::nothrow) TypeSupportProxy(response_type_impl); + if(!info->response_type_support_) { + RMW_SET_ERROR_MSG("failed to allocate response TypeSupportProxy"); + goto fail; + } _register_type(participant, info->response_type_support_); } @@ -245,6 +269,8 @@ rmw_create_client( "leaking type support objects because node impl is null"); } + delete request_type_impl; + delete response_type_impl; delete info; info = nullptr; } @@ -263,6 +289,17 @@ rmw_create_client( rmw_ret_t rmw_destroy_client(rmw_node_t * node, rmw_client_t * client) { + auto info = static_cast(client->data); + RCUTILS_CHECK_FOR_NULL_WITH_MSG(info, "client info pointer is null", return RMW_RET_ERROR); + + auto impl = static_cast(const_cast(info->request_type_support_impl_)); + RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "client's request type support is null", return RMW_RET_ERROR); + delete impl; + + impl = static_cast(const_cast(info->response_type_support_impl_)); + RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "client's response type support is null", return RMW_RET_ERROR); + delete impl; + return rmw_fastrtps_shared_cpp::__rmw_destroy_client( eprosima_fastrtps_identifier, node, client); } From 9287f984b1d6280ac8e39071954313f6d32d7e07 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Mon, 24 Feb 2020 14:27:35 +0100 Subject: [PATCH 06/20] Changes on rmw_service Signed-off-by: Miguel Company --- rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp | 52 +++++++++++++++++--- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp index f21e1034f..137bc8939 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp @@ -49,7 +49,7 @@ using Domain = eprosima::fastrtps::Domain; using Participant = eprosima::fastrtps::Participant; using TopicDataType = eprosima::fastrtps::TopicDataType; -using CustomParticipantInfo = CustomParticipantInfo; +using TypeSupportProxy = rmw_fastrtps_dynamic_cpp::TypeSupportProxy; extern "C" { @@ -111,6 +111,26 @@ rmw_create_service( info->participant_ = participant; info->typesupport_identifier_ = type_support->typesupport_identifier; + auto request_type_impl = _create_request_type_support( + type_support->data, info->typesupport_identifier_); + if(!request_type_impl) { + delete info; + RMW_SET_ERROR_MSG("failed to allocate request type support"); + return nullptr; + } + + auto response_type_impl = _create_response_type_support( + type_support->data, info->typesupport_identifier_); + if(!response_type_impl) { + delete request_type_impl; + delete info; + RMW_SET_ERROR_MSG("failed to allocate response type support"); + return nullptr; + } + + info->request_type_support_impl_ = request_type_impl; + info->response_type_support_impl_ = response_type_impl; + const void * untyped_request_members; const void * untyped_response_members; @@ -119,9 +139,6 @@ rmw_create_service( untyped_response_members = get_response_ptr( type_support->data, info->typesupport_identifier_); - info->request_type_support_impl_ = untyped_request_members; - info->response_type_support_impl_ = untyped_response_members; - std::string request_type_name = _create_type_name( untyped_request_members, info->typesupport_identifier_); std::string response_type_name = _create_type_name( @@ -131,8 +148,11 @@ rmw_create_service( participant, request_type_name.c_str(), reinterpret_cast(&info->request_type_support_))) { - info->request_type_support_ = _create_request_type_support( - type_support->data, info->typesupport_identifier_); + info->request_type_support_ = new (std::nothrow) TypeSupportProxy(request_type_impl); + if(!info->request_type_support_) { + RMW_SET_ERROR_MSG("failed to allocate request TypeSupportProxy"); + goto fail; + } _register_type(participant, info->request_type_support_); } @@ -140,8 +160,11 @@ rmw_create_service( participant, response_type_name.c_str(), reinterpret_cast(&info->response_type_support_))) { - info->response_type_support_ = _create_response_type_support( - type_support->data, info->typesupport_identifier_); + info->response_type_support_ = new (std::nothrow) TypeSupportProxy(response_type_impl); + if(!info->response_type_support_) { + RMW_SET_ERROR_MSG("failed to allocate response TypeSupportProxy"); + goto fail; + } _register_type(participant, info->response_type_support_); } @@ -242,6 +265,8 @@ rmw_create_service( rmw_fastrtps_shared_cpp::_unregister_type(participant, info->response_type_support_); } + delete request_type_impl; + delete response_type_impl; delete info; } @@ -257,6 +282,17 @@ rmw_create_service( rmw_ret_t rmw_destroy_service(rmw_node_t * node, rmw_service_t * service) { + auto info = static_cast(service->data); + RCUTILS_CHECK_FOR_NULL_WITH_MSG(info, "service info pointer is null", return RMW_RET_ERROR); + + auto impl = static_cast(const_cast(info->request_type_support_impl_)); + RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "service's request type support is null", return RMW_RET_ERROR); + delete impl; + + impl = static_cast(const_cast(info->response_type_support_impl_)); + RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "service's response type support is null", return RMW_RET_ERROR); + delete impl; + return rmw_fastrtps_shared_cpp::__rmw_destroy_service( eprosima_fastrtps_identifier, node, service); } From 5e861afd9bf89073e72e606f458bfc0996d53ef9 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Mon, 24 Feb 2020 14:49:11 +0100 Subject: [PATCH 07/20] linters Signed-off-by: Miguel Company --- .../rmw_fastrtps_dynamic_cpp/TypeSupport.hpp | 2 +- rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp | 24 +++++++++++-------- .../src/rmw_publisher.cpp | 12 +++++----- rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp | 24 +++++++++++-------- .../src/rmw_subscription.cpp | 10 ++++---- .../src/type_support_common.cpp | 8 +++---- 6 files changed, 44 insertions(+), 36 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp index 441a69255..788063965 100644 --- a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp +++ b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp @@ -133,7 +133,7 @@ struct StringHelper class TypeSupportProxy : public rmw_fastrtps_shared_cpp::TypeSupport { public: - TypeSupportProxy(rmw_fastrtps_shared_cpp::TypeSupport * inner_type); + explicit TypeSupportProxy(rmw_fastrtps_shared_cpp::TypeSupport * inner_type); size_t getEstimatedSerializedSize(const void * ros_message, const void * impl) const override; diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp index 6e5f949a2..138b9c544 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp @@ -104,21 +104,21 @@ rmw_create_client( auto request_type_impl = _create_request_type_support( type_support->data, info->typesupport_identifier_); - if(!request_type_impl) { + if (!request_type_impl) { delete info; RMW_SET_ERROR_MSG("failed to allocate request type support"); return nullptr; } - + auto response_type_impl = _create_response_type_support( - type_support->data, info->typesupport_identifier_); - if(!response_type_impl) { + type_support->data, info->typesupport_identifier_); + if (!response_type_impl) { delete request_type_impl; delete info; RMW_SET_ERROR_MSG("failed to allocate response type support"); return nullptr; } - + info->request_type_support_impl_ = request_type_impl; info->response_type_support_impl_ = response_type_impl; @@ -140,7 +140,7 @@ rmw_create_client( reinterpret_cast(&info->request_type_support_))) { info->request_type_support_ = new (std::nothrow) TypeSupportProxy(request_type_impl); - if(!info->request_type_support_) { + if (!info->request_type_support_) { RMW_SET_ERROR_MSG("failed to allocate request TypeSupportProxy"); goto fail; } @@ -152,7 +152,7 @@ rmw_create_client( reinterpret_cast(&info->response_type_support_))) { info->response_type_support_ = new (std::nothrow) TypeSupportProxy(response_type_impl); - if(!info->response_type_support_) { + if (!info->response_type_support_) { RMW_SET_ERROR_MSG("failed to allocate response TypeSupportProxy"); goto fail; } @@ -293,11 +293,15 @@ rmw_destroy_client(rmw_node_t * node, rmw_client_t * client) RCUTILS_CHECK_FOR_NULL_WITH_MSG(info, "client info pointer is null", return RMW_RET_ERROR); auto impl = static_cast(const_cast(info->request_type_support_impl_)); - RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "client's request type support is null", return RMW_RET_ERROR); + RCUTILS_CHECK_FOR_NULL_WITH_MSG( + impl, "client's request type support is null", + return RMW_RET_ERROR); delete impl; - + impl = static_cast(const_cast(info->response_type_support_impl_)); - RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "client's response type support is null", return RMW_RET_ERROR); + RCUTILS_CHECK_FOR_NULL_WITH_MSG( + impl, "client's response type support is null", + return RMW_RET_ERROR); delete impl; return rmw_fastrtps_shared_cpp::__rmw_destroy_client( diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp index 388790e02..1199f27c9 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp @@ -133,15 +133,15 @@ rmw_create_publisher( RMW_SET_ERROR_MSG("failed to allocate CustomPublisherInfo"); return nullptr; } - + auto type_impl = _create_message_type_support( type_support->data, type_support->typesupport_identifier); - if(!type_impl) { + if (!type_impl) { delete info; RMW_SET_ERROR_MSG("failed to allocate type support"); return nullptr; } - + info->typesupport_identifier_ = type_support->typesupport_identifier; info->type_support_impl_ = type_impl; @@ -152,7 +152,7 @@ rmw_create_publisher( reinterpret_cast(&info->type_support_))) { info->type_support_ = new (std::nothrow) TypeSupportProxy(type_impl); - if(!info->type_support_) { + if (!info->type_support_) { RMW_SET_ERROR_MSG("failed to allocate TypeSupportProxy"); goto fail; } @@ -239,7 +239,7 @@ rmw_create_publisher( } delete info; } - + delete type_impl; if (rmw_publisher) { @@ -309,7 +309,7 @@ rmw_destroy_publisher(rmw_node_t * node, rmw_publisher_t * publisher) auto impl = static_cast(const_cast(info->type_support_impl_)); RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "publisher type support is null", return RMW_RET_ERROR); - + delete impl; return rmw_fastrtps_shared_cpp::__rmw_destroy_publisher( diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp index 137bc8939..260ecf0ba 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp @@ -113,21 +113,21 @@ rmw_create_service( auto request_type_impl = _create_request_type_support( type_support->data, info->typesupport_identifier_); - if(!request_type_impl) { + if (!request_type_impl) { delete info; RMW_SET_ERROR_MSG("failed to allocate request type support"); return nullptr; } - + auto response_type_impl = _create_response_type_support( - type_support->data, info->typesupport_identifier_); - if(!response_type_impl) { + type_support->data, info->typesupport_identifier_); + if (!response_type_impl) { delete request_type_impl; delete info; RMW_SET_ERROR_MSG("failed to allocate response type support"); return nullptr; } - + info->request_type_support_impl_ = request_type_impl; info->response_type_support_impl_ = response_type_impl; @@ -149,7 +149,7 @@ rmw_create_service( reinterpret_cast(&info->request_type_support_))) { info->request_type_support_ = new (std::nothrow) TypeSupportProxy(request_type_impl); - if(!info->request_type_support_) { + if (!info->request_type_support_) { RMW_SET_ERROR_MSG("failed to allocate request TypeSupportProxy"); goto fail; } @@ -161,7 +161,7 @@ rmw_create_service( reinterpret_cast(&info->response_type_support_))) { info->response_type_support_ = new (std::nothrow) TypeSupportProxy(response_type_impl); - if(!info->response_type_support_) { + if (!info->response_type_support_) { RMW_SET_ERROR_MSG("failed to allocate response TypeSupportProxy"); goto fail; } @@ -286,11 +286,15 @@ rmw_destroy_service(rmw_node_t * node, rmw_service_t * service) RCUTILS_CHECK_FOR_NULL_WITH_MSG(info, "service info pointer is null", return RMW_RET_ERROR); auto impl = static_cast(const_cast(info->request_type_support_impl_)); - RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "service's request type support is null", return RMW_RET_ERROR); + RCUTILS_CHECK_FOR_NULL_WITH_MSG( + impl, "service's request type support is null", + return RMW_RET_ERROR); delete impl; - + impl = static_cast(const_cast(info->response_type_support_impl_)); - RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "service's response type support is null", return RMW_RET_ERROR); + RCUTILS_CHECK_FOR_NULL_WITH_MSG( + impl, "service's response type support is null", + return RMW_RET_ERROR); delete impl; return rmw_fastrtps_shared_cpp::__rmw_destroy_service( diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp index 85533bce7..f459afab6 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp @@ -138,12 +138,12 @@ rmw_create_subscription( auto type_impl = _create_message_type_support( type_support->data, type_support->typesupport_identifier); - if(!type_impl) { + if (!type_impl) { delete info; RMW_SET_ERROR_MSG("failed to allocate type support"); return nullptr; } - + info->typesupport_identifier_ = type_support->typesupport_identifier; info->type_support_impl_ = type_impl; @@ -154,7 +154,7 @@ rmw_create_subscription( reinterpret_cast(&info->type_support_))) { info->type_support_ = new (std::nothrow) TypeSupportProxy(type_impl); - if(!info->type_support_) { + if (!info->type_support_) { RMW_SET_ERROR_MSG("failed to allocate TypeSupportProxy"); goto fail; } @@ -219,7 +219,7 @@ rmw_create_subscription( } delete info; } - + delete type_impl; if (rmw_subscription) { @@ -255,7 +255,7 @@ rmw_destroy_subscription(rmw_node_t * node, rmw_subscription_t * subscription) auto impl = static_cast(const_cast(info->type_support_impl_)); RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "subscription type support is null", return RMW_RET_ERROR); - + delete impl; return rmw_fastrtps_shared_cpp::__rmw_destroy_subscription( diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp b/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp index 467118cf7..d3add8ba6 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp @@ -101,24 +101,24 @@ TypeSupportProxy::TypeSupportProxy(rmw_fastrtps_shared_cpp::TypeSupport * inner_ } size_t TypeSupportProxy::getEstimatedSerializedSize( - const void * ros_message, const void * impl) const + const void * ros_message, const void * impl) const { auto type_impl = static_cast(impl); return type_impl->getEstimatedSerializedSize(ros_message, impl); } bool TypeSupportProxy::serializeROSmessage( - const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) const + const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) const { auto type_impl = static_cast(impl); return type_impl->serializeROSmessage(ros_message, ser, impl); } bool TypeSupportProxy::deserializeROSmessage( - eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const + eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const { auto type_impl = static_cast(impl); return type_impl->deserializeROSmessage(deser, ros_message, impl); } -} // namespace rmw_fastrtps_dynamic_cpp \ No newline at end of file +} // namespace rmw_fastrtps_dynamic_cpp From c530c31b648f72f0f955523f9c8dd1ab8358b3aa Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 25 Feb 2020 11:25:07 +0100 Subject: [PATCH 08/20] Moving LockedObject template to its own header. Signed-off-by: Miguel Company --- .../rmw_fastrtps_shared_cpp/locked_object.hpp | 49 +++++++++++++++++++ .../rmw_fastrtps_shared_cpp/topic_cache.hpp | 29 +---------- 2 files changed, 50 insertions(+), 28 deletions(-) create mode 100644 rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/locked_object.hpp diff --git a/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/locked_object.hpp b/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/locked_object.hpp new file mode 100644 index 000000000..b880d04ab --- /dev/null +++ b/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/locked_object.hpp @@ -0,0 +1,49 @@ +// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef RMW_FASTRTPS_SHARED_CPP__LOCKED_OBJECT_HPP_ +#define RMW_FASTRTPS_SHARED_CPP__LOCKED_OBJECT_HPP_ + +#include + +#include "rcpputils/thread_safety_annotations.hpp" + +template +class LockedObject +{ +private: + mutable std::mutex mutex_; + T object_ RCPPUTILS_TSA_GUARDED_BY(mutex_); + +public: + /** + * \return a reference to this object to lock. + */ + std::mutex & getMutex() const RCPPUTILS_TSA_RETURN_CAPABILITY(mutex_) + { + return mutex_; + } + + T & operator()() + { + return object_; + } + + const T & operator()() const + { + return object_; + } +}; + +#endif // RMW_FASTRTPS_SHARED_CPP__LOCKED_OBJECT_HPP_ diff --git a/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/topic_cache.hpp b/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/topic_cache.hpp index ba8f9647e..68a6e1f03 100644 --- a/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/topic_cache.hpp +++ b/rmw_fastrtps_shared_cpp/include/rmw_fastrtps_shared_cpp/topic_cache.hpp @@ -28,9 +28,9 @@ #include "fastrtps/participant/Participant.h" #include "fastrtps/rtps/common/Guid.h" #include "fastrtps/rtps/common/InstanceHandle.h" -#include "rcpputils/thread_safety_annotations.hpp" #include "rcutils/logging_macros.h" +#include "locked_object.hpp" #include "qos.hpp" typedef eprosima::fastrtps::rtps::GUID_t GUID_t; @@ -282,31 +282,4 @@ inline std::ostream & operator<<( return ostream; } -template -class LockedObject -{ -private: - mutable std::mutex mutex_; - T object_ RCPPUTILS_TSA_GUARDED_BY(mutex_); - -public: - /** - * \return a reference to this object to lock. - */ - std::mutex & getMutex() const RCPPUTILS_TSA_RETURN_CAPABILITY(mutex_) - { - return mutex_; - } - - T & operator()() - { - return object_; - } - - const T & operator()() const - { - return object_; - } -}; - #endif // RMW_FASTRTPS_SHARED_CPP__TOPIC_CACHE_HPP_ From 86e589c089a998da119c22b173ba06e178ff2204 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 25 Feb 2020 13:19:47 +0100 Subject: [PATCH 09/20] class TypeSupportProxy on its own source file. Signed-off-by: Miguel Company --- rmw_fastrtps_dynamic_cpp/CMakeLists.txt | 1 + .../src/type_support_common.cpp | 32 ------------- .../src/type_support_proxy.cpp | 47 +++++++++++++++++++ 3 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 rmw_fastrtps_dynamic_cpp/src/type_support_proxy.cpp diff --git a/rmw_fastrtps_dynamic_cpp/CMakeLists.txt b/rmw_fastrtps_dynamic_cpp/CMakeLists.txt index c64f4215b..d1821cc2c 100644 --- a/rmw_fastrtps_dynamic_cpp/CMakeLists.txt +++ b/rmw_fastrtps_dynamic_cpp/CMakeLists.txt @@ -85,6 +85,7 @@ add_library(rmw_fastrtps_dynamic_cpp src/rmw_wait.cpp src/rmw_wait_set.cpp src/type_support_common.cpp + src/type_support_proxy.cpp src/serialization_format.cpp ) target_link_libraries(rmw_fastrtps_dynamic_cpp diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp b/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp index d3add8ba6..cf6300285 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp @@ -90,35 +90,3 @@ _register_type( { eprosima::fastrtps::Domain::registerType(participant, typed_typesupport); } - -namespace rmw_fastrtps_dynamic_cpp -{ - -TypeSupportProxy::TypeSupportProxy(rmw_fastrtps_shared_cpp::TypeSupport * inner_type) -{ - setName(inner_type->getName()); - m_typeSize = inner_type->m_typeSize; -} - -size_t TypeSupportProxy::getEstimatedSerializedSize( - const void * ros_message, const void * impl) const -{ - auto type_impl = static_cast(impl); - return type_impl->getEstimatedSerializedSize(ros_message, impl); -} - -bool TypeSupportProxy::serializeROSmessage( - const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) const -{ - auto type_impl = static_cast(impl); - return type_impl->serializeROSmessage(ros_message, ser, impl); -} - -bool TypeSupportProxy::deserializeROSmessage( - eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const -{ - auto type_impl = static_cast(impl); - return type_impl->deserializeROSmessage(deser, ros_message, impl); -} - -} // namespace rmw_fastrtps_dynamic_cpp diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_proxy.cpp b/rmw_fastrtps_dynamic_cpp/src/type_support_proxy.cpp new file mode 100644 index 000000000..cc9eead5d --- /dev/null +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_proxy.cpp @@ -0,0 +1,47 @@ +// Copyright 2016-2018 Proyectos y Sistemas de Mantenimiento SL (eProsima). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "rmw_fastrtps_dynamic_cpp/TypeSupport.hpp" + +namespace rmw_fastrtps_dynamic_cpp +{ + +TypeSupportProxy::TypeSupportProxy(rmw_fastrtps_shared_cpp::TypeSupport * inner_type) +{ + setName(inner_type->getName()); + m_typeSize = inner_type->m_typeSize; +} + +size_t TypeSupportProxy::getEstimatedSerializedSize( + const void * ros_message, const void * impl) const +{ + auto type_impl = static_cast(impl); + return type_impl->getEstimatedSerializedSize(ros_message, impl); +} + +bool TypeSupportProxy::serializeROSmessage( + const void * ros_message, eprosima::fastcdr::Cdr & ser, const void * impl) const +{ + auto type_impl = static_cast(impl); + return type_impl->serializeROSmessage(ros_message, ser, impl); +} + +bool TypeSupportProxy::deserializeROSmessage( + eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const +{ + auto type_impl = static_cast(impl); + return type_impl->deserializeROSmessage(deser, ros_message, impl); +} + +} // namespace rmw_fastrtps_dynamic_cpp From 05a719149dc457e0b1bd4f895c66155dd7f2d19d Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 25 Feb 2020 15:12:09 +0100 Subject: [PATCH 10/20] Added TypeSupportRegistry Signed-off-by: Miguel Company --- rmw_fastrtps_dynamic_cpp/CMakeLists.txt | 1 + .../src/type_support_registry.cpp | 96 +++++++++++++++++++ .../src/type_support_registry.hpp | 72 ++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp create mode 100644 rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp diff --git a/rmw_fastrtps_dynamic_cpp/CMakeLists.txt b/rmw_fastrtps_dynamic_cpp/CMakeLists.txt index d1821cc2c..ace635149 100644 --- a/rmw_fastrtps_dynamic_cpp/CMakeLists.txt +++ b/rmw_fastrtps_dynamic_cpp/CMakeLists.txt @@ -86,6 +86,7 @@ add_library(rmw_fastrtps_dynamic_cpp src/rmw_wait_set.cpp src/type_support_common.cpp src/type_support_proxy.cpp + src/type_support_registry.cpp src/serialization_format.cpp ) target_link_libraries(rmw_fastrtps_dynamic_cpp diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp new file mode 100644 index 000000000..f3b6b8cb4 --- /dev/null +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp @@ -0,0 +1,96 @@ +// Copyright 2020 Proyectos y Sistemas de Mantenimiento SL (eProsima). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "type_support_common.hpp" +#include "type_support_registry.hpp" + +template +rmw_fastrtps_shared_cpp::TypeSupport * get_type_support( + const key_type & ros_type_support, map_type & map, creator fun) +{ + std::lock_guard guard(map.getMutex()); + RefCountedTypeSupport & item = map()[ros_type_support]; + if (0 == item.ref_count++) { + item.type_support = fun( + ros_type_support->data, ros_type_support->typesupport_identifier); + if (!item.type_support) { + map().erase(ros_type_support); + return nullptr; + } + } + return item.type_support; +} + +template +void return_type_support( + const key_type & ros_type_support, map_type & map) +{ + std::lock_guard guard(map.getMutex()); + auto it = map().find(ros_type_support); + assert(it != map().end()); + if (0 == --it->second.ref_count) { + map().erase(it); + } +} + +TypeSupportRegistry::~TypeSupportRegistry() +{ + assert(message_types_.empty()); + assert(request_types_.empty()); + assert(response_types_.empty()); +} + +rmw_fastrtps_shared_cpp::TypeSupport * TypeSupportRegistry::get_message_type_support( + const rosidl_message_type_support_t * ros_type_support) +{ + return get_type_support(ros_type_support, message_types_, _create_message_type_support); +} + +rmw_fastrtps_shared_cpp::TypeSupport * TypeSupportRegistry::get_request_type_support( + const rosidl_service_type_support_t * ros_type_support) +{ + return get_type_support(ros_type_support, request_types_, _create_request_type_support); +} + +rmw_fastrtps_shared_cpp::TypeSupport * TypeSupportRegistry::get_response_type_support( + const rosidl_service_type_support_t * ros_type_support) +{ + return get_type_support(ros_type_support, response_types_, _create_response_type_support); +} + +void TypeSupportRegistry::return_message_type_support( + const rosidl_message_type_support_t * ros_type_support) +{ + return_type_support(ros_type_support, message_types_); +} + +void TypeSupportRegistry::return_request_type_support( + const rosidl_service_type_support_t * ros_type_support) +{ + return_type_support(ros_type_support, request_types_); +} + +void TypeSupportRegistry::return_response_type_support( + const rosidl_service_type_support_t * ros_type_support) +{ + return_type_support(ros_type_support, response_types_); +} + +static TypeSupportRegistry type_registry_instance; + +TypeSupportRegistry & get_type_support_registry(const rmw_node_t * node) +{ + (void)node; + return type_registry_instance; +} diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp new file mode 100644 index 000000000..efba1e39a --- /dev/null +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp @@ -0,0 +1,72 @@ +// Copyright 2020 Proyectos y Sistemas de Mantenimiento SL (eProsima). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef TYPE_SUPPORT_REGISTRY_HPP_ +#define TYPE_SUPPORT_REGISTRY_HPP_ + +#include + +#include "rmw/rmw.h" + +#include "rmw_fastrtps_shared_cpp/locked_object.hpp" +#include "rmw_fastrtps_shared_cpp/TypeSupport.hpp" + +#include "type_support_common.hpp" + +using type_support_ptr = rmw_fastrtps_shared_cpp::TypeSupport *; + +/** + * A data structure to use as value type for the type registry. + */ +struct RefCountedTypeSupport +{ + type_support_ptr type_support = nullptr; + uint32_t ref_count = 0; +}; + +using msg_map_t = std::unordered_map; +using srv_map_t = std::unordered_map; + +class TypeSupportRegistry +{ +private: + LockedObject message_types_; + LockedObject request_types_; + LockedObject response_types_; + +public: + ~TypeSupportRegistry(); + + rmw_fastrtps_shared_cpp::TypeSupport * get_message_type_support( + const rosidl_message_type_support_t * ros_type_support); + + rmw_fastrtps_shared_cpp::TypeSupport * get_request_type_support( + const rosidl_service_type_support_t * ros_type_support); + + rmw_fastrtps_shared_cpp::TypeSupport * get_response_type_support( + const rosidl_service_type_support_t * ros_type_support); + + void return_message_type_support( + const rosidl_message_type_support_t * ros_type_support); + + void return_request_type_support( + const rosidl_service_type_support_t * ros_type_support); + + void return_response_type_support( + const rosidl_service_type_support_t * ros_type_support); +}; + +TypeSupportRegistry & get_type_support_registry(const rmw_node_t * node); + +#endif // TYPE_SUPPORT_REGISTRY_HPP_ From 36c8faf4c5f856aa6a9ca73715ba0c0e6fd03fb3 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 25 Feb 2020 16:14:40 +0100 Subject: [PATCH 11/20] Added generic pointer to ros type support so we can reference the registry key. Signed-off-by: Miguel Company --- .../MessageTypeSupport.hpp | 2 +- .../MessageTypeSupport_impl.hpp | 4 +++- .../ServiceTypeSupport.hpp | 15 ++++----------- .../ServiceTypeSupport_impl.hpp | 11 ++++------- .../rmw_fastrtps_dynamic_cpp/TypeSupport.hpp | 8 +++++++- .../rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp | 3 ++- .../src/type_support_common.cpp | 12 ++++++------ 7 files changed, 27 insertions(+), 28 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/MessageTypeSupport.hpp b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/MessageTypeSupport.hpp index db9158643..b63518852 100644 --- a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/MessageTypeSupport.hpp +++ b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/MessageTypeSupport.hpp @@ -32,7 +32,7 @@ template class MessageTypeSupport : public TypeSupport { public: - explicit MessageTypeSupport(const MembersType * members); + MessageTypeSupport(const MembersType * members, const void * ros_type_support); }; } // namespace rmw_fastrtps_dynamic_cpp diff --git a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/MessageTypeSupport_impl.hpp b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/MessageTypeSupport_impl.hpp index b0a26cb13..980888915 100644 --- a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/MessageTypeSupport_impl.hpp +++ b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/MessageTypeSupport_impl.hpp @@ -32,7 +32,9 @@ namespace rmw_fastrtps_dynamic_cpp { template -MessageTypeSupport::MessageTypeSupport(const MembersType * members) +MessageTypeSupport::MessageTypeSupport( + const MembersType * members, const void * ros_type_support) +: TypeSupport(ros_type_support) { assert(members); this->members_ = members; diff --git a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/ServiceTypeSupport.hpp b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/ServiceTypeSupport.hpp index 3b908f1e9..607bf86ef 100644 --- a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/ServiceTypeSupport.hpp +++ b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/ServiceTypeSupport.hpp @@ -25,25 +25,18 @@ namespace rmw_fastrtps_dynamic_cpp { -template -class ServiceTypeSupport : public TypeSupport -{ -protected: - ServiceTypeSupport(); -}; - template -class RequestTypeSupport : public ServiceTypeSupport +class RequestTypeSupport : public TypeSupport { public: - explicit RequestTypeSupport(const ServiceMembersType * members); + RequestTypeSupport(const ServiceMembersType * members, const void * ros_type_support); }; template -class ResponseTypeSupport : public ServiceTypeSupport +class ResponseTypeSupport : public TypeSupport { public: - explicit ResponseTypeSupport(const ServiceMembersType * members); + ResponseTypeSupport(const ServiceMembersType * members, const void * ros_type_support); }; } // namespace rmw_fastrtps_dynamic_cpp diff --git a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/ServiceTypeSupport_impl.hpp b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/ServiceTypeSupport_impl.hpp index 218cbb91e..61237fdb9 100644 --- a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/ServiceTypeSupport_impl.hpp +++ b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/ServiceTypeSupport_impl.hpp @@ -29,14 +29,10 @@ namespace rmw_fastrtps_dynamic_cpp { -template -ServiceTypeSupport::ServiceTypeSupport() -{ -} - template RequestTypeSupport::RequestTypeSupport( - const ServiceMembersType * members) + const ServiceMembersType * members, const void * ros_type_support) +: TypeSupport(ros_type_support) { assert(members); this->members_ = members->request_members_; @@ -65,7 +61,8 @@ RequestTypeSupport::RequestTypeSupport( template ResponseTypeSupport::ResponseTypeSupport( - const ServiceMembersType * members) + const ServiceMembersType * members, const void * ros_type_support) +: TypeSupport(ros_type_support) { assert(members); this->members_ = members->response_members_; diff --git a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp index 788063965..1ed178b56 100644 --- a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp +++ b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp @@ -156,12 +156,18 @@ class TypeSupport : public rmw_fastrtps_shared_cpp::TypeSupport bool deserializeROSmessage( eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const override; + const void * ros_type_support() const + { + return ros_type_support_; + } + protected: - TypeSupport(); + explicit TypeSupport(const void * ros_type_support); size_t calculateMaxSerializedSize(const MembersType * members, size_t current_alignment); const MembersType * members_; + const void * ros_type_support_; private: size_t getEstimatedSerializedSize( diff --git a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp index 6d2b6e5f7..659d07497 100644 --- a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp +++ b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp @@ -108,10 +108,11 @@ rosidl_generator_c__void__Sequence__fini(rosidl_generator_c__void__Sequence * se } template -TypeSupport::TypeSupport() +TypeSupport::TypeSupport(const void * ros_type_support) { m_isGetKeyDefined = false; max_size_bound_ = false; + ros_type_support_ = ros_type_support; } static inline void * diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp b/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp index cf6300285..6bfe46c9f 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp @@ -41,11 +41,11 @@ _create_message_type_support(const void * untyped_members, const char * typesupp if (using_introspection_c_typesupport(typesupport_identifier)) { auto members = static_cast( untyped_members); - return new MessageTypeSupport_c(members); + return new MessageTypeSupport_c(members, nullptr); } else if (using_introspection_cpp_typesupport(typesupport_identifier)) { auto members = static_cast( untyped_members); - return new MessageTypeSupport_cpp(members); + return new MessageTypeSupport_cpp(members, nullptr); } RMW_SET_ERROR_MSG("Unknown typesupport identifier"); return nullptr; @@ -57,11 +57,11 @@ _create_request_type_support(const void * untyped_members, const char * typesupp if (using_introspection_c_typesupport(typesupport_identifier)) { auto members = static_cast( untyped_members); - return new RequestTypeSupport_c(members); + return new RequestTypeSupport_c(members, nullptr); } else if (using_introspection_cpp_typesupport(typesupport_identifier)) { auto members = static_cast( untyped_members); - return new RequestTypeSupport_cpp(members); + return new RequestTypeSupport_cpp(members, nullptr); } RMW_SET_ERROR_MSG("Unknown typesupport identifier"); return nullptr; @@ -73,11 +73,11 @@ _create_response_type_support(const void * untyped_members, const char * typesup if (using_introspection_c_typesupport(typesupport_identifier)) { auto members = static_cast( untyped_members); - return new ResponseTypeSupport_c(members); + return new ResponseTypeSupport_c(members, nullptr); } else if (using_introspection_cpp_typesupport(typesupport_identifier)) { auto members = static_cast( untyped_members); - return new ResponseTypeSupport_cpp(members); + return new ResponseTypeSupport_cpp(members, nullptr); } RMW_SET_ERROR_MSG("Unknown typesupport identifier"); return nullptr; From 3eecc83065d9a11400812434d254b29ddc40cc5b Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 25 Feb 2020 16:30:47 +0100 Subject: [PATCH 12/20] Creator functions passed to type_support_registry. Signed-off-by: Miguel Company --- .../src/type_support_registry.cpp | 56 +++++++++++++++++-- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp index f3b6b8cb4..de76c6e9d 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "rmw/error_handling.h" + #include "type_support_common.hpp" #include "type_support_registry.hpp" @@ -22,8 +24,7 @@ rmw_fastrtps_shared_cpp::TypeSupport * get_type_support( std::lock_guard guard(map.getMutex()); RefCountedTypeSupport & item = map()[ros_type_support]; if (0 == item.ref_count++) { - item.type_support = fun( - ros_type_support->data, ros_type_support->typesupport_identifier); + item.type_support = fun(); if (!item.type_support) { map().erase(ros_type_support); return nullptr; @@ -54,19 +55,64 @@ TypeSupportRegistry::~TypeSupportRegistry() rmw_fastrtps_shared_cpp::TypeSupport * TypeSupportRegistry::get_message_type_support( const rosidl_message_type_support_t * ros_type_support) { - return get_type_support(ros_type_support, message_types_, _create_message_type_support); + auto creator_fun = [&ros_type_support]() -> rmw_fastrtps_shared_cpp::TypeSupport * + { + if (using_introspection_c_typesupport(ros_type_support->typesupport_identifier)) { + auto members = static_cast( + ros_type_support->data); + return new MessageTypeSupport_c(members, ros_type_support); + } else if (using_introspection_cpp_typesupport(ros_type_support->typesupport_identifier)) { + auto members = static_cast( + ros_type_support->data); + return new MessageTypeSupport_cpp(members, ros_type_support); + } + RMW_SET_ERROR_MSG("Unknown typesupport identifier"); + return nullptr; + }; + + return get_type_support(ros_type_support, message_types_, creator_fun); } rmw_fastrtps_shared_cpp::TypeSupport * TypeSupportRegistry::get_request_type_support( const rosidl_service_type_support_t * ros_type_support) { - return get_type_support(ros_type_support, request_types_, _create_request_type_support); + auto creator_fun = [&ros_type_support]() -> rmw_fastrtps_shared_cpp::TypeSupport * + { + if (using_introspection_c_typesupport(ros_type_support->typesupport_identifier)) { + auto members = static_cast( + ros_type_support->data); + return new RequestTypeSupport_c(members, ros_type_support); + } else if (using_introspection_cpp_typesupport(ros_type_support->typesupport_identifier)) { + auto members = static_cast( + ros_type_support->data); + return new RequestTypeSupport_cpp(members, ros_type_support); + } + RMW_SET_ERROR_MSG("Unknown typesupport identifier"); + return nullptr; + }; + + return get_type_support(ros_type_support, request_types_, creator_fun); } rmw_fastrtps_shared_cpp::TypeSupport * TypeSupportRegistry::get_response_type_support( const rosidl_service_type_support_t * ros_type_support) { - return get_type_support(ros_type_support, response_types_, _create_response_type_support); + auto creator_fun = [&ros_type_support]() -> rmw_fastrtps_shared_cpp::TypeSupport * + { + if (using_introspection_c_typesupport(ros_type_support->typesupport_identifier)) { + auto members = static_cast( + ros_type_support->data); + return new ResponseTypeSupport_c(members, ros_type_support); + } else if (using_introspection_cpp_typesupport(ros_type_support->typesupport_identifier)) { + auto members = static_cast( + ros_type_support->data); + return new ResponseTypeSupport_cpp(members, ros_type_support); + } + RMW_SET_ERROR_MSG("Unknown typesupport identifier"); + return nullptr; + }; + + return get_type_support(ros_type_support, response_types_, creator_fun); } void TypeSupportRegistry::return_message_type_support( From 6cdac9b2311a9c006249823b8e6e202e54ab5468 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 25 Feb 2020 16:46:38 +0100 Subject: [PATCH 13/20] Added base class for all rmw_fastrtps_dynamic_cpp type support classes. Signed-off-by: Miguel Company --- .../rmw_fastrtps_dynamic_cpp/TypeSupport.hpp | 26 ++++++++++++++----- .../TypeSupport_impl.hpp | 2 +- .../src/type_support_registry.cpp | 14 +++++----- .../src/type_support_registry.hpp | 8 +++--- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp index 1ed178b56..f16fff136 100644 --- a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp +++ b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport.hpp @@ -144,8 +144,26 @@ class TypeSupportProxy : public rmw_fastrtps_shared_cpp::TypeSupport eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const override; }; +class BaseTypeSupport : public rmw_fastrtps_shared_cpp::TypeSupport +{ +public: + const void * ros_type_support() const + { + return ros_type_support_; + } + +protected: + explicit BaseTypeSupport(const void * ros_type_support) + { + ros_type_support_ = ros_type_support; + } + +private: + const void * ros_type_support_; +}; + template -class TypeSupport : public rmw_fastrtps_shared_cpp::TypeSupport +class TypeSupport : public BaseTypeSupport { public: size_t getEstimatedSerializedSize(const void * ros_message, const void * impl) const override; @@ -156,18 +174,12 @@ class TypeSupport : public rmw_fastrtps_shared_cpp::TypeSupport bool deserializeROSmessage( eprosima::fastcdr::Cdr & deser, void * ros_message, const void * impl) const override; - const void * ros_type_support() const - { - return ros_type_support_; - } - protected: explicit TypeSupport(const void * ros_type_support); size_t calculateMaxSerializedSize(const MembersType * members, size_t current_alignment); const MembersType * members_; - const void * ros_type_support_; private: size_t getEstimatedSerializedSize( diff --git a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp index 659d07497..1c97c8e05 100644 --- a/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp +++ b/rmw_fastrtps_dynamic_cpp/include/rmw_fastrtps_dynamic_cpp/TypeSupport_impl.hpp @@ -109,10 +109,10 @@ rosidl_generator_c__void__Sequence__fini(rosidl_generator_c__void__Sequence * se template TypeSupport::TypeSupport(const void * ros_type_support) +: BaseTypeSupport(ros_type_support) { m_isGetKeyDefined = false; max_size_bound_ = false; - ros_type_support_ = ros_type_support; } static inline void * diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp index de76c6e9d..75e02e847 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp @@ -18,7 +18,7 @@ #include "type_support_registry.hpp" template -rmw_fastrtps_shared_cpp::TypeSupport * get_type_support( +type_support_ptr get_type_support( const key_type & ros_type_support, map_type & map, creator fun) { std::lock_guard guard(map.getMutex()); @@ -52,10 +52,10 @@ TypeSupportRegistry::~TypeSupportRegistry() assert(response_types_.empty()); } -rmw_fastrtps_shared_cpp::TypeSupport * TypeSupportRegistry::get_message_type_support( +type_support_ptr TypeSupportRegistry::get_message_type_support( const rosidl_message_type_support_t * ros_type_support) { - auto creator_fun = [&ros_type_support]() -> rmw_fastrtps_shared_cpp::TypeSupport * + auto creator_fun = [&ros_type_support]() -> type_support_ptr { if (using_introspection_c_typesupport(ros_type_support->typesupport_identifier)) { auto members = static_cast( @@ -73,10 +73,10 @@ rmw_fastrtps_shared_cpp::TypeSupport * TypeSupportRegistry::get_message_type_sup return get_type_support(ros_type_support, message_types_, creator_fun); } -rmw_fastrtps_shared_cpp::TypeSupport * TypeSupportRegistry::get_request_type_support( +type_support_ptr TypeSupportRegistry::get_request_type_support( const rosidl_service_type_support_t * ros_type_support) { - auto creator_fun = [&ros_type_support]() -> rmw_fastrtps_shared_cpp::TypeSupport * + auto creator_fun = [&ros_type_support]() -> type_support_ptr { if (using_introspection_c_typesupport(ros_type_support->typesupport_identifier)) { auto members = static_cast( @@ -94,10 +94,10 @@ rmw_fastrtps_shared_cpp::TypeSupport * TypeSupportRegistry::get_request_type_sup return get_type_support(ros_type_support, request_types_, creator_fun); } -rmw_fastrtps_shared_cpp::TypeSupport * TypeSupportRegistry::get_response_type_support( +type_support_ptr TypeSupportRegistry::get_response_type_support( const rosidl_service_type_support_t * ros_type_support) { - auto creator_fun = [&ros_type_support]() -> rmw_fastrtps_shared_cpp::TypeSupport * + auto creator_fun = [&ros_type_support]() -> type_support_ptr { if (using_introspection_c_typesupport(ros_type_support->typesupport_identifier)) { auto members = static_cast( diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp index efba1e39a..d4e1eac39 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp @@ -24,7 +24,7 @@ #include "type_support_common.hpp" -using type_support_ptr = rmw_fastrtps_shared_cpp::TypeSupport *; +using type_support_ptr = rmw_fastrtps_dynamic_cpp::BaseTypeSupport *; /** * A data structure to use as value type for the type registry. @@ -48,13 +48,13 @@ class TypeSupportRegistry public: ~TypeSupportRegistry(); - rmw_fastrtps_shared_cpp::TypeSupport * get_message_type_support( + type_support_ptr get_message_type_support( const rosidl_message_type_support_t * ros_type_support); - rmw_fastrtps_shared_cpp::TypeSupport * get_request_type_support( + type_support_ptr get_request_type_support( const rosidl_service_type_support_t * ros_type_support); - rmw_fastrtps_shared_cpp::TypeSupport * get_response_type_support( + type_support_ptr get_response_type_support( const rosidl_service_type_support_t * ros_type_support); void return_message_type_support( From 205c1d00cc2181bf4319a384b24bb848a3dced5e Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 25 Feb 2020 17:15:04 +0100 Subject: [PATCH 14/20] using TypeSupportRegistry on services and messages Signed-off-by: Miguel Company --- rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp | 31 ++++++++++------ .../src/rmw_publisher.cpp | 16 +++++--- rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp | 37 ++++++++++++------- .../src/rmw_subscription.cpp | 18 ++++++--- 4 files changed, 66 insertions(+), 36 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp index 138b9c544..0ff5b303f 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp @@ -34,7 +34,9 @@ #include "client_service_common.hpp" #include "type_support_common.hpp" +#include "type_support_registry.hpp" +using BaseTypeSupport = rmw_fastrtps_dynamic_cpp::BaseTypeSupport; using Domain = eprosima::fastrtps::Domain; using Participant = eprosima::fastrtps::Participant; using TopicDataType = eprosima::fastrtps::TopicDataType; @@ -102,18 +104,17 @@ rmw_create_client( info->request_publisher_matched_count_ = 0; info->response_subscriber_matched_count_ = 0; - auto request_type_impl = _create_request_type_support( - type_support->data, info->typesupport_identifier_); + TypeSupportRegistry & type_registry = get_type_support_registry(node); + auto request_type_impl = type_registry.get_request_type_support(type_support); if (!request_type_impl) { delete info; RMW_SET_ERROR_MSG("failed to allocate request type support"); return nullptr; } - auto response_type_impl = _create_response_type_support( - type_support->data, info->typesupport_identifier_); + auto response_type_impl = type_registry.get_response_type_support(type_support); if (!response_type_impl) { - delete request_type_impl; + type_registry.return_request_type_support(type_support); delete info; RMW_SET_ERROR_MSG("failed to allocate response type support"); return nullptr; @@ -269,8 +270,8 @@ rmw_create_client( "leaking type support objects because node impl is null"); } - delete request_type_impl; - delete response_type_impl; + type_registry.return_request_type_support(type_support); + type_registry.return_response_type_support(type_support); delete info; info = nullptr; } @@ -292,17 +293,25 @@ rmw_destroy_client(rmw_node_t * node, rmw_client_t * client) auto info = static_cast(client->data); RCUTILS_CHECK_FOR_NULL_WITH_MSG(info, "client info pointer is null", return RMW_RET_ERROR); - auto impl = static_cast(const_cast(info->request_type_support_impl_)); + auto impl = static_cast(const_cast(info->request_type_support_impl_)); RCUTILS_CHECK_FOR_NULL_WITH_MSG( impl, "client's request type support is null", return RMW_RET_ERROR); - delete impl; - impl = static_cast(const_cast(info->response_type_support_impl_)); + auto ros_type_support = static_cast( + impl->ros_type_support()); + + TypeSupportRegistry & type_registry = get_type_support_registry(node); + type_registry.return_request_type_support(ros_type_support); + + impl = static_cast(const_cast(info->response_type_support_impl_)); RCUTILS_CHECK_FOR_NULL_WITH_MSG( impl, "client's response type support is null", return RMW_RET_ERROR); - delete impl; + + ros_type_support = static_cast( + impl->ros_type_support()); + type_registry.return_response_type_support(ros_type_support); return rmw_fastrtps_shared_cpp::__rmw_destroy_client( eprosima_fastrtps_identifier, node, client); diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp index 1199f27c9..bb1a98fb1 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp @@ -28,7 +28,9 @@ #include "rmw_fastrtps_dynamic_cpp/identifier.hpp" #include "type_support_common.hpp" +#include "type_support_registry.hpp" +using BaseTypeSupport = rmw_fastrtps_dynamic_cpp::BaseTypeSupport; using Domain = eprosima::fastrtps::Domain; using Participant = eprosima::fastrtps::Participant; using TopicDataType = eprosima::fastrtps::TopicDataType; @@ -134,8 +136,8 @@ rmw_create_publisher( return nullptr; } - auto type_impl = _create_message_type_support( - type_support->data, type_support->typesupport_identifier); + TypeSupportRegistry & type_registry = get_type_support_registry(node); + auto type_impl = type_registry.get_message_type_support(type_support); if (!type_impl) { delete info; RMW_SET_ERROR_MSG("failed to allocate type support"); @@ -240,7 +242,7 @@ rmw_create_publisher( delete info; } - delete type_impl; + type_registry.return_message_type_support(type_support); if (rmw_publisher) { rmw_publisher_free(rmw_publisher); @@ -307,10 +309,14 @@ rmw_destroy_publisher(rmw_node_t * node, rmw_publisher_t * publisher) auto info = static_cast(publisher->data); RCUTILS_CHECK_FOR_NULL_WITH_MSG(info, "publisher info pointer is null", return RMW_RET_ERROR); - auto impl = static_cast(const_cast(info->type_support_impl_)); + auto impl = static_cast(info->type_support_impl_); RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "publisher type support is null", return RMW_RET_ERROR); - delete impl; + auto ros_type_support = static_cast( + impl->ros_type_support()); + + TypeSupportRegistry & type_registry = get_type_support_registry(node); + type_registry.return_message_type_support(ros_type_support); return rmw_fastrtps_shared_cpp::__rmw_destroy_publisher( eprosima_fastrtps_identifier, node, publisher); diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp index 260ecf0ba..557e00d28 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp @@ -43,9 +43,11 @@ #include "rmw_fastrtps_dynamic_cpp/identifier.hpp" -#include "type_support_common.hpp" #include "client_service_common.hpp" +#include "type_support_common.hpp" +#include "type_support_registry.hpp" +using BaseTypeSupport = rmw_fastrtps_dynamic_cpp::BaseTypeSupport; using Domain = eprosima::fastrtps::Domain; using Participant = eprosima::fastrtps::Participant; using TopicDataType = eprosima::fastrtps::TopicDataType; @@ -111,18 +113,17 @@ rmw_create_service( info->participant_ = participant; info->typesupport_identifier_ = type_support->typesupport_identifier; - auto request_type_impl = _create_request_type_support( - type_support->data, info->typesupport_identifier_); + TypeSupportRegistry & type_registry = get_type_support_registry(node); + auto request_type_impl = type_registry.get_request_type_support(type_support); if (!request_type_impl) { delete info; RMW_SET_ERROR_MSG("failed to allocate request type support"); return nullptr; } - auto response_type_impl = _create_response_type_support( - type_support->data, info->typesupport_identifier_); + auto response_type_impl = type_registry.get_response_type_support(type_support); if (!response_type_impl) { - delete request_type_impl; + type_registry.return_request_type_support(type_support); delete info; RMW_SET_ERROR_MSG("failed to allocate response type support"); return nullptr; @@ -265,8 +266,8 @@ rmw_create_service( rmw_fastrtps_shared_cpp::_unregister_type(participant, info->response_type_support_); } - delete request_type_impl; - delete response_type_impl; + type_registry.return_request_type_support(type_support); + type_registry.return_response_type_support(type_support); delete info; } @@ -285,17 +286,25 @@ rmw_destroy_service(rmw_node_t * node, rmw_service_t * service) auto info = static_cast(service->data); RCUTILS_CHECK_FOR_NULL_WITH_MSG(info, "service info pointer is null", return RMW_RET_ERROR); - auto impl = static_cast(const_cast(info->request_type_support_impl_)); + auto impl = static_cast(const_cast(info->request_type_support_impl_)); RCUTILS_CHECK_FOR_NULL_WITH_MSG( - impl, "service's request type support is null", + impl, "client's request type support is null", return RMW_RET_ERROR); - delete impl; - impl = static_cast(const_cast(info->response_type_support_impl_)); + auto ros_type_support = static_cast( + impl->ros_type_support()); + + TypeSupportRegistry & type_registry = get_type_support_registry(node); + type_registry.return_request_type_support(ros_type_support); + + impl = static_cast(const_cast(info->response_type_support_impl_)); RCUTILS_CHECK_FOR_NULL_WITH_MSG( - impl, "service's response type support is null", + impl, "client's response type support is null", return RMW_RET_ERROR); - delete impl; + + ros_type_support = static_cast( + impl->ros_type_support()); + type_registry.return_response_type_support(ros_type_support); return rmw_fastrtps_shared_cpp::__rmw_destroy_service( eprosima_fastrtps_identifier, node, service); diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp index f459afab6..5a1ecadba 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp @@ -32,7 +32,9 @@ #include "rmw_fastrtps_dynamic_cpp/identifier.hpp" #include "type_support_common.hpp" +#include "type_support_registry.hpp" +using BaseTypeSupport = rmw_fastrtps_dynamic_cpp::BaseTypeSupport; using Domain = eprosima::fastrtps::Domain; using Participant = eprosima::fastrtps::Participant; using TopicDataType = eprosima::fastrtps::TopicDataType; @@ -136,8 +138,8 @@ rmw_create_subscription( return nullptr; } - auto type_impl = _create_message_type_support( - type_support->data, type_support->typesupport_identifier); + TypeSupportRegistry & type_registry = get_type_support_registry(node); + auto type_impl = type_registry.get_message_type_support(type_support); if (!type_impl) { delete info; RMW_SET_ERROR_MSG("failed to allocate type support"); @@ -220,7 +222,7 @@ rmw_create_subscription( delete info; } - delete type_impl; + type_registry.return_message_type_support(type_support); if (rmw_subscription) { rmw_subscription_free(rmw_subscription); @@ -253,10 +255,14 @@ rmw_destroy_subscription(rmw_node_t * node, rmw_subscription_t * subscription) auto info = static_cast(subscription->data); RCUTILS_CHECK_FOR_NULL_WITH_MSG(info, "subscription info pointer is null", return RMW_RET_ERROR); - auto impl = static_cast(const_cast(info->type_support_impl_)); - RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "subscription type support is null", return RMW_RET_ERROR); + auto impl = static_cast(info->type_support_impl_); + RCUTILS_CHECK_FOR_NULL_WITH_MSG(impl, "publisher type support is null", return RMW_RET_ERROR); - delete impl; + auto ros_type_support = static_cast( + impl->ros_type_support()); + + TypeSupportRegistry & type_registry = get_type_support_registry(node); + type_registry.return_message_type_support(ros_type_support); return rmw_fastrtps_shared_cpp::__rmw_destroy_subscription( eprosima_fastrtps_identifier, node, subscription); From b518fc5ff05fd1d678d28ed6a1a39a9d6a4411cd Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 25 Feb 2020 17:24:45 +0100 Subject: [PATCH 15/20] using TypeSupportRegistry on rmw_serialize Signed-off-by: Miguel Company --- rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp index ec0cb8378..feee65833 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp @@ -19,6 +19,7 @@ #include "rmw/rmw.h" #include "./type_support_common.hpp" +#include "./type_support_registry.hpp" extern "C" { @@ -39,11 +40,13 @@ rmw_serialize( } } - auto tss = _create_message_type_support(ts->data, ts->typesupport_identifier); + TypeSupportRegistry & type_registry = get_type_support_registry(nullptr); + auto tss = type_registry.get_message_type_support(ts); auto data_length = tss->getEstimatedSerializedSize(ros_message, ts->data); if (serialized_message->buffer_capacity < data_length) { if (rmw_serialized_message_resize(serialized_message, data_length) != RMW_RET_OK) { RMW_SET_ERROR_MSG("unable to dynamically resize serialized message"); + type_registry.return_message_type_support(ts); return RMW_RET_ERROR; } } @@ -57,7 +60,7 @@ rmw_serialize( auto ret = tss->serializeROSmessage(ros_message, ser, ts->data); serialized_message->buffer_length = data_length; serialized_message->buffer_capacity = data_length; - delete tss; + type_registry.return_message_type_support(ts); return ret == true ? RMW_RET_OK : RMW_RET_ERROR; } @@ -78,14 +81,15 @@ rmw_deserialize( } } - auto tss = _create_message_type_support(ts->data, ts->typesupport_identifier); + TypeSupportRegistry & type_registry = get_type_support_registry(nullptr); + auto tss = type_registry.get_message_type_support(ts); eprosima::fastcdr::FastBuffer buffer( reinterpret_cast(serialized_message->buffer), serialized_message->buffer_length); eprosima::fastcdr::Cdr deser(buffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR); auto ret = tss->deserializeROSmessage(deser, ros_message, ts->data); - delete tss; + type_registry.return_message_type_support(ts); return ret == true ? RMW_RET_OK : RMW_RET_ERROR; } From f9ef818865bff5a54af3b6b84ffc3a5b69a7110b Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 25 Feb 2020 17:28:11 +0100 Subject: [PATCH 16/20] Removed unused functions from type_support_common Signed-off-by: Miguel Company --- .../src/type_support_common.cpp | 48 ------------------- .../src/type_support_common.hpp | 9 ---- 2 files changed, 57 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp b/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp index 6bfe46c9f..fc5e3a23b 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_common.cpp @@ -35,54 +35,6 @@ using_introspection_cpp_typesupport(const char * typesupport_identifier) rosidl_typesupport_introspection_cpp::typesupport_identifier; } -rmw_fastrtps_shared_cpp::TypeSupport * -_create_message_type_support(const void * untyped_members, const char * typesupport_identifier) -{ - if (using_introspection_c_typesupport(typesupport_identifier)) { - auto members = static_cast( - untyped_members); - return new MessageTypeSupport_c(members, nullptr); - } else if (using_introspection_cpp_typesupport(typesupport_identifier)) { - auto members = static_cast( - untyped_members); - return new MessageTypeSupport_cpp(members, nullptr); - } - RMW_SET_ERROR_MSG("Unknown typesupport identifier"); - return nullptr; -} - -rmw_fastrtps_shared_cpp::TypeSupport * -_create_request_type_support(const void * untyped_members, const char * typesupport_identifier) -{ - if (using_introspection_c_typesupport(typesupport_identifier)) { - auto members = static_cast( - untyped_members); - return new RequestTypeSupport_c(members, nullptr); - } else if (using_introspection_cpp_typesupport(typesupport_identifier)) { - auto members = static_cast( - untyped_members); - return new RequestTypeSupport_cpp(members, nullptr); - } - RMW_SET_ERROR_MSG("Unknown typesupport identifier"); - return nullptr; -} - -rmw_fastrtps_shared_cpp::TypeSupport * -_create_response_type_support(const void * untyped_members, const char * typesupport_identifier) -{ - if (using_introspection_c_typesupport(typesupport_identifier)) { - auto members = static_cast( - untyped_members); - return new ResponseTypeSupport_c(members, nullptr); - } else if (using_introspection_cpp_typesupport(typesupport_identifier)) { - auto members = static_cast( - untyped_members); - return new ResponseTypeSupport_cpp(members, nullptr); - } - RMW_SET_ERROR_MSG("Unknown typesupport identifier"); - return nullptr; -} - void _register_type( eprosima::fastrtps::Participant * participant, diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_common.hpp b/rmw_fastrtps_dynamic_cpp/src/type_support_common.hpp index bd9e22d1d..a396d68f3 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_common.hpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_common.hpp @@ -112,15 +112,6 @@ _create_type_name( return ""; } -rmw_fastrtps_shared_cpp::TypeSupport * -_create_message_type_support(const void * untyped_members, const char * typesupport_identifier); - -rmw_fastrtps_shared_cpp::TypeSupport * -_create_request_type_support(const void * untyped_members, const char * typesupport_identifier); - -rmw_fastrtps_shared_cpp::TypeSupport * -_create_response_type_support(const void * untyped_members, const char * typesupport_identifier); - void _register_type( eprosima::fastrtps::Participant * participant, From 20113d150d7b53f7b88903171517a7e7c0f59ed7 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Tue, 25 Feb 2020 17:50:15 +0100 Subject: [PATCH 17/20] Fixing assert code. Signed-off-by: Miguel Company --- rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp index 75e02e847..dd9ffd3bc 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp @@ -47,9 +47,9 @@ void return_type_support( TypeSupportRegistry::~TypeSupportRegistry() { - assert(message_types_.empty()); - assert(request_types_.empty()); - assert(response_types_.empty()); + assert(message_types_().empty()); + assert(request_types_().empty()); + assert(response_types_().empty()); } type_support_ptr TypeSupportRegistry::get_message_type_support( From 6c79fca2095e87e9239542768e7746f2187cfca0 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Wed, 26 Feb 2020 11:51:33 +0100 Subject: [PATCH 18/20] Removing unused argument. Signed-off-by: Miguel Company --- rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp | 4 ++-- rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp | 4 ++-- rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp | 4 ++-- rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp | 4 ++-- rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp | 4 ++-- rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp | 3 +-- rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp | 2 +- 7 files changed, 12 insertions(+), 13 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp index 0ff5b303f..e797f24ce 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp @@ -104,7 +104,7 @@ rmw_create_client( info->request_publisher_matched_count_ = 0; info->response_subscriber_matched_count_ = 0; - TypeSupportRegistry & type_registry = get_type_support_registry(node); + TypeSupportRegistry & type_registry = get_type_support_registry(); auto request_type_impl = type_registry.get_request_type_support(type_support); if (!request_type_impl) { delete info; @@ -301,7 +301,7 @@ rmw_destroy_client(rmw_node_t * node, rmw_client_t * client) auto ros_type_support = static_cast( impl->ros_type_support()); - TypeSupportRegistry & type_registry = get_type_support_registry(node); + TypeSupportRegistry & type_registry = get_type_support_registry(); type_registry.return_request_type_support(ros_type_support); impl = static_cast(const_cast(info->response_type_support_impl_)); diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp index bb1a98fb1..1ae7df190 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp @@ -136,7 +136,7 @@ rmw_create_publisher( return nullptr; } - TypeSupportRegistry & type_registry = get_type_support_registry(node); + TypeSupportRegistry & type_registry = get_type_support_registry(); auto type_impl = type_registry.get_message_type_support(type_support); if (!type_impl) { delete info; @@ -315,7 +315,7 @@ rmw_destroy_publisher(rmw_node_t * node, rmw_publisher_t * publisher) auto ros_type_support = static_cast( impl->ros_type_support()); - TypeSupportRegistry & type_registry = get_type_support_registry(node); + TypeSupportRegistry & type_registry = get_type_support_registry(); type_registry.return_message_type_support(ros_type_support); return rmw_fastrtps_shared_cpp::__rmw_destroy_publisher( diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp index feee65833..b64208972 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp @@ -40,7 +40,7 @@ rmw_serialize( } } - TypeSupportRegistry & type_registry = get_type_support_registry(nullptr); + TypeSupportRegistry & type_registry = get_type_support_registry(); auto tss = type_registry.get_message_type_support(ts); auto data_length = tss->getEstimatedSerializedSize(ros_message, ts->data); if (serialized_message->buffer_capacity < data_length) { @@ -81,7 +81,7 @@ rmw_deserialize( } } - TypeSupportRegistry & type_registry = get_type_support_registry(nullptr); + TypeSupportRegistry & type_registry = get_type_support_registry(); auto tss = type_registry.get_message_type_support(ts); eprosima::fastcdr::FastBuffer buffer( reinterpret_cast(serialized_message->buffer), serialized_message->buffer_length); diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp index 557e00d28..29d3c0d2f 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp @@ -113,7 +113,7 @@ rmw_create_service( info->participant_ = participant; info->typesupport_identifier_ = type_support->typesupport_identifier; - TypeSupportRegistry & type_registry = get_type_support_registry(node); + TypeSupportRegistry & type_registry = get_type_support_registry(); auto request_type_impl = type_registry.get_request_type_support(type_support); if (!request_type_impl) { delete info; @@ -294,7 +294,7 @@ rmw_destroy_service(rmw_node_t * node, rmw_service_t * service) auto ros_type_support = static_cast( impl->ros_type_support()); - TypeSupportRegistry & type_registry = get_type_support_registry(node); + TypeSupportRegistry & type_registry = get_type_support_registry(); type_registry.return_request_type_support(ros_type_support); impl = static_cast(const_cast(info->response_type_support_impl_)); diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp index 5a1ecadba..2eff126f5 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp @@ -138,7 +138,7 @@ rmw_create_subscription( return nullptr; } - TypeSupportRegistry & type_registry = get_type_support_registry(node); + TypeSupportRegistry & type_registry = get_type_support_registry(); auto type_impl = type_registry.get_message_type_support(type_support); if (!type_impl) { delete info; @@ -261,7 +261,7 @@ rmw_destroy_subscription(rmw_node_t * node, rmw_subscription_t * subscription) auto ros_type_support = static_cast( impl->ros_type_support()); - TypeSupportRegistry & type_registry = get_type_support_registry(node); + TypeSupportRegistry & type_registry = get_type_support_registry(); type_registry.return_message_type_support(ros_type_support); return rmw_fastrtps_shared_cpp::__rmw_destroy_subscription( diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp index dd9ffd3bc..9af508899 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp @@ -135,8 +135,7 @@ void TypeSupportRegistry::return_response_type_support( static TypeSupportRegistry type_registry_instance; -TypeSupportRegistry & get_type_support_registry(const rmw_node_t * node) +TypeSupportRegistry & get_type_support_registry() { - (void)node; return type_registry_instance; } diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp index d4e1eac39..b87c6b82d 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp @@ -67,6 +67,6 @@ class TypeSupportRegistry const rosidl_service_type_support_t * ros_type_support); }; -TypeSupportRegistry & get_type_support_registry(const rmw_node_t * node); +TypeSupportRegistry & get_type_support_registry(); #endif // TYPE_SUPPORT_REGISTRY_HPP_ From 7a9e9dcaf0584b860cf49dd4481fbfeaa7f1752a Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Wed, 26 Feb 2020 11:48:08 +0100 Subject: [PATCH 19/20] Cleanup instead of asserting on destructor. Signed-off-by: Miguel Company --- .../src/type_support_registry.cpp | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp index 9af508899..8338dc07d 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "rcutils/logging_macros.h" + #include "rmw/error_handling.h" #include "type_support_common.hpp" @@ -41,15 +43,31 @@ void return_type_support( auto it = map().find(ros_type_support); assert(it != map().end()); if (0 == --it->second.ref_count) { + delete it->second.type_support; map().erase(it); } } +template +void cleanup(map_type & map, const char * msg) +{ + std::lock_guard guard(map.getMutex()); + if (!map().empty()) { + RCUTILS_LOG_DEBUG_NAMED( + "rmw_fastrtps_dynamic_cpp", + "TypeSupportRegistry %s is not empty. Cleaning it up...", msg); + for (auto it : map() ) { + delete it.second.type_support; + } + map().clear(); + } +} + TypeSupportRegistry::~TypeSupportRegistry() { - assert(message_types_().empty()); - assert(request_types_().empty()); - assert(response_types_().empty()); + cleanup(message_types_, "message_types_"); + cleanup(request_types_, "request_types_"); + cleanup(response_types_, "response_types_"); } type_support_ptr TypeSupportRegistry::get_message_type_support( From 4d456879d2fbbd7e7f38ab42291ff8d733ef7f60 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Thu, 27 Feb 2020 07:21:25 +0100 Subject: [PATCH 20/20] Improving singleton behavior Signed-off-by: Miguel Company --- rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp | 4 ++-- rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp | 4 ++-- rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp | 4 ++-- rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp | 4 ++-- rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp | 4 ++-- .../src/type_support_registry.cpp | 13 ++++++------- .../src/type_support_registry.hpp | 6 ++++-- 7 files changed, 20 insertions(+), 19 deletions(-) diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp index e797f24ce..44823c8a8 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp @@ -104,7 +104,7 @@ rmw_create_client( info->request_publisher_matched_count_ = 0; info->response_subscriber_matched_count_ = 0; - TypeSupportRegistry & type_registry = get_type_support_registry(); + TypeSupportRegistry & type_registry = TypeSupportRegistry::get_instance(); auto request_type_impl = type_registry.get_request_type_support(type_support); if (!request_type_impl) { delete info; @@ -301,7 +301,7 @@ rmw_destroy_client(rmw_node_t * node, rmw_client_t * client) auto ros_type_support = static_cast( impl->ros_type_support()); - TypeSupportRegistry & type_registry = get_type_support_registry(); + TypeSupportRegistry & type_registry = TypeSupportRegistry::get_instance(); type_registry.return_request_type_support(ros_type_support); impl = static_cast(const_cast(info->response_type_support_impl_)); diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp index 1ae7df190..206414381 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_publisher.cpp @@ -136,7 +136,7 @@ rmw_create_publisher( return nullptr; } - TypeSupportRegistry & type_registry = get_type_support_registry(); + TypeSupportRegistry & type_registry = TypeSupportRegistry::get_instance(); auto type_impl = type_registry.get_message_type_support(type_support); if (!type_impl) { delete info; @@ -315,7 +315,7 @@ rmw_destroy_publisher(rmw_node_t * node, rmw_publisher_t * publisher) auto ros_type_support = static_cast( impl->ros_type_support()); - TypeSupportRegistry & type_registry = get_type_support_registry(); + TypeSupportRegistry & type_registry = TypeSupportRegistry::get_instance(); type_registry.return_message_type_support(ros_type_support); return rmw_fastrtps_shared_cpp::__rmw_destroy_publisher( diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp index b64208972..b1124edcf 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_serialize.cpp @@ -40,7 +40,7 @@ rmw_serialize( } } - TypeSupportRegistry & type_registry = get_type_support_registry(); + TypeSupportRegistry & type_registry = TypeSupportRegistry::get_instance(); auto tss = type_registry.get_message_type_support(ts); auto data_length = tss->getEstimatedSerializedSize(ros_message, ts->data); if (serialized_message->buffer_capacity < data_length) { @@ -81,7 +81,7 @@ rmw_deserialize( } } - TypeSupportRegistry & type_registry = get_type_support_registry(); + TypeSupportRegistry & type_registry = TypeSupportRegistry::get_instance(); auto tss = type_registry.get_message_type_support(ts); eprosima::fastcdr::FastBuffer buffer( reinterpret_cast(serialized_message->buffer), serialized_message->buffer_length); diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp index 29d3c0d2f..12975bf4c 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp @@ -113,7 +113,7 @@ rmw_create_service( info->participant_ = participant; info->typesupport_identifier_ = type_support->typesupport_identifier; - TypeSupportRegistry & type_registry = get_type_support_registry(); + TypeSupportRegistry & type_registry = TypeSupportRegistry::get_instance(); auto request_type_impl = type_registry.get_request_type_support(type_support); if (!request_type_impl) { delete info; @@ -294,7 +294,7 @@ rmw_destroy_service(rmw_node_t * node, rmw_service_t * service) auto ros_type_support = static_cast( impl->ros_type_support()); - TypeSupportRegistry & type_registry = get_type_support_registry(); + TypeSupportRegistry & type_registry = TypeSupportRegistry::get_instance(); type_registry.return_request_type_support(ros_type_support); impl = static_cast(const_cast(info->response_type_support_impl_)); diff --git a/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp b/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp index 2eff126f5..594f0c017 100644 --- a/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/rmw_subscription.cpp @@ -138,7 +138,7 @@ rmw_create_subscription( return nullptr; } - TypeSupportRegistry & type_registry = get_type_support_registry(); + TypeSupportRegistry & type_registry = TypeSupportRegistry::get_instance(); auto type_impl = type_registry.get_message_type_support(type_support); if (!type_impl) { delete info; @@ -261,7 +261,7 @@ rmw_destroy_subscription(rmw_node_t * node, rmw_subscription_t * subscription) auto ros_type_support = static_cast( impl->ros_type_support()); - TypeSupportRegistry & type_registry = get_type_support_registry(); + TypeSupportRegistry & type_registry = TypeSupportRegistry::get_instance(); type_registry.return_message_type_support(ros_type_support); return rmw_fastrtps_shared_cpp::__rmw_destroy_subscription( diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp index 8338dc07d..d7f635b84 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.cpp @@ -70,6 +70,12 @@ TypeSupportRegistry::~TypeSupportRegistry() cleanup(response_types_, "response_types_"); } +TypeSupportRegistry & TypeSupportRegistry::get_instance() +{ + static TypeSupportRegistry type_registry_instance; + return type_registry_instance; +} + type_support_ptr TypeSupportRegistry::get_message_type_support( const rosidl_message_type_support_t * ros_type_support) { @@ -150,10 +156,3 @@ void TypeSupportRegistry::return_response_type_support( { return_type_support(ros_type_support, response_types_); } - -static TypeSupportRegistry type_registry_instance; - -TypeSupportRegistry & get_type_support_registry() -{ - return type_registry_instance; -} diff --git a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp index b87c6b82d..a3c66520a 100644 --- a/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp +++ b/rmw_fastrtps_dynamic_cpp/src/type_support_registry.hpp @@ -45,9 +45,13 @@ class TypeSupportRegistry LockedObject request_types_; LockedObject response_types_; + TypeSupportRegistry() = default; + public: ~TypeSupportRegistry(); + static TypeSupportRegistry & get_instance(); + type_support_ptr get_message_type_support( const rosidl_message_type_support_t * ros_type_support); @@ -67,6 +71,4 @@ class TypeSupportRegistry const rosidl_service_type_support_t * ros_type_support); }; -TypeSupportRegistry & get_type_support_registry(); - #endif // TYPE_SUPPORT_REGISTRY_HPP_