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 85 G4AssemblyStore* store = GetInstance(); 88 G4AssemblyStore* store = GetInstance(); 86 89 87 for(const auto & pos : *store) << 90 for(auto pos=store->cbegin(); pos!=store->cend(); ++pos) 88 { 91 { 89 if (fgNotifier != nullptr) { fgNotifier->N 92 if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); } 90 delete pos; << 93 if (*pos) { delete *pos; } 91 } 94 } 92 95 93 locked = false; 96 locked = false; 94 store->clear(); 97 store->clear(); 95 } 98 } 96 99 97 // ******************************************* 100 // *************************************************************************** 98 // Associate user notifier to the store 101 // Associate user notifier to the store 99 // ******************************************* 102 // *************************************************************************** 100 // 103 // 101 void G4AssemblyStore::SetNotifier(G4VStoreNoti 104 void G4AssemblyStore::SetNotifier(G4VStoreNotifier* pNotifier) 102 { 105 { 103 GetInstance(); 106 GetInstance(); 104 fgNotifier = pNotifier; 107 fgNotifier = pNotifier; 105 } 108 } 106 109 107 // ******************************************* 110 // *************************************************************************** 108 // Add Assembly to container 111 // Add Assembly to container 109 // ******************************************* 112 // *************************************************************************** 110 // 113 // 111 void G4AssemblyStore::Register(G4AssemblyVolum 114 void G4AssemblyStore::Register(G4AssemblyVolume* pAssembly) 112 { 115 { 113 GetInstance()->push_back(pAssembly); 116 GetInstance()->push_back(pAssembly); 114 if (fgNotifier != nullptr) { fgNotifier->No 117 if (fgNotifier != nullptr) { fgNotifier->NotifyRegistration(); } 115 } 118 } 116 119 117 // ******************************************* 120 // *************************************************************************** 118 // Remove Assembly from container 121 // Remove Assembly from container 119 // ******************************************* 122 // *************************************************************************** 120 // 123 // 121 void G4AssemblyStore::DeRegister(G4AssemblyVol 124 void G4AssemblyStore::DeRegister(G4AssemblyVolume* pAssembly) 122 { 125 { 123 if (!locked) // Do not de-register if loc 126 if (!locked) // Do not de-register if locked ! 124 { 127 { 125 if (fgNotifier != nullptr) { fgNotifier-> 128 if (fgNotifier != nullptr) { fgNotifier->NotifyDeRegistration(); } 126 for (auto i=GetInstance()->cbegin(); i!=Ge 129 for (auto i=GetInstance()->cbegin(); i!=GetInstance()->cend(); ++i) 127 { 130 { 128 if (*i==pAssembly) 131 if (*i==pAssembly) 129 { 132 { 130 GetInstance()->erase(i); 133 GetInstance()->erase(i); 131 break; 134 break; 132 } 135 } 133 } 136 } 134 } 137 } 135 } 138 } 136 139 137 // ******************************************* 140 // *************************************************************************** 138 // Return ptr to Store, setting if necessary 141 // Return ptr to Store, setting if necessary 139 // ******************************************* 142 // *************************************************************************** 140 // 143 // 141 G4AssemblyStore* G4AssemblyStore::GetInstance( 144 G4AssemblyStore* G4AssemblyStore::GetInstance() 142 { 145 { 143 static G4AssemblyStore assemblyStore; 146 static G4AssemblyStore assemblyStore; 144 if (fgInstance == nullptr) 147 if (fgInstance == nullptr) 145 { 148 { 146 fgInstance = &assemblyStore; 149 fgInstance = &assemblyStore; 147 } 150 } 148 return fgInstance; 151 return fgInstance; 149 } 152 } 150 153 151 // ******************************************* 154 // *************************************************************************** 152 // Returns an assembly through its name specif 155 // Returns an assembly through its name specification. 153 // ******************************************* 156 // *************************************************************************** 154 // 157 // 155 G4AssemblyVolume* 158 G4AssemblyVolume* 156 G4AssemblyStore::GetAssembly(unsigned int id, 159 G4AssemblyStore::GetAssembly(unsigned int id, G4bool verbose) const 157 { 160 { 158 for (const auto & i : *GetInstance()) << 161 for (auto i=GetInstance()->cbegin(); i!=GetInstance()->cend(); ++i) 159 { 162 { 160 if (i->GetAssemblyID() == id) { return i; << 163 if ((*i)->GetAssemblyID() == id) { return *i; } 161 } 164 } 162 if (verbose) 165 if (verbose) 163 { 166 { 164 std::ostringstream message; 167 std::ostringstream message; 165 message << "Assembly NOT found in store !" 168 message << "Assembly NOT found in store !" << G4endl 166 << " Assembly " << id << " 169 << " Assembly " << id << " NOT found in store !" << G4endl 167 << " Returning NULL pointer 170 << " Returning NULL pointer."; 168 G4Exception("G4AssemblyStore::GetAssembly( 171 G4Exception("G4AssemblyStore::GetAssembly()", 169 "GeomVol1001", JustWarning, me 172 "GeomVol1001", JustWarning, message); 170 } 173 } 171 return nullptr; << 174 return 0; 172 } 175 } 173 176