16 #include "rclcpp/typesupport_helpers.hpp"
26 #include "ament_index_cpp/get_package_prefix.hpp"
27 #include "ament_index_cpp/get_resources.hpp"
28 #include "rcpputils/shared_library.hpp"
29 #include "rcpputils/find_library.hpp"
30 #include "rosidl_runtime_cpp/message_type_support_decl.hpp"
38 const void * get_typesupport_handle_impl(
39 const std::string & type,
40 const std::string & typesupport_identifier,
41 const std::string & typesupport_name,
42 const std::string & symbol_part_name,
43 const std::string & middle_module_additional,
44 rcpputils::SharedLibrary & library)
46 std::string package_name;
47 std::string middle_module;
48 std::string type_name;
51 if (middle_module.empty()) {
52 middle_module = middle_module_additional;
55 auto mk_error = [&package_name, &type_name, &typesupport_name](
auto reason) {
56 std::stringstream rcutils_dynamic_loading_error;
57 rcutils_dynamic_loading_error <<
58 "Something went wrong loading the typesupport library for " <<
59 typesupport_name <<
" type " << package_name <<
60 "/" << type_name <<
". " << reason;
61 return rcutils_dynamic_loading_error.str();
65 std::string symbol_name = typesupport_identifier + symbol_part_name +
66 package_name +
"__" + middle_module +
"__" + type_name;
67 const void * (* get_ts)() =
nullptr;
69 get_ts =
reinterpret_cast<decltype(get_ts)
>(library.get_symbol(symbol_name));
71 }
catch (std::runtime_error &) {
72 throw std::runtime_error{mk_error(
"Library could not be found.")};
78 std::tuple<std::string, std::string, std::string>
81 char type_separator =
'/';
82 auto sep_position_back = full_type.find_last_of(type_separator);
83 auto sep_position_front = full_type.find_first_of(type_separator);
84 if (sep_position_back == std::string::npos ||
85 sep_position_back == 0 ||
86 sep_position_back == full_type.length() - 1)
88 throw std::runtime_error(
89 "Message type is not of the form package/type and cannot be processed");
92 std::string package_name = full_type.substr(0, sep_position_front);
93 std::string middle_module =
"";
94 if (sep_position_back - sep_position_front > 0) {
96 full_type.substr(sep_position_front + 1, sep_position_back - sep_position_front - 1);
98 std::string type_name = full_type.substr(sep_position_back + 1);
100 return std::make_tuple(package_name, middle_module, type_name);
104 const std::string & package_name,
const std::string & typesupport_identifier)
106 const char * dynamic_library_folder;
108 dynamic_library_folder =
"/bin/";
110 dynamic_library_folder =
"/lib/";
112 dynamic_library_folder =
"/lib/";
115 std::string package_prefix;
117 package_prefix = ament_index_cpp::get_package_prefix(package_name);
118 }
catch (ament_index_cpp::PackageNotFoundError & e) {
119 throw std::runtime_error(e.what());
122 const std::string library_path = rcpputils::path_for_library(
123 package_prefix + dynamic_library_folder,
124 package_name +
"__" + typesupport_identifier);
125 if (library_path.empty()) {
126 throw std::runtime_error(
127 "Typesupport library for " + package_name +
" does not exist in '" + package_prefix +
133 std::shared_ptr<rcpputils::SharedLibrary>
138 return std::make_shared<rcpputils::SharedLibrary>(library_path);
142 const std::string & type,
143 const std::string & typesupport_identifier,
144 rcpputils::SharedLibrary & library)
150 const std::string & type,
151 const std::string & typesupport_identifier,
152 rcpputils::SharedLibrary & library)
154 static const std::string typesupport_name =
"message";
155 static const std::string symbol_part_name =
"__get_message_type_support_handle__";
156 static const std::string middle_module_additional =
"msg";
158 return static_cast<const rosidl_message_type_support_t *
>(get_typesupport_handle_impl(
159 type, typesupport_identifier, typesupport_name, symbol_part_name,
160 middle_module_additional, library
165 const std::string & type,
166 const std::string & typesupport_identifier,
167 rcpputils::SharedLibrary & library)
169 static const std::string typesupport_name =
"service";
170 static const std::string symbol_part_name =
"__get_service_type_support_handle__";
171 static const std::string middle_module_additional =
"srv";
173 return static_cast<const rosidl_service_type_support_t *
>(get_typesupport_handle_impl(
174 type, typesupport_identifier, typesupport_name, symbol_part_name,
175 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_message_type_support_t * get_typesupport_handle(const std::string &type, const std::string &typesupport_identifier, rcpputils::SharedLibrary &library)
Extract the 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 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.