OpenWalnut  1.4.0
Classes | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | Friends
WSharedLib Class Reference

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

#include <WSharedLib.h>

List of all members.

Classes

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

Public Member Functions

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

Static Public Member Functions

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

Private Types

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

Private Member Functions

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

Private Attributes

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

Friends

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

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@informatik.uni-leipzig.de>. For more details, see http://www.informatik.uni-leipzig.de/~hg/openbug .

Notes:
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

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

neutral function pointer type

Definition at line 155 of file WSharedLib.h.


Constructor & Destructor Documentation

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.
Notes:
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.

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 ( ) [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

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.

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().

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().

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().

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().

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.

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.

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().

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().

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

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

internal data

Definition at line 176 of file WSharedLib.h.

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

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: