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: G4PhysicalVolumeStore.cc,v 1.20 2008/07/10 09:41:20 gcosmo Exp $ |
---|
28 | // GEANT4 tag $Name: HEAD $ |
---|
29 | // |
---|
30 | // G4PhysicalVolumeStore |
---|
31 | // |
---|
32 | // Implementation for singleton container |
---|
33 | // |
---|
34 | // History: |
---|
35 | // 25.07.95 P.Kent Initial version |
---|
36 | // -------------------------------------------------------------------- |
---|
37 | |
---|
38 | #include "G4Types.hh" |
---|
39 | #include "G4PhysicalVolumeStore.hh" |
---|
40 | #include "G4GeometryManager.hh" |
---|
41 | #include "G4LogicalVolume.hh" |
---|
42 | |
---|
43 | // *************************************************************************** |
---|
44 | // Static class variables |
---|
45 | // *************************************************************************** |
---|
46 | // |
---|
47 | G4PhysicalVolumeStore* G4PhysicalVolumeStore::fgInstance = 0; |
---|
48 | G4VStoreNotifier* G4PhysicalVolumeStore::fgNotifier = 0; |
---|
49 | G4bool G4PhysicalVolumeStore::locked = false; |
---|
50 | |
---|
51 | // *************************************************************************** |
---|
52 | // Protected constructor: Construct underlying container with |
---|
53 | // initial size of 100 entries |
---|
54 | // *************************************************************************** |
---|
55 | // |
---|
56 | G4PhysicalVolumeStore::G4PhysicalVolumeStore() |
---|
57 | : std::vector<G4VPhysicalVolume*>() |
---|
58 | { |
---|
59 | reserve(100); |
---|
60 | } |
---|
61 | |
---|
62 | // *************************************************************************** |
---|
63 | // Destructor |
---|
64 | // *************************************************************************** |
---|
65 | // |
---|
66 | G4PhysicalVolumeStore::~G4PhysicalVolumeStore() |
---|
67 | { |
---|
68 | Clean(); |
---|
69 | } |
---|
70 | |
---|
71 | // *************************************************************************** |
---|
72 | // Delete all elements from the store |
---|
73 | // *************************************************************************** |
---|
74 | // |
---|
75 | void G4PhysicalVolumeStore::Clean() |
---|
76 | { |
---|
77 | // Do nothing if geometry is closed |
---|
78 | // |
---|
79 | if (G4GeometryManager::GetInstance()->IsGeometryClosed()) |
---|
80 | { |
---|
81 | G4cout << "WARNING - Attempt to delete the physical volume store" |
---|
82 | << " while geometry closed !" << G4endl; |
---|
83 | return; |
---|
84 | } |
---|
85 | |
---|
86 | // Locks store for deletion of volumes. De-registration will be |
---|
87 | // performed at this stage. G4VPhysicalVolumes will not de-register |
---|
88 | // themselves. |
---|
89 | // |
---|
90 | locked = true; |
---|
91 | |
---|
92 | size_t i=0; |
---|
93 | G4PhysicalVolumeStore* store = GetInstance(); |
---|
94 | std::vector<G4VPhysicalVolume*>::iterator pos; |
---|
95 | |
---|
96 | #ifdef G4GEOMETRY_VOXELDEBUG |
---|
97 | G4cout << "Deleting Physical Volumes ... "; |
---|
98 | #endif |
---|
99 | |
---|
100 | for(pos=store->begin(); pos!=store->end(); pos++) |
---|
101 | { |
---|
102 | if (fgNotifier) { fgNotifier->NotifyDeRegistration(); } |
---|
103 | if (*pos) { delete *pos; } |
---|
104 | i++; |
---|
105 | } |
---|
106 | |
---|
107 | #ifdef G4GEOMETRY_VOXELDEBUG |
---|
108 | if (store->size() < i-1) |
---|
109 | { G4cout << "No volumes deleted. Already deleted by user ?" << G4endl; } |
---|
110 | else |
---|
111 | { G4cout << i-1 << " volumes deleted !" << G4endl; } |
---|
112 | #endif |
---|
113 | |
---|
114 | locked = false; |
---|
115 | store->clear(); |
---|
116 | } |
---|
117 | |
---|
118 | // *************************************************************************** |
---|
119 | // Associate user notifier to the store |
---|
120 | // *************************************************************************** |
---|
121 | // |
---|
122 | void G4PhysicalVolumeStore::SetNotifier(G4VStoreNotifier* pNotifier) |
---|
123 | { |
---|
124 | GetInstance(); |
---|
125 | fgNotifier = pNotifier; |
---|
126 | } |
---|
127 | |
---|
128 | // *************************************************************************** |
---|
129 | // Add Volume to container |
---|
130 | // *************************************************************************** |
---|
131 | // |
---|
132 | void G4PhysicalVolumeStore::Register(G4VPhysicalVolume* pVolume) |
---|
133 | { |
---|
134 | GetInstance()->push_back(pVolume); |
---|
135 | if (fgNotifier) { fgNotifier->NotifyRegistration(); } |
---|
136 | } |
---|
137 | |
---|
138 | // *************************************************************************** |
---|
139 | // Remove Volume from container and update the list of daughters |
---|
140 | // of the mother's logical volume |
---|
141 | // *************************************************************************** |
---|
142 | // |
---|
143 | void G4PhysicalVolumeStore::DeRegister(G4VPhysicalVolume* pVolume) |
---|
144 | { |
---|
145 | if (!locked) // Do not de-register if locked ! |
---|
146 | { |
---|
147 | if (fgNotifier) { fgNotifier->NotifyDeRegistration(); } |
---|
148 | G4LogicalVolume* motherLogical = pVolume->GetMotherLogical(); |
---|
149 | if (motherLogical) { motherLogical->RemoveDaughter(pVolume); } |
---|
150 | for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++) |
---|
151 | { |
---|
152 | if (**i==*pVolume) |
---|
153 | { |
---|
154 | GetInstance()->erase(i); |
---|
155 | break; |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | // *************************************************************************** |
---|
162 | // Retrieve the first volume pointer in the container having that name |
---|
163 | // *************************************************************************** |
---|
164 | // |
---|
165 | G4VPhysicalVolume* |
---|
166 | G4PhysicalVolumeStore::GetVolume(const G4String& name, G4bool verbose) const |
---|
167 | { |
---|
168 | for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++) |
---|
169 | { |
---|
170 | if ((*i)->GetName() == name) { return *i; } |
---|
171 | } |
---|
172 | if (verbose) |
---|
173 | { |
---|
174 | G4cerr << "ERROR - G4PhysicalVolumeStore::GetVolume()" << G4endl |
---|
175 | << " Volume " << name << " NOT found in store !" << G4endl |
---|
176 | << " Returning NULL pointer." << G4endl; |
---|
177 | G4Exception("G4PhysicalVolumeStore::GetVolume()", "InvalidQuery", |
---|
178 | JustWarning, "Volume NOT found in store !"); |
---|
179 | } |
---|
180 | return 0; |
---|
181 | } |
---|
182 | |
---|
183 | // *************************************************************************** |
---|
184 | // Return ptr to Store, setting if necessary |
---|
185 | // *************************************************************************** |
---|
186 | // |
---|
187 | G4PhysicalVolumeStore* G4PhysicalVolumeStore::GetInstance() |
---|
188 | { |
---|
189 | static G4PhysicalVolumeStore worldStore; |
---|
190 | if (!fgInstance) |
---|
191 | { |
---|
192 | fgInstance = &worldStore; |
---|
193 | } |
---|
194 | return fgInstance; |
---|
195 | } |
---|