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

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

#include <siplasplas/utility/dynamiclibrary.hpp>

Public Member Functions

const std::string & name () const
 Returns the name of the symbol. More...
 
void * handle ()
 Returns the raw address of the loaded symbol. More...
 
const void * handle () const
 Returns the raw address of the loaded symbol. More...
 
template<typename T >
get ()
 Returns the symbol converted to the required type. More...
 
template<typename T >
T & getObject ()
 Returns the symbol interpreted as a reference to an object. More...
 
template<typename T >
const T & getObject () const
 Returns the symbol interpreted as a const reference to an object. More...
 

Friends

class DynamicLibrary
 

Detailed Description

Represents a symbol loaded from a dynamic library.

This class provides access to a loaded symbol from an external library. The class wraps the raw pointer to the loaded symbol returned from the OS-specific API. You can access the loaded symbol address directly or use the convenient get() and getObject() functions that already apply type conversions to the symbol. Symbols are associated with a DynamicLibrary instanced. When its parent dynamic library goes out of scope symbols are invalidated and accessing them has undefined behavior.

// Load "external_function" symbol from our shared library:
auto externalFunction = DynamicLibrary::Symbol::load(myLib, "external_function");
// Read the symbol as a function pointer:
auto fptr = externalFunction.get<void(int)>();
// Invoke the external library function:
fptr(42);

Member Function Documentation

template<typename T >
T cpp::DynamicLibrary::Symbol::get ( )
inline

Returns the symbol converted to the required type.

Template Parameters
TType to convert the symbol to. Must be a pointer type.
Returns
Symbol value converted to T.
template<typename T >
T& cpp::DynamicLibrary::Symbol::getObject ( )
inline

Returns the symbol interpreted as a reference to an object.

This function is useful when loading variables from the shared library. Loading objects with automatic or dynamic storage duration has undefined behavior.

Template Parameters
TValue type to convert the symbol to.
Returns
Lvalue reference of type T to the symbol.
// Load a global variable symbol from a DynamicLibrary:
auto myLibGlobalVariable = myLibrary.getSymbol("my_lib_global_variable");
// Interpret as an int and read its value:
std::cout << "myLib::my_lib_global_variable: " << myLibGlobalVariable.getObject<int>();
template<typename T >
const T& cpp::DynamicLibrary::Symbol::getObject ( ) const
inline

Returns the symbol interpreted as a const reference to an object.

This function is useful when loading variables from the shared library. Loading objects with automatic or dynamic storage duration has undefined behavior.

Template Parameters
TValue type to convert the symbol to.
Returns
Lvalue const reference of type T to the symbol.
// Load a global variable symbol from a DynamicLibrary:
auto myLibGlobalVariable = myLibrary.getSymbol("my_lib_global_variable");
// Interpret as an int and read its value:
std::cout << "myLib::my_lib_global_variable: " << myLibGlobalVariable.getObject<int>();
void* cpp::DynamicLibrary::Symbol::handle ( )

Returns the raw address of the loaded symbol.

Returns
Pointer to the symbol.
const void* cpp::DynamicLibrary::Symbol::handle ( ) const

Returns the raw address of the loaded symbol.

Returns
Const pointer to the symbol.
const std::string& cpp::DynamicLibrary::Symbol::name ( ) const

Returns the name of the symbol.

Returns
std::string containing the name of the symbol.

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