[831] | 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 | // |
---|
| 27 | // $Id: G4SolidStore.cc,v 1.17 2007/04/10 10:13:50 gcosmo Exp $ |
---|
[1337] | 28 | // GEANT4 tag $Name: geant4-09-04-beta-01 $ |
---|
[831] | 29 | // |
---|
| 30 | // G4SolidStore |
---|
| 31 | // |
---|
| 32 | // Implementation for singleton container |
---|
| 33 | // |
---|
| 34 | // History: |
---|
| 35 | // 10.07.95 P.Kent Initial version |
---|
| 36 | // -------------------------------------------------------------------- |
---|
| 37 | |
---|
| 38 | #include "globals.hh" |
---|
| 39 | #include "G4SolidStore.hh" |
---|
| 40 | #include "G4GeometryManager.hh" |
---|
| 41 | |
---|
| 42 | // *************************************************************************** |
---|
| 43 | // Static class variables |
---|
| 44 | // *************************************************************************** |
---|
| 45 | // |
---|
| 46 | G4SolidStore* G4SolidStore::fgInstance = 0; |
---|
| 47 | G4VStoreNotifier* G4SolidStore::fgNotifier = 0; |
---|
| 48 | G4bool G4SolidStore::locked = false; |
---|
| 49 | |
---|
| 50 | // *************************************************************************** |
---|
| 51 | // Protected constructor: Construct underlying container with |
---|
| 52 | // initial size of 100 entries |
---|
| 53 | // *************************************************************************** |
---|
| 54 | // |
---|
| 55 | G4SolidStore::G4SolidStore() |
---|
| 56 | : std::vector<G4VSolid*>() |
---|
| 57 | { |
---|
| 58 | reserve(100); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | // *************************************************************************** |
---|
| 62 | // Destructor |
---|
| 63 | // *************************************************************************** |
---|
| 64 | // |
---|
| 65 | G4SolidStore::~G4SolidStore() |
---|
| 66 | { |
---|
| 67 | Clean(); |
---|
| 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()->IsGeometryClosed()) |
---|
| 79 | { |
---|
| 80 | G4cout << "WARNING - Attempt to delete the solid store" |
---|
| 81 | << " while geometry closed !" << G4endl; |
---|
| 82 | return; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | // Locks store for deletion of solids. De-registration will be |
---|
| 86 | // performed at this stage. G4VSolids will not de-register themselves. |
---|
| 87 | // |
---|
| 88 | locked = true; |
---|
| 89 | |
---|
| 90 | size_t i=0; |
---|
| 91 | G4SolidStore* store = GetInstance(); |
---|
| 92 | |
---|
| 93 | #ifdef G4GEOMETRY_VOXELDEBUG |
---|
| 94 | G4cout << "Deleting Solids ... "; |
---|
| 95 | #endif |
---|
| 96 | |
---|
| 97 | for(iterator pos=store->begin(); pos!=store->end(); pos++) |
---|
| 98 | { |
---|
| 99 | if (fgNotifier) { fgNotifier->NotifyDeRegistration(); } |
---|
| 100 | if (*pos) { delete *pos; } |
---|
| 101 | i++; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | #ifdef G4GEOMETRY_VOXELDEBUG |
---|
| 105 | if (store->size() < i-1) |
---|
| 106 | { G4cout << "No solids deleted. Already deleted by user ?" << G4endl; } |
---|
| 107 | else |
---|
| 108 | { G4cout << i-1 << " solids deleted !" << G4endl; } |
---|
| 109 | #endif |
---|
| 110 | |
---|
| 111 | locked = false; |
---|
| 112 | store->clear(); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | // *************************************************************************** |
---|
| 116 | // Associate user notifier to the store |
---|
| 117 | // *************************************************************************** |
---|
| 118 | // |
---|
| 119 | void G4SolidStore::SetNotifier(G4VStoreNotifier* pNotifier) |
---|
| 120 | { |
---|
| 121 | GetInstance(); |
---|
| 122 | fgNotifier = pNotifier; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | // *************************************************************************** |
---|
| 126 | // Add Solid to container |
---|
| 127 | // *************************************************************************** |
---|
| 128 | // |
---|
| 129 | void G4SolidStore::Register(G4VSolid* pSolid) |
---|
| 130 | { |
---|
| 131 | GetInstance()->push_back(pSolid); |
---|
| 132 | if (fgNotifier) { fgNotifier->NotifyRegistration(); } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | // *************************************************************************** |
---|
| 136 | // Remove Solid from container |
---|
| 137 | // *************************************************************************** |
---|
| 138 | // |
---|
| 139 | void G4SolidStore::DeRegister(G4VSolid* pSolid) |
---|
| 140 | { |
---|
| 141 | if (!locked) // Do not de-register if locked ! |
---|
| 142 | { |
---|
| 143 | if (fgNotifier) { fgNotifier->NotifyDeRegistration(); } |
---|
| 144 | for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++) |
---|
| 145 | { |
---|
| 146 | if (**i==*pSolid) |
---|
| 147 | { |
---|
| 148 | GetInstance()->erase(i); |
---|
| 149 | break; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | // *************************************************************************** |
---|
| 156 | // Retrieve the first solid pointer in the container having that name |
---|
| 157 | // *************************************************************************** |
---|
| 158 | // |
---|
| 159 | G4VSolid* G4SolidStore::GetSolid(const G4String& name, G4bool verbose) const |
---|
| 160 | { |
---|
| 161 | for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++) |
---|
| 162 | { |
---|
| 163 | if ((*i)->GetName() == name) { return *i; } |
---|
| 164 | } |
---|
| 165 | if (verbose) |
---|
| 166 | { |
---|
| 167 | G4cerr << "ERROR - G4SolidStore::GetSolid()" << G4endl |
---|
| 168 | << " Solid " << name << " not found in store !" << G4endl |
---|
| 169 | << " Returning NULL pointer." << G4endl; |
---|
| 170 | G4Exception("G4SolidStore::GetSolid()", "InvalidQuery", |
---|
| 171 | JustWarning, "Solid NOT found in store !"); |
---|
| 172 | } |
---|
| 173 | return 0; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | // *************************************************************************** |
---|
| 177 | // Return ptr to Store, setting if necessary |
---|
| 178 | // *************************************************************************** |
---|
| 179 | // |
---|
| 180 | G4SolidStore* G4SolidStore::GetInstance() |
---|
| 181 | { |
---|
| 182 | static G4SolidStore worldStore; |
---|
| 183 | if (!fgInstance) |
---|
| 184 | { |
---|
| 185 | fgInstance = &worldStore; |
---|
| 186 | } |
---|
| 187 | return fgInstance; |
---|
| 188 | } |
---|