Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4StateManager class implementation << 27 // 26 // 28 // Authors: G.Cosmo, M.Asai - November 1996 << 27 // 29 // ------------------------------------------- << 28 // >> 29 // ------------------------------------------------------------ >> 30 // GEANT 4 class implementation file >> 31 // >> 32 // ---------------- G4StateManager ---------------- >> 33 // by Gabriele Cosmo, November 1996 >> 34 // ------------------------------------------------------------ 30 35 31 #include "G4StateManager.hh" 36 #include "G4StateManager.hh" 32 #include "G4ios.hh" 37 #include "G4ios.hh" 33 38 34 // Initialization of the static pointer of the 39 // Initialization of the static pointer of the single class instance 35 // 40 // 36 G4ThreadLocal G4StateManager* G4StateManager:: << 41 G4ThreadLocal G4StateManager* G4StateManager::theStateManager = 0; 37 G4int G4StateManager::verboseLevel << 42 G4int G4StateManager::verboseLevel = 0; 38 43 39 // ------------------------------------------- << 40 G4StateManager::G4StateManager() 44 G4StateManager::G4StateManager() >> 45 : theCurrentState(G4State_PreInit), >> 46 thePreviousState(G4State_PreInit), >> 47 theBottomDependent(0), >> 48 suppressAbortion(0), >> 49 msgptr(0), >> 50 exceptionHandler(0) 41 { 51 { 42 #ifdef G4MULTITHREADED 52 #ifdef G4MULTITHREADED 43 G4iosInitialization(); 53 G4iosInitialization(); 44 #endif 54 #endif 45 } 55 } 46 56 47 G4StateManager::~G4StateManager() 57 G4StateManager::~G4StateManager() 48 { 58 { 49 G4VStateDependent* state = nullptr; << 59 G4VStateDependent* state=0; 50 60 51 while(!theDependentsList.empty()) << 61 while (theDependentsList.size()>0) 52 { 62 { 53 state = theDependentsList.back(); 63 state = theDependentsList.back(); 54 theDependentsList.pop_back(); 64 theDependentsList.pop_back(); 55 for(auto i = theDependentsList.cbegin(); i << 65 for (std::vector<G4VStateDependent*>::iterator >> 66 i=theDependentsList.begin(); i!=theDependentsList.end();) 56 { 67 { 57 if(*i == state) << 68 if (*i==state) 58 { 69 { 59 i = theDependentsList.erase(i); 70 i = theDependentsList.erase(i); 60 } 71 } 61 else 72 else 62 { 73 { 63 ++i; 74 ++i; 64 } 75 } 65 } << 76 } 66 delete state; << 77 if ( state ) { delete state; } 67 } 78 } 68 theStateManager = nullptr; << 79 theStateManager = 0; 69 #ifdef G4MULTITHREADED_DEACTIVATE 80 #ifdef G4MULTITHREADED_DEACTIVATE 70 G4iosFinalization(); 81 G4iosFinalization(); 71 #endif 82 #endif 72 } 83 } 73 84 74 // ------------------------------------------- << 85 // ------------------------------------------------------------------------- 75 G4StateManager* G4StateManager::GetStateManage << 86 // No matter how copy-constructor and operators below are implemented ... >> 87 // just dummy implementations, since not relevant for the singleton and >> 88 // declared private. >> 89 // >> 90 G4StateManager::G4StateManager(const G4StateManager &right) >> 91 : theCurrentState(right.theCurrentState), >> 92 thePreviousState(right.thePreviousState), >> 93 theDependentsList(right.theDependentsList), >> 94 theBottomDependent(right.theBottomDependent), >> 95 suppressAbortion(right.suppressAbortion), >> 96 msgptr(right.msgptr), >> 97 exceptionHandler(right.exceptionHandler) 76 { 98 { 77 if(theStateManager == nullptr) << 78 { << 79 theStateManager = new G4StateManager; << 80 } << 81 return theStateManager; << 82 } 99 } 83 100 84 // ------------------------------------------- << 101 G4StateManager& 85 G4bool G4StateManager::RegisterDependent(G4VSt << 102 G4StateManager::operator=(const G4StateManager &right) 86 G4boo << 87 { 103 { 88 G4bool ack = true; << 104 if (&right == this) { return *this; } 89 if(!bottom) << 90 { << 91 theDependentsList.push_back(aDependent); << 92 } << 93 else << 94 { << 95 if(theBottomDependent != nullptr) << 96 { << 97 theDependentsList.push_back(theBottomDep << 98 } << 99 theBottomDependent = aDependent; << 100 } << 101 return ack; << 102 } << 103 105 104 // ------------------------------------------- << 106 theCurrentState = right.theCurrentState; 105 G4bool G4StateManager::DeregisterDependent(G4V << 107 thePreviousState = right.thePreviousState; 106 { << 108 theDependentsList = right.theDependentsList; 107 G4VStateDependent* tmp = nullptr; << 109 theBottomDependent = right.theBottomDependent; 108 for(auto i = theDependentsList.cbegin(); i ! << 110 suppressAbortion = right.suppressAbortion; 109 { << 111 msgptr = right.msgptr; 110 if(**i == *aDependent) << 112 exceptionHandler = right.exceptionHandler; 111 { << 113 112 tmp = *i; << 114 return *this; 113 i = theDependentsList.erase(i); << 114 } << 115 else << 116 { << 117 ++i; << 118 } << 119 } << 120 return (tmp != nullptr); << 121 } 115 } 122 116 123 // ------------------------------------------- << 117 G4bool 124 const G4ApplicationState& G4StateManager::GetC << 118 G4StateManager::operator==(const G4StateManager &right) const 125 { 119 { 126 return theCurrentState; << 120 return (this == &right); 127 } 121 } 128 122 129 // ------------------------------------------- << 123 G4bool 130 const G4ApplicationState& G4StateManager::GetP << 124 G4StateManager::operator!=(const G4StateManager &right) const 131 { 125 { 132 return thePreviousState; << 126 return (this != &right); 133 } 127 } >> 128 // >> 129 // ------------------------------------------------------------------------- 134 130 135 // ------------------------------------------- << 131 G4StateManager* 136 G4bool G4StateManager::SetNewState(const G4App << 132 G4StateManager::GetStateManager() 137 { 133 { 138 return SetNewState(requestedState, nullptr); << 134 if (!theStateManager) >> 135 { >> 136 theStateManager = new G4StateManager; >> 137 } >> 138 return theStateManager; 139 } 139 } 140 140 141 // ------------------------------------------- << 141 G4bool 142 G4bool G4StateManager::SetNewState(const G4App << 142 G4StateManager::RegisterDependent(G4VStateDependent* aDependent, G4bool bottom) 143 const char* << 144 { 143 { 145 if(requestedState == G4State_Abort && suppre << 144 G4bool ack=true; 146 { << 145 if(!bottom) 147 if(suppressAbortion == 2) << 148 { 146 { 149 return false; << 147 theDependentsList.push_back(aDependent); 150 } 148 } 151 if(theCurrentState == G4State_EventProc) << 149 else 152 { << 150 { 153 return false; << 151 if(theBottomDependent) >> 152 { >> 153 theDependentsList.push_back(theBottomDependent); >> 154 } >> 155 theBottomDependent = aDependent; 154 } 156 } 155 } << 157 return ack; 156 msgptr = msg; << 158 } 157 std::size_t i = 0; << 158 G4bool ack = true; << 159 G4ApplicationState savedState = thePreviousS << 160 thePreviousState = theCurrentSt << 161 << 162 while((ack) && (i < theDependentsList.size() << 163 { << 164 ack = theDependentsList[i]->Notify(request << 165 ++i; << 166 } << 167 if(theBottomDependent != nullptr) << 168 { << 169 ack = theBottomDependent->Notify(requested << 170 } << 171 159 172 if(!ack) << 160 G4bool 173 { << 161 G4StateManager::DeregisterDependent(G4VStateDependent* aDependent) 174 thePreviousState = savedState; << 162 { 175 } << 163 G4VStateDependent* tmp = 0; 176 else << 164 for (std::vector<G4VStateDependent*>::iterator i=theDependentsList.begin(); >> 165 i!=theDependentsList.end();) 177 { 166 { 178 theCurrentState = requestedState; << 167 if (**i==*aDependent) 179 if(verboseLevel > 0) << 180 { 168 { 181 G4cout << "#### G4StateManager::SetNewSt << 169 tmp = *i; 182 << GetStateString(thePreviousStat << 170 i = theDependentsList.erase(i); 183 << GetStateString(requestedState) << 171 } >> 172 else >> 173 { >> 174 ++i; 184 } 175 } 185 } 176 } 186 msgptr = nullptr; << 177 return (tmp != 0); 187 return ack; << 188 } 178 } 189 179 190 // ------------------------------------------- << 180 const G4ApplicationState& 191 G4VStateDependent* G4StateManager::RemoveDepen << 181 G4StateManager::GetCurrentState() const 192 const G4VStateDependent* aDependent) << 193 { 182 { 194 G4VStateDependent* tmp = nullptr; << 183 return theCurrentState; 195 for(auto i = theDependentsList.cbegin(); i ! << 184 } >> 185 >> 186 const G4ApplicationState& >> 187 G4StateManager::GetPreviousState() const >> 188 { >> 189 return thePreviousState; >> 190 } >> 191 >> 192 G4bool >> 193 G4StateManager::SetNewState(const G4ApplicationState& requestedState) >> 194 { return SetNewState(requestedState,0); } >> 195 >> 196 G4bool >> 197 G4StateManager::SetNewState(const G4ApplicationState& requestedState, >> 198 const char* msg) >> 199 { >> 200 if(requestedState==G4State_Abort && suppressAbortion>0) >> 201 { >> 202 if(suppressAbortion==2) { return false; } >> 203 if(theCurrentState==G4State_EventProc) { return false; } >> 204 } >> 205 msgptr = msg; >> 206 size_t i=0; >> 207 G4bool ack = true; >> 208 G4ApplicationState savedState = thePreviousState; >> 209 thePreviousState = theCurrentState; >> 210 >> 211 while ((ack) && (i<theDependentsList.size())) >> 212 { >> 213 ack = theDependentsList[i]->Notify(requestedState); >> 214 i++; >> 215 } >> 216 if(theBottomDependent) >> 217 { >> 218 ack = theBottomDependent->Notify(requestedState); >> 219 } >> 220 >> 221 if(!ack) >> 222 { thePreviousState = savedState; } >> 223 else >> 224 { >> 225 theCurrentState = requestedState; >> 226 if(verboseLevel>0) >> 227 { >> 228 G4cout<<"#### G4StateManager::SetNewState from " >> 229 <<GetStateString(thePreviousState)<<" to " >> 230 <<GetStateString(requestedState)<<G4endl; >> 231 } >> 232 } >> 233 msgptr = 0; >> 234 return ack; >> 235 } >> 236 >> 237 G4VStateDependent* >> 238 G4StateManager::RemoveDependent(const G4VStateDependent* aDependent) >> 239 { >> 240 G4VStateDependent* tmp = 0; >> 241 for (std::vector<G4VStateDependent*>::iterator i=theDependentsList.begin(); >> 242 i!=theDependentsList.end();) 196 { 243 { 197 if(**i == *aDependent) << 244 if (**i==*aDependent) 198 { 245 { 199 tmp = *i; 246 tmp = *i; 200 i = theDependentsList.erase(i); << 247 i = theDependentsList.erase(i); 201 } 248 } 202 else 249 else 203 { 250 { 204 ++i; 251 ++i; 205 } 252 } 206 } 253 } 207 return tmp; 254 return tmp; 208 } 255 } 209 256 210 // ------------------------------------------- << 257 G4String 211 G4String G4StateManager::GetStateString(const << 258 G4StateManager::GetStateString(const G4ApplicationState& aState) const 212 { 259 { 213 G4String stateName; 260 G4String stateName; 214 switch(aState) 261 switch(aState) 215 { 262 { 216 case G4State_PreInit: 263 case G4State_PreInit: 217 stateName = "PreInit"; << 264 stateName = "PreInit"; break; 218 break; << 219 case G4State_Init: 265 case G4State_Init: 220 stateName = "Init"; << 266 stateName = "Init"; break; 221 break; << 222 case G4State_Idle: 267 case G4State_Idle: 223 stateName = "Idle"; << 268 stateName = "Idle"; break; 224 break; << 225 case G4State_GeomClosed: 269 case G4State_GeomClosed: 226 stateName = "GeomClosed"; << 270 stateName = "GeomClosed"; break; 227 break; << 228 case G4State_EventProc: 271 case G4State_EventProc: 229 stateName = "EventProc"; << 272 stateName = "EventProc"; break; 230 break; << 231 case G4State_Quit: 273 case G4State_Quit: 232 stateName = "Quit"; << 274 stateName = "Quit"; break; 233 break; << 234 case G4State_Abort: 275 case G4State_Abort: 235 stateName = "Abort"; << 276 stateName = "Abort"; break; 236 break; << 237 default: 277 default: 238 stateName = "Unknown"; << 278 stateName = "Unknown"; break; 239 break; << 240 } 279 } 241 return stateName; 280 return stateName; 242 } 281 } 243 282 244 // ------------------------------------------- << 283 void 245 void G4StateManager::SetVerboseLevel(G4int val << 284 G4StateManager::SetVerboseLevel(G4int val) 246 << 247 // ------------------------------------------- << 248 void G4StateManager::NotifyDeletion(const G4Ev << 249 { << 250 for(auto& d : theDependentsList) << 251 { d->NotifyDeletion(evt); } << 252 } << 253 << 254 // ------------------------------------------- << 255 void G4StateManager::NotifyDeletion(const G4Ru << 256 { 285 { 257 for(auto& d : theDependentsList) << 286 verboseLevel = val; 258 { d->NotifyDeletion(rn); } << 259 } 287 } 260 288