Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-23 09:10:40

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 #ifndef G4CrossSectionFactory_h
0027 #define G4CrossSectionFactory_h 1
0028 
0029 
0030 #include "globals.hh"
0031 #include "G4VCrossSectionDataSet.hh"
0032 #include "G4CrossSectionFactoryRegistry.hh"
0033 #include "G4Threading.hh"
0034 
0035 class G4VBaseXSFactory
0036 {
0037 public:
0038 
0039   G4VBaseXSFactory()
0040   {
0041     fRegistry = G4CrossSectionFactoryRegistry::Instance();
0042   }
0043    
0044   virtual ~G4VBaseXSFactory() = default;
0045 
0046   virtual G4VCrossSectionDataSet* Instantiate() = 0;
0047 
0048 protected:
0049   G4CrossSectionFactoryRegistry* fRegistry;
0050 };
0051 
0052 //Generic template XS-factory
0053 template <typename T, int mode> class G4CrossSectionFactory : public G4VBaseXSFactory
0054 {
0055 public:
0056   G4CrossSectionFactory(const G4String& name)
0057   {
0058     fRegistry->Register(name, this);
0059   }
0060 
0061   G4VCrossSectionDataSet* Instantiate() override
0062   {
0063       G4ExceptionDescription msg;
0064       msg<<"Factory mode: "<<mode<<" not supported!";
0065       G4Exception("G4CrossSectionFactory::Instantiate","CrossSectionFactory001",FatalException,msg);
0066     return static_cast<T*>(0);
0067   }
0068 };
0069 
0070 //Partial specialized template for non-singleton non-shared factory
0071 // each call to Instantiate creates a new XS
0072 template <typename T> class G4CrossSectionFactory<T,0> : public G4VBaseXSFactory
0073 {
0074 public:
0075     
0076   G4CrossSectionFactory(const G4String& name)
0077   {
0078     fRegistry->Register(name, this);
0079   }
0080 
0081   G4VCrossSectionDataSet* Instantiate() override
0082   {
0083     return new T();
0084   }
0085 };
0086 
0087 //Partial specialized template for singleton, shared factory
0088 // each call to Instantiate returns pointer to static object
0089 template <typename T> class G4CrossSectionFactory<T,1> : public G4VBaseXSFactory
0090 {
0091 public:
0092   G4CrossSectionFactory(const G4String& name)
0093   {
0094     fRegistry->Register(name,this);
0095   }
0096 
0097   G4VCrossSectionDataSet* Instantiate() override
0098   {
0099     static T* shared = new T();
0100     return shared;
0101   }
0102 };
0103 
0104 //Partial specialized template for singleton, shared factory
0105 // each call to Instantiate returns pointer to static thread-local object
0106 template <typename T> class G4CrossSectionFactory<T,2> : public G4VBaseXSFactory
0107 {
0108   G4CrossSectionFactory(const G4String& name)
0109   {
0110     fRegistry->Register(name,this);
0111   }
0112 
0113   G4VCrossSectionDataSet* Instantiate() override
0114   {
0115     static G4ThreadLocal T* shared = new T();
0116     return shared;
0117   }
0118 };
0119 
0120 
0121 #define G4_BASE_DECLARE_XS_FACTORY(cross_section, flag) \
0122   const G4CrossSectionFactory<cross_section,flag>& cross_section##Factory = G4CrossSectionFactory<cross_section,flag>(cross_section::Default_Name())
0123 
0124 #define G4_BASE_REFERENCE_XS_FACTORY(cross_section,flag) \
0125   class cross_section; \
0126   extern const G4CrossSectionFactory<cross_section,flag>& cross_section##Factory; \
0127   const G4CrossSectionFactory<cross_section,flag>& cross_section##FactoryRef = cross_section##Factory
0128 
0129 //Macros to help define and reference factories
0130 #define G4_DECLARE_XS_FACTORY(cross_section) G4_BASE_DECLARE_XS_FACTORY(cross_section,0)
0131 #define G4_DECLARE_SHAREDXS_FACTORY(cross_section) G4_BASE_DECLARE_XS_FACTORY(cross_section,1)
0132 #define G4_DECLARE_SHAREDTLSXS_FACTORY(cross_section) G4_BASE_DECLARE_XS_FACTORY(cross_section,2)
0133 
0134 
0135 #define G4_REFERENCE_XS_FACTORY(cross_section) G4_BASE_REFERENCE_XS_FACTORY(cross_section,0)
0136 #define G4_REFERENCE_SHAREDXS_FACTORY(cross_section) G4_BASE_REFERENCE_XS_FACTORY(cross_section,1)
0137 #define G4_REFERENCE_SHAREDTLSXS_FACTORY(cross_section) G4_BASE_REFERENCE_XS_FACTORY(cross_section,2)
0138 
0139 #endif