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 ModuleTypeResolverBase;
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>> 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  ModuleTypeResolverBase const* resolver,
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 
64  if (it == makers_.end()) {
65  std::shared_ptr<Maker> wm(
67 
68  //cerr << "Factory: created the worker" << endl;
69 
70  std::pair<typename MakerMap::iterator, bool> ret =
71  makers_.insert(std::pair<std::string, std::shared_ptr<Maker>>(modtype, wm));
72 
73  if (ret.second == false) {
74  Exception::throwThis(errors::Configuration, "Maker Factory map insert failed");
75  }
76 
77  it = ret.first;
78  }
79 
80  try {
81  return convertException::wrap([&]() -> std::shared_ptr<base_type> {
82  return it->second->addTo(esController, iProvider, iConfiguration, replaceExisting);
83  });
84  } catch (cms::Exception& iException) {
85  std::string edmtype = iConfiguration.template getParameter<std::string>("@module_edm_type");
86  std::string label = iConfiguration.template getParameter<std::string>("@module_label");
87  std::ostringstream ost;
88  ost << "Constructing " << edmtype << ": class=" << modtype << " label='" << label << "'";
89  iException.addContext(ost.str());
90  throw;
91  }
92  return std::shared_ptr<base_type>();
93  }
94 
95  // ---------- static member functions --------------------
96  static ComponentFactory<T> const* get();
97 
98  // ---------- member functions ---------------------------
99 
100  private:
101  ComponentFactory(const ComponentFactory&); // stop default
102 
103  const ComponentFactory& operator=(const ComponentFactory&); // stop default
104 
105  // ---------- member data --------------------------------
106  //Creating a new component is not done across threads
108  };
109 
110  } // namespace eventsetup
111 } // namespace edm
112 #define COMPONENTFACTORY_GET(_type_) \
113  EDM_REGISTER_PLUGINFACTORY(edmplugin::PluginFactory<edm::eventsetup::ComponentMakerBase<_type_>*()>, \
114  _type_::name()); \
115  static edm::eventsetup::ComponentFactory<_type_> const s_dummyfactory; \
116  namespace edm { \
117  namespace eventsetup { \
118  template <> \
119  edm::eventsetup::ComponentFactory<_type_> const* edm::eventsetup::ComponentFactory<_type_>::get() { \
120  return &s_dummyfactory; \
121  } \
122  } \
123  } \
124  typedef int componentfactory_get_needs_semicolon
125 
126 #endif
#define CMS_SA_ALLOW
ComponentMakerBase< T > Maker
ret
prodAgent to be discontinued
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:84
char const * label
auto resolveMaker(std::string const &moduleType, ModuleTypeResolverBase const *resolver)
Definition: resolveMaker.h:16
std::shared_ptr< base_type > addTo(EventSetupsController &esController, EventSetupProvider &iProvider, edm::ParameterSet &iConfiguration, ModuleTypeResolverBase const *resolver, bool replaceExisting=false) const
void addContext(std::string const &context)
Definition: Exception.cc:165
std::map< std::string, std::shared_ptr< Maker > > MakerMap
HLT enums.
auto wrap(F iFunc) -> decltype(iFunc())