-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
ComponentFactory.h
134 lines (112 loc) · 5.77 KB
/
ComponentFactory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef Framework_ComponentFactory_h
#define Framework_ComponentFactory_h
// -*- C++ -*-
//
// Package: Framework
// Class : ComponentFactory
//
/**\class ComponentFactory ComponentFactory.h FWCore/Framework/interface/ComponentFactory.h
Description: Factory for building the Factories for the various 'plug-in' components needed for the EventSetup
Usage:
NOTE: it is not safe to call this class across threads, even if only calling const member functions.
*/
//
// Author: Chris Jones
// Created: Wed May 25 15:21:05 EDT 2005
//
// system include files
#include <string>
#include <map>
#include <memory>
#include <exception>
// user include files
#include "FWCore/PluginManager/interface/PluginFactory.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/ComponentMaker.h"
#include "FWCore/Utilities/interface/ConvertException.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/thread_safety_macros.h"
// forward declarations
namespace edm {
namespace eventsetup {
class EventSetupProvider;
class EventSetupsController;
template <typename T>
class ComponentFactory {
public:
ComponentFactory() : makers_() {}
//~ComponentFactory();
typedef ComponentMakerBase<T> Maker;
typedef std::map<std::string, std::shared_ptr<Maker> > MakerMap;
typedef typename T::base_type base_type;
// ---------- const member functions ---------------------
std::shared_ptr<base_type> addTo(EventSetupsController& esController,
EventSetupProvider& iProvider,
edm::ParameterSet const& iConfiguration,
bool replaceExisting = false) const {
std::string modtype = iConfiguration.template getParameter<std::string>("@module_type");
//cerr << "Factory: module_type = " << modtype << endl;
typename MakerMap::iterator it = makers_.find(modtype);
if (it == makers_.end()) {
std::shared_ptr<Maker> wm(edmplugin::PluginFactory<ComponentMakerBase<T>*()>::get()->create(modtype));
if (wm.get() == nullptr) {
Exception::throwThis(errors::Configuration,
"UnknownModule",
T::name().c_str(),
" of type ",
modtype.c_str(),
" has not been registered.\n"
"Perhaps your module type is misspelled or is not a "
"framework plugin.\n"
"Try running EdmPluginDump to obtain a list of "
"available Plugins.");
}
//cerr << "Factory: created the worker" << endl;
std::pair<typename MakerMap::iterator, bool> ret =
makers_.insert(std::pair<std::string, std::shared_ptr<Maker> >(modtype, wm));
if (ret.second == false) {
Exception::throwThis(errors::Configuration, "Maker Factory map insert failed");
}
it = ret.first;
}
try {
return convertException::wrap([&]() -> std::shared_ptr<base_type> {
return it->second->addTo(esController, iProvider, iConfiguration, replaceExisting);
});
} catch (cms::Exception& iException) {
std::string edmtype = iConfiguration.template getParameter<std::string>("@module_edm_type");
std::string label = iConfiguration.template getParameter<std::string>("@module_label");
std::ostringstream ost;
ost << "Constructing " << edmtype << ": class=" << modtype << " label='" << label << "'";
iException.addContext(ost.str());
throw;
}
return std::shared_ptr<base_type>();
}
// ---------- static member functions --------------------
static ComponentFactory<T> const* get();
// ---------- member functions ---------------------------
private:
ComponentFactory(const ComponentFactory&); // stop default
const ComponentFactory& operator=(const ComponentFactory&); // stop default
// ---------- member data --------------------------------
//Creating a new component is not done across threads
CMS_SA_ALLOW mutable MakerMap makers_;
};
} // namespace eventsetup
} // namespace edm
#define COMPONENTFACTORY_GET(_type_) \
EDM_REGISTER_PLUGINFACTORY(edmplugin::PluginFactory<edm::eventsetup::ComponentMakerBase<_type_>*()>, \
_type_::name()); \
static edm::eventsetup::ComponentFactory<_type_> const s_dummyfactory; \
namespace edm { \
namespace eventsetup { \
template <> \
edm::eventsetup::ComponentFactory<_type_> const* edm::eventsetup::ComponentFactory<_type_>::get() { \
return &s_dummyfactory; \
} \
} \
} \
typedef int componentfactory_get_needs_semicolon
#endif