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