Go to the documentation of this file.00001 #ifndef Framework_ComponentFactory_h
00002 #define Framework_ComponentFactory_h
00003
00004
00005
00006
00007
00016
00017
00018
00019
00020
00021
00022 #include <string>
00023 #include <map>
00024 #include "boost/shared_ptr.hpp"
00025
00026
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
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
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
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
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
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
00102 static ComponentFactory<T>* get();
00103
00104
00105
00106 private:
00107
00108 ComponentFactory(const ComponentFactory&);
00109
00110 const ComponentFactory& operator=(const ComponentFactory&);
00111
00112
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