Safir SDK Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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. More...
 
 ~DynamicLibraryLoader ()
 Destructor. More...
 
void Load (const std::string &libraryName, const bool unloadOnDestruction, const bool global=false)
 Load a library. More...
 
void Unload ()
 Unload the library. More...
 
template<class T >
T * GetFunction (const std::string &functionName)
 Find a function in the library. More...
 

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.

Constructor & Destructor Documentation

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

Constructor.

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

Destructor.

Member Function Documentation

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 boost::function object since otherwise things get very messy if you have to specify calling convention. But it easy to put the result into a boost function object. For example: boost::function<double(int,int)> func = lib.GetFunction<double(int,int)>("myfunc") And with specified calling convention on ms visual c++: boost::function<double(int,int)> func = lib.GetFunction<double __stdcall (int,int)>("myfunc") And with specified calling convention on gcc: boost::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.
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.
void Safir::Utilities::DynamicLibraryLoader::Unload ( )

Unload the library.