shared_library_UNIX.cpp
Go to the documentation of this file.
1 #include <string>
2 #include <mutex>
3 #include <dlfcn.h>
6 
7 namespace BT
8 {
10 {
11  _handle = nullptr;
12 }
13 
14 void SharedLibrary::load(const std::string& path, int)
15 {
16  std::unique_lock<std::mutex> lock(_mutex);
17 
18  if(_handle)
19  {
20  throw RuntimeError("Library already loaded: " + path);
21  }
22 
23  _handle = dlopen(path.c_str(), RTLD_NOW | RTLD_GLOBAL);
24  if(!_handle)
25  {
26  const char* err = dlerror();
27  throw RuntimeError("Could not load library: " + (err ? std::string(err) : path));
28  }
29  _path = path;
30 }
31 
33 {
34  std::unique_lock<std::mutex> lock(_mutex);
35 
36  if(_handle)
37  {
38  dlclose(_handle);
39  _handle = nullptr;
40  }
41 }
42 
44 {
45  return _handle != nullptr;
46 }
47 
48 void* SharedLibrary::findSymbol(const std::string& name)
49 {
50  std::unique_lock<std::mutex> lock(_mutex);
51 
52  void* result = nullptr;
53  if(_handle)
54  {
55  result = dlsym(_handle, name.c_str());
56  }
57  return result;
58 }
59 
60 const std::string& SharedLibrary::getPath() const
61 {
62  return _path;
63 }
64 
65 std::string SharedLibrary::prefix()
66 {
67 #if BT_OS == BT_OS_CYGWIN
68  return "cyg";
69 #else
70  return "lib";
71 #endif
72 }
73 
74 std::string SharedLibrary::suffix()
75 {
76 #if BT_OS == BT_OS_MAC_OS_X
77 #if defined(_DEBUG) && !defined(CL_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
78  return "d.dylib";
79 #else
80  return ".dylib";
81 #endif
82 #elif BT_OS == BT_OS_HPUX
83 #if defined(_DEBUG) && !defined(CL_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
84  return "d.sl";
85 #else
86  return ".sl";
87 #endif
88 #elif BT_OS == BT_OS_CYGWIN
89 #if defined(_DEBUG) && !defined(CL_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
90  return "d.dll";
91 #else
92  return ".dll";
93 #endif
94 #else
95 #if defined(_DEBUG) && !defined(CL_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
96  return "d.so";
97 #else
98  return ".so";
99 #endif
100 #endif
101 }
102 
103 } // namespace BT
BT
Definition: ex01_wrap_legacy.cpp:29
BT::SharedLibrary::load
void load(const std::string &path, int flags=0)
Definition: shared_library_UNIX.cpp:14
exceptions.h
BT::SharedLibrary::_handle
void * _handle
Definition: shared_library.h:137
BT::SharedLibrary::SharedLibrary
SharedLibrary()
Creates a SharedLibrary object.
Definition: shared_library_UNIX.cpp:9
BT::SharedLibrary::getPath
const std::string & getPath() const
Definition: shared_library_UNIX.cpp:60
BT::SharedLibrary::prefix
static std::string prefix()
Definition: shared_library_UNIX.cpp:65
BT::SharedLibrary::suffix
static std::string suffix()
Definition: shared_library_UNIX.cpp:74
BT::SharedLibrary::unload
void unload()
Unloads a shared library.
Definition: shared_library_UNIX.cpp:32
BT::SharedLibrary::_path
std::string _path
Definition: shared_library.h:136
BT::RuntimeError
Definition: exceptions.h:58
BT::SharedLibrary::_mutex
std::mutex _mutex
Definition: shared_library.h:138
BT::SharedLibrary::findSymbol
void * findSymbol(const std::string &name)
Definition: shared_library_UNIX.cpp:48
BT::SharedLibrary::isLoaded
bool isLoaded() const
Returns true iff a library has been loaded.
Definition: shared_library_UNIX.cpp:43
shared_library.h


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:08