CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/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 <exception>
00025 #include "boost/shared_ptr.hpp"
00026 
00027 // user include files
00028 #include "FWCore/PluginManager/interface/PluginFactory.h"
00029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00030 #include "FWCore/Framework/interface/ComponentMaker.h"
00031 #include "FWCore/Utilities/interface/ConvertException.h"
00032 #include "FWCore/Utilities/interface/EDMException.h"
00033 #include "FWCore/Utilities/interface/Exception.h"
00034 
00035 // forward declarations
00036 namespace edm {
00037    namespace eventsetup {
00038       class EventSetupProvider;
00039       class EventSetupsController;
00040       
00041 template<typename T>
00042   class ComponentFactory
00043 {
00044 
00045    public:
00046    ComponentFactory(): makers_() {}
00047    //~ComponentFactory();
00048 
00049    typedef  ComponentMakerBase<T> Maker;
00050    typedef std::map<std::string, boost::shared_ptr<Maker> > MakerMap;
00051    typedef typename T::base_type base_type;
00052       // ---------- const member functions ---------------------
00053    boost::shared_ptr<base_type> addTo(EventSetupsController& esController,
00054                                       EventSetupProvider& iProvider,
00055                                       edm::ParameterSet const& iConfiguration) 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            try {
00092              return it->second->addTo(esController, iProvider, iConfiguration);
00093            }
00094            catch (cms::Exception& e) { throw; }
00095            catch(std::bad_alloc& bda) { convertException::badAllocToEDM(); }
00096            catch (std::exception& e) { convertException::stdToEDM(e); }
00097            catch(std::string& s) { convertException::stringToEDM(s); }
00098            catch(char const* c) { convertException::charPtrToEDM(c); }
00099            catch (...) { convertException::unknownToEDM(); }
00100          }
00101          catch(cms::Exception & iException) {
00102            std::string edmtype = iConfiguration.template getParameter<std::string>("@module_edm_type");
00103            std::string label = iConfiguration.template getParameter<std::string>("@module_label");
00104            std::ostringstream ost;
00105            ost << "Constructing " << edmtype << ": class=" << modtype << " label='" << label << "'";
00106            iException.addContext(ost.str());
00107            throw;
00108          }
00109          return boost::shared_ptr<base_type>();
00110       }
00111    
00112       // ---------- static member functions --------------------
00113       static ComponentFactory<T>* get();
00114 
00115       // ---------- member functions ---------------------------
00116 
00117    private:
00118       
00119       ComponentFactory(const ComponentFactory&); // stop default
00120 
00121       const ComponentFactory& operator=(const ComponentFactory&); // stop default
00122 
00123       // ---------- member data --------------------------------
00124       mutable MakerMap makers_;
00125 };
00126 
00127    }
00128 }
00129 #define COMPONENTFACTORY_GET(_type_) \
00130 EDM_REGISTER_PLUGINFACTORY(edmplugin::PluginFactory<edm::eventsetup::ComponentMakerBase<_type_>* ()>,_type_::name()); \
00131 static edm::eventsetup::ComponentFactory<_type_> s_dummyfactory; \
00132 namespace edm { namespace eventsetup { \
00133 template<> edm::eventsetup::ComponentFactory<_type_>* edm::eventsetup::ComponentFactory<_type_>::get() \
00134 { return &s_dummyfactory; } \
00135   } } \
00136 typedef int componentfactory_get_needs_semicolon
00137 
00138 #endif