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
35 
36 // forward declarations
37 namespace edm {
38  namespace eventsetup {
39  class EventSetupProvider;
40  class EventSetupsController;
41 
42  template <typename T>
44  public:
46  //~ComponentFactory();
47 
49  typedef std::map<std::string, std::shared_ptr<Maker> > MakerMap;
50  typedef typename T::base_type base_type;
51  // ---------- const member functions ---------------------
52  std::shared_ptr<base_type> addTo(EventSetupsController& esController,
53  EventSetupProvider& iProvider,
54  edm::ParameterSet const& iConfiguration,
55  bool replaceExisting = false) const {
56  std::string modtype = iConfiguration.template getParameter<std::string>("@module_type");
57  //cerr << "Factory: module_type = " << modtype << endl;
58  typename MakerMap::iterator it = makers_.find(modtype);
59 
60  if (it == makers_.end()) {
61  std::shared_ptr<Maker> wm(edmplugin::PluginFactory<ComponentMakerBase<T>*()>::get()->create(modtype));
62 
63  if (wm.get() == nullptr) {
65  "UnknownModule",
66  T::name().c_str(),
67  " of type ",
68  modtype.c_str(),
69  " has not been registered.\n"
70  "Perhaps your module type is misspelled or is not a "
71  "framework plugin.\n"
72  "Try running EdmPluginDump to obtain a list of "
73  "available Plugins.");
74  }
75 
76  //cerr << "Factory: created the worker" << endl;
77 
78  std::pair<typename MakerMap::iterator, bool> ret =
79  makers_.insert(std::pair<std::string, std::shared_ptr<Maker> >(modtype, wm));
80 
81  if (ret.second == false) {
82  Exception::throwThis(errors::Configuration, "Maker Factory map insert failed");
83  }
84 
85  it = ret.first;
86  }
87 
88  try {
89  return convertException::wrap([&]() -> std::shared_ptr<base_type> {
90  return it->second->addTo(esController, iProvider, iConfiguration, replaceExisting);
91  });
92  } catch (cms::Exception& iException) {
93  std::string edmtype = iConfiguration.template getParameter<std::string>("@module_edm_type");
94  std::string label = iConfiguration.template getParameter<std::string>("@module_label");
95  std::ostringstream ost;
96  ost << "Constructing " << edmtype << ": class=" << modtype << " label='" << label << "'";
97  iException.addContext(ost.str());
98  throw;
99  }
100  return std::shared_ptr<base_type>();
101  }
102 
103  // ---------- static member functions --------------------
104  static ComponentFactory<T> const* get();
105 
106  // ---------- member functions ---------------------------
107 
108  private:
109  ComponentFactory(const ComponentFactory&); // stop default
110 
111  const ComponentFactory& operator=(const ComponentFactory&); // stop default
112 
113  // ---------- member data --------------------------------
114  //Creating a new component is not done across threads
116  };
117 
118  } // namespace eventsetup
119 } // namespace edm
120 #define COMPONENTFACTORY_GET(_type_) \
121  EDM_REGISTER_PLUGINFACTORY(edmplugin::PluginFactory<edm::eventsetup::ComponentMakerBase<_type_>*()>, \
122  _type_::name()); \
123  static edm::eventsetup::ComponentFactory<_type_> const s_dummyfactory; \
124  namespace edm { \
125  namespace eventsetup { \
126  template <> \
127  edm::eventsetup::ComponentFactory<_type_> const* edm::eventsetup::ComponentFactory<_type_>::get() { \
128  return &s_dummyfactory; \
129  } \
130  } \
131  } \
132  typedef int componentfactory_get_needs_semicolon
133 
134 #endif
#define CMS_SA_ALLOW
ComponentMakerBase< T > Maker
def create(alignables, pedeDump, additionalData, outputFile, config)
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
std::shared_ptr< base_type > addTo(EventSetupsController &esController, EventSetupProvider &iProvider, edm::ParameterSet const &iConfiguration, 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())