Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-30 02:33:12

0001 #ifndef JetMETCorrections_FFTJetObjects_FFTJetObjectFactory_h
0002 #define JetMETCorrections_FFTJetObjects_FFTJetObjectFactory_h
0003 
0004 //
0005 // Simple factory for objects which can be constructed from ParameterSet
0006 //
0007 #include <map>
0008 #include <string>
0009 
0010 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0011 #include "FWCore/Utilities/interface/Exception.h"
0012 
0013 template <class Base>
0014 struct AbsFFTJetObjectFactory {
0015   virtual ~AbsFFTJetObjectFactory() {}
0016   virtual Base* create(const edm::ParameterSet& ps) const = 0;
0017 };
0018 
0019 template <class Base, class Derived>
0020 struct ConcreteFFTJetObjectFactory : public AbsFFTJetObjectFactory<Base> {
0021   ~ConcreteFFTJetObjectFactory() override {}
0022   inline Derived* create(const edm::ParameterSet& ps) const override { return new Derived(ps); }
0023 };
0024 
0025 template <class Base>
0026 struct DefaultFFTJetObjectFactory : public std::map<std::string, AbsFFTJetObjectFactory<Base>*> {
0027   typedef Base base_type;
0028 
0029   inline DefaultFFTJetObjectFactory() : std::map<std::string, AbsFFTJetObjectFactory<Base>*>() {}
0030 
0031   virtual ~DefaultFFTJetObjectFactory() {
0032     for (typename std::map<std::string, AbsFFTJetObjectFactory<Base>*>::iterator it = this->begin(); it != this->end();
0033          ++it)
0034       delete it->second;
0035   }
0036 
0037   inline Base* create(const std::string& derivedType, const edm::ParameterSet& ps) const {
0038     typename std::map<std::string, AbsFFTJetObjectFactory<Base>*>::const_iterator it = this->find(derivedType);
0039     if (it == this->end())
0040       throw cms::Exception("KeyNotFound")
0041           << "Derived type \"" << derivedType << "\" is not registered with the factory\n";
0042     return it->second->create(ps);
0043   }
0044 
0045   DefaultFFTJetObjectFactory(const DefaultFFTJetObjectFactory&) = delete;
0046   DefaultFFTJetObjectFactory& operator=(const DefaultFFTJetObjectFactory&) = delete;
0047 };
0048 
0049 //
0050 // Singleton for the factory
0051 //
0052 template <class Factory>
0053 class StaticFFTJetObjectFactory {
0054 public:
0055   typedef typename Factory::Base::base_type base_type;
0056 
0057   static const Factory& instance() {
0058     static Factory const obj;
0059     return obj;
0060   }
0061 
0062   template <class T>
0063   static void registerType(const std::string& className) {
0064     Factory& rd = const_cast<Factory&>(instance());
0065     delete rd[className];
0066     rd[className] = new ConcreteFFTJetObjectFactory<base_type, T>();
0067   }
0068 
0069   StaticFFTJetObjectFactory() = delete;
0070 };
0071 
0072 #endif  // JetMETCorrections_FFTJetObjects_FFTJetObjectFactory_h