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