00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _pplugin_h_
00021 #define _pplugin_h_
00022
00023 #include <pclasses/pexport.h>
00024 #include <pclasses/psharedlib.h>
00025 #include <typeinfo>
00026 #include <list>
00027 #include <map>
00028 #include <string>
00029
00030 namespace P {
00031
00033
00039 class PCORE_EXPORT PluginBase {
00040 public:
00041 PluginBase();
00042 virtual ~PluginBase();
00043 };
00044
00046
00052 struct PluginMetaInfo {
00053 const char* iface;
00054 const char* feature;
00055 PluginBase* (*create)();
00056 void (*destroy)(PluginBase* c);
00057 };
00058
00059 #define P_PLUGINS_BEGIN \
00060 extern "C" { \
00061 CXX_CLASS_EXPORT PluginMetaInfo P_plugin[] = {
00062
00063 #define P_PLUGIN(iface, feature, cl) \
00064 { typeid(iface).name(), feature, &cl::create, &cl::destroy },
00065
00066 #define P_PLUGINS_END \
00067 { 0, 0, 0, 0 } \
00068 }; }
00069
00071
00083 class PCORE_EXPORT PluginFactoryImpl {
00084 public:
00085 PluginFactoryImpl(const char* iface);
00086 virtual ~PluginFactoryImpl();
00087
00089 void addPlugin(const char* path) throw(SystemError);
00090
00092 void addPluginDir(const char* path) throw(SystemError, IOError);
00093
00095 PluginBase* create(const char* feature);
00096
00098 void destroy(PluginBase* inst);
00099
00101 bool provides(const char* feature) const;
00102
00103 private:
00104 struct Plugin;
00105 Plugin* find(const char* feature) const;
00106
00107 std::string m_iface;
00108 std::list<Plugin*> m_plugins;
00109 std::map<PluginBase*, Plugin*> m_pinst;
00110 };
00111
00112
00114
00115
00116
00117
00118
00119 template <class Iface>
00120 class PluginFactory: public PluginFactoryImpl {
00121 public:
00123
00127 inline PluginFactory()
00128 : PluginFactoryImpl(typeid(Iface).name())
00129 {}
00130
00131 inline ~PluginFactory()
00132 {}
00133
00135 inline Iface* create(const char* feature)
00136 { return dynamic_cast<Iface*>(PluginFactoryImpl::create(feature)); }
00137
00139 inline void destroy(Iface* inst)
00140 { PluginFactoryImpl::destroy(dynamic_cast<PluginBase*>(inst)); }
00141
00142 };
00143
00144 }
00145
00146 #endif