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.
template<typename PtrType >
void fetchVariable (const std::string &name, PtrType &variable) const
 Search for an variable in the shared library.

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

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 48 of file WSharedLib.h.


Member Typedef Documentation

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

neutral function pointer type

Definition at line 146 of file WSharedLib.h.


Constructor & Destructor Documentation

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

Constructor.

Loads the specified library.

Parameters:
lib the 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:
WLibraryLoadFailed if the lib could not be loaded. Maybe because of file not found or link errors.

Definition at line 239 of file WSharedLib.cpp.

WSharedLib::WSharedLib ( const WSharedLib rhs  ) 

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

Parameters:
rhs the other Lib.

Definition at line 244 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 249 of file WSharedLib.cpp.

References m_data.


Member Function Documentation

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

Search for a function in the shared library.

Template Parameters:
FuncType a function type
Parameters:
name the name of the function
func will be set to the function pointer
Exceptions:
WLibraryFetchFailed if the symbol was not found
Warning:
type unsafe, make sure the symbol actually is of the proper type

Definition at line 174 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:
PtrType a pointer type
Parameters:
name the name of the variable
variable will be set to the variable pointer
Exceptions:
WLibraryFetchFailed if the symbol was not found
Warning:
type unsafe, make sure the symbol actually is of the proper type

Definition at line 180 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:
name the symbol to search
Returns:
the pointer to the symbol as function pointer

Definition at line 266 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:
name the symbol to search
Returns:
the pointer to the symbol as function pointer.

Definition at line 271 of file WSharedLib.cpp.

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

Referenced by fetchVariable().

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 300 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 289 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 294 of file WSharedLib.cpp.

Referenced by WModuleLoader::load().

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

Copy assignment for shared libraries.

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

Definition at line 254 of file WSharedLib.cpp.

References swap.


Friends And Related Function Documentation

void swap ( WSharedLib lhs,
WSharedLib rhs 
) [friend]

Swap to shared libraries.

Parameters:
lhs the one
rhs the other

Definition at line 261 of file WSharedLib.cpp.

Referenced by operator=().


Member Data Documentation

internal data

Definition at line 167 of file WSharedLib.h.

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


The documentation for this class was generated from the following files:
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends