Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-01-11 16:27:11

0001 #ifndef Framework_ComponentFactory_h
0002 #define Framework_ComponentFactory_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     Framework
0006 // Class  :     ComponentFactory
0007 //
0008 /**\class ComponentFactory ComponentFactory.h FWCore/Framework/interface/ComponentFactory.h
0009 
0010  Description: Factory for building the Factories for the various 'plug-in' components needed for the EventSetup
0011 
0012  Usage:
0013     NOTE: it is not safe to call this class across threads, even if only calling const member functions.
0014 
0015 */
0016 //
0017 // Author:      Chris Jones
0018 // Created:     Wed May 25 15:21:05 EDT 2005
0019 //
0020 
0021 // system include files
0022 #include <string>
0023 #include <map>
0024 #include <memory>
0025 #include <exception>
0026 
0027 // user include files
0028 #include "FWCore/PluginManager/interface/PluginFactory.h"
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/Framework/interface/ComponentMaker.h"
0031 #include "FWCore/Framework/interface/resolveMaker.h"
0032 #include "FWCore/Utilities/interface/ConvertException.h"
0033 #include "FWCore/Utilities/interface/EDMException.h"
0034 #include "FWCore/Utilities/interface/Exception.h"
0035 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0036 
0037 // forward declarations
0038 namespace edm {
0039   class ModuleTypeResolverMaker;
0040 
0041   namespace eventsetup {
0042     class EventSetupProvider;
0043     class EventSetupsController;
0044 
0045     template <typename T>
0046     class ComponentFactory {
0047     public:
0048       ComponentFactory() : makers_() {}
0049       //~ComponentFactory();
0050 
0051       typedef ComponentMakerBase<T> Maker;
0052       typedef std::map<std::string, std::shared_ptr<Maker const>> MakerMap;
0053       typedef typename T::base_type base_type;
0054       // ---------- const member functions ---------------------
0055       std::shared_ptr<base_type> addTo(EventSetupsController& esController,
0056                                        EventSetupProvider& iProvider,
0057                                        edm::ParameterSet& iConfiguration,
0058                                        ModuleTypeResolverMaker const* resolverMaker,
0059                                        bool replaceExisting = false) const {
0060         std::string modtype = iConfiguration.template getParameter<std::string>("@module_type");
0061         //cerr << "Factory: module_type = " << modtype << endl;
0062         typename MakerMap::iterator it = makers_.find(modtype);
0063         Maker const* maker = nullptr;
0064 
0065         if (it == makers_.end()) {
0066           maker = detail::resolveMaker<edmplugin::PluginFactory<ComponentMakerBase<T>*()>>(
0067               modtype, resolverMaker, iConfiguration, makers_);
0068 
0069           if (not maker) {
0070             Exception::throwThis(errors::Configuration, "Maker Factory map insert failed");
0071           }
0072         } else {
0073           maker = it->second.get();
0074         }
0075 
0076         try {
0077           return convertException::wrap([&]() -> std::shared_ptr<base_type> {
0078             return maker->addTo(esController, iProvider, iConfiguration, replaceExisting);
0079           });
0080         } catch (cms::Exception& iException) {
0081           std::string edmtype = iConfiguration.template getParameter<std::string>("@module_edm_type");
0082           std::string label = iConfiguration.template getParameter<std::string>("@module_label");
0083           std::ostringstream ost;
0084           ost << "Constructing " << edmtype << ": class=" << modtype << " label='" << label << "'";
0085           iException.addContext(ost.str());
0086           throw;
0087         }
0088         return std::shared_ptr<base_type>();
0089       }
0090 
0091       // ---------- static member functions --------------------
0092       static ComponentFactory<T> const* get();
0093 
0094       // ---------- member functions ---------------------------
0095 
0096     private:
0097       ComponentFactory(const ComponentFactory&);  // stop default
0098 
0099       const ComponentFactory& operator=(const ComponentFactory&);  // stop default
0100 
0101       // ---------- member data --------------------------------
0102       //Creating a new component is not done across threads
0103       CMS_SA_ALLOW mutable MakerMap makers_;
0104     };
0105 
0106   }  // namespace eventsetup
0107 }  // namespace edm
0108 #define COMPONENTFACTORY_GET(_type_)                                                                      \
0109   EDM_REGISTER_PLUGINFACTORY(edmplugin::PluginFactory<edm::eventsetup::ComponentMakerBase<_type_>*()>,    \
0110                              _type_::name());                                                             \
0111   static edm::eventsetup::ComponentFactory<_type_> const s_dummyfactory;                                  \
0112   namespace edm {                                                                                         \
0113     namespace eventsetup {                                                                                \
0114       template <>                                                                                         \
0115       edm::eventsetup::ComponentFactory<_type_> const* edm::eventsetup::ComponentFactory<_type_>::get() { \
0116         return &s_dummyfactory;                                                                           \
0117       }                                                                                                   \
0118     }                                                                                                     \
0119   }                                                                                                       \
0120   typedef int componentfactory_get_needs_semicolon
0121 
0122 #endif