Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef FWCore_Framework_test_TestTypeResolvers_h
0002 #define FWCore_Framework_test_TestTypeResolvers_h
0003 
0004 #include "FWCore/Framework/interface/ModuleTypeResolverBase.h"
0005 #include "FWCore/Framework/interface/ModuleTypeResolverMaker.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0007 #include "FWCore/Utilities/interface/EDMException.h"
0008 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0009 
0010 #include <cstring>
0011 #include <memory>
0012 #include <string>
0013 #include <unordered_map>
0014 #include <utility>
0015 
0016 namespace edm::test {
0017   class SimpleTestTypeResolver : public edm::ModuleTypeResolverBase {
0018   public:
0019     SimpleTestTypeResolver() = default;
0020     std::pair<std::string, int> resolveType(std::string basename, int index) const final {
0021       return {basename, kLastIndex};
0022     }
0023   };
0024   class SimpleTestTypeResolverMaker : public edm::ModuleTypeResolverMaker {
0025   public:
0026     SimpleTestTypeResolverMaker() = default;
0027     std::shared_ptr<ModuleTypeResolverBase const> makeResolver(edm::ParameterSet const&) const final {
0028       return std::make_shared<SimpleTestTypeResolver>();
0029     }
0030   };
0031 
0032   class ComplexTestTypeResolver : public edm::ModuleTypeResolverBase {
0033   public:
0034     ComplexTestTypeResolver() = default;
0035     std::pair<std::string, int> resolveType(std::string basename, int index) const final {
0036       constexpr auto kGeneric = "generic::";
0037       constexpr auto kOther = "edm::test::other::";
0038       constexpr auto kCPU = "edm::test::cpu::";
0039       if (index != kInitialIndex and index != kLastIndex) {
0040         basename.replace(basename.find(kOther), strlen(kOther), kCPU);
0041         return {basename, kLastIndex};
0042       }
0043       if (index == kInitialIndex and basename.find(kGeneric) != std::string::npos) {
0044         basename.replace(basename.find(kGeneric), strlen(kGeneric), kOther);
0045         return {basename, kInitialIndex + 1};
0046       }
0047       return {basename, kLastIndex};
0048     }
0049   };
0050   class ComplexTestTypeResolverMaker : public edm::ModuleTypeResolverMaker {
0051   public:
0052     ComplexTestTypeResolverMaker() = default;
0053     std::shared_ptr<ModuleTypeResolverBase const> makeResolver(edm::ParameterSet const&) const final {
0054       return std::make_shared<ComplexTestTypeResolver>();
0055     }
0056   };
0057 
0058   class ConfigurableTestTypeResolver : public edm::ModuleTypeResolverBase {
0059   public:
0060     ConfigurableTestTypeResolver(std::string variant) : variant_(std::move(variant)) {}
0061     std::pair<std::string, int> resolveType(std::string basename, int index) const final {
0062       constexpr auto kGeneric = "generic::";
0063       constexpr auto kOther = "edm::test::other::";
0064       constexpr auto kCPU = "edm::test::cpu::";
0065       if (index != kInitialIndex and index != kLastIndex) {
0066         basename.replace(basename.find(kOther), strlen(kOther), kCPU);
0067         return {basename, kLastIndex};
0068       }
0069       if (index == kInitialIndex and basename.find(kGeneric) != std::string::npos) {
0070         if (not variant_.empty()) {
0071           if (variant_ == "other") {
0072             basename.replace(basename.find(kGeneric), strlen(kGeneric), kOther);
0073           } else if (variant_ == "cpu") {
0074             basename.replace(basename.find(kGeneric), strlen(kGeneric), kCPU);
0075           }
0076           return {basename, kLastIndex};
0077         }
0078         basename.replace(basename.find(kGeneric), strlen(kGeneric), kOther);
0079         return {basename, kInitialIndex + 1};
0080       }
0081       return {basename, kLastIndex};
0082     }
0083 
0084   private:
0085     std::string const variant_;
0086   };
0087   class ConfigurableTestTypeResolverMaker : public edm::ModuleTypeResolverMaker {
0088   public:
0089     ConfigurableTestTypeResolverMaker() = default;
0090     std::shared_ptr<ModuleTypeResolverBase const> makeResolver(edm::ParameterSet const& pset) const final {
0091       std::string variant;
0092       if (pset.existsAs<std::string>("variant", false)) {
0093         variant = pset.getUntrackedParameter<std::string>("variant");
0094         if (variant != "other" and variant != "cpu") {
0095           throw edm::Exception(edm::errors::Configuration) << "Variant must be 'other' or 'cpu'. Got " << variant;
0096         }
0097       }
0098       auto found = cache_.find(variant);
0099       if (found == cache_.end()) {
0100         bool inserted;
0101         std::tie(found, inserted) = cache_.emplace(variant, std::make_shared<ConfigurableTestTypeResolver>(variant));
0102       }
0103       return found->second;
0104     }
0105 
0106   private:
0107     // no protection needed because this object is used only in single-thread context
0108     CMS_SA_ALLOW mutable std::unordered_map<std::string, std::shared_ptr<ConfigurableTestTypeResolver>> cache_;
0109   };
0110 }  // namespace edm::test
0111 
0112 #endif