pluginmgr.h

Go to the documentation of this file.
00001 /*
00002  * pluginmgr.h
00003  *
00004  * Plugin Manager Class Declarations
00005  *
00006  * Portable Windows Library
00007  *
00008  * Contributor(s): Snark at GnomeMeeting
00009  *
00010  * $Log: pluginmgr.h,v $
00011  * Revision 1.24  2007/08/09 10:31:33  csoutheren
00012  * Restored template for OpenH323 compatibility
00013  *
00014  * Revision 1.23  2007/08/08 08:58:39  csoutheren
00015  * More plugin manager changes, as the last approach dead-ended :(
00016  *
00017  * Revision 1.22  2007/08/08 07:12:03  csoutheren
00018  * More re-arrangement of plugin suffixes
00019  *
00020  * Revision 1.21  2007/08/07 07:59:11  csoutheren
00021  * Allow plugin suffix to be determined via virtual
00022  *
00023  * Revision 1.20  2007/04/02 05:29:54  rjongbloed
00024  * Tidied some trace logs to assure all have a category (bit before a tab character) set.
00025  *
00026  * Revision 1.19  2006/10/25 12:51:05  shorne
00027  * fix for devices having same name for different drivers.
00028  *
00029  * Revision 1.18  2006/01/21 13:43:05  dsandras
00030  * Allow the plugin manager to look for plugins in symlinked directories.
00031  *
00032  * Revision 1.17  2005/08/09 09:08:09  rjongbloed
00033  * Merged new video code from branch back to the trunk.
00034  *
00035  * Revision 1.16.6.1  2005/07/17 09:27:04  rjongbloed
00036  * Major revisions of the PWLib video subsystem including:
00037  *   removal of F suffix on colour formats for vertical flipping, all done with existing bool
00038  *   working through use of RGB and BGR formats so now consistent
00039  *   cleaning up the plug in system to use virtuals instead of pointers to functions.
00040  *   rewrite of SDL to be a plug in compatible video output device.
00041  *   extensive enhancement of video test program
00042  *
00043  * Revision 1.16  2004/08/05 03:45:35  csoutheren
00044  * Fixed problems with plugin suffix not being propagated to sudirectories
00045  *
00046  * Revision 1.15  2004/06/24 23:10:27  csoutheren
00047  * Require plugins to have _pwplugin suffix
00048  *
00049  * Revision 1.14  2004/06/01 05:44:57  csoutheren
00050  * Added OnShutdown to allow cleanup on exit
00051  *
00052  * Revision 1.13  2004/05/19 06:54:11  csoutheren
00053  * Removed unused code
00054  *
00055  * Revision 1.12  2004/05/18 06:01:06  csoutheren
00056  * Deferred plugin loading until after main has executed by using abstract factory classes
00057  *
00058  * Revision 1.11  2004/05/17 06:05:20  csoutheren
00059  * Changed "make docs" to use doxygen
00060  * Added new config file and main page
00061  *
00062  * Revision 1.10  2004/04/22 11:43:47  csoutheren
00063  * Factored out functions useful for loading dynamic libraries
00064  *
00065  * Revision 1.9  2004/04/22 07:55:30  csoutheren
00066  * Fix problem with generic plugin manager having pure virtual. Thanks to Ben Lear
00067  *
00068  * Revision 1.8  2004/04/14 11:14:10  csoutheren
00069  * Final fix for generic plugin manager
00070  *
00071  * Revision 1.7  2004/04/14 10:57:38  csoutheren
00072  * Removed multiple definition of statc function in generic plugin functions
00073  *
00074  * Revision 1.6  2004/04/14 10:01:54  csoutheren
00075  * Fixed compile problem on Windows
00076  *
00077  * Revision 1.5  2004/04/14 08:12:02  csoutheren
00078  * Added support for generic plugin managers
00079  *
00080  * Revision 1.4  2004/03/23 04:43:42  csoutheren
00081  * Modified plugin manager to allow code modules to be notified when plugins
00082  * are loaded or unloaded
00083  *
00084  * Revision 1.3  2003/11/12 10:24:35  csoutheren
00085  * Changes to allow operation of static plugins under Windows
00086  *
00087  * Revision 1.2  2003/11/12 03:26:17  csoutheren
00088  * Initial version of plugin code from Snark of GnomeMeeting with changes
00089  *    by Craig Southeren os Post Increment
00090  *
00091  *
00092  */
00093 
00094 #ifndef _PLUGINMGR_H
00095 #define _PLUGINMGR_H
00096 
00097 #define DEFAULT_PLUGINDIR "/usr/lib/pwlib"
00098 
00099 #include <ptlib/plugin.h>
00100 
00101 class PPluginSuffix {
00102   private:
00103     int dummy;
00104 };
00105 
00106 template <class C>
00107 void PLoadPluginDirectory(C & obj, const PDirectory & directory, const char * suffix = NULL)
00108 {
00109   PDirectory dir = directory;
00110   if (!dir.Open()) {
00111     PTRACE(4, "Cannot open plugin directory " << dir);
00112     return;
00113   }
00114   PTRACE(4, "Enumerating plugin directory " << dir);
00115   do {
00116     PString entry = dir + dir.GetEntryName();
00117     PDirectory subdir = entry;
00118     if (subdir.Open())
00119       PLoadPluginDirectory<C>(obj, entry, suffix);
00120     else {
00121       PFilePath fn(entry);
00122       if (
00123            (fn.GetType() *= PDynaLink::GetExtension()) &&
00124            (
00125              (suffix == NULL) || (fn.GetTitle().Right(strlen(suffix)) *= suffix)
00126            )
00127          )
00128         obj.LoadPlugin(entry);
00129     }
00130   } while (dir.Next());
00131 }
00132 
00134 //
00135 //  Manager for plugins
00136 //
00137 
00138 class PPluginManager : public PObject
00139 {
00140   PCLASSINFO(PPluginManager, PObject);
00141 
00142   public:
00143     // functions to load/unload a dynamic plugin 
00144     BOOL LoadPlugin (const PString & fileName);
00145     void LoadPluginDirectory (const PDirectory & dir);
00146   
00147     // functions to access the plugins' services 
00148     PStringList GetPluginTypes() const;
00149     PStringList GetPluginsProviding(const PString & serviceType) const;
00150     PPluginServiceDescriptor * GetServiceDescriptor(const PString & serviceName, const PString & serviceType) const;
00151     PObject * CreatePluginsDevice(const PString & serviceName, const PString & serviceType, int userData = 0) const;
00152     PObject * CreatePluginsDeviceByName(const PString & deviceName, const PString & serviceType, int userData = 0, const PString & serviceName = PString::Empty()) const;
00153     PStringList GetPluginsDeviceNames(const PString & serviceName, const PString & serviceType, int userData = 0) const;
00154 
00155     // function to register a service (used by the plugins themselves)
00156     BOOL RegisterService (const PString & serviceName, const PString & serviceType, PPluginServiceDescriptor * descriptor);
00157 
00158     // Get the list of plugin directories
00159     static PStringArray GetPluginDirs();
00160 
00161     // static functions for accessing global instances of plugin managers
00162     static PPluginManager & GetPluginManager();
00163 
00181     void AddNotifier(
00182       const PNotifier & filterFunction,
00183       BOOL existing = FALSE
00184     );
00185 
00186     void RemoveNotifier(
00187       const PNotifier & filterFunction
00188     );
00189 
00190   protected:
00191     void LoadPluginDirectory (const PDirectory & directory, const PStringList & suffixes);
00192     void CallNotifier(PDynaLink & dll, INT code);
00193 
00194     PMutex pluginListMutex;
00195     PList<PDynaLink> pluginList;
00196     
00197     PMutex serviceListMutex;
00198     PList<PPluginService> serviceList;
00199 
00200     PMutex notifierMutex;
00201     PList<PNotifier> notifierList;
00202 };
00203 
00205 //
00206 //  Manager for plugin modules
00207 //
00208 
00209 class PPluginModuleManager : public PObject
00210 {
00211   public:
00212     typedef PDictionary<PString, PDynaLink> PluginListType;
00213 
00214     PPluginModuleManager(const char * _signatureFunctionName, PPluginManager * pluginMgr = NULL);
00215 
00216     BOOL LoadPlugin(const PString & fileName)
00217     { if (pluginMgr == NULL) return FALSE; else return pluginMgr->LoadPlugin(fileName); }
00218 
00219     void LoadPluginDirectory(const PDirectory &directory)
00220     { if (pluginMgr != NULL) pluginMgr->LoadPluginDirectory(directory); }
00221 
00222     virtual void OnLoadPlugin(PDynaLink & /*dll*/, INT /*code*/)
00223     { }
00224 
00225     virtual PluginListType GetPluginList() const
00226     { return pluginList; }
00227 
00228     virtual void OnShutdown()
00229     { }
00230 
00231   protected:
00232     PluginListType pluginList;
00233     PDECLARE_NOTIFIER(PDynaLink, PPluginModuleManager, OnLoadModule);
00234 
00235   protected:
00236     const char * signatureFunctionName;
00237     PPluginManager * pluginMgr;
00238 };
00239 
00240 #endif // ifndef _PLUGINMGR_H

Generated on Fri Mar 7 06:25:02 2008 for PTLib by  doxygen 1.5.1