Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 // G4UImanager << 27 // 23 // 28 // Author: Makoto Asai, 1997 << 24 // $Id: G4UImanager.cc,v 1.20 2001/11/24 18:37:11 asaim Exp $ 29 // ------------------------------------------- << 25 // GEANT4 tag $Name: geant4-04-00 $ >> 26 // >> 27 // >> 28 // --------------------------------------------------------------------- 30 29 31 #include "G4UImanager.hh" 30 #include "G4UImanager.hh" 32 << 33 #include "G4LocalThreadCoutMessenger.hh" << 34 #include "G4MTcoutDestination.hh" << 35 #include "G4StateManager.hh" << 36 #include "G4Threading.hh" << 37 #include "G4Tokenizer.hh" << 38 #include "G4UIaliasList.hh" << 39 #include "G4UIbatch.hh" << 40 #include "G4UIbridge.hh" << 41 #include "G4UIcommand.hh" << 42 #include "G4UIcommandTree.hh" 31 #include "G4UIcommandTree.hh" 43 #include "G4UIcontrolMessenger.hh" << 32 #include "G4UIcommand.hh" 44 #include "G4UIsession.hh" 33 #include "G4UIsession.hh" >> 34 #include "G4UIbatch.hh" >> 35 #include "G4UIcontrolMessenger.hh" 45 #include "G4UnitsMessenger.hh" 36 #include "G4UnitsMessenger.hh" 46 #include "G4ios.hh" 37 #include "G4ios.hh" >> 38 #include "G4strstreambuf.hh" >> 39 #include "G4StateManager.hh" >> 40 #include "G4UIaliasList.hh" >> 41 #include "G4Tokenizer.hh" 47 42 48 #include <fstream> << 43 #include "g4std/strstream" 49 #include <sstream> << 50 << 51 G4bool G4UImanager::doublePrecisionStr = false << 52 G4int G4UImanager::igThreadID = -1; << 53 << 54 // ------------------------------------------- << 55 G4UImanager*& G4UImanager::fUImanager() << 56 { << 57 G4ThreadLocalStatic G4UImanager* _instance = << 58 return _instance; << 59 } << 60 44 61 // ------------------------------------------- << 62 G4bool& G4UImanager::fUImanagerHasBeenKilled() << 63 { << 64 G4ThreadLocalStatic G4bool _instance = false << 65 return _instance; << 66 } << 67 45 68 // ------------------------------------------- << 46 G4UImanager * G4UImanager::fUImanager = 0; 69 G4UImanager*& G4UImanager::fMasterUImanager() << 47 G4bool G4UImanager::fUImanagerHasBeenKilled = false; 70 { << 71 static G4UImanager* _instance = nullptr; << 72 return _instance; << 73 } << 74 48 75 // ------------------------------------------- << 49 G4UImanager * G4UImanager::GetUIpointer() 76 G4UImanager* G4UImanager::GetUIpointer() << 77 { 50 { 78 if (fUImanager() == nullptr) { << 51 if(!fUImanager) 79 if (!fUImanagerHasBeenKilled()) { << 52 { 80 fUImanager() = new G4UImanager; << 53 if(!fUImanagerHasBeenKilled) 81 fUImanager()->CreateMessenger(); << 54 { >> 55 fUImanager = new G4UImanager; >> 56 fUImanager->CreateMessenger(); 82 } 57 } 83 } 58 } 84 return fUImanager(); << 59 return fUImanager; 85 } 60 } 86 61 87 // ------------------------------------------- << 62 G4UImanager::G4UImanager() 88 G4UImanager* G4UImanager::GetMasterUIpointer() << 63 :G4VStateDependent(true) 89 { << 90 return fMasterUImanager(); << 91 } << 92 << 93 // ------------------------------------------- << 94 G4UImanager::G4UImanager() : G4VStateDependent << 95 { 64 { >> 65 savedCommand = 0; 96 treeTop = new G4UIcommandTree("/"); 66 treeTop = new G4UIcommandTree("/"); 97 aliasList = new G4UIaliasList; 67 aliasList = new G4UIaliasList; >> 68 G4String nullString; >> 69 savedParameters = nullString; >> 70 verboseLevel = 0; >> 71 saveHistory = false; >> 72 session = NULL; 98 SetCoutDestination(session); 73 SetCoutDestination(session); 99 commandStack = new std::vector<G4String>; << 74 pauseAtBeginOfEvent = false; >> 75 pauseAtEndOfEvent = false; 100 } 76 } 101 77 102 // ------------------------------------------- << 103 void G4UImanager::CreateMessenger() 78 void G4UImanager::CreateMessenger() 104 { 79 { 105 UImessenger = new G4UIcontrolMessenger; 80 UImessenger = new G4UIcontrolMessenger; 106 UnitsMessenger = new G4UnitsMessenger; 81 UnitsMessenger = new G4UnitsMessenger; 107 CoutMessenger = new G4LocalThreadCoutMesseng << 108 } 82 } 109 83 110 // ------------------------------------------- << 111 G4UImanager::~G4UImanager() 84 G4UImanager::~G4UImanager() 112 { 85 { 113 if (bridges != nullptr) { << 86 SetCoutDestination(NULL); 114 for (auto bridge : *bridges) { << 115 delete bridge; << 116 } << 117 delete bridges; << 118 } << 119 SetCoutDestination(nullptr); << 120 histVec.clear(); 87 histVec.clear(); 121 if (saveHistory) { << 88 if(saveHistory) historyFile.close(); 122 historyFile.close(); << 123 } << 124 delete CoutMessenger; << 125 delete UnitsMessenger; << 126 delete UImessenger; 89 delete UImessenger; >> 90 delete UnitsMessenger; 127 delete treeTop; 91 delete treeTop; 128 delete aliasList; 92 delete aliasList; 129 fUImanagerHasBeenKilled() = true; << 93 fUImanagerHasBeenKilled = true; 130 fUImanager() = nullptr; << 94 fUImanager = NULL; 131 if (commandStack != nullptr) { << 132 commandStack->clear(); << 133 delete commandStack; << 134 } << 135 if (threadID >= 0) { << 136 delete threadCout; << 137 G4iosFinalization(); << 138 threadID = -1; << 139 } << 140 } << 141 << 142 // ------------------------------------------- << 143 void G4UImanager::UseDoublePrecisionStr(G4bool << 144 { << 145 doublePrecisionStr = val; << 146 } 95 } 147 96 148 // ------------------------------------------- << 97 G4UImanager::G4UImanager(const G4UImanager &right) { } 149 G4bool G4UImanager::DoublePrecisionStr() << 98 const G4UImanager & G4UImanager::operator=(const G4UImanager &right) 150 { << 99 { return right; } 151 return doublePrecisionStr; << 100 G4int G4UImanager::operator==(const G4UImanager &right) const 152 } << 101 { return false; } >> 102 G4int G4UImanager::operator!=(const G4UImanager &right) const >> 103 { return true; } 153 104 154 // ------------------------------------------- << 105 G4String G4UImanager::GetCurrentValues(const char * aCommand) 155 G4String G4UImanager::GetCurrentValues(const c << 156 { 106 { 157 G4String theCommand = aCommand; 107 G4String theCommand = aCommand; 158 savedCommand = treeTop->FindPath(theCommand) << 108 savedCommand = treeTop->FindPath( theCommand ); 159 if (savedCommand == nullptr) { << 109 if( savedCommand == NULL ) >> 110 { 160 G4cerr << "command not found" << G4endl; 111 G4cerr << "command not found" << G4endl; 161 return G4String(); 112 return G4String(); 162 } 113 } 163 return savedCommand->GetCurrentValue(); 114 return savedCommand->GetCurrentValue(); 164 } 115 } 165 116 166 // ------------------------------------------- << 117 G4String G4UImanager::GetCurrentStringValue(const char * aCommand, 167 G4String G4UImanager::GetCurrentStringValue(co << 118 G4int parameterNumber, G4bool reGet) 168 G4 << 169 { 119 { 170 if (reGet || savedCommand == nullptr) { << 120 if(reGet || savedCommand == NULL) 171 savedParameters = GetCurrentValues(aComman << 121 { >> 122 savedParameters = GetCurrentValues( aCommand ); 172 } 123 } 173 G4Tokenizer savedToken(savedParameters); << 124 G4Tokenizer savedToken( savedParameters ); 174 G4String token; 125 G4String token; 175 for (G4int i_thParameter = 0; i_thParameter << 126 for(G4int i_thParameter=0;i_thParameter<parameterNumber;i_thParameter++) >> 127 { 176 token = savedToken(); 128 token = savedToken(); 177 if (token.empty()) { << 129 if( token.isNull() ) return G4String(); 178 return G4String(); << 130 if( token[(size_t)0] == '"' ) 179 } << 131 { 180 if (token[(size_t)0] == '"') { << 181 token.append(" "); 132 token.append(" "); 182 token.append(savedToken("\"")); 133 token.append(savedToken("\"")); 183 } 134 } 184 } 135 } 185 return token; 136 return token; 186 } 137 } 187 138 188 // ------------------------------------------- << 139 G4String G4UImanager::GetCurrentStringValue(const char * aCommand, 189 G4String G4UImanager::GetCurrentStringValue(co << 140 const char * aParameterName, G4bool reGet) 190 G4 << 141 { 191 { << 142 if(reGet || savedCommand == NULL) 192 if (reGet || savedCommand == nullptr) { << 143 { 193 G4String parameterValues = GetCurrentValue << 144 G4String parameterValues = GetCurrentValues( aCommand ); 194 } << 145 } 195 for (G4int i = 0; i < (G4int)savedCommand->G << 146 for(G4int i=0;i<savedCommand->GetParameterEntries();i++) 196 if (aParameterName == savedCommand->GetPar << 147 { 197 return GetCurrentStringValue(aCommand, i << 148 if( aParameterName == 198 } << 149 savedCommand->GetParameter(i)->GetParameterName() ) >> 150 return GetCurrentStringValue(aCommand,i+1,false); 199 } 151 } 200 return G4String(); 152 return G4String(); 201 } 153 } 202 154 203 // ------------------------------------------- << 155 G4int G4UImanager::GetCurrentIntValue(const char * aCommand, 204 G4int G4UImanager::GetCurrentIntValue(const ch << 156 const char * aParameterName, G4bool reGet) 205 G4bool r << 206 { 157 { 207 G4String targetParameter = GetCurrentStringV << 158 G4String targetParameter = >> 159 GetCurrentStringValue( aCommand, aParameterName, reGet ); 208 G4int value; 160 G4int value; 209 const char* t = targetParameter; 161 const char* t = targetParameter; 210 std::istringstream is(t); << 162 G4std::istrstream is((char*)t); 211 is >> value; 163 is >> value; 212 return value; 164 return value; 213 } 165 } 214 166 215 // ------------------------------------------- << 167 G4int G4UImanager::GetCurrentIntValue(const char * aCommand, 216 G4int G4UImanager::GetCurrentIntValue(const ch << 168 G4int parameterNumber, G4bool reGet) 217 { 169 { 218 G4String targetParameter = GetCurrentStringV << 170 G4String targetParameter = >> 171 GetCurrentStringValue( aCommand, parameterNumber, reGet ); 219 G4int value; 172 G4int value; 220 const char* t = targetParameter; 173 const char* t = targetParameter; 221 std::istringstream is(t); << 174 G4std::istrstream is((char*)t); 222 is >> value; 175 is >> value; 223 return value; 176 return value; 224 } 177 } 225 178 226 // ------------------------------------------- << 179 G4double G4UImanager::GetCurrentDoubleValue(const char * aCommand, 227 G4double G4UImanager::GetCurrentDoubleValue(co << 180 const char * aParameterName, G4bool reGet) 228 G4 << 229 { 181 { 230 G4String targetParameter = GetCurrentStringV << 182 G4String targetParameter = >> 183 GetCurrentStringValue( aCommand, aParameterName, reGet ); 231 G4double value; 184 G4double value; 232 const char* t = targetParameter; 185 const char* t = targetParameter; 233 std::istringstream is(t); << 186 G4std::istrstream is((char*)t); 234 is >> value; 187 is >> value; 235 return value; 188 return value; 236 } 189 } 237 190 238 // ------------------------------------------- << 191 G4double G4UImanager::GetCurrentDoubleValue(const char * aCommand, 239 G4double G4UImanager::GetCurrentDoubleValue(co << 192 G4int parameterNumber, G4bool reGet) 240 G4 << 241 { 193 { 242 G4String targetParameter = GetCurrentStringV << 194 G4String targetParameter = >> 195 GetCurrentStringValue( aCommand, parameterNumber, reGet ); 243 G4double value; 196 G4double value; 244 const char* t = targetParameter; 197 const char* t = targetParameter; 245 std::istringstream is(t); << 198 G4std::istrstream is((char*)t); 246 is >> value; 199 is >> value; 247 return value; 200 return value; 248 } 201 } 249 202 250 // ------------------------------------------- << 203 void G4UImanager::AddNewCommand(G4UIcommand * newCommand) 251 void G4UImanager::AddNewCommand(G4UIcommand* n << 252 { 204 { 253 treeTop->AddNewCommand(newCommand); << 205 treeTop->AddNewCommand( newCommand ); 254 if (fMasterUImanager() != nullptr && G4Threa << 255 fMasterUImanager()->AddWorkerCommand(newCo << 256 } << 257 } << 258 << 259 // ------------------------------------------- << 260 void G4UImanager::AddWorkerCommand(G4UIcommand << 261 { << 262 treeTop->AddNewCommand(newCommand, true); << 263 } 206 } 264 207 265 // ------------------------------------------- << 208 void G4UImanager::RemoveCommand(G4UIcommand * aCommand) 266 void G4UImanager::RemoveCommand(G4UIcommand* a << 267 { 209 { 268 treeTop->RemoveCommand(aCommand); << 210 treeTop->RemoveCommand( aCommand ); 269 if (fMasterUImanager() != nullptr && G4Threa << 270 fMasterUImanager()->RemoveWorkerCommand(aC << 271 } << 272 } 211 } 273 212 274 // ------------------------------------------- << 213 void G4UImanager::ExecuteMacroFile(const char * fileName) 275 void G4UImanager::RemoveWorkerCommand(G4UIcomm << 276 { 214 { 277 treeTop->RemoveCommand(aCommand, true); << 215 G4String fn = fileName; >> 216 ExecuteMacroFile(fn); 278 } 217 } 279 218 280 // ------------------------------------------- << 219 void G4UImanager::ExecuteMacroFile(G4String fileName) 281 void G4UImanager::ExecuteMacroFile(const char* << 282 { 220 { 283 G4UIsession* batchSession = new G4UIbatch(fi << 221 G4UIsession* batchSession = new G4UIbatch(fileName,session); 284 session = batchSession; 222 session = batchSession; 285 lastRC = 0; << 286 G4UIsession* previousSession = session->Sess 223 G4UIsession* previousSession = session->SessionStart(); 287 lastRC = session->GetLastReturnCode(); << 288 delete session; 224 delete session; 289 session = previousSession; 225 session = previousSession; 290 } 226 } 291 227 292 // ------------------------------------------- << 228 void G4UImanager::LoopS(G4String valueList) 293 void G4UImanager::LoopS(const char* valueList) << 294 { 229 { 295 G4String vl = valueList; << 230 G4Tokenizer parameterToken( valueList ); 296 G4Tokenizer parameterToken(vl); << 297 G4String mf = parameterToken(); 231 G4String mf = parameterToken(); 298 G4String vn = parameterToken(); 232 G4String vn = parameterToken(); 299 G4String c1 = parameterToken(); 233 G4String c1 = parameterToken(); 300 c1 += " "; 234 c1 += " "; 301 c1 += parameterToken(); 235 c1 += parameterToken(); 302 c1 += " "; 236 c1 += " "; 303 c1 += parameterToken(); 237 c1 += parameterToken(); 304 const char* t1 = c1; 238 const char* t1 = c1; 305 std::istringstream is(t1); << 239 G4std::istrstream is((char*)t1); 306 G4double d1; 240 G4double d1; 307 G4double d2; 241 G4double d2; 308 G4double d3; 242 G4double d3; 309 is >> d1 >> d2 >> d3; 243 is >> d1 >> d2 >> d3; 310 Loop(mf, vn, d1, d2, d3); << 244 Loop(mf,vn,d1,d2,d3); >> 245 } >> 246 >> 247 void G4UImanager::Loop(const char * macroFile,const char * variableName, >> 248 G4double initialValue,G4double finalValue,G4double stepSize) >> 249 { >> 250 G4String mf = macroFile; >> 251 G4String vn = variableName; >> 252 Loop(mf,vn,initialValue,finalValue,stepSize); 311 } 253 } 312 254 313 // ------------------------------------------- << 255 void G4UImanager::Loop(G4String macroFile,G4String variableName, 314 void G4UImanager::Loop(const char* macroFile, << 256 G4double initialValue,G4double finalValue,G4double stepSize) 315 G4double finalValue, G4 << 316 { 257 { 317 G4String cd; 258 G4String cd; 318 if (stepSize > 0) { << 259 for(G4double d=initialValue;d<=finalValue;d+=stepSize) 319 for (G4double d = initialValue; d <= final << 260 { 320 std::ostringstream os; << 261 char st[20]; 321 os << d; << 262 G4std::ostrstream os(st,20); 322 cd += os.str(); << 263 os << d << '\0'; 323 cd += " "; << 264 cd += st; 324 } << 265 cd += " "; 325 } << 326 else { << 327 for (G4double d = initialValue; d >= final << 328 std::ostringstream os; << 329 os << d; << 330 cd += os.str(); << 331 cd += " "; << 332 } << 333 } 266 } 334 Foreach(macroFile, variableName, cd); << 267 Foreach(macroFile,variableName,cd); 335 } 268 } 336 269 337 // ------------------------------------------- << 270 void G4UImanager::ForeachS(G4String valueList) 338 void G4UImanager::ForeachS(const char* valueLi << 339 { 271 { 340 const G4String& vl = valueList; << 272 G4Tokenizer parameterToken( valueList ); 341 G4Tokenizer parameterToken(vl); << 273 G4String mf = parameterToken(); 342 const G4String& mf = parameterToken(); << 274 G4String vn = parameterToken(); 343 const G4String& vn = parameterToken(); << 344 G4String c1 = parameterToken(); 275 G4String c1 = parameterToken(); 345 G4String ca; 276 G4String ca; 346 while (!((ca = parameterToken()).empty())) { << 277 while(!((ca=parameterToken()).isNull())) >> 278 { 347 c1 += " "; 279 c1 += " "; 348 c1 += ca; 280 c1 += ca; 349 } 281 } >> 282 Foreach(mf,vn,c1); >> 283 } 350 284 351 G4String aliasValue = std::move(c1); << 285 void G4UImanager::Foreach(const char * macroFile,const char * variableName, 352 if (aliasValue[0] == '"') { << 286 const char * candidates) 353 G4String strippedValue; << 287 { 354 if (aliasValue.back() == '"') { << 288 G4String mf = macroFile; 355 strippedValue = aliasValue.substr(1, ali << 289 G4String vn = variableName; 356 } << 290 G4String cd = candidates; 357 else { << 291 Foreach(mf,vn,cd); 358 strippedValue = aliasValue.substr(1, ali << 359 } << 360 aliasValue = std::move(strippedValue); << 361 } << 362 << 363 // Foreach(mf,vn,c1); << 364 Foreach(mf, vn, aliasValue); << 365 } 292 } 366 293 367 // ------------------------------------------- << 294 void G4UImanager::Foreach(G4String macroFile,G4String variableName,G4String candidates) 368 void G4UImanager::Foreach(const char* macroFil << 369 { 295 { 370 G4String candidatesString = candidates; << 296 G4Tokenizer parameterToken( candidates ); 371 G4Tokenizer parameterToken(candidatesString) << 372 G4String cd; 297 G4String cd; 373 while (!((cd = parameterToken()).empty())) { << 298 while(!((cd=parameterToken()).isNull())) 374 G4String vl = variableName; << 299 { 375 vl += " "; << 300 SetAlias(variableName+" "+cd); 376 vl += cd; << 301 ExecuteMacroFile(macroFile); 377 SetAlias(vl); << 378 ExecuteMacroFile(FindMacroPath(macroFile)) << 379 if (lastRC != 0) { << 380 G4ExceptionDescription ed; << 381 ed << "Loop aborted due to a command exe << 382 << "error code " << lastRC; << 383 G4Exception("G4UImanager::Foreach", "UIM << 384 break; << 385 } << 386 } 302 } 387 } 303 } 388 304 389 // ------------------------------------------- << 305 390 G4String G4UImanager::SolveAlias(const char* a << 306 G4int G4UImanager::ApplyCommand(const char * aCommand) >> 307 { >> 308 G4String theCommand = aCommand; >> 309 return ApplyCommand(theCommand); >> 310 } >> 311 >> 312 G4String G4UImanager::SolveAlias(G4String aCmd) 391 { 313 { 392 G4String aCommand = aCmd; 314 G4String aCommand = aCmd; 393 std::size_t ia = aCommand.find('{'); << 315 G4int ia = aCommand.index("{"); 394 std::size_t iz = aCommand.find('#'); << 316 G4int iz = aCommand.index("#"); 395 while ((ia != std::string::npos) && ((iz == << 317 while((ia != G4int(G4std::string::npos))&&((iz==G4int(G4std::string::npos))||(ia<iz))) >> 318 { 396 G4int ibx = -1; 319 G4int ibx = -1; 397 while (ibx < 0) { << 320 while(ibx<0) 398 std::size_t ib = aCommand.find('}'); << 321 { 399 if (ib == std::string::npos) { << 322 G4int ib = aCommand.index("}"); >> 323 if( ib == G4int(G4std::string::npos) ) >> 324 { 400 G4cerr << aCommand << G4endl; 325 G4cerr << aCommand << G4endl; 401 for (std::size_t i = 0; i < ia; ++i) { << 326 for(G4int iz=0;iz<ia;iz++) G4cerr << " "; 402 G4cerr << " "; << 403 } << 404 G4cerr << "^" << G4endl; 327 G4cerr << "^" << G4endl; 405 G4cerr << "Unmatched alias parenthesis << 328 G4cerr << "Unmatched alias parenthis -- command ignored" << G4endl; 406 G4String nullStr; << 329 return fAliasNotFound; 407 return nullStr; << 408 } << 409 G4String ps = aCommand.substr(ia + 1, aC << 410 std::size_t ic = ps.find('{'); << 411 std::size_t id = ps.find('}'); << 412 if (ic != std::string::npos && ic < id) << 413 ia += ic + 1; << 414 } << 415 else { << 416 ibx = (G4int)ib; << 417 } 330 } >> 331 G4String ps = aCommand(ia+1,aCommand.length()-(ia+1)); >> 332 G4int ic = ps.index("{"); >> 333 G4int id = ps.index("}"); >> 334 if(ic!=G4int(G4std::string::npos) && ic < id) >> 335 { ia+=ic+1; } >> 336 else >> 337 { ibx = ib; } 418 } 338 } 419 //--- Here ia represents the position of i 339 //--- Here ia represents the position of innermost "{" 420 //--- and ibx represents corresponding "}" 340 //--- and ibx represents corresponding "}" 421 G4String subs; 341 G4String subs; 422 if (ia > 0) { << 342 if(ia>0) subs = aCommand(0,ia); 423 subs = aCommand.substr(0, ia); << 343 G4String alis = aCommand(ia+1,ibx-ia-1); 424 } << 344 G4String rems = aCommand(ibx+1,aCommand.length()-ibx); 425 G4String alis = aCommand.substr(ia + 1, ib << 345 // G4cout << "<" << subs << "> <" << alis << "> <" << rems << ">" << G4endl; 426 G4String rems = aCommand.substr(ibx + 1, a << 346 G4String* alVal = aliasList->FindAlias(alis); 427 const G4String* alVal = aliasList->FindAli << 347 if(!alVal) 428 if (alVal == nullptr) { << 348 { 429 G4cerr << "Alias <" << alis << "> not fo 349 G4cerr << "Alias <" << alis << "> not found -- command ignored" << G4endl; 430 G4String nullStr; 350 G4String nullStr; 431 return nullStr; 351 return nullStr; 432 } 352 } 433 aCommand = subs + (*alVal) + rems; << 353 aCommand = subs+(*alVal)+rems; 434 ia = aCommand.find('{'); << 354 ia = aCommand.index("{"); 435 } 355 } 436 return aCommand; 356 return aCommand; 437 } 357 } 438 358 439 // ------------------------------------------- << 359 G4int G4UImanager::ApplyCommand(G4String aCommand) 440 G4int G4UImanager::ApplyCommand(const G4String << 441 { 360 { 442 return ApplyCommand(aCmd.data()); << 361 G4String aCmd = SolveAlias(aCommand); 443 } << 362 if(aCmd.isNull()) return fAliasNotFound; 444 << 363 aCommand = aCmd; 445 // ------------------------------------------- << 364 if(verboseLevel) G4cout << aCommand << G4endl; 446 G4int G4UImanager::ApplyCommand(const char* aC << 447 { << 448 const G4String& aCommand = SolveAlias(aCmd); << 449 if (aCommand.empty()) { << 450 return fAliasNotFound; << 451 } << 452 if (verboseLevel != 0) { << 453 if (G4Threading::IsMasterThread()) { << 454 fLastCommandOutputTreated = false; << 455 } << 456 G4cout << aCommand << G4endl; << 457 } << 458 G4String commandString; 365 G4String commandString; 459 G4String commandParameter; 366 G4String commandParameter; 460 367 461 std::size_t i = aCommand.find(' '); << 368 G4int i = aCommand.index(" "); 462 if (i != std::string::npos) { << 369 if( i != G4int(G4std::string::npos) ) 463 commandString = aCommand.substr(0, i); << 370 { 464 commandParameter = aCommand.substr(i + 1, << 371 commandString = aCommand(0,i); >> 372 commandParameter = aCommand(i+1,aCommand.length()-(i+1)); 465 } 373 } 466 else { << 374 else >> 375 { 467 commandString = aCommand; 376 commandString = aCommand; 468 } 377 } 469 378 470 // remove doubled slash 379 // remove doubled slash 471 std::size_t len = commandString.length(); << 380 G4int len = commandString.length(); 472 std::size_t ll = 0; << 381 G4int ll = 0; 473 G4String a1; 382 G4String a1; 474 G4String a2; 383 G4String a2; 475 while (ll < len - 1) { << 384 while(ll<len-1) 476 if (commandString.substr(ll, 2) == "//") { << 385 { 477 if (ll == 0) { << 386 if(commandString(ll,2)=="//") 478 // Safe because index argument always << 387 { 479 commandString.erase(ll, 1); << 388 if(ll==0) 480 } << 389 { commandString.remove(ll,1); } 481 else { << 390 else 482 a1 = commandString.substr(0, ll); << 391 { 483 a2 = commandString.substr(ll + 1, len << 392 a1 = commandString(0,ll); 484 commandString = a1 + a2; << 393 a2 = commandString(ll+1,len-ll-1); 485 } << 394 commandString = a1+a2; 486 --len; << 487 } << 488 else { << 489 ++ll; << 490 } << 491 } << 492 << 493 if (isMaster && bridges != nullptr) { << 494 for (auto bridge : *bridges) { << 495 G4int leng = bridge->DirLength(); << 496 if (commandString.substr(0, leng) == bri << 497 return bridge->LocalUI()->ApplyCommand << 498 } << 499 } << 500 } << 501 << 502 G4UIcommand* targetCommand = treeTop->FindPa << 503 if (targetCommand == nullptr) { << 504 if (ignoreCmdNotFound) { << 505 if (stackCommandsForBroadcast) { << 506 commandStack->push_back(commandString << 507 } 395 } 508 return fCommandSucceeded; << 396 len--; 509 } 397 } 510 << 398 else 511 return fCommandNotFound; << 399 { ll++; } 512 } 400 } 513 401 514 if (stackCommandsForBroadcast && targetComma << 402 G4UIcommand * targetCommand = treeTop->FindPath( commandString ); 515 commandStack->push_back(commandString + " << 403 if( targetCommand == NULL ) 516 } << 404 { return fCommandNotFound; } 517 << 518 if (!(targetCommand->IsAvailable())) { << 519 return fIllegalApplicationState; << 520 } << 521 405 522 if (saveHistory) { << 406 if(!(targetCommand->IsAvailable())) 523 historyFile << aCommand << G4endl; << 407 { return fIllegalApplicationState; } 524 } << 408 525 if (G4int(histVec.size()) >= maxHistSize) { << 409 if(saveHistory) historyFile << aCommand << G4endl; 526 histVec.erase(histVec.begin()); << 527 } << 528 histVec.push_back(aCommand); 410 histVec.push_back(aCommand); 529 << 411 return targetCommand->DoIt( commandParameter ); 530 targetCommand->ResetFailure(); << 531 G4int commandFailureCode = targetCommand->Do << 532 if (commandFailureCode == 0) { << 533 G4int additionalFailureCode = targetComman << 534 if (additionalFailureCode > 0) { << 535 G4ExceptionDescription msg; << 536 msg << targetCommand->GetFailureDescript << 537 << "Error code : " << additionalFail << 538 G4Exception("G4UImanager::ApplyCommand", << 539 commandFailureCode += additionalFailureC << 540 } << 541 } << 542 return commandFailureCode; << 543 } << 544 << 545 // ------------------------------------------- << 546 G4UIcommand* G4UImanager::FindCommand(const G4 << 547 { << 548 return FindCommand(aCmd.data()); << 549 } << 550 << 551 // ------------------------------------------- << 552 G4UIcommand* G4UImanager::FindCommand(const ch << 553 { << 554 const G4String& aCommand = SolveAlias(aCmd); << 555 if (aCommand.empty()) { << 556 return nullptr; << 557 } << 558 << 559 G4String commandString; << 560 << 561 std::size_t i = aCommand.find(' '); << 562 if (i != std::string::npos) { << 563 commandString = aCommand.substr(0, i); << 564 } << 565 else { << 566 commandString = aCommand; << 567 } << 568 << 569 return treeTop->FindPath(commandString); << 570 } 412 } 571 413 572 // ------------------------------------------- << 414 void G4UImanager::StoreHistory(G4String fileName) 573 void G4UImanager::StoreHistory(const char* fil << 415 { StoreHistory(true,fileName); } 574 { << 575 StoreHistory(true, fileName); << 576 } << 577 416 578 // ------------------------------------------- << 417 void G4UImanager::StoreHistory(G4bool historySwitch,G4String fileName) 579 void G4UImanager::StoreHistory(G4bool historyS << 580 { 418 { 581 if (historySwitch) { << 419 if(historySwitch) 582 if (saveHistory) { << 420 { 583 historyFile.close(); << 421 if(saveHistory) 584 } << 422 { historyFile.close(); } 585 historyFile.open((char*)fileName); << 423 const char* theFileName = fileName; >> 424 historyFile.open((char*)theFileName); 586 saveHistory = true; 425 saveHistory = true; 587 } 426 } 588 else { << 427 else >> 428 { 589 historyFile.close(); 429 historyFile.close(); 590 saveHistory = false; 430 saveHistory = false; 591 } 431 } 592 saveHistory = historySwitch; 432 saveHistory = historySwitch; 593 } 433 } 594 434 595 // ------------------------------------------- << 435 void G4UImanager::PauseSession(G4String msg) 596 void G4UImanager::PauseSession(const char* msg << 597 { 436 { 598 if (session != nullptr) { << 437 if(session) session->PauseSessionStart(msg); 599 session->PauseSessionStart(msg); << 600 } << 601 } 438 } 602 439 603 // ------------------------------------------- << 440 void G4UImanager::ListCommands(G4String direct) 604 void G4UImanager::ListCommands(const char* dir << 605 { 441 { 606 G4UIcommandTree* comTree = FindDirectory(dir 442 G4UIcommandTree* comTree = FindDirectory(direct); 607 if (comTree != nullptr) { << 443 if(comTree) 608 comTree->List(); << 444 { comTree->List(); } 609 } << 445 else 610 else { << 446 { G4cout << direct << " is not found." << G4endl; } 611 G4cout << direct << " is not found." << G4 << 612 } << 613 } 447 } 614 448 615 // ------------------------------------------- << 616 G4UIcommandTree* G4UImanager::FindDirectory(co 449 G4UIcommandTree* G4UImanager::FindDirectory(const char* dirName) 617 { 450 { 618 const G4String& aDirName = dirName; << 451 G4String aDirName = dirName; 619 G4String targetDir = G4StrUtil::strip_copy(a << 452 G4String targetDir = aDirName.strip(G4String::both); 620 if (targetDir.back() != '/') { << 453 if( targetDir( targetDir.length()-1 ) != '/' ) 621 targetDir += "/"; << 454 { targetDir += "/"; } 622 } << 623 G4UIcommandTree* comTree = treeTop; 455 G4UIcommandTree* comTree = treeTop; 624 if (targetDir == "/") { << 456 if( targetDir == "/" ) 625 return comTree; << 457 { return comTree; } 626 } << 458 G4int idx = 1; 627 std::size_t idx = 1; << 459 while( idx < G4int(targetDir.length())-1 ) 628 while (idx < targetDir.length() - 1) { << 460 { 629 std::size_t i = targetDir.find('/', idx); << 461 G4int i = targetDir.index("/",idx); 630 G4String targetDirString = targetDir.subst << 462 comTree = comTree->GetTree(targetDir(0,i+1)); 631 comTree = comTree->GetTree(targetDirString << 463 if( comTree == NULL ) 632 if (comTree == nullptr) { << 464 { return NULL; } 633 return nullptr; << 465 idx = i+1; 634 } << 635 idx = i + 1; << 636 } 466 } 637 return comTree; 467 return comTree; 638 } 468 } 639 469 640 // ------------------------------------------- << 641 G4bool G4UImanager::Notify(G4ApplicationState 470 G4bool G4UImanager::Notify(G4ApplicationState requestedState) 642 { 471 { 643 if (pauseAtBeginOfEvent) { << 472 //G4cout << G4StateManager::GetStateManager()->GetStateString(requestedState) << " <--- " << G4StateManager::GetStateManager()->GetStateString(G4StateManager::GetStateManager()->GetPreviousState()) << G4endl; 644 if (requestedState == G4State_EventProc << 473 if(pauseAtBeginOfEvent) 645 && G4StateManager::GetStateManager()-> << 474 { 646 { << 475 if(requestedState==EventProc && 647 PauseSession("BeginOfEvent"); << 476 G4StateManager::GetStateManager()->GetPreviousState()==GeomClosed) 648 } << 477 { PauseSession("BeginOfEvent"); } 649 } << 478 } 650 if (pauseAtEndOfEvent) { << 479 if(pauseAtEndOfEvent) 651 if (requestedState == G4State_GeomClosed << 480 { 652 && G4StateManager::GetStateManager()-> << 481 if(requestedState==GeomClosed && 653 { << 482 G4StateManager::GetStateManager()->GetPreviousState()==EventProc) 654 PauseSession("EndOfEvent"); << 483 { PauseSession("EndOfEvent"); } 655 } << 656 } 484 } 657 return true; 485 return true; 658 } 486 } 659 487 660 // ------------------------------------------- << 488 void G4UImanager::Interact() 661 void G4UImanager::SetCoutDestination(G4UIsessi << 662 { << 663 G4iosSetDestination(value); << 664 } << 665 << 666 // ------------------------------------------- << 667 void G4UImanager::SetAlias(const char* aliasLi << 668 { << 669 const G4String& aLine = aliasLine; << 670 std::size_t i = aLine.find(' '); << 671 const G4String& aliasName = aLine.substr(0, << 672 G4String aliasValue = aLine.substr(i + 1, aL << 673 if (aliasValue[0] == '"') { << 674 G4String strippedValue; << 675 if (aliasValue.back() == '"') { << 676 strippedValue = aliasValue.substr(1, ali << 677 } << 678 else { << 679 strippedValue = aliasValue.substr(1, ali << 680 } << 681 aliasValue = std::move(strippedValue); << 682 } << 683 << 684 aliasList->ChangeAlias(aliasName, aliasValue << 685 } << 686 << 687 // ------------------------------------------- << 688 void G4UImanager::RemoveAlias(const char* alia << 689 { << 690 const G4String& aL = aliasName; << 691 G4String targetAlias = G4StrUtil::strip_copy << 692 aliasList->RemoveAlias(targetAlias); << 693 } << 694 << 695 // ------------------------------------------- << 696 void G4UImanager::ListAlias() << 697 { << 698 aliasList->List(); << 699 } << 700 << 701 // ------------------------------------------- << 702 void G4UImanager::CreateHTML(const char* dir) << 703 { << 704 G4UIcommandTree* tr = FindDirectory(dir); << 705 if (tr != nullptr) { << 706 tr->CreateHTML(); << 707 } << 708 else { << 709 G4cerr << "Directory <" << dir << "> is no << 710 } << 711 } << 712 << 713 // ------------------------------------------- << 714 void G4UImanager::ParseMacroSearchPath() << 715 { << 716 searchDirs.clear(); << 717 << 718 std::size_t idxfirst = 0; << 719 std::size_t idxend = 0; << 720 G4String pathstring = ""; << 721 while ((idxend = searchPath.find(':', idxfir << 722 pathstring = searchPath.substr(idxfirst, i << 723 if (!pathstring.empty()) { << 724 searchDirs.push_back(pathstring); << 725 } << 726 idxfirst = idxend + 1; << 727 } << 728 << 729 pathstring = searchPath.substr(idxfirst, sea << 730 if (!pathstring.empty()) { << 731 searchDirs.push_back(std::move(pathstring) << 732 } << 733 } << 734 << 735 // ------------------------------------------- << 736 static G4bool FileFound(const G4String& fname) << 737 { 489 { 738 G4bool qopen = false; << 490 Interact(G4String("G4> ")); 739 std::ifstream fs; << 740 fs.open(fname.c_str(), std::ios::in); << 741 if (fs.good()) { << 742 fs.close(); << 743 qopen = true; << 744 } << 745 return qopen; << 746 } 491 } 747 492 748 // ------------------------------------------- << 493 void G4UImanager::Interact(const char * pC) 749 G4String G4UImanager::FindMacroPath(const G4St << 750 { 494 { 751 G4String macrofile = fname; << 495 G4String cc = pC; 752 << 496 Interact(cc); 753 for (const auto& searchDir : searchDirs) { << 754 const G4String& fullpath = searchDir + "/" << 755 if (FileFound(fullpath)) { << 756 macrofile = fullpath; << 757 break; << 758 } << 759 } << 760 return macrofile; << 761 } 497 } 762 498 763 // ------------------------------------------- << 499 void G4UImanager::Interact(G4String pC) 764 std::vector<G4String>* G4UImanager::GetCommand << 765 { 500 { 766 std::vector<G4String>* returnValue = command << 501 G4cerr << "G4UImanager::Interact() is out of date and is not used anymore." << G4endl; 767 commandStack = new std::vector<G4String>; << 502 G4cerr << "This method will be removed shortly!!!" << G4endl; 768 return returnValue; << 503 G4cerr << "In case of main() use" << G4endl; >> 504 G4cerr << " G4UIsession * session = new G4UIterminal;" << G4endl; >> 505 G4cerr << " session->SessionStart();" << G4endl; >> 506 G4cerr << "In other cases use" << G4endl; >> 507 G4cerr << " G4StateManager::GetStateManager()->Pause();" << G4endl; 769 } 508 } 770 509 771 // ------------------------------------------- << 772 void G4UImanager::RegisterBridge(G4UIbridge* b << 773 { << 774 if (brg->LocalUI() == this) { << 775 G4Exception("G4UImanager::RegisterBridge() << 776 "G4UIBridge cannot bridge betw << 777 } << 778 else { << 779 bridges->push_back(brg); << 780 } << 781 } << 782 510 783 // ------------------------------------------- << 784 void G4UImanager::SetUpForAThread(G4int tId) << 785 { << 786 threadID = tId; << 787 G4iosInitialization(); << 788 threadCout = new G4MTcoutDestination(threadI << 789 threadCout->SetIgnoreCout(igThreadID); << 790 } << 791 511 792 // ------------------------------------------- << 512 void G4UImanager::SetCoutDestination(G4UIsession *const value) 793 void G4UImanager::SetUpForSpecialThread(const << 794 { 513 { 795 threadID = G4Threading::GENERICTHREAD_ID; << 514 G4coutbuf.SetDestination(value); 796 G4Threading::G4SetThreadId(threadID); << 515 G4cerrbuf.SetDestination(value); 797 G4iosInitialization(); << 798 threadCout = new G4MTcoutDestination(threadI << 799 threadCout->SetPrefixString(pref); << 800 threadCout->SetIgnoreCout(igThreadID); << 801 } 516 } 802 517 803 // ------------------------------------------- << 518 void G4UImanager::SetAlias(const char * aliasLine) 804 void G4UImanager::SetCoutFileName(const G4Stri << 805 { 519 { 806 // for sequential mode, ignore this method. << 520 G4String aL = aliasLine; 807 if (threadID < 0) { << 521 SetAlias(aL); 808 return; << 809 } << 810 << 811 if (fileN == "**Screen**") { << 812 threadCout->SetCoutFileName(fileN, ifAppen << 813 } << 814 else { << 815 std::stringstream fn; << 816 fn << "G4W_" << threadID << "_" << fileN; << 817 threadCout->SetCoutFileName(fn.str(), ifAp << 818 } << 819 } 522 } 820 523 821 // ------------------------------------------- << 524 void G4UImanager::SetAlias(G4String aliasLine) 822 void G4UImanager::SetCerrFileName(const G4Stri << 823 { 525 { 824 // for sequential mode, ignore this method. << 526 G4int i = aliasLine.index(" "); 825 if (threadID < 0) { << 527 G4String aliasName = aliasLine(0,i); 826 return; << 528 G4String aliasValue = aliasLine(i+1,aliasLine.length()-(i+1)); >> 529 if(aliasValue(0)=='"') >> 530 { >> 531 G4String strippedValue; >> 532 if(aliasValue(aliasValue.length()-1)=='"') >> 533 { strippedValue = aliasValue(1,aliasValue.length()-2); } >> 534 else >> 535 { strippedValue = aliasValue(1,aliasValue.length()-1); } >> 536 aliasValue = strippedValue; 827 } 537 } 828 538 829 if (fileN == "**Screen**") { << 539 aliasList->ChangeAlias(aliasName,aliasValue); 830 threadCout->SetCerrFileName(fileN, ifAppen << 831 } << 832 else { << 833 std::stringstream fn; << 834 fn << "G4W_" << threadID << "_" << fileN; << 835 threadCout->SetCerrFileName(fn.str(), ifAp << 836 } << 837 } 540 } 838 541 839 // ------------------------------------------- << 542 void G4UImanager::RemoveAlias(const char * aliasName) 840 void G4UImanager::SetThreadPrefixString(const << 841 { 543 { 842 // for sequential mode, ignore this method. << 544 G4String aL = aliasName; 843 if (threadID < 0) { << 545 RemoveAlias(aL); 844 return; << 845 } << 846 threadCout->SetPrefixString(s); << 847 } 546 } 848 547 849 // ------------------------------------------- << 548 void G4UImanager::RemoveAlias(G4String aliasName) 850 void G4UImanager::SetThreadUseBuffer(G4bool fl << 851 { 549 { 852 // for sequential mode, ignore this method. << 550 G4String targetAlias = aliasName.strip(G4String::both); 853 if (threadID < 0) { << 551 aliasList->RemoveAlias(targetAlias); 854 return; << 855 } << 856 threadCout->EnableBuffering(flg); << 857 } 552 } 858 553 859 // ------------------------------------------- << 554 void G4UImanager::ListAlias() 860 void G4UImanager::SetThreadIgnore(G4int tid) << 861 { 555 { 862 // for sequential mode, ignore this method. << 556 aliasList->List(); 863 if (threadID < 0) { << 864 igThreadID = tid; << 865 return; << 866 } << 867 threadCout->SetIgnoreCout(tid); << 868 } 557 } 869 558 870 // ------------------------------------------- << 559 void G4UImanager::CreateHTML(const char* dir) 871 void G4UImanager::SetThreadIgnoreInit(G4bool f << 560 { 872 { << 561 G4UIcommandTree* tr = FindDirectory(dir); 873 // for sequential mode, ignore this method. << 562 if(tr!=0) 874 if (threadID < 0) { << 563 { tr->CreateHTML(); } 875 return; << 564 else 876 } << 565 { G4cerr << "Directory <" << dir << "> is not found." << G4endl; } 877 threadCout->SetIgnoreInit(flg); << 878 } 566 } 879 567 880 G4UIsession* G4UImanager::GetBaseSession() con << 881 { << 882 // There may be no session - pure batch mode << 883 // If there is a session, it may be a batch << 884 // Find base session of this hierarchy of ba << 885 G4UIsession* baseSession = session; << 886 while (auto aBatchSession = dynamic_cast<G4U << 887 auto previousSession = aBatchSession->GetP << 888 if (previousSession == nullptr) { << 889 // No previouse session - aBatchSession << 890 baseSession = aBatchSession; << 891 break; << 892 } << 893 // There is a previous session, which may << 894 // If not, it will be our desired base - s << 895 baseSession = previousSession; << 896 } << 897 return baseSession; << 898 } << 899 568