casa
$Rev:20696$
|
Class to handle loadig of dynamic libraries. More...
#include <DynLib.h>
Public Member Functions | |
DynLib (const std::string &library, const std::string &prefix=std::string(), const std::string &funcName=std::string(), bool closeOnDestruction=True) | |
Load the dynamic library. | |
DynLib (const std::string &library, Bool closeOnDestruction, const std::string &prefix="lib", const std::string &suffix=".so") | |
Load the dynamic library with the given name, prefix, and suffix. | |
~DynLib () | |
Close the dynamic library if told so in the constructor. | |
void * | getFunc (const std::string &funcName) |
Get a pointer to a function in the dynamic library. | |
void * | getHandle () const |
Get the dynamic library handle. | |
Private Member Functions | |
void | open (const std::string &name) |
Open (load)the dynamic library. | |
void | close () |
Close (unload) the dynamic library (if opened). | |
Private Attributes | |
void * | itsHandle |
Bool | itsDoClose |
Class to handle loadig of dynamic libraries.
<h3>Intended use:</h3> Public interface <h3>Prerequisite</h3><ul> <li> Basic knowledge of the dlopen function family </ul> <h3>Synopsis</h3> This class makes it possible to load a dynamic library and execute an initialization function. Furthermore one can get a pointer to any function in the dynamic library and close the library. It is a wrapper around functions dlopen, dlsym, and dlclose. If dlopen and so is not supported on a platform, the class acts as if the shared library could not be found. <h3>Example</h3> @anchor DynLib_example
DynLib dl("bitflagsengine", "register_bitflagsengine"); AlwaysAssert (dl.getHandle());
loads the executes library bitflagsengine
and executes the given register initialization function.
dlopen is a standard UNIX system call, but some operating systems do not support it or have different function names (notably Windows). In this way use of dynamic libraries is centralized and can easily b tailored as needed.
casa::DynLib::DynLib | ( | const std::string & | library, |
const std::string & | prefix = std::string() , |
||
const std::string & | funcName = std::string() , |
||
bool | closeOnDestruction = True |
||
) |
Load the dynamic library.
It is tried with prefixes "lib" and prefix
and suffixes ".so" and ".dylib". If not loaded successfully, the internal handle is NULL.
If a non-empty funcName is given, that function is looked up and executed. Its signature must be void func()
. Note that the function name should not be mangled, thus declared extern "C"
. An exception is thrown if the library is loaded successfully, but funcName
could not be found.
If closeOnDestruction=True
, the dynamic library is closed on destruction of the DynLib object.
casa::DynLib::DynLib | ( | const std::string & | library, |
Bool | closeOnDestruction, | ||
const std::string & | prefix = "lib" , |
||
const std::string & | suffix = ".so" |
||
) |
Load the dynamic library with the given name, prefix, and suffix.
If not loaded successfully, the internal handle is NULL.
If closeOnDestruction=True
, the dynamic library is closed when the DynLib object is destructed.
Close the dynamic library if told so in the constructor.
void casa::DynLib::close | ( | ) | [private] |
Close (unload) the dynamic library (if opened).
void* casa::DynLib::getFunc | ( | const std::string & | funcName | ) |
Get a pointer to a function in the dynamic library.
The pointer has to be casted with a reinterpret_cast to a function pointer with the correct signature. When compiling with -pedantic the compiler will give a warning for such a cast, because on some systems (in particular some micro-controllers) a data pointer differs from a function pointer. However, that problem cannot be solved. For example:
typedef Int MyFunc(Int, Int); void* initfunc = DynLib::getFunc (mod, ("register_"+name).c_str()); if (initFunc) { MyFunc* func = reinterpret_cast<MyFunc*>(initfunc); Int result = func(1,2); }
casts to a function returning Int and taking two Ints.
A null pointer is returned if the function could not be found.
void* casa::DynLib::getHandle | ( | ) | const [inline] |
void casa::DynLib::open | ( | const std::string & | name | ) | [private] |
Open (load)the dynamic library.
Bool casa::DynLib::itsDoClose [private] |
void* casa::DynLib::itsHandle [private] |
Definition at line 143 of file DynLib.h.
Referenced by getHandle().