CMS 3D CMS Logo

ComponentFactory.h
Go to the documentation of this file.
1 #ifndef Framework_ComponentFactory_h
2 #define Framework_ComponentFactory_h
3 // -*- C++ -*-
4 //
5 // Package: Framework
6 // Class : ComponentFactory
7 //
16 //
17 // Author: Chris Jones
18 // Created: Wed May 25 15:21:05 EDT 2005
19 //
20 
21 // system include files
22 #include <string>
23 #include <map>
24 #include <memory>
25 #include <exception>
26 
27 // user include files
36 
37 // forward declarations
38 namespace edm {
39  class ModuleTypeResolverMaker;
40 
41  namespace eventsetup {
42  class EventSetupProvider;
43  class EventSetupsController;
44 
45  template <typename T>
47  public:
49  //~ComponentFactory();
50 
52  typedef std::map<std::string, std::shared_ptr<Maker const>> MakerMap;
53  typedef typename T::base_type base_type;
54  // ---------- const member functions ---------------------
55  std::shared_ptr<base_type> addTo(EventSetupsController& esController,
56  EventSetupProvider& iProvider,
57  edm::ParameterSet& iConfiguration,
58  ModuleTypeResolverMaker const* resolverMaker,
59  bool replaceExisting = false) const {
60  std::string modtype = iConfiguration.template getParameter<std::string>("@module_type");
61  //cerr << "Factory: module_type = " << modtype << endl;
62  typename MakerMap::iterator it = makers_.find(modtype);
63  Maker const* maker = nullptr;
64 
65  if (it == makers_.end()) {
66  maker = detail::resolveMaker<edmplugin::PluginFactory<ComponentMakerBase<T>*()>>(
67  modtype, resolverMaker, iConfiguration, makers_);
68 
69  if (not maker) {
70  Exception::throwThis(errors::Configuration, "Maker Factory map insert failed");
71  }
72  } else {
73  maker = it->second.get();
74  }
75 
76  try {
77  return convertException::wrap([&]() -> std::shared_ptr<base_type> {
78  return maker->addTo(esController, iProvider, iConfiguration, replaceExisting);
79  });
80  } catch (cms::Exception& iException) {
81  std::string edmtype = iConfiguration.template getParameter<std::string>("@module_edm_type");
82  std::string label = iConfiguration.template getParameter<std::string>("@module_label");
83  std::ostringstream ost;
84  ost << "Constructing " << edmtype << ": class=" << modtype << " label='" << label << "'";
85  iException.addContext(ost.str());
86  throw;
87  }
88  return std::shared_ptr<base_type>();
89  }
90 
91  // ---------- static member functions --------------------
92  static ComponentFactory<T> const* get();
93 
94  // ---------- member functions ---------------------------
95 
96  private:
97  ComponentFactory(const ComponentFactory&); // stop default
98 
99  const ComponentFactory& operator=(const ComponentFactory&); // stop default
100 
101  // ---------- member data --------------------------------
102  //Creating a new component is not done across threads
104  };
105 
106  } // namespace eventsetup
107 } // namespace edm
108 #define COMPONENTFACTORY_GET(_type_) \
109  EDM_REGISTER_PLUGINFACTORY(edmplugin::PluginFactory<edm::eventsetup::ComponentMakerBase<_type_>*()>, \
110  _type_::name()); \
111  static edm::eventsetup::ComponentFactory<_type_> const s_dummyfactory; \
112  namespace edm { \
113  namespace eventsetup { \
114  template <> \
115  edm::eventsetup::ComponentFactory<_type_> const* edm::eventsetup::ComponentFactory<_type_>::get() { \
116  return &s_dummyfactory; \
117  } \
118  } \
119  } \
120  typedef int componentfactory_get_needs_semicolon
121 
122 #endif
#define CMS_SA_ALLOW
ComponentMakerBase< T > Maker
virtual std::shared_ptr< base_type > addTo(EventSetupsController &esController, EventSetupProvider &iProvider, ParameterSet &iConfiguration, bool replaceExisting) const =0
const ComponentFactory & operator=(const ComponentFactory &)
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
Definition: EDMException.cc:86
char const * label
std::map< std::string, std::shared_ptr< Maker const > > MakerMap
void addContext(std::string const &context)
Definition: Exception.cc:165
std::shared_ptr< base_type > addTo(EventSetupsController &esController, EventSetupProvider &iProvider, edm::ParameterSet &iConfiguration, ModuleTypeResolverMaker const *resolverMaker, bool replaceExisting=false) const
HLT enums.
auto wrap(F iFunc) -> decltype(iFunc())