casacore
Public Member Functions | Private Member Functions | Private Attributes | List of all members
casacore::DynLib Class Reference

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

Detailed Description


Class to handle loading of dynamic libraries

Review Status

Reviewed By:
UNKNOWN
Date Reviewed:
before2004/08/25

Intended use:

Public interface

Prerequisite

Synopsis

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.

Example

DynLib dl("derivedmscal", "libcasa_", "register_derivedmscal");
AlwaysAssert (dl.getHandle());
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the constructors of assert_ to allow additi...
Definition: Assert.h:157
DynLib(const std::string &library, const std::string &prefix=std::string(), const std::string &funcName=std::string(), bool closeOnDestruction=True)
Load the dynamic library.

Using this loads the shared library libcasa_derivedmscal.so and executes the given register initialization function.

Motivation

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.

Definition at line 87 of file DynLib.h.

Constructor & Destructor Documentation

◆ DynLib() [1/3]

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.

◆ DynLib() [2/3]

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

◆ DynLib() [3/3]

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.

◆ ~DynLib()

casacore::DynLib::~DynLib ( )

Close the dynamic library if told so in the constructor.

Member Function Documentation

◆ attach()

void casacore::DynLib::attach ( const std::string &  name,
const std::string &  prefix,
const std::string &  version,
const std::string &  funcName 
)
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.

◆ close()

void casacore::DynLib::close ( )
private

Close (unload) the dynamic library (if opened).

◆ getError()

const std::string& casacore::DynLib::getError ( ) const
inline

Get the possible error.

Definition at line 157 of file DynLib.h.

References itsError.

◆ getFunc()

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:

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);
}
void * getFunc(const std::string &funcName)
Get a pointer to a function in the dynamic library.
int Int
Definition: aipstype.h:50

casts to a function returning Int and taking two Ints.
A null pointer is returned if the function could not be found.

◆ getHandle()

void* casacore::DynLib::getHandle ( ) const
inline

Get the dynamic library handle.

Definition at line 153 of file DynLib.h.

References itsHandle.

◆ open()

void casacore::DynLib::open ( const std::string &  name)
private

Open (load) the dynamic library.

◆ tryCasacorePath()

std::string casacore::DynLib::tryCasacorePath ( const std::string &  library,
const std::string &  prefix,
const std::string &  version 
)
private

Try if the library can be opened using CASACORE_LDPATH.

◆ tryOpen()

std::string casacore::DynLib::tryOpen ( const std::string &  name,
const std::string &  libdir,
const std::string &  prefix,
const std::string &  version 
)
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.

Member Data Documentation

◆ itsDoClose

Bool casacore::DynLib::itsDoClose
private

Definition at line 191 of file DynLib.h.

◆ itsError

std::string casacore::DynLib::itsError
private

Definition at line 192 of file DynLib.h.

Referenced by getError().

◆ itsHandle

void* casacore::DynLib::itsHandle
private

Definition at line 190 of file DynLib.h.

Referenced by getHandle().


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