Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 // G4LogicalVolumeStore implementation << 27 // 23 // 28 // 10.07.95, P.Kent, G.Cosmo << 24 // $Id: G4LogicalVolumeStore.cc,v 1.12 2003/11/02 14:01:23 gcosmo Exp $ >> 25 // GEANT4 tag $Name: geant4-06-00-patch-01 $ >> 26 // >> 27 // G4LogicalVolumeStore >> 28 // >> 29 // Implementation for singleton container >> 30 // >> 31 // History: >> 32 // 10.07.95 P.Kent Initial version 29 // ------------------------------------------- 33 // -------------------------------------------------------------------- 30 34 31 #include "G4Types.hh" 35 #include "G4Types.hh" 32 #include "G4LogicalVolumeStore.hh" 36 #include "G4LogicalVolumeStore.hh" 33 #include "G4GeometryManager.hh" 37 #include "G4GeometryManager.hh" 34 38 35 #include "G4AutoLock.hh" << 36 << 37 namespace << 38 { << 39 G4Mutex mapMutex = G4MUTEX_INITIALIZER; << 40 } << 41 << 42 // ******************************************* 39 // *************************************************************************** 43 // Static class variables 40 // Static class variables 44 // ******************************************* 41 // *************************************************************************** 45 // 42 // 46 G4LogicalVolumeStore* G4LogicalVolumeStore::fg << 43 G4LogicalVolumeStore* G4LogicalVolumeStore::fgInstance = 0; 47 G4ThreadLocal G4VStoreNotifier* G4LogicalVolum << 44 G4bool G4LogicalVolumeStore::locked = false; 48 G4ThreadLocal G4bool G4LogicalVolumeStore::loc << 49 45 50 // ******************************************* 46 // *************************************************************************** 51 // Protected constructor: Construct underlying 47 // Protected constructor: Construct underlying container with 52 // initial size of 100 entries 48 // initial size of 100 entries 53 // ******************************************* 49 // *************************************************************************** 54 // 50 // 55 G4LogicalVolumeStore::G4LogicalVolumeStore() 51 G4LogicalVolumeStore::G4LogicalVolumeStore() 56 << 52 : std::vector<G4LogicalVolume*>() 57 { 53 { 58 reserve(100); 54 reserve(100); 59 } 55 } 60 56 61 // ******************************************* 57 // *************************************************************************** 62 // Destructor 58 // Destructor 63 // ******************************************* 59 // *************************************************************************** 64 // 60 // 65 G4LogicalVolumeStore::~G4LogicalVolumeStore() 61 G4LogicalVolumeStore::~G4LogicalVolumeStore() 66 { 62 { 67 Clean(); // Delete all volumes in the store << 63 Clean(); 68 G4LogicalVolume::Clean(); // Delete allocat << 69 } 64 } 70 65 71 // ******************************************* 66 // *************************************************************************** 72 // Delete all elements from the store 67 // Delete all elements from the store 73 // ******************************************* 68 // *************************************************************************** 74 // 69 // 75 void G4LogicalVolumeStore::Clean() 70 void G4LogicalVolumeStore::Clean() 76 { 71 { 77 // Do nothing if geometry is closed 72 // Do nothing if geometry is closed 78 // 73 // 79 if (G4GeometryManager::GetInstance()->IsGeom 74 if (G4GeometryManager::GetInstance()->IsGeometryClosed()) 80 { 75 { 81 G4cout << "WARNING - Attempt to delete the 76 G4cout << "WARNING - Attempt to delete the logical volume store" 82 << " while geometry closed !" << G4 77 << " while geometry closed !" << G4endl; 83 return; 78 return; 84 } 79 } 85 80 86 // Locks store for deletion of volumes. De-r 81 // Locks store for deletion of volumes. De-registration will be 87 // performed at this stage. G4LogicalVolumes 82 // performed at this stage. G4LogicalVolumes will not de-register themselves. 88 // 83 // 89 locked = true; 84 locked = true; 90 85 >> 86 size_t i=0; 91 G4LogicalVolumeStore* store = GetInstance(); 87 G4LogicalVolumeStore* store = GetInstance(); >> 88 std::vector<G4LogicalVolume*>::iterator pos; >> 89 >> 90 #ifdef G4GEOMETRY_VOXELDEBUG >> 91 G4cout << "Deleting Logical Volumes ... "; >> 92 #endif 92 93 93 for(auto pos=store->cbegin(); pos!=store->ce << 94 for(pos=store->begin(); pos!=store->end(); pos++) 94 { 95 { 95 if (fgNotifier != nullptr) { fgNotifier->N << 96 if (*pos) delete *pos; i++; 96 if (*pos != nullptr) { (*pos)->Lock(); del << 97 } 97 } 98 98 99 store->bmap.clear(); store->mvalid = false; << 99 #ifdef G4GEOMETRY_VOXELDEBUG >> 100 if (store->size() < i-1) >> 101 { G4cout << "No volumes deleted. Already deleted by user ?" << G4endl; } >> 102 else >> 103 { G4cout << i-1 << " volumes deleted !" << G4endl; } >> 104 #endif >> 105 100 locked = false; 106 locked = false; 101 store->clear(); 107 store->clear(); 102 } 108 } 103 109 104 // ******************************************* 110 // *************************************************************************** 105 // Associate user notifier to the store << 106 // ******************************************* << 107 // << 108 void G4LogicalVolumeStore::SetNotifier(G4VStor << 109 { << 110 GetInstance(); << 111 fgNotifier = pNotifier; << 112 } << 113 << 114 // ******************************************* << 115 // Bring contents of internal map up to date a << 116 // ******************************************* << 117 // << 118 void G4LogicalVolumeStore::UpdateMap() << 119 { << 120 G4AutoLock l(&mapMutex); // to avoid thread << 121 if (mvalid) return; << 122 bmap.clear(); << 123 for(auto pos=GetInstance()->cbegin(); pos!=G << 124 { << 125 const G4String& vol_name = (*pos)->GetName << 126 auto it = bmap.find(vol_name); << 127 if (it != bmap.cend()) << 128 { << 129 it->second.push_back(*pos); << 130 } << 131 else << 132 { << 133 std::vector<G4LogicalVolume*> vol_vec { << 134 bmap.insert(std::make_pair(vol_name, vol << 135 } << 136 } << 137 mvalid = true; << 138 l.unlock(); << 139 } << 140 << 141 // ******************************************* << 142 // Add volume to container 111 // Add volume to container 143 // ******************************************* 112 // *************************************************************************** 144 // 113 // 145 void G4LogicalVolumeStore::Register(G4LogicalV 114 void G4LogicalVolumeStore::Register(G4LogicalVolume* pVolume) 146 { 115 { 147 G4LogicalVolumeStore* store = GetInstance(); << 116 GetInstance()->push_back(pVolume); 148 store->push_back(pVolume); << 149 const G4String& vol_name = pVolume->GetName( << 150 auto it = store->bmap.find(vol_name); << 151 if (it != store->bmap.cend()) << 152 { << 153 it->second.push_back(pVolume); << 154 } << 155 else << 156 { << 157 std::vector<G4LogicalVolume*> vol_vec { pV << 158 store->bmap.insert(std::make_pair(vol_name << 159 } << 160 if (fgNotifier != nullptr) { fgNotifier->Not << 161 store->mvalid = true; << 162 } 117 } 163 118 164 // ******************************************* 119 // *************************************************************************** 165 // Remove volume from container 120 // Remove volume from container 166 // ******************************************* 121 // *************************************************************************** 167 // 122 // 168 void G4LogicalVolumeStore::DeRegister(G4Logica 123 void G4LogicalVolumeStore::DeRegister(G4LogicalVolume* pVolume) 169 { 124 { 170 G4LogicalVolumeStore* store = GetInstance(); << 171 if (!locked) // Do not de-register if loc 125 if (!locked) // Do not de-register if locked ! 172 { 126 { 173 if (fgNotifier != nullptr) { fgNotifier->N << 127 for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++) 174 for (auto i=store->cbegin(); i!=store->cen << 175 { 128 { 176 if (**i==*pVolume) 129 if (**i==*pVolume) 177 { 130 { 178 store->erase(i); << 131 GetInstance()->erase(i); 179 break; 132 break; 180 } 133 } 181 } 134 } 182 const G4String& vol_name = pVolume->GetNam << 183 auto it = store->bmap.find(vol_name); << 184 if (it != store->bmap.cend()) << 185 { << 186 if (it->second.size() > 1) << 187 { << 188 for (auto i=it->second.cbegin(); i!=it << 189 { << 190 if (**i==*pVolume) << 191 { << 192 it->second.erase(i); << 193 break; << 194 } << 195 } << 196 } << 197 else << 198 { << 199 store->bmap.erase(it); << 200 } << 201 } << 202 } << 203 } << 204 << 205 // ******************************************* << 206 // Retrieve the first or last volume pointer i << 207 // ******************************************* << 208 // << 209 G4LogicalVolume* << 210 G4LogicalVolumeStore::GetVolume(const G4String << 211 G4bool reverse << 212 { << 213 G4LogicalVolumeStore* store = GetInstance(); << 214 if (!store->mvalid) { store->UpdateMap(); } << 215 auto pos = store->bmap.find(name); << 216 if(pos != store->bmap.cend()) << 217 { << 218 if ((verbose) && (pos->second.size()>1)) << 219 { << 220 std::ostringstream message; << 221 message << "There exists more than ONE l << 222 << name << "!" << G4endl << 223 << "Returning the first found."; << 224 G4Exception("G4LogicalVolumeStore::GetVo << 225 "GeomMgt1001", JustWarning, << 226 } << 227 if(reverseSearch) << 228 { << 229 return pos->second[pos->second.size()-1] << 230 } << 231 else << 232 { << 233 return pos->second[0]; << 234 } << 235 } << 236 if (verbose) << 237 { << 238 std::ostringstream message; << 239 message << "Volume NOT found in store !" << 240 << " Volume " << name << " << 241 << " Returning NULL pointe << 242 G4Exception("G4LogicalVolumeStore::GetVol << 243 "GeomMgt1001", JustWarning, m << 244 } 135 } 245 return nullptr; << 246 } 136 } 247 137 248 // ******************************************* 138 // *************************************************************************** 249 // Return ptr to Store, setting if necessary 139 // Return ptr to Store, setting if necessary 250 // ******************************************* 140 // *************************************************************************** 251 // 141 // 252 G4LogicalVolumeStore* G4LogicalVolumeStore::Ge 142 G4LogicalVolumeStore* G4LogicalVolumeStore::GetInstance() 253 { 143 { 254 static G4LogicalVolumeStore worldStore; 144 static G4LogicalVolumeStore worldStore; 255 if (fgInstance == nullptr) << 145 if (!fgInstance) 256 { 146 { 257 fgInstance = &worldStore; 147 fgInstance = &worldStore; 258 } 148 } 259 return fgInstance; 149 return fgInstance; 260 } 150 } 261 151