CMS 3D CMS Logo

PluginFactory.h
Go to the documentation of this file.
1 #ifndef FWCore_PluginManager_PluginFactory_h
2 #define FWCore_PluginManager_PluginFactory_h
3 // -*- C++ -*-
4 //
5 // Package: PluginManager
6 // Class : PluginFactory
7 //
16 //
17 // Original Author: Chris Jones
18 // Created: Thu Apr 5 12:10:23 EDT 2007
19 //
20 
21 // system include files
22 #include <map>
23 #include <memory>
24 #include <vector>
25 
26 // user include files
30 // forward declarations
31 
32 namespace edmplugin {
33  template <class T>
35  class DummyFriend;
36 
37  template <typename R, typename... Args>
38  class PluginFactory<R*(Args...)> : public PluginFactoryBase {
39  friend class DummyFriend;
40 
41  public:
42  using TemplateArgType = R*(Args...);
43  using CreatedType = R;
44 
45  PluginFactory(const PluginFactory&) = delete; // stop default
46  const PluginFactory& operator=(const PluginFactory&) = delete; // stop default
47 
48  struct PMakerBase {
49  virtual std::unique_ptr<R> create(Args...) const = 0;
50  virtual ~PMakerBase() {}
51  };
52  template <class TPlug>
53  struct PMaker : public PMakerBase {
54  PMaker(const std::string& iName) { PluginFactory<R*(Args...)>::get()->registerPMaker(this, iName); }
55  std::unique_ptr<R> create(Args... args) const override {
56  return std::make_unique<TPlug>(std::forward<Args>(args)...);
57  }
58  };
59 
60  // ---------- const member functions ---------------------
61  const std::string& category() const override;
62 
63  std::unique_ptr<R> create(const std::string& iName, Args... args) const {
64  return reinterpret_cast<PMakerBase*>(PluginFactoryBase::findPMaker(iName))->create(std::forward<Args>(args)...);
65  }
66 
68  std::unique_ptr<R> tryToCreate(const std::string& iName, Args... args) const {
70  if (found == nullptr) {
71  return nullptr;
72  }
73  return reinterpret_cast<PMakerBase*>(found)->create(args...);
74  }
75  // ---------- static member functions --------------------
76 
77  static PluginFactory<R*(Args...)>* get();
78  // ---------- member functions ---------------------------
79  void registerPMaker(PMakerBase* iPMaker, const std::string& iName) {
80  PluginFactoryBase::registerPMaker(iPMaker, iName);
81  }
82 
83  private:
84  PluginFactory() { finishedConstruction(); }
85  };
86 } // namespace edmplugin
87 #define FWCORE_CONCATENATE_HIDDEN(a, b) a##b
88 #define FWCORE_CONCATENATE(a, b) FWCORE_CONCATENATE_HIDDEN(a, b)
89 #define EDM_REGISTER_PLUGINFACTORY(_factory_, _category_) \
90  namespace edmplugin { \
91  template <> \
92  edmplugin::PluginFactory<_factory_::TemplateArgType>* edmplugin::PluginFactory<_factory_::TemplateArgType>::get() { \
93  CMS_THREAD_SAFE static edmplugin::PluginFactory<_factory_::TemplateArgType> s_instance; \
94  return &s_instance; \
95  } \
96  template <> \
97  const std::string& edmplugin::PluginFactory<_factory_::TemplateArgType>::category() const { \
98  static const std::string s_cat(_category_); \
99  return s_cat; \
100  } \
101  } \
102  enum { FWCORE_CONCATENATE(dummy_edm_register_pluginfactory_, __LINE__) }
103 
104 #define EDM_REGISTER_PLUGINFACTORY2(_factory_, _category_) \
105  namespace edmplugin { \
106  template <> \
107  edmplugin::PluginFactory<_factory_::TemplateArgType>* edmplugin::PluginFactory<_factory_::TemplateArgType>::get() { \
108  CMS_THREAD_SAFE static edmplugin::PluginFactory<_factory_::TemplateArgType> s_instance; \
109  return &s_instance; \
110  } \
111  template <> \
112  const std::string& edmplugin::PluginFactory<_factory_::TemplateArgType>::category() const { \
113  static const std::string s_cat(_category_); \
114  return s_cat; \
115  } \
116  } \
117  enum { FWCORE_CONCATENATE(dummy_edm_register_pluginfactory_2_, __LINE__) }
118 
119 #endif
120 
121 #define EDM_PLUGIN_SYM(x, y) EDM_PLUGIN_SYM2(x, y)
122 #define EDM_PLUGIN_SYM2(x, y) x##y
123 
124 #define DEFINE_EDM_PLUGIN(factory, type, name) \
125  static const factory::PMaker<type> EDM_PLUGIN_SYM(s_maker, __LINE__)(name)
126 
127 #define DEFINE_EDM_PLUGIN2(factory, type, name) \
128  static const factory::PMaker<type> EDM_PLUGIN_SYM(s_maker2, __LINE__)(name)
writedatasetfile.args
args
Definition: writedatasetfile.py:18
edmplugin
Definition: AlignmentAlgorithmPluginFactory.cc:9
taus_updatedMVAIds_cff.category
category
Definition: taus_updatedMVAIds_cff.py:31
edmplugin::PluginFactory< R *(Args...)>::create
std::unique_ptr< R > create(const std::string &iName, Args... args) const
Definition: PluginFactory.h:63
edmplugin::PluginFactory< R *(Args...)>::CreatedType
R CreatedType
Definition: PluginFactory.h:43
beamerCreator.create
def create(alignables, pedeDump, additionalData, outputFile, config)
Definition: beamerCreator.py:44
newFWLiteAna.found
found
Definition: newFWLiteAna.py:118
edmplugin::PluginFactory< R *(Args...)>::PMaker::create
std::unique_ptr< R > create(Args... args) const override
Definition: PluginFactory.h:55
edmplugin::PluginFactoryBase::findPMaker
void * findPMaker(const std::string &iName) const
Definition: PluginFactoryBase.cc:66
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edmplugin::PluginFactory< R *(Args...)>::PMaker::PMaker
PMaker(const std::string &iName)
Definition: PluginFactory.h:54
edmplugin::PluginFactory
Definition: PluginFactory.h:34
thread_safety_macros.h
edmplugin::PluginFactory< R *(Args...)>::TemplateArgType
R *(Args...) TemplateArgType
Definition: PluginFactory.h:42
edmplugin::PluginFactory< R *(Args...)>::PMakerBase::~PMakerBase
virtual ~PMakerBase()
Definition: PluginFactory.h:50
edmplugin::PluginFactoryBase
Definition: PluginFactoryBase.h:37
PluginFactoryBase.h
get
#define get
PluginManager.h
edmplugin::PluginFactory< R *(Args...)>::registerPMaker
void registerPMaker(PMakerBase *iPMaker, const std::string &iName)
Definition: PluginFactory.h:79
edmplugin::PluginFactoryBase::registerPMaker
void registerPMaker(void *iPMaker, const std::string &iName)
Definition: PluginFactoryBase.cc:146
edmplugin::PluginFactory< R *(Args...)>::PluginFactory
PluginFactory()
Definition: PluginFactory.h:84
edmplugin::PluginFactory< R *(Args...)>::tryToCreate
std::unique_ptr< R > tryToCreate(const std::string &iName, Args... args) const
like above but returns 0 if iName is unknown
Definition: PluginFactory.h:68
dttmaxenums::R
Definition: DTTMax.h:29
edmplugin::PluginFactoryBase::tryToFindPMaker
void * tryToFindPMaker(const std::string &iName) const
Definition: PluginFactoryBase.cc:88