Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:46:53

0001 #ifndef FWCore_Framework_interface_resolveMaker_h
0002 #define FWCore_Framework_interface_resolveMaker_h
0003 
0004 #include "FWCore/Framework/interface/ModuleTypeResolverBase.h"
0005 #include "FWCore/Framework/interface/ModuleTypeResolverMaker.h"
0006 #include "FWCore/Utilities/interface/DebugMacros.h"
0007 #include "FWCore/Utilities/interface/Exception.h"
0008 
0009 #include <memory>
0010 #include <string>
0011 
0012 namespace edm::detail {
0013   void annotateResolverMakerExceptionAndRethrow(cms::Exception& except,
0014                                                 std::string const& modtype,
0015                                                 ModuleTypeResolverBase const* resolver);
0016 
0017   // Returns a non-owning pointer to the maker. Can be nullptr if
0018   // failed to insert the maker to the cache
0019   template <typename TFactory, typename TCache>
0020   auto resolveMaker(std::string const& moduleType,
0021                     ModuleTypeResolverMaker const* resolverMaker,
0022                     edm::ParameterSet const& modulePSet,
0023                     TCache& makerCache) -> typename TCache::mapped_type::element_type const* {
0024     if (resolverMaker) {
0025       auto resolver = resolverMaker->makeResolver(modulePSet);
0026       auto index = resolver->kInitialIndex;
0027       auto newType = moduleType;
0028       do {
0029         auto [ttype, tindex] = resolver->resolveType(std::move(newType), index);
0030         newType = std::move(ttype);
0031         index = tindex;
0032         // try the maker cache first
0033         auto found = makerCache.find(newType);
0034         if (found != makerCache.end()) {
0035           return found->second.get();
0036         }
0037 
0038         // if not in cache, then try to create
0039         auto m = TFactory::get()->tryToCreate(newType);
0040         if (m) {
0041           //FDEBUG(1) << "Factory:  created worker of type " << newType << std::endl;
0042           auto [it, succeeded] = makerCache.emplace(newType, std::move(m));
0043           if (not succeeded) {
0044             return nullptr;
0045           }
0046           return it->second.get();
0047         }
0048         // not found, try next one
0049       } while (index != resolver->kLastIndex);
0050       try {
0051         //failed to find a plugin
0052         auto m = TFactory::get()->create(moduleType);
0053         return nullptr;  // dummy return, the create() call throws an exception
0054       } catch (cms::Exception& iExcept) {
0055         detail::annotateResolverMakerExceptionAndRethrow(iExcept, moduleType, resolver.get());
0056       }
0057     }
0058     auto [it, succeeded] = makerCache.emplace(moduleType, TFactory::get()->create(moduleType));
0059     //FDEBUG(1) << "Factory:  created worker of type " << moduleType << std::endl;
0060     if (not succeeded) {
0061       return nullptr;
0062     }
0063     return it->second.get();
0064   }
0065 }  // namespace edm::detail
0066 
0067 #endif