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
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 Jun 8 2019 18:04:05