CLHEP/HepPDT/DMFactory.hh

00001 
00002 //
00003 // File: DMFactory.hh
00004 // $Id: DMFactory.hh,v 1.8 2002/05/31 12:34:53 evc Exp $
00005 // Purpose: interface for the DMFactory class
00006 //
00007 // Author:  Marc Paterno
00008 // Created:
00009 // Modified:
00010 //
00012 
00013 #ifndef PATTERNS_DMFACTORY_HPP
00014 #define PATTERNS_DMFACTORY_HPP
00015 
00016 #if defined(NT_MSVCPP)
00017 #pragma warning (disable: 4786)
00018 #endif
00019 
00020 #include "CLHEP/config/CLHEP.h"
00021 #include "CLHEP/config/iostream.h"
00022 #include <assert.h>
00023 #include <memory>
00024 #include <string>
00025 #include <vector>
00026 #include <map>
00027 #include <algorithm>
00028 #include <iterator>
00029 
00030 #include "CLHEP/HepPDT/DecayModelBase.hh"
00031 #include "CLHEP/HepPDT/pretend.hh"
00032 //
00033 // This template deals with creation of objects of type T.
00034 // T must inherit from DecayModelBase.
00035 //
00036 
00037 namespace HepPDT {
00038 
00043 template< class Config >
00044 struct MakerBase
00045 {
00046   typedef typename Config::ParticleData PD;     // ParticleDataType
00047   typedef typename Config::PT           PT;     // ParticleType
00048   typedef DecayModelBase<Config>        Product; 
00049         virtual Product* make(const std::vector<double>& arg) = 0;
00050         virtual ~MakerBase() {};
00051 };
00052 
00057 template <class T, class Config>
00058 struct Maker : public MakerBase<Config>
00059 {
00060   typedef DecayModelBase<Config>        Product; 
00061         virtual ~Maker() {}
00062         virtual Product* make(const std::vector<double>& arg) { return new T(arg); }
00063 };
00064 
00069 template< class Config >
00070 class DMFactory
00071 {
00072 public:
00073   typedef typename Config::ParticleData PD;     // ParticleDataType
00074   typedef typename Config::PT           PT;     // ParticleType
00075   typedef DecayModelBase<Config>            Product; 
00076         
00077         typedef std::map<std::string, MakerBase<Config>*>  funcmap;
00078         typedef typename funcmap::const_iterator      const_iterator;
00079         typedef typename funcmap::iterator            iterator;
00080         
00081         // The singleton instance
00082         static DMFactory* instance();
00083         
00084 
00085         void addProduct(const std::string& k, MakerBase<Config>* m);
00086 
00087         std::auto_ptr<Product> makeProduct(const std::string& k, 
00088                                            const std::vector<double>& a) const;
00089         
00090         // Don't use the following, unless you like memory leaks.
00091         Product*         unsafeMakeProduct(const std::string& k, 
00092                                            const std::vector<double>& a) const;
00093                 
00094         // Return the number of keys we know about (same as the number of
00095         // products, unless one product comes under more than one key).
00096         unsigned long numKeys() const;
00097 
00098         
00099 private:
00100         
00101         funcmap _makerfuncs;
00102         
00103         // Singleton stuff
00104         DMFactory();
00105         ~DMFactory();
00106 
00107         DMFactory(const DMFactory&);            // not implemented
00108         DMFactory& operator=(const DMFactory&); // not implemented
00109 
00110         static DMFactory* _inst;
00111         static void destroy_and_clear();
00112                 
00113         struct Cleaner;
00114         friend struct Cleaner;
00115         struct Cleaner { ~Cleaner() { DMFactory::destroy_and_clear(); } };
00116                 
00117 }; // class DMFactory
00118 
00119 //
00120 // This template deals with registration of products. Creation of an
00121 // instance of RegisterMe<T> registers something than can create a T
00122 // in the DMFactory.
00123 //
00124 
00129 template <class T, class Config>
00130 struct RegisterMe
00131 {
00132         RegisterMe(const std::string& key)
00133         {
00134                 typedef T product_t;
00135                 MakerBase<Config>* p = new Maker<product_t,Config>;
00136                 DMFactory<Config>::instance()->addProduct(key, p);
00137         }
00138 };
00139 
00140 }       // HepPDT
00141 
00142 #include "CLHEP/HepPDT/DMFactory.icc"
00143 
00144 #endif // #ifndef PATTERNS_DMFACTORY_HPP
00145 
00146 /*
00147 ** Local Variables: --
00148 ** mode: c++ --
00149 ** c-file-style: "gnu" --
00150 ** tab-width: 2 --
00151 ** End: --
00152 */

Class Library for High Energy Physics (version 1.8)