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