test
CMS 3D CMS Logo

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