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 <exception>
00025 #include "boost/shared_ptr.hpp"
00026
00027
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
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
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
00053 boost::shared_ptr<base_type> addTo(EventSetupsController& esController,
00054 EventSetupProvider& iProvider,
00055 edm::ParameterSet const& iConfiguration,
00056 bool replaceExisting = false) 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 Exception::throwThis(errors::Configuration,
00068 "UnknownModule",
00069 T::name().c_str(),
00070 " of type ",
00071 modtype.c_str(),
00072 " has not been registered.\n"
00073 "Perhaps your module type is misspelled or is not a "
00074 "framework plugin.\n"
00075 "Try running EdmPluginDump to obtain a list of "
00076 "available Plugins.");
00077 }
00078
00079
00080
00081 std::pair<typename MakerMap::iterator,bool> ret =
00082 makers_.insert(std::pair<std::string,boost::shared_ptr<Maker> >(modtype,wm));
00083
00084 if(ret.second == false) {
00085 Exception::throwThis(errors::Configuration,"Maker Factory map insert failed");
00086 }
00087
00088 it = ret.first;
00089 }
00090
00091 try {
00092 try {
00093 return it->second->addTo(esController, iProvider, iConfiguration, replaceExisting);
00094 }
00095 catch (cms::Exception& e) { throw; }
00096 catch(std::bad_alloc& bda) { convertException::badAllocToEDM(); }
00097 catch (std::exception& e) { convertException::stdToEDM(e); }
00098 catch(std::string& s) { convertException::stringToEDM(s); }
00099 catch(char const* c) { convertException::charPtrToEDM(c); }
00100 catch (...) { convertException::unknownToEDM(); }
00101 }
00102 catch(cms::Exception & iException) {
00103 std::string edmtype = iConfiguration.template getParameter<std::string>("@module_edm_type");
00104 std::string label = iConfiguration.template getParameter<std::string>("@module_label");
00105 std::ostringstream ost;
00106 ost << "Constructing " << edmtype << ": class=" << modtype << " label='" << label << "'";
00107 iException.addContext(ost.str());
00108 throw;
00109 }
00110 return boost::shared_ptr<base_type>();
00111 }
00112
00113
00114 static ComponentFactory<T>* get();
00115
00116
00117
00118 private:
00119
00120 ComponentFactory(const ComponentFactory&);
00121
00122 const ComponentFactory& operator=(const ComponentFactory&);
00123
00124
00125 mutable MakerMap makers_;
00126 };
00127
00128 }
00129 }
00130 #define COMPONENTFACTORY_GET(_type_) \
00131 EDM_REGISTER_PLUGINFACTORY(edmplugin::PluginFactory<edm::eventsetup::ComponentMakerBase<_type_>* ()>,_type_::name()); \
00132 static edm::eventsetup::ComponentFactory<_type_> s_dummyfactory; \
00133 namespace edm { namespace eventsetup { \
00134 template<> edm::eventsetup::ComponentFactory<_type_>* edm::eventsetup::ComponentFactory<_type_>::get() \
00135 { return &s_dummyfactory; } \
00136 } } \
00137 typedef int componentfactory_get_needs_semicolon
00138
00139 #endif