forked from nu774/qaac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdl.h
41 lines (37 loc) · 909 Bytes
/
dl.h
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
#ifndef DL_H
#define DL_H
#include <string>
#include <memory>
#include <dlfcn.h> // dlopen
class AutoCast {
void* m_pointer;
public:
AutoCast(void* p): m_pointer(p) {}
template <typename U>
operator U*() { return reinterpret_cast<U*>(m_pointer); }
};
class DL {
std::shared_ptr<void> m_handle;
public:
DL() {}
DL(void* handle, bool own=true)
{
if (own)
m_handle.reset(handle, dlclose);
else
m_handle.reset(handle, [](void*){});
}
bool load(const std::string &path)
{
void* handle = dlopen(path.c_str(), RTLD_LAZY);
if (handle) m_handle.reset(handle, dlclose);
return loaded();
}
bool loaded() const { return m_handle.get() != nullptr; }
void reset() { m_handle.reset(); }
AutoCast fetch(const char *name)
{
return AutoCast(dlsym(m_handle.get(), name));
}
};
#endif