-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlib.cpp
More file actions
executable file
·168 lines (162 loc) · 4.24 KB
/
Copy pathdlib.cpp
File metadata and controls
executable file
·168 lines (162 loc) · 4.24 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
// definions
// CPUF_DLIB_NO_EXTENSION
// CPUF_DLIB_DEFAULT_FLAG x
module;
# ifdef _WIN32
# include <Windows.h>
# else
# include <dlfcn.h>
# endif
module cpuf.dlib;
import std;
static std::string getLastError() {
#ifdef _WIN32
DWORD errorCode = GetLastError();
if (errorCode == 0) {
return "";
}
LPSTR messageBuffer = nullptr;
FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, errorCode, 0, (LPSTR)&messageBuffer, 0, nullptr
);
std::string errorMsg = messageBuffer ? std::string(messageBuffer) : "";
LocalFree(messageBuffer);
return errorMsg;
#else
char* errorMsg = dlerror();
return errorMsg ? std::string(errorMsg) : "";
#endif
}
/**
* @brief The default flag for the dlib class when the program is compiled for unix systems. On Windows all interaction ignored.
* Default state is RTLD_LAZY
*/
int dlibDefFlags = 0;
dlib::dlib_exception::dlib_exception(const char*& e, Kind k) {
kind = k;
m += e;
}
dlib::dlib_exception::dlib_exception(const std::string& e, Kind k) {
kind = k;
m += e;
}
const char* dlib::dlib_exception::what() const noexcept {
return m.c_str();
}
dlib::dlib(const char* path) {
loadlib(path);
}
dlib::dlib(const std::string path) {
loadlib(path);
}
dlib::dlib() {}
dlib::~dlib() {
free();
}
// Free the dynamic library if loaded
void dlib::free() {
if (handler != nullptr) {
#ifdef _WIN32
FreeLibrary(handler);
#else
dlclose(handler);
#endif
handler = nullptr;
}
}
#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 dlib::loadlib(const const char* path) {
loadlib(std::string(path));
}
inline void dlib::loadlib(const std::string path) {
if (handler) free();
# ifdef _WIN32
handler = LoadLibraryA(path.c_str());
# else
handler = dlopen(path.c_str(), RTLD_NOW | RTLD_GLOBAL);
# endif
if (handler == nullptr) {
std::string error_message = "cannot open '";
error_message += path;
error_message += "', error: ";
error_message += getLastError();
throw std::dlib_err(error_message, std::dlib_err::Kind::loadlib);
}
}
#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 dlib::loadlib(const char* path) {
loadlib(std::string(path));
}
void dlib::loadlib(std::string path) {
#ifdef _WIN32
path += ".dll";
handler = LoadLibraryA(path.c_str());
#else
#ifdef __APPLE__
path += ".dylib";
#else
path += ".so";
#endif
handler = dlopen(path.c_str(), RTLD_NOW | RTLD_GLOBAL);
#endif
if (handler == nullptr) {
std::string error_message = "cannot open '";
error_message += path;
error_message += "', error: ";
error_message += getLastError();
throw dlib::dlib_exception(error_message, dlib::dlib_exception::Kind::loadlib);
}
}
#endif
/**
* @brief Sets the flag to value you specify
* @param flag Flag you specify. Use | operator if need multiple
*/
inline void dlib::setFlag(const int& flag) {
#ifndef _WIN32
f = flag;
#endif
}
/**
* @brief Adds flag you specify
* @param flag Flag you specify. Use | operator if need multiple
*/
inline void dlib::addFlag(const int& flag) {
#ifndef _WIN32
f |= flag;
#endif
}
/**
* @brief Remove flag you specify
* @param flag Flag to remove
*/
inline void dlib::removeFlag(const int& flag) {
#ifndef _WIN32
f &= ~flag;
#endif
}
/**
* @brief Set flags to its default state (std::dlibDefFlags)
*/
inline void dlib::clearFlags() {
#ifndef _WIN32
f = dlibDefFlags;
#endif
}
#undef CPUF_DLIB_NO_EXTENSION
#undef CPUF_DLIB_DEFAULT_FLAG