CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/FWCore/PluginManager/interface/PluginFactory.h

Go to the documentation of this file.
00001 #ifndef FWCore_PluginManager_PluginFactory_h
00002 #define FWCore_PluginManager_PluginFactory_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     PluginManager
00006 // Class  :     PluginFactory
00007 // 
00016 //
00017 // Original Author:  Chris Jones
00018 //         Created:  Thu Apr  5 12:10:23 EDT 2007
00019 //
00020 
00021 // system include files
00022 #include <map>
00023 #include <vector>
00024 
00025 // user include files
00026 #include "FWCore/PluginManager/interface/PluginFactoryBase.h"
00027 #include "FWCore/PluginManager/interface/PluginManager.h"
00028 // forward declarations
00029 
00030 namespace edmplugin {
00031   template< class T> class PluginFactory;
00032   class DummyFriend;
00033   
00034 template<class R>
00035 class PluginFactory<R * (void)> : public PluginFactoryBase
00036 {
00037       friend class DummyFriend;
00038    public:
00039       typedef R* TemplateArgType(void);
00040 
00041       struct PMakerBase {
00042         virtual R* create(void) const = 0;
00043         virtual ~PMakerBase() {}
00044       };
00045       template<class TPlug>
00046       struct PMaker : public PMakerBase {
00047         PMaker(const std::string& iName) {
00048           PluginFactory<R*(void)>::get()->registerPMaker(this,iName);
00049         }
00050         virtual R* create() const {
00051           return new TPlug();
00052         }
00053       };
00054 
00055       // ---------- const member functions ---------------------
00056       virtual const std::string& category() const ;
00057       
00058       R* create(const std::string& iName) const {
00059         return reinterpret_cast<PMakerBase*>(PluginFactoryBase::findPMaker(iName)->second.front().first)->create();
00060       }
00061 
00063       R* tryToCreate(const std::string& iName) const {
00064         typename Plugins::const_iterator itFound = PluginFactoryBase::tryToFindPMaker(iName);
00065         if(itFound ==m_plugins.end() ) {
00066           return 0;
00067         }
00068         return reinterpret_cast<PMakerBase*>(itFound->second.front().first)->create();
00069       }
00070       // ---------- static member functions --------------------
00071 
00072       static PluginFactory<R*(void)>* get();
00073       // ---------- member functions ---------------------------
00074       void registerPMaker(PMakerBase* iPMaker, const std::string& iName) {
00075         PluginFactoryBase::registerPMaker(iPMaker, iName);
00076       }
00077 
00078    private:
00079       PluginFactory() {
00080         finishedConstruction();
00081       }
00082       PluginFactory(const PluginFactory&); // stop default
00083 
00084       const PluginFactory& operator=(const PluginFactory&); // stop default
00085 
00086 };
00087 
00088 template<class R, class Arg>
00089 class PluginFactory<R * (Arg)> : public PluginFactoryBase
00090 {
00091   friend class DummyFriend;
00092 public:
00093   typedef R *TemplateArgType(Arg);
00094   struct PMakerBase {
00095     virtual R* create(Arg) const = 0;
00096     virtual ~PMakerBase() {}
00097   };
00098   template<class TPlug>
00099     struct PMaker : public PMakerBase {
00100       PMaker(const std::string& iName) {
00101         PluginFactory<R*(Arg)>::get()->registerPMaker(this,iName);
00102       }
00103       virtual R* create(Arg iArg) const {
00104         return new TPlug(iArg);
00105       }
00106     };
00107   
00108   // ---------- const member functions ---------------------
00109   virtual const std::string& category() const ;
00110   
00111   R* create(const std::string& iName, Arg iArg) const {
00112     return reinterpret_cast<PMakerBase *>(PluginFactoryBase::findPMaker(iName)->second.front().first)->create(iArg);
00113   }
00115   R* tryToCreate(const std::string& iName, Arg iArg) const {
00116     typename Plugins::const_iterator itFound = PluginFactoryBase::tryToFindPMaker(iName);
00117     if(itFound ==m_plugins.end() ) {
00118       return 0;
00119     }
00120     return reinterpret_cast<PMakerBase *>(itFound->second.front().first)->create(iArg);
00121   }
00122   // ---------- static member functions --------------------
00123   
00124   static PluginFactory<R*(Arg)>* get();
00125   // ---------- member functions ---------------------------
00126   void registerPMaker(PMakerBase* iPMaker, const std::string& iName) {
00127     PluginFactoryBase::registerPMaker(iPMaker, iName);
00128   }
00129   
00130 private:
00131     PluginFactory() {
00132       finishedConstruction();
00133     }
00134   PluginFactory(const PluginFactory&); // stop default
00135   
00136   const PluginFactory& operator=(const PluginFactory&); // stop default
00137   
00138 };
00139 
00140 template<class R, class Arg1, class Arg2>
00141 class PluginFactory<R * (Arg1, Arg2)> : public PluginFactoryBase
00142 {
00143   friend class DummyFriend;
00144 public:
00145   typedef R *TemplateArgType(Arg1,Arg2);
00146   struct PMakerBase {
00147     virtual R* create(Arg1, Arg2) const = 0;
00148     virtual ~PMakerBase() {}
00149   };
00150   template<class TPlug>
00151     struct PMaker : public PMakerBase {
00152       PMaker(const std::string& iName) {
00153         PluginFactory<R*(Arg1,Arg2)>::get()->registerPMaker(this,iName);
00154       }
00155       virtual R* create(Arg1 iArg1, Arg2 iArg2) const {
00156         return new TPlug(iArg1, iArg2);
00157       }
00158     };
00159   
00160   // ---------- const member functions ---------------------
00161   virtual const std::string& category() const ;
00162   
00163   R* create(const std::string& iName, Arg1 iArg1, Arg2 iArg2) const {
00164     return reinterpret_cast<PMakerBase *>(PluginFactoryBase::findPMaker(iName)->second.front().first)->create(iArg1, iArg2);
00165   }
00167   R* tryToCreate(const std::string& iName, Arg1 iArg1, Arg2 iArg2) const {
00168     typename Plugins::const_iterator itFound = PluginFactoryBase::tryToFindPMaker(iName);
00169     if(itFound ==m_plugins.end() ) {
00170       return 0;
00171     }
00172     return reinterpret_cast<PMakerBase *>(itFound->second.front().first)->create(iArg1,iArg2);
00173   }
00174 
00175   // ---------- static member functions --------------------
00176   
00177   static PluginFactory<R*(Arg1,Arg2)>* get();
00178   // ---------- member functions ---------------------------
00179   void registerPMaker(PMakerBase* iPMaker, const std::string& iName) {
00180     PluginFactoryBase::registerPMaker(iPMaker, iName);
00181   }
00182   
00183 private:
00184     PluginFactory() {
00185       finishedConstruction();
00186     }
00187   PluginFactory(const PluginFactory&); // stop default
00188   
00189   const PluginFactory& operator=(const PluginFactory&); // stop default
00190   
00191 };
00192 
00193 }
00194 #define CONCATENATE_HIDDEN(a,b) a ## b 
00195 #define CONCATENATE(a,b) CONCATENATE_HIDDEN(a,b)
00196 #define EDM_REGISTER_PLUGINFACTORY(_factory_,_category_) \
00197 namespace edmplugin {\
00198   template<> edmplugin::PluginFactory<_factory_::TemplateArgType>* edmplugin::PluginFactory<_factory_::TemplateArgType>::get() { static edmplugin::PluginFactory<_factory_::TemplateArgType> s_instance; return &s_instance;}\
00199   template<> const std::string& edmplugin::PluginFactory<_factory_::TemplateArgType>::category() const { static std::string s_cat(_category_);  return s_cat;}\
00200   } enum {CONCATENATE(dummy_edm_register_pluginfactory_, __LINE__)}
00201 
00202 #endif
00203 
00204 #define EDM_PLUGIN_SYM(x,y) EDM_PLUGIN_SYM2(x,y)
00205 #define EDM_PLUGIN_SYM2(x,y) x ## y
00206 
00207 #define DEFINE_EDM_PLUGIN(factory,type,name) \
00208 static factory::PMaker<type> EDM_PLUGIN_SYM(s_maker , __LINE__ ) (name)
00209