Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

pplugin.h

Go to the documentation of this file.
00001 /*
00002  *   P::Classes - Portable C++ Application Framework
00003  *   Copyright (C) 2000-2004  Christian Prochnow <cproch@seculogix.de>
00004  *
00005  *   This library is free software; you can redistribute it and/or
00006  *   modify it under the terms of the GNU Lesser General Public
00007  *   License as published by the Free Software Foundation; either
00008  *   version 2 of the License, or (at your option) any later version.
00009  *
00010  *   This library is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *   Lesser General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU Lesser General Public
00016  *   License along with this library; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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   \sa PluginFactoryImpl
00116   \ingroup core
00117   \author Christian Prochnow <cproch@seculogix.de>
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

Generated on Fri Mar 12 21:08:31 2004 for P::Classes by doxygen 1.3.3