Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches

Cross-platform dynamic library loader. More...

#include <dynamic_library.hpp>

Public Types

using HandleType = void*
 Native handle type (void* on all platforms for ABI stability).

Public Member Functions

 DynamicLibrary ()=default
 DynamicLibrary (const DynamicLibrary &)=delete
 DynamicLibrary (DynamicLibrary &&other) noexcept
 ~DynamicLibrary () noexcept
 Destructor that unloads the library if loaded.
DynamicLibraryoperator= (const DynamicLibrary &)=delete
DynamicLibraryoperator= (DynamicLibrary &&other) noexcept
auto Load (const std::filesystem::path &path) -> std::expected< void, DynamicLibraryError >
 Loads a dynamic library from the specified path.
auto Unload () -> std::expected< void, DynamicLibraryError >
 Unloads the currently loaded library.
auto Reload () -> std::expected< void, DynamicLibraryError >
 Reloads the library from the same path.
auto GetSymbolAddress (std::string_view name) const -> std::expected< void *, DynamicLibraryError >
 Gets a raw symbol address from the library.
template<typename T>
requires std::is_pointer_v<T>
auto GetSymbol (std::string_view name) const -> std::expected< T, DynamicLibraryError >
 Gets a typed function pointer from the library.
bool Loaded () const noexcept
 Checks if a library is currently loaded.
const std::filesystem::path & Path () const noexcept
 Gets the path of the loaded library.
HandleType Handle () const noexcept
 Gets the native handle of the loaded library.

Static Public Member Functions

static auto FromPath (const std::filesystem::path &path) -> std::expected< DynamicLibrary, DynamicLibraryError >
static std::string GetLastErrorMessage () noexcept
 Gets the last platform-specific error message.
static constexpr std::string_view GetPlatformExtension () noexcept
 Gets the platform-specific file extension for dynamic libraries.
static constexpr std::string_view GetPlatformPrefix () noexcept
 Gets the platform-specific library prefix.

Static Public Attributes

static constexpr HandleType kInvalidHandle = nullptr

Detailed Description

Cross-platform dynamic library loader.

Provides a unified interface for loading dynamic libraries (DLLs on Windows, .so on Linux, .dylib on macOS) and retrieving function symbols.

Note
Not thread-safe. External synchronization required for concurrent access.
if (auto result = lib.Load("my_module.so"); result) {
using CreateModuleFn = Module* (*)();
if (auto fn = lib.GetSymbol<CreateModuleFn>("create_module"); fn) {
Module* module = (*fn)();
// Use module...
}
}
auto GetSymbol(std::string_view name) const -> std::expected< T, DynamicLibraryError >
Gets a typed function pointer from the library.
auto Load(const std::filesystem::path &path) -> std::expected< void, DynamicLibraryError >
Loads a dynamic library from the specified path.

Definition at line 71 of file dynamic_library.hpp.

Member Typedef Documentation

◆ HandleType

Native handle type (void* on all platforms for ABI stability).

Definition at line 74 of file dynamic_library.hpp.

Constructor & Destructor Documentation

◆ DynamicLibrary() [1/3]

helios::utils::DynamicLibrary::DynamicLibrary ( )
default

◆ DynamicLibrary() [2/3]

helios::utils::DynamicLibrary::DynamicLibrary ( const DynamicLibrary & )
delete

◆ DynamicLibrary() [3/3]

helios::utils::DynamicLibrary::DynamicLibrary ( DynamicLibrary && other)
inlinenoexcept

Definition at line 192 of file dynamic_library.hpp.

◆ ~DynamicLibrary()

helios::utils::DynamicLibrary::~DynamicLibrary ( )
inlinenoexcept

Destructor that unloads the library if loaded.

Definition at line 197 of file dynamic_library.hpp.

Member Function Documentation

◆ FromPath()

auto helios::utils::DynamicLibrary::FromPath ( const std::filesystem::path & path) -> std::expected< DynamicLibrary, DynamicLibraryError >
staticnodiscard

◆ GetLastErrorMessage()

std::string helios::utils::DynamicLibrary::GetLastErrorMessage ( )
staticnodiscardnoexcept

Gets the last platform-specific error message.

Returns
Error message string

Definition at line 106 of file dynamic_library.cpp.

◆ GetPlatformExtension()

constexpr std::string_view helios::utils::DynamicLibrary::GetPlatformExtension ( )
inlinestaticnodiscardconstexprnoexcept

Gets the platform-specific file extension for dynamic libraries.

Returns
".dll" on Windows, ".so" on Linux, ".dylib" on macOS

Definition at line 165 of file dynamic_library.hpp.

◆ GetPlatformPrefix()

constexpr std::string_view helios::utils::DynamicLibrary::GetPlatformPrefix ( )
inlinestaticnodiscardconstexprnoexcept

Gets the platform-specific library prefix.

Returns
Empty on Windows, "lib" on Unix-like systems

Definition at line 179 of file dynamic_library.hpp.

◆ GetSymbol()

template<typename T>
requires std::is_pointer_v<T>
auto helios::utils::DynamicLibrary::GetSymbol ( std::string_view name) const -> std::expected< T, DynamicLibraryError >
inlinenodiscard

Gets a typed function pointer from the library.

Template Parameters
TFunction pointer type
Parameters
nameName of the symbol to retrieve
Returns
Expected with function pointer on success, or error on failure

Definition at line 243 of file dynamic_library.hpp.

◆ GetSymbolAddress()

auto helios::utils::DynamicLibrary::GetSymbolAddress ( std::string_view name) const -> std::expected< void *, DynamicLibraryError >
nodiscard

Gets a raw symbol address from the library.

Parameters
nameName of the symbol to retrieve
Returns
Expected with void pointer on success, or error on failure

Definition at line 79 of file dynamic_library.cpp.

◆ Handle()

HandleType helios::utils::DynamicLibrary::Handle ( ) const
inlinenodiscardnoexcept

Gets the native handle of the loaded library.

Returns
Native handle, or kInvalidHandle if not loaded

Definition at line 152 of file dynamic_library.hpp.

◆ Load()

auto helios::utils::DynamicLibrary::Load ( const std::filesystem::path & path) -> std::expected< void, DynamicLibraryError >
nodiscard

Loads a dynamic library from the specified path.

Parameters
pathPath to the dynamic library
Returns
Expected with void on success, or error on failure

Definition at line 28 of file dynamic_library.cpp.

◆ Loaded()

bool helios::utils::DynamicLibrary::Loaded ( ) const
inlinenodiscardnoexcept

Checks if a library is currently loaded.

Returns
True if a library is loaded

Definition at line 136 of file dynamic_library.hpp.

◆ operator=() [1/2]

DynamicLibrary & helios::utils::DynamicLibrary::operator= ( const DynamicLibrary & )
delete

◆ operator=() [2/2]

DynamicLibrary & helios::utils::DynamicLibrary::operator= ( DynamicLibrary && other)
inlinenoexcept

Definition at line 203 of file dynamic_library.hpp.

◆ Path()

const std::filesystem::path & helios::utils::DynamicLibrary::Path ( ) const
inlinenodiscardnoexcept

Gets the path of the loaded library.

Returns
Path to the library, or empty if not loaded

Definition at line 144 of file dynamic_library.hpp.

◆ Reload()

auto helios::utils::DynamicLibrary::Reload ( ) -> std::expected< void, DynamicLibraryError >
inlinenodiscard

Reloads the library from the same path.

Unloads the current library and loads it again.

Returns
Expected with void on success, or error on failure

Definition at line 225 of file dynamic_library.hpp.

◆ Unload()

auto helios::utils::DynamicLibrary::Unload ( ) -> std::expected< void, DynamicLibraryError >
nodiscard

Unloads the currently loaded library.

Returns
Expected with void on success, or error on failure

Definition at line 55 of file dynamic_library.cpp.

Member Data Documentation

◆ kInvalidHandle

HandleType helios::utils::DynamicLibrary::kInvalidHandle = nullptr
staticconstexpr

Definition at line 75 of file dynamic_library.hpp.