Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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 for singleton container
27//
28// 10.07.95 - P.Kent, Initial version
29// --------------------------------------------------------------------
30
31#include "G4Types.hh"
33#include "G4GeometryManager.hh"
34
35// ***************************************************************************
36// Static class variables
37// ***************************************************************************
38//
39G4LogicalVolumeStore* G4LogicalVolumeStore::fgInstance = nullptr;
40G4ThreadLocal G4VStoreNotifier* G4LogicalVolumeStore::fgNotifier = nullptr;
41G4ThreadLocal G4bool G4LogicalVolumeStore::locked = false;
42
43// ***************************************************************************
44// Protected constructor: Construct underlying container with
45// initial size of 100 entries
46// ***************************************************************************
47//
49 : std::vector<G4LogicalVolume*>()
50{
51 reserve(100);
52}
53
54// ***************************************************************************
55// Destructor
56// ***************************************************************************
57//
59{
60 Clean(); // Delete all volumes in the store
61 G4LogicalVolume::Clean(); // Delete allocated sub-instance data
62}
63
64// ***************************************************************************
65// Delete all elements from the store
66// ***************************************************************************
67//
69{
70 // Do nothing if geometry is closed
71 //
73 {
74 G4cout << "WARNING - Attempt to delete the logical volume store"
75 << " while geometry closed !" << G4endl;
76 return;
77 }
78
79 // Locks store for deletion of volumes. De-registration will be
80 // performed at this stage. G4LogicalVolumes will not de-register themselves.
81 //
82 locked = true;
83
84 size_t i = 0;
86
87#ifdef G4GEOMETRY_VOXELDEBUG
88 G4cout << "Deleting Logical Volumes ... ";
89#endif
90
91 for(auto pos=store->cbegin(); pos!=store->cend(); ++pos)
92 {
93 if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); }
94 if (*pos != nullptr) { (*pos)->Lock(); delete *pos; }
95 ++i;
96 }
97
98#ifdef G4GEOMETRY_VOXELDEBUG
99 if (store->size() < i-1)
100 { G4cout << "No volumes deleted. Already deleted by user ?" << G4endl; }
101 else
102 { G4cout << i-1 << " volumes deleted !" << G4endl; }
103#endif
104
105 locked = false;
106 store->clear();
107}
108
109// ***************************************************************************
110// Associate user notifier to the store
111// ***************************************************************************
112//
114{
115 GetInstance();
116 fgNotifier = pNotifier;
117}
118
119// ***************************************************************************
120// Add volume to container
121// ***************************************************************************
122//
124{
125 GetInstance()->push_back(pVolume);
126 if (fgNotifier) { fgNotifier->NotifyRegistration(); }
127}
128
129// ***************************************************************************
130// Remove volume from container
131// ***************************************************************************
132//
134{
135 if (!locked) // Do not de-register if locked !
136 {
137 if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); }
138 for (auto i=GetInstance()->cbegin(); i!=GetInstance()->cend(); ++i)
139 {
140 if (**i==*pVolume)
141 {
142 GetInstance()->erase(i);
143 break;
144 }
145 }
146 }
147}
148
149// ***************************************************************************
150// Retrieve the first volume pointer in the container having that name
151// ***************************************************************************
152//
155{
156 for (auto i=GetInstance()->cbegin(); i!=GetInstance()->cend(); ++i)
157 {
158 if ((*i)->GetName() == name) { return *i; }
159 }
160 if (verbose)
161 {
162 std::ostringstream message;
163 message << "Volume NOT found in store !" << G4endl
164 << " Volume " << name << " NOT found in store !" << G4endl
165 << " Returning NULL pointer.";
166 G4Exception("G4LogicalVolumeStore::GetVolume()",
167 "GeomMgt1001", JustWarning, message);
168 }
169 return 0;
170}
171
172// ***************************************************************************
173// Return ptr to Store, setting if necessary
174// ***************************************************************************
175//
177{
178 static G4LogicalVolumeStore worldStore;
179 if (fgInstance == nullptr)
180 {
181 fgInstance = &worldStore;
182 }
183 return fgInstance;
184}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
bool G4bool
Definition: G4Types.hh:86
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
static G4bool IsGeometryClosed()
G4LogicalVolume * GetVolume(const G4String &name, G4bool verbose=true) const
static void DeRegister(G4LogicalVolume *pVolume)
static void Register(G4LogicalVolume *pVolume)
static G4LogicalVolumeStore * GetInstance()
static void SetNotifier(G4VStoreNotifier *pNotifier)
static void Clean()
virtual void NotifyRegistration()=0
virtual void NotifyDeRegistration()=0
#define G4ThreadLocal
Definition: tls.hh:77