Cogs.Foundation
|
Base class for managing libraries dynamically loaded at runtime. More...
#include <Module.h>
Public Types | |
enum class | LoadStatus { Unloaded , Loaded , Failed } |
Public Member Functions | |
virtual | ~Module ()=default |
virtual void | initPtrs () |
template<typename T > | |
void | getProcAddress (T &ptr, const char *procName) |
void * | getProcAddress (const char *procName) |
Retrieves a pointer to the named function. | |
bool | operator== (const Module &rhs) const |
Static Public Member Functions | |
static void | load (const std::string &path, LoadStatus &status, void *&handle) |
Attempts to load a shared library with the given name. | |
static void | unload (void *handle) |
template<typename T = Module> | |
static T | load (const std::string &path) |
Loads the named shared library. | |
static void * | getProcAddress (void *handle, const char *procName) |
Retrives the address of the named function within the shared library identified by the specified handle. | |
Public Attributes | |
LoadStatus | status = LoadStatus::Unloaded |
void * | handle = nullptr |
Protected Member Functions | |
template<typename RET , typename FN , typename... ARGS> | |
RET | Call (FN *fn, ARGS... args) const |
Base class for managing libraries dynamically loaded at runtime.
Derive a module implementation from this class with function pointers to library functions. Override the virtual function initPtrs to initialise them through repeated calls to getProcAddress, and then call them as you would any regular function pointer. (Or provide a real API in your class to hide the function pointers from your users and use the protected Call function to safely call them.)
Use the templated Module::load function to retrieve an instance of your Module derived class.
|
strong |
|
virtualdefault |
|
inlineprotected |
void * Cogs::Module::getProcAddress | ( | const char * | procName | ) |
Retrieves a pointer to the named function.
This function should typically be called from an overridden initPtrs implementation that will be called during Module loading. *============================================================================================
References getProcAddress(), and handle.
References Cogs::T.
Referenced by getProcAddress(), and Cogs::ModuleAPI< API >::initPtrs().
|
static |
Retrives the address of the named function within the shared library identified by the specified handle.
On Linux, this function merely forwards the request to the OS. *============================================================================================
On MacOS, this function merely forwards the request to the OS. *============================================================================================
On Windows, this function merely forwards the request to the OS. *============================================================================================
|
inlinevirtual |
Reimplemented in Cogs::ModuleAPI< API >.
Loads the named shared library.
If T is derived from Module (as opposed to being Module) an overloaded initPtrs function can be used to initialise pointers to functions exported from the library. *============================================================================================
References Cogs::T.
|
static |
Attempts to load a shared library with the given name.
The path can be a complete path to the library file, or just the filename of the library excluding any file extension such as DLL or SO. In the latter instance the library will be located for according to the host system's search criteria.
This function is primarily used to load Cogs extensions, but can be used to dynamically load any shared library. *============================================================================================
The path can be a complete path to the library file, or just the filename of the library excluding any file extension. In the latter instance the library will be located according to the host system's search criteria.
This function is primarily used to load Cogs extensions, but can be used to dynamically load any shared library. *============================================================================================
|
static |
void* Cogs::Module::handle = nullptr |
Referenced by getProcAddress(), Cogs::ModuleAPI< API >::initPtrs(), load(), Cogs::ModuleAPI< API >::ModuleAPI(), and operator==().
LoadStatus Cogs::Module::status = LoadStatus::Unloaded |
Referenced by load(), Cogs::ModuleAPI< API >::ModuleAPI(), and operator==().