Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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$
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 
39 // Initialization of the static pointer of the single class instance
40 //
41 G4StateManager* G4StateManager::theStateManager = 0;
42 
44  : theCurrentState(G4State_PreInit),
45  thePreviousState(G4State_PreInit),
46  theBottomDependent(0),
47  suppressAbortion(0),
48  msgptr(0),
49  exceptionHandler(0)
50 {
51 }
52 
54 {
55  G4VStateDependent* state=0;
56 
57  while (theDependentsList.size()>0)
58  {
59  state = theDependentsList.back();
60  theDependentsList.pop_back();
61  for (std::vector<G4VStateDependent*>::iterator
62  i=theDependentsList.begin(); i!=theDependentsList.end();)
63  {
64  if (*i==state)
65  {
66  i = theDependentsList.erase(i);
67  }
68  else
69  {
70  ++i;
71  }
72  }
73  if ( state ) { delete state; }
74  }
75 }
76 
77 // -------------------------------------------------------------------------
78 // No matter how copy-constructor and operators below are implemented ...
79 // just dummy implementations, since not relevant for the singleton and
80 // declared private.
81 //
83  : theCurrentState(right.theCurrentState),
84  thePreviousState(right.thePreviousState),
85  theDependentsList(right.theDependentsList),
86  theBottomDependent(right.theBottomDependent),
87  suppressAbortion(right.suppressAbortion),
88  msgptr(right.msgptr),
89  exceptionHandler(right.exceptionHandler)
90 {
91 }
92 
94 G4StateManager::operator=(const G4StateManager &right)
95 {
96  if (&right == this) { return *this; }
97 
98  theCurrentState = right.theCurrentState;
99  thePreviousState = right.thePreviousState;
100  theDependentsList = right.theDependentsList;
101  theBottomDependent = right.theBottomDependent;
102  suppressAbortion = right.suppressAbortion;
103  msgptr = right.msgptr;
104  exceptionHandler = right.exceptionHandler;
105 
106  return *this;
107 }
108 
109 G4int
110 G4StateManager::operator==(const G4StateManager &right) const
111 {
112  return (this == &right);
113 }
114 
115 G4int
116 G4StateManager::operator!=(const G4StateManager &right) const
117 {
118  return (this != &right);
119 }
120 //
121 // -------------------------------------------------------------------------
122 
125 {
126  if (!theStateManager)
127  {
128  theStateManager = new G4StateManager;
129  }
130  return theStateManager;
131 }
132 
133 G4bool
135 {
136  G4bool ack=true;
137  if(!bottom)
138  {
139  theDependentsList.push_back(aDependent);
140  }
141  else
142  {
143  if(theBottomDependent)
144  {
145  theDependentsList.push_back(theBottomDependent);
146  }
147  theBottomDependent = aDependent;
148  }
149  return ack;
150 }
151 
152 G4bool
154 {
155  G4VStateDependent* tmp = 0;
156  for (std::vector<G4VStateDependent*>::iterator i=theDependentsList.begin();
157  i!=theDependentsList.end();)
158  {
159  if (**i==*aDependent)
160  {
161  tmp = *i;
162  i = theDependentsList.erase(i);
163  }
164  else
165  {
166  ++i;
167  }
168  }
169  return (tmp != 0);
170 }
171 
174 {
175  return theCurrentState;
176 }
177 
180 {
181  return thePreviousState;
182 }
183 
184 G4bool
186 { return SetNewState(requestedState,0); }
187 
188 G4bool
189 G4StateManager::SetNewState(G4ApplicationState requestedState, const char* msg)
190 {
191  if(requestedState==G4State_Abort && suppressAbortion>0)
192  {
193  if(suppressAbortion==2) { return false; }
194  if(theCurrentState==G4State_EventProc) { return false; }
195  }
196  msgptr = msg;
197  size_t i=0;
198  G4bool ack = true;
199  G4ApplicationState savedState = thePreviousState;
200  thePreviousState = theCurrentState;
201  while ((ack) && (i<theDependentsList.size()))
202  {
203  ack = theDependentsList[i]->Notify(requestedState);
204  i++;
205  }
206  if(theBottomDependent)
207  {
208  ack = theBottomDependent->Notify(requestedState);
209  }
210 
211  if(!ack)
212  { thePreviousState = savedState; }
213  else
214  { theCurrentState = requestedState; }
215  msgptr = 0;
216  return ack;
217 }
218 
221 {
222  G4VStateDependent* tmp = 0;
223  for (std::vector<G4VStateDependent*>::iterator i=theDependentsList.begin();
224  i!=theDependentsList.end();)
225  {
226  if (**i==*aDependent)
227  {
228  tmp = *i;
229  i = theDependentsList.erase(i);
230  }
231  else
232  {
233  ++i;
234  }
235  }
236  return tmp;
237 }
238 
239 G4String
241 {
242  G4String stateName;
243  switch(aState)
244  {
245  case G4State_PreInit:
246  stateName = "PreInit"; break;
247  case G4State_Init:
248  stateName = "Init"; break;
249  case G4State_Idle:
250  stateName = "Idle"; break;
251  case G4State_GeomClosed:
252  stateName = "GeomClosed"; break;
253  case G4State_EventProc:
254  stateName = "EventProc"; break;
255  case G4State_Quit:
256  stateName = "Quit"; break;
257  case G4State_Abort:
258  stateName = "Abort"; break;
259  default:
260  stateName = "Unknown"; break;
261  }
262  return stateName;
263 }
264 
265 //void G4StateManager::Pause()
266 //{
267 // Pause("G4_pause> ");
268 //}
269 //
270 //void G4StateManager::Pause(const char* msg)
271 //{
272 // G4String msgS = msg;
273 // Pause(msgS);
274 //}
275 //
276 //void G4StateManager::Pause(G4String msg)
277 //{
278 // G4UImanager::GetUIpointer()->PauseSession(msg);
279 //}