Geant4 Cross Reference |
>> 1 // This code implementation is the intellectual property of >> 2 // the GEANT4 collaboration. 1 // 3 // 2 // ******************************************* << 4 // By copying, distributing or modifying the Program (or any work 3 // * License and Disclaimer << 5 // based on the Program) you indicate your acceptance of this statement, 4 // * << 6 // and all its terms. 5 // * The Geant4 software is copyright of th << 6 // * the Geant4 Collaboration. It is provided << 7 // * conditions of the Geant4 Software License << 8 // * LICENSE and available at http://cern.ch/ << 9 // * include a list of copyright holders. << 10 // * << 11 // * Neither the authors of this software syst << 12 // * institutes,nor the agencies providing fin << 13 // * work make any representation or warran << 14 // * regarding this software system or assum << 15 // * use. Please see the license in the file << 16 // * for the full disclaimer and the limitatio << 17 // * << 18 // * This code implementation is the result << 19 // * technical work of the GEANT4 collaboratio << 20 // * By using, copying, modifying or distri << 21 // * any work based on the software) you ag << 22 // * use in resulting scientific publicati << 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* << 25 // 7 // 26 // G4SolidStore implementation << 8 // $Id: G4SolidStore.cc,v 1.3.2.1 1999/12/07 20:48:14 gunter Exp $ >> 9 // GEANT4 tag $Name: geant4-01-00 $ 27 // 10 // 28 // 10.07.95, P.Kent, G.Cosmo << 11 // G4SolidStore 29 // ------------------------------------------- << 12 // >> 13 // Implementation for singleton container >> 14 // >> 15 // History: >> 16 // 10.07.95 P.Kent Initial version 30 17 31 #include "globals.hh" << 32 #include "G4SolidStore.hh" 18 #include "G4SolidStore.hh" 33 #include "G4GeometryManager.hh" << 19 #include "globals.hh" 34 << 35 #include "G4AutoLock.hh" << 36 << 37 namespace << 38 { << 39 G4Mutex mapMutex = G4MUTEX_INITIALIZER; << 40 } << 41 << 42 // ******************************************* << 43 // Static class variables << 44 // ******************************************* << 45 // << 46 G4SolidStore* G4SolidStore::fgInstance = nullp << 47 G4ThreadLocal G4VStoreNotifier* G4SolidStore:: << 48 G4ThreadLocal G4bool G4SolidStore::locked = fa << 49 20 50 // ******************************************* << 51 // Protected constructor: Construct underlying 21 // Protected constructor: Construct underlying container with 52 // initial size of 100 entries 22 // initial size of 100 entries 53 // ******************************************* << 23 G4SolidStore::G4SolidStore() : G4RWTPtrOrderedVector<G4VSolid>(100) 54 // << 55 G4SolidStore::G4SolidStore() << 56 << 57 { 24 { 58 reserve(100); << 59 } 25 } 60 26 61 // ******************************************* << 62 // Destructor 27 // Destructor 63 // ******************************************* << 64 // << 65 G4SolidStore::~G4SolidStore() 28 G4SolidStore::~G4SolidStore() 66 { 29 { 67 Clean(); << 30 clearAndDestroy(); 68 } << 69 << 70 // ******************************************* << 71 // Delete all elements from the store << 72 // ******************************************* << 73 // << 74 void G4SolidStore::Clean() << 75 { << 76 // Do nothing if geometry is closed << 77 // << 78 if (G4GeometryManager::GetInstance()->IsGeom << 79 { << 80 G4cout << "WARNING - Attempt to delete the << 81 << " while geometry closed !" << G4 << 82 return; << 83 } << 84 << 85 // Locks store for deletion of solids. De-re << 86 // performed at this stage. G4VSolids will n << 87 // << 88 locked = true; << 89 << 90 G4SolidStore* store = GetInstance(); << 91 << 92 for(auto pos=store->cbegin(); pos!=store->ce << 93 { << 94 if (fgNotifier != nullptr) { fgNotifier->N << 95 delete *pos; << 96 } << 97 << 98 store->bmap.clear(); store->mvalid = false; << 99 locked = false; << 100 store->clear(); << 101 } << 102 << 103 // ******************************************* << 104 // Associate user notifier to the store << 105 // ******************************************* << 106 // << 107 void G4SolidStore::SetNotifier(G4VStoreNotifie << 108 { << 109 GetInstance(); << 110 fgNotifier = pNotifier; << 111 } 31 } 112 32 113 // ******************************************* << 33 // Static class variable 114 // Bring contents of internal map up to date a << 34 G4SolidStore* G4SolidStore::fgInstance = 0; 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 35 140 // ******************************************* << 141 // Add Solid to container 36 // Add Solid to container 142 // ******************************************* << 143 // << 144 void G4SolidStore::Register(G4VSolid* pSolid) 37 void G4SolidStore::Register(G4VSolid* pSolid) 145 { 38 { 146 G4SolidStore* store = GetInstance(); << 39 GetInstance()->insert(pSolid); 147 store->push_back(pSolid); << 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 } 40 } 162 41 163 // ******************************************* << 164 // Remove Solid from container 42 // Remove Solid from container 165 // ******************************************* << 166 // << 167 void G4SolidStore::DeRegister(G4VSolid* pSolid 43 void G4SolidStore::DeRegister(G4VSolid* pSolid) 168 { 44 { 169 G4SolidStore* store = GetInstance(); << 45 GetInstance()->remove(pSolid); 170 if (!locked) // Do not de-register if loc << 171 { << 172 if (fgNotifier != nullptr) { fgNotifier->N << 173 for (auto i=store->crbegin(); i!=store->cr << 174 { << 175 if (**i==*pSolid) << 176 { << 177 store->erase(std::next(i).base()); << 178 store->mvalid = false; << 179 break; << 180 } << 181 } << 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 } << 203 } << 204 << 205 // ******************************************* << 206 // Retrieve the first or last solid pointer in << 207 // ******************************************* << 208 // << 209 G4VSolid* G4SolidStore::GetSolid(const G4Strin << 210 G4bool revers << 211 { << 212 G4SolidStore* store = GetInstance(); << 213 if (!store->mvalid) { store->UpdateMap(); } << 214 auto pos = store->bmap.find(name); << 215 if(pos != store->bmap.cend()) << 216 { << 217 if ((verbose) && (pos->second.size()>1)) << 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 } << 235 if (verbose) << 236 { << 237 std::ostringstream message; << 238 message << "Solid " << name << " not found << 239 << "Returning NULL pointer."; << 240 G4Exception("G4SolidStore::GetSolid()", << 241 "GeomMgt1001", JustWarning, me << 242 } << 243 return nullptr; << 244 } 46 } 245 47 246 // ******************************************* << 247 // Return ptr to Store, setting if necessary 48 // Return ptr to Store, setting if necessary 248 // ******************************************* << 249 // << 250 G4SolidStore* G4SolidStore::GetInstance() 49 G4SolidStore* G4SolidStore::GetInstance() 251 { 50 { 252 static G4SolidStore worldStore; << 51 static G4SolidStore worldStore; 253 if (fgInstance == nullptr) << 52 if (!fgInstance) 254 { << 53 { 255 fgInstance = &worldStore; << 54 fgInstance = &worldStore; 256 } << 55 } 257 return fgInstance; << 56 return fgInstance; 258 } 57 } 259 58