|
| 1 | +// Copyright 2021 Open Source Robotics Foundation, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef DYNAMIC_SUPPORT_DISPATCH_HPP_ |
| 16 | +#define DYNAMIC_SUPPORT_DISPATCH_HPP_ |
| 17 | + |
| 18 | +#include <cstddef> |
| 19 | +#include <cstdio> |
| 20 | +#include <cstring> |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <stdexcept> |
| 24 | +#include <list> |
| 25 | +#include <string> |
| 26 | + |
| 27 | +#include "rcpputils/shared_library.hpp" |
| 28 | +#include "rcutils/error_handling.h" |
| 29 | +#include "rcutils/snprintf.h" |
| 30 | +#include "rosidl_typesupport_c/type_support_map.h" |
| 31 | + |
| 32 | +namespace rosidl_typesupport_c |
| 33 | +{ |
| 34 | + |
| 35 | +static void * |
| 36 | +handle_shared_library_from_name( |
| 37 | + const type_support_map_t * map, |
| 38 | + size_t map_item, |
| 39 | + const char * identifier |
| 40 | +) |
| 41 | +{ |
| 42 | + rcpputils::SharedLibrary * lib = nullptr; |
| 43 | + |
| 44 | + if (!map->data[map_item]) { |
| 45 | + char library_basename[1024]; |
| 46 | + int ret = rcutils_snprintf( |
| 47 | + library_basename, 1023, "%s__%s", |
| 48 | + map->package_name, identifier); |
| 49 | + if (ret < 0) { |
| 50 | + RCUTILS_SET_ERROR_MSG("Failed to format library name"); |
| 51 | + return nullptr; |
| 52 | + } |
| 53 | + |
| 54 | + std::string library_name; |
| 55 | + try { |
| 56 | + library_name = rcpputils::get_platform_library_name(library_basename); |
| 57 | + } catch (const std::exception & e) { |
| 58 | + RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING( |
| 59 | + "Failed to compute library name for '%s' due to %s", |
| 60 | + library_basename, e.what()); |
| 61 | + return nullptr; |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + lib = new rcpputils::SharedLibrary(library_name); |
| 66 | + } catch (const std::runtime_error & e) { |
| 67 | + RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING( |
| 68 | + "Could not load library %s: %s", library_name.c_str(), e.what()); |
| 69 | + return nullptr; |
| 70 | + } catch (const std::bad_alloc & e) { |
| 71 | + RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING( |
| 72 | + "Could not load library %s: %s", library_name.c_str(), e.what()); |
| 73 | + return nullptr; |
| 74 | + } |
| 75 | + map->data[map_item] = lib; |
| 76 | + } |
| 77 | + |
| 78 | + auto clib = static_cast<const rcpputils::SharedLibrary *>(map->data[map_item]); |
| 79 | + lib = const_cast<rcpputils::SharedLibrary *>(clib); |
| 80 | + |
| 81 | + void * sym = nullptr; |
| 82 | + |
| 83 | + try { |
| 84 | + if (!lib->has_symbol(map->symbol_name[map_item])) { |
| 85 | + RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING( |
| 86 | + "Failed to find symbol '%s' in library", map->symbol_name[map_item]); |
| 87 | + return nullptr; |
| 88 | + } |
| 89 | + sym = lib->get_symbol(map->symbol_name[map_item]); |
| 90 | + } catch (const std::exception & e) { |
| 91 | + RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING( |
| 92 | + "Failed to get symbol '%s' in library: %s", |
| 93 | + map->symbol_name[map_item], e.what()); |
| 94 | + return nullptr; |
| 95 | + } |
| 96 | + |
| 97 | + return sym; |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace rosidl_typesupport_c |
| 101 | + |
| 102 | +#endif // DYNAMIC_SUPPORT_DISPATCH_HPP_ |
0 commit comments