Lodestone.Core
Loading...
Searching...
No Matches
LibraryHandle.h
Go to the documentation of this file.
1
11#ifndef LODESTONE_LIBRARYHANDLE_H
12#define LODESTONE_LIBRARYHANDLE_H
13#include <filesystem>
14#include <optional>
15
16#ifdef _WIN32
17#include <windows.h>
18#elif defined(__unix__) || defined (__APPLE__)
19#include <dlfcn.h>
20#endif
21
56 public:
57#ifdef _WIN32
58 using handle_t = HMODULE;
59 using function_t = FARPROC;
60
61 static constexpr const handle_t NULL_HANDLE = NULL;
62 static constexpr const function_t NULL_FUNCTION = nullptr;
63#elif defined(__unix__) || defined (__APPLE__)
64 using handle_t = void *;
65 using function_t = void *;
66
67 static constexpr const handle_t NULL_HANDLE = nullptr;
68 static constexpr const function_t NULL_FUNCTION = nullptr;
69#endif
70
75 explicit LibraryHandle(const std::filesystem::path &path);
76
81 void load();
82
87 void unload();
88
90 bool valid() const noexcept;
91
93 template <typename Func>
94 requires std::is_function_v<std::remove_pointer_t<std::remove_reference_t<Func>>>
95 Func getFunction(const char *name) {
96 return reinterpret_cast<Func>(this->getFunction(name));
97 }
98
100 function_t getFunction(const char *name);
101
102 private:
103 std::filesystem::path m_path;
104
105 handle_t m_handle;
106 // TODO for emscripten and other platforms we will impl custom, internal loader which initializes libs added at compile time
107 };
108}
109
110#endif // LODESTONE_LIBRARYHANDLE_H
std::filesystem::path m_path
The path of the library file.
Definition LibraryHandle.h:103
void load()
Loads a library at the stored path.
Definition LibraryHandle.cpp:32
LibraryHandle(const std::filesystem::path &path)
Creates a new LibraryHandle.
Definition LibraryHandle.cpp:30
bool valid() const noexcept
Returns true if the library handle is valid.
Definition LibraryHandle.cpp:89
void unload()
Unloads the library.
Definition LibraryHandle.cpp:60
handle_t m_handle
The library handle.
Definition LibraryHandle.h:105
Func getFunction(const char *name)
Returns the address of an exported function by name, casts to the given function type.
Definition LibraryHandle.h:95
Definition LibraryException.cpp:10