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