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 <exception>
25 #include "boost/shared_ptr.hpp"
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, boost::shared_ptr<Maker> > MakerMap;
51  typedef typename T::base_type base_type;
52  // ---------- const member functions ---------------------
53  boost::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  boost::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,boost::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  try {
93  return it->second->addTo(esController, iProvider, iConfiguration, replaceExisting);
94  }
95  catch (cms::Exception& e) { throw; }
96  catch(std::bad_alloc& bda) { convertException::badAllocToEDM(); }
99  catch(char const* c) { convertException::charPtrToEDM(c); }
100  catch (...) { convertException::unknownToEDM(); }
101  }
102  catch(cms::Exception & iException) {
103  std::string edmtype = iConfiguration.template getParameter<std::string>("@module_edm_type");
104  std::string label = iConfiguration.template getParameter<std::string>("@module_label");
105  std::ostringstream ost;
106  ost << "Constructing " << edmtype << ": class=" << modtype << " label='" << label << "'";
107  iException.addContext(ost.str());
108  throw;
109  }
110  return boost::shared_ptr<base_type>();
111  }
112 
113  // ---------- static member functions --------------------
114  static ComponentFactory<T>* get();
115 
116  // ---------- member functions ---------------------------
117 
118  private:
119 
120  ComponentFactory(const ComponentFactory&); // stop default
121 
122  const ComponentFactory& operator=(const ComponentFactory&); // stop default
123 
124  // ---------- member data --------------------------------
125  mutable MakerMap makers_;
126 };
127 
128  }
129 }
130 #define COMPONENTFACTORY_GET(_type_) \
131 EDM_REGISTER_PLUGINFACTORY(edmplugin::PluginFactory<edm::eventsetup::ComponentMakerBase<_type_>* ()>,_type_::name()); \
132 static edm::eventsetup::ComponentFactory<_type_> s_dummyfactory; \
133 namespace edm { namespace eventsetup { \
134 template<> edm::eventsetup::ComponentFactory<_type_>* edm::eventsetup::ComponentFactory<_type_>::get() \
135 { return &s_dummyfactory; } \
136  } } \
137 typedef int componentfactory_get_needs_semicolon
138 
139 #endif
ComponentMakerBase< T > Maker
std::map< std::string, boost::shared_ptr< Maker > > MakerMap
boost::shared_ptr< base_type > addTo(EventSetupsController &esController, EventSetupProvider &iProvider, edm::ParameterSet const &iConfiguration, bool replaceExisting=false) const
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="")
void stdToEDM(std::exception const &e)
void charPtrToEDM(char const *c)
void stringToEDM(std::string &s)
void addContext(std::string const &context)
Definition: Exception.cc:227
SurfaceDeformation * create(int type, const std::vector< double > &params)