forked from BehaviorTree/BehaviorTree.CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared_library_UNIX.cpp
More file actions
103 lines (88 loc) · 1.89 KB
/
shared_library_UNIX.cpp
File metadata and controls
103 lines (88 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "behaviortree_cpp/exceptions.h"
#include "behaviortree_cpp/utils/shared_library.h"
#include <mutex>
#include <string>
#include <dlfcn.h>
namespace BT
{
SharedLibrary::SharedLibrary() = default;
void SharedLibrary::load(const std::string& path, int)
{
const std::unique_lock<std::mutex> lock(_mutex);
if(_handle != nullptr)
{
throw RuntimeError("Library already loaded: " + path);
}
_handle = dlopen(path.c_str(), RTLD_NOW | RTLD_GLOBAL);
if(_handle == nullptr)
{
const char* err = dlerror();
throw RuntimeError("Could not load library: " +
(err != nullptr ? std::string(err) : path));
}
_path = path;
}
void SharedLibrary::unload()
{
const std::unique_lock<std::mutex> lock(_mutex);
if(_handle != nullptr)
{
dlclose(_handle);
_handle = nullptr;
}
}
bool SharedLibrary::isLoaded() const
{
return _handle != nullptr;
}
void* SharedLibrary::findSymbol(const std::string& name)
{
const std::unique_lock<std::mutex> lock(_mutex);
void* result = nullptr;
if(_handle != nullptr)
{
result = dlsym(_handle, name.c_str());
}
return result;
}
const std::string& SharedLibrary::getPath() const
{
return _path;
}
std::string SharedLibrary::prefix()
{
#if BT_OS == BT_OS_CYGWIN
return "cyg";
#else
return "lib";
#endif
}
std::string SharedLibrary::suffix()
{
#if BT_OS == BT_OS_MAC_OS_X
#if defined(_DEBUG) && !defined(CL_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
return "d.dylib";
#else
return ".dylib";
#endif
#elif BT_OS == BT_OS_HPUX
#if defined(_DEBUG) && !defined(CL_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
return "d.sl";
#else
return ".sl";
#endif
#elif BT_OS == BT_OS_CYGWIN
#if defined(_DEBUG) && !defined(CL_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
return "d.dll";
#else
return ".dll";
#endif
#else
#if defined(_DEBUG) && !defined(CL_NO_SHARED_LIBRARY_DEBUG_SUFFIX)
return "d.so";
#else
return ".so";
#endif
#endif
}
} // namespace BT