# 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  * $Revision: 26636 $
    00011  * $Author: rjongbloed $
    00012  * $Date: 2011-11-03 18:57:12 -0500 (Thu, 03 Nov 2011) $
    00013  */
    00014
    00015 #ifndef PTLIB_PLUGINMGR_H
    00016 #define PTLIB_PLUGINMGR_H
    00017
    00018 #include <ptlib/plugin.h>
    00019
    00020 class PPluginSuffix {
    00021   private:
    00022     int dummy;
    00023 };
    00024
    00025 template <class C>
    00026 void PLoadPluginDirectory(C & obj, const PDirectory & directory, const char * suffix = NULL)
    00027 {
    00028   PDirectory dir = directory;
    00029   if (!dir.Open()) {
    00030     PTRACE(4, "Cannot open plugin directory " << dir);
    00031     return;
    00032   }
    00033   PTRACE(4, "Enumerating plugin directory " << dir);
    00034   do {
    00035     PString entry = dir + dir.GetEntryName();
    00036     PDirectory subdir = entry;
    00037     if (subdir.Open())
    00038       PLoadPluginDirectory<C>(obj, entry, suffix);
    00039     else {
    00040       PFilePath fn(entry);
    00041       if (
    00042            (fn.GetType() *= PDynaLink::GetExtension()) &&
    00043            (
    00044              (suffix == NULL) || (fn.GetTitle().Right(strlen(suffix)) *= suffix)
    00045            )
    00046          )
    00047         obj.LoadPlugin(entry);
    00048     }
    00049   } while (dir.Next());
    00050 }
    00051
    00053 //
    00054 //  Manager for plugins
    00055 //
    00056
    00057 class PPluginManager : public PObject
    00058 {
    00059   PCLASSINFO(PPluginManager, PObject);
    00060
    00061   public:
    00062     // functions to load/unload a dynamic plugin
    00063     PBoolean LoadPlugin (const PString & fileName);
    00064     void LoadPluginDirectory (const PDirectory & dir);
    00065
    00066     void OnShutdown();
    00067
    00068     // functions to access the plugins' services
    00069     PStringArray GetPluginTypes() const;
    00070     PStringArray GetPluginsProviding(const PString & serviceType) const;
    00071     PPluginServiceDescriptor * GetServiceDescriptor(const PString & serviceName, const PString & serviceType) const;
    00072     PObject * CreatePluginsDevice(const PString & serviceName, const PString & serviceType, int userData = 0) const;
    00073     PObject * CreatePluginsDeviceByName(const PString & deviceName, const PString & serviceType, int userData = 0, const PString & serviceName = PString::Empty()) const;
    00074     PStringArray GetPluginsDeviceNames(const PString & serviceName, const PString & serviceType, int userData = 0) const;
    00075     PBoolean GetPluginsDeviceCapabilities(const PString & serviceType,const PString & serviceName,const PString & deviceName,void * capabilities) const;
    00076
    00077     // function to register a service (used by the plugins themselves)
    00078     PBoolean RegisterService (const PString & serviceName, const PString & serviceType, PPluginServiceDescriptor * descriptor);
    00079
    00080     // Add a directory to the list of plugin directories (used by OPAL)
    00081     static bool AddPluginDirs(const PString & dirs);
    00082
    00083     // Get the list of plugin directories
    00084     static PStringArray GetPluginDirs();
    00085
    00086     // static functions for accessing global instances of plugin managers
    00087     static PPluginManager & GetPluginManager();
    00088
    00089     enum NotificationCode {
    00090       LoadingPlugIn,
    00091       UnloadingPlugIn
    00092     };
    00093
    00111     void AddNotifier(
    00112       const PNotifier & filterFunction,
    00113       PBoolean existing = false
    00114     );
    00115
    00116     void RemoveNotifier(
    00117       const PNotifier & filterFunction
    00118     );
    00119
    00120   protected:
    00121     void LoadPluginDirectory (const PDirectory & directory, const PStringList & suffixes);
    00122     void CallNotifier(PDynaLink & dll, NotificationCode code);
    00123
    00124     PMutex            m_pluginsMutex;
    00125     PArray<PDynaLink> m_plugins;
    00126
    00127     PMutex                 m_servicesMutex;
    00128     PArray<PPluginService> m_services;
    00129
    00130     PMutex           m_notifiersMutex;
    00131     PList<PNotifier> m_notifiers;
    00132 };
    00133
    00135 //
    00136 //  Manager for plugin modules
    00137 //
    00138
    00139 class PPluginModuleManager : public PObject
    00140 {
    00141   public:
    00142     typedef PDictionary<PString, PDynaLink> PluginListType;
    00143
    00144     PPluginModuleManager(const char * signatureFunctionName, PPluginManager * pluginMgr = NULL);
    00145
    00146     PBoolean LoadPlugin(const PString & fileName)
    00147     { if (pluginMgr == NULL) return false; else return pluginMgr->LoadPlugin(fileName); }
    00148
    00149     void LoadPluginDirectory(const PDirectory &directory)
    00150     { if (pluginMgr != NULL) pluginMgr->LoadPluginDirectory(directory); }
    00151
    00152     virtual void OnLoadPlugin(PDynaLink & /*dll*/, INT /*code*/)
    00153     { }
    00154
    00155     virtual PluginListType GetPluginList() const
    00156     { return pluginDLLs; }
    00157
    00158     virtual void OnStartup()
    00159     { }
    00160     virtual void OnShutdown()
    00161     { }
    00162
    00163   protected:
    00164     PluginListType pluginDLLs;
    00165     PDECLARE_NOTIFIER(PDynaLink, PPluginModuleManager, OnLoadModule);
    00166
    00167   protected:
    00168     const char * signatureFunctionName;
    00169     PPluginManager * pluginMgr;
    00170 };
    00171
    00172
    00173 #define PLUGIN_LOADER_STARTUP_NAME "PluginLoaderStartup"
    00174
    00175 PFACTORY_LOAD(PluginLoaderStartup);
    00176
    00177
    00178 #endif // PTLIB_PLUGINMGR_H
    00179
    00180
    00181 // End Of File ///////////////////////////////////////////////////////////////