libzed 1.9.9
A general-purpose library for quick and simple data manipulation.
 
Loading...
Searching...
No Matches
z::file::library Class Reference

A class for loading dynamic libraries. More...

#include <library.hpp>

Public Member Functions

 library () noexcept
 Default empty constructor.
 
 ~library () noexcept
 Destructor frees any loaded library.
 
bool load (const zpath &fileName, bool autoExtension=true) noexcept
 Load a dynamic library with the given file name.
 
bool unload () noexcept
 Unload the dynamic library.
 
bool good () noexcept
 Get whether the library has been loaded.
 
bool bad () noexcept
 Get whether the library has not been loaded.
 
template<typename T >
T * symbol (const zpath &symbolName) noexcept
 Get a pointer to the symbol with the given name.
 
template<typename T >
std::function< T > function (const zpath &symbolName) noexcept
 Get a pointer to the function with the given name.
 

Detailed Description

A class for loading dynamic libraries.

This class is meant to allow an easy and platform-independent way to dynamically load libraries at run time. Note that if this is compiled for Linux, the linker flag -ldl must be included.

Examples
loadLib.cpp.

Member Function Documentation

◆ bad()

bool z::file::library::bad ( )
noexcept

Get whether the library has not been loaded.

Returns
False if the library has been loaded. True otherwise.
See also
good()

◆ function()

template<typename T >
std::function< T > z::file::library::function ( const zpath symbolName)
inlinenoexcept

Get a pointer to the function with the given name.

This is similar to symbol(), except the symbol is assumed to be a function.

Warning
It is up to the caller to indicate the correct function signature at compile time. Not doing so can cause crashes.

Example usage: To get a function of the form bool func(int, float), call this_lib.function<bool(int, float)>("symbol_name"). To get a function of the form void func(), call this_lib.function<void()>("symbol_name").

Parameters
symbolNamethe name of the symbol to retrieve.
Returns
If a symbol with the given name was found, returns a pointer to the symbol. Otherwise, if the symbol was not found or the library hasn't been loaded, returns NULL.
Examples
loadLib.cpp.

◆ good()

bool z::file::library::good ( )
noexcept

Get whether the library has been loaded.

Returns
True if the library has been loaded. False otherwise.
See also
bad()

◆ load()

bool z::file::library::load ( const zpath fileName,
bool  autoExtension = true 
)
noexcept

Load a dynamic library with the given file name.

Parameters
fileNamethe path of the library to load.
autoExtensionIf true, automatically append the extension (".so" or ".dll" onto the file name). Useful for loading libraries in a platform-independent way.
Returns
True if the library loaded successfully. False otherwise.
Examples
loadLib.cpp.

◆ symbol()

template<typename T >
T * z::file::library::symbol ( const zpath symbolName)
inlinenoexcept

Get a pointer to the symbol with the given name.

Warning
It is up to the caller to indicate the correct data type at compile time. Not doing so can cause crashes.
Parameters
symbolNamethe name of the symbol to retrieve.
Returns
If a symbol with the given name was found, returns a pointer to the symbol. Otherwise, if the symbol was not found or the library hasn't been loaded, returns NULL.

◆ unload()

bool z::file::library::unload ( )
noexcept

Unload the dynamic library.

Returns
False if unable to unload previously loaded library. True otherwise.
Examples
loadLib.cpp.

The documentation for this class was generated from the following file: