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 
57 namespace g4nu {
58 
60 {
61 public:
63  // no public ctor for singleton, all user access is through Instance()
64 
65  G4VPhysicsConstructor* GetPhysicsProcess(const G4String&);
66  // instantiate a PhysProc by name
67 
68  G4bool IsKnownPhysicsProcess(const G4String&);
69  // check if the name is in the list of PhysicsProcess names
70 
71  const std::vector<G4String>& AvailablePhysicsProcesses() const;
72  // return a list of available PhysicsProcess names
73 
74  void PrintAvailablePhysicsProcesses() const;
75  // print a list of available PhysicsProcess names
76 
77  G4bool RegisterCreator(G4String name, PhysProcCtorFuncPtr_t ctorptr, G4bool* ptr);
78  // register a new PhysProc type by passing pointer to creator function
79 
80 private:
82  // the one and only instance
83 
84  std::map<G4String, PhysProcCtorFuncPtr_t> fFunctionMap;
85  // mapping between known class names and a registered ctor function
86 
87  std::map<G4String, G4bool*> fBoolPtrMap;
88 
89  mutable std::vector<G4String> listnames;
90  // copy of list of names, used solely due to AvailablePhysicsProcesses()
91  // method returning a const reference rather than a vector object.
92  // mutable because AvailablePhysicsProcesses() is const, but list might
93  // need recreation if new entries have been registered.
94 
95 private:
97  // private ctor, users access class via Instance()
98 
100 
102  // method private and not implement, declared to prevent copying
103 
105  // method private and not implement, declared to prevent assignment
106 
107  // sub-class Cleaner struct is used to clean up singleton at the end of job.
108  struct Cleaner {
109  void UseMe() { } // Dummy method to quiet compiler
114  } } };
115  friend struct Cleaner;
116 
117 };
118 
119 } // end-of-namespace g4nu
120 
121 // Define macro to create a function to call the class' ctor
122 // and then registers this function with the factory instance for later use
123 // Users should have in their myPhyList.cc two lines that look like:
124 // #include "G4PhysicsProcessFactorySingleton.hh"
125 // PHYSPROCREG(myPhysProc) // no semicolon
126 // where "myPhysProc" is the name of the class (assuming no special namespace)
127 // If the class is defined in a namespace use:
128 // #include "G4PhysicsProcessFactorySingleton.hh"
129 // PHYSPROCREG3(myspace,myAltPhysProc,myspace::myAltPhysProc) // no semicolon
130 // and either can then be retrieved from the factory using:
131 // G4PhysicsProcessFactorySingleton& factory =
132 // G4PhysicsProcessFactorySingleton::Instance();
133 // G4VPhysicsConstructor* p = 0;
134 // p = factory.GetPhysicsProcess("myPhyList");
135 // p = factory.GetPhysicsProcess("myspace::myAltPhysProc");
136 //
137 // The expanded code looks like:
138 // G4VPhysicsConstructor* myPhysProc_ctor_function () { return new myPhysProc; }
139 // static G4bool myPhysProc_creator_registered =
140 // G4PhysicsProcessFactorySingleton::Instance().RegisterCreator("myPhysProc",
141 // & myPhysProc_ctor_function );
142 // namespace myspace {
143 // G4VPhysicsConstructor* myAltPhysProc_ctor_function () { return new myspace::myAltPhysProc; }
144 // static G4bool myPhysProc_creator_registered =
145 // G4PhysicsProcessFactorySingleton::Instance().RegisterCreator("myspace::myAltPhysProc",
146 // & myspace::myAltPhysProc_ctor_function ); }
147 
148 #define PHYSPROCREG( _name ) \
149  G4VPhysicsConstructor* _name ## _ctor_function () { return new _name; } \
150  static G4bool _name ## _creator_registered = \
151  g4nu::G4PhysicsProcessFactorySingleton::Instance().RegisterCreator(# _name, \
152  & _name ## _ctor_function, \
153  & _name ## _creator_registered );
154 
155 #define PHYSPROCREG3( _ns, _name, _fqname ) \
156 namespace _ns { \
157  G4VPhysicsConstructor* _name ## _ctor_function () { return new _fqname; } \
158  static G4bool _name ## _creator_registered = \
159  g4nu::G4PhysicsProcessFactorySingleton::Instance().RegisterCreator(# _fqname, \
160  & _fqname ## _ctor_function, \
161  & _fqname ## _creator_registered );}
162 #endif
static QCString name
Definition: declinfo.cpp:673
void operator=(const G4PhysicsProcessFactorySingleton &)
G4bool RegisterCreator(G4String name, PhysProcCtorFuncPtr_t ctorptr, G4bool *ptr)
const std::vector< G4String > & AvailablePhysicsProcesses() const
static G4PhysicsProcessFactorySingleton * fgTheInstance
static G4PhysicsProcessFactorySingleton & Instance()
std::map< G4String, PhysProcCtorFuncPtr_t > fFunctionMap
G4VPhysicsConstructor *(* PhysProcCtorFuncPtr_t)()
G4VPhysicsConstructor * GetPhysicsProcess(const G4String &)