CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/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<typename R, typename... Args>
00035 class PluginFactory<R*(Args...)> : public PluginFactoryBase
00036 {
00037       friend class DummyFriend;
00038    public:
00039       typedef R* TemplateArgType(Args...);
00040 
00041       struct PMakerBase {
00042         virtual R* create(Args...) const = 0;
00043         virtual ~PMakerBase() {}
00044       };
00045       template<class TPlug>
00046       struct PMaker : public PMakerBase {
00047         PMaker(const std::string& iName) {
00048           PluginFactory<R*(Args...)>::get()->registerPMaker(this,iName);
00049         }
00050         virtual R* create(Args... args) const {
00051           return new TPlug(args...);
00052         }
00053       };
00054 
00055       // ---------- const member functions ---------------------
00056       virtual const std::string& category() const ;
00057       
00058       R* create(const std::string& iName, Args... args) const {
00059         return reinterpret_cast<PMakerBase*>(PluginFactoryBase::findPMaker(iName)->second.front().first)->create(args...);
00060       }
00061 
00063       R* tryToCreate(const std::string& iName, Args... args) 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(args...);
00069       }
00070       // ---------- static member functions --------------------
00071 
00072       static PluginFactory<R*(Args...)>* 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&) = delete; // stop default
00083 
00084       const PluginFactory& operator=(const PluginFactory&) = delete; // stop default
00085 
00086 };
00087 }
00088 #define CONCATENATE_HIDDEN(a,b) a ## b 
00089 #define CONCATENATE(a,b) CONCATENATE_HIDDEN(a,b)
00090 #define EDM_REGISTER_PLUGINFACTORY(_factory_,_category_) \
00091 namespace edmplugin {\
00092   template<> edmplugin::PluginFactory<_factory_::TemplateArgType>* edmplugin::PluginFactory<_factory_::TemplateArgType>::get() { static edmplugin::PluginFactory<_factory_::TemplateArgType> s_instance; return &s_instance;}\
00093   template<> const std::string& edmplugin::PluginFactory<_factory_::TemplateArgType>::category() const { static std::string s_cat(_category_);  return s_cat;}\
00094   } enum {CONCATENATE(dummy_edm_register_pluginfactory_, __LINE__)}
00095 
00096 #endif
00097 
00098 #define EDM_PLUGIN_SYM(x,y) EDM_PLUGIN_SYM2(x,y)
00099 #define EDM_PLUGIN_SYM2(x,y) x ## y
00100 
00101 #define DEFINE_EDM_PLUGIN(factory,type,name) \
00102 static factory::PMaker<type> EDM_PLUGIN_SYM(s_maker , __LINE__ ) (name)
00103