From af7f8cfbc77eeecc9921efaba8f0289105f38b9c Mon Sep 17 00:00:00 2001 From: Chen Lihui Date: Mon, 12 Jun 2023 10:27:15 +0800 Subject: [PATCH 1/4] make type support helper supported for service and action as well Signed-off-by: Chen Lihui --- rclcpp/include/rclcpp/typesupport_helpers.hpp | 78 ++++++++++++++++- rclcpp/src/rclcpp/typesupport_helpers.cpp | 76 +++++++++++++++-- .../test/rclcpp/test_typesupport_helpers.cpp | 85 +++++++++++++++++++ 3 files changed, 227 insertions(+), 12 deletions(-) diff --git a/rclcpp/include/rclcpp/typesupport_helpers.hpp b/rclcpp/include/rclcpp/typesupport_helpers.hpp index 2fad84cf3b..4a3e59dad6 100644 --- a/rclcpp/include/rclcpp/typesupport_helpers.hpp +++ b/rclcpp/include/rclcpp/typesupport_helpers.hpp @@ -21,7 +21,9 @@ #include #include "rcpputils/shared_library.hpp" +#include "rosidl_runtime_cpp/action_type_support_decl.hpp" #include "rosidl_runtime_cpp/message_type_support_decl.hpp" +#include "rosidl_runtime_cpp/service_type_support_decl.hpp" #include "rclcpp/visibility_control.hpp" @@ -37,20 +39,88 @@ RCLCPP_PUBLIC std::shared_ptr get_typesupport_library(const std::string & type, const std::string & typesupport_identifier); +namespace internal +{ + +struct typesupport_message_tag {}; +struct typesupport_service_tag {}; +struct typesupport_action_tag {}; + +template +struct typesupport_traits +{ + using type_tag = T; +}; + +template<> +struct typesupport_traits +{ + using type_tag = typesupport_message_tag; +}; + +template<> +struct typesupport_traits +{ + using type_tag = typesupport_service_tag; +}; + +template<> +struct typesupport_traits +{ + using type_tag = typesupport_action_tag; +}; + + +RCLCPP_PUBLIC +const rosidl_message_type_support_t * _get_typesupport_handle( + const std::string & type, + const std::string & typesupport_identifier, + rcpputils::SharedLibrary & library, + typesupport_message_tag +); + +RCLCPP_PUBLIC +const rosidl_service_type_support_t * _get_typesupport_handle( + const std::string & type, + const std::string & typesupport_identifier, + rcpputils::SharedLibrary & library, + typesupport_service_tag +); + +RCLCPP_PUBLIC +const rosidl_action_type_support_t * _get_typesupport_handle( + const std::string & type, + const std::string & typesupport_identifier, + rcpputils::SharedLibrary & library, + typesupport_action_tag +); + +} // namespace internal + /// Extract the type support handle from the library. /** - * The library needs to match the topic type. The shared library must stay loaded for the lifetime of the result. + * The library needs to match the type of topic, service or action. + * The shared library must stay loaded for the lifetime of the result. + * + * The template parameter can be `rosidl_message_type_support_t`, `rosidl_service_type_support_t` + * and `rosidl_action_type_support_t`. + * * \param[in] type The topic type, e.g. "std_msgs/msg/String" * \param[in] typesupport_identifier Type support identifier, typically "rosidl_typesupport_cpp" * \param[in] library The shared type support library * \return A type support handle + * \throws std::runtime_error if library could not be found. */ -RCLCPP_PUBLIC -const rosidl_message_type_support_t * +template +const T * get_typesupport_handle( const std::string & type, const std::string & typesupport_identifier, - rcpputils::SharedLibrary & library); + rcpputils::SharedLibrary & library) +{ + return internal::_get_typesupport_handle( + type, typesupport_identifier, library, typename internal::typesupport_traits::type_tag()); +} } // namespace rclcpp diff --git a/rclcpp/src/rclcpp/typesupport_helpers.cpp b/rclcpp/src/rclcpp/typesupport_helpers.cpp index 7286c35baa..5dd9528b26 100644 --- a/rclcpp/src/rclcpp/typesupport_helpers.cpp +++ b/rclcpp/src/rclcpp/typesupport_helpers.cpp @@ -101,10 +101,15 @@ get_typesupport_library(const std::string & type, const std::string & typesuppor return std::make_shared(library_path); } -const rosidl_message_type_support_t * -get_typesupport_handle( +namespace internal +{ + +static const void * _get_typesupport_handle_impl( const std::string & type, const std::string & typesupport_identifier, + const std::string & typesupport_name, + const std::string & symbol_part_name, + const std::string & middle_module_additional, rcpputils::SharedLibrary & library) { std::string package_name; @@ -112,19 +117,23 @@ get_typesupport_handle( std::string type_name; std::tie(package_name, middle_module, type_name) = extract_type_identifier(type); - auto mk_error = [&package_name, &type_name](auto reason) { + if (middle_module.empty()) { + middle_module = middle_module_additional; + } + + auto mk_error = [&package_name, &type_name, &typesupport_name](auto reason) { std::stringstream rcutils_dynamic_loading_error; rcutils_dynamic_loading_error << - "Something went wrong loading the typesupport library for message type " << package_name << + "Something went wrong loading the typesupport library for " << + typesupport_name << " type " << package_name << "/" << type_name << ". " << reason; return rcutils_dynamic_loading_error.str(); }; try { - std::string symbol_name = typesupport_identifier + "__get_message_type_support_handle__" + - package_name + "__" + (middle_module.empty() ? "msg" : middle_module) + "__" + type_name; - - const rosidl_message_type_support_t * (* get_ts)() = nullptr; + std::string symbol_name = typesupport_identifier + symbol_part_name + + package_name + "__" + middle_module + "__" + type_name; + const void * (* get_ts)() = nullptr; // This will throw runtime_error if the symbol was not found. get_ts = reinterpret_cast(library.get_symbol(symbol_name)); return get_ts(); @@ -133,4 +142,55 @@ get_typesupport_handle( } } +const rosidl_message_type_support_t * _get_typesupport_handle( + const std::string & type, + const std::string & typesupport_identifier, + rcpputils::SharedLibrary & library, + typesupport_message_tag) +{ + static const std::string typesupport_name = "message"; + static const std::string symbol_part_name = "__get_message_type_support_handle__"; + static const std::string middle_module_additional = "msg"; + + return static_cast(_get_typesupport_handle_impl( + type, typesupport_identifier, typesupport_name, symbol_part_name, + middle_module_additional, library + )); +} + +const rosidl_service_type_support_t * _get_typesupport_handle( + const std::string & type, + const std::string & typesupport_identifier, + rcpputils::SharedLibrary & library, + typesupport_service_tag) +{ + static const std::string typesupport_name = "service"; + static const std::string symbol_part_name = "__get_service_type_support_handle__"; + static const std::string middle_module_additional = "srv"; + + return static_cast(_get_typesupport_handle_impl( + type, typesupport_identifier, typesupport_name, symbol_part_name, + middle_module_additional, library + )); +} + +const rosidl_action_type_support_t * _get_typesupport_handle( + const std::string & type, + const std::string & typesupport_identifier, + rcpputils::SharedLibrary & library, + typesupport_action_tag) +{ + static const std::string typesupport_name = "action"; + static const std::string symbol_part_name = "__get_action_type_support_handle__"; + static const std::string middle_module_additional = "action"; + + return static_cast(_get_typesupport_handle_impl( + type, typesupport_identifier, typesupport_name, symbol_part_name, + middle_module_additional, library + )); +} + +} // namespace internal + + } // namespace rclcpp diff --git a/rclcpp/test/rclcpp/test_typesupport_helpers.cpp b/rclcpp/test/rclcpp/test_typesupport_helpers.cpp index 8cdcfc19c0..77f771c40d 100644 --- a/rclcpp/test/rclcpp/test_typesupport_helpers.cpp +++ b/rclcpp/test/rclcpp/test_typesupport_helpers.cpp @@ -75,3 +75,88 @@ TEST(TypesupportHelpersTest, returns_c_type_info_for_valid_library) { FAIL() << e.what(); } } + +TEST(TypesupportHelpersTest, returns_service_type_info_for_valid_legacy_library) { + try { + auto library = rclcpp::get_typesupport_library( + "test_msgs/Empty", "rosidl_typesupport_cpp"); + auto empty_typesupport = rclcpp::get_typesupport_handle( + "test_msgs/Empty", "rosidl_typesupport_cpp", *library); + + EXPECT_THAT( + std::string(empty_typesupport->typesupport_identifier), + ContainsRegex("rosidl_typesupport")); + } catch (const std::runtime_error & e) { + FAIL() << e.what(); + } +} + +TEST(TypesupportHelpersTest, returns_service_type_info_for_valid_library) { + try { + auto library = rclcpp::get_typesupport_library( + "test_msgs/srv/Empty", "rosidl_typesupport_cpp"); + auto empty_typesupport = rclcpp::get_typesupport_handle( + "test_msgs/srv/Empty", "rosidl_typesupport_cpp", *library); + + EXPECT_THAT( + std::string(empty_typesupport->typesupport_identifier), + ContainsRegex("rosidl_typesupport")); + } catch (const std::runtime_error & e) { + FAIL() << e.what(); + } +} + +TEST(TypesupportHelpersTest, returns_action_type_info_for_valid_legacy_library) { + try { + auto library = rclcpp::get_typesupport_library( + "test_msgs/Fibonacci", "rosidl_typesupport_cpp"); + auto fibonacci_typesupport = rclcpp::get_typesupport_handle( + "test_msgs/Fibonacci", "rosidl_typesupport_cpp", *library); + + EXPECT_NE(nullptr, fibonacci_typesupport); + } catch (const std::runtime_error & e) { + FAIL() << e.what(); + } +} + +TEST(TypesupportHelpersTest, returns_action_type_info_for_valid_library) { + try { + auto library = rclcpp::get_typesupport_library( + "test_msgs/action/Fibonacci", "rosidl_typesupport_cpp"); + auto fibonacci_typesupport = rclcpp::get_typesupport_handle( + "test_msgs/action/Fibonacci", "rosidl_typesupport_cpp", *library); + + EXPECT_NE(nullptr, fibonacci_typesupport); + } catch (const std::runtime_error & e) { + FAIL() << e.what(); + } +} + +TEST(TypesupportHelpersTest, test_throw_exception_with_invalid_type) { + // message + std::string invalid_type = "test_msgs/msg/InvalidType"; + auto library = rclcpp::get_typesupport_library(invalid_type, "rosidl_typesupport_cpp"); + EXPECT_THROW( + rclcpp::get_typesupport_handle(invalid_type, "rosidl_typesupport_cpp", *library), + std::runtime_error); + EXPECT_THROW( + rclcpp::get_typesupport_handle( + invalid_type, "rosidl_typesupport_cpp", *library), + std::runtime_error); + + // service + invalid_type = "test_msgs/srv/InvalidType"; + library = rclcpp::get_typesupport_library(invalid_type, "rosidl_typesupport_cpp"); + EXPECT_THROW( + rclcpp::get_typesupport_handle( + invalid_type, "rosidl_typesupport_cpp", *library), + std::runtime_error); + + // action + invalid_type = "test_msgs/action/InvalidType"; + library = rclcpp::get_typesupport_library(invalid_type, "rosidl_typesupport_cpp"); + EXPECT_THROW( + rclcpp::get_typesupport_handle( + invalid_type, "rosidl_typesupport_cpp", *library), + std::runtime_error); +} From 733a6d27cbc6968f14617a60d407d974b39ba3c7 Mon Sep 17 00:00:00 2001 From: Chen Lihui Date: Thu, 16 Nov 2023 08:48:51 +0800 Subject: [PATCH 2/4] not to use template and only add the necessary service type currently Signed-off-by: Chen Lihui --- rclcpp/include/rclcpp/typesupport_helpers.hpp | 92 +++++-------------- rclcpp/src/rclcpp/typesupport_helpers.cpp | 58 ++++-------- .../test/rclcpp/test_typesupport_helpers.cpp | 42 +-------- 3 files changed, 42 insertions(+), 150 deletions(-) diff --git a/rclcpp/include/rclcpp/typesupport_helpers.hpp b/rclcpp/include/rclcpp/typesupport_helpers.hpp index 4a3e59dad6..dadf026ea8 100644 --- a/rclcpp/include/rclcpp/typesupport_helpers.hpp +++ b/rclcpp/include/rclcpp/typesupport_helpers.hpp @@ -21,7 +21,6 @@ #include #include "rcpputils/shared_library.hpp" -#include "rosidl_runtime_cpp/action_type_support_decl.hpp" #include "rosidl_runtime_cpp/message_type_support_decl.hpp" #include "rosidl_runtime_cpp/service_type_support_decl.hpp" @@ -39,88 +38,39 @@ RCLCPP_PUBLIC std::shared_ptr get_typesupport_library(const std::string & type, const std::string & typesupport_identifier); -namespace internal -{ - -struct typesupport_message_tag {}; -struct typesupport_service_tag {}; -struct typesupport_action_tag {}; - -template -struct typesupport_traits -{ - using type_tag = T; -}; - -template<> -struct typesupport_traits -{ - using type_tag = typesupport_message_tag; -}; - -template<> -struct typesupport_traits -{ - using type_tag = typesupport_service_tag; -}; - -template<> -struct typesupport_traits -{ - using type_tag = typesupport_action_tag; -}; - - -RCLCPP_PUBLIC -const rosidl_message_type_support_t * _get_typesupport_handle( - const std::string & type, - const std::string & typesupport_identifier, - rcpputils::SharedLibrary & library, - typesupport_message_tag -); - -RCLCPP_PUBLIC -const rosidl_service_type_support_t * _get_typesupport_handle( - const std::string & type, - const std::string & typesupport_identifier, - rcpputils::SharedLibrary & library, - typesupport_service_tag -); - +/// Extract message the type support handle from the library. +/** + * The library needs to match the topic type. The shared library must stay loaded for the lifetime of the result. + * + * \param[in] type The topic type, e.g. "std_msgs/msg/String" + * \param[in] typesupport_identifier Type support identifier, typically "rosidl_typesupport_cpp" + * \param[in] library The shared type support library + * \throws std::runtime_error if library could not be found. + * \return A message type support handle + */ RCLCPP_PUBLIC -const rosidl_action_type_support_t * _get_typesupport_handle( +const rosidl_message_type_support_t * +get_typesupport_handle( const std::string & type, const std::string & typesupport_identifier, - rcpputils::SharedLibrary & library, - typesupport_action_tag -); + rcpputils::SharedLibrary & library); -} // namespace internal - -/// Extract the type support handle from the library. +/// Extract the service type support handle from the library. /** - * The library needs to match the type of topic, service or action. - * The shared library must stay loaded for the lifetime of the result. - * - * The template parameter can be `rosidl_message_type_support_t`, `rosidl_service_type_support_t` - * and `rosidl_action_type_support_t`. + * The library needs to match the topic type. The shared library must stay loaded for the lifetime of the result. * - * \param[in] type The topic type, e.g. "std_msgs/msg/String" + * \param[in] type The service type, e.g. "std_msgs/srv/String" * \param[in] typesupport_identifier Type support identifier, typically "rosidl_typesupport_cpp" * \param[in] library The shared type support library - * \return A type support handle * \throws std::runtime_error if library could not be found. + * \return A service type support handle */ -template -const T * -get_typesupport_handle( +RCLCPP_PUBLIC +const rosidl_service_type_support_t * +get_service_typesupport_handle( const std::string & type, const std::string & typesupport_identifier, - rcpputils::SharedLibrary & library) -{ - return internal::_get_typesupport_handle( - type, typesupport_identifier, library, typename internal::typesupport_traits::type_tag()); -} + rcpputils::SharedLibrary & library); } // namespace rclcpp diff --git a/rclcpp/src/rclcpp/typesupport_helpers.cpp b/rclcpp/src/rclcpp/typesupport_helpers.cpp index 5dd9528b26..6bd0d90ecc 100644 --- a/rclcpp/src/rclcpp/typesupport_helpers.cpp +++ b/rclcpp/src/rclcpp/typesupport_helpers.cpp @@ -91,20 +91,7 @@ extract_type_identifier(const std::string & full_type) return std::make_tuple(package_name, middle_module, type_name); } -} // anonymous namespace - -std::shared_ptr -get_typesupport_library(const std::string & type, const std::string & typesupport_identifier) -{ - auto package_name = std::get<0>(extract_type_identifier(type)); - auto library_path = get_typesupport_library_path(package_name, typesupport_identifier); - return std::make_shared(library_path); -} - -namespace internal -{ - -static const void * _get_typesupport_handle_impl( +const void * get_typesupport_handle_impl( const std::string & type, const std::string & typesupport_identifier, const std::string & typesupport_name, @@ -142,55 +129,44 @@ static const void * _get_typesupport_handle_impl( } } -const rosidl_message_type_support_t * _get_typesupport_handle( +} // anonymous namespace + +std::shared_ptr +get_typesupport_library(const std::string & type, const std::string & typesupport_identifier) +{ + auto package_name = std::get<0>(extract_type_identifier(type)); + auto library_path = get_typesupport_library_path(package_name, typesupport_identifier); + return std::make_shared(library_path); +} + +const rosidl_message_type_support_t * get_typesupport_handle( const std::string & type, const std::string & typesupport_identifier, - rcpputils::SharedLibrary & library, - typesupport_message_tag) + rcpputils::SharedLibrary & library) { static const std::string typesupport_name = "message"; static const std::string symbol_part_name = "__get_message_type_support_handle__"; static const std::string middle_module_additional = "msg"; - return static_cast(_get_typesupport_handle_impl( + return static_cast(get_typesupport_handle_impl( type, typesupport_identifier, typesupport_name, symbol_part_name, middle_module_additional, library )); } -const rosidl_service_type_support_t * _get_typesupport_handle( +const rosidl_service_type_support_t * get_service_typesupport_handle( const std::string & type, const std::string & typesupport_identifier, - rcpputils::SharedLibrary & library, - typesupport_service_tag) + rcpputils::SharedLibrary & library) { static const std::string typesupport_name = "service"; static const std::string symbol_part_name = "__get_service_type_support_handle__"; static const std::string middle_module_additional = "srv"; - return static_cast(_get_typesupport_handle_impl( + return static_cast(get_typesupport_handle_impl( type, typesupport_identifier, typesupport_name, symbol_part_name, middle_module_additional, library )); } -const rosidl_action_type_support_t * _get_typesupport_handle( - const std::string & type, - const std::string & typesupport_identifier, - rcpputils::SharedLibrary & library, - typesupport_action_tag) -{ - static const std::string typesupport_name = "action"; - static const std::string symbol_part_name = "__get_action_type_support_handle__"; - static const std::string middle_module_additional = "action"; - - return static_cast(_get_typesupport_handle_impl( - type, typesupport_identifier, typesupport_name, symbol_part_name, - middle_module_additional, library - )); -} - -} // namespace internal - - } // namespace rclcpp diff --git a/rclcpp/test/rclcpp/test_typesupport_helpers.cpp b/rclcpp/test/rclcpp/test_typesupport_helpers.cpp index 77f771c40d..ab227b09c8 100644 --- a/rclcpp/test/rclcpp/test_typesupport_helpers.cpp +++ b/rclcpp/test/rclcpp/test_typesupport_helpers.cpp @@ -80,7 +80,7 @@ TEST(TypesupportHelpersTest, returns_service_type_info_for_valid_legacy_library) try { auto library = rclcpp::get_typesupport_library( "test_msgs/Empty", "rosidl_typesupport_cpp"); - auto empty_typesupport = rclcpp::get_typesupport_handle( + auto empty_typesupport = rclcpp::get_service_typesupport_handle( "test_msgs/Empty", "rosidl_typesupport_cpp", *library); EXPECT_THAT( @@ -95,7 +95,7 @@ TEST(TypesupportHelpersTest, returns_service_type_info_for_valid_library) { try { auto library = rclcpp::get_typesupport_library( "test_msgs/srv/Empty", "rosidl_typesupport_cpp"); - auto empty_typesupport = rclcpp::get_typesupport_handle( + auto empty_typesupport = rclcpp::get_service_typesupport_handle( "test_msgs/srv/Empty", "rosidl_typesupport_cpp", *library); EXPECT_THAT( @@ -106,32 +106,6 @@ TEST(TypesupportHelpersTest, returns_service_type_info_for_valid_library) { } } -TEST(TypesupportHelpersTest, returns_action_type_info_for_valid_legacy_library) { - try { - auto library = rclcpp::get_typesupport_library( - "test_msgs/Fibonacci", "rosidl_typesupport_cpp"); - auto fibonacci_typesupport = rclcpp::get_typesupport_handle( - "test_msgs/Fibonacci", "rosidl_typesupport_cpp", *library); - - EXPECT_NE(nullptr, fibonacci_typesupport); - } catch (const std::runtime_error & e) { - FAIL() << e.what(); - } -} - -TEST(TypesupportHelpersTest, returns_action_type_info_for_valid_library) { - try { - auto library = rclcpp::get_typesupport_library( - "test_msgs/action/Fibonacci", "rosidl_typesupport_cpp"); - auto fibonacci_typesupport = rclcpp::get_typesupport_handle( - "test_msgs/action/Fibonacci", "rosidl_typesupport_cpp", *library); - - EXPECT_NE(nullptr, fibonacci_typesupport); - } catch (const std::runtime_error & e) { - FAIL() << e.what(); - } -} - TEST(TypesupportHelpersTest, test_throw_exception_with_invalid_type) { // message std::string invalid_type = "test_msgs/msg/InvalidType"; @@ -140,7 +114,7 @@ TEST(TypesupportHelpersTest, test_throw_exception_with_invalid_type) { rclcpp::get_typesupport_handle(invalid_type, "rosidl_typesupport_cpp", *library), std::runtime_error); EXPECT_THROW( - rclcpp::get_typesupport_handle( + rclcpp::get_service_typesupport_handle( invalid_type, "rosidl_typesupport_cpp", *library), std::runtime_error); @@ -148,15 +122,7 @@ TEST(TypesupportHelpersTest, test_throw_exception_with_invalid_type) { invalid_type = "test_msgs/srv/InvalidType"; library = rclcpp::get_typesupport_library(invalid_type, "rosidl_typesupport_cpp"); EXPECT_THROW( - rclcpp::get_typesupport_handle( - invalid_type, "rosidl_typesupport_cpp", *library), - std::runtime_error); - - // action - invalid_type = "test_msgs/action/InvalidType"; - library = rclcpp::get_typesupport_library(invalid_type, "rosidl_typesupport_cpp"); - EXPECT_THROW( - rclcpp::get_typesupport_handle( + rclcpp::get_service_typesupport_handle( invalid_type, "rosidl_typesupport_cpp", *library), std::runtime_error); } From 51ef714d8ef3749db439b3c00d2624b6b58e32c3 Mon Sep 17 00:00:00 2001 From: Chen Lihui Date: Thu, 16 Nov 2023 09:01:25 +0800 Subject: [PATCH 3/4] update comment Signed-off-by: Chen Lihui --- rclcpp/include/rclcpp/typesupport_helpers.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rclcpp/include/rclcpp/typesupport_helpers.hpp b/rclcpp/include/rclcpp/typesupport_helpers.hpp index dadf026ea8..4810612321 100644 --- a/rclcpp/include/rclcpp/typesupport_helpers.hpp +++ b/rclcpp/include/rclcpp/typesupport_helpers.hpp @@ -38,14 +38,14 @@ RCLCPP_PUBLIC std::shared_ptr get_typesupport_library(const std::string & type, const std::string & typesupport_identifier); -/// Extract message the type support handle from the library. +/// Extract the message type support handle from the library. /** * The library needs to match the topic type. The shared library must stay loaded for the lifetime of the result. * * \param[in] type The topic type, e.g. "std_msgs/msg/String" * \param[in] typesupport_identifier Type support identifier, typically "rosidl_typesupport_cpp" * \param[in] library The shared type support library - * \throws std::runtime_error if library could not be found. + * \throws std::runtime_error if the symbol of type not found in the library. * \return A message type support handle */ RCLCPP_PUBLIC @@ -59,10 +59,10 @@ get_typesupport_handle( /** * The library needs to match the topic type. The shared library must stay loaded for the lifetime of the result. * - * \param[in] type The service type, e.g. "std_msgs/srv/String" + * \param[in] type The service type, e.g. "std_srvs/srv/Empty" * \param[in] typesupport_identifier Type support identifier, typically "rosidl_typesupport_cpp" * \param[in] library The shared type support library - * \throws std::runtime_error if library could not be found. + * \throws std::runtime_error if the symbol of type not found in the library. * \return A service type support handle */ RCLCPP_PUBLIC From ca006150602196dd00561deb3501bad17c46f667 Mon Sep 17 00:00:00 2001 From: Chen Lihui Date: Tue, 5 Dec 2023 09:35:41 +0800 Subject: [PATCH 4/4] add deprecated cycle for `get_typesupport_handle` Signed-off-by: Chen Lihui --- rclcpp/include/rclcpp/generic_publisher.hpp | 2 +- .../include/rclcpp/generic_subscription.hpp | 2 +- rclcpp/include/rclcpp/typesupport_helpers.hpp | 21 ++++++++++++++++++- rclcpp/src/rclcpp/typesupport_helpers.cpp | 8 +++++++ .../test/rclcpp/test_typesupport_helpers.cpp | 12 +++++------ 5 files changed, 35 insertions(+), 10 deletions(-) diff --git a/rclcpp/include/rclcpp/generic_publisher.hpp b/rclcpp/include/rclcpp/generic_publisher.hpp index 7cd2d8bc39..292e6900d3 100644 --- a/rclcpp/include/rclcpp/generic_publisher.hpp +++ b/rclcpp/include/rclcpp/generic_publisher.hpp @@ -77,7 +77,7 @@ class GenericPublisher : public rclcpp::PublisherBase : rclcpp::PublisherBase( node_base, topic_name, - *rclcpp::get_typesupport_handle(topic_type, "rosidl_typesupport_cpp", *ts_lib), + *rclcpp::get_message_typesupport_handle(topic_type, "rosidl_typesupport_cpp", *ts_lib), options.template to_rcl_publisher_options(qos), // NOTE(methylDragon): Passing these args separately is necessary for event binding options.event_callbacks, diff --git a/rclcpp/include/rclcpp/generic_subscription.hpp b/rclcpp/include/rclcpp/generic_subscription.hpp index 975a9d0d0d..dfbae0467b 100644 --- a/rclcpp/include/rclcpp/generic_subscription.hpp +++ b/rclcpp/include/rclcpp/generic_subscription.hpp @@ -79,7 +79,7 @@ class GenericSubscription : public rclcpp::SubscriptionBase const rclcpp::SubscriptionOptionsWithAllocator & options) : SubscriptionBase( node_base, - *rclcpp::get_typesupport_handle(topic_type, "rosidl_typesupport_cpp", *ts_lib), + *rclcpp::get_message_typesupport_handle(topic_type, "rosidl_typesupport_cpp", *ts_lib), topic_name, options.to_rcl_subscription_options(qos), options.event_callbacks, diff --git a/rclcpp/include/rclcpp/typesupport_helpers.hpp b/rclcpp/include/rclcpp/typesupport_helpers.hpp index 4810612321..c93b318440 100644 --- a/rclcpp/include/rclcpp/typesupport_helpers.hpp +++ b/rclcpp/include/rclcpp/typesupport_helpers.hpp @@ -38,6 +38,25 @@ RCLCPP_PUBLIC std::shared_ptr get_typesupport_library(const std::string & type, const std::string & typesupport_identifier); +/// Extract the type support handle from the library. +/** + * The library needs to match the topic type. The shared library must stay loaded for the lifetime of the result. + * + * \deprecated Use get_message_typesupport_handle() instead + * + * \param[in] type The topic type, e.g. "std_msgs/msg/String" + * \param[in] typesupport_identifier Type support identifier, typically "rosidl_typesupport_cpp" + * \param[in] library The shared type support library + * \return A type support handle + */ +[[deprecated("Use `get_message_typesupport_handle` instead")]] +RCLCPP_PUBLIC +const rosidl_message_type_support_t * +get_typesupport_handle( + const std::string & type, + const std::string & typesupport_identifier, + rcpputils::SharedLibrary & library); + /// Extract the message type support handle from the library. /** * The library needs to match the topic type. The shared library must stay loaded for the lifetime of the result. @@ -50,7 +69,7 @@ get_typesupport_library(const std::string & type, const std::string & typesuppor */ RCLCPP_PUBLIC const rosidl_message_type_support_t * -get_typesupport_handle( +get_message_typesupport_handle( const std::string & type, const std::string & typesupport_identifier, rcpputils::SharedLibrary & library); diff --git a/rclcpp/src/rclcpp/typesupport_helpers.cpp b/rclcpp/src/rclcpp/typesupport_helpers.cpp index 6bd0d90ecc..04dd8c4c9f 100644 --- a/rclcpp/src/rclcpp/typesupport_helpers.cpp +++ b/rclcpp/src/rclcpp/typesupport_helpers.cpp @@ -143,6 +143,14 @@ const rosidl_message_type_support_t * get_typesupport_handle( const std::string & type, const std::string & typesupport_identifier, rcpputils::SharedLibrary & library) +{ + return get_message_typesupport_handle(type, typesupport_identifier, library); +} + +const rosidl_message_type_support_t * get_message_typesupport_handle( + const std::string & type, + const std::string & typesupport_identifier, + rcpputils::SharedLibrary & library) { static const std::string typesupport_name = "message"; static const std::string symbol_part_name = "__get_message_type_support_handle__"; diff --git a/rclcpp/test/rclcpp/test_typesupport_helpers.cpp b/rclcpp/test/rclcpp/test_typesupport_helpers.cpp index ab227b09c8..2117b89455 100644 --- a/rclcpp/test/rclcpp/test_typesupport_helpers.cpp +++ b/rclcpp/test/rclcpp/test_typesupport_helpers.cpp @@ -50,7 +50,7 @@ TEST(TypesupportHelpersTest, returns_c_type_info_for_valid_legacy_library) { try { auto library = rclcpp::get_typesupport_library( "test_msgs/BasicTypes", "rosidl_typesupport_cpp"); - auto string_typesupport = rclcpp::get_typesupport_handle( + auto string_typesupport = rclcpp::get_message_typesupport_handle( "test_msgs/BasicTypes", "rosidl_typesupport_cpp", *library); EXPECT_THAT( @@ -65,7 +65,7 @@ TEST(TypesupportHelpersTest, returns_c_type_info_for_valid_library) { try { auto library = rclcpp::get_typesupport_library( "test_msgs/msg/BasicTypes", "rosidl_typesupport_cpp"); - auto string_typesupport = rclcpp::get_typesupport_handle( + auto string_typesupport = rclcpp::get_message_typesupport_handle( "test_msgs/msg/BasicTypes", "rosidl_typesupport_cpp", *library); EXPECT_THAT( @@ -111,18 +111,16 @@ TEST(TypesupportHelpersTest, test_throw_exception_with_invalid_type) { std::string invalid_type = "test_msgs/msg/InvalidType"; auto library = rclcpp::get_typesupport_library(invalid_type, "rosidl_typesupport_cpp"); EXPECT_THROW( - rclcpp::get_typesupport_handle(invalid_type, "rosidl_typesupport_cpp", *library), + rclcpp::get_message_typesupport_handle(invalid_type, "rosidl_typesupport_cpp", *library), std::runtime_error); EXPECT_THROW( - rclcpp::get_service_typesupport_handle( - invalid_type, "rosidl_typesupport_cpp", *library), + rclcpp::get_service_typesupport_handle(invalid_type, "rosidl_typesupport_cpp", *library), std::runtime_error); // service invalid_type = "test_msgs/srv/InvalidType"; library = rclcpp::get_typesupport_library(invalid_type, "rosidl_typesupport_cpp"); EXPECT_THROW( - rclcpp::get_service_typesupport_handle( - invalid_type, "rosidl_typesupport_cpp", *library), + rclcpp::get_service_typesupport_handle(invalid_type, "rosidl_typesupport_cpp", *library), std::runtime_error); }