te::core::Library Class Reference

A class for handling shared libraries (DLLs, SO, DyLibs). More...

#include <Library.h>

Inheritance diagram for te::core::Library:

Public Member Functions

void * getAddress (const char *symbol) const
 Return the address where the given symbol is loaded into memory. More...
 
const std::string & getFileName () const
 Return the shared library file name as informed in the constructor. More...
 
bool isLoaded () const
 Return true if the shared library is loaded otherwise return false. More...
 
 Library (const std::string &slib_file_name, const bool &delay_load=false)
 Load a new shared library. More...
 
void load ()
 Load the shared library to memory. More...
 
void unload ()
 Force the unload of the shared library from memory. More...
 
 ~Library ()
 The destructor automatically unloads from memory the shared library if it was not unloaded explicitly. More...
 

Static Public Member Functions

static void addSearchDir (const std::string &dir_name)
 Add the informed dir to the path used by the operational system to lookup for shared libraries. More...
 
static std::string getNativeName (const std::string &name)
 Given a shared library name without file extensions, prefixes and nor suffixes it will construct a library name according to the specifc platform. More...
 
static std::string getSearchPath ()
 Returns the system lookup path. More...
 
static void resetSearchPath ()
 Comes back the application lookup path to the original state, before any add_search_dir has been called. More...
 

Private Attributes

Impl * m_pimpl
 

Detailed Description

A class for handling shared libraries (DLLs, SO, DyLibs).

You can use this class to load any shared library and acces any address inside it.

If the shared library you are attempting to load has dependencies the other shared libraries must be in a folder reachable by the operational system.

On Mac OS X the environment variables PATH, DYLD_LIBRARY_PATH, DYLD_FALLBACK_LIBRARY_PATH and DYLD_FRAMEWORK_FALLBACK_PATH may be set to indicate the list of folders containing shared libraries.

On Linux the environment variables PATH and LD_LIBRARY_PATH may be used to indicate the folders searchable by the operational system.

On Windows you can set the environment variable PATH.

Note that this class has a special static method (or class method) named addSearchDir that can be used to control the behaviour of operational system when loading the shared library dependencies.

Warning
Shared libraries should not be loaded concurrently by multiple threads.

Definition at line 73 of file Library.h.

Constructor & Destructor Documentation

te::core::Library::Library ( const std::string &  slib_file_name,
const bool &  delay_load = false 
)

Load a new shared library.

The file name may be a relative or absolute path.

If just the name of the shared library is given the class will look for the library in the operational system specific locations.

Parameters
slib_file_nameThe library file name. The file name may contain the full library path.
delay_loadIf true the client object must call explicitly the load method before trying to access any symbol in the library.
Exceptions
LibraryLoadExceptionIt can throw an exception if delay_load is set to false and the library can not be loaded.
LibraryNameExceptionIt throws an exception if library name is empty or just white-spaces.
te::core::Library::~Library ( )

The destructor automatically unloads from memory the shared library if it was not unloaded explicitly.

Member Function Documentation

static void te::core::Library::addSearchDir ( const std::string &  dir_name)
static

Add the informed dir to the path used by the operational system to lookup for shared libraries.

Parameters
dir_nameA directory to be added to the lookup of shared libraries.
Exceptions
LibraryInvalidSearchPathExceptionIt throws an exception if the path couldn't be added to the search path.
Note
Not thread-safe.
For Linux and Mac OS X this method doesn't perform operations.
void* te::core::Library::getAddress ( const char *  symbol) const

Return the address where the given symbol is loaded into memory.

Parameters
symbolThe name of the symbol inside the library you are searching for. It may be for instance a function name.
Returns
The address where the symbol is loaded.
Exceptions
LibrarySymbolNotFoundExceptionIt throws an exception if it is not possible to locate the given symbol.
Note
Not thread-safe.
const std::string& te::core::Library::getFileName ( ) const

Return the shared library file name as informed in the constructor.

static std::string te::core::Library::getNativeName ( const std::string &  name)
static

Given a shared library name without file extensions, prefixes and nor suffixes it will construct a library name according to the specifc platform.

static std::string te::core::Library::getSearchPath ( )
static

Returns the system lookup path.

Exceptions
LibrarySearchPathExceptionIt throws an exception if the path couldn't be reset.
Note
Not thread-safe.
bool te::core::Library::isLoaded ( ) const

Return true if the shared library is loaded otherwise return false.

void te::core::Library::load ( )

Load the shared library to memory.

If the shared library was already loaded this method doesn't perform operations.

Exceptions
LibraryLoadExceptionIf the shared library can not be loaded to memory.
Note
Not thread-safe.
static void te::core::Library::resetSearchPath ( )
static

Comes back the application lookup path to the original state, before any add_search_dir has been called.

Exceptions
LibraryResetSearchPathExceptionIt throws an exception if the path couldn't be reset.
Note
Not thread-safe.
For Linux and Mac OS X this method doesn't perform operations.
void te::core::Library::unload ( )

Force the unload of the shared library from memory.

If the library wasn't previously loaded or if it was already unloaded this method doesn't perform operations.

Exceptions
LibraryUnloadExceptionIf the library can not be unload from memory this method raises an exception.
Note
Not thread-safe.

Member Data Documentation

Impl* te::core::Library::m_pimpl
private

Definition at line 171 of file Library.h.


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