CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
28 // forward declarations
29 
30 namespace edmplugin {
31 template< class T> class PluginFactory;
32  class DummyFriend;
33 
34 template<typename R, typename... Args>
35 class PluginFactory<R*(Args...)> : public PluginFactoryBase
36 {
37  friend class DummyFriend;
38  public:
39  typedef R* TemplateArgType(Args...);
40 
41  struct PMakerBase {
42  virtual R* create(Args...) const = 0;
43  virtual ~PMakerBase() {}
44  };
45  template<class TPlug>
46  struct PMaker : public PMakerBase {
47  PMaker(const std::string& iName) {
48  PluginFactory<R*(Args...)>::get()->registerPMaker(this,iName);
49  }
50  virtual R* create(Args... args) const {
51  return new TPlug(std::forward<Args>(args)...);
52  }
53  };
54 
55  // ---------- const member functions ---------------------
56  virtual const std::string& category() const ;
57 
58  R* create(const std::string& iName, Args... args) const {
59  return reinterpret_cast<PMakerBase*>(PluginFactoryBase::findPMaker(iName))->create(std::forward<Args>(args)...);
60  }
61 
63  R* tryToCreate(const std::string& iName, Args... args) const {
65  if(found ==nullptr) {
66  return nullptr;
67  }
68  return reinterpret_cast<PMakerBase*>(found)->create(args...);
69  }
70  // ---------- static member functions --------------------
71 
72  static PluginFactory<R*(Args...)>* get();
73  // ---------- member functions ---------------------------
74  void registerPMaker(PMakerBase* iPMaker, const std::string& iName) {
75  PluginFactoryBase::registerPMaker(iPMaker, iName);
76  }
77 
78  private:
80  finishedConstruction();
81  }
82  PluginFactory(const PluginFactory&) = delete; // stop default
83 
84  const PluginFactory& operator=(const PluginFactory&) = delete; // stop default
85 
86 };
87 }
88 #define CONCATENATE_HIDDEN(a,b) a ## b
89 #define CONCATENATE(a,b) CONCATENATE_HIDDEN(a,b)
90 #define EDM_REGISTER_PLUGINFACTORY(_factory_,_category_) \
91 namespace edmplugin {\
92  template<> edmplugin::PluginFactory<_factory_::TemplateArgType>* edmplugin::PluginFactory<_factory_::TemplateArgType>::get() { [[cms::thread_safe]] static edmplugin::PluginFactory<_factory_::TemplateArgType> s_instance; return &s_instance;}\
93  template<> const std::string& edmplugin::PluginFactory<_factory_::TemplateArgType>::category() const { static const std::string s_cat(_category_); return s_cat;}\
94  } enum {CONCATENATE(dummy_edm_register_pluginfactory_, __LINE__)}
95 
96 #endif
97 
98 #define EDM_PLUGIN_SYM(x,y) EDM_PLUGIN_SYM2(x,y)
99 #define EDM_PLUGIN_SYM2(x,y) x ## y
100 
101 #define DEFINE_EDM_PLUGIN(factory,type,name) \
102 static const factory::PMaker<type> EDM_PLUGIN_SYM(s_maker , __LINE__ ) (name)
103 
void registerPMaker(void *iPMaker, const std::string &iName)
R * create(const std::string &iName, Args...args) const
Definition: PluginFactory.h:58
virtual R * create(Args...args) const
Definition: PluginFactory.h:50
void registerPMaker(PMakerBase *iPMaker, const std::string &iName)
Definition: PluginFactory.h:74
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:63
T get(const Candidate &c)
Definition: component.h:55
void * tryToFindPMaker(const std::string &iName) const