Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-06-29 22:58:02

0001 #ifndef FWCore_Framework_ModuleRegistry_h
0002 #define FWCore_Framework_ModuleRegistry_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWCore/Framework
0006 // Class  :     ModuleRegistry
0007 //
0008 /**\class edm::ModuleRegistry ModuleRegistry.h "FWCore/Framework/interface/ModuleRegistry.h"
0009 
0010  Description: Constructs and owns framework modules
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Fri, 23 Aug 2013 16:21:10 GMT
0019 //
0020 
0021 // system include files
0022 #include <map>
0023 #include <memory>
0024 #include <string>
0025 
0026 // user include files
0027 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
0028 #include "FWCore/Utilities/interface/propagate_const.h"
0029 #include "FWCore/Utilities/interface/Exception.h"
0030 #include "FWCore/Framework/interface/ModuleTypeResolverMaker.h"
0031 #include "FWCore/Framework/interface/maker/ModuleHolder.h"
0032 
0033 // forward declarations
0034 namespace edm {
0035   class ParameterSet;
0036   struct MakeModuleParams;
0037   class ModuleDescription;
0038   class PreallocationConfiguration;
0039 
0040   class ModuleRegistry {
0041   public:
0042     ModuleRegistry() = default;
0043     explicit ModuleRegistry(ModuleTypeResolverMaker const* resolverMaker) : typeResolverMaker_(resolverMaker) {}
0044     std::shared_ptr<maker::ModuleHolder> getModule(MakeModuleParams const& p,
0045                                                    std::string const& moduleLabel,
0046                                                    signalslot::Signal<void(ModuleDescription const&)>& iPre,
0047                                                    signalslot::Signal<void(ModuleDescription const&)>& iPost);
0048     //returns a null if module not found
0049     std::shared_ptr<maker::ModuleHolder> getExistingModule(std::string const& moduleLabel);
0050 
0051     maker::ModuleHolder* replaceModule(std::string const& iModuleLabel,
0052                                        edm::ParameterSet const& iPSet,
0053                                        edm::PreallocationConfiguration const&);
0054 
0055     void deleteModule(std::string const& iModuleLabel,
0056                       signalslot::Signal<void(ModuleDescription const&)>& iPre,
0057                       signalslot::Signal<void(ModuleDescription const&)>& iPost);
0058 
0059     template <typename T, typename... Args>
0060     std::shared_ptr<T> makeExplicitModule(ModuleDescription const& md,
0061                                           PreallocationConfiguration const& iPrealloc,
0062                                           SignallingProductRegistryFiller* iReg,
0063                                           signalslot::Signal<void(ModuleDescription const&)>& iPre,
0064                                           signalslot::Signal<void(ModuleDescription const&)>& iPost,
0065                                           Args&&... args) {
0066       bool postCalled = false;
0067       if (labelToModule_.find(md.moduleLabel()) != labelToModule_.end()) {
0068         throw cms::Exception("InsertError") << "Module with label '" << md.moduleLabel() << "' already exists.";
0069       }
0070 
0071       try {
0072         std::shared_ptr<T> module;
0073         convertException::wrap([&]() {
0074           iPre(md);
0075           module = std::make_shared<T>(std::forward<Args>(args)...);
0076 
0077           auto holder = std::make_shared<maker::ModuleHolderT<typename T::ModuleType>>(module);
0078           holder->finishModuleInitialization(md, iPrealloc, iReg);
0079           labelToModule_.emplace(md.moduleLabel(), std::move(holder));
0080 
0081           if (maxModuleID_ < module->moduleDescription().id()) {
0082             maxModuleID_ = module->moduleDescription().id();
0083           }
0084           // if exception then post will be called in the catch block
0085           postCalled = true;
0086           iPost(md);
0087         });
0088         return module;
0089       } catch (cms::Exception& iException) {
0090         if (!postCalled) {
0091           CMS_SA_ALLOW try { iPost(md); } catch (...) {
0092             // If post throws an exception ignore it because we are already handling another exception
0093           }
0094         }
0095         throw;
0096       }
0097     }
0098 
0099     template <typename F>
0100     void forAllModuleHolders(F iFunc) {
0101       for (auto& labelMod : labelToModule_) {
0102         maker::ModuleHolder* t = labelMod.second.get();
0103         iFunc(t);
0104       }
0105     }
0106 
0107     unsigned int maxModuleID() const { return maxModuleID_; }
0108 
0109   private:
0110     std::map<std::string, edm::propagate_const<std::shared_ptr<maker::ModuleHolder>>> labelToModule_;
0111     ModuleTypeResolverMaker const* typeResolverMaker_;
0112     unsigned int maxModuleID_ = 0;
0113   };
0114 }  // namespace edm
0115 
0116 #endif