Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

kore::PluginLoader Class Reference

class kore::PluginLoader - platform-independant loader for dll's (dynamic library). More...

#include <pluginloader.h>

Inheritance diagram for kore::PluginLoader::

kore::ServiceProvider kore::Module List of all members.

Public Methods

 PluginLoader ()
 Default constructor. More...

 ~PluginLoader ()
 Destructor. More...

virtual PluginopenPlugin (const char *libName, const char *libPath=0, int libFlags=0)
 Loads a dll and gets the Plugin object for it. More...

virtual PluginrunPlugin (const char *libName, const char *libPath=0, int libFlags=0)
 Loads a dll, gets the dll's Plugin object and initialises it. More...

virtual void closePlugin (Plugin *plugin)
 Unloads a previously loaded dll. More...

virtual const char * lastError ()
 Gets the error message for the last failed openPlugin() or runPlugin(). More...

virtual char * libName2fileName (const char *libName)
 Helper method. More...

virtual char * fileName2libName (const char *fileName)
 Helper method. More...


Private Attributes

const Version_loaderVersion
const Version_loaderAPIVersion
const Info * _loaderInfo
const Service * _loaderService
char _lastError [100]

Detailed Description

class kore::PluginLoader - platform-independant loader for dll's (dynamic library).

It provides methods for loading (openPlugin()) and initialising (runPlugin()) plugins.

Definition at line 32 of file pluginloader.h.


Constructor & Destructor Documentation

PluginLoader::PluginLoader  
 

Default constructor.

Creates a new PluginLoader instance.

Definition at line 31 of file pluginloader.cpp.

References _loaderAPIVersion, _loaderInfo, _loaderService, _loaderVersion, kore::ServiceProvider::addService(), PLDR_API_MAJOR, PLDR_API_MINOR, PLDR_API_REVISION, PLDR_API_VERSION, PLDR_DESCRIPTION, PLDR_MAJOR, PLDR_MINOR, PLDR_NAME, PLDR_REVISION, PLDR_SERVICE, PLDR_SERVICE_DESCRIPTION, PLDR_TYPE, PLDR_VERSION, and kore::Module::setInfo().

PluginLoader::~PluginLoader  
 

Destructor.

Definition at line 40 of file pluginloader.cpp.

References _loaderAPIVersion, _loaderInfo, _loaderService, and _loaderVersion.

00041 {
00042     delete _loaderInfo;
00043     delete _loaderVersion;
00044     delete _loaderAPIVersion;
00045     delete _loaderService;
00046 }


Member Function Documentation

void PluginLoader::closePlugin Plugin   plugin [virtual]
 

Unloads a previously loaded dll.

Parameters:
plugin  - the Plugin object to be closed.

Definition at line 150 of file pluginloader.cpp.

00151 {
00152     plugin->unloadingPlugin();
00153 #if defined( KORE_WIN32 )
00154     FreeLibrary( plugin->libHandle() );
00155 #elif defined( KORE_BEOS )
00156     unload_add_on( plugin->libHandle() );
00157 #elif defined( KORE_ATHEOS )
00158     unload_library( plugin->libHandle() );
00159 #else
00160     dlclose(const_cast<void*> (plugin->libHandle()));
00161 #endif
00162 }

char * PluginLoader::fileName2libName const char *    fileName [virtual]
 

Helper method.

It converts a platform-dependant dll filename (ie. "libmy_plugin.so", or "my_plugin.dll") to the corresponding platform-independant dll filename (ie. "my_plugin")

Parameters:
fileName  - the platform-dependant dll filename.
Returns:
- the platform-independant dll name.

Definition at line 61 of file pluginloader.cpp.

00062 {
00063 #ifdef KORE_WIN32
00064     int n = strlen(libName) - 4;
00065     char* ln = new char[n+1];
00066 #else
00067     char* pos = strstr(fileName, ".so");
00068     int n = pos - fileName - 3;
00069     char* ln = new char[n+1];
00070     fileName += 3;
00071 #endif
00072     return strncpy(ln, fileName, n);
00073 }

const char * PluginLoader::lastError   [virtual]
 

Gets the error message for the last failed openPlugin() or runPlugin().

Returns:
- the last error message generated by openPlugin() or runPlugin().

Definition at line 164 of file pluginloader.cpp.

References _lastError.

00165 {
00166     _lastError[0]=0;
00167 #if defined( KORE_WIN32 )
00168     FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
00169         GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), _lastError, 100, NULL );
00170 #elif defined( KORE_BEOS )
00171     strncpy(_lastError, strerror(errno), 100);
00172 #elif defined( KORE_ATHEOS )
00173     strncpy(_lastError, strerror(errno), 100);
00174 #else
00175     strncpy(_lastError, dlerror(), 100);
00176 #endif
00177     return _lastError;
00178 }

char * PluginLoader::libName2fileName const char *    libName [virtual]
 

Helper method.

It converts a platform-independant dll name (ie. "my_plugin") to the corresponding platform-dependant dll filename (ie. "libmy_plugin.so", or "my_plugin.dll").

Parameters:
libName  - the platform-independant dll name.
Returns:
- the platform-dependant dll filename.

Definition at line 49 of file pluginloader.cpp.

Referenced by openPlugin().

00050 {
00051     int n = strlen(libName);
00052 #ifdef KORE_WIN32
00053     char* fn = new char[n+1+4];
00054     return strcat(strcpy(fn,libName),".dll");
00055 #else
00056     char* fn = new char[n+1+6];
00057     return strcat(strcat(strcpy(fn,"lib"),libName),".so");
00058 #endif
00059 }

Plugin * PluginLoader::openPlugin const char *    libName,
const char *    libPath = 0,
int    libFlags = 0
[virtual]
 

Loads a dll and gets the Plugin object for it.

Parameters:
libName  - the platform-independant library name. Please note that this is NOT the actual filename of the dll. I.e. if you use: openPlugin("my_plugin"), the Plugin loader will look for a dll named "libmy_plugin.so" (or "my_plugin.dll") and try to load it.
libPath  - the filesystem path were to look for the plugin (optional).
libFlags  - system-dependant flags to be used when loading the dll.
Returns:
- the Plugin object associated to the given libName, if the dll was found, NULL otherwise.

Definition at line 82 of file pluginloader.cpp.

References HMODULE, libName2fileName(), and kore::PluginFuncType.

Referenced by runPlugin().

00083 {
00084     HMODULE handle;
00085     char* fileName = libName2fileName(libName);
00086     char* filePath = 0;
00087     Plugin* res;
00088     if( libPath && libPath[0] )
00089     {
00090         int n = strlen(libPath);
00091         int slash = libPath[n-1] == '/' ? 0 : 1;
00092         filePath = new char[n+strlen(fileName)+slash+1];
00093         strcpy(filePath, libPath);
00094         if( slash )
00095         {
00096             filePath[n] = '/';
00097             filePath[n+1] = 0;
00098         }
00099     }
00100     else
00101     {
00102         filePath = new char[strlen(fileName)+1];
00103         filePath[0] = 0;
00104     }
00105     strcat( filePath, fileName );
00106     //cout << "Loading: " << filePath << endl;
00107     delete[] fileName;
00108 #if defined( KORE_WIN32 )
00109     handle = LoadLibrary(filePath);
00110 #elif defined( KORE_BEOS )
00111     handle = load_add_on(filePath);
00112 #elif defined( KORE_ATHEOS )
00113     handle = load_library( filePath, 0 );
00114 #else
00115     handle = dlopen(filePath, libFlags | RTLD_NOW );
00116 #endif
00117     delete[] filePath;
00118     //cout << "Loaded: " << handle << endl;
00119 #if defined( KORE_BEOS )
00120     if( handle < B_NO_ERROR )
00121 #elif defined( KORE_ATHEOS )
00122     if( handle < 0 )
00123 #else
00124     if( !handle )
00125 #endif
00126         return 0;
00127     //cout << "Found: " << handle << endl;
00128     PluginFuncType sym = 0;
00129 #if defined( KORE_WIN32 )
00130     sym = (PluginFuncType) GetProcAddress( handle, "plugin" );
00131 #elif defined( KORE_BEOS )
00132     if( B_OK != get_image_symbol( handle, "plugin", B_SYMBOL_TYPE_TEXT, reinterpret_cast<void**>(&sym) ) )
00133         sym = 0;
00134 #elif defined( KORE_ATHEOS )
00135     if( 0 != get_symbol_address( handle, "plugin", -1, reinterpret_cast<void**>(&sym) ) )
00136     sym = 0;
00137 #else
00138     sym = (PluginFuncType) dlsym( const_cast<void*>(handle), "plugin" );
00139 #endif
00140     //cout << "Done: " << sym << endl;
00141     if( !sym )
00142         res = new Plugin(handle,libName,libPath,libFlags);
00143     else
00144         res = sym(handle,libName,libPath,libFlags);
00145     //cout << "Called: " << sym << endl;
00146     res->pluginLoaded();
00147     return res;
00148 }

Plugin * PluginLoader::runPlugin const char *    libName,
const char *    libPath = 0,
int    libFlags = 0
[virtual]
 

Loads a dll, gets the dll's Plugin object and initialises it.

Parameters:
libName  - the platform-independant library name. Please note that this is NOT the actual filename of the dll. I.e. if you use: openPlugin("my_plugin"), the Plugin loader will look for a dll named "libmy_plugin.so" (or "my_plugin.dll") and try to load it.
libPath  - the filesystem path were to look for the plugin (optional).
libFlags  - system-dependant flags to be used when loading the dll.
Returns:
- the Plugin object associated to the given libName, if the dll was found, NULL otherwise.

Definition at line 75 of file pluginloader.cpp.

References openPlugin().

00076 {
00077     Plugin* plugin = openPlugin(libName, libPath, libFlags);
00078     if( plugin )
00079         plugin->initPlugin();
00080     return plugin;
00081 }


Member Data Documentation

char kore::PluginLoader::_lastError[100] [private]
 

Definition at line 104 of file pluginloader.h.

Referenced by lastError().

const Version* kore::PluginLoader::_loaderAPIVersion [private]
 

Definition at line 98 of file pluginloader.h.

Referenced by PluginLoader(), and ~PluginLoader().

const Info* kore::PluginLoader::_loaderInfo [private]
 

Definition at line 100 of file pluginloader.h.

Referenced by PluginLoader(), and ~PluginLoader().

const Service* kore::PluginLoader::_loaderService [private]
 

Definition at line 102 of file pluginloader.h.

Referenced by PluginLoader(), and ~PluginLoader().

const Version* kore::PluginLoader::_loaderVersion [private]
 

Definition at line 96 of file pluginloader.h.

Referenced by PluginLoader(), and ~PluginLoader().


The documentation for this class was generated from the following files:
Generated on Sat Feb 16 08:40:50 2002 for Korelib by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001