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