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 <vector>
24 
25 // user include files
29 // forward declarations
30 
31 namespace edmplugin {
32  template <class T>
34  class DummyFriend;
35 
36  template <typename R, typename... Args>
37  class PluginFactory<R*(Args...)> : public PluginFactoryBase {
38  friend class DummyFriend;
39 
40  public:
41  using TemplateArgType = R*(Args...);
42  using CreatedType = R;
43 
44  struct PMakerBase {
45  virtual R* create(Args...) const = 0;
46  virtual ~PMakerBase() {}
47  };
48  template <class TPlug>
49  struct PMaker : public PMakerBase {
50  PMaker(const std::string& iName) { PluginFactory<R*(Args...)>::get()->registerPMaker(this, iName); }
51  R* create(Args... args) const override { return new TPlug(std::forward<Args>(args)...); }
52  };
53 
54  // ---------- const member functions ---------------------
55  const std::string& category() const override;
56 
57  R* create(const std::string& iName, Args... args) const {
58  return reinterpret_cast<PMakerBase*>(PluginFactoryBase::findPMaker(iName))->create(std::forward<Args>(args)...);
59  }
60 
62  R* tryToCreate(const std::string& iName, Args... args) const {
64  if (found == nullptr) {
65  return nullptr;
66  }
67  return reinterpret_cast<PMakerBase*>(found)->create(args...);
68  }
69  // ---------- static member functions --------------------
70 
71  static PluginFactory<R*(Args...)>* get();
72  // ---------- member functions ---------------------------
73  void registerPMaker(PMakerBase* iPMaker, const std::string& iName) {
74  PluginFactoryBase::registerPMaker(iPMaker, iName);
75  }
76 
77  private:
78  PluginFactory() { finishedConstruction(); }
79  PluginFactory(const PluginFactory&) = delete; // stop default
80 
81  const PluginFactory& operator=(const PluginFactory&) = delete; // stop default
82  };
83 } // namespace edmplugin
84 #define FWCORE_CONCATENATE_HIDDEN(a, b) a##b
85 #define FWCORE_CONCATENATE(a, b) FWCORE_CONCATENATE_HIDDEN(a, b)
86 #define EDM_REGISTER_PLUGINFACTORY(_factory_, _category_) \
87  namespace edmplugin { \
88  template <> \
89  edmplugin::PluginFactory<_factory_::TemplateArgType>* edmplugin::PluginFactory<_factory_::TemplateArgType>::get() { \
90  CMS_THREAD_SAFE static edmplugin::PluginFactory<_factory_::TemplateArgType> s_instance; \
91  return &s_instance; \
92  } \
93  template <> \
94  const std::string& edmplugin::PluginFactory<_factory_::TemplateArgType>::category() const { \
95  static const std::string s_cat(_category_); \
96  return s_cat; \
97  } \
98  } \
99  enum { FWCORE_CONCATENATE(dummy_edm_register_pluginfactory_, __LINE__) }
100 
101 #define EDM_REGISTER_PLUGINFACTORY2(_factory_, _category_) \
102  namespace edmplugin { \
103  template <> \
104  edmplugin::PluginFactory<_factory_::TemplateArgType>* edmplugin::PluginFactory<_factory_::TemplateArgType>::get() { \
105  CMS_THREAD_SAFE static edmplugin::PluginFactory<_factory_::TemplateArgType> s_instance; \
106  return &s_instance; \
107  } \
108  template <> \
109  const std::string& edmplugin::PluginFactory<_factory_::TemplateArgType>::category() const { \
110  static const std::string s_cat(_category_); \
111  return s_cat; \
112  } \
113  } \
114  enum { FWCORE_CONCATENATE(dummy_edm_register_pluginfactory_2_, __LINE__) }
115 
116 #endif
117 
118 #define EDM_PLUGIN_SYM(x, y) EDM_PLUGIN_SYM2(x, y)
119 #define EDM_PLUGIN_SYM2(x, y) x##y
120 
121 #define DEFINE_EDM_PLUGIN(factory, type, name) \
122  static const factory::PMaker<type> EDM_PLUGIN_SYM(s_maker, __LINE__)(name)
123 
124 #define DEFINE_EDM_PLUGIN2(factory, type, name) \
125  static const factory::PMaker<type> EDM_PLUGIN_SYM(s_maker2, __LINE__)(name)
def create(alignables, pedeDump, additionalData, outputFile, config)
void registerPMaker(void *iPMaker, const std::string &iName)
R * create(const std::string &iName, Args...args) const
Definition: PluginFactory.h:57
R * create(Args...args) const override
Definition: PluginFactory.h:51
void registerPMaker(PMakerBase *iPMaker, const std::string &iName)
Definition: PluginFactory.h:73
void * findPMaker(const std::string &iName) const
R * tryToCreate(const std::string &iName, Args...args) const
like above but returns 0 if iName is unknown
Definition: PluginFactory.h:62
T get(const Candidate &c)
Definition: component.h:55
void * tryToFindPMaker(const std::string &iName) const