GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
SharedLibrary.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_SYSTEM_SHARED_LIBRARY_HPP
5#define GREM_CORE_SYSTEM_SHARED_LIBRARY_HPP
6
8
9#include <GREM/core/Error.hpp>
12
13namespace grem {
14
15class Filesystem; // Forward declaration, to avoid including Filesystem.hpp
16
27public:
31 struct Error : grem::Error {
33 };
34
43 [[nodiscard]] GREM_API(core) static CStringView getLibraryFilenameExtension();
44
48 SharedLibrary() noexcept = default;
49
60 explicit SharedLibrary(const Filesystem& filesystem, CStringView filepath) {
61 open(filesystem, filepath);
62 }
63
69 explicit operator bool() const noexcept {
70 return is_open();
71 }
72
78 [[nodiscard]] bool is_open() const noexcept {
79 return static_cast<bool>(library);
80 }
81
89 GREM_API(core) void close();
90
101 GREM_API(core) void open(const Filesystem& filesystem, CStringView filepath);
102
117 template <typename T>
119 return *(T*)getSymbolAddress(name);
120 }
121
122private:
123 struct LibraryDeleter {
124 GREM_API(core) void operator()(void* handle) const noexcept;
125 };
126
127 [[nodiscard]] GREM_API(core) void* getSymbolAddress(CStringView name) const;
128
129 UniqueHandle<void*, LibraryDeleter> library{};
130};
131
132} // namespace grem
133
134#endif
Base exception type for runtime errors.
Definition Error.hpp:38
Error(CStringView message)
Definition Error.hpp:55
Base interface for a filesystem.
Definition Filesystem.hpp:25
void open(const Filesystem &filesystem, CStringView filepath)
Open the shared library from a file.
bool is_open() const noexcept
Check if the shared library is currently open.
Definition SharedLibrary.hpp:78
static CStringView getLibraryFilenameExtension()
Get the proper filename extension for shared libraries on the current platform.
T & getSymbol(CStringView name)
Get a reference to the symbol with a specific name in the library.
Definition SharedLibrary.hpp:118
SharedLibrary() noexcept=default
Construct a closed shared library.
void close()
Close the shared library if it is currently open, invalidating all current pointers to its symbols fr...
Definition aliases.hpp:9
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
Error(CStringView message)
Definition Error.hpp:55