Geant4  10.01.p01
G4StateManager.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 // $Id: G4StateManager.cc 67970 2013-03-13 10:10:06Z gcosmo $
28 //
29 //
30 // ------------------------------------------------------------
31 // GEANT 4 class implementation file
32 //
33 // ---------------- G4StateManager ----------------
34 // by Gabriele Cosmo, November 1996
35 // ------------------------------------------------------------
36 
37 #include "G4StateManager.hh"
38 #include "G4ios.hh"
39 
40 // Initialization of the static pointer of the single class instance
41 //
43 
45  : theCurrentState(G4State_PreInit),
46  thePreviousState(G4State_PreInit),
47  theBottomDependent(0),
48  suppressAbortion(0),
49  msgptr(0),
50  exceptionHandler(0)
51 {
52 #ifdef G4MULTITHREADED
54 #endif
55 }
56 
58 {
59  G4VStateDependent* state=0;
60 
61  while (theDependentsList.size()>0)
62  {
63  state = theDependentsList.back();
64  theDependentsList.pop_back();
65  for (std::vector<G4VStateDependent*>::iterator
66  i=theDependentsList.begin(); i!=theDependentsList.end();)
67  {
68  if (*i==state)
69  {
70  i = theDependentsList.erase(i);
71  }
72  else
73  {
74  ++i;
75  }
76  }
77  if ( state ) { delete state; }
78  }
79 #ifdef G4MULTITHREADED_DEACTIVATE
81 #endif
82 }
83 
84 // -------------------------------------------------------------------------
85 // No matter how copy-constructor and operators below are implemented ...
86 // just dummy implementations, since not relevant for the singleton and
87 // declared private.
88 //
90  : theCurrentState(right.theCurrentState),
91  thePreviousState(right.thePreviousState),
92  theDependentsList(right.theDependentsList),
93  theBottomDependent(right.theBottomDependent),
94  suppressAbortion(right.suppressAbortion),
95  msgptr(right.msgptr),
96  exceptionHandler(right.exceptionHandler)
97 {
98 }
99 
102 {
103  if (&right == this) { return *this; }
104 
110  msgptr = right.msgptr;
112 
113  return *this;
114 }
115 
116 G4int
118 {
119  return (this == &right);
120 }
121 
122 G4int
124 {
125  return (this != &right);
126 }
127 //
128 // -------------------------------------------------------------------------
129 
132 {
133  if (!theStateManager)
134  {
136  }
137  return theStateManager;
138 }
139 
140 G4bool
142 {
143  G4bool ack=true;
144  if(!bottom)
145  {
146  theDependentsList.push_back(aDependent);
147  }
148  else
149  {
151  {
153  }
154  theBottomDependent = aDependent;
155  }
156  return ack;
157 }
158 
159 G4bool
161 {
162  G4VStateDependent* tmp = 0;
163  for (std::vector<G4VStateDependent*>::iterator i=theDependentsList.begin();
164  i!=theDependentsList.end();)
165  {
166  if (**i==*aDependent)
167  {
168  tmp = *i;
169  i = theDependentsList.erase(i);
170  }
171  else
172  {
173  ++i;
174  }
175  }
176  return (tmp != 0);
177 }
178 
181 {
182  return theCurrentState;
183 }
184 
187 {
188  return thePreviousState;
189 }
190 
191 G4bool
193 { return SetNewState(requestedState,0); }
194 
195 G4bool
196 G4StateManager::SetNewState(G4ApplicationState requestedState, const char* msg)
197 {
198  if(requestedState==G4State_Abort && suppressAbortion>0)
199  {
200  if(suppressAbortion==2) { return false; }
201  if(theCurrentState==G4State_EventProc) { return false; }
202  }
203  msgptr = msg;
204  size_t i=0;
205  G4bool ack = true;
208  while ((ack) && (i<theDependentsList.size()))
209  {
210  ack = theDependentsList[i]->Notify(requestedState);
211  i++;
212  }
214  {
215  ack = theBottomDependent->Notify(requestedState);
216  }
217 
218  if(!ack)
219  { thePreviousState = savedState; }
220  else
221  { theCurrentState = requestedState; }
222  msgptr = 0;
223  return ack;
224 }
225 
228 {
229  G4VStateDependent* tmp = 0;
230  for (std::vector<G4VStateDependent*>::iterator i=theDependentsList.begin();
231  i!=theDependentsList.end();)
232  {
233  if (**i==*aDependent)
234  {
235  tmp = *i;
236  i = theDependentsList.erase(i);
237  }
238  else
239  {
240  ++i;
241  }
242  }
243  return tmp;
244 }
245 
246 G4String
248 {
249  G4String stateName;
250  switch(aState)
251  {
252  case G4State_PreInit:
253  stateName = "PreInit"; break;
254  case G4State_Init:
255  stateName = "Init"; break;
256  case G4State_Idle:
257  stateName = "Idle"; break;
258  case G4State_GeomClosed:
259  stateName = "GeomClosed"; break;
260  case G4State_EventProc:
261  stateName = "EventProc"; break;
262  case G4State_Quit:
263  stateName = "Quit"; break;
264  case G4State_Abort:
265  stateName = "Abort"; break;
266  default:
267  stateName = "Unknown"; break;
268  }
269  return stateName;
270 }
271 
272 //void G4StateManager::Pause()
273 //{
274 // Pause("G4_pause> ");
275 //}
276 //
277 //void G4StateManager::Pause(const char* msg)
278 //{
279 // G4String msgS = msg;
280 // Pause(msgS);
281 //}
282 //
283 //void G4StateManager::Pause(G4String msg)
284 //{
285 // G4UImanager::GetUIpointer()->PauseSession(msg);
286 //}
G4ApplicationState GetPreviousState() const
G4ApplicationState theCurrentState
G4ApplicationState thePreviousState
G4bool RegisterDependent(G4VStateDependent *aDependent, G4bool bottom=false)
G4int operator==(const G4StateManager &right) const
G4int operator!=(const G4StateManager &right) const
#define G4ThreadLocal
Definition: tls.hh:89
G4VExceptionHandler * exceptionHandler
int G4int
Definition: G4Types.hh:78
std::vector< G4VStateDependent * > theDependentsList
static G4StateManager * GetStateManager()
virtual G4bool Notify(G4ApplicationState requestedState)=0
G4bool SetNewState(G4ApplicationState requestedState)
G4VStateDependent * theBottomDependent
const char * msgptr
bool G4bool
Definition: G4Types.hh:79
G4ApplicationState GetCurrentState() const
void G4iosInitialization()
Definition: G4ios.cc:85
G4VStateDependent * RemoveDependent(const G4VStateDependent *aDependent)
void G4iosFinalization()
Definition: G4ios.cc:86
G4StateManager & operator=(const G4StateManager &right)
G4ApplicationState
G4String GetStateString(G4ApplicationState aState) const
static G4ThreadLocal G4StateManager * theStateManager
G4bool DeregisterDependent(G4VStateDependent *aDependent)