G4PhysicsProcessFactorySingleton.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: G4PhysicsProcessFactorySingleton.hh,v 1.2 2012-09-20 21:47:05 greenc Exp $
27 // GEANT4 tag $Name: not supported by cvs2svn $
28 //
29 //---------------------------------------------------------------------------
30 //
31 // ClassName: G4PhysicsProcessFactorySingleton
32 //
33 // Author: 2012-02-02 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 G4VPhysicsConstructor*.
40 //
41 // Class header also defines cpp macros for automatically defining
42 // and registering functions mapped to G4VPhysicsConstructor classes.
43 //
44 //----------------------------------------------------------------------------
45 //
46 #ifndef G4PhysicsProcessFactorySingleton_h
47 #define G4PhysicsProcessFactorySingleton_h 1
48 
49 #include "Geant4/G4VPhysicsConstructor.hh"
50 #include "Geant4/globals.hh"
51 
52 // define a type for the pointer to a function that returns a
53 // G4VPhysicsConstructor*
54 // i.e. calls the (typically default) ctor for the class.
55 typedef G4VPhysicsConstructor* (*PhysProcCtorFuncPtr_t)();
56 
58 {
59 public:
61  // no public ctor for singleton, all user access is through Instance()
62 
63  G4VPhysicsConstructor* GetPhysicsProcess(const G4String&);
64  // instantiate a PhysProc by name
65 
66  G4bool IsKnownPhysicsProcess(const G4String&);
67  // check if the name is in the list of PhysicsProcess names
68 
69  const std::vector<G4String>& AvailablePhysicsProcesses() const;
70  // return a list of available PhysicsProcess names
71 
72  void PrintAvailablePhysicsProcesses() const;
73  // print a list of available PhysicsProcess names
74 
75  G4bool RegisterCreator(G4String name, PhysProcCtorFuncPtr_t ctorptr, G4bool* ptr);
76  // register a new PhysProc type by passing pointer to creator function
77 
78 private:
80  // the one and only instance
81 
82  std::map<G4String, PhysProcCtorFuncPtr_t> fFunctionMap;
83  // mapping between known class names and a registered ctor function
84 
85  std::map<G4String, G4bool*> fBoolPtrMap;
86 
87  mutable std::vector<G4String> listnames;
88  // copy of list of names, used solely due to AvailablePhysicsProcesses()
89  // method returning a const reference rather than a vector object.
90  // mutable because AvailablePhysicsProcesses() is const, but list might
91  // need recreation if new entries have been registered.
92 
93 private:
95  // private ctor, users access class via Instance()
96 
98 
100  // method private and not implement, declared to prevent copying
101 
103  // method private and not implement, declared to prevent assignment
104 
105  // sub-class Cleaner struct is used to clean up singleton at the end of job.
106  struct Cleaner {
107  void UseMe() { } // Dummy method to quiet compiler
112  } } };
113  friend struct Cleaner;
114 
115 };
116 
117 // Define macro to create a function to call the class' ctor
118 // and then registers this function with the factory instance for later use
119 // Users should have in their myPhyList.cc two lines that look like:
120 // #include "G4PhysicsProcessFactorySingleton.hh"
121 // PHYSPROCREG(myPhysProc) // no semicolon
122 // where "myPhysProc" is the name of the class (assuming no special namespace)
123 // If the class is defined in a namespace use:
124 // #include "G4PhysicsProcessFactorySingleton.hh"
125 // PHYSPROCREG3(myspace,myAltPhysProc,myspace::myAltPhysProc) // no semicolon
126 // and either can then be retrieved from the factory using:
127 // G4PhysicsProcessFactorySingleton& factory =
128 // G4PhysicsProcessFactorySingleton::Instance();
129 // G4VPhysicsConstructor* p = 0;
130 // p = factory.GetPhysicsProcess("myPhyList");
131 // p = factory.GetPhysicsProcess("myspace::myAltPhysProc");
132 //
133 // The expanded code looks like:
134 // G4VPhysicsConstructor* myPhysProc_ctor_function () { return new myPhysProc; }
135 // static G4bool myPhysProc_creator_registered =
136 // G4PhysicsProcessFactorySingleton::Instance().RegisterCreator("myPhysProc",
137 // & myPhysProc_ctor_function );
138 // namespace myspace {
139 // G4VPhysicsConstructor* myAltPhysProc_ctor_function () { return new myspace::myAltPhysProc; }
140 // static G4bool myPhysProc_creator_registered =
141 // G4PhysicsProcessFactorySingleton::Instance().RegisterCreator("myspace::myAltPhysProc",
142 // & myspace::myAltPhysProc_ctor_function ); }
143 
144 #define PHYSPROCREG( _name ) \
145  G4VPhysicsConstructor* _name ## _ctor_function () { return new _name; } \
146  static G4bool _name ## _creator_registered = \
147  G4PhysicsProcessFactorySingleton::Instance().RegisterCreator(# _name, \
148  & _name ## _ctor_function, \
149  & _name ## _creator_registered );
150 
151 #define PHYSPROCREG3( _ns, _name, _fqname ) \
152 namespace _ns { \
153  G4VPhysicsConstructor* _name ## _ctor_function () { return new _fqname; } \
154  static G4bool _name ## _creator_registered = \
155  G4PhysicsProcessFactorySingleton::Instance().RegisterCreator(# _fqname, \
156  & _fqname ## _ctor_function, \
157  & _fqname ## _creator_registered );}
158 #endif
static QCString name
Definition: declinfo.cpp:673
G4bool RegisterCreator(G4String name, PhysProcCtorFuncPtr_t ctorptr, G4bool *ptr)
const std::vector< G4String > & AvailablePhysicsProcesses() const
static G4PhysicsProcessFactorySingleton & Instance()
std::map< G4String, PhysProcCtorFuncPtr_t > fFunctionMap
G4VPhysicsConstructor *(* PhysProcCtorFuncPtr_t)()
static G4PhysicsProcessFactorySingleton * fgTheInstance
G4VPhysicsConstructor * GetPhysicsProcess(const G4String &)
void operator=(const G4PhysicsProcessFactorySingleton &)