Lugdunum  0.1.0
Library.inl
Go to the documentation of this file.
1 #if defined(LUG_SYSTEM_WINDOWS)
2 
3 inline const char* getLastErrorWindows() {
4  static const DWORD size = 200 + 1;
5  static char buffer[size];
6 
7  auto lastError = GetLastError();
8 
9  auto messageSize = FormatMessage(
10  FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
11  nullptr,
12  lastError,
13  0,
14  buffer,
15  size,
16  nullptr
17  );
18 
19  if (messageSize > 0) {
20  buffer[messageSize - 1] = 0;
21  }
22 
23  return buffer;
24 }
25 
26 #endif
27 
28 inline Handle open(const char* name) {
29  Handle handle = nullptr;
30 
31 #if defined(LUG_SYSTEM_WINDOWS)
32  handle = LoadLibraryA(name);
33 #else
34  handle = dlopen(name, RTLD_LAZY | RTLD_LOCAL);
35 #endif
36 
37  if (!handle) {
38 #if defined(LUG_SYSTEM_WINDOWS)
39  LUG_LOG.warn("Library: Can't load the library: {}: {}", name, getLastErrorWindows());
40 #else
41  LUG_LOG.warn("Library: Can't load the library: {}", dlerror());
42 #endif
43  }
44 
45  return handle;
46 }
47 
48 inline void close(Handle handle) {
49  if (!handle) {
50  return;
51  }
52 
53 #if defined(LUG_SYSTEM_WINDOWS)
54  FreeLibrary(handle);
55 #else
56  dlclose(handle);
57 #endif
58 }
59 
60 template<typename Function>
61 inline Function sym(Handle handle, const char *name) {
62  void* sym = nullptr;
63 
64 #if defined(LUG_SYSTEM_WINDOWS)
65  sym = GetProcAddress(handle, name);
66 #else
67  sym = dlsym(handle, name);
68 #endif
69 
70  if (!sym) {
71 #if defined(LUG_SYSTEM_WINDOWS)
72  LUG_LOG.warn("Library: Can't load the symbol {}: {}", name, getLastErrorWindows());
73 #else
74  LUG_LOG.warn("Library: Can't load the symbol {}: {}", name, dlerror());
75 #endif
76  }
77 
78  return reinterpret_cast<Function>(sym);
79 }
Handle open(const char *name)
Definition: Library.inl:28
void close(Handle handle)
Definition: Library.inl:48
#define LUG_LOG
Definition: Logger.hpp:73
Function sym(Handle handle, const char *name)
Definition: Library.inl:61