Skip to content

Commit 48a4761

Browse files
Support action typesupport helper (#2750)
Signed-off-by: Barry Xu <[email protected]>
1 parent 6c10f94 commit 48a4761

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

rclcpp/include/rclcpp/typesupport_helpers.hpp

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <tuple>
2222

2323
#include "rcpputils/shared_library.hpp"
24+
#include "rosidl_runtime_cpp/action_type_support_decl.hpp"
2425
#include "rosidl_runtime_cpp/message_type_support_decl.hpp"
2526
#include "rosidl_runtime_cpp/service_type_support_decl.hpp"
2627

@@ -72,6 +73,24 @@ get_service_typesupport_handle(
7273
const std::string & typesupport_identifier,
7374
rcpputils::SharedLibrary & library);
7475

76+
/// Extract the action type support handle from the library.
77+
/**
78+
* The library needs to match the action type. The shared library must stay loaded for the lifetime
79+
* of the result.
80+
*
81+
* \param[in] type The action type, e.g. "example_interfaces/action/Fibonacci"
82+
* \param[in] typesupport_identifier Type support identifier, typically "rosidl_typesupport_cpp"
83+
* \param[in] library The shared type support library
84+
* \throws std::runtime_error if the symbol of type not found in the library.
85+
* \return A action type support handle
86+
*/
87+
RCLCPP_PUBLIC
88+
const rosidl_action_type_support_t *
89+
get_action_typesupport_handle(
90+
const std::string & type,
91+
const std::string & typesupport_identifier,
92+
rcpputils::SharedLibrary & library);
93+
7594
} // namespace rclcpp
7695

7796
#endif // RCLCPP__TYPESUPPORT_HELPERS_HPP_

rclcpp/src/rclcpp/typesupport_helpers.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,19 @@ const rosidl_service_type_support_t * get_service_typesupport_handle(
177177
));
178178
}
179179

180+
const rosidl_action_type_support_t * get_action_typesupport_handle(
181+
const std::string & type,
182+
const std::string & typesupport_identifier,
183+
rcpputils::SharedLibrary & library)
184+
{
185+
static const std::string typesupport_name = "action";
186+
static const std::string symbol_part_name = "__get_action_type_support_handle__";
187+
static const std::string middle_module_additional = "action";
188+
189+
return static_cast<const rosidl_action_type_support_t *>(get_typesupport_handle_impl(
190+
type, typesupport_identifier, typesupport_name, symbol_part_name,
191+
middle_module_additional, library
192+
));
193+
}
194+
180195
} // namespace rclcpp

rclcpp/test/rclcpp/test_typesupport_helpers.cpp

+37-3
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,55 @@ TEST(TypesupportHelpersTest, returns_service_type_info_for_valid_library) {
106106
}
107107
}
108108

109+
TEST(TypesupportHelpersTest, returns_action_type_info_for_valid_legacy_library) {
110+
try {
111+
auto library = rclcpp::get_typesupport_library(
112+
"test_msgs/NestedMessage", "rosidl_typesupport_cpp");
113+
auto nestedmessage_typesupport = rclcpp::get_action_typesupport_handle(
114+
"test_msgs/NestedMessage", "rosidl_typesupport_cpp", *library);
115+
116+
EXPECT_THAT(
117+
std::string(nestedmessage_typesupport->goal_service_type_support->typesupport_identifier),
118+
ContainsRegex("rosidl_typesupport"));
119+
} catch (const std::runtime_error & e) {
120+
FAIL() << e.what();
121+
}
122+
}
123+
124+
TEST(TypesupportHelpersTest, returns_action_type_info_for_valid_library) {
125+
try {
126+
auto library = rclcpp::get_typesupport_library(
127+
"test_msgs/action/NestedMessage", "rosidl_typesupport_cpp");
128+
auto nestedmessage_typesupport = rclcpp::get_action_typesupport_handle(
129+
"test_msgs/action/NestedMessage", "rosidl_typesupport_cpp", *library);
130+
131+
EXPECT_THAT(
132+
std::string(nestedmessage_typesupport->goal_service_type_support->typesupport_identifier),
133+
ContainsRegex("rosidl_typesupport"));
134+
} catch (const std::runtime_error & e) {
135+
FAIL() << e.what();
136+
}
137+
}
138+
109139
TEST(TypesupportHelpersTest, test_throw_exception_with_invalid_type) {
110140
// message
111141
std::string invalid_type = "test_msgs/msg/InvalidType";
112142
auto library = rclcpp::get_typesupport_library(invalid_type, "rosidl_typesupport_cpp");
113143
EXPECT_THROW(
114144
rclcpp::get_message_typesupport_handle(invalid_type, "rosidl_typesupport_cpp", *library),
115145
std::runtime_error);
116-
EXPECT_THROW(
117-
rclcpp::get_service_typesupport_handle(invalid_type, "rosidl_typesupport_cpp", *library),
118-
std::runtime_error);
119146

120147
// service
121148
invalid_type = "test_msgs/srv/InvalidType";
122149
library = rclcpp::get_typesupport_library(invalid_type, "rosidl_typesupport_cpp");
123150
EXPECT_THROW(
124151
rclcpp::get_service_typesupport_handle(invalid_type, "rosidl_typesupport_cpp", *library),
125152
std::runtime_error);
153+
154+
// action
155+
invalid_type = "test_msgs/action/InvalidType";
156+
library = rclcpp::get_typesupport_library(invalid_type, "rosidl_typesupport_cpp");
157+
EXPECT_THROW(
158+
rclcpp::get_action_typesupport_handle(invalid_type, "rosidl_typesupport_cpp", *library),
159+
std::runtime_error);
126160
}

0 commit comments

Comments
 (0)