Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 //
27 // $Id:$
28 //
29 // class G4NavigationHistoryPool
30 //
31 // Class description:
32 //
33 // Thread-local pool for navigation history levels collections being
34 // allocated by G4NavigationHistory. Allows for reuse of the vectors
35 // allocated according to lifetime of G4NavigationHistory objects.
36 
37 // History:
38 // 07.05.14 G.Cosmo Initial version
39 // --------------------------------------------------------------------
40 #ifndef G4NAVIGATIONHISTORYPOOL_HH
41 #define G4NAVIGATIONHISTORYPOOL_HH
42 
43 #include <vector>
44 
45 #include "G4NavigationLevel.hh"
46 
48 {
49  public: // with description
50 
52  // Return unique instance of G4NavigationHistoryPool.
53 
54  inline std::vector<G4NavigationLevel> * GetNewLevels();
55  // Return the pointer to a new collection of levels being allocated.
56 
57  inline std::vector<G4NavigationLevel> * GetLevels();
58  // Return the pointer of the first available collection of levels
59  // If none are available (i.e. empty Free vector) allocate collection.
60 
61  inline void DeRegister(std::vector<G4NavigationLevel> * pLevels);
62  // Deactivate levels collection in pool.
63 
64  void Clean();
65  // Delete all levels stored in the pool.
66 
67  void Print() const;
68  // Print number of entries.
69 
71  // Destructor: takes care to delete allocated levels.
72 
73  private:
74 
76  // Default constructor.
77 
78  inline void Register(std::vector<G4NavigationLevel> * pLevels);
79  // Register levels collection to pool and activate it.
80 
81  void Reset();
82  // Set internal vectors content to zero.
83 
84  private:
85 
86  static G4ThreadLocal G4NavigationHistoryPool* fgInstance;
87 
88  std::vector<std::vector<G4NavigationLevel> *> fPool;
89  std::vector<std::vector<G4NavigationLevel> *> fFree;
90 };
91 
92 // ***************************************************************************
93 // Register levels collection to pool (add and/or activate)
94 // ***************************************************************************
95 //
96 inline void G4NavigationHistoryPool::
97 Register(std::vector<G4NavigationLevel> * pLevels)
98 {
99  fPool.push_back(pLevels);
100 }
101 
102 // ***************************************************************************
103 // Deactivate levels collection in pool
104 // ***************************************************************************
105 //
106 inline void G4NavigationHistoryPool::
107 DeRegister(std::vector<G4NavigationLevel> * pLevels)
108 {
109  fFree.push_back(pLevels);
110 }
111 
112 // ***************************************************************************
113 // Return the pointer of a new collection of levels allocated
114 // ***************************************************************************
115 //
116 inline std::vector<G4NavigationLevel> * G4NavigationHistoryPool::GetNewLevels()
117 {
118  std::vector<G4NavigationLevel> * aLevelVec =
119  new std::vector<G4NavigationLevel>(kHistoryMax);
120  Register(aLevelVec);
121 
122  return aLevelVec;
123 }
124 
125 // ***************************************************************************
126 // Return the pointer of the first available collection of levels
127 // If none are available (i.e. non active) allocate collection
128 // ***************************************************************************
129 //
130 inline std::vector<G4NavigationLevel> * G4NavigationHistoryPool::GetLevels()
131 {
132  std::vector<G4NavigationLevel> * levels = 0;
133 
134  if (fFree.size() !=0)
135  {
136  levels = fFree.back();
137  fFree.pop_back();
138  }
139  else
140  {
141  levels = GetNewLevels();
142  }
143 
144  return levels;
145 }
146 
147 #endif
static const G4int kHistoryMax
Definition: geomdefs.hh:73
std::vector< G4NavigationLevel > * GetNewLevels()
#define G4ThreadLocal
Definition: tls.hh:89
void DeRegister(std::vector< G4NavigationLevel > *pLevels)
std::vector< G4NavigationLevel > * GetLevels()
static G4NavigationHistoryPool * GetInstance()