OBT::Plugin Class Reference

#include <OBTPlugin.h>

Collaboration diagram for OBT::Plugin:

Collaboration graph
[legend]

List of all members.

Public Types

typedef bool(* InitFunc )(const char *configurationFile)
 Initialization function implemented by the plugin.
typedef bool(* DeInitFunc )()
 De-initialization function implemented by the plugin.
typedef const
PluginInformation &(* 
PluginInformationFunc )()
 Plugin information function implemented by the plugin.

Public Member Functions

 Plugin (const char *path, const char *name, const char *configurationFile=0)
virtual ~Plugin ()
bool load ()
bool unload ()
bool isLoaded () const
const PluginInformationgetInformation () const
const char * getPath () const
const char * getConfigurationFile () const

Private Member Functions

 Plugin (const Plugin &)
 No copy.
Pluginoperator= (const Plugin &)
 No copy.

Private Attributes

void * _pluginHandle
 OBTPlugin.handle.
bool _initialised
 initialised state (set by a call to initPlugin)
std::string _path
 plugin path
std::string _configurationFile
 plugin configuration file path


Detailed Description

This class permits to load / unload a plugin (a cross-platform dynamically linked library).

Definition at line 20 of file OBTPlugin.h.


Member Typedef Documentation

typedef bool( * OBT::Plugin::InitFunc)(const char *configurationFile)

Initialization function implemented by the plugin.

Definition at line 25 of file OBTPlugin.h.

typedef bool( * OBT::Plugin::DeInitFunc)()

De-initialization function implemented by the plugin.

Definition at line 28 of file OBTPlugin.h.

typedef const PluginInformation&( * OBT::Plugin::PluginInformationFunc)()

Plugin information function implemented by the plugin.

Definition at line 31 of file OBTPlugin.h.


Constructor & Destructor Documentation

Plugin::Plugin ( const char *  path,
const char *  name,
const char *  configurationFile = 0 
)

Constructor.

Parameters:
[in] path of the plugin file.
[in] name of the plugin file. Can be null.
[in] configurationFile of the plugin. Null by default.
If the name is null the path must include the complete name an path of the plugin library. If the name is defined the plugin library is:

Definition at line 15 of file OBTPlugin.cpp.

References _configurationFile, _path, and OBT_ASSERT.

00016 :
00017 _pluginHandle( 0 ),
00018 _initialised( false )
00019 {
00020   OBT_ASSERT( path != NULL ) ;
00021   _path = path ;
00022   if( name )
00023   {
00024 #ifdef _MSC_VER
00025 #  ifndef NDEBUG
00026     // first try in _path
00027     _path += "/" + std::string( name ) + "_d.dll" ;
00028     struct stat buffer ;
00029     if( stat( _path.c_str(), &buffer ) )
00030     { // The file doesn't exist => search in sub-directory according to the debug/release flag
00031       _path = std::string( path ) + "/debug/" + std::string( name ) + "_d.dll" ;
00032     }
00033 #  else
00034     // first try in _path
00035     _path += "/" + std::string( name ) + ".dll" ;
00036     struct stat buffer ;
00037     if( stat( _path.c_str(), &buffer ) )
00038     { // The file doesn't exist => search in sub-directory according to the debug/release flag
00039       _path = std::string( path ) + "/release/" + std::string( name ) + ".dll" ;
00040     }
00041 #  endif
00042 #else
00043     // first try in _path
00044     _path += ( ( "" == _path ) ? "lib" : "/lib" ) + std::string( name ) + ".so" ;
00045 #endif
00046   }
00047   
00048   if ( configurationFile != NULL )
00049   {
00050     _configurationFile = configurationFile ;
00051   }
00052   else
00053   {
00054     _configurationFile = "" ;
00055   }
00056 }

Plugin::~Plugin (  )  [virtual]

Destructor.

Definition at line 61 of file OBTPlugin.cpp.

References unload().

00062 {
00063   unload( ) ;
00064 }

OBT::Plugin::Plugin ( const Plugin  )  [private]

No copy.


Member Function Documentation

bool Plugin::load (  ) 

Load the plugin.

Returns:
true on success, false otherwise.

Definition at line 70 of file OBTPlugin.cpp.

References _initialised, _path, _pluginHandle, getConfigurationFile(), and TRACE_ERROR.

00071 {
00072   if ( _pluginHandle == NULL )
00073   {
00074     // load the plugin
00075   #ifdef _MSC_VER
00076     _pluginHandle = LoadLibrary( _path.c_str() ) ;
00077   #else
00078     _pluginHandle = dlopen( _path.c_str(), RTLD_LAZY | RTLD_GLOBAL ) ;
00079   #endif
00080     if ( _pluginHandle == NULL )
00081     {
00082       TRACE_ERROR( "Unable to load plugin: " << _path.c_str() ) ;
00083   #ifndef _MSC_VER
00084       char *s = dlerror() ;
00085       if (s)
00086       {
00087         TRACE_ERROR( "Error message from plugin loading: " << s ) ;
00088         free( s ) ;
00089       }
00090   #endif
00091       _initialised = false ;
00092     }
00093     // call the initPlugin function of the plugin
00094     else
00095     {
00096       _initialised = true ;
00097       void* functionPointer( NULL ) ;
00098   #ifdef _MSC_VER
00099       functionPointer = GetProcAddress( _pluginHandle, "initPlugin" ) ;
00100   #else
00101       functionPointer = dlsym( _pluginHandle, "initPlugin" ) ;
00102   #endif
00103       InitFunc initFunc( reinterpret_cast<InitFunc>( functionPointer ) ) ;
00104       if( initFunc == NULL )
00105       {
00106         TRACE_ERROR( "Unable to call the initPlugin function of the plugin: " << _path.c_str() ) ;
00107         _initialised = false ;
00108       }
00109       else 
00110       {
00111         _initialised = initFunc( getConfigurationFile() ) ;
00112       }
00113     }
00114   }
00115 
00116   return _initialised ;
00117 }

bool Plugin::unload (  ) 

Unload the plugin.

Returns:
true on success, false otherwise.

Definition at line 123 of file OBTPlugin.cpp.

References _initialised, _path, _pluginHandle, and TRACE_ERROR.

Referenced by ~Plugin().

00124 {
00125   bool returnCode( true ) ;
00126   if( _pluginHandle != NULL )
00127   {  
00128     // call the finishPlugin function of the plugin
00129     void* functionPointer( NULL ) ;
00130 #ifdef _MSC_VER
00131     functionPointer = GetProcAddress( _pluginHandle, "finishPlugin" ) ;
00132 #else
00133     functionPointer = dlsym( _pluginHandle, "finishPlugin" ) ;
00134 #endif
00135     DeInitFunc deInitFunc( reinterpret_cast<DeInitFunc>( functionPointer ) ) ;
00136     if( deInitFunc == NULL )
00137     {
00138       TRACE_ERROR( "Unable to call the finishPlugin function of the plugin: " << _path.c_str() ) ;
00139       returnCode = false ;
00140     }
00141     else 
00142     {
00143       returnCode = deInitFunc() ;
00144     }
00145 
00146     // unload the plugin
00147 #ifdef _MSC_VER
00148     FreeLibrary( _pluginHandle ) ;
00149 #else
00150     dlclose( _pluginHandle ) ;
00151 #endif
00152     _pluginHandle = NULL ;
00153     _path = "" ;
00154   }
00155   _initialised = false ;
00156 
00157   return returnCode ;
00158 }

bool OBT::Plugin::isLoaded (  )  const [inline]

Get accessor to the loaded state.

Returns:
true if the plugin is loaded, false otherwise.

Definition at line 120 of file OBTPlugin.h.

References _initialised.

00121 {
00122         return _initialised ;
00123 }

const PluginInformation & Plugin::getInformation (  )  const

Get plugin information.

Returns:
plugin information.

Definition at line 164 of file OBTPlugin.cpp.

References _path, _pluginHandle, TRACE_ERROR, and OBT::PluginInformation::Unknown.

00165 {
00166   if( _pluginHandle != NULL )
00167   {
00168     void* functionPointer( NULL ) ;
00169 #ifdef _MSC_VER
00170     functionPointer = GetProcAddress( _pluginHandle, "getPluginInformation" ) ;
00171 #else
00172     functionPointer = dlsym( _pluginHandle, "getPluginInformation" ) ;
00173 #endif
00174     PluginInformationFunc pluginInformationFunc( reinterpret_cast<PluginInformationFunc>( functionPointer ) ) ;
00175     if( pluginInformationFunc != NULL )
00176     {
00177       return pluginInformationFunc() ;
00178     }
00179     else
00180     {
00181       TRACE_ERROR( "Unable to call the getPluginInformation function of the plugin: " << _path.c_str() ) ;
00182     }
00183   }
00184 
00185   return PluginInformation::Unknown ;
00186 }

const char * OBT::Plugin::getPath (  )  const [inline]

get accessor to the plugin path.

Returns:
a C string of the plugin path.

Definition at line 129 of file OBTPlugin.h.

References _path.

00130 {
00131   return _path.c_str() ;
00132 }

const char * OBT::Plugin::getConfigurationFile (  )  const [inline]

get accessor to the plugin configuration file path.

Returns:
a C string to the configuration file path, null if there is no configuration file.

Definition at line 138 of file OBTPlugin.h.

References _configurationFile.

Referenced by load().

00139 {
00140   return _configurationFile.empty() ? NULL : _configurationFile.c_str() ;
00141 }

Plugin& OBT::Plugin::operator= ( const Plugin  )  [private]

No copy.


Member Data Documentation

void* OBT::Plugin::_pluginHandle [private]

OBTPlugin.handle.

Definition at line 103 of file OBTPlugin.h.

Referenced by getInformation(), load(), and unload().

bool OBT::Plugin::_initialised [private]

initialised state (set by a call to initPlugin)

Definition at line 107 of file OBTPlugin.h.

Referenced by isLoaded(), load(), and unload().

std::string OBT::Plugin::_path [private]

plugin path

Definition at line 110 of file OBTPlugin.h.

Referenced by getInformation(), getPath(), load(), Plugin(), and unload().

std::string OBT::Plugin::_configurationFile [private]

plugin configuration file path

Definition at line 113 of file OBTPlugin.h.

Referenced by getConfigurationFile(), and Plugin().


Generated on Wed Oct 1 11:34:18 2008 for OBT by  doxygen 1.5.3