Safir SDK Core
Loading...
Searching...
No Matches
Safir::Utilities::DynamicLibraryLoader Class Reference

This class provides a simple wrapper around dynamic loading functionality of the operating system. More...

#include <Safir/Utilities/DynamicLibraryLoader.h>

Public Member Functions

 DynamicLibraryLoader ()
 Constructor.
 
 ~DynamicLibraryLoader ()
 Destructor.
 
void Load (const std::string &libraryName, const bool unloadOnDestruction, const bool global=false)
 Load a library.
 
void Load (const std::string &libraryName, const std::string &path, const bool unloadOnDestruction, const bool global=false)
 Load a library from a given location.
 
void Unload ()
 Unload the library.
 
template<class T >
T * GetFunction (const std::string &functionName)
 Find a function in the library.
 

Detailed Description

This class provides a simple wrapper around dynamic loading functionality of the operating system.

E.g. dlopen/dlsym of *nix and LoadLibrary/GetProcAddress of Win32.

Deprecated
This class lacks functionality and flexibility. A more competent alternative would be Boost.DLL.

Constructor & Destructor Documentation

◆ DynamicLibraryLoader()

Safir::Utilities::DynamicLibraryLoader::DynamicLibraryLoader ( )

Constructor.

◆ ~DynamicLibraryLoader()

Safir::Utilities::DynamicLibraryLoader::~DynamicLibraryLoader ( )

Destructor.

Member Function Documentation

◆ GetFunction()

template<class T >
T * Safir::Utilities::DynamicLibraryLoader::GetFunction ( const std::string & functionName)

Find a function in the library.

Attempt to load the specified function and return it as a function pointer with the specified signature. The return type is a raw function pointer rather than a std::function object since otherwise things get very messy if you have to specify calling convention. But it easy to put the result into a function object. For example: std::function<double(int,int)> func = lib.GetFunction<double(int,int)>("myfunc") And with specified calling convention on ms visual c++: std::function<double(int,int)> func = lib.GetFunction<double __stdcall (int,int)>("myfunc") And with specified calling convention on gcc: std::function<double(int,int)> func = lib.GetFunction<double __attribute__(stdcall) (int,int)>("myfunc")

Parameters
[in]functionNameName of the function to load
Exceptions
std::logic_errorIf function cannot be found.

◆ Load() [1/2]

void Safir::Utilities::DynamicLibraryLoader::Load ( const std::string & libraryName,
const bool unloadOnDestruction,
const bool global = false )

Load a library.

Attempt to load the specified library dynamically.

Parameters
[in]libraryNameThe name of the library to load. On linux "lib" and ".so" are added to the beginning and end of the name, and on windows ".dll" is appended.
[in]unloadOnDestructionIf this is true the library will be unloaded when the object is destroyed. This will invalidate any function pointers.
[in]globalUse RTLD_GLOBAL flag when loading on *nix.
Exceptions
std::logic_errorIf library cannot be found or cannot be not loaded.

◆ Load() [2/2]

void Safir::Utilities::DynamicLibraryLoader::Load ( const std::string & libraryName,
const std::string & path,
const bool unloadOnDestruction,
const bool global = false )

Load a library from a given location.

Attempt to load the specified library dynamically.

Parameters
[in]libraryNameThe name of the library to load. On linux "lib" and ".so" are added to the beginning and end of the name, and on windows ".dll" is appended.
[in]pathLocation to load the library from.
[in]unloadOnDestructionIf this is true the library will be unloaded when the object is destroyed. This will invalidate any function pointers.
[in]globalUse RTLD_GLOBAL flag when loading on *nix.
Exceptions
std::logic_errorIf library cannot be found or cannot be not loaded.

◆ Unload()

void Safir::Utilities::DynamicLibraryLoader::Unload ( )

Unload the library.