Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4NavigationHistoryPool.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// G4NavigationHistoryPool
27//
28// Class description:
29//
30// Thread-local pool for navigation history levels collections being
31// allocated by G4NavigationHistory. Allows for reuse of the vectors
32// allocated according to lifetime of G4NavigationHistory objects.
33
34// Author: Gabriele Cosmo (CERN), 07.05.2014 - Initial version
35// --------------------------------------------------------------------
36#ifndef G4NAVIGATIONHISTORYPOOL_HH
37#define G4NAVIGATIONHISTORYPOOL_HH
38
39#include <vector>
40
41#include "G4NavigationLevel.hh"
42
43/**
44 * @brief G4NavigationHistoryPool is a thread-local pool for navigation history
45 * levels collections being allocated by G4NavigationHistory. It allows for
46 * reuse of the vectors allocated according to lifetime of G4NavigationHistory
47 * objects.
48 */
49
50class G4NavigationHistoryPool
51{
52 public:
53
54 /**
55 * Destructor: takes care to delete the allocated levels.
56 */
58
59 /**
60 * Returns the unique instance of G4NavigationHistoryPool.
61 */
62 static G4NavigationHistoryPool* GetInstance();
63
64 /**
65 * Returns the pointer to a new collection of levels being allocated.
66 */
67 inline std::vector<G4NavigationLevel> * GetNewLevels();
68
69 /**
70 * Returns the pointer of the first available collection of levels
71 * If none are available (i.e. empty free vector) allocates the collection.
72 */
73 inline std::vector<G4NavigationLevel> * GetLevels();
74
75 /**
76 * Deactivates the levels collection in pool.
77 */
78 inline void DeRegister(std::vector<G4NavigationLevel> * pLevels);
79
80 /**
81 * Deletes all levels stored in the pool.
82 */
83 void Clean();
84
85 /**
86 * Prints the number of entries.
87 */
88 void Print() const;
89
90 private:
91
92 /**
93 * Private default Constructor.
94 */
95 G4NavigationHistoryPool();
96
97 /**
98 * Registers the levels collection to the pool and activates it.
99 */
100 inline void Register(std::vector<G4NavigationLevel> * pLevels);
101
102 /**
103 * Sets internal vectors content to zero.
104 */
105 void Reset();
106
107 private:
108
109 static G4ThreadLocal G4NavigationHistoryPool* fgInstance;
110
111 std::vector<std::vector<G4NavigationLevel> *> fPool;
112 std::vector<std::vector<G4NavigationLevel> *> fFree;
113};
114
115// ***************************************************************************
116// Register levels collection to pool (add and/or activate)
117// ***************************************************************************
118//
119inline void G4NavigationHistoryPool::
120Register(std::vector<G4NavigationLevel> * pLevels)
121{
122 fPool.push_back(pLevels);
123}
124
125// ***************************************************************************
126// Deactivate levels collection in pool
127// ***************************************************************************
128//
130DeRegister(std::vector<G4NavigationLevel> * pLevels)
131{
132 fFree.push_back(pLevels);
133}
134
135// ***************************************************************************
136// Return the pointer of a new collection of levels allocated
137// ***************************************************************************
138//
139inline std::vector<G4NavigationLevel> * G4NavigationHistoryPool::GetNewLevels()
140{
141 auto aLevelVec = new std::vector<G4NavigationLevel>(kHistoryMax);
142 Register(aLevelVec);
143
144 return aLevelVec;
145}
146
147// ***************************************************************************
148// Return the pointer of the first available collection of levels
149// If none are available (i.e. non active) allocate collection
150// ***************************************************************************
151//
152inline std::vector<G4NavigationLevel> * G4NavigationHistoryPool::GetLevels()
153{
154 std::vector<G4NavigationLevel> * levels = nullptr;
155
156 if (!fFree.empty())
157 {
158 levels = fFree.back();
159 fFree.pop_back();
160 }
161 else
162 {
163 levels = GetNewLevels();
164 }
165
166 return levels;
167}
168
169#endif
std::vector< G4NavigationLevel > * GetNewLevels()
static G4NavigationHistoryPool * GetInstance()
void DeRegister(std::vector< G4NavigationLevel > *pLevels)
std::vector< G4NavigationLevel > * GetLevels()
#define G4ThreadLocal
Definition tls.hh:77