ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4LogicalVolumeStore.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4LogicalVolumeStore.cc
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"
32 #include "G4LogicalVolumeStore.hh"
33 #include "G4GeometryManager.hh"
34 
35 // ***************************************************************************
36 // Static class variables
37 // ***************************************************************************
38 //
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);
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 }