00001 // 00002 // ******************************************************************** 00003 // * License and Disclaimer * 00004 // * * 00005 // * The Geant4 software is copyright of the Copyright Holders of * 00006 // * the Geant4 Collaboration. It is provided under the terms and * 00007 // * conditions of the Geant4 Software License, included in the file * 00008 // * LICENSE and available at http://cern.ch/geant4/license . These * 00009 // * include a list of copyright holders. * 00010 // * * 00011 // * Neither the authors of this software system, nor their employing * 00012 // * institutes,nor the agencies providing financial support for this * 00013 // * work make any representation or warranty, express or implied, * 00014 // * regarding this software system or assume any liability for its * 00015 // * use. Please see the license in the file LICENSE and URL above * 00016 // * for the full disclaimer and the limitation of liability. * 00017 // * * 00018 // * This code implementation is the result of the scientific and * 00019 // * technical work of the GEANT4 collaboration. * 00020 // * By using, copying, modifying or distributing the software (or * 00021 // * any work based on the software) you agree to acknowledge its * 00022 // * use in resulting scientific publications, and indicate your * 00023 // * acceptance of all terms of the Geant4 Software license. * 00024 // ******************************************************************** 00025 // 00026 // 00027 // $Id$ 00028 // 00029 // G4PhysicalVolumeStore 00030 // 00031 // Implementation for singleton container 00032 // 00033 // History: 00034 // 25.07.95 P.Kent Initial version 00035 // -------------------------------------------------------------------- 00036 00037 #include "G4Types.hh" 00038 #include "G4PhysicalVolumeStore.hh" 00039 #include "G4GeometryManager.hh" 00040 #include "G4LogicalVolume.hh" 00041 00042 // *************************************************************************** 00043 // Static class variables 00044 // *************************************************************************** 00045 // 00046 G4PhysicalVolumeStore* G4PhysicalVolumeStore::fgInstance = 0; 00047 G4VStoreNotifier* G4PhysicalVolumeStore::fgNotifier = 0; 00048 G4bool G4PhysicalVolumeStore::locked = false; 00049 00050 // *************************************************************************** 00051 // Protected constructor: Construct underlying container with 00052 // initial size of 100 entries 00053 // *************************************************************************** 00054 // 00055 G4PhysicalVolumeStore::G4PhysicalVolumeStore() 00056 : std::vector<G4VPhysicalVolume*>() 00057 { 00058 reserve(100); 00059 } 00060 00061 // *************************************************************************** 00062 // Destructor 00063 // *************************************************************************** 00064 // 00065 G4PhysicalVolumeStore::~G4PhysicalVolumeStore() 00066 { 00067 Clean(); 00068 } 00069 00070 // *************************************************************************** 00071 // Delete all elements from the store 00072 // *************************************************************************** 00073 // 00074 void G4PhysicalVolumeStore::Clean() 00075 { 00076 // Do nothing if geometry is closed 00077 // 00078 if (G4GeometryManager::GetInstance()->IsGeometryClosed()) 00079 { 00080 G4cout << "WARNING - Attempt to delete the physical volume store" 00081 << " while geometry closed !" << G4endl; 00082 return; 00083 } 00084 00085 // Locks store for deletion of volumes. De-registration will be 00086 // performed at this stage. G4VPhysicalVolumes will not de-register 00087 // themselves. 00088 // 00089 locked = true; 00090 00091 size_t i=0; 00092 G4PhysicalVolumeStore* store = GetInstance(); 00093 std::vector<G4VPhysicalVolume*>::iterator pos; 00094 00095 #ifdef G4GEOMETRY_VOXELDEBUG 00096 G4cout << "Deleting Physical Volumes ... "; 00097 #endif 00098 00099 for(pos=store->begin(); pos!=store->end(); pos++) 00100 { 00101 if (fgNotifier) { fgNotifier->NotifyDeRegistration(); } 00102 if (*pos) { delete *pos; } 00103 i++; 00104 } 00105 00106 #ifdef G4GEOMETRY_VOXELDEBUG 00107 if (store->size() < i-1) 00108 { G4cout << "No volumes deleted. Already deleted by user ?" << G4endl; } 00109 else 00110 { G4cout << i-1 << " volumes deleted !" << G4endl; } 00111 #endif 00112 00113 locked = false; 00114 store->clear(); 00115 } 00116 00117 // *************************************************************************** 00118 // Associate user notifier to the store 00119 // *************************************************************************** 00120 // 00121 void G4PhysicalVolumeStore::SetNotifier(G4VStoreNotifier* pNotifier) 00122 { 00123 GetInstance(); 00124 fgNotifier = pNotifier; 00125 } 00126 00127 // *************************************************************************** 00128 // Add Volume to container 00129 // *************************************************************************** 00130 // 00131 void G4PhysicalVolumeStore::Register(G4VPhysicalVolume* pVolume) 00132 { 00133 GetInstance()->push_back(pVolume); 00134 if (fgNotifier) { fgNotifier->NotifyRegistration(); } 00135 } 00136 00137 // *************************************************************************** 00138 // Remove Volume from container and update the list of daughters 00139 // of the mother's logical volume 00140 // *************************************************************************** 00141 // 00142 void G4PhysicalVolumeStore::DeRegister(G4VPhysicalVolume* pVolume) 00143 { 00144 if (!locked) // Do not de-register if locked ! 00145 { 00146 if (fgNotifier) { fgNotifier->NotifyDeRegistration(); } 00147 G4LogicalVolume* motherLogical = pVolume->GetMotherLogical(); 00148 if (motherLogical) { motherLogical->RemoveDaughter(pVolume); } 00149 for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++) 00150 { 00151 if (**i==*pVolume) 00152 { 00153 GetInstance()->erase(i); 00154 break; 00155 } 00156 } 00157 } 00158 } 00159 00160 // *************************************************************************** 00161 // Retrieve the first volume pointer in the container having that name 00162 // *************************************************************************** 00163 // 00164 G4VPhysicalVolume* 00165 G4PhysicalVolumeStore::GetVolume(const G4String& name, G4bool verbose) const 00166 { 00167 for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++) 00168 { 00169 if ((*i)->GetName() == name) { return *i; } 00170 } 00171 if (verbose) 00172 { 00173 std::ostringstream message; 00174 message << "Volume NOT found in store !" << G4endl 00175 << " Volume " << name << " NOT found in store !" << G4endl 00176 << " Returning NULL pointer."; 00177 G4Exception("G4PhysicalVolumeStore::GetVolume()", 00178 "GeomMgt1001", JustWarning, message); 00179 } 00180 return 0; 00181 } 00182 00183 // *************************************************************************** 00184 // Return ptr to Store, setting if necessary 00185 // *************************************************************************** 00186 // 00187 G4PhysicalVolumeStore* G4PhysicalVolumeStore::GetInstance() 00188 { 00189 static G4PhysicalVolumeStore worldStore; 00190 if (!fgInstance) 00191 { 00192 fgInstance = &worldStore; 00193 } 00194 return fgInstance; 00195 }