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