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 // 26 // G4AssemblyStore 27 // G4AssemblyStore 27 // 28 // 28 // Implementation for singleton container 29 // Implementation for singleton container 29 // 30 // 30 // 9.10.2018 G.Cosmo, CERN - Initial version << 31 // History: >> 32 // 9.10.18 G.Cosmo Initial version 31 // ------------------------------------------- 33 // -------------------------------------------------------------------- 32 34 33 #include "G4AssemblyVolume.hh" 35 #include "G4AssemblyVolume.hh" 34 #include "G4AssemblyStore.hh" 36 #include "G4AssemblyStore.hh" 35 #include "G4GeometryManager.hh" 37 #include "G4GeometryManager.hh" 36 #include "G4ios.hh" 38 #include "G4ios.hh" 37 39 38 // ******************************************* 40 // *************************************************************************** 39 // Static class variables 41 // Static class variables 40 // ******************************************* 42 // *************************************************************************** 41 // 43 // 42 G4AssemblyStore* G4AssemblyStore::fgInstance = 44 G4AssemblyStore* G4AssemblyStore::fgInstance = nullptr; 43 G4ThreadLocal G4VStoreNotifier* G4AssemblyStor 45 G4ThreadLocal G4VStoreNotifier* G4AssemblyStore::fgNotifier = nullptr; 44 G4ThreadLocal G4bool G4AssemblyStore::locked = 46 G4ThreadLocal G4bool G4AssemblyStore::locked = false; 45 47 46 // ******************************************* 48 // *************************************************************************** 47 // Protected constructor: Construct underlying 49 // Protected constructor: Construct underlying container with 48 // initial size of 20 entries 50 // initial size of 20 entries 49 // ******************************************* 51 // *************************************************************************** 50 // 52 // 51 G4AssemblyStore::G4AssemblyStore() 53 G4AssemblyStore::G4AssemblyStore() >> 54 : std::vector<G4AssemblyVolume*>() 52 { 55 { 53 reserve(20); 56 reserve(20); 54 } 57 } 55 58 56 // ******************************************* 59 // *************************************************************************** 57 // Destructor 60 // Destructor 58 // ******************************************* 61 // *************************************************************************** 59 // 62 // 60 G4AssemblyStore::~G4AssemblyStore() 63 G4AssemblyStore::~G4AssemblyStore() 61 { 64 { 62 Clean(); // Delete all assemblies in the st 65 Clean(); // Delete all assemblies in the store 63 } 66 } 64 67 65 // ******************************************* 68 // *************************************************************************** 66 // Delete all assemblies from the store 69 // Delete all assemblies from the store 67 // ******************************************* 70 // *************************************************************************** 68 // 71 // 69 void G4AssemblyStore::Clean() 72 void G4AssemblyStore::Clean() 70 { 73 { 71 // Do nothing if geometry is closed 74 // Do nothing if geometry is closed 72 // 75 // 73 if (G4GeometryManager::GetInstance()->IsGeom << 76 if (G4GeometryManager::IsGeometryClosed()) 74 { 77 { 75 G4cout << "WARNING - Attempt to delete the 78 G4cout << "WARNING - Attempt to delete the assembly store" 76 << " while geometry closed !" << G4 79 << " while geometry closed !" << G4endl; 77 return; 80 return; 78 } 81 } 79 82 80 // Locks store for deletion of assemblies. D 83 // Locks store for deletion of assemblies. De-registration will be 81 // performed at this stage. Assemblies will 84 // performed at this stage. Assemblies will not de-register themselves. 82 // 85 // 83 locked = true; 86 locked = true; 84 87 >> 88 size_t i=0; 85 G4AssemblyStore* store = GetInstance(); 89 G4AssemblyStore* store = GetInstance(); 86 90 87 for(const auto & pos : *store) << 91 #ifdef G4DEBUG_NAVIGATION >> 92 G4cout << "Deleting Assemblies ... "; >> 93 #endif >> 94 >> 95 for(auto pos=store->cbegin(); pos!=store->cend(); ++pos) 88 { 96 { 89 if (fgNotifier != nullptr) { fgNotifier->N 97 if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); } 90 delete pos; << 98 if (*pos) { delete *pos; } >> 99 ++i; 91 } 100 } 92 101 >> 102 #ifdef G4DEBUG_NAVIGATION >> 103 if (store->size() < i-1) >> 104 { G4cout << "No assembly deleted. Already deleted by user ?" << G4endl; } >> 105 else >> 106 { G4cout << i-1 << " assemblies deleted !" << G4endl; } >> 107 #endif >> 108 93 locked = false; 109 locked = false; 94 store->clear(); 110 store->clear(); 95 } 111 } 96 112 97 // ******************************************* 113 // *************************************************************************** 98 // Associate user notifier to the store 114 // Associate user notifier to the store 99 // ******************************************* 115 // *************************************************************************** 100 // 116 // 101 void G4AssemblyStore::SetNotifier(G4VStoreNoti 117 void G4AssemblyStore::SetNotifier(G4VStoreNotifier* pNotifier) 102 { 118 { 103 GetInstance(); 119 GetInstance(); 104 fgNotifier = pNotifier; 120 fgNotifier = pNotifier; 105 } 121 } 106 122 107 // ******************************************* 123 // *************************************************************************** 108 // Add Assembly to container 124 // Add Assembly to container 109 // ******************************************* 125 // *************************************************************************** 110 // 126 // 111 void G4AssemblyStore::Register(G4AssemblyVolum 127 void G4AssemblyStore::Register(G4AssemblyVolume* pAssembly) 112 { 128 { 113 GetInstance()->push_back(pAssembly); 129 GetInstance()->push_back(pAssembly); 114 if (fgNotifier != nullptr) { fgNotifier->No 130 if (fgNotifier != nullptr) { fgNotifier->NotifyRegistration(); } 115 } 131 } 116 132 117 // ******************************************* 133 // *************************************************************************** 118 // Remove Assembly from container 134 // Remove Assembly from container 119 // ******************************************* 135 // *************************************************************************** 120 // 136 // 121 void G4AssemblyStore::DeRegister(G4AssemblyVol 137 void G4AssemblyStore::DeRegister(G4AssemblyVolume* pAssembly) 122 { 138 { 123 if (!locked) // Do not de-register if loc 139 if (!locked) // Do not de-register if locked ! 124 { 140 { 125 if (fgNotifier != nullptr) { fgNotifier-> 141 if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); } 126 for (auto i=GetInstance()->cbegin(); i!=Ge 142 for (auto i=GetInstance()->cbegin(); i!=GetInstance()->cend(); ++i) 127 { 143 { 128 if (*i==pAssembly) 144 if (*i==pAssembly) 129 { 145 { 130 GetInstance()->erase(i); 146 GetInstance()->erase(i); 131 break; 147 break; 132 } 148 } 133 } 149 } 134 } 150 } 135 } 151 } 136 152 137 // ******************************************* 153 // *************************************************************************** 138 // Return ptr to Store, setting if necessary 154 // Return ptr to Store, setting if necessary 139 // ******************************************* 155 // *************************************************************************** 140 // 156 // 141 G4AssemblyStore* G4AssemblyStore::GetInstance( 157 G4AssemblyStore* G4AssemblyStore::GetInstance() 142 { 158 { 143 static G4AssemblyStore assemblyStore; 159 static G4AssemblyStore assemblyStore; 144 if (fgInstance == nullptr) 160 if (fgInstance == nullptr) 145 { 161 { 146 fgInstance = &assemblyStore; 162 fgInstance = &assemblyStore; 147 } 163 } 148 return fgInstance; 164 return fgInstance; 149 } 165 } 150 166 151 // ******************************************* 167 // *************************************************************************** 152 // Returns an assembly through its name specif 168 // Returns an assembly through its name specification. 153 // ******************************************* 169 // *************************************************************************** 154 // 170 // 155 G4AssemblyVolume* 171 G4AssemblyVolume* 156 G4AssemblyStore::GetAssembly(unsigned int id, 172 G4AssemblyStore::GetAssembly(unsigned int id, G4bool verbose) const 157 { 173 { 158 for (const auto & i : *GetInstance()) << 174 for (auto i=GetInstance()->cbegin(); i!=GetInstance()->cend(); ++i) 159 { 175 { 160 if (i->GetAssemblyID() == id) { return i; << 176 if ((*i)->GetAssemblyID() == id) { return *i; } 161 } 177 } 162 if (verbose) 178 if (verbose) 163 { 179 { 164 std::ostringstream message; 180 std::ostringstream message; 165 message << "Assembly NOT found in store !" 181 message << "Assembly NOT found in store !" << G4endl 166 << " Assembly " << id << " 182 << " Assembly " << id << " NOT found in store !" << G4endl 167 << " Returning NULL pointer 183 << " Returning NULL pointer."; 168 G4Exception("G4AssemblyStore::GetAssembly( 184 G4Exception("G4AssemblyStore::GetAssembly()", 169 "GeomVol1001", JustWarning, me 185 "GeomVol1001", JustWarning, message); 170 } 186 } 171 return nullptr; << 187 return 0; 172 } 188 } 173 189