-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlib.cppm
More file actions
executable file
·190 lines (185 loc) · 5.37 KB
/
dlib.cppm
File metadata and controls
executable file
·190 lines (185 loc) · 5.37 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
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
// definions
// CPUF_DLIB_NO_EXTENSION
// CPUF_DLIB_DEFAULT_FLAG x
module;
# ifdef _WIN32
# include <Windows.h>
# else
# include <dlfcn.h>
# endif
export module cpuf.dlib;
import std;
extern int dlibDefFlags;
/**
* @brief A cross-platform class for dynamic library loading.
*/
export class dlib {
public:
class dlib_exception : public std::exception {
public:
enum class Kind {
loadlib,
loadfun,
load
};
dlib_exception(const char*& e, Kind k);
dlib_exception(const std::string& e, Kind k);
virtual const char* what() const noexcept override;
Kind kind;
private:
std::string m;
};
/**
* @brief Construct the object and load dynamic library
*
* @param path The path to the dynamic library without extension.
* The extension is added based on the platform an app is compiled for.
* You may disable automatic extension by defining CPUF_DLIB_NO_EXTENSION
*/
dlib(const char* path);
dlib(const std::string path);
dlib();
~dlib();
// Free the dynamic library if loaded
void free();
#ifdef CPUF_DLIB_NO_EXTENSION
/**
* @brief Load a library. If the library is already loaded it is overriden
*
* @param path The path to the dynamic library without extension.
* The extension is added based on the platform an app is compiled for.
* You may disable automatic extension by defining CPUF_DLIB_NO_EXTENSION
*/
inline void loadlib(const std::string path);
#else
/**
* @brief Load a library. This function is directly used by constructor. If the library is already loaded it is overriden
*
* @param path The path to the dynamic library without extension.
* The extension is added based on the platform an app is compiled for.
* You may disable automatic extension by defining CPUF_DLIB_NO_EXTENSION
*/
void loadlib(const char* path);
void loadlib(std::string path);
#endif
/**
* @brief Sets the flag to value you specify
* @param flag Flag you specify. Use | operator if need multiple
*/
void setFlag(const int& flag);
/**
* @brief Adds flag you specify
* @param flag Flag you specify. Use | operator if need multiple
*/
void addFlag(const int& flag);
/**
* @brief Remove flag you specify
* @param flag Flag to remove
*/
void removeFlag(const int& flag);
/**
* @brief Set flags to its default state (std::dlibDefFlags)
*/
void clearFlags();
/**
* @brief Load function from currently loaded library
*
* @tparam ReturnT The function return type
* @tparam Params Function parameter types
* @param name The name of function
* @return returns it's pointer that you can use to call within your code
*/
template<typename ReturnT, typename... Params>
inline ReturnT(*loadfun(const char* name))(Params...) {
auto p = reinterpret_cast<ReturnT(*)(Params...)>(
#ifdef _WIN32
GetProcAddress(handler, name)
#else
dlsym(handler, name)
#endif
);
if (p == nullptr) {
free();
std::string n = "cannot load function '";
n += name;
n+= "'";
throw dlib::dlib_exception(n, dlib::dlib_exception::Kind::loadfun);
}
return p;
}
template<typename ReturnT, typename... Params>
inline ReturnT(*loadfun(std::string name))(Params...) {
auto p = reinterpret_cast<ReturnT(*)(Params...)>(
#ifdef _WIN32
GetProcAddress(handler, name.c_str())
#else
dlsym(handler, name.c_str())
#endif
);
if (p == nullptr) {
free();
name.insert(0, "cannot load function '");
name += "'";
throw dlib::dlib_exception(name, dlib::dlib_exception::Kind::loadfun);
}
return p;
}
/**
* @brief Load something from dynamic library with your own type
*
* @tparam T The type of thing you want to load
* @param name The name of thing you want to load
* @return T Returns pointer to thing you want to load
*/
template<typename T>
T load(std::string name) {
T p = reinterpret_cast<T>(
#ifdef _WIN32
GetProcAddress
#else
dlsym
#endif
(handler, name.c_str())
);
if (p == nullptr) {
free();
name.insert(0, "cannot load '");
name += "'";
throw dlib::dlib_exception(name, dlib::dlib_exception::Kind::load);
}
return p;
}
/**
* @brief Whether currently library is loaded (handler != nullptr)
*
* @return true
* @return false
*/
constexpr bool isLoaded() const {
return handler != nullptr;
}
/**
* @brief Whether currently library is not loaded and handler is being free (handler == nullptr)
*
* @return true
* @return false
*/
constexpr bool isfree() const {
return handler == nullptr;
}
private:
template <typename Signature>
struct FunctionPtr;
template <typename R, typename... Args>
struct FunctionPtr<R(Args...)> {
using type = R (*)(Args...);
};
#ifdef _WIN32
HMODULE handler;
#else
void* handler;
int f;
#endif
};
#undef CPUF_DLIB_NO_EXTENSION
#undef CPUF_DLIB_DEFAULT_FLAG