casacore::DynLib Class Reference

Class to handle loading of dynamic libraries. More...

#include <DynLib.h>

List of all members.

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.
const std::string & getError () const
 Get the possible error.

Private Member Functions

void open (const std::string &name)
 Open (load)the dynamic library.
void close ()
 Close (unload) the dynamic library (if opened).
std::string tryCasacorePath (const std::string &library, const std::string &prefix)
 Try if the library can be opened using CASACORE_LDPATH.

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

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

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

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.


Member Function Documentation

void casacore::DynLib::close (  )  [private]

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

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

Get the possible error.

Definition at line 146 of file DynLib.h.

References itsError.

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);
 }

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

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

Get the dynamic library handle.

Definition at line 142 of file DynLib.h.

References itsHandle.

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

Open (load)the dynamic library.

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

Try if the library can be opened using CASACORE_LDPATH.


Member Data Documentation

Definition at line 163 of file DynLib.h.

std::string casacore::DynLib::itsError [private]

Definition at line 164 of file DynLib.h.

Referenced by getError().

void* casacore::DynLib::itsHandle [private]

Definition at line 162 of file DynLib.h.

Referenced by getHandle().


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

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1