G4PhysListFactorySingleton.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // $Id: G4PhysListFactorySingleton.hh,v 1.4 2012-09-20 21:47:05 greenc Exp $
27 // GEANT4 tag $Name: not supported by cvs2svn $
28 //
29 //---------------------------------------------------------------------------
30 //
31 // ClassName: G4PhysListFactorySingleton
32 //
33 // Author: 2011-10-06 R. Hatcher
34 //
35 // Modified:
36 //
37 // Class Description: A singleton holding a map between names and
38 // pointers-to-functions (that call a class default constructor).
39 // The functions pointers must return G4VModularPhysicList*.
40 //
41 // Methods other than those related to the singleton-ness and
42 // registering function pointers are based on the original
43 // G4PhysListFactory methods and supply the same functionality.
44 //
45 // Class header also defines cpp macros for automatically defining
46 // and registering functions mapped to PhysicsList classes.
47 //
48 // Special handling is performed for names that include one of:
49 // "_EMV" "_EMX" "_EMY" "_LIV" "_PEN"
50 // which override the base physics list default EM physics
51 //
52 //----------------------------------------------------------------------------
53 //
54 #ifndef G4PhysListFactorySingleton_h
55 #define G4PhysListFactorySingleton_h 1
56 
57 #include "Geant4/G4VModularPhysicsList.hh"
58 #include "Geant4/globals.hh"
59 
60 // define a type for the pointer to a function that returns a
61 // G4VModularPhysicsList*
62 // i.e. calls the (typically default) ctor for the class.
63 typedef G4VModularPhysicsList* (*PhysListCtorFuncPtr_t)();
64 
65 namespace g4nu {
66 
68 {
69 public:
71  // no public ctor for singleton, all user access is through Instance()
72 
73  G4VModularPhysicsList* GetReferencePhysList(const G4String&);
74  // instantiate a PhysList by name
75 
76  G4VModularPhysicsList* ReferencePhysList();
77  // instantiate a PhysList indicated by the environment variable "PHYSLIST"
78 
79  G4bool IsReferencePhysList(const G4String&);
80  // check if the name is in the list of PhysLists names
81 
82  const std::vector<G4String>& AvailablePhysLists() const;
83  // return a list of available PhysLists
84 
85  void PrintAvailablePhysLists() const;
86  // print a list of available PhysLists
87 
88  G4bool RegisterCreator(G4String name, PhysListCtorFuncPtr_t ctorptr, G4bool* ptr);
89  // register a new PhysList type by passing pointer to creator function
90 
91  G4bool RegisterPhysicsReplacement(G4String key, G4String physics);
92  // register a mapping between a key and a G4PhysicsConstructor
93  // assumes G4PhysicsConstructor is registered w/
94  // G4PhysicsProcessFactorySingleton
95 
96  void SetDefaultName(const G4String& defname) { defName = defname; }
97  const G4String& GetDefaultName() const { return defName; }
98 
99 private:
101  // the one and only instance
102 
103  std::map<G4String, PhysListCtorFuncPtr_t> fFunctionMap;
104  // mapping between known class names and a registered ctor function
105 
106  std::map<G4String, G4bool*> fBoolPtrMap;
107 
108  std::map<G4String, G4String> fPhysicsReplaceList;
109  // mapping between EM variant key to G4VPhysicsConstructor class
110  // uses G4PhysicsProcessFactorySingleton to get class
111  //
112 
113  G4String defName;
114  // name of default in case of unset PHYSLIST
115 
116  mutable std::vector<G4String> listnames;
117  // copy of list of names, used solely due to AvailablePhysList() method
118  // returning a const reference rather than a vector object.
119  // mutable because AvailablePhysLists() is const, but list might need
120  // recreation if new entries have been registered.
121 
122 private:
124  // private ctor, users access via Instance()
125 
126  G4String GetBaseName(G4String name, std::vector<G4String>& physicsReplacements, G4bool& allKnown);
127  // strip out physics replacement keys and return base physics list name
128  // update vector with the name of the physics processes to be replaced
129 
130  virtual ~G4PhysListFactorySingleton();
131 
133  // method private and not implement, declared to prevent copying
134 
136  // method private and not implement, declared to prevent assignment
137 
138  // sub-class Cleaner struct is used to clean up singleton at the end of job.
139  struct Cleaner {
140  void UseMe() { } // Dummy method to quiet compiler
145  } } };
146  friend struct Cleaner;
147 
148 };
149 
150 } // end-of-namespace g4nu
151 
152 // Define macro to create a function to call the class' ctor
153 // and then registers this function with the factory instance for later use
154 // Users should have in their myPhyList.cc two lines that look like:
155 // #include "G4PhysListFactorySingleton.hh"
156 // PHYSLISTREG(myPhysList) // no semicolon
157 // where "myPhysList" is the name of the class (assuming no special namespace)
158 // If the class is defined in a namespace use:
159 // #include "G4PhysListFactorySingleton.hh"
160 // PHYSLISTREG3(myspace,myAltPhyList,myspace::myAltPhyList) // no semicolon
161 // and either can then be retrieved from the factory using:
162 // G4PhysListFactory factory;
163 // G4VModularPhysicsList* p = 0;
164 // p = factory.GetReferencePhysList("myPhyList");
165 // p = factory.GetReferencePhysList("myspace::myAltPhysList");
166 //
167 // The expanded code looks like:
168 // G4VModularPhysicsList* myPhysList_ctor_function () { return new myPhysList; }
169 // static G4bool myPhysList_creator_registered =
170 // G4PhysListFactorySingleton::Instance().RegisterCreator("myPhysList",
171 // & myPhysList_ctor_function );
172 // namespace myspace {
173 // G4VModularPhysicsList* myAltPhysList_ctor_function () { return new myspace::myAltPhysList; }
174 // static G4bool myPhysList_creator_registered =
175 // G4PhysListFactorySingleton::Instance().RegisterCreator("myspace::myAltPhysList",
176 // & myspace::myAltPhysList_ctor_function ); }
177 
178 #define PHYSLISTREG( _name ) \
179  G4VModularPhysicsList* _name ## _ctor_function () { return new _name; } \
180  static G4bool _name ## _creator_registered = \
181  g4nu::G4PhysListFactorySingleton::Instance().RegisterCreator(# _name, \
182  & _name ## _ctor_function, \
183  & _name ## _creator_registered );
184 
185 #define PHYSLISTREG3( _ns, _name, _fqname ) \
186 namespace _ns { \
187  G4VModularPhysicsList* _name ## _ctor_function () { return new _fqname; } \
188  static G4bool _name ## _creator_registered = \
189  g4nu::G4PhysListFactorySingleton::Instance().RegisterCreator(# _fqname, \
190  & _fqname ## _ctor_function, \
191  & _fqname ## _creator_registered );}
192 #endif
static QCString name
Definition: declinfo.cpp:673
static G4PhysListFactorySingleton & Instance()
const std::vector< G4String > & AvailablePhysLists() const
std::map< G4String, PhysListCtorFuncPtr_t > fFunctionMap
G4String GetBaseName(G4String name, std::vector< G4String > &physicsReplacements, G4bool &allKnown)
G4VModularPhysicsList * GetReferencePhysList(const G4String &)
def key(type, name=None)
Definition: graph.py:13
void SetDefaultName(const G4String &defname)
void operator=(const G4PhysListFactorySingleton &)
G4VModularPhysicsList *(* PhysListCtorFuncPtr_t)()
G4bool RegisterCreator(G4String name, PhysListCtorFuncPtr_t ctorptr, G4bool *ptr)
std::map< G4String, G4bool * > fBoolPtrMap
static G4PhysListFactorySingleton * fgTheInstance
G4bool RegisterPhysicsReplacement(G4String key, G4String physics)
std::map< G4String, G4String > fPhysicsReplaceList