OpenWalnut  1.5.0dev
Classes | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | Friends | List of all members
WSharedLib Class Reference

This class loads shared libraries and provides function pointers. More...

#include <WSharedLib.h>

+ Collaboration diagram for WSharedLib:

Classes

struct  data
 Simple class holding an opened library. More...
 

Public Member Functions

 WSharedLib (boost::filesystem::path lib)
 Constructor. More...
 
 WSharedLib (const WSharedLib &rhs)
 Copies this instance by increasing the reference counter of the loaded library by 1. More...
 
virtual ~WSharedLib ()
 Destructor. More...
 
WSharedLiboperator= (const WSharedLib &rhs)
 Copy assignment for shared libraries. More...
 
template<typename FuncType >
void fetchFunction (const std::string &name, FuncType &func) const
 Search for a function in the shared library. More...
 
bool existsFunction (const std::string &name) const
 Check whether the function exists. More...
 
template<typename PtrType >
void fetchVariable (const std::string &name, PtrType &variable) const
 Search for an variable in the shared library. More...
 
std::string getLibraryName ()
 Returns the filename of the library without path. More...
 

Static Public Member Functions

static std::string getSystemPrefix ()
 Returns the prefix used for libraries on the system. More...
 
static std::string getSystemSuffix ()
 Returns the suffix for libraries used on the system. More...
 
static std::string getSystemLibPath ()
 Returns the default path for libraries on the current system. More...
 

Private Types

typedef void(* func_ptr_type) (void)
 neutral function pointer type More...
 

Private Member Functions

func_ptr_type findFunction (const std::string &name) const
 Find the specified function pointer in the library. More...
 
void * findVariable (const std::string &name) const
 Find the specified symbol in the library. More...
 

Private Attributes

datam_data
 internal data More...
 
boost::filesystem::path m_libPath
 path to lib More...
 

Friends

void swap (WSharedLib &lhs, WSharedLib &rhs)
 Swap to shared libraries. More...
 

Detailed Description

This class loads shared libraries and provides function pointers.

This is especially useful for dynamic loading of shared libraries during runtime. This works on Windows, Linux and Mac OS and is based on the openbug shared_lib implementation by Christian Heine heine.nosp@m.@inf.nosp@m.ormat.nosp@m.ik.u.nosp@m.ni-le.nosp@m.ipzi.nosp@m.g.de.

Note
This class performs locking so that under any system variables of shared_lib may be used in multi-threaded environments.
Warning
Because the POSIX standard does not enforce thread safety for the functions dlopen, dlclose, dlerror, and dlsym, these should not be used simultaneously with variables of this class.

Definition at line 44 of file WSharedLib.h.

Member Typedef Documentation

◆ func_ptr_type

typedef void(* WSharedLib::func_ptr_type) (void)
private

neutral function pointer type

Definition at line 155 of file WSharedLib.h.

Constructor & Destructor Documentation

◆ WSharedLib() [1/2]

WSharedLib::WSharedLib ( boost::filesystem::path  lib)
explicit

Constructor.

Loads the specified library.

Parameters
libthe library to load. Can be a DLL,SO or DYLIB (depending on system). This can be an absolut or relative path. Otherwise standard library search directory may be searched.
Note
If the shared library is already loaded, this constructor just increases its reference count. This is detected even if different paths were used (e.g. "./somelib.so", "../libs/somelib.so").
Exceptions
WLibraryLoadFailedif the lib could not be loaded. Maybe because of file not found or link errors.

Definition at line 267 of file WSharedLib.cpp.

◆ WSharedLib() [2/2]

WSharedLib::WSharedLib ( const WSharedLib rhs)

Copies this instance by increasing the reference counter of the loaded library by 1.

Parameters
rhsthe other Lib.

Definition at line 273 of file WSharedLib.cpp.

◆ ~WSharedLib()

WSharedLib::~WSharedLib ( )
virtual

Destructor.

Decreases the reference counter and unloads the library if the reference count drops to zero.

Definition at line 279 of file WSharedLib.cpp.

References m_data.

Member Function Documentation

◆ existsFunction()

bool WSharedLib::existsFunction ( const std::string &  name) const

Check whether the function exists.

Parameters
namethe name of the function
Returns
true if it exists.

Definition at line 306 of file WSharedLib.cpp.

References WSharedLib::data::existsFunction(), and m_data.

+ Here is the call graph for this function:

◆ fetchFunction()

template<typename FuncType >
void WSharedLib::fetchFunction ( const std::string &  name,
FuncType &  func 
) const

Search for a function in the shared library.

Template Parameters
FuncTypea function type
Parameters
namethe name of the function
funcwill be set to the function pointer
Exceptions
WLibraryFetchFailedif the symbol was not found
Warning
type unsafe, make sure the symbol actually is of the proper type

Definition at line 186 of file WSharedLib.h.

References findFunction().

+ Here is the call graph for this function:

◆ fetchVariable()

template<typename PtrType >
void WSharedLib::fetchVariable ( const std::string &  name,
PtrType &  variable 
) const

Search for an variable in the shared library.

Template Parameters
PtrTypea pointer type
Parameters
namethe name of the variable
variablewill be set to the variable pointer
Exceptions
WLibraryFetchFailedif the symbol was not found
Warning
type unsafe, make sure the symbol actually is of the proper type

Definition at line 192 of file WSharedLib.h.

References findVariable().

+ Here is the call graph for this function:

◆ findFunction()

WSharedLib::func_ptr_type WSharedLib::findFunction ( const std::string &  name) const
private

Find the specified function pointer in the library.

Parameters
namethe symbol to search
Returns
the pointer to the symbol as function pointer

Definition at line 296 of file WSharedLib.cpp.

References WSharedLib::data::findFunction(), and m_data.

Referenced by fetchFunction().

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

◆ findVariable()

void * WSharedLib::findVariable ( const std::string &  name) const
private

Find the specified symbol in the library.

Parameters
namethe symbol to search
Returns
the pointer to the symbol as function pointer.

Definition at line 301 of file WSharedLib.cpp.

References WSharedLib::data::findVariable(), and m_data.

Referenced by fetchVariable().

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

◆ getLibraryName()

std::string WSharedLib::getLibraryName ( )

Returns the filename of the library without path.

Returns
the filename.

Definition at line 326 of file WSharedLib.cpp.

References m_libPath.

◆ getSystemLibPath()

std::string WSharedLib::getSystemLibPath ( )
static

Returns the default path for libraries on the current system.

This is the directory where to search for .so,.dll or .dylib files. On Unix, this will be "../lib", on Windows ".".

Returns
the path on the system.

Definition at line 321 of file WSharedLib.cpp.

◆ getSystemPrefix()

std::string WSharedLib::getSystemPrefix ( )
static

Returns the prefix used for libraries on the system.

On Unix this mostly is "lib".

Returns
the prefix.

Definition at line 311 of file WSharedLib.cpp.

Referenced by WModuleLoader::getModulePrefix().

+ Here is the caller graph for this function:

◆ getSystemSuffix()

std::string WSharedLib::getSystemSuffix ( )
static

Returns the suffix for libraries used on the system.

On Unix this mostly is "so", Windows uses "dll" and Mac something like "dylib".

Returns
the suffix.

Definition at line 316 of file WSharedLib.cpp.

Referenced by WModuleLoader::load().

+ Here is the caller graph for this function:

◆ operator=()

WSharedLib & WSharedLib::operator= ( const WSharedLib rhs)

Copy assignment for shared libraries.

Parameters
rhsthe one to assign
Returns
this instance copied from the specified one.

Definition at line 284 of file WSharedLib.cpp.

References swap.

Friends And Related Function Documentation

◆ swap

void swap ( WSharedLib lhs,
WSharedLib rhs 
)
friend

Swap to shared libraries.

Parameters
lhsthe one
rhsthe other

Definition at line 291 of file WSharedLib.cpp.

Referenced by operator=().

Member Data Documentation

◆ m_data

data* WSharedLib::m_data
private

internal data

Definition at line 179 of file WSharedLib.h.

Referenced by existsFunction(), findFunction(), findVariable(), and ~WSharedLib().

◆ m_libPath

boost::filesystem::path WSharedLib::m_libPath
private

path to lib

Definition at line 182 of file WSharedLib.h.

Referenced by getLibraryName().


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