Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

plugin.h

Go to the documentation of this file.
00001 /*
00002  * plugin.h
00003  *
00004  * Plugin Class Declarations
00005  *
00006  * Portable Windows Library
00007  *
00008  * Contributor(s): Snark at GnomeMeeting
00009  *
00010  * $Log: plugin.h,v $
00011  * Revision 1.11  2004/08/16 11:57:47  csoutheren
00012  * More changes for VS.net
00013  *
00014  * Revision 1.10  2004/08/16 10:55:09  csoutheren
00015  * Fixed problems compiling under Linux
00016  *
00017  * Revision 1.9  2004/08/16 06:40:59  csoutheren
00018  * Added adapters template to make device plugins available via the abstract factory interface
00019  *
00020  * Revision 1.8  2004/06/21 10:40:02  csoutheren
00021  * Fixed problem with dynamic plugins
00022  *
00023  * Revision 1.7  2004/06/21 00:57:40  csoutheren
00024  * Changed service plugin static registration to use attribute (( constructor ))
00025  *
00026  * Revision 1.6  2003/12/19 00:34:27  csoutheren
00027  * Ensured that older compilers do not get confused about functions wth empty
00028  * parameter lists. Thanks to Kilian Krause
00029  *
00030  * Revision 1.5  2003/11/19 09:29:19  csoutheren
00031  * Added super hack to avoid problems with multiple plugins in a single file
00032  *
00033  * Revision 1.4  2003/11/12 10:24:35  csoutheren
00034  * Changes to allow operation of static plugins under Windows
00035  *
00036  * Revision 1.3  2003/11/12 06:58:21  csoutheren
00037  * Changes to help in making static plugins autoregister under Windows
00038  *
00039  * Revision 1.2  2003/11/12 03:26:17  csoutheren
00040  * Initial version of plugin code from Snark of GnomeMeeting with changes
00041  *    by Craig Southeren os Post Increment
00042  *
00043  *
00044  */
00045 
00046 #ifndef _PLUGIN_H
00047 #define _PLUGIN_H
00048 
00050 //
00051 //  these templates implement an adapter to make the old style device plugins appear in the new factory system
00052 //
00053 
00054 #include <ptlib/pfactory.h>
00055 
00056 template <class _Abstract_T, typename _Key_T = PString>
00057 class PDevicePluginFactory : public PFactory<_Abstract_T, _Key_T>
00058 {
00059   public:
00060     class Worker : public PFactory<_Abstract_T, _Key_T>::WorkerBase 
00061     {
00062       public:
00063         Worker(const _Key_T & key, bool singleton = false)
00064           : PFactory<_Abstract_T, _Key_T>::WorkerBase(singleton)
00065         {
00066           PFactory<_Abstract_T, _Key_T>::Register(key, this);
00067         }
00068 
00069       protected:
00070         virtual _Abstract_T * Create(const _Key_T & key) const;
00071     };
00072 };
00073 
00074 class PDevicePluginAdapterBase
00075 {
00076   public:
00077     PDevicePluginAdapterBase()
00078     { }
00079     virtual ~PDevicePluginAdapterBase()
00080     { }
00081     virtual void CreateFactory(const PString & device) = 0;
00082 };
00083 
00084 template <typename DeviceBase>
00085 class PDevicePluginAdapter : public PDevicePluginAdapterBase
00086 {
00087   public:
00088     typedef PDevicePluginFactory<DeviceBase> Factory_T;
00089     typedef typename Factory_T::Worker Worker_T;
00090     void CreateFactory(const PString & device)
00091     { new Worker_T(device, TRUE); }
00092 };
00093 
00094 #define PWLIB_PLUGIN_API_VERSION 0
00095 
00097 //
00098 //  Ancestor Service descriptor for plugins
00099 //
00100 
00101 class PPluginServiceDescriptor 
00102 {
00103   public:
00104     PPluginServiceDescriptor(unsigned (*_GetPluginAPIVersion)())
00105       : GetPluginAPIVersion(_GetPluginAPIVersion)
00106     { }
00107 
00108     unsigned (*GetPluginAPIVersion)();
00109 };
00110 
00111 
00113 //
00114 // Define a service provided by a plugin, which consists of the following:
00115 //
00116 //    serviceType - the base class name of the service which is used to identify
00117 //                  the service type, such as PSoundChannel, 
00118 //
00119 //    serviceName - the name of the service provided by the plugin. This will usually
00120 //                  be related to the class implementing the service, such as:
00121 //                       service name = OSS, class name = PSoundChannelOSS
00122 //
00123 //    descriptor  - a pointer to a class containing pointers to any static functions
00124 //                  for this class
00125 //   
00126 //
00127 
00128 class PPluginService: public PObject
00129 {
00130   public:
00131     PPluginService(const PString & _serviceName,
00132                    const PString & _serviceType,
00133                    PPluginServiceDescriptor *_descriptor)
00134     {
00135       serviceName = _serviceName;
00136       serviceType = _serviceType;
00137       descriptor  = _descriptor;
00138     }
00139 
00140     PString serviceName;
00141     PString serviceType;
00142     PPluginServiceDescriptor * descriptor;
00143 };
00144 
00146 
00147 #define PCREATE_PLUGIN_VERSION_DECLARE 
00148 
00149 #define PCREATE_STATIC_PLUGIN_VERSION_FN(serviceName, serviceType) \
00150 unsigned PPlugin_##serviceType##_##serviceName##_GetVersion() \
00151   { return PWLIB_PLUGIN_API_VERSION; } 
00152 
00153 #define PCREATE_DYNAMIC_PLUGIN_VERSION_FN(serviceName, serviceType) \
00154 extern "C" unsigned PWLibPlugin_GetAPIVersion (void) \
00155 { return PWLIB_PLUGIN_API_VERSION; } 
00156 
00158 //
00159 //  These crazy macros are needed to cause automatic registration of 
00160 //  static plugins. They are made more complex by the arcane behaviour
00161 //  of the Windows link system that requires an external reference in the
00162 //  object module before it will instantiate any globals in in it
00163 //
00164 
00165 #define PCREATE_PLUGIN_REGISTERER(serviceName, serviceType, descriptor) \
00166 class PPlugin_##serviceType##_##serviceName##_Registration { \
00167   public: \
00168     PPlugin_##serviceType##_##serviceName##_Registration(PPluginManager * pluginMgr) \
00169     { \
00170       static PDevicePluginFactory<serviceType>::Worker factory(#serviceName); \
00171       pluginMgr->RegisterService(#serviceName, #serviceType, descriptor); \
00172     } \
00173     int kill_warning; \
00174 }; \
00175 
00176 #ifdef _WIN32
00177 
00178 #define PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor) \
00179 PCREATE_PLUGIN_REGISTERER(serviceName, serviceType, descriptor) \
00180 PPlugin_##serviceType##_##serviceName##_Registration \
00181   PPlugin_##serviceType##_##serviceName##_Registration_Instance(&PPluginManager::GetPluginManager()); \
00182 
00183 #define PWLIB_STATIC_LOAD_PLUGIN(cls) \
00184   class PPlugin_##cls##_Registration; \
00185   extern PPlugin_##cls##_Registration PPlugin_##cls##_Registration_Instance; \
00186   static PPlugin_##cls##_Registration * PPlugin_##cls##_Registration_Static_Library_Loader = &PPlugin_##cls##_Registration_Instance
00187 
00188 #else
00189 
00190 #define PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor) \
00191 static void __attribute__ (( constructor )) PWLIB_StaticLoader_##serviceName##_##serviceType() \
00192 { PPluginManager::GetPluginManager().RegisterService(#serviceName, #serviceType, descriptor); } \
00193 
00194 #define PWLIB_STATIC_LOAD_PLUGIN(cls) 
00195 
00196 #endif
00197 
00198 #define PCREATE_PLUGIN_DYNAMIC(serviceName, serviceType, descriptor) \
00199 PCREATE_PLUGIN_REGISTERER(serviceName, serviceType, descriptor) \
00200 extern "C" void PWLibPlugin_TriggerRegister (PPluginManager * pluginMgr) { \
00201 PPlugin_##serviceType##_##serviceName##_Registration \
00202      pplugin_##serviceType##_##serviceName##_Registration_Instance(pluginMgr); \
00203      pplugin_##serviceType##_##serviceName##_Registration_Instance.kill_warning = 0; \
00204 } 
00205 
00207 
00208 #if defined(P_HAS_PLUGINS) && ! defined(P_FORCE_STATIC_PLUGIN)
00209 #  define PCREATE_PLUGIN(serviceName, serviceType, descriptor) \
00210     PCREATE_PLUGIN_DYNAMIC(serviceName, serviceType, descriptor)
00211 
00212 #  define PCREATE_PLUGIN_VERSION_FN(serviceName, serviceType) \
00213     PCREATE_DYNAMIC_PLUGIN_VERSION_FN(serviceName, serviceType)
00214 
00215 #  define PPLUGIN_VERSION_FN(serviceName, serviceType) \
00216     PWLibPlugin_GetAPIVersion
00217 
00218 #else
00219 
00220 #  define PCREATE_PLUGIN(serviceName, serviceType, descriptor) \
00221     PCREATE_PLUGIN_STATIC(serviceName, serviceType, descriptor)
00222 
00223 #  define PCREATE_PLUGIN_VERSION_FN(serviceName, serviceType) \
00224     PCREATE_STATIC_PLUGIN_VERSION_FN(serviceName, serviceType)
00225 
00226 #  define PPLUGIN_VERSION_FN(serviceName, serviceType) \
00227     PPlugin_##serviceType##_##serviceName##_GetVersion
00228 
00229 #endif
00230 
00232 
00233 #endif

Generated on Mon Feb 21 20:43:09 2005 for PWLib by  doxygen 1.4.1