Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:     FWCore/Framework
0004 // Class  :     edm::ModuleRegistry
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Fri, 23 Aug 2013 16:39:58 GMT
0011 //
0012 
0013 // system include files
0014 
0015 // user include files
0016 #include "FWCore/Framework/interface/ModuleRegistry.h"
0017 #include "FWCore/Framework/src/ModuleHolderFactory.h"
0018 
0019 namespace edm {
0020   std::shared_ptr<maker::ModuleHolder> ModuleRegistry::getModule(
0021       MakeModuleParams const& p,
0022       std::string const& moduleLabel,
0023       signalslot::Signal<void(ModuleDescription const&)>& iPre,
0024       signalslot::Signal<void(ModuleDescription const&)>& iPost) {
0025     auto modItr = labelToModule_.find(moduleLabel);
0026     if (modItr == labelToModule_.end()) {
0027       auto modPtr = ModuleHolderFactory::get()->makeModule(p, typeResolverMaker_, iPre, iPost);
0028 
0029       if (maxModuleID_ < modPtr->moduleDescription().id()) {
0030         maxModuleID_ = modPtr->moduleDescription().id();
0031       }
0032       // Transfer ownership of worker to the registry
0033       labelToModule_[moduleLabel] = modPtr;
0034       return modPtr;
0035     }
0036     return get_underlying_safe(modItr->second);
0037   }
0038 
0039   std::shared_ptr<maker::ModuleHolder> ModuleRegistry::getExistingModule(std::string const& moduleLabel) {
0040     auto modItr = labelToModule_.find(moduleLabel);
0041     if (modItr == labelToModule_.end()) {
0042       return {};
0043     }
0044     return get_underlying_safe(modItr->second);
0045   }
0046 
0047   maker::ModuleHolder* ModuleRegistry::replaceModule(std::string const& iModuleLabel,
0048                                                      edm::ParameterSet const& iPSet,
0049                                                      edm::PreallocationConfiguration const& iPrealloc) {
0050     auto modItr = labelToModule_.find(iModuleLabel);
0051     if (modItr == labelToModule_.end()) {
0052       return nullptr;
0053     }
0054 
0055     auto modPtr = ModuleHolderFactory::get()->makeReplacementModule(iPSet);
0056     modPtr->finishModuleInitialization(modItr->second->moduleDescription(), iPrealloc, nullptr);
0057 
0058     if (maxModuleID_ < modPtr->moduleDescription().id()) {
0059       maxModuleID_ = modPtr->moduleDescription().id();
0060     }
0061 
0062     // Transfer ownership of worker to the registry
0063     modItr->second = modPtr;
0064     return modItr->second.get();
0065   }
0066 
0067   void ModuleRegistry::deleteModule(std::string const& iModuleLabel,
0068                                     signalslot::Signal<void(ModuleDescription const&)>& iPre,
0069                                     signalslot::Signal<void(ModuleDescription const&)>& iPost) {
0070     auto modItr = labelToModule_.find(iModuleLabel);
0071     if (modItr == labelToModule_.end()) {
0072       throw cms::Exception("LogicError")
0073           << "Trying to delete module " << iModuleLabel
0074           << " but it does not exist in the ModuleRegistry. Please contact framework developers.";
0075     }
0076     // If iPost throws and exception, let it propagate
0077     // If deletion throws an exception, capture it and call iPost before throwing an exception
0078     // If iPost throws an exception, let it propagate
0079     auto md = modItr->second->moduleDescription();
0080     iPre(modItr->second->moduleDescription());
0081     bool postCalled = false;
0082     // exception is rethrown
0083     CMS_SA_ALLOW try {
0084       labelToModule_.erase(modItr);
0085       // if exception then post will be called in the catch block
0086       postCalled = true;
0087       iPost(md);
0088     } catch (...) {
0089       if (not postCalled) {
0090         // we're already handling exception, nothing we can do if iPost throws
0091         CMS_SA_ALLOW try { iPost(md); } catch (...) {
0092         }
0093       }
0094       throw;
0095     }
0096   }
0097 }  // namespace edm