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 << 27 // 26 // 28 // 10.07.95, P.Kent, G.Cosmo << 27 // $Id$ >> 28 // >> 29 // G4SolidStore >> 30 // >> 31 // Implementation for singleton container >> 32 // >> 33 // History: >> 34 // 10.07.95 P.Kent Initial version 29 // ------------------------------------------- 35 // -------------------------------------------------------------------- 30 36 31 #include "globals.hh" 37 #include "globals.hh" 32 #include "G4SolidStore.hh" 38 #include "G4SolidStore.hh" 33 #include "G4GeometryManager.hh" 39 #include "G4GeometryManager.hh" 34 40 35 #include "G4AutoLock.hh" << 36 << 37 namespace << 38 { << 39 G4Mutex mapMutex = G4MUTEX_INITIALIZER; << 40 } << 41 << 42 // ******************************************* 41 // *************************************************************************** 43 // Static class variables 42 // Static class variables 44 // ******************************************* 43 // *************************************************************************** 45 // 44 // 46 G4SolidStore* G4SolidStore::fgInstance = nullp << 45 G4SolidStore* G4SolidStore::fgInstance = 0; 47 G4ThreadLocal G4VStoreNotifier* G4SolidStore:: << 46 G4VStoreNotifier* G4SolidStore::fgNotifier = 0; 48 G4ThreadLocal G4bool G4SolidStore::locked = fa << 47 G4bool G4SolidStore::locked = false; 49 48 50 // ******************************************* 49 // *************************************************************************** 51 // Protected constructor: Construct underlying 50 // Protected constructor: Construct underlying container with 52 // initial size of 100 entries 51 // initial size of 100 entries 53 // ******************************************* 52 // *************************************************************************** 54 // 53 // 55 G4SolidStore::G4SolidStore() 54 G4SolidStore::G4SolidStore() 56 << 55 : std::vector<G4VSolid*>() 57 { 56 { 58 reserve(100); 57 reserve(100); 59 } 58 } 60 59 61 // ******************************************* 60 // *************************************************************************** 62 // Destructor 61 // Destructor 63 // ******************************************* 62 // *************************************************************************** 64 // 63 // 65 G4SolidStore::~G4SolidStore() 64 G4SolidStore::~G4SolidStore() 66 { 65 { 67 Clean(); 66 Clean(); 68 } 67 } 69 68 70 // ******************************************* 69 // *************************************************************************** 71 // Delete all elements from the store 70 // Delete all elements from the store 72 // ******************************************* 71 // *************************************************************************** 73 // 72 // 74 void G4SolidStore::Clean() 73 void G4SolidStore::Clean() 75 { 74 { 76 // Do nothing if geometry is closed 75 // Do nothing if geometry is closed 77 // 76 // 78 if (G4GeometryManager::GetInstance()->IsGeom 77 if (G4GeometryManager::GetInstance()->IsGeometryClosed()) 79 { 78 { 80 G4cout << "WARNING - Attempt to delete the 79 G4cout << "WARNING - Attempt to delete the solid store" 81 << " while geometry closed !" << G4 80 << " while geometry closed !" << G4endl; 82 return; 81 return; 83 } 82 } 84 83 85 // Locks store for deletion of solids. De-re 84 // Locks store for deletion of solids. De-registration will be 86 // performed at this stage. G4VSolids will n 85 // performed at this stage. G4VSolids will not de-register themselves. 87 // 86 // 88 locked = true; 87 locked = true; 89 88 >> 89 size_t i=0; 90 G4SolidStore* store = GetInstance(); 90 G4SolidStore* store = GetInstance(); 91 91 92 for(auto pos=store->cbegin(); pos!=store->ce << 92 #ifdef G4GEOMETRY_VOXELDEBUG >> 93 G4cout << "Deleting Solids ... "; >> 94 #endif >> 95 >> 96 for(iterator pos=store->begin(); pos!=store->end(); pos++) 93 { 97 { 94 if (fgNotifier != nullptr) { fgNotifier->N << 98 if (fgNotifier) { fgNotifier->NotifyDeRegistration(); } 95 delete *pos; << 99 if (*pos) { delete *pos; } >> 100 i++; 96 } 101 } 97 102 98 store->bmap.clear(); store->mvalid = false; << 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 99 locked = false; 110 locked = false; 100 store->clear(); 111 store->clear(); 101 } 112 } 102 113 103 // ******************************************* 114 // *************************************************************************** 104 // Associate user notifier to the store 115 // Associate user notifier to the store 105 // ******************************************* 116 // *************************************************************************** 106 // 117 // 107 void G4SolidStore::SetNotifier(G4VStoreNotifie 118 void G4SolidStore::SetNotifier(G4VStoreNotifier* pNotifier) 108 { 119 { 109 GetInstance(); 120 GetInstance(); 110 fgNotifier = pNotifier; 121 fgNotifier = pNotifier; 111 } 122 } 112 123 113 // ******************************************* 124 // *************************************************************************** 114 // Bring contents of internal map up to date a << 115 // ******************************************* << 116 // << 117 void G4SolidStore::UpdateMap() << 118 { << 119 G4AutoLock l(&mapMutex); // to avoid thread << 120 if (mvalid) return; << 121 bmap.clear(); << 122 for(auto pos=GetInstance()->cbegin(); pos!=G << 123 { << 124 const G4String& sol_name = (*pos)->GetName << 125 auto it = bmap.find(sol_name); << 126 if (it != bmap.cend()) << 127 { << 128 it->second.push_back(*pos); << 129 } << 130 else << 131 { << 132 std::vector<G4VSolid*> sol_vec { *pos }; << 133 bmap.insert(std::make_pair(sol_name, sol << 134 } << 135 } << 136 mvalid = true; << 137 l.unlock(); << 138 } << 139 << 140 // ******************************************* << 141 // Add Solid to container 125 // Add Solid to container 142 // ******************************************* 126 // *************************************************************************** 143 // 127 // 144 void G4SolidStore::Register(G4VSolid* pSolid) 128 void G4SolidStore::Register(G4VSolid* pSolid) 145 { 129 { 146 G4SolidStore* store = GetInstance(); << 130 GetInstance()->push_back(pSolid); 147 store->push_back(pSolid); << 131 if (fgNotifier) { fgNotifier->NotifyRegistration(); } 148 const G4String& sol_name = pSolid->GetName() << 149 auto it = store->bmap.find(sol_name); << 150 if (it != store->bmap.cend()) << 151 { << 152 it->second.push_back(pSolid); << 153 } << 154 else << 155 { << 156 std::vector<G4VSolid*> sol_vec { pSolid }; << 157 store->bmap.insert(std::make_pair(sol_name << 158 } << 159 if (fgNotifier != nullptr) { fgNotifier->Not << 160 store->mvalid = true; << 161 } 132 } 162 133 163 // ******************************************* 134 // *************************************************************************** 164 // Remove Solid from container 135 // Remove Solid from container 165 // ******************************************* 136 // *************************************************************************** 166 // 137 // 167 void G4SolidStore::DeRegister(G4VSolid* pSolid 138 void G4SolidStore::DeRegister(G4VSolid* pSolid) 168 { 139 { 169 G4SolidStore* store = GetInstance(); << 170 if (!locked) // Do not de-register if loc 140 if (!locked) // Do not de-register if locked ! 171 { 141 { 172 if (fgNotifier != nullptr) { fgNotifier->N << 142 if (fgNotifier) { fgNotifier->NotifyDeRegistration(); } 173 for (auto i=store->crbegin(); i!=store->cr << 143 for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++) 174 { 144 { 175 if (**i==*pSolid) 145 if (**i==*pSolid) 176 { 146 { 177 store->erase(std::next(i).base()); << 147 GetInstance()->erase(i); 178 store->mvalid = false; << 179 break; 148 break; 180 } 149 } 181 } 150 } 182 const G4String& sol_name = pSolid->GetName << 183 auto it = store->bmap.find(sol_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==*pSolid) << 191 { << 192 it->second.erase(i); << 193 break; << 194 } << 195 } << 196 } << 197 else << 198 { << 199 store->bmap.erase(it); << 200 } << 201 } << 202 } 151 } 203 } 152 } 204 153 205 // ******************************************* 154 // *************************************************************************** 206 // Retrieve the first or last solid pointer in << 155 // Retrieve the first solid pointer in the container having that name 207 // ******************************************* 156 // *************************************************************************** 208 // 157 // 209 G4VSolid* G4SolidStore::GetSolid(const G4Strin << 158 G4VSolid* G4SolidStore::GetSolid(const G4String& name, G4bool verbose) const 210 G4bool revers << 211 { 159 { 212 G4SolidStore* store = GetInstance(); << 160 for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++) 213 if (!store->mvalid) { store->UpdateMap(); } << 214 auto pos = store->bmap.find(name); << 215 if(pos != store->bmap.cend()) << 216 { 161 { 217 if ((verbose) && (pos->second.size()>1)) << 162 if ((*i)->GetName() == name) { return *i; } 218 { << 219 std::ostringstream message; << 220 message << "There exists more than ONE s << 221 << name << "!" << G4endl << 222 << "Returning the first found."; << 223 G4Exception("G4SolidStore::GetSolid()", << 224 "GeomMgt1001", JustWarning, << 225 } << 226 if(reverseSearch) << 227 { << 228 return pos->second[pos->second.size()-1] << 229 } << 230 else << 231 { << 232 return pos->second[0]; << 233 } << 234 } 163 } 235 if (verbose) 164 if (verbose) 236 { 165 { 237 std::ostringstream message; << 166 std::ostringstream message; 238 message << "Solid " << name << " not found << 167 message << "Solid " << name << " not found in store !" << G4endl 239 << "Returning NULL pointer."; << 168 << "Returning NULL pointer."; 240 G4Exception("G4SolidStore::GetSolid()", << 169 G4Exception("G4SolidStore::GetSolid()", 241 "GeomMgt1001", JustWarning, me << 170 "GeomMgt1001", JustWarning, message); 242 } 171 } 243 return nullptr; << 172 return 0; 244 } 173 } 245 174 246 // ******************************************* 175 // *************************************************************************** 247 // Return ptr to Store, setting if necessary 176 // Return ptr to Store, setting if necessary 248 // ******************************************* 177 // *************************************************************************** 249 // 178 // 250 G4SolidStore* G4SolidStore::GetInstance() 179 G4SolidStore* G4SolidStore::GetInstance() 251 { 180 { 252 static G4SolidStore worldStore; 181 static G4SolidStore worldStore; 253 if (fgInstance == nullptr) << 182 if (!fgInstance) 254 { 183 { 255 fgInstance = &worldStore; 184 fgInstance = &worldStore; 256 } 185 } 257 return fgInstance; 186 return fgInstance; 258 } 187 } 259 188