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