Library Class Reference

#include <Pt/System/Library.h>

Shared library loader. More...

Public Member Functions

 Library ()
 Default Constructor which does not load a library.
 
 Library (const Path &path)
 Loads a shared library. More...
 
 Library (const Library &other)
 Copy constructor.
 
Libraryoperator= (const Library &other)
 Assignment operator.
 
 ~Library ()
 The destructor unloads the shared library from memory.
 
Libraryopen (const Path &path)
 Loads a shared library. More...
 
void close ()
 Closes the shared library.
 
void * operator[] (const char *symbol) const
 Resolves the symbol symbol from the shared library Returns the address of the symbol or 0 if it was not found.
 
void * resolve (const char *symbol) const
 Resolves the symbol symbol from the shared library Returns the address of the symbol or 0 if it was not found.
 
 operator const void * () const
 Returns null if invalid.
 
Symbol getSymbol (const char *symbol) const
 Resolves the symbol symbol from the shared library. More...
 
bool operator! () const
 Returns true if invalid.
 
const Pathpath () const
 Returns the path to the shared library image.
 

Static Public Member Functions

static const char * suffix ()
 Returns the extension for shared libraries. More...
 
static const char * prefix ()
 Returns the prefix for shared libraries. More...
 

Detailed Description

The Pt::System::Library class can be used to dynamically load shared libraries and resolve symbols from it. It also provides the static functions prefix() and suffix(), which allow to build library names in a portable way. The next example shows how to load a library with the basename "MyLib" at runtime and how to retrieve the address of the function "myFunction":

typedef int (*MyFunc)();
Pt::System::Path libPath = "MyLib";
Pt::System::Library library(libPath);
Pt::System::Symbol symbol = library.getSymbol("myFunction");
MyFunc func = reinterpret_cast<MyFunc>(symbol.sym());
int result = func();

The constructor of the Library class will try to load the library at the given path. If no library could be found, the path is extended by the platform-specific library extension first, and then also by the shared library prefix. If no library could be found at either path, an AccessFailed exception is thrown. The function getSymbol() returns a Symbol object when the library symbol could be resolved or a SymbolNotFound exception, if the symbol name could not be found. The actual address of the library symbol is returned by sym(). Alternatively, the index operator can be used to load symbols, which will return the address of the symbol as a pointer to void or a nullptr on failure. Note, that standard C++ does not allow to cast void pointers to function pointers, but nearly all runtimes implement that as an extension.

Constructor & Destructor Documentation

◆ Library()

Library ( const Path path)
explicit

If a file could not be found at the given path, the path will be extended by the platform-specific shared library extension first and then also by the shared library prefix. If still no file can be found an exception of type AccessFailed is thrown. Otherwise, the library is loaded immediately.

Member Function Documentation

◆ open()

Library& open ( const Path path)

If a file could not be found at the given path, the path will be extended by the platform-specific shared library extension first and then also by the shared library prefix. If still no file can be found an exception of type AccessFailed is thrown. Otherwise, the library is loaded immediately. Calling this method twice might close the previously loaded library.

◆ getSymbol()

Symbol getSymbol ( const char *  symbol) const

Throws SymbolNotFound if the symbol could not be resolved.

◆ suffix()

static const char* suffix ( )
static

Returns ".so" on Linux, ".dll" on Windows.

◆ prefix()

static const char* prefix ( )
static

Returns "lib" on Linux, "" on Windows

Shared library loader.
Definition: Library.h:107
Symbol resolved from a shared library.
Definition: Library.h:205
Represents a path in the file-system.
Definition: Path.h:48
void * sym() const
Returns the symbol address.
Definition: Library.h:221