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