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 
66 {
67 public:
69  // no public ctor for singleton, all user access is through Instance()
70 
71  G4VModularPhysicsList* GetReferencePhysList(const G4String&);
72  // instantiate a PhysList by name
73 
74  G4VModularPhysicsList* ReferencePhysList();
75  // instantiate a PhysList indicated by the environment variable "PHYSLIST"
76 
77  G4bool IsReferencePhysList(const G4String&);
78  // check if the name is in the list of PhysLists names
79 
80  const std::vector<G4String>& AvailablePhysLists() const;
81  // return a list of available PhysLists
82 
83  void PrintAvailablePhysLists() const;
84  // print a list of available PhysLists
85 
86  G4bool RegisterCreator(G4String name, PhysListCtorFuncPtr_t ctorptr, G4bool* ptr);
87  // register a new PhysList type by passing pointer to creator function
88 
89  G4bool RegisterPhysicsReplacement(G4String key, G4String physics);
90  // register a mapping between a key and a G4PhysicsConstructor
91  // assumes G4PhysicsConstructor is registered w/
92  // G4PhysicsProcessFactorySingleton
93 
94  void SetDefaultName(const G4String& defname) { defName = defname; }
95  const G4String& GetDefaultName() const { return defName; }
96 
97 private:
99  // the one and only instance
100 
101  std::map<G4String, PhysListCtorFuncPtr_t> fFunctionMap;
102  // mapping between known class names and a registered ctor function
103 
104  std::map<G4String, G4bool*> fBoolPtrMap;
105 
106  std::map<G4String, G4String> fPhysicsReplaceList;
107  // mapping between EM variant key to G4VPhysicsConstructor class
108  // uses G4PhysicsProcessFactorySingleton to get class
109  //
110 
111  G4String defName;
112  // name of default in case of unset PHYSLIST
113 
114  mutable std::vector<G4String> listnames;
115  // copy of list of names, used solely due to AvailablePhysList() method
116  // returning a const reference rather than a vector object.
117  // mutable because AvailablePhysLists() is const, but list might need
118  // recreation if new entries have been registered.
119 
120 private:
122  // private ctor, users access via Instance()
123 
124  G4String GetBaseName(G4String name, std::vector<G4String>& physicsReplacements, G4bool& allKnown);
125  // strip out physics replacement keys and return base physics list name
126  // update vector with the name of the physics processes to be replaced
127 
128  virtual ~G4PhysListFactorySingleton();
129 
131  // method private and not implement, declared to prevent copying
132 
134  // method private and not implement, declared to prevent assignment
135 
136  // sub-class Cleaner struct is used to clean up singleton at the end of job.
137  struct Cleaner {
138  void UseMe() { } // Dummy method to quiet compiler
143  } } };
144  friend struct Cleaner;
145 
146 };
147 
148 // Define macro to create a function to call the class' ctor
149 // and then registers this function with the factory instance for later use
150 // Users should have in their myPhyList.cc two lines that look like:
151 // #include "G4PhysListFactorySingleton.hh"
152 // PHYSLISTREG(myPhysList) // no semicolon
153 // where "myPhysList" is the name of the class (assuming no special namespace)
154 // If the class is defined in a namespace use:
155 // #include "G4PhysListFactorySingleton.hh"
156 // PHYSLISTREG3(myspace,myAltPhyList,myspace::myAltPhyList) // no semicolon
157 // and either can then be retrieved from the factory using:
158 // G4PhysListFactory factory;
159 // G4VModularPhysicsList* p = 0;
160 // p = factory.GetReferencePhysList("myPhyList");
161 // p = factory.GetReferencePhysList("myspace::myAltPhysList");
162 //
163 // The expanded code looks like:
164 // G4VModularPhysicsList* myPhysList_ctor_function () { return new myPhysList; }
165 // static G4bool myPhysList_creator_registered =
166 // G4PhysListFactorySingleton::Instance().RegisterCreator("myPhysList",
167 // & myPhysList_ctor_function );
168 // namespace myspace {
169 // G4VModularPhysicsList* myAltPhysList_ctor_function () { return new myspace::myAltPhysList; }
170 // static G4bool myPhysList_creator_registered =
171 // G4PhysListFactorySingleton::Instance().RegisterCreator("myspace::myAltPhysList",
172 // & myspace::myAltPhysList_ctor_function ); }
173 
174 #define PHYSLISTREG( _name ) \
175  G4VModularPhysicsList* _name ## _ctor_function () { return new _name; } \
176  static G4bool _name ## _creator_registered = \
177  G4PhysListFactorySingleton::Instance().RegisterCreator(# _name, \
178  & _name ## _ctor_function, \
179  & _name ## _creator_registered );
180 
181 #define PHYSLISTREG3( _ns, _name, _fqname ) \
182 namespace _ns { \
183  G4VModularPhysicsList* _name ## _ctor_function () { return new _fqname; } \
184  static G4bool _name ## _creator_registered = \
185  G4PhysListFactorySingleton::Instance().RegisterCreator(# _fqname, \
186  & _fqname ## _ctor_function, \
187  & _fqname ## _creator_registered );}
188 #endif
static QCString name
Definition: declinfo.cpp:673
static G4PhysListFactorySingleton * fgTheInstance
void SetDefaultName(const G4String &defname)
std::map< G4String, PhysListCtorFuncPtr_t > fFunctionMap
std::map< G4String, G4String > fPhysicsReplaceList
void operator=(const G4PhysListFactorySingleton &)
G4String GetBaseName(G4String name, std::vector< G4String > &physicsReplacements, G4bool &allKnown)
const std::vector< G4String > & AvailablePhysLists() const
def key(type, name=None)
Definition: graph.py:13
G4bool RegisterPhysicsReplacement(G4String key, G4String physics)
const G4String & GetDefaultName() const
G4VModularPhysicsList *(* PhysListCtorFuncPtr_t)()
G4bool IsReferencePhysList(const G4String &)
G4bool RegisterCreator(G4String name, PhysListCtorFuncPtr_t ctorptr, G4bool *ptr)
static G4PhysListFactorySingleton & Instance()
G4VModularPhysicsList * ReferencePhysList()
G4VModularPhysicsList * GetReferencePhysList(const G4String &)
std::map< G4String, G4bool * > fBoolPtrMap