Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:59

0001 #ifndef FWCore_PluginManager_PluginFactory_h
0002 #define FWCore_PluginManager_PluginFactory_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     PluginManager
0006 // Class  :     PluginFactory
0007 //
0008 /**\class PluginFactory PluginFactory.h FWCore/PluginManager/interface/PluginFactory.h
0009 
0010  Description: Public interface for loading a plugin
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Thu Apr  5 12:10:23 EDT 2007
0019 //
0020 
0021 // system include files
0022 #include <map>
0023 #include <memory>
0024 #include <vector>
0025 
0026 // user include files
0027 #include "FWCore/PluginManager/interface/PluginFactoryBase.h"
0028 #include "FWCore/PluginManager/interface/PluginManager.h"
0029 #include "FWCore/Utilities/interface/concatenate.h"
0030 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0031 // forward declarations
0032 
0033 namespace edmplugin {
0034   template <class T>
0035   class PluginFactory;
0036   class DummyFriend;
0037 
0038   template <typename R, typename... Args>
0039   class PluginFactory<R*(Args...)> : public PluginFactoryBase {
0040     friend class DummyFriend;
0041 
0042   public:
0043     using TemplateArgType = R*(Args...);
0044     using CreatedType = R;
0045 
0046     PluginFactory(const PluginFactory&) = delete;                   // stop default
0047     const PluginFactory& operator=(const PluginFactory&) = delete;  // stop default
0048 
0049     struct PMakerBase {
0050       virtual std::unique_ptr<R> create(Args...) const = 0;
0051       virtual ~PMakerBase() {}
0052     };
0053     template <class TPlug>
0054     struct PMaker : public PMakerBase {
0055       PMaker(const std::string& iName) { PluginFactory<R*(Args...)>::get()->registerPMaker(this, iName); }
0056       std::unique_ptr<R> create(Args... args) const override {
0057         return std::make_unique<TPlug>(std::forward<Args>(args)...);
0058       }
0059     };
0060 
0061     // ---------- const member functions ---------------------
0062     const std::string& category() const override;
0063 
0064     std::unique_ptr<R> create(const std::string& iName, Args... args) const {
0065       return reinterpret_cast<PMakerBase*>(PluginFactoryBase::findPMaker(iName))->create(std::forward<Args>(args)...);
0066     }
0067 
0068     ///like above but returns 0 if iName is unknown
0069     std::unique_ptr<R> tryToCreate(const std::string& iName, Args... args) const {
0070       auto found = PluginFactoryBase::tryToFindPMaker(iName);
0071       if (found == nullptr) {
0072         return nullptr;
0073       }
0074       return reinterpret_cast<PMakerBase*>(found)->create(args...);
0075     }
0076     // ---------- static member functions --------------------
0077 
0078     static PluginFactory<R*(Args...)>* get();
0079     // ---------- member functions ---------------------------
0080     void registerPMaker(PMakerBase* iPMaker, const std::string& iName) {
0081       PluginFactoryBase::registerPMaker(iPMaker, iName);
0082     }
0083 
0084   private:
0085     PluginFactory() { finishedConstruction(); }
0086   };
0087 }  // namespace edmplugin
0088 #define EDM_REGISTER_PLUGINFACTORY(_factory_, _category_)                                                               \
0089   namespace edmplugin {                                                                                                 \
0090     template <>                                                                                                         \
0091     edmplugin::PluginFactory<_factory_::TemplateArgType>* edmplugin::PluginFactory<_factory_::TemplateArgType>::get() { \
0092       CMS_THREAD_SAFE static edmplugin::PluginFactory<_factory_::TemplateArgType> s_instance;                           \
0093       return &s_instance;                                                                                               \
0094     }                                                                                                                   \
0095     template <>                                                                                                         \
0096     const std::string& edmplugin::PluginFactory<_factory_::TemplateArgType>::category() const {                         \
0097       static const std::string s_cat(_category_);                                                                       \
0098       return s_cat;                                                                                                     \
0099     }                                                                                                                   \
0100   }                                                                                                                     \
0101   enum { EDM_CONCATENATE(dummy_edm_register_pluginfactory_, __LINE__) }
0102 
0103 #define EDM_REGISTER_PLUGINFACTORY2(_factory_, _category_)                                                              \
0104   namespace edmplugin {                                                                                                 \
0105     template <>                                                                                                         \
0106     edmplugin::PluginFactory<_factory_::TemplateArgType>* edmplugin::PluginFactory<_factory_::TemplateArgType>::get() { \
0107       CMS_THREAD_SAFE static edmplugin::PluginFactory<_factory_::TemplateArgType> s_instance;                           \
0108       return &s_instance;                                                                                               \
0109     }                                                                                                                   \
0110     template <>                                                                                                         \
0111     const std::string& edmplugin::PluginFactory<_factory_::TemplateArgType>::category() const {                         \
0112       static const std::string s_cat(_category_);                                                                       \
0113       return s_cat;                                                                                                     \
0114     }                                                                                                                   \
0115   }                                                                                                                     \
0116   enum { EDM_CONCATENATE(dummy_edm_register_pluginfactory_2_, __LINE__) }
0117 
0118 #endif
0119 
0120 #define EDM_PLUGIN_SYM(x, y) EDM_PLUGIN_SYM2(x, y)
0121 #define EDM_PLUGIN_SYM2(x, y) x##y
0122 
0123 #define DEFINE_EDM_PLUGIN(factory, type, name) \
0124   static const factory::PMaker<type> EDM_PLUGIN_SYM(s_maker, __LINE__)(name)
0125 
0126 #define DEFINE_EDM_PLUGIN2(factory, type, name) \
0127   static const factory::PMaker<type> EDM_PLUGIN_SYM(s_maker2_, __LINE__)(name)