PTLib  Version 2.10.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
plugin.h
Go to the documentation of this file.
1 /*
2  * plugin.h
3  *
4  * Plugin Class Declarations
5  *
6  * Portable Windows Library
7  *
8  * Contributor(s): Snark at GnomeMeeting
9  *
10  * $Revision: 25329 $
11  * $Author: ededu $
12  * $Date: 2011-03-15 18:03:58 -0500 (Tue, 15 Mar 2011) $
13  */
14 
15 #ifndef PTLIB_PLUGIN_H
16 #define PTLIB_PLUGIN_H
17 
19 //
20 // these templates implement an adapter to make the old style device plugins appear in the new factory system
21 //
22 
23 #include <ptlib/pfactory.h>
24 
25 template <class AbstractClass, typename KeyType = PString>
26 class PDevicePluginFactory : public PFactory<AbstractClass, KeyType>
27 {
28  public:
29  class Worker : public PFactory<AbstractClass, KeyType>::WorkerBase
30  {
31  public:
32  Worker(const KeyType & key, bool singleton = false)
33  : PFactory<AbstractClass, KeyType>::WorkerBase(singleton)
34  {
36  }
37 
39  {
40  typedef typename PFactory<AbstractClass, KeyType>::WorkerBase WorkerBase_T;
41  typedef std::map<KeyType, WorkerBase_T *> KeyMap_T;
42  KeyType key;
43 
45 
46  typename KeyMap_T::const_iterator entry;
47  for (entry = km.begin(); entry != km.end(); ++entry) {
48  if (entry->second == this) {
49  key = entry->first;
50  break;
51  }
52  }
53  if (key != NULL)
55  }
56 
57  protected:
58  virtual AbstractClass * Create(const KeyType & key) const;
59  };
60 };
61 
63 {
64  public:
66  { }
68  { }
69  virtual void CreateFactory(const PString & device) = 0;
70 };
71 
72 template <typename DeviceBase>
74 {
75  public:
77  typedef typename Factory_T::Worker Worker_T;
78  void CreateFactory(const PString & device)
79  {
80  if (!(Factory_T::IsRegistered(device)))
81  new Worker_T(device, false);
82  }
83 };
84 
85 
86 #ifndef PWLIB_PLUGIN_API_VERSION
87 #define PWLIB_PLUGIN_API_VERSION 0
88 #endif
89 
90 
92 //
93 // Ancestor Service descriptor for plugins
94 //
95 
97 {
98  public:
101 
102  virtual unsigned GetPluginAPIVersion() const { return version; }
103 
104  protected:
105  unsigned version;
106 };
107 
108 
110 {
111  public:
112  static const char SeparatorChar;
113 
114  virtual PObject * CreateInstance(int userData) const = 0;
115  virtual PStringArray GetDeviceNames(int userData) const = 0;
116  virtual bool ValidateDeviceName(const PString & deviceName, int userData) const;
117  virtual bool GetDeviceCapabilities(const PString & deviceName, void * capabilities) const;
118 };
119 
120 
122 //
123 // Define a service provided by a plugin, which consists of the following:
124 //
125 // serviceType - the base class name of the service which is used to identify
126 // the service type, such as PSoundChannel,
127 //
128 // serviceName - the name of the service provided by the plugin. This will usually
129 // be related to the class implementing the service, such as:
130 // service name = OSS, class name = PSoundChannelOSS
131 //
132 // descriptor - a pointer to a class containing pointers to any static functions
133 // for this class
134 //
135 //
136 
137 class PPluginService: public PObject
138 {
139  public:
140  PPluginService(const PString & name,
141  const PString & type,
143  : serviceName(name)
144  , serviceType(type)
145  , descriptor(desc)
146  {
147  }
148 
152 };
153 
154 
156 //
157 // These crazy macros are needed to cause automatic registration of
158 // static plugins. They are made more complex by the arcane behaviour
159 // of the Windows link system that requires an external reference in the
160 // object module before it will instantiate any globals in in it
161 //
162 
163 #define PCREATE_PLUGIN_REGISTERER(serviceName, serviceType, descriptor) \
164 class PPlugin_##serviceType##_##serviceName##_Registration { \
165  public: \
166  PPlugin_##serviceType##_##serviceName##_Registration(PPluginManager * pluginMgr) \
167  { \
168  static PDevicePluginFactory<serviceType>::Worker factory(#serviceName); \
169  pluginMgr->RegisterService(#serviceName, #serviceType, descriptor); \
170  } \
171  int kill_warning; \
172 }; \
173 
174 #ifdef _WIN32
175 
176 #define PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor) \
177 PCREATE_PLUGIN_REGISTERER(serviceName, serviceType, descriptor) \
178 PPlugin_##serviceType##_##serviceName##_Registration \
179  PPlugin_##serviceType##_##serviceName##_Registration_Instance(&PPluginManager::GetPluginManager()); \
180  int PPlugin_##serviceType##_##serviceName##_link() { return 0; }
181 
182 #define PPLUGIN_STATIC_LOAD(serviceName, serviceType) \
183  extern int PPlugin_##serviceType##_##serviceName##_link(); \
184  int const PPlugin_##serviceType##_##serviceName##_loader = PPlugin_##serviceType##_##serviceName##_link();
185 
186 // always define static plugins in Windows, since otherwise they seem not to work
187 #ifndef P_FORCE_STATIC_PLUGIN
188  #define P_FORCE_STATIC_PLUGIN 1
189 #endif
190 
191 #else
192 
193 #ifdef USE_GCC
194 #define PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor) \
195 static void __attribute__ (( constructor )) PWLIB_StaticLoader_##serviceName##_##serviceType() \
196 { PPluginManager::GetPluginManager().RegisterService(#serviceName, #serviceType, descriptor); } \
197  int PPlugin_##serviceType##_##serviceName##_link() { return 0; }
198 
199 #else
200 #define PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor) \
201 extern int PWLIB_gStaticLoader__##serviceName##_##serviceType; \
202 static int PWLIB_StaticLoader_##serviceName##_##serviceType() \
203 { PPluginManager::GetPluginManager().RegisterService(#serviceName, #serviceType, descriptor); return 1; } \
204  int PWLIB_gStaticLoader__##serviceName##_##serviceType = PWLIB_StaticLoader_##serviceName##_##serviceType(); \
205  int PPlugin_##serviceType##_##serviceName##_link() { return 0; }
206 #endif
207 
208 #define PPLUGIN_STATIC_LOAD(serviceName, serviceType) \
209  extern int PPlugin_##serviceType##_##serviceName##_link(); \
210  int const PPlugin_##serviceType##_##serviceName##_loader = PPlugin_##serviceType##_##serviceName##_link();
211 
212 #ifndef P_SHAREDLIB
213 #ifndef P_FORCE_STATIC_PLUGIN
214  #define P_FORCE_STATIC_PLUGIN 1
215 #endif
216 #endif
217 
218 #endif
219 
220 
222 
223 #if defined(P_PLUGINS) && ! defined(P_FORCE_STATIC_PLUGIN)
224 
225 # define PCREATE_PLUGIN(serviceName, serviceType, descriptor) \
226  PCREATE_PLUGIN_REGISTERER(serviceName, serviceType, descriptor) \
227  extern "C" void PWLibPlugin_TriggerRegister (PPluginManager * pluginMgr) { \
228  PPlugin_##serviceType##_##serviceName##_Registration \
229  pplugin_##serviceType##_##serviceName##_Registration_Instance(pluginMgr); \
230  pplugin_##serviceType##_##serviceName##_Registration_Instance.kill_warning = 0; \
231  } \
232  extern "C" unsigned PWLibPlugin_GetAPIVersion (void) \
233  { return PWLIB_PLUGIN_API_VERSION; }
234 
235 #else
236 
237 # define PCREATE_PLUGIN(serviceName, serviceType, descriptor) \
238  PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor)
239 
240 #endif
241 
243 
244 
245 #endif // PTLIB_PLUGIN_H
246 
247 
248 // End Of File ///////////////////////////////////////////////////////////////
std::map< Key_T, WorkerBase * > KeyMap_T
Definition: pfactory.h:223
virtual ~PDevicePluginAdapterBase()
Definition: plugin.h:67
static const char SeparatorChar
Definition: plugin.h:112
Worker(const KeyType &key, bool singleton=false)
Definition: plugin.h:32
Definition: plugin.h:73
static KeyMap_T & GetKeyMap()
Definition: pfactory.h:281
Definition: plugin.h:29
void CreateFactory(const PString &device)
Definition: plugin.h:78
Definition: plugin.h:109
static void Unregister(const Key_T &key)
Definition: pfactory.h:245
Definition: plugin.h:137
virtual unsigned GetPluginAPIVersion() const
Definition: plugin.h:102
PPluginServiceDescriptor * descriptor
Definition: plugin.h:151
PPluginServiceDescriptor()
Definition: plugin.h:99
virtual PStringArray GetDeviceNames(int userData) const =0
virtual AbstractClass * Create(const KeyType &key) const
~Worker()
Definition: plugin.h:38
#define PWLIB_PLUGIN_API_VERSION
Definition: plugin.h:87
This is an array collection class of PString objects.
Definition: pstring.h:2024
Definition: plugin.h:62
Factory_T::Worker Worker_T
Definition: plugin.h:77
virtual bool GetDeviceCapabilities(const PString &deviceName, void *capabilities) const
virtual bool ValidateDeviceName(const PString &deviceName, int userData) const
virtual PObject * CreateInstance(int userData) const =0
PString serviceName
Definition: plugin.h:149
virtual void CreateFactory(const PString &device)=0
unsigned version
Definition: plugin.h:105
PDevicePluginAdapterBase()
Definition: plugin.h:65
PPluginService(const PString &name, const PString &type, PPluginServiceDescriptor *desc)
Definition: plugin.h:140
static bool Register(const Key_T &key, WorkerBase *worker)
Definition: pfactory.h:226
The character string class.
Definition: pstring.h:108
virtual ~PPluginServiceDescriptor()
Definition: plugin.h:100
static bool IsRegistered(const Key_T &key)
Definition: pfactory.h:255
Template class for generic factories of an abstract class.
Definition: pfactory.h:144
Definition: plugin.h:96
Definition: pfactory.h:150
PDevicePluginFactory< DeviceBase > Factory_T
Definition: plugin.h:76
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
PString serviceType
Definition: plugin.h:150
Definition: plugin.h:26