CMS 3D CMS Logo

Factory.cc
Go to the documentation of this file.
1 
8 
9 #include <iostream>
10 
12 namespace edm {
13 
14  static void cleanup(const Factory::MakerMap::value_type& v) { delete v.second.get(); }
15 
16  Factory const Factory::singleInstance_;
17 
19 
21  : makers_()
22 
23  {}
24 
25  Factory const* Factory::get() { return &singleInstance_; }
26 
28  const MakeModuleParams& p,
29  std::string const& modtype,
30  ModuleTypeResolverBase const* resolver) {
31  if (not resolver) {
32  throw except;
33  }
34  //if needed, create list of alternative types that were tried
35  std::string alternativeTypes;
36  auto index = resolver->kInitialIndex;
37  auto newType = modtype;
38  int tries = 0;
39  do {
40  ++tries;
41  if (not alternativeTypes.empty()) {
42  alternativeTypes.append(", ");
43  }
44  auto [ttype, tindex] = resolver->resolveType(std::move(newType), index);
45  newType = std::move(ttype);
46  index = tindex;
47  alternativeTypes.append(newType);
48  } while (index != resolver->kLastIndex);
49  if (tries == 1 and alternativeTypes == modtype) {
50  throw except;
51  }
52  alternativeTypes.insert(0, "These alternative types were tried: ");
53  except.addAdditionalInfo(alternativeTypes);
54  throw except;
55  }
56 
58  std::string modtype = p.pset_->getParameter<std::string>("@module_type");
59  FDEBUG(1) << "Factory: module_type = " << modtype << std::endl;
60  MakerMap::iterator it = makers_.find(modtype);
61 
62  if (it == makers_.end()) {
63  auto make = [](auto resolver, const auto& modtype, auto const& p) {
64  if (resolver) {
65  auto index = resolver->kInitialIndex;
66  auto newType = modtype;
67  do {
68  auto [ttype, tindex] = resolver->resolveType(std::move(newType), index);
69  newType = std::move(ttype);
70  index = tindex;
71  auto m = MakerPluginFactory::get()->tryToCreate(newType);
72  if (m) {
73  return m;
74  }
75  } while (index != resolver->kLastIndex);
76  try {
77  //failed to find a plugin
78  return MakerPluginFactory::get()->create(modtype);
79  } catch (cms::Exception& iExcept) {
80  annotateExceptionAndRethrow(iExcept, p, modtype, resolver);
81  }
82  }
83  return MakerPluginFactory::get()->create(modtype);
84  };
85  std::unique_ptr<Maker> wm = make(resolver, modtype, p);
86  FDEBUG(1) << "Factory: created worker of type " << modtype << std::endl;
87 
88  std::pair<MakerMap::iterator, bool> ret = makers_.insert(std::pair<std::string, Maker*>(modtype, wm.get()));
89 
90  it = ret.first;
91  wm.release();
92  }
93  return it->second;
94  }
95 
96  std::shared_ptr<maker::ModuleHolder> Factory::makeModule(
97  const MakeModuleParams& p,
98  const ModuleTypeResolverBase* resolver,
99  signalslot::Signal<void(const ModuleDescription&)>& pre,
100  signalslot::Signal<void(const ModuleDescription&)>& post) const {
101  auto maker = findMaker(p, resolver);
102  auto mod(maker->makeModule(p, pre, post));
103  return mod;
104  }
105 
106  std::shared_ptr<maker::ModuleHolder> Factory::makeReplacementModule(const edm::ParameterSet& p) const {
107  std::string modtype = p.getParameter<std::string>("@module_type");
108  MakerMap::iterator it = makers_.find(modtype);
109  if (it != makers_.end()) {
110  return it->second->makeReplacementModule(p);
111  }
112  return std::shared_ptr<maker::ModuleHolder>{};
113  }
114 } // namespace edm
static constexpr int kInitialIndex
ret
prodAgent to be discontinued
S make(const edm::ParameterSet &cfg)
static void cleanup(const Factory::MakerMap::value_type &v)
Definition: Factory.cc:14
std::shared_ptr< maker::ModuleHolder > makeReplacementModule(const edm::ParameterSet &) const
Definition: Factory.cc:106
#define FDEBUG(lev)
Definition: DebugMacros.h:19
Maker * findMaker(const MakeModuleParams &p, const ModuleTypeResolverBase *) const
Definition: Factory.cc:57
static void annotateExceptionAndRethrow(cms::Exception &except, const MakeModuleParams &p, std::string const &modtype, ModuleTypeResolverBase const *resolver)
Definition: Factory.cc:27
Func for_all(ForwardSequence &s, Func f)
wrapper for std::for_each
Definition: Algorithms.h:14
static Factory const singleInstance_
Definition: Factory.h:37
void addAdditionalInfo(std::string const &info)
Definition: Exception.cc:169
#define EDM_REGISTER_PLUGINFACTORY(_factory_, _category_)
Definition: PluginFactory.h:88
HLT enums.
std::shared_ptr< maker::ModuleHolder > makeModule(const MakeModuleParams &, const ModuleTypeResolverBase *, signalslot::Signal< void(const ModuleDescription &)> &pre, signalslot::Signal< void(const ModuleDescription &)> &post) const
Definition: Factory.cc:96
static Factory const * get()
Definition: Factory.cc:25
virtual std::pair< std::string, int > resolveType(std::string basename, int index) const =0
#define get
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4
def move(src, dest)
Definition: eostools.py:511
MakerMap makers_
Definition: Factory.h:39