blob: ea6789db3a9dc307c6b7981cfb2d07b718516719 (
plain)
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
|
/* ========================================
Copyright (C) 2025 Amlal El Mahrouss, licensed under the Apache 2.0 license
======================================== */
#pragma once
#include <CompilerKit/Detail/Config.h>
#include <dlfcn.h>
#include <mutex>
namespace CompilerKit {
class DLLLoader final {
public:
using EntryT = Int32 (*)(Int32 argc, Char const* argv[]);
using HandleT = VoidPtr;
using MutexT = std::mutex;
EntryT fEntrypoint{nullptr};
private:
HandleT mDLL{nullptr};
MutexT mMutex;
public:
explicit operator bool() { return this->mDLL; }
DLLLoader& operator()(const Char* path, const Char* entrypoint) {
if (!path || !entrypoint) return *this;
std::lock_guard<MutexT> lock(this->mMutex);
if (this->mDLL) {
this->Destroy();
}
this->mDLL = ::dlopen(path, RTLD_LAZY);
if (!this->mDLL) {
return *this;
}
this->fEntrypoint = reinterpret_cast<EntryT>(::dlsym(this->mDLL, entrypoint));
if (!this->fEntrypoint) {
this->Destroy();
return *this;
}
return *this;
}
NECTI_COPY_DELETE(DLLLoader)
DLLLoader() = default;
~DLLLoader() { this->Destroy(); }
private:
void Destroy() noexcept {
if (this->mDLL) {
::dlclose(this->mDLL);
this->mDLL = nullptr;
}
this->fEntrypoint = nullptr;
}
};
} // namespace CompilerKit
|