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