CMS 3D CMS Logo

ComponentMaker.h
Go to the documentation of this file.
1 #ifndef Framework_ComponentMaker_h
2 #define Framework_ComponentMaker_h
3 // -*- C++ -*-
4 //
5 // Package: Framework
6 // Class : ComponentMaker
7 //
16 //
17 // Author: Chris Jones
18 // Created: Wed May 25 16:56:05 EDT 2005
19 //
20 
21 // system include files
22 #include <memory>
23 #include <string>
24 
25 #include <fmt/format.h>
26 
27 // user include files
33 
34 // forward declarations
35 
36 namespace edm {
37  namespace eventsetup {
38  class EventSetupProvider;
39  class EventSetupsController;
40  class ESProductResolverProvider;
41 
43  public:
45 
46  protected:
48  };
49 
50  template <class T>
52  public:
53  typedef typename T::base_type base_type;
54  virtual std::shared_ptr<base_type> addTo(EventSetupsController& esController,
55  EventSetupProvider& iProvider,
56  ParameterSet& iConfiguration,
57  bool replaceExisting) const = 0;
58  };
59 
60  template <class T, class TComponent>
61  class ComponentMaker : public ComponentMakerBase<T> {
62  public:
64  ComponentMaker(const ComponentMaker&) = delete; // stop default
65  const ComponentMaker& operator=(const ComponentMaker&) = delete; // stop default
66  typedef typename T::base_type base_type;
67 
68  // ---------- const member functions ---------------------
69  std::shared_ptr<base_type> addTo(EventSetupsController& esController,
70  EventSetupProvider& iProvider,
71  ParameterSet& iConfiguration,
72  bool replaceExisting) const override;
73 
74  // ---------- static member functions --------------------
75 
76  // ---------- member functions ---------------------------
77  private:
79  iProv->setDescription(iDesc);
80  }
82  iFinder->setDescriptionForFinder(iDesc);
83  }
85  //The 'appendToDataLabel' parameter was added very late in the development cycle and since
86  // the ParameterSet is not sent to the base class we must set the value after construction
87  iProv->setAppendToDataLabel(iPSet);
88  }
89  void setDescription(void*, const ComponentDescription&) const {}
90  void setDescriptionForFinder(void*, const ComponentDescription&) const {}
91  void setPostConstruction(void*, const edm::ParameterSet&) const {}
92  // ---------- member data --------------------------------
93  };
94 
95  template <class T, class TComponent>
96  std::shared_ptr<typename ComponentMaker<T, TComponent>::base_type> ComponentMaker<T, TComponent>::addTo(
97  EventSetupsController& esController,
98  EventSetupProvider& iProvider,
99  ParameterSet& iConfiguration,
100  bool replaceExisting) const {
101  // This adds components to the EventSetupProvider for the process. It might
102  // make a new component then add it or reuse a component from an earlier
103  // SubProcess or the top level process and add that.
104 
105  {
106  auto modtype = iConfiguration.getParameter<std::string>("@module_type");
107  auto moduleLabel = iConfiguration.getParameter<std::string>("@module_label");
108  try {
110  ConfigurationDescriptions descriptions(T::baseType(), modtype);
111  fillDetails::fillIfExists<TComponent>(descriptions);
112  fillDetails::prevalidateIfExists<TComponent>(descriptions);
113  descriptions.validate(iConfiguration, moduleLabel);
114  iConfiguration.registerIt();
115  });
116  } catch (cms::Exception& iException) {
117  iException.addContext(fmt::format(
118  "Validating configuration of {} of type {} with label: '{}'", T::baseType(), modtype, moduleLabel));
119  throw;
120  }
121  }
122 
123  if (!replaceExisting) {
124  std::shared_ptr<typename T::base_type> alreadyMadeComponent =
125  T::getComponentAndRegisterProcess(esController, iConfiguration);
126 
127  if (alreadyMadeComponent) {
128  // This is for the case when a component is shared between
129  // a SubProcess and a previous SubProcess or the top level process
130  // because the component has an identical configuration to a component
131  // from the top level process or earlier SubProcess.
132  std::shared_ptr<TComponent> component(
133  std::static_pointer_cast<TComponent, typename T::base_type>(alreadyMadeComponent));
134  T::addTo(iProvider, component, iConfiguration, true);
135  return component;
136  }
137  }
138 
139  std::shared_ptr<TComponent> component = std::make_shared<TComponent>(iConfiguration);
140  ComponentDescription description = this->createComponentDescription(iConfiguration);
141 
142  this->setDescription(component.get(), description);
143  this->setDescriptionForFinder(component.get(), description);
144  this->setPostConstruction(component.get(), iConfiguration);
145 
146  if (replaceExisting) {
147  // This case is for ESProducers where in the first pass
148  // the algorithm thought the component could be shared
149  // across SubProcess's because there was an ESProducer
150  // from a previous process with an identical configuration.
151  // But in a later check it was determined that sharing was not
152  // possible because other components associated with the
153  // same record or records that record depends on had
154  // differing configurations.
155  T::replaceExisting(iProvider, component);
156  } else {
157  // This is for the case when a new component is being constructed.
158  // All components for the top level process fall in this category.
159  // Or it could be a SubProcess where neither the top level process
160  // nor any prior SubProcess had a component with exactly the same configuration.
161  T::addTo(iProvider, component, iConfiguration, false);
162  T::putComponent(esController, iConfiguration, component);
163  }
164  return component;
165  }
166  } // namespace eventsetup
167 } // namespace edm
168 #endif
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
ComponentDescription createComponentDescription(ParameterSet const &iConfiguration) const
void validate(ParameterSet &pset, std::string const &moduleLabel) const
void setDescriptionForFinder(void *, const ComponentDescription &) const
std::shared_ptr< base_type > addTo(EventSetupsController &esController, EventSetupProvider &iProvider, ParameterSet &iConfiguration, bool replaceExisting) const override
virtual std::shared_ptr< base_type > addTo(EventSetupsController &esController, EventSetupProvider &iProvider, ParameterSet &iConfiguration, bool replaceExisting) const =0
void setDescription(void *, const ComponentDescription &) const
void setDescription(const ComponentDescription &iDescription)
ParameterSet const & registerIt()
void setDescription(ESProductResolverProvider *iProv, const ComponentDescription &iDesc) const
void setPostConstruction(ESProductResolverProvider *iProv, const edm::ParameterSet &iPSet) const
void setPostConstruction(void *, const edm::ParameterSet &) const
void setDescriptionForFinder(const eventsetup::ComponentDescription &iDescription)
void addContext(std::string const &context)
Definition: Exception.cc:169
void setAppendToDataLabel(const edm::ParameterSet &)
void setDescriptionForFinder(EventSetupRecordIntervalFinder *iFinder, const ComponentDescription &iDesc) const
HLT enums.
const ComponentMaker & operator=(const ComponentMaker &)=delete
auto wrap(F iFunc) -> decltype(iFunc())