00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #ifndef _PLUGIN_H
00071 #define _PLUGIN_H
00072
00074
00075
00076
00077
00078 #include <ptlib/pfactory.h>
00079
00080 template <class _Abstract_T, typename _Key_T = PString>
00081 class PDevicePluginFactory : public PFactory<_Abstract_T, _Key_T>
00082 {
00083 public:
00084 class Worker : public PFactory<_Abstract_T, _Key_T>::WorkerBase
00085 {
00086 public:
00087 Worker(const _Key_T & key, bool singleton = false)
00088 : PFactory<_Abstract_T, _Key_T>::WorkerBase(singleton)
00089 {
00090 PFactory<_Abstract_T, _Key_T>::Register(key, this);
00091 }
00092
00093 ~Worker()
00094 {
00095 typedef typename PFactory<_Abstract_T, _Key_T>::WorkerBase WorkerBase_T;
00096 typedef std::map<_Key_T, WorkerBase_T *> KeyMap_T;
00097 _Key_T key;
00098
00099 KeyMap_T km = PFactory<_Abstract_T, _Key_T>::GetKeyMap();
00100
00101 typename KeyMap_T::const_iterator entry;
00102 for (entry = km.begin(); entry != km.end(); ++entry) {
00103 if (entry->second == this) {
00104 key = entry->first;
00105 break;
00106 }
00107 }
00108 if (key != NULL)
00109 PFactory<_Abstract_T, _Key_T>::Unregister(key);
00110 }
00111
00112 protected:
00113 virtual _Abstract_T * Create(const _Key_T & key) const;
00114 };
00115 };
00116
00117 class PDevicePluginAdapterBase
00118 {
00119 public:
00120 PDevicePluginAdapterBase()
00121 { }
00122 virtual ~PDevicePluginAdapterBase()
00123 { }
00124 virtual void CreateFactory(const PString & device) = 0;
00125 };
00126
00127 template <typename DeviceBase>
00128 class PDevicePluginAdapter : public PDevicePluginAdapterBase
00129 {
00130 public:
00131 typedef PDevicePluginFactory<DeviceBase> Factory_T;
00132 typedef typename Factory_T::Worker Worker_T;
00133 void CreateFactory(const PString & device)
00134 {
00135 if (!(Factory_T::IsRegistered(device)))
00136 new Worker_T(device, FALSE);
00137 }
00138 };
00139
00140
00141 #ifndef PWLIB_PLUGIN_API_VERSION
00142 #define PWLIB_PLUGIN_API_VERSION 0
00143 #endif
00144
00145
00147
00148
00149
00150
00151 class PPluginServiceDescriptor
00152 {
00153 public:
00154 PPluginServiceDescriptor() { version = PWLIB_PLUGIN_API_VERSION; }
00155 virtual ~PPluginServiceDescriptor() { }
00156
00157 virtual unsigned GetPluginAPIVersion() const { return version; }
00158
00159 protected:
00160 unsigned version;
00161 };
00162
00163
00164 class PDevicePluginServiceDescriptor : public PPluginServiceDescriptor
00165 {
00166 public:
00167 static const char SeparatorChar;
00168
00169 virtual PObject * CreateInstance(int userData) const = 0;
00170 virtual PStringList GetDeviceNames(int userData) const = 0;
00171 virtual bool ValidateDeviceName(const PString & deviceName, int userData) const;
00172 };
00173
00174
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 class PPluginService: public PObject
00192 {
00193 public:
00194 PPluginService(const PString & _serviceName,
00195 const PString & _serviceType,
00196 PPluginServiceDescriptor *_descriptor)
00197 {
00198 serviceName = _serviceName;
00199 serviceType = _serviceType;
00200 descriptor = _descriptor;
00201 }
00202
00203 PString serviceName;
00204 PString serviceType;
00205 PPluginServiceDescriptor * descriptor;
00206 };
00207
00208
00210
00211
00212
00213
00214
00215
00216
00217 #define PCREATE_PLUGIN_REGISTERER(serviceName, serviceType, descriptor) \
00218 class PPlugin_##serviceType##_##serviceName##_Registration { \
00219 public: \
00220 PPlugin_##serviceType##_##serviceName##_Registration(PPluginManager * pluginMgr) \
00221 { \
00222 static PDevicePluginFactory<serviceType>::Worker factory(#serviceName); \
00223 pluginMgr->RegisterService(#serviceName, #serviceType, descriptor); \
00224 } \
00225 int kill_warning; \
00226 }; \
00227
00228 #ifdef _WIN32
00229
00230 #define PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor) \
00231 PCREATE_PLUGIN_REGISTERER(serviceName, serviceType, descriptor) \
00232 PPlugin_##serviceType##_##serviceName##_Registration \
00233 PPlugin_##serviceType##_##serviceName##_Registration_Instance(&PPluginManager::GetPluginManager()); \
00234
00235 #define PWLIB_STATIC_LOAD_PLUGIN(serviceName, serviceType) \
00236 class PPlugin_##serviceType##_##serviceName##_Registration; \
00237 extern PPlugin_##serviceType##_##serviceName##_Registration PPlugin_##serviceType##_##serviceName##_Registration_Instance; \
00238 static PPlugin_##serviceType##_##serviceName##_Registration * PPlugin_##serviceType##_##serviceName##_Registration_Static_Library_Loader = &PPlugin_##serviceType##_##serviceName##_Registration_Instance;
00239
00240
00241 #define P_FORCE_STATIC_PLUGIN
00242
00243 #else
00244
00245 #ifdef USE_GCC
00246 #define PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor) \
00247 static void __attribute__ (( constructor )) PWLIB_StaticLoader_##serviceName##_##serviceType() \
00248 { PPluginManager::GetPluginManager().RegisterService(#serviceName, #serviceType, descriptor); } \
00249
00250 #else
00251 #define PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor) \
00252 extern int PWLIB_gStaticLoader__##serviceName##_##serviceType; \
00253 static int PWLIB_StaticLoader_##serviceName##_##serviceType() \
00254 { PPluginManager::GetPluginManager().RegisterService(#serviceName, #serviceType, descriptor); return 1; } \
00255 int PWLIB_gStaticLoader__##serviceName##_##serviceType = PWLIB_StaticLoader_##serviceName##_##serviceType();
00256 #endif
00257 #define PWLIB_STATIC_LOAD_PLUGIN(serviceName, serviceType)
00258
00259 #endif
00260
00261
00263
00264 #if defined(P_HAS_PLUGINS) && ! defined(P_FORCE_STATIC_PLUGIN)
00265
00266 # define PCREATE_PLUGIN(serviceName, serviceType, descriptor) \
00267 PCREATE_PLUGIN_REGISTERER(serviceName, serviceType, descriptor) \
00268 extern "C" void PWLibPlugin_TriggerRegister (PPluginManager * pluginMgr) { \
00269 PPlugin_##serviceType##_##serviceName##_Registration \
00270 pplugin_##serviceType##_##serviceName##_Registration_Instance(pluginMgr); \
00271 pplugin_##serviceType##_##serviceName##_Registration_Instance.kill_warning = 0; \
00272 } \
00273 extern "C" unsigned PWLibPlugin_GetAPIVersion (void) \
00274 { return PWLIB_PLUGIN_API_VERSION; }
00275
00276 #else
00277
00278 # define PCREATE_PLUGIN(serviceName, serviceType, descriptor) \
00279 PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor)
00280
00281 #endif
00282
00284
00285 #endif