PTLib Version 2.12.9

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pluginmgr.h
Go to the documentation of this file.
1 /*
2 * pluginmgr.h
3 *
4 * Plugin Manager Class Declarations
5 *
6 * Portable Windows Library
7 *
8 * Contributor(s): Snark at GnomeMeeting
9 *
10 * $Revision: 28941 $
11 * $Author: csoutheren $
12 * $Date: 2013-01-19 01:18:28 +1100 (Sat, 19 Jan 2013) $
13 */
14
15 #ifndef PTLIB_PLUGINMGR_H
16 #define PTLIB_PLUGINMGR_H
17
18 #include <ptlib/plugin.h>
19
20 class PPluginSuffix {
21 private:
22 int dummy;
23 };
24
25 template <class C>
26 void PLoadPluginDirectory(C & obj, const PDirectory & directory, const char * suffix = NULL)
27 {
28 PDirectory dir = directory;
29 if (!dir.Open()) {
30 PTRACE(4, "Cannot open plugin directory " << dir);
31 return;
32 }
33 PTRACE(4, "Enumerating plugin directory " << dir);
34 do {
35 PString entry = dir + dir.GetEntryName();
36 PDirectory subdir = entry;
37 if (subdir.Open())
38 PLoadPluginDirectory<C>(obj, entry, suffix);
39 else {
40 PFilePath fn(entry);
41 if (
42 (fn.GetType() *= PDynaLink::GetExtension()) &&
43 (
44 (suffix == NULL) || (fn.GetTitle().Right(strlen(suffix)) *= suffix)
45 )
46 )
47 obj.LoadPlugin(entry);
48 }
49 } while (dir.Next());
50 }
51
53 //
54 // Manager for plugins
55 //
56
57 class PPluginManager : public PObject
58 {
59 PCLASSINFO(PPluginManager, PObject);
60
61 public:
62 // functions to load/unload a dynamic plugin
63 PBoolean LoadPlugin (const PString & fileName);
64 void LoadPluginDirectory (const PDirectory & dir);
65
66 void OnShutdown();
67
68 // functions to access the plugins' services
69 PStringArray GetPluginTypes() const;
70 PStringArray GetPluginsProviding(const PString & serviceType) const;
71 PPluginServiceDescriptor * GetServiceDescriptor(const PString & serviceName, const PString & serviceType) const;
72 PObject * CreatePluginsDevice(const PString & serviceName, const PString & serviceType, int userData = 0) const;
73 PObject * CreatePluginsDeviceByName(const PString & deviceName, const PString & serviceType, int userData = 0, const PString & serviceName = PString::Empty()) const;
74 PStringArray GetPluginsDeviceNames(const PString & serviceName, const PString & serviceType, int userData = 0) const;
75 PBoolean GetPluginsDeviceCapabilities(const PString & serviceType,const PString & serviceName,const PString & deviceName,void * capabilities) const;
76
77 // function to register a service (used by the plugins themselves)
78 PBoolean RegisterService (const PString & serviceName, const PString & serviceType, PPluginServiceDescriptor * descriptor);
79
80 // Add a directory to the list of plugin directories (used by OPAL)
81 static bool AddPluginDirs(const PString & dirs);
82
83 // Get the list of plugin directories
84 static PStringArray GetPluginDirs();
85
86 // static functions for accessing global instances of plugin managers
87 static PPluginManager & GetPluginManager();
88
89 enum NotificationCode {
90 LoadingPlugIn,
91 UnloadingPlugIn
92 };
93
111 void AddNotifier(
112 const PNotifier & filterFunction,
113 PBoolean existing = false
114 );
115
116 void RemoveNotifier(
117 const PNotifier & filterFunction
118 );
119
120 protected:
121 void LoadPluginDirectory (const PDirectory & directory, const PStringList & suffixes);
122 void CallNotifier(PDynaLink & dll, NotificationCode code);
123
124 PMutex m_pluginsMutex;
125 PArray<PDynaLink> m_plugins;
126
127 PMutex m_servicesMutex;
128 PArray<PPluginService> m_services;
129
130 PMutex m_notifiersMutex;
131 PList<PNotifier> m_notifiers;
132 };
133
135 //
136 // Manager for plugin modules
137 //
138
139 class PPluginModuleManager : public PObject
140 {
141 public:
142 typedef PDictionary<PString, PDynaLink> PluginListType;
143
144 PPluginModuleManager(const char * signatureFunctionName, PPluginManager * pluginMgr = NULL);
145
146 PBoolean LoadPlugin(const PString & fileName)
147 { if (pluginMgr == NULL) return false; else return pluginMgr->LoadPlugin(fileName); }
148
149 void LoadPluginDirectory(const PDirectory &directory)
150 { if (pluginMgr != NULL) pluginMgr->LoadPluginDirectory(directory); }
151
152 virtual void OnLoadPlugin(PDynaLink & /*dll*/, P_INT_PTR /*code*/)
153 { }
154
155 virtual PluginListType GetPluginList() const
156 { return pluginDLLs; }
157
158 virtual void OnStartup()
159 { }
160 virtual void OnShutdown()
161 { }
162
163 protected:
164 PluginListType pluginDLLs;
165 PDECLARE_NOTIFIER(PDynaLink, PPluginModuleManager, OnLoadModule);
166
167 protected:
168 const char * signatureFunctionName;
169 PPluginManager * pluginMgr;
170 };
171
172
173 #define PLUGIN_LOADER_STARTUP_NAME "PluginLoaderStartup"
174
175 PFACTORY_LOAD(PluginLoaderStartup);
176
177
178 #endif // PTLIB_PLUGINMGR_H
179
180
181 // End Of File ///////////////////////////////////////////////////////////////