Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:37

0001 #ifndef PhysicsTools_MVAComputer_ProcessRegistry_h
0002 #define PhysicsTools_MVAComputer_ProcessRegistry_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     MVAComputer
0006 // Class  :     ProcessRegistry
0007 //
0008 
0009 //
0010 // Author:  Christophe Saout <christophe.saout@cern.ch>
0011 // Created:     Sat Apr 24 15:18 CEST 2007
0012 //
0013 // Refactoring for gcc 4.7.0 and higher
0014 // by Gena Kukartsev following design and advice from Chris Jones
0015 // January 2013
0016 //
0017 
0018 #include <string>
0019 #include "oneapi/tbb/concurrent_unordered_map.h"
0020 
0021 namespace PhysicsTools {
0022 
0023   // forward declaration
0024   template <class Base_t, class CalibBase_t, class Parent_t, class Instance_t, class Calibration_t>
0025   class ProcessRegistryImpl;
0026 
0027   /** \class ProcessRegistry
0028    *
0029    * \short Generic registry template for polymorphic processor implementations
0030    *
0031    * template parameters are: base class, calibration base class and a
0032    * pointer to a user-definable "parent type".
0033    * Template allows registration by name of a given base type using the factory.
0034    * The variable processors can register themselves with the registry of the
0035    * common base class.
0036    *
0037    ************************************************************/
0038   template <class Base_t, class CalibBase_t, class Parent_t>
0039   class ProcessRegistry {
0040   public:
0041 #ifndef __GCCXML__
0042 
0043     // template alias to replace the former Registry class
0044     template <class Instance_t, class Calibration_t>
0045     using Registry = ProcessRegistryImpl<Base_t, CalibBase_t, Parent_t, Instance_t, Calibration_t>;
0046 
0047 #endif
0048 
0049     /** \class Factory
0050      *
0051      * \short Factory helper class to instantiate a processor.
0052      *
0053      * The common base class of a processor can inherit from this
0054      * helper class to provide a create() method to instantiate variable
0055      * processor instances.
0056      *
0057      ************************************************************/
0058     class Factory {
0059     public:
0060       static Base_t *create(const char *name, const CalibBase_t *calib, Parent_t *parent = 0);
0061     };
0062 
0063   protected:
0064     friend class Factory;
0065 
0066     /// instantiate registry and registers itself with \a name
0067     ProcessRegistry(const char *name) : name(name) { registerProcess(name, this); }
0068     virtual ~ProcessRegistry() { unregisterProcess(name); }
0069 
0070     /// create an instance of \a name, given a calibration \a calib and parent \a parent
0071     static Base_t *create(const char *name, const CalibBase_t *calib, Parent_t *parent);
0072 
0073     /// virtual method to implement by respective processor instance classes
0074     virtual Base_t *instance(const char *name, const CalibBase_t *calib, Parent_t *parent) const = 0;
0075 
0076   private:
0077     static void registerProcess(const char *name, const ProcessRegistry *process);
0078     static void unregisterProcess(const char *name);
0079 
0080     typedef tbb::concurrent_unordered_map<std::string, const ProcessRegistry *> RegistryMap;
0081 
0082     /// return map of all registered processes, allocate if necessary
0083     static RegistryMap *getRegistry();
0084 
0085     const char *name;
0086   };  // class ProcessRegistry
0087 
0088   /** \class ProcessRegistryImpl (formerly ProcessRegistry::Registry)
0089    *
0090    * \short template to generate a registry singleton for a type.
0091    *
0092    * Instantiating an instance of this type registers that class
0093    * with the registry of the base type and provides a factory that
0094    * calls the constructor of the instance type.
0095    *
0096    ************************************************************/
0097   template <class Base_t, class CalibBase_t, class Parent_t, class Instance_t, class Calibration_t>
0098   class ProcessRegistryImpl : public ProcessRegistry<Base_t, CalibBase_t, Parent_t> {
0099   public:
0100     ProcessRegistryImpl(const char *name) : ProcessRegistry<Base_t, CalibBase_t, Parent_t>(name) {}
0101 
0102   protected:
0103     Base_t *instance(const char *name, const CalibBase_t *calib, Parent_t *parent) const override {
0104       return new Instance_t(name, dynamic_cast<const Calibration_t *>(calib), parent);
0105     }
0106   };  // class ProcessRegistryImpl
0107 
0108 }  // namespace PhysicsTools
0109 
0110 #endif  // PhysicsTools_MVAComputer_ProcessRegistry_h