siplasplas
A library for C++ reflection and introspection
cpp::DynamicLibrary Class Reference

Provides a cross-platform interface to load symbols from shared libraries at runtime. More...

#include <siplasplas/utility/dynamiclibrary.hpp>

Classes

class  Symbol
 Represents a symbol loaded from a dynamic library. More...
 

Public Member Functions

SymbolgetSymbol (const std::string &name)
 Loads a symbol from this shared library. More...
 
void * handle () const
 OS-specific shared library handle. More...
 
const std::string & path () const
 Shared library path. See DynamicLibrary::load(). More...
 

Static Public Member Functions

static DynamicLibrary load (const std::string &libraryPath)
 Loads a shared library. More...
 

Detailed Description

Provides a cross-platform interface to load symbols from shared libraries at runtime.

DynamicLibrary is a RAII abstraction over a OS-specific shared library handle. The library handle is refcounted and automatically released when there are no more DynamicLibrary instances referencing it. Symbols can be loaded from the library using getSymbol() function.

Member Function Documentation

Symbol& cpp::DynamicLibrary::getSymbol ( const std::string &  name)

Loads a symbol from this shared library.

Library loaded symbols are cached by DynamicLibrary instances to avoid invoking the OS API again on an already loaded symbol. First call to getSymbol(<symbol name>) invokes DynamicLibrary::Symbol::loadSymbol() using *this as first argument. Consecuent calls read symbols from the cache.

Parameters
nameName of the symbol to load.
Returns
Reference to the cached symbol.
void* cpp::DynamicLibrary::handle ( ) const

OS-specific shared library handle.

Returns
void* pointer representing the OS-specific library handle.
static DynamicLibrary cpp::DynamicLibrary::load ( const std::string &  libraryPath)
static

Loads a shared library.

This function loads a shared library from the given path. It follows the path conventions of the underlying OS API, no special path handling is done from siplasplas side. On linux, after loading the library the dlinfo() API is invoked to get the right path to the library. DynamicLibrary class can only be instanced through this factory function.

Parameters
libraryPathPath to the shared library. Can be relative to the executable working directory or absolute. The given path could not be the same registred into the DynamicLibrary instance (See details).
Returns
A DynamicLibrary object representing the loaded library. If the library could not be loaded the function throws a std::runtime_error exception.
auto myLib = DynamicLibrary::load("myLib.dll");
Examples:
cmake/cmake-project.cpp.
const std::string& cpp::DynamicLibrary::path ( ) const

Shared library path. See DynamicLibrary::load().

Returns
const reference to the string containing the path to the library.

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