DynamicLibrary Class Reference

The DynamicLibrary class provides a mechanism to load libraries at runtime. More...

#include <ArmarXCore/core/system/DynamicLibrary.h>

+ Inheritance diagram for DynamicLibrary:

Public Member Functions

 DynamicLibrary ()
 
std::string getErrorMessage () noexcept
 
std::filesystem::path getLibraryFilename () noexcept
 
template<class SymbolType >
SymbolType getSymbol (const std::string &functionName)
 Retrieves a symbol (mostly a function) from the loaded library. More...
 
bool isLibraryLoaded () noexcept
 Checks if a library is currently loaded. More...
 
void load (std::filesystem::path libPath)
 Loads a shared library from the specified path. More...
 
void reload ()
 Reloads the current library. More...
 
void setFlags (int newFlags)
 Sets the shared library opening flags and reloads the library, if a library is already loaded. More...
 
void setUnloadOnDestruct (bool unload)
 
void unload ()
 Unloads library. More...
 
 ~DynamicLibrary () override
 
- Public Member Functions inherited from Logging
SpamFilterDataPtr deactivateSpam (float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
 disables the logging for the current line for the given amount of seconds. More...
 
MessageTypeT getEffectiveLoggingLevel () const
 
 Logging ()
 
void setLocalMinimumLoggingLevel (MessageTypeT level)
 With setLocalMinimumLoggingLevel the minimum verbosity-level of log-messages can be set. More...
 
void setTag (const LogTag &tag)
 
void setTag (const std::string &tagName)
 
virtual ~Logging ()
 

Static Public Member Functions

static std::string GetSharedLibraryFileExtension ()
 

Additional Inherited Members

- Protected Member Functions inherited from Logging
bool checkLogLevel (MessageTypeT level) const
 
const LogSenderPtrgetLogSender () const
 Retrieve log sender. More...
 
LogSenderPtr loghelper (const char *file, int line, const char *function) const
 
- Protected Attributes inherited from Logging
MessageTypeT minimumLoggingLevel
 
SpamFilterDataPtr spamFilter
 
LogTag tag
 

Detailed Description

The DynamicLibrary class provides a mechanism to load libraries at runtime.

A quick usage example looks as follows:

std::string relPath = "libArmarXCore.so";
std::string path;
CMakePackageFinder package("ArmarXCore");
if (!ArmarXDataPath::getAbsolutePath(relPath, path, {package.getLibraryPaths()}))
return;
ARMARX_INFO_S << "Loading lib from " << path;
// Load the library
lib.load(path);
if (!lib.isLibraryLoaded())
{
throw LocalException("Could not load library ") << path;
}
// create typedef for the function
typedef void (*FunctionType)();
// retrieve function from library
FunctionType handleExceptionsFunc = lib.getSymbol<FunctionType>("handleExceptions");
// the function can be called now
try
{
throw LocalException("Test Exception");
}
catch (...)
{
handleExceptionsFunc();
}
// Unload lib (also done in destructor of DynamicLibrary)
lib.unload();

Definition at line 49 of file DynamicLibrary.h.

Constructor & Destructor Documentation

◆ DynamicLibrary()

Definition at line 29 of file DynamicLibrary.cpp.

◆ ~DynamicLibrary()

~DynamicLibrary ( )
override

Definition at line 37 of file DynamicLibrary.cpp.

+ Here is the call graph for this function:

Member Function Documentation

◆ getErrorMessage()

std::string getErrorMessage ( )
noexcept

Definition at line 174 of file DynamicLibrary.cpp.

◆ getLibraryFilename()

std::filesystem::path getLibraryFilename ( )
noexcept

Definition at line 168 of file DynamicLibrary.cpp.

◆ GetSharedLibraryFileExtension()

std::string GetSharedLibraryFileExtension ( )
static

Definition at line 157 of file DynamicLibrary.cpp.

+ Here is the caller graph for this function:

◆ getSymbol()

SymbolType getSymbol ( const std::string &  functionName)

Retrieves a symbol (mostly a function) from the loaded library.

The requested function needs to be exported with "extern "C"".

Template Parameters
SymbolTypetype of the requested symbol
Parameters
functionNamename of the requested function
Exceptions
exceptions::local::DynamicLibraryException
Returns
requested symbol

Definition at line 133 of file DynamicLibrary.h.

◆ isLibraryLoaded()

bool isLibraryLoaded ( )
noexcept

Checks if a library is currently loaded.

Returns
true if a library is loaded.

Definition at line 137 of file DynamicLibrary.cpp.

+ Here is the caller graph for this function:

◆ load()

void load ( std::filesystem::path  libPath)

Loads a shared library from the specified path.

Throws exceptions::local::DynamicLibraryException on error.

Parameters
libPathPath to the library. Can be relative to ArmarXDataPath's paths.
Exceptions
exceptions::local::DynamicLibraryException
See also
ArmarXDataPath

Definition at line 54 of file DynamicLibrary.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reload()

void reload ( )

Reloads the current library.

Useful, if the library has changed since last loading.

Exceptions
exceptions::local::DynamicLibraryException

Definition at line 131 of file DynamicLibrary.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setFlags()

void setFlags ( int  newFlags)

Sets the shared library opening flags and reloads the library, if a library is already loaded.

Parameters
newFlagsflags of ::dlopen()
Exceptions
exceptions::local::DynamicLibraryException

Definition at line 147 of file DynamicLibrary.cpp.

+ Here is the call graph for this function:

◆ setUnloadOnDestruct()

void setUnloadOnDestruct ( bool  unload)

Definition at line 142 of file DynamicLibrary.cpp.

+ Here is the call graph for this function:

◆ unload()

void unload ( )

Unloads library.

User has to make sure that all references to the symbols in this library are deleted!

Exceptions
exceptions::local::DynamicLibraryException

Definition at line 104 of file DynamicLibrary.cpp.

+ Here is the caller graph for this function:

The documentation for this class was generated from the following files:
armarx::DynamicLibrary::DynamicLibrary
DynamicLibrary()
Definition: DynamicLibrary.cpp:29
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
armarx::ArmarXDataPath::getAbsolutePath
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
Definition: ArmarXDataPath.cpp:111