G4LogicalVolumeStore.cc

Go to the documentation of this file.
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 // G4LogicalVolumeStore
00030 //
00031 // Implementation for singleton container
00032 //
00033 // History:
00034 // 10.07.95 P.Kent Initial version
00035 // --------------------------------------------------------------------
00036 
00037 #include "G4Types.hh"
00038 #include "G4LogicalVolumeStore.hh"
00039 #include "G4GeometryManager.hh"
00040 
00041 // ***************************************************************************
00042 // Static class variables
00043 // ***************************************************************************
00044 //
00045 G4LogicalVolumeStore* G4LogicalVolumeStore::fgInstance = 0;
00046 G4VStoreNotifier* G4LogicalVolumeStore::fgNotifier = 0;
00047 G4bool G4LogicalVolumeStore::locked = false;
00048 
00049 // ***************************************************************************
00050 // Protected constructor: Construct underlying container with
00051 // initial size of 100 entries
00052 // ***************************************************************************
00053 //
00054 G4LogicalVolumeStore::G4LogicalVolumeStore()
00055  : std::vector<G4LogicalVolume*>()
00056 {
00057   reserve(100);
00058 }
00059 
00060 // ***************************************************************************
00061 // Destructor
00062 // ***************************************************************************
00063 //
00064 G4LogicalVolumeStore::~G4LogicalVolumeStore()
00065 {
00066   Clean();
00067 }
00068 
00069 // ***************************************************************************
00070 // Delete all elements from the store
00071 // ***************************************************************************
00072 //
00073 void G4LogicalVolumeStore::Clean()
00074 {
00075   // Do nothing if geometry is closed
00076   //
00077   if (G4GeometryManager::GetInstance()->IsGeometryClosed())
00078   {
00079     G4cout << "WARNING - Attempt to delete the logical volume store"
00080            << " while geometry closed !" << G4endl;
00081     return;
00082   }
00083 
00084   // Locks store for deletion of volumes. De-registration will be
00085   // performed at this stage. G4LogicalVolumes will not de-register themselves.
00086   //
00087   locked = true;  
00088 
00089   size_t i=0;
00090   G4LogicalVolumeStore* store = GetInstance();
00091 
00092 #ifdef G4GEOMETRY_VOXELDEBUG
00093   G4cout << "Deleting Logical Volumes ... ";
00094 #endif
00095 
00096   for(iterator pos=store->begin(); pos!=store->end(); pos++)
00097   {
00098     if (fgNotifier) { fgNotifier->NotifyDeRegistration(); }
00099     if (*pos) { (*pos)->Lock(); delete *pos; }
00100     i++;
00101   }
00102 
00103 #ifdef G4GEOMETRY_VOXELDEBUG
00104   if (store->size() < i-1)
00105     { G4cout << "No volumes deleted. Already deleted by user ?" << G4endl; }
00106   else
00107     { G4cout << i-1 << " volumes deleted !" << G4endl; }
00108 #endif
00109 
00110   locked = false;
00111   store->clear();
00112 }
00113 
00114 // ***************************************************************************
00115 // Associate user notifier to the store
00116 // ***************************************************************************
00117 //
00118 void G4LogicalVolumeStore::SetNotifier(G4VStoreNotifier* pNotifier)
00119 {
00120   GetInstance();
00121   fgNotifier = pNotifier;
00122 }
00123 
00124 // ***************************************************************************
00125 // Add volume to container
00126 // ***************************************************************************
00127 //
00128 void G4LogicalVolumeStore::Register(G4LogicalVolume* pVolume)
00129 {
00130   GetInstance()->push_back(pVolume);
00131   if (fgNotifier) { fgNotifier->NotifyRegistration(); }
00132 }
00133 
00134 // ***************************************************************************
00135 // Remove volume from container
00136 // ***************************************************************************
00137 //
00138 void G4LogicalVolumeStore::DeRegister(G4LogicalVolume* pVolume)
00139 {
00140   if (!locked)    // Do not de-register if locked !
00141   {
00142     if (fgNotifier) { fgNotifier->NotifyDeRegistration(); }
00143     for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
00144     {
00145       if (**i==*pVolume)
00146       {
00147         GetInstance()->erase(i);
00148         break;
00149       }
00150     }
00151   }
00152 }
00153 
00154 // ***************************************************************************
00155 // Retrieve the first volume pointer in the container having that name
00156 // ***************************************************************************
00157 //
00158 G4LogicalVolume*
00159 G4LogicalVolumeStore::GetVolume(const G4String& name, G4bool verbose) const
00160 {
00161   for (iterator i=GetInstance()->begin(); i!=GetInstance()->end(); i++)
00162   {
00163     if ((*i)->GetName() == name) { return *i; }
00164   }
00165   if (verbose)
00166   {
00167      std::ostringstream message;
00168      message << "Volume NOT found in store !" << G4endl
00169              << "        Volume " << name << " NOT found in store !" << G4endl
00170              << "        Returning NULL pointer.";
00171      G4Exception("G4LogicalVolumeStore::GetVolume()",
00172                  "GeomMgt1001", JustWarning, message);
00173   }
00174   return 0;
00175 }
00176 
00177 // ***************************************************************************
00178 // Return ptr to Store, setting if necessary
00179 // ***************************************************************************
00180 //
00181 G4LogicalVolumeStore* G4LogicalVolumeStore::GetInstance()
00182 {
00183   static G4LogicalVolumeStore worldStore;
00184   if (!fgInstance)
00185   {
00186     fgInstance = &worldStore;
00187   }
00188   return fgInstance;
00189 }

Generated on Mon May 27 17:48:49 2013 for Geant4 by  doxygen 1.4.7