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


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 04:01:53