CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/FWCore/Framework/interface/ComponentFactory.h

Go to the documentation of this file.
00001 #ifndef Framework_ComponentFactory_h
00002 #define Framework_ComponentFactory_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     Framework
00006 // Class  :     ComponentFactory
00007 // 
00016 //
00017 // Author:      Chris Jones
00018 // Created:     Wed May 25 15:21:05 EDT 2005
00019 //
00020 
00021 // system include files
00022 #include <string>
00023 #include <map>
00024 #include "boost/shared_ptr.hpp"
00025 
00026 // user include files
00027 #include "FWCore/PluginManager/interface/PluginFactory.h"
00028 #include "DataFormats/Provenance/interface/PassID.h"
00029 #include "DataFormats/Provenance/interface/ReleaseVersion.h"
00030 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00031 #include "FWCore/Framework/interface/ComponentMaker.h"
00032 #include "FWCore/Utilities/interface/EDMException.h"
00033 
00034 // forward declarations
00035 namespace edm {
00036    namespace eventsetup {
00037       class EventSetupProvider;
00038       
00039 template<typename T>
00040   class ComponentFactory
00041 {
00042 
00043    public:
00044    ComponentFactory(): makers_() {}
00045    //~ComponentFactory();
00046 
00047    typedef  ComponentMakerBase<T> Maker;
00048    typedef std::map<std::string, boost::shared_ptr<Maker> > MakerMap;
00049    typedef typename T::base_type base_type;
00050       // ---------- const member functions ---------------------
00051    boost::shared_ptr<base_type> addTo(EventSetupProvider& iProvider,
00052                   edm::ParameterSet const& iConfiguration,
00053                   std::string const& iProcessName,
00054                   ReleaseVersion const& iVersion,
00055                   PassID const& iPass) const
00056       {
00057          std::string modtype = iConfiguration.template getParameter<std::string>("@module_type");
00058          //cerr << "Factory: module_type = " << modtype << endl;
00059          typename MakerMap::iterator it = makers_.find(modtype);
00060          
00061          if(it == makers_.end())
00062          {
00063             boost::shared_ptr<Maker> wm(edmplugin::PluginFactory<ComponentMakerBase<T>* ()>::get()->create(modtype));
00064             
00065             if(wm.get() == 0) {
00066               Exception::throwThis(errors::Configuration,
00067               "UnknownModule",
00068                T::name().c_str(),
00069               " of type ",
00070               modtype.c_str(),
00071               " has not been registered.\n"
00072               "Perhaps your module type is misspelled or is not a "
00073               "framework plugin.\n"
00074               "Try running EdmPluginDump to obtain a list of "
00075               "available Plugins.");            
00076             }
00077             
00078             //cerr << "Factory: created the worker" << endl;
00079             
00080             std::pair<typename MakerMap::iterator,bool> ret =
00081                makers_.insert(std::pair<std::string,boost::shared_ptr<Maker> >(modtype,wm));
00082             
00083             if(ret.second == false) {
00084               Exception::throwThis(errors::Configuration,"Maker Factory map insert failed");
00085             }
00086             
00087             it = ret.first;
00088          }
00089          
00090          try {
00091             return it->second->addTo(iProvider,iConfiguration,iProcessName,iVersion,iPass);
00092          } catch(cms::Exception& iException) {
00093             Exception toThrow(errors::Configuration,"Error occurred while creating ");
00094             toThrow<<modtype<<"\n";
00095             toThrow.append(iException);
00096             toThrow.raise();
00097          }
00098          return boost::shared_ptr<base_type>();
00099       }
00100    
00101       // ---------- static member functions --------------------
00102       static ComponentFactory<T>* get();
00103 
00104       // ---------- member functions ---------------------------
00105 
00106    private:
00107       
00108       ComponentFactory(const ComponentFactory&); // stop default
00109 
00110       const ComponentFactory& operator=(const ComponentFactory&); // stop default
00111 
00112       // ---------- member data --------------------------------
00113       mutable MakerMap makers_;
00114 };
00115 
00116    }
00117 }
00118 #define COMPONENTFACTORY_GET(_type_) \
00119 EDM_REGISTER_PLUGINFACTORY(edmplugin::PluginFactory<edm::eventsetup::ComponentMakerBase<_type_>* ()>,_type_::name()); \
00120 static edm::eventsetup::ComponentFactory<_type_> s_dummyfactory; \
00121 namespace edm { namespace eventsetup { \
00122 template<> edm::eventsetup::ComponentFactory<_type_>* edm::eventsetup::ComponentFactory<_type_>::get() \
00123 { return &s_dummyfactory; } \
00124   } } \
00125 typedef int componentfactory_get_needs_semicolon
00126 
00127 #endif