16 #include "rclcpp/typesupport_helpers.hpp"
27 #include "ament_index_cpp/get_package_prefix.hpp"
28 #include "ament_index_cpp/get_resources.hpp"
29 #include "rcpputils/shared_library.hpp"
30 #include "rcpputils/find_library.hpp"
31 #include "rosidl_runtime_cpp/message_type_support_decl.hpp"
39 const void * get_typesupport_handle_impl(
40 const std::string & type,
41 const std::string & typesupport_identifier,
42 const std::string & typesupport_name,
43 const std::string & symbol_part_name,
44 const std::string & middle_module_additional,
45 rcpputils::SharedLibrary & library)
47 std::string package_name;
48 std::string middle_module;
49 std::string type_name;
52 if (middle_module.empty()) {
53 middle_module = middle_module_additional;
56 auto mk_error = [&package_name, &type_name, &typesupport_name](
auto reason) {
57 std::stringstream rcutils_dynamic_loading_error;
58 rcutils_dynamic_loading_error <<
59 "Something went wrong loading the typesupport library for " <<
60 typesupport_name <<
" type " << package_name <<
61 "/" << type_name <<
". " << reason;
62 return rcutils_dynamic_loading_error.str();
66 std::string symbol_name = typesupport_identifier + symbol_part_name +
67 package_name +
"__" + middle_module +
"__" + type_name;
68 const void * (* get_ts)() =
nullptr;
70 get_ts =
reinterpret_cast<decltype(get_ts)
>(library.get_symbol(symbol_name));
72 }
catch (std::runtime_error &) {
73 throw std::runtime_error{mk_error(
"Library could not be found.")};
78 std::string string_trim(std::string_view str_v)
80 auto begin = std::find_if_not(str_v.begin(), str_v.end(), [](
unsigned char ch) {
81 return std::isspace(ch);
83 auto end = std::find_if_not(str_v.rbegin(), str_v.rend(), [](
unsigned char ch) {
84 return std::isspace(ch);
89 return std::string(begin, end);
94 std::tuple<std::string, std::string, std::string>
97 char type_separator =
'/';
98 auto sep_position_back = full_type.find_last_of(type_separator);
99 auto sep_position_front = full_type.find_first_of(type_separator);
100 if (sep_position_back == std::string::npos ||
101 sep_position_front == 0 ||
102 sep_position_back == 0 ||
103 sep_position_back == full_type.length() - 1)
105 throw std::runtime_error(
106 "Message type is not of the form package/type and cannot be processed");
109 std::string package_name = full_type.substr(0, sep_position_front);
110 std::string middle_module =
"";
111 if (sep_position_back - sep_position_front > 0) {
113 full_type.substr(sep_position_front + 1, sep_position_back - sep_position_front - 1);
115 std::string type_name = full_type.substr(sep_position_back + 1);
117 return std::make_tuple(
118 string_trim(package_name), string_trim(middle_module), string_trim(type_name));
122 const std::string & package_name,
const std::string & typesupport_identifier)
124 const char * dynamic_library_folder;
126 dynamic_library_folder =
"/bin/";
128 dynamic_library_folder =
"/lib/";
130 dynamic_library_folder =
"/lib/";
133 std::string package_prefix;
135 package_prefix = ament_index_cpp::get_package_prefix(package_name);
136 }
catch (ament_index_cpp::PackageNotFoundError & e) {
137 throw std::runtime_error(e.what());
140 const std::string library_path = rcpputils::path_for_library(
141 package_prefix + dynamic_library_folder,
142 package_name +
"__" + typesupport_identifier);
143 if (library_path.empty()) {
144 throw std::runtime_error(
145 "Typesupport library for " + package_name +
" does not exist in '" + package_prefix +
151 std::shared_ptr<rcpputils::SharedLibrary>
156 return std::make_shared<rcpputils::SharedLibrary>(library_path);
160 const std::string & type,
161 const std::string & typesupport_identifier,
162 rcpputils::SharedLibrary & library)
164 static const std::string typesupport_name =
"message";
165 static const std::string symbol_part_name =
"__get_message_type_support_handle__";
166 static const std::string middle_module_additional =
"msg";
168 return static_cast<const rosidl_message_type_support_t *
>(get_typesupport_handle_impl(
169 type, typesupport_identifier, typesupport_name, symbol_part_name,
170 middle_module_additional, library
175 const std::string & type,
176 const std::string & typesupport_identifier,
177 rcpputils::SharedLibrary & library)
179 static const std::string typesupport_name =
"service";
180 static const std::string symbol_part_name =
"__get_service_type_support_handle__";
181 static const std::string middle_module_additional =
"srv";
183 return static_cast<const rosidl_service_type_support_t *
>(get_typesupport_handle_impl(
184 type, typesupport_identifier, typesupport_name, symbol_part_name,
185 middle_module_additional, library
190 const std::string & type,
191 const std::string & typesupport_identifier,
192 rcpputils::SharedLibrary & library)
194 static const std::string typesupport_name =
"action";
195 static const std::string symbol_part_name =
"__get_action_type_support_handle__";
196 static const std::string middle_module_additional =
"action";
198 return static_cast<const rosidl_action_type_support_t *
>(get_typesupport_handle_impl(
199 type, typesupport_identifier, typesupport_name, symbol_part_name,
200 middle_module_additional, library
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC std::string get_typesupport_library_path(const std::string &package_name, const std::string &typesupport_identifier)
Look for the library in the ament prefix paths and return the path to the type support library.
RCLCPP_PUBLIC std::shared_ptr< rcpputils::SharedLibrary > get_typesupport_library(const std::string &type, const std::string &typesupport_identifier)
Load the type support library for the given type.
RCLCPP_PUBLIC const rosidl_message_type_support_t * get_message_typesupport_handle(const std::string &type, const std::string &typesupport_identifier, rcpputils::SharedLibrary &library)
Extracts the message type support handle from the library.
RCLCPP_PUBLIC const rosidl_service_type_support_t * get_service_typesupport_handle(const std::string &type, const std::string &typesupport_identifier, rcpputils::SharedLibrary &library)
Extracts the service type support handle from the library.
RCLCPP_PUBLIC const rosidl_action_type_support_t * get_action_typesupport_handle(const std::string &type, const std::string &typesupport_identifier, rcpputils::SharedLibrary &library)
Extracts the action type support handle from the library.
RCLCPP_PUBLIC std::tuple< std::string, std::string, std::string > extract_type_identifier(const std::string &full_type)
Extract the package name, middle module, and type name from a full type string.