casacore
|
#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. More... | |
DynLib (const std::string &library, const std::string &prefix, const std::string &version, const std::string &funcName, bool closeOnDestruction=True) | |
The same as above, but it is tried with and without the given version (in that order). More... | |
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. More... | |
~DynLib () | |
Close the dynamic library if told so in the constructor. More... | |
void * | getFunc (const std::string &funcName) |
Get a pointer to a function in the dynamic library. More... | |
void * | getHandle () const |
Get the dynamic library handle. More... | |
const std::string & | getError () const |
Get the possible error. More... | |
Private Member Functions | |
void | attach (const std::string &name, const std::string &prefix, const std::string &version, const std::string &funcName) |
Try to open the library with some prefixes, suffixes and versions and to execute the initialization function. More... | |
std::string | tryOpen (const std::string &name, const std::string &libdir, const std::string &prefix, const std::string &version) |
Try to open the library with some prefixes, suffixes and versions If successful, itsHandle is filled and the full library name is returned. More... | |
void | open (const std::string &name) |
Open (load) the dynamic library. More... | |
void | close () |
Close (unload) the dynamic library (if opened). More... | |
std::string | tryCasacorePath (const std::string &library, const std::string &prefix, const std::string &version) |
Try if the library can be opened using CASACORE_LDPATH. More... | |
Private Attributes | |
void * | itsHandle |
Bool | itsDoClose |
std::string | itsError |
Class to handle loading of dynamic libraries
Public interface
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.
The search path of the shared library is as follows:
It is a wrapper around functions dlopen, dlsym, and dlclose. If dlopen and so are not supported on a platform, the class acts as if the shared library could not be found.
Using this loads the shared library libcasa_derivedmscal.so
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.
casacore::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 prefix
and "lib" (in that order) and with suffix ".so" or ".dylib" (for Apple). No library version number is used. If not loaded successfully, an exception is thrown.
If a non-empty funcName is given, that function is looked up and executed for initialization purposes. 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.
casacore::DynLib::DynLib | ( | const std::string & | library, |
const std::string & | prefix, | ||
const std::string & | version, | ||
const std::string & | funcName, | ||
bool | closeOnDestruction = True |
||
) |
The same as above, but it is tried with and without the given version (in that order).
casacore::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.
casacore::DynLib::~DynLib | ( | ) |
Close the dynamic library if told so in the constructor.
|
private |
Try to open the library with some prefixes, suffixes and versions and to execute the initialization function.
If successful, itsHandle is filled. Otherwise an exception is thrown.
|
private |
Close (unload) the dynamic library (if opened).
|
inline |
void* casacore::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:
casts to a function returning Int and taking two Ints.
A null pointer is returned if the function could not be found.
|
inline |
|
private |
Open (load) the dynamic library.
|
private |
Try if the library can be opened using CASACORE_LDPATH.
|
private |
Try to open the library with some prefixes, suffixes and versions If successful, itsHandle is filled and the full library name is returned.
Otherwise an empty name is returned.
|
private |
Definition at line 192 of file DynLib.h.
Referenced by getError().
|
private |
Definition at line 190 of file DynLib.h.
Referenced by getHandle().