ROS 2 rclcpp + rcl - jazzy  jazzy
ROS 2 C++ Client Library with ROS Client Library
typesupport_helpers.cpp
1 // Copyright 2018, Bosch Software Innovations GmbH.
2 // Copyright 2021, Apex.AI Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "rclcpp/typesupport_helpers.hpp"
17 
18 #include <functional>
19 #include <memory>
20 #include <sstream>
21 #include <stdexcept>
22 #include <string>
23 #include <tuple>
24 #include <utility>
25 
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"
31 
32 namespace rclcpp
33 {
34 
35 namespace
36 {
37 
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)
45 {
46  std::string package_name;
47  std::string middle_module;
48  std::string type_name;
49  std::tie(package_name, middle_module, type_name) = extract_type_identifier(type);
50 
51  if (middle_module.empty()) {
52  middle_module = middle_module_additional;
53  }
54 
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();
62  };
63 
64  try {
65  std::string symbol_name = typesupport_identifier + symbol_part_name +
66  package_name + "__" + middle_module + "__" + type_name;
67  const void * (* get_ts)() = nullptr;
68  // This will throw runtime_error if the symbol was not found.
69  get_ts = reinterpret_cast<decltype(get_ts)>(library.get_symbol(symbol_name));
70  return get_ts();
71  } catch (std::runtime_error &) {
72  throw std::runtime_error{mk_error("Library could not be found.")};
73  }
74 }
75 
76 } // anonymous namespace
77 
78 std::tuple<std::string, std::string, std::string>
79 extract_type_identifier(const std::string & full_type)
80 {
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)
87  {
88  throw std::runtime_error(
89  "Message type is not of the form package/type and cannot be processed");
90  }
91 
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) {
95  middle_module =
96  full_type.substr(sep_position_front + 1, sep_position_back - sep_position_front - 1);
97  }
98  std::string type_name = full_type.substr(sep_position_back + 1);
99 
100  return std::make_tuple(package_name, middle_module, type_name);
101 }
102 
104  const std::string & package_name, const std::string & typesupport_identifier)
105 {
106  const char * dynamic_library_folder;
107 #ifdef _WIN32
108  dynamic_library_folder = "/bin/";
109 #elif __APPLE__
110  dynamic_library_folder = "/lib/";
111 #else
112  dynamic_library_folder = "/lib/";
113 #endif
114 
115  std::string package_prefix;
116  try {
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());
120  }
121 
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 +
128  "'.");
129  }
130  return library_path;
131 }
132 
133 std::shared_ptr<rcpputils::SharedLibrary>
134 get_typesupport_library(const std::string & type, const std::string & typesupport_identifier)
135 {
136  auto package_name = std::get<0>(extract_type_identifier(type));
137  auto library_path = get_typesupport_library_path(package_name, typesupport_identifier);
138  return std::make_shared<rcpputils::SharedLibrary>(library_path);
139 }
140 
141 const rosidl_message_type_support_t * get_typesupport_handle(
142  const std::string & type,
143  const std::string & typesupport_identifier,
144  rcpputils::SharedLibrary & library)
145 {
146  return get_message_typesupport_handle(type, typesupport_identifier, library);
147 }
148 
149 const rosidl_message_type_support_t * get_message_typesupport_handle(
150  const std::string & type,
151  const std::string & typesupport_identifier,
152  rcpputils::SharedLibrary & library)
153 {
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";
157 
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
161  ));
162 }
163 
164 const rosidl_service_type_support_t * get_service_typesupport_handle(
165  const std::string & type,
166  const std::string & typesupport_identifier,
167  rcpputils::SharedLibrary & library)
168 {
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";
172 
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
176  ));
177 }
178 
179 } // namespace rclcpp
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.