G4StateManager.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // 
00030 // ------------------------------------------------------------
00031 //      GEANT 4 class implementation file 
00032 //
00033 //      ---------------- G4StateManager ----------------
00034 //             by Gabriele Cosmo, November 1996
00035 // ------------------------------------------------------------
00036 
00037 #include "G4StateManager.hh"
00038 
00039 // Initialization of the static pointer of the single class instance
00040 //
00041 G4StateManager* G4StateManager::theStateManager = 0;
00042 
00043 G4StateManager::G4StateManager()
00044  : theCurrentState(G4State_PreInit),
00045    thePreviousState(G4State_PreInit),
00046    theBottomDependent(0),
00047    suppressAbortion(0),
00048    msgptr(0),
00049    exceptionHandler(0)
00050 {
00051 }
00052 
00053 G4StateManager::~G4StateManager()
00054 {
00055   G4VStateDependent* state=0;
00056 
00057   while (theDependentsList.size()>0)
00058   {
00059     state = theDependentsList.back();
00060     theDependentsList.pop_back();
00061     for (std::vector<G4VStateDependent*>::iterator
00062          i=theDependentsList.begin(); i!=theDependentsList.end();)
00063     {
00064       if (*i==state)
00065       {
00066         i = theDependentsList.erase(i);
00067       }
00068       else
00069       {
00070         ++i;
00071       }
00072     } 
00073     if ( state )  { delete state; }
00074   } 
00075 }
00076 
00077 // -------------------------------------------------------------------------
00078 // No matter how copy-constructor and operators below are implemented ...
00079 // just dummy implementations, since not relevant for the singleton and
00080 // declared private.
00081 //
00082 G4StateManager::G4StateManager(const G4StateManager &right)
00083   : theCurrentState(right.theCurrentState),
00084     thePreviousState(right.thePreviousState),
00085     theDependentsList(right.theDependentsList),
00086     theBottomDependent(right.theBottomDependent),
00087     suppressAbortion(right.suppressAbortion),
00088     msgptr(right.msgptr),
00089     exceptionHandler(right.exceptionHandler)
00090 {
00091 }
00092 
00093 G4StateManager&
00094 G4StateManager::operator=(const G4StateManager &right)
00095 {
00096    if (&right == this)  { return *this; }
00097 
00098    theCurrentState = right.theCurrentState;
00099    thePreviousState = right.thePreviousState;
00100    theDependentsList = right.theDependentsList;
00101    theBottomDependent = right.theBottomDependent;
00102    suppressAbortion = right.suppressAbortion;
00103    msgptr = right.msgptr;
00104    exceptionHandler = right.exceptionHandler;
00105 
00106    return *this;
00107 }
00108 
00109 G4int
00110 G4StateManager::operator==(const G4StateManager &right) const
00111 {
00112    return (this == &right);
00113 }
00114 
00115 G4int
00116 G4StateManager::operator!=(const G4StateManager &right) const
00117 {
00118    return (this != &right);
00119 }
00120 //
00121 // -------------------------------------------------------------------------
00122 
00123 G4StateManager*
00124 G4StateManager::GetStateManager()
00125 {
00126     if (!theStateManager)
00127     {
00128       theStateManager = new G4StateManager;
00129     }
00130     return theStateManager;    
00131 }
00132 
00133 G4bool
00134 G4StateManager::RegisterDependent(G4VStateDependent* aDependent, G4bool bottom)
00135 {
00136     G4bool ack=true;
00137     if(!bottom)
00138     {
00139       theDependentsList.push_back(aDependent);
00140     }
00141     else
00142     { 
00143       if(theBottomDependent)
00144       {
00145         theDependentsList.push_back(theBottomDependent);
00146       }
00147       theBottomDependent = aDependent;
00148     }
00149     return ack;
00150 }
00151 
00152 G4bool
00153 G4StateManager::DeregisterDependent(G4VStateDependent* aDependent)
00154 {
00155   G4VStateDependent* tmp = 0;
00156   for (std::vector<G4VStateDependent*>::iterator i=theDependentsList.begin();
00157        i!=theDependentsList.end();)
00158   {
00159     if (**i==*aDependent) 
00160     {
00161       tmp = *i;
00162       i = theDependentsList.erase(i);
00163     }
00164     else
00165     {
00166       ++i;
00167     }
00168   }
00169   return (tmp != 0);
00170 }
00171 
00172 G4ApplicationState
00173 G4StateManager::GetCurrentState() const
00174 {
00175    return theCurrentState;
00176 }
00177 
00178 G4ApplicationState
00179 G4StateManager::GetPreviousState() const
00180 {
00181    return thePreviousState;
00182 }
00183 
00184 G4bool
00185 G4StateManager::SetNewState(G4ApplicationState requestedState)
00186 { return SetNewState(requestedState,0); }
00187 
00188 G4bool
00189 G4StateManager::SetNewState(G4ApplicationState requestedState, const char* msg)
00190 {
00191    if(requestedState==G4State_Abort && suppressAbortion>0)
00192    {
00193      if(suppressAbortion==2)  { return false; }
00194      if(theCurrentState==G4State_EventProc)  { return false; }
00195    }
00196    msgptr = msg;
00197    size_t i=0;
00198    G4bool ack = true;
00199    G4ApplicationState savedState = thePreviousState;
00200    thePreviousState = theCurrentState;
00201    while ((ack) && (i<theDependentsList.size()))
00202    {
00203      ack = theDependentsList[i]->Notify(requestedState);
00204      i++;
00205    }
00206    if(theBottomDependent)
00207    {
00208      ack = theBottomDependent->Notify(requestedState);
00209    }
00210 
00211    if(!ack)
00212    { thePreviousState = savedState; }
00213    else
00214    { theCurrentState = requestedState; }
00215    msgptr = 0;
00216    return ack;
00217 }
00218 
00219 G4VStateDependent*
00220 G4StateManager::RemoveDependent(const G4VStateDependent* aDependent)
00221 {
00222   G4VStateDependent* tmp = 0;
00223   for (std::vector<G4VStateDependent*>::iterator i=theDependentsList.begin();
00224        i!=theDependentsList.end();)
00225   {
00226     if (**i==*aDependent) 
00227     {
00228       tmp = *i;
00229       i = theDependentsList.erase(i);
00230     }
00231     else
00232     {
00233       ++i;
00234     }
00235   }
00236   return tmp;
00237 }
00238 
00239 G4String
00240 G4StateManager::GetStateString(G4ApplicationState aState) const
00241 {
00242   G4String stateName;
00243   switch(aState)
00244   {
00245     case G4State_PreInit:
00246      stateName = "PreInit"; break;
00247     case G4State_Init:
00248      stateName = "Init"; break;
00249     case G4State_Idle:
00250      stateName = "Idle"; break;
00251     case G4State_GeomClosed:
00252      stateName = "GeomClosed"; break;
00253     case G4State_EventProc:
00254      stateName = "EventProc"; break;
00255     case G4State_Quit:
00256      stateName = "Quit"; break;
00257     case G4State_Abort:
00258      stateName = "Abort"; break;
00259     default:
00260      stateName = "Unknown"; break;
00261   }
00262   return stateName;
00263 }
00264 
00265 //void G4StateManager::Pause()
00266 //{
00267 //  Pause("G4_pause> ");
00268 //}
00269 //
00270 //void G4StateManager::Pause(const char* msg)
00271 //{
00272 //  G4String msgS = msg;
00273 //  Pause(msgS);
00274 //}
00275 //
00276 //void G4StateManager::Pause(G4String msg)
00277 //{
00278 //  G4UImanager::GetUIpointer()->PauseSession(msg);
00279 //}

Generated on Mon May 27 17:49:53 2013 for Geant4 by  doxygen 1.4.7