source: trunk/source/global/management/src/G4StateManager.cc @ 833

Last change on this file since 833 was 833, checked in by garnier, 16 years ago

import all except CVS

File size: 7.2 KB
Line 
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,v 1.13 2006/11/23 00:41:56 asaim Exp $
28// GEANT4 tag $Name:  $
29//
30//
31// ------------------------------------------------------------
32//      GEANT 4 class implementation file
33//
34//      ---------------- G4StateManager ----------------
35//             by Gabriele Cosmo, November 1996
36// ------------------------------------------------------------
37
38#include "G4StateManager.hh"
39
40// Initialization of the static pointer of the single class instance
41//
42G4StateManager* G4StateManager::theStateManager = 0;
43
44G4StateManager::G4StateManager()
45 : theCurrentState(G4State_PreInit),
46   thePreviousState(G4State_PreInit),
47   theBottomDependent(0),
48   suppressAbortion(0),
49   msgptr(0),
50   exceptionHandler(0)
51{
52}
53
54G4StateManager::~G4StateManager()
55{
56  G4VStateDependent* state=0;
57
58  while (theDependentsList.size()>0)
59  {
60    state = theDependentsList.back();
61    theDependentsList.pop_back();
62    for (std::vector<G4VStateDependent*>::iterator
63         i=theDependentsList.begin(); i!=theDependentsList.end(); i++)
64    {
65      if (*i==state)
66      {
67        theDependentsList.erase(i);
68        i--;
69      }
70    } 
71    if ( state )  { delete state; }
72  } 
73}
74
75// -------------------------------------------------------------------------
76// No matter how copy-constructor and operators below are implemented ...
77// just dummy implementations, since not relevant for the singleton and
78// declared private.
79//
80G4StateManager::G4StateManager(const G4StateManager &right)
81  : theCurrentState(right.theCurrentState),
82    thePreviousState(right.thePreviousState),
83    theDependentsList(right.theDependentsList),
84    theBottomDependent(right.theBottomDependent),
85    suppressAbortion(right.suppressAbortion),
86    msgptr(right.msgptr),
87    exceptionHandler(right.exceptionHandler)
88{
89}
90
91G4StateManager&
92G4StateManager::operator=(const G4StateManager &right)
93{
94   if (&right == this)  { return *this; }
95
96   theCurrentState = right.theCurrentState;
97   thePreviousState = right.thePreviousState;
98   theDependentsList = right.theDependentsList;
99   theBottomDependent = right.theBottomDependent;
100   suppressAbortion = right.suppressAbortion;
101   msgptr = right.msgptr;
102   exceptionHandler = right.exceptionHandler;
103
104   return *this;
105}
106
107G4int
108G4StateManager::operator==(const G4StateManager &right) const
109{
110   return (this == &right);
111}
112
113G4int
114G4StateManager::operator!=(const G4StateManager &right) const
115{
116   return (this != &right);
117}
118//
119// -------------------------------------------------------------------------
120
121G4StateManager*
122G4StateManager::GetStateManager()
123{
124    if (!theStateManager)
125    {
126      theStateManager = new G4StateManager;
127    }
128    return theStateManager;   
129}
130
131G4bool
132G4StateManager::RegisterDependent(G4VStateDependent* aDependent, G4bool bottom)
133{
134    G4bool ack=true;
135    if(!bottom)
136    {
137      theDependentsList.push_back(aDependent);
138    }
139    else
140    { 
141      if(theBottomDependent)
142      {
143        theDependentsList.push_back(theBottomDependent);
144      }
145      theBottomDependent = aDependent;
146    }
147    return ack;
148}
149
150G4bool
151G4StateManager::DeregisterDependent(G4VStateDependent* aDependent)
152{
153  G4VStateDependent* tmp = 0;
154  for (std::vector<G4VStateDependent*>::iterator i=theDependentsList.begin();
155       i!=theDependentsList.end(); i++)
156    {
157      if (**i==*aDependent) 
158        {
159          tmp = *i;
160          theDependentsList.erase(i);
161        } 
162    }
163  return (tmp != 0);
164}
165
166G4ApplicationState
167G4StateManager::GetCurrentState() const
168{
169   return theCurrentState;
170}
171
172G4ApplicationState
173G4StateManager::GetPreviousState() const
174{
175   return thePreviousState;
176}
177
178G4bool
179G4StateManager::SetNewState(G4ApplicationState requestedState)
180{ return SetNewState(requestedState,0); }
181
182G4bool
183G4StateManager::SetNewState(G4ApplicationState requestedState, const char* msg)
184{
185   if(requestedState==G4State_Abort && suppressAbortion>0)
186   {
187     if(suppressAbortion==2)  { return false; }
188     if(theCurrentState==G4State_EventProc)  { return false; }
189   }
190   msgptr = msg;
191   size_t i=0;
192   G4bool ack = true;
193   G4ApplicationState savedState = thePreviousState;
194   thePreviousState = theCurrentState;
195   while ((ack) && (i<theDependentsList.size()))
196   {
197     ack = theDependentsList[i]->Notify(requestedState);
198     i++;
199   }
200   if(theBottomDependent)
201   {
202     ack = theBottomDependent->Notify(requestedState);
203   }
204
205   if(!ack)
206   { thePreviousState = savedState; }
207   else
208   { theCurrentState = requestedState; }
209   msgptr = 0;
210   return ack;
211}
212
213G4VStateDependent*
214G4StateManager::RemoveDependent(const G4VStateDependent* aDependent)
215{
216  G4VStateDependent* tmp = 0;
217  for (std::vector<G4VStateDependent*>::iterator i=theDependentsList.begin();
218       i!=theDependentsList.end(); i++)
219    {
220      if (**i==*aDependent) 
221        {
222          tmp = *i;
223          theDependentsList.erase(i);
224        } 
225    }
226  return tmp;
227}
228
229G4String
230G4StateManager::GetStateString(G4ApplicationState aState) const
231{
232  G4String stateName;
233  switch(aState)
234  {
235    case G4State_PreInit:
236     stateName = "PreInit"; break;
237    case G4State_Init:
238     stateName = "Init"; break;
239    case G4State_Idle:
240     stateName = "Idle"; break;
241    case G4State_GeomClosed:
242     stateName = "GeomClosed"; break;
243    case G4State_EventProc:
244     stateName = "EventProc"; break;
245    case G4State_Quit:
246     stateName = "Quit"; break;
247    case G4State_Abort:
248     stateName = "Abort"; break;
249    default:
250     stateName = "Unknown"; break;
251  }
252  return stateName;
253}
254
255//void G4StateManager::Pause()
256//{
257//  Pause("G4_pause> ");
258//}
259//
260//void G4StateManager::Pause(const char* msg)
261//{
262//  G4String msgS = msg;
263//  Pause(msgS);
264//}
265//
266//void G4StateManager::Pause(G4String msg)
267//{
268//  G4UImanager::GetUIpointer()->PauseSession(msg);
269//}
Note: See TracBrowser for help on using the repository browser.