forked from martin-olivier/dylib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdylib.hpp
289 lines (265 loc) · 7.59 KB
/
dylib.hpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* \file dylib.hpp
* \brief Cross-platform Dynamic Library Loader
* \author Martin Olivier
* \version 1.7.0
*
* MIT License
* Copyright (c) 2022 Martin Olivier
*/
#pragma once
#include <string>
#include <functional>
#include <exception>
#include <utility>
#if defined(_WIN32) || defined(_WIN64)
#define WIN32_LEAN_AND_MEAN
#define DYLIB_API extern "C" __declspec(dllexport)
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#else
#define DYLIB_API extern "C"
#include <dlfcn.h>
#endif
/**
* The dylib class can hold a dynamic library instance and interact with it
* by getting its symbols like functions or global variables
*/
class dylib
{
private:
#if defined(_WIN32) || defined(_WIN64)
HINSTANCE m_handle{nullptr};
static HINSTANCE open_lib(const char *path) noexcept
{
return LoadLibraryA(path);
}
FARPROC get_symbol(const char *name) const noexcept
{
return GetProcAddress(m_handle, name);
}
void close_lib() noexcept
{
FreeLibrary(m_handle);
}
static char *get_error_message() noexcept
{
constexpr size_t bufSize = 512;
auto errorCode = GetLastError();
if (!errorCode)
return nullptr;
static char msg[bufSize];
auto lang = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
const DWORD len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, errorCode, lang, msg, bufSize, nullptr);
if (len > 0)
return msg;
return nullptr;
}
#else
void *m_handle{nullptr};
static void *open_lib(const char *path) noexcept
{
return dlopen(path, RTLD_NOW | RTLD_LOCAL);
}
void *get_symbol(const char *name) const noexcept
{
return dlsym(m_handle, name);
}
void close_lib() noexcept
{
dlclose(m_handle);
}
static char *get_error_message() noexcept
{
return dlerror();
}
#endif
static std::string get_handle_error(const std::string &name)
{
auto err = get_error_message();
if (!err)
return "error while loading dynamic library \"" + name + "\"";
return err;
}
static std::string get_symbol_error(const std::string &name)
{
auto err = get_error_message();
if (!err)
return "error while loading symbol \"" + name + "\"";
return err;
}
public:
#if defined(_WIN32) || defined(_WIN64)
static constexpr auto extension = ".dll";
#elif defined(__APPLE__)
static constexpr auto extension = ".dylib";
#else
static constexpr auto extension = ".so";
#endif
/**
* This exception is thrown when the dylib class encountered an error
*
* @return the error message by calling what() member function
*/
class exception : public std::exception
{
protected:
const std::string m_error;
public:
explicit exception(std::string &&message) : m_error(std::move(message)) {}
const char *what() const noexcept override {return m_error.c_str();}
};
/**
* This exception is thrown when the library failed to load
* or encountered symbol resolution issues
*
* @param message the error message
*/
class handle_error : public exception
{
public:
explicit handle_error(std::string &&message) : exception(std::move(message)) {}
};
/**
* This exception is thrown when the library failed to load a symbol.
* This usually happens when you forgot to put <DYLIB_API> before a library function or variable
*
* @param message the error message
*/
class symbol_error : public exception
{
public:
explicit symbol_error(std::string &&message) : exception(std::move(message)) {}
};
dylib(const dylib&) = delete;
dylib& operator=(const dylib&) = delete;
dylib(dylib &&other) noexcept
{
m_handle = other.m_handle;
other.m_handle = nullptr;
}
dylib& operator=(dylib &&other) noexcept
{
if (this != &other) {
close();
m_handle = other.m_handle;
other.m_handle = nullptr;
}
return *this;
}
dylib() noexcept = default;
/**
* Creates a dynamic library instance
*
* @param path path to the dynamic library to load
* @param ext use dylib::extension to specify the os extension (optional parameter)
*/
explicit dylib(const char *path)
{
open(path);
}
explicit dylib(const std::string &path)
{
open(path.c_str());
}
dylib(std::string path, const char *ext)
{
open(std::move(path), ext);
}
~dylib()
{
close();
}
/**
* Load a dynamic library into the object.
* If a dynamic library was already opened, it will be unload and replaced
*
* @param path the path of the dynamic library to load
* @param ext use dylib::extension to detect the current os extension (optional parameter)
*/
void open(const char *path)
{
close();
if (!path)
throw handle_error(get_handle_error("(nullptr)"));
m_handle = open_lib(path);
if (!m_handle)
throw handle_error(get_handle_error(path));
}
void open(const std::string &path)
{
open(path.c_str());
}
void open(std::string path, const char *ext)
{
close();
if (!ext)
throw handle_error("bad extension : (nullptr)");
path += ext;
m_handle = open_lib(path.c_str());
if (!m_handle)
throw handle_error(get_handle_error(path));
}
/**
* Get a function from the dynamic library currently loaded in the object
*
* @param T the template argument must be the function prototype.
* it must be the same pattern as the template of std::function
* @param name the symbol name of the function to get from the dynamic library
*
* @returns std::function<T> that contains the function
*/
template<typename T>
std::function<T> get_function(const char *name) const
{
if (!m_handle)
throw handle_error("error : no dynamic library loaded");
if (!name)
throw symbol_error(get_symbol_error("(nullptr)"));
auto sym = get_symbol(name);
if (!sym)
throw symbol_error(get_symbol_error(name));
return reinterpret_cast<T *>(sym);
}
template<typename T>
std::function<T> get_function(const std::string &name) const
{
return get_function<T>(name.c_str());
}
/**
* Get a global variable from the dynamic library currently loaded in the object
*
* @param T type of the global variable
* @param name the name of the global variable to get from the dynamic library
*
* @returns global variable of type <T>
*/
template<typename T>
T &get_variable(const char *name) const
{
if (!m_handle)
throw handle_error("error : no dynamic library loaded");
if (!name)
throw symbol_error(get_symbol_error("(nullptr)"));
auto sym = get_symbol(name);
if (!sym)
throw symbol_error(get_symbol_error(name));
return *reinterpret_cast<T *>(sym);
}
template<typename T>
T &get_variable(const std::string &name) const
{
return get_variable<T>(name.c_str());
}
/**
* Close the dynamic library currently loaded in the object.
* This function will be automatically called by the class destructor
*/
void close() noexcept
{
if (m_handle) {
close_lib();
m_handle = nullptr;
}
}
};