Geant4-11
G4LogicalVolumeStore.cc
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// G4LogicalVolumeStore implementation
27//
28// 10.07.95, P.Kent, G.Cosmo
29// --------------------------------------------------------------------
30
31#include "G4Types.hh"
33#include "G4GeometryManager.hh"
34
35#include "G4AutoLock.hh"
36
37namespace
38{
40}
41
42// ***************************************************************************
43// Static class variables
44// ***************************************************************************
45//
49
50// ***************************************************************************
51// Protected constructor: Construct underlying container with
52// initial size of 100 entries
53// ***************************************************************************
54//
56 : std::vector<G4LogicalVolume*>()
57{
58 reserve(100);
59}
60
61// ***************************************************************************
62// Destructor
63// ***************************************************************************
64//
66{
67 Clean(); // Delete all volumes in the store
68 G4LogicalVolume::Clean(); // Delete allocated sub-instance data
69}
70
71// ***************************************************************************
72// Delete all elements from the store
73// ***************************************************************************
74//
76{
77 // Do nothing if geometry is closed
78 //
80 {
81 G4cout << "WARNING - Attempt to delete the logical 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. G4LogicalVolumes will not de-register themselves.
88 //
89 locked = true;
90
91 std::size_t i = 0;
93
94#ifdef G4GEOMETRY_VOXELDEBUG
95 G4cout << "Deleting Logical Volumes ... ";
96#endif
97
98 for(auto pos=store->cbegin(); pos!=store->cend(); ++pos)
99 {
100 if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); }
101 if (*pos != nullptr) { (*pos)->Lock(); delete *pos; }
102 ++i;
103 }
104
105#ifdef G4GEOMETRY_VOXELDEBUG
106 if (store->size() < i-1)
107 { G4cout << "No volumes deleted. Already deleted by user ?" << G4endl; }
108 else
109 { G4cout << i-1 << " volumes deleted !" << G4endl; }
110#endif
111
112 store->bmap.clear(); store->mvalid = false;
113 locked = false;
114 store->clear();
115}
116
117// ***************************************************************************
118// Associate user notifier to the store
119// ***************************************************************************
120//
122{
123 GetInstance();
124 fgNotifier = pNotifier;
125}
126
127// ***************************************************************************
128// Bring contents of internal map up to date and reset validity flag
129// ***************************************************************************
130//
132{
133 G4AutoLock l(&mapMutex); // to avoid thread contention at initialisation
134 if (mvalid) return;
135 bmap.clear();
136 for(auto pos=GetInstance()->cbegin(); pos!=GetInstance()->cend(); ++pos)
137 {
138 const G4String& vol_name = (*pos)->GetName();
139 auto it = bmap.find(vol_name);
140 if (it != bmap.cend())
141 {
142 it->second.push_back(*pos);
143 }
144 else
145 {
146 std::vector<G4LogicalVolume*> vol_vec { *pos };
147 bmap.insert(std::make_pair(vol_name, vol_vec));
148 }
149 }
150 mvalid = true;
151 l.unlock();
152}
153
154// ***************************************************************************
155// Add volume to container
156// ***************************************************************************
157//
159{
161 store->push_back(pVolume);
162 const G4String& vol_name = pVolume->GetName();
163 auto it = store->bmap.find(vol_name);
164 if (it != store->bmap.cend())
165 {
166 it->second.push_back(pVolume);
167 }
168 else
169 {
170 std::vector<G4LogicalVolume*> vol_vec { pVolume };
171 store->bmap.insert(std::make_pair(vol_name, vol_vec));
172 }
174 store->mvalid = true;
175}
176
177// ***************************************************************************
178// Remove volume from container
179// ***************************************************************************
180//
182{
184 if (!locked) // Do not de-register if locked !
185 {
186 if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); }
187 for (auto i=store->cbegin(); i!=store->cend(); ++i)
188 {
189 if (**i==*pVolume)
190 {
191 store->erase(i);
192 break;
193 }
194 }
195 const G4String& vol_name = pVolume->GetName();
196 auto it = store->bmap.find(vol_name);
197 if (it != store->bmap.cend())
198 {
199 if (it->second.size() > 1)
200 {
201 for (auto i=it->second.cbegin(); i!=it->second.cend(); ++i)
202 {
203 if (**i==*pVolume)
204 {
205 it->second.erase(i);
206 break;
207 }
208 }
209 }
210 else
211 {
212 store->bmap.erase(it);
213 }
214 }
215 }
216}
217
218// ***************************************************************************
219// Retrieve the first or last volume pointer in the container having that name
220// ***************************************************************************
221//
224 G4bool reverseSearch) const
225{
227 if (!store->mvalid) { store->UpdateMap(); }
228 auto pos = store->bmap.find(name);
229 if(pos != store->bmap.cend())
230 {
231 if ((verbose) && (pos->second.size()>1))
232 {
233 std::ostringstream message;
234 message << "There exists more than ONE logical volume in store named: "
235 << name << "!" << G4endl
236 << "Returning the first found.";
237 G4Exception("G4LogicalVolumeStore::GetVolume()",
238 "GeomMgt1001", JustWarning, message);
239 }
240 if(reverseSearch)
241 {
242 return pos->second[pos->second.size()-1];
243 }
244 else
245 {
246 return pos->second[0];
247 }
248 }
249 if (verbose)
250 {
251 std::ostringstream message;
252 message << "Volume NOT found in store !" << G4endl
253 << " Volume " << name << " NOT found in store !" << G4endl
254 << " Returning NULL pointer.";
255 G4Exception("G4LogicalVolumeStore::GetVolume()",
256 "GeomMgt1001", JustWarning, message);
257 }
258 return nullptr;
259}
260
261// ***************************************************************************
262// Return ptr to Store, setting if necessary
263// ***************************************************************************
264//
266{
267 static G4LogicalVolumeStore worldStore;
268 if (fgInstance == nullptr)
269 {
270 fgInstance = &worldStore;
271 }
272 return fgInstance;
273}
static const G4double pos
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:85
std::mutex G4Mutex
Definition: G4Threading.hh:81
bool G4bool
Definition: G4Types.hh:86
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
static G4bool IsGeometryClosed()
static G4ThreadLocal G4bool locked
static G4ThreadLocal G4VStoreNotifier * fgNotifier
static void DeRegister(G4LogicalVolume *pVolume)
static G4LogicalVolumeStore * fgInstance
G4LogicalVolume * GetVolume(const G4String &name, G4bool verbose=true, G4bool reverseSearch=false) const
static void Register(G4LogicalVolume *pVolume)
static G4LogicalVolumeStore * GetInstance()
static void SetNotifier(G4VStoreNotifier *pNotifier)
std::map< G4String, std::vector< G4LogicalVolume * > > bmap
static void Clean()
const G4String & GetName() const
virtual void NotifyRegistration()=0
virtual void NotifyDeRegistration()=0
const char * name(G4int ptype)
#define G4ThreadLocal
Definition: tls.hh:77