Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-09-16 00:05:34

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 ModuleTypeResolverBase;
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>> 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 const& iConfiguration,
0058                                        ModuleTypeResolverBase const* resolver,
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 
0064         if (it == makers_.end()) {
0065           std::shared_ptr<Maker> wm(
0066               detail::resolveMaker<edmplugin::PluginFactory<ComponentMakerBase<T>*()>>(modtype, resolver));
0067 
0068           //cerr << "Factory: created the worker" << endl;
0069 
0070           std::pair<typename MakerMap::iterator, bool> ret =
0071               makers_.insert(std::pair<std::string, std::shared_ptr<Maker>>(modtype, wm));
0072 
0073           if (ret.second == false) {
0074             Exception::throwThis(errors::Configuration, "Maker Factory map insert failed");
0075           }
0076 
0077           it = ret.first;
0078         }
0079 
0080         try {
0081           return convertException::wrap([&]() -> std::shared_ptr<base_type> {
0082             return it->second->addTo(esController, iProvider, iConfiguration, replaceExisting);
0083           });
0084         } catch (cms::Exception& iException) {
0085           std::string edmtype = iConfiguration.template getParameter<std::string>("@module_edm_type");
0086           std::string label = iConfiguration.template getParameter<std::string>("@module_label");
0087           std::ostringstream ost;
0088           ost << "Constructing " << edmtype << ": class=" << modtype << " label='" << label << "'";
0089           iException.addContext(ost.str());
0090           throw;
0091         }
0092         return std::shared_ptr<base_type>();
0093       }
0094 
0095       // ---------- static member functions --------------------
0096       static ComponentFactory<T> const* get();
0097 
0098       // ---------- member functions ---------------------------
0099 
0100     private:
0101       ComponentFactory(const ComponentFactory&);  // stop default
0102 
0103       const ComponentFactory& operator=(const ComponentFactory&);  // stop default
0104 
0105       // ---------- member data --------------------------------
0106       //Creating a new component is not done across threads
0107       CMS_SA_ALLOW mutable MakerMap makers_;
0108     };
0109 
0110   }  // namespace eventsetup
0111 }  // namespace edm
0112 #define COMPONENTFACTORY_GET(_type_)                                                                      \
0113   EDM_REGISTER_PLUGINFACTORY(edmplugin::PluginFactory<edm::eventsetup::ComponentMakerBase<_type_>*()>,    \
0114                              _type_::name());                                                             \
0115   static edm::eventsetup::ComponentFactory<_type_> const s_dummyfactory;                                  \
0116   namespace edm {                                                                                         \
0117     namespace eventsetup {                                                                                \
0118       template <>                                                                                         \
0119       edm::eventsetup::ComponentFactory<_type_> const* edm::eventsetup::ComponentFactory<_type_>::get() { \
0120         return &s_dummyfactory;                                                                           \
0121       }                                                                                                   \
0122     }                                                                                                     \
0123   }                                                                                                       \
0124   typedef int componentfactory_get_needs_semicolon
0125 
0126 #endif