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