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 #ifndef _PLUGIN_H
00068 #define _PLUGIN_H
00069
00071
00072
00073
00074
00075 #include <ptlib/pfactory.h>
00076
00077 template <class _Abstract_T, typename _Key_T = PString>
00078 class PDevicePluginFactory : public PFactory<_Abstract_T, _Key_T>
00079 {
00080 public:
00081 class Worker : public PFactory<_Abstract_T, _Key_T>::WorkerBase
00082 {
00083 public:
00084 Worker(const _Key_T & key, bool singleton = false)
00085 : PFactory<_Abstract_T, _Key_T>::WorkerBase(singleton)
00086 {
00087 PFactory<_Abstract_T, _Key_T>::Register(key, this);
00088 }
00089
00090 ~Worker()
00091 {
00092 typedef typename PFactory<_Abstract_T, _Key_T>::WorkerBase WorkerBase_T;
00093 typedef std::map<_Key_T, WorkerBase_T *> KeyMap_T;
00094 _Key_T key;
00095
00096 KeyMap_T km = PFactory<_Abstract_T, _Key_T>::GetKeyMap();
00097
00098 typename KeyMap_T::const_iterator entry;
00099 for (entry = km.begin(); entry != km.end(); ++entry) {
00100 if (entry->second == this) {
00101 key = entry->first;
00102 break;
00103 }
00104 }
00105 if (key != NULL)
00106 PFactory<_Abstract_T, _Key_T>::Unregister(key);
00107 }
00108
00109 protected:
00110 virtual _Abstract_T * Create(const _Key_T & key) const;
00111 };
00112 };
00113
00114 class PDevicePluginAdapterBase
00115 {
00116 public:
00117 PDevicePluginAdapterBase()
00118 { }
00119 virtual ~PDevicePluginAdapterBase()
00120 { }
00121 virtual void CreateFactory(const PString & device) = 0;
00122 };
00123
00124 template <typename DeviceBase>
00125 class PDevicePluginAdapter : public PDevicePluginAdapterBase
00126 {
00127 public:
00128 typedef PDevicePluginFactory<DeviceBase> Factory_T;
00129 typedef typename Factory_T::Worker Worker_T;
00130 void CreateFactory(const PString & device)
00131 {
00132 if (!(Factory_T::IsRegistered(device)))
00133 new Worker_T(device, FALSE);
00134 }
00135 };
00136
00137 #define PWLIB_PLUGIN_API_VERSION 0
00138
00140
00141
00142
00143
00144 class PPluginServiceDescriptor
00145 {
00146 public:
00147 PPluginServiceDescriptor() { version = PWLIB_PLUGIN_API_VERSION; }
00148 virtual ~PPluginServiceDescriptor() { }
00149
00150 virtual unsigned GetPluginAPIVersion() const { return version; }
00151
00152 protected:
00153 unsigned version;
00154 };
00155
00156
00157 class PDevicePluginServiceDescriptor : public PPluginServiceDescriptor
00158 {
00159 public:
00160 static const char SeparatorChar;
00161
00162 virtual PObject * CreateInstance(int userData) const = 0;
00163 virtual PStringList GetDeviceNames(int userData) const = 0;
00164 virtual bool ValidateDeviceName(const PString & deviceName, int userData) const;
00165 };
00166
00167
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 class PPluginService: public PObject
00185 {
00186 public:
00187 PPluginService(const PString & _serviceName,
00188 const PString & _serviceType,
00189 PPluginServiceDescriptor *_descriptor)
00190 {
00191 serviceName = _serviceName;
00192 serviceType = _serviceType;
00193 descriptor = _descriptor;
00194 }
00195
00196 PString serviceName;
00197 PString serviceType;
00198 PPluginServiceDescriptor * descriptor;
00199 };
00200
00201
00203
00204
00205
00206
00207
00208
00209
00210 #define PCREATE_PLUGIN_REGISTERER(serviceName, serviceType, descriptor) \
00211 class PPlugin_##serviceType##_##serviceName##_Registration { \
00212 public: \
00213 PPlugin_##serviceType##_##serviceName##_Registration(PPluginManager * pluginMgr) \
00214 { \
00215 static PDevicePluginFactory<serviceType>::Worker factory(#serviceName); \
00216 pluginMgr->RegisterService(#serviceName, #serviceType, descriptor); \
00217 } \
00218 int kill_warning; \
00219 }; \
00220
00221 #ifdef _WIN32
00222
00223 #define PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor) \
00224 PCREATE_PLUGIN_REGISTERER(serviceName, serviceType, descriptor) \
00225 PPlugin_##serviceType##_##serviceName##_Registration \
00226 PPlugin_##serviceType##_##serviceName##_Registration_Instance(&PPluginManager::GetPluginManager()); \
00227
00228 #define PWLIB_STATIC_LOAD_PLUGIN(serviceName, serviceType) \
00229 class PPlugin_##serviceType##_##serviceName##_Registration; \
00230 extern PPlugin_##serviceType##_##serviceName##_Registration PPlugin_##serviceType##_##serviceName##_Registration_Instance; \
00231 static PPlugin_##serviceType##_##serviceName##_Registration * PPlugin_##serviceType##_##serviceName##_Registration_Static_Library_Loader = &PPlugin_##serviceType##_##serviceName##_Registration_Instance;
00232
00233
00234 #define P_FORCE_STATIC_PLUGIN
00235
00236 #else
00237
00238 #ifdef USE_GCC
00239 #define PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor) \
00240 static void __attribute__ (( constructor )) PWLIB_StaticLoader_##serviceName##_##serviceType() \
00241 { PPluginManager::GetPluginManager().RegisterService(#serviceName, #serviceType, descriptor); } \
00242
00243 #else
00244 #define PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor) \
00245 extern int PWLIB_gStaticLoader__##serviceName##_##serviceType; \
00246 static int PWLIB_StaticLoader_##serviceName##_##serviceType() \
00247 { PPluginManager::GetPluginManager().RegisterService(#serviceName, #serviceType, descriptor); return 1; } \
00248 int PWLIB_gStaticLoader__##serviceName##_##serviceType = PWLIB_StaticLoader_##serviceName##_##serviceType();
00249 #endif
00250 #define PWLIB_STATIC_LOAD_PLUGIN(serviceName, serviceType)
00251
00252 #endif
00253
00254
00256
00257 #if defined(P_HAS_PLUGINS) && ! defined(P_FORCE_STATIC_PLUGIN)
00258
00259 # define PCREATE_PLUGIN(serviceName, serviceType, descriptor) \
00260 PCREATE_PLUGIN_REGISTERER(serviceName, serviceType, descriptor) \
00261 extern "C" void PWLibPlugin_TriggerRegister (PPluginManager * pluginMgr) { \
00262 PPlugin_##serviceType##_##serviceName##_Registration \
00263 pplugin_##serviceType##_##serviceName##_Registration_Instance(pluginMgr); \
00264 pplugin_##serviceType##_##serviceName##_Registration_Instance.kill_warning = 0; \
00265 } \
00266 extern "C" unsigned PWLibPlugin_GetAPIVersion (void) \
00267 { return PWLIB_PLUGIN_API_VERSION; }
00268
00269 #else
00270
00271 # define PCREATE_PLUGIN(serviceName, serviceType, descriptor) \
00272 PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor)
00273
00274 #endif
00275
00277
00278 #endif