-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdynamic_loader.h
More file actions
109 lines (96 loc) · 2.9 KB
/
Copy pathdynamic_loader.h
File metadata and controls
109 lines (96 loc) · 2.9 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
/* Developed by Jimmy Hu */
/* Cross-Platform Dynamic Shared Object (DLL/SO) Loader */
#ifndef TINYDIP_DYNAMIC_LOADER_H
#define TINYDIP_DYNAMIC_LOADER_H
#include <filesystem>
#include <iostream>
#include <stdexcept>
#include <string>
#include <string_view>
#ifdef _WIN32
#define NOMINMAX // Prevents <windows.h> from defining min/max macros
#include <windows.h>
#else
#include <dlfcn.h>
#endif
class DynamicLibrary
{
public:
DynamicLibrary(const std::filesystem::path& library_path)
{
const std::string path_str = library_path.string();
#ifdef _WIN32
handle_ = LoadLibraryA(path_str.c_str());
if (!handle_)
{
throw std::runtime_error("Failed to load DLL: " + path_str);
}
#else
// RTLD_NOW ensures all symbols are resolved, RTLD_GLOBAL is crucial
// for cross-boundary std::any typeid matching!
handle_ = dlopen(path_str.c_str(), RTLD_NOW | RTLD_GLOBAL);
if (!handle_)
{
const char* err = dlerror();
throw std::runtime_error("Failed to load Shared Object: " + path_str + "\nReason: " + (err ? err : "Unknown"));
}
#endif
}
~DynamicLibrary()
{
if (handle_)
{
#ifdef _WIN32
FreeLibrary(static_cast<HMODULE>(handle_));
#else
dlclose(handle_);
#endif
}
}
// Disable copy to maintain strict resource ownership (RAII)
DynamicLibrary(const DynamicLibrary&) = delete;
DynamicLibrary& operator=(const DynamicLibrary&) = delete;
// Enable move semantics natively for std::vector compatibility
DynamicLibrary(DynamicLibrary&& other) noexcept : handle_(other.handle_)
{
other.handle_ = nullptr;
}
DynamicLibrary& operator=(DynamicLibrary&& other) noexcept
{
if (this != &other)
{
if (handle_)
{
#ifdef _WIN32
FreeLibrary(static_cast<HMODULE>(handle_));
#else
dlclose(handle_);
#endif
}
handle_ = other.handle_;
other.handle_ = nullptr;
}
return *this;
}
template <typename Signature>
Signature get_function(const std::string_view symbol_name) const
{
#ifdef _WIN32
void* symbol = reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle_), std::string(symbol_name).c_str()));
#else
void* symbol = dlsym(handle_, std::string(symbol_name).c_str());
#endif
if (!symbol)
{
throw std::runtime_error(std::string("Failed to locate symbol: ") + std::string(symbol_name));
}
// Strict ISO C++ and POSIX compliant cast from object pointer (void*) to function pointer.
// Bypasses strict-aliasing compiler warnings natively without invoking Undefined Behavior.
Signature func_ptr;
*reinterpret_cast<void**>(&func_ptr) = symbol;
return func_ptr;
}
private:
void* handle_{ nullptr };
};
#endif