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 // G4UIcommand 26 // G4UIcommand 27 // 27 // 28 // Author: Makoto Asai (SLAC), 1998 28 // Author: Makoto Asai (SLAC), 1998 29 // ------------------------------------------- 29 // -------------------------------------------------------------------- 30 30 31 #include "G4UIcommand.hh" 31 #include "G4UIcommand.hh" 32 << 33 #include "G4StateManager.hh" << 34 #include "G4Threading.hh" << 35 #include "G4Tokenizer.hh" << 36 #include "G4UIcommandStatus.hh" << 37 #include "G4UImanager.hh" << 38 #include "G4UImessenger.hh" 32 #include "G4UImessenger.hh" 39 #include "G4UIparsing.hh" << 33 #include "G4UImanager.hh" >> 34 #include "G4UIcommandStatus.hh" >> 35 #include "G4StateManager.hh" 40 #include "G4UnitsTable.hh" 36 #include "G4UnitsTable.hh" >> 37 #include "G4Tokenizer.hh" 41 #include "G4ios.hh" 38 #include "G4ios.hh" >> 39 #include <sstream> >> 40 #include <iomanip> >> 41 >> 42 #include "G4UItokenNum.hh" >> 43 >> 44 #include "G4Threading.hh" >> 45 >> 46 using namespace G4UItokenNum; >> 47 >> 48 // -------------------------------------------------------------------- >> 49 G4UIcommand::G4UIcommand() >> 50 { >> 51 } 42 52 43 // ------------------------------------------- 53 // -------------------------------------------------------------------- 44 G4UIcommand::G4UIcommand(const char* theComman << 54 G4UIcommand::G4UIcommand(const char* theCommandPath, 45 : toBeBroadcasted(tBB), messenger(theMesseng << 55 G4UImessenger* theMessenger, G4bool tBB) >> 56 : toBeBroadcasted(tBB) >> 57 , messenger(theMessenger) 46 { 58 { 47 G4String comStr = theCommandPath; 59 G4String comStr = theCommandPath; 48 G4UIcommandCommonConstructorCode(comStr); 60 G4UIcommandCommonConstructorCode(comStr); 49 availabelStateList = {G4State_PreInit, G4 << 61 availabelStateList.clear(); 50 G4State_GeomClosed, G4 << 62 availabelStateList.push_back(G4State_PreInit); >> 63 availabelStateList.push_back(G4State_Init); >> 64 availabelStateList.push_back(G4State_Idle); >> 65 availabelStateList.push_back(G4State_GeomClosed); >> 66 availabelStateList.push_back(G4State_EventProc); >> 67 availabelStateList.push_back(G4State_Abort); 51 } 68 } 52 69 53 // ------------------------------------------- 70 // -------------------------------------------------------------------- 54 void G4UIcommand::G4UIcommandCommonConstructor 71 void G4UIcommand::G4UIcommandCommonConstructorCode(const char* theCommandPath) 55 { 72 { 56 commandPath = theCommandPath; << 73 commandPath = theCommandPath; 57 commandName = theCommandPath; << 74 commandName = theCommandPath; 58 auto commandNameIndex = (G4int)commandName.r << 75 G4int commandNameIndex = commandName.rfind('/'); 59 commandName.erase(0, commandNameIndex + 1); 76 commandName.erase(0, commandNameIndex + 1); 60 #ifdef G4MULTITHREADED 77 #ifdef G4MULTITHREADED 61 if ((messenger != nullptr) && messenger->Com << 78 if(messenger && messenger->CommandsShouldBeInMaster() && 62 && G4Threading::IsWorkerThread()) << 79 G4Threading::IsWorkerThread()) 63 { 80 { 64 toBeBroadcasted = false; 81 toBeBroadcasted = false; 65 G4UImanager::GetMasterUIpointer()->AddNewC 82 G4UImanager::GetMasterUIpointer()->AddNewCommand(this); 66 } 83 } 67 else { << 84 else >> 85 { 68 G4UImanager::GetUIpointer()->AddNewCommand 86 G4UImanager::GetUIpointer()->AddNewCommand(this); 69 } 87 } 70 #else 88 #else 71 G4UImanager::GetUIpointer()->AddNewCommand(t 89 G4UImanager::GetUIpointer()->AddNewCommand(this); 72 #endif 90 #endif 73 } 91 } 74 92 75 // ------------------------------------------- 93 // -------------------------------------------------------------------- 76 void G4UIcommand::SetCommandType(CommandType t 94 void G4UIcommand::SetCommandType(CommandType typ) 77 { 95 { 78 if (messenger == nullptr) { // this must be << 96 if(messenger==nullptr) 79 if (typ != CmdDirectory) { << 97 { // this must be a directory >> 98 if(typ != CmdDirectory) >> 99 { 80 G4ExceptionDescription ed; 100 G4ExceptionDescription ed; 81 ed << "A UI command <" << commandPath << << 101 ed << "A UI command <" << commandPath 82 G4Exception("G4UIcommand::SetCommandType << 102 << "> is defined without vaild messenger."; >> 103 G4Exception("G4UIcommand::SetCommandType","UI2031", >> 104 FatalException,ed); 83 } 105 } 84 else if (commandPath.back() != '/') { << 106 else if(commandPath.back() != '/') >> 107 { 85 G4ExceptionDescription ed; 108 G4ExceptionDescription ed; 86 ed << "G4UIcommand Warning : \n" << 109 ed << "G4UIcommand Warning : \n" 87 << " <" << commandPath << "> must be << 110 << " <" << commandPath << "> must be a directory." 88 << " '/' is appended."; 111 << " '/' is appended."; 89 G4Exception("G4UIcommand::SetCommandType << 112 G4Exception("G4UIcommand::SetCommandType","UI2032", >> 113 JustWarning,ed); 90 commandPath += "/"; 114 commandPath += "/"; 91 } 115 } 92 } 116 } 93 commandType = typ; 117 commandType = typ; 94 } 118 } 95 119 96 // ------------------------------------------- 120 // -------------------------------------------------------------------- 97 G4UIcommand::~G4UIcommand() 121 G4UIcommand::~G4UIcommand() 98 { 122 { 99 G4UImanager* fUImanager = G4UImanager::GetUI 123 G4UImanager* fUImanager = G4UImanager::GetUIpointer(); 100 if (fUImanager != nullptr) { << 124 if(fUImanager) >> 125 { 101 fUImanager->RemoveCommand(this); 126 fUImanager->RemoveCommand(this); 102 } 127 } 103 128 104 for (const auto& p : parameter) { << 129 G4int n_parameterEntry = parameter.size(); 105 delete p; << 130 for(G4int i_thParameter = 0; i_thParameter < n_parameterEntry; >> 131 ++i_thParameter) >> 132 { >> 133 delete parameter[i_thParameter]; 106 } 134 } >> 135 parameter.clear(); 107 } 136 } 108 137 109 // ------------------------------------------- 138 // -------------------------------------------------------------------- 110 G4bool G4UIcommand::operator==(const G4UIcomma 139 G4bool G4UIcommand::operator==(const G4UIcommand& right) const 111 { 140 { 112 return (commandPath == right.GetCommandPath( 141 return (commandPath == right.GetCommandPath()); 113 } 142 } 114 143 115 // ------------------------------------------- 144 // -------------------------------------------------------------------- 116 G4bool G4UIcommand::operator!=(const G4UIcomma 145 G4bool G4UIcommand::operator!=(const G4UIcommand& right) const 117 { 146 { 118 return (commandPath != right.GetCommandPath( 147 return (commandPath != right.GetCommandPath()); 119 } 148 } 120 149 121 // ------------------------------------------- 150 // -------------------------------------------------------------------- 122 G4int G4UIcommand::DoIt(const G4String& parame << 151 G4int G4UIcommand::DoIt(G4String parameterList) 123 { 152 { 124 G4String correctParameters; 153 G4String correctParameters; 125 std::size_t n_parameterEntry = parameter.siz << 154 G4int n_parameterEntry = parameter.size(); 126 if (n_parameterEntry != 0) { << 155 if(n_parameterEntry != 0) >> 156 { 127 G4String aToken; 157 G4String aToken; 128 G4String correctToken; 158 G4String correctToken; 129 G4Tokenizer parameterToken(parameterList); 159 G4Tokenizer parameterToken(parameterList); 130 for (std::size_t i_thParameter = 0; i_thPa << 160 for(G4int i_thParameter = 0; i_thParameter < n_parameterEntry; 131 if (i_thParameter > 0) { << 161 ++i_thParameter) >> 162 { >> 163 if(i_thParameter > 0) >> 164 { 132 correctParameters.append(" "); 165 correctParameters.append(" "); 133 } 166 } 134 aToken = parameterToken(); 167 aToken = parameterToken(); 135 if (aToken.length() > 0 && aToken[0] == << 168 if(aToken.length() > 0 && aToken[0] == '"') 136 while (aToken.back() != '"' || (aToken << 169 { >> 170 while(aToken.back() != '"' || >> 171 (aToken.length() == 1 && aToken[0] == '"')) >> 172 { 137 G4String additionalToken = parameter 173 G4String additionalToken = parameterToken(); 138 if (additionalToken.empty()) { << 174 if(additionalToken.empty()) 139 return G4int(fParameterUnreadable << 175 { >> 176 return fParameterUnreadable + i_thParameter; 140 } 177 } 141 aToken += " "; 178 aToken += " "; 142 aToken += additionalToken; 179 aToken += additionalToken; 143 } 180 } 144 } 181 } 145 else if (i_thParameter == n_parameterEnt << 182 else if(i_thParameter == n_parameterEntry - 1 && 146 && parameter[i_thParameter]->Ge << 183 parameter[i_thParameter]->GetParameterType() == 's') 147 { 184 { 148 G4String anotherToken; 185 G4String anotherToken; 149 while (!((anotherToken = parameterToke << 186 while(!((anotherToken = parameterToken()).empty())) 150 std::size_t idxs = anotherToken.find << 187 { 151 if (idxs == std::string::npos) { << 188 G4int idxs = anotherToken.find("#"); >> 189 if(idxs == G4int(std::string::npos)) >> 190 { 152 aToken += " "; 191 aToken += " "; 153 aToken += anotherToken; 192 aToken += anotherToken; 154 } 193 } 155 else if (idxs > 0) { << 194 else if(idxs > 0) >> 195 { 156 aToken += " "; 196 aToken += " "; 157 aToken += anotherToken.substr(0, i 197 aToken += anotherToken.substr(0, idxs); 158 break; 198 break; 159 } 199 } 160 else { << 200 else >> 201 { 161 break; 202 break; 162 } 203 } 163 } 204 } 164 } 205 } 165 206 166 if (aToken.empty() || aToken == "!") { << 207 if(aToken.empty() || aToken == "!") 167 if (parameter[i_thParameter]->IsOmitta << 208 { 168 if (parameter[i_thParameter]->GetCur << 209 if(parameter[i_thParameter]->IsOmittable()) >> 210 { >> 211 if(parameter[i_thParameter]->GetCurrentAsDefault()) >> 212 { 169 G4Tokenizer cvSt(messenger->GetCur 213 G4Tokenizer cvSt(messenger->GetCurrentValue(this)); 170 G4String parVal; 214 G4String parVal; 171 for (std::size_t ii = 0; ii < i_th << 215 for(G4int ii = 0; ii < i_thParameter; ++ii) >> 216 { 172 parVal = cvSt(); 217 parVal = cvSt(); 173 if (parVal[0] == '"') { << 218 if(parVal[0] == '"') 174 while (parVal.back() != '"') { << 219 { >> 220 while(parVal.back() != '"') >> 221 { 175 G4String additionalToken = c 222 G4String additionalToken = cvSt(); 176 if (additionalToken.empty()) << 223 if(additionalToken.empty()) 177 return G4int(fParameterUnr << 224 { >> 225 return fParameterUnreadable + i_thParameter; 178 } 226 } 179 parVal += " "; 227 parVal += " "; 180 parVal += additionalToken; 228 parVal += additionalToken; 181 } 229 } 182 } 230 } 183 } 231 } 184 G4String aCVToken = cvSt(); 232 G4String aCVToken = cvSt(); 185 if (aCVToken[0] == '"') { << 233 if(aCVToken[0] == '"') 186 while (aCVToken.back() != '"') { << 234 { >> 235 while(aCVToken.back() != '"') >> 236 { 187 G4String additionalToken = cvS 237 G4String additionalToken = cvSt(); 188 if (additionalToken.empty()) { << 238 if(additionalToken.empty()) 189 return G4int(fParameterUnrea << 239 { >> 240 return fParameterUnreadable + i_thParameter; 190 } 241 } 191 aCVToken += " "; 242 aCVToken += " "; 192 aCVToken += additionalToken; 243 aCVToken += additionalToken; 193 } 244 } 194 } 245 } 195 correctParameters.append(aCVToken) 246 correctParameters.append(aCVToken); 196 } 247 } 197 else { << 248 else 198 correctParameters.append(parameter << 249 { >> 250 correctParameters.append( >> 251 parameter[i_thParameter]->GetDefaultValue()); 199 } 252 } 200 } 253 } 201 else { << 254 else 202 return G4int(fParameterUnreadable + << 255 { >> 256 return fParameterUnreadable + i_thParameter; 203 } 257 } 204 } 258 } 205 else { << 259 else >> 260 { 206 G4int stat = parameter[i_thParameter]- 261 G4int stat = parameter[i_thParameter]->CheckNewValue(aToken); 207 if (stat != 0) { << 262 if(stat) 208 return stat + G4int(i_thParameter); << 263 return stat + i_thParameter; 209 } << 210 correctParameters.append(aToken); 264 correctParameters.append(aToken); 211 } 265 } 212 } 266 } 213 } 267 } 214 268 215 if (CheckNewValue(correctParameters) != 0) { << 269 if(CheckNewValue(correctParameters)) >> 270 { 216 return fParameterOutOfRange + 99; 271 return fParameterOutOfRange + 99; 217 } 272 } 218 273 219 if (workerThreadOnly && G4Threading::IsMaste << 274 if(workerThreadOnly && G4Threading::IsMasterThread()) 220 return 0; 275 return 0; 221 } << 222 276 223 messenger->SetNewValue(this, std::move(corre << 277 messenger->SetNewValue(this, correctParameters); 224 return 0; 278 return 0; 225 } 279 } 226 280 227 // ------------------------------------------- 281 // -------------------------------------------------------------------- 228 G4String G4UIcommand::GetCurrentValue() 282 G4String G4UIcommand::GetCurrentValue() 229 { 283 { 230 return messenger->GetCurrentValue(this); 284 return messenger->GetCurrentValue(this); 231 } 285 } 232 286 233 // ------------------------------------------- 287 // -------------------------------------------------------------------- 234 void G4UIcommand::AvailableForStates(G4Applica 288 void G4UIcommand::AvailableForStates(G4ApplicationState s1) 235 { 289 { 236 availabelStateList = {s1}; << 290 availabelStateList.clear(); >> 291 availabelStateList.push_back(s1); 237 } 292 } 238 293 239 // ------------------------------------------- 294 // -------------------------------------------------------------------- 240 void G4UIcommand::AvailableForStates(G4Applica << 295 void G4UIcommand::AvailableForStates(G4ApplicationState s1, >> 296 G4ApplicationState s2) 241 { 297 { 242 availabelStateList = {s1, s2}; << 298 availabelStateList.clear(); >> 299 availabelStateList.push_back(s1); >> 300 availabelStateList.push_back(s2); 243 } 301 } 244 302 245 // ------------------------------------------- 303 // -------------------------------------------------------------------- 246 void G4UIcommand::AvailableForStates(G4Applica << 304 void G4UIcommand::AvailableForStates(G4ApplicationState s1, >> 305 G4ApplicationState s2, 247 G4Applica 306 G4ApplicationState s3) 248 { 307 { 249 availabelStateList = {s1, s2, s3}; << 308 availabelStateList.clear(); >> 309 availabelStateList.push_back(s1); >> 310 availabelStateList.push_back(s2); >> 311 availabelStateList.push_back(s3); 250 } 312 } 251 313 252 // ------------------------------------------- 314 // -------------------------------------------------------------------- 253 void G4UIcommand::AvailableForStates(G4Applica << 315 void G4UIcommand::AvailableForStates(G4ApplicationState s1, 254 G4Applica << 316 G4ApplicationState s2, >> 317 G4ApplicationState s3, >> 318 G4ApplicationState s4) 255 { 319 { 256 availabelStateList = {s1, s2, s3, s4}; << 320 availabelStateList.clear(); >> 321 availabelStateList.push_back(s1); >> 322 availabelStateList.push_back(s2); >> 323 availabelStateList.push_back(s3); >> 324 availabelStateList.push_back(s4); 257 } 325 } 258 326 259 // ------------------------------------------- 327 // -------------------------------------------------------------------- 260 void G4UIcommand::AvailableForStates(G4Applica << 328 void G4UIcommand::AvailableForStates(G4ApplicationState s1, 261 G4Applica << 329 G4ApplicationState s2, >> 330 G4ApplicationState s3, >> 331 G4ApplicationState s4, 262 G4Applica 332 G4ApplicationState s5) 263 { 333 { 264 availabelStateList = {s1, s2, s3, s4, s5}; << 334 availabelStateList.clear(); >> 335 availabelStateList.push_back(s1); >> 336 availabelStateList.push_back(s2); >> 337 availabelStateList.push_back(s3); >> 338 availabelStateList.push_back(s4); >> 339 availabelStateList.push_back(s5); 265 } 340 } 266 341 267 // ------------------------------------------- 342 // -------------------------------------------------------------------- 268 G4bool G4UIcommand::IsAvailable() 343 G4bool G4UIcommand::IsAvailable() 269 { 344 { 270 G4ApplicationState currentState = G4StateMan << 345 G4bool av = false; >> 346 G4ApplicationState currentState = >> 347 G4StateManager::GetStateManager()->GetCurrentState(); 271 348 272 for (const auto& s : availabelStateList) { << 349 G4int nState = availabelStateList.size(); 273 if (s == currentState) { << 350 for(G4int i = 0; i < nState; ++i) 274 return true; << 351 { >> 352 if(availabelStateList[i] == currentState) >> 353 { >> 354 av = true; >> 355 break; 275 } 356 } 276 } 357 } 277 358 278 return false; << 359 return av; 279 } 360 } 280 361 281 // ------------------------------------------- 362 // -------------------------------------------------------------------- 282 G4double G4UIcommand::ValueOf(const char* unit 363 G4double G4UIcommand::ValueOf(const char* unitName) 283 { 364 { 284 return G4UnitDefinition::GetValueOf(unitName << 365 G4double value = 0.; >> 366 value = G4UnitDefinition::GetValueOf(unitName); >> 367 return value; 285 } 368 } 286 369 287 // ------------------------------------------- 370 // -------------------------------------------------------------------- 288 G4String G4UIcommand::CategoryOf(const char* u 371 G4String G4UIcommand::CategoryOf(const char* unitName) 289 { 372 { 290 return G4UnitDefinition::GetCategory(unitNam 373 return G4UnitDefinition::GetCategory(unitName); 291 } 374 } 292 375 293 // ------------------------------------------- 376 // -------------------------------------------------------------------- 294 G4String G4UIcommand::UnitsList(const char* un 377 G4String G4UIcommand::UnitsList(const char* unitCategory) 295 { 378 { >> 379 G4String retStr; 296 G4UnitsTable& UTbl = G4UnitDefinition::GetUn 380 G4UnitsTable& UTbl = G4UnitDefinition::GetUnitsTable(); 297 << 381 std::size_t i; 298 auto ucatIter = std::find_if(std::cbegin(UTb << 382 for(i = 0; i < UTbl.size(); ++i) 299 return ud->GetName() == unitCategory; << 383 { 300 }); << 384 if(UTbl[i]->GetName() == unitCategory) 301 << 385 break; 302 if (ucatIter == std::cend(UTbl)) { << 386 } 303 G4cerr << "Unit category <" << unitCategor << 387 if(i == UTbl.size()) 304 return G4String(); << 388 { 305 } << 389 G4cerr << "Unit category <" << unitCategory << "> is not defined." 306 << 390 << G4endl; 307 G4String symList; << 391 return retStr; 308 G4String nameList; << 392 } 309 G4UnitsContainer& UCnt = (*ucatIter)->GetUni << 393 G4UnitsContainer& UCnt = UTbl[i]->GetUnitsList(); 310 << 394 retStr = UCnt[0]->GetSymbol(); 311 for (const auto& uDef : UCnt) { << 395 G4int je = UCnt.size(); 312 symList += uDef->GetSymbol(); << 396 for(G4int j = 1; j < je; ++j) 313 symList += " "; << 397 { 314 nameList += uDef->GetName(); << 398 retStr += " "; 315 nameList += " "; << 399 retStr += UCnt[j]->GetSymbol(); 316 } << 400 } 317 symList += nameList; << 401 for(G4int k = 0; k < je; ++k) 318 G4StrUtil::rstrip(symList); << 402 { 319 return symList; << 403 retStr += " "; >> 404 retStr += UCnt[k]->GetName(); >> 405 } >> 406 return retStr; 320 } 407 } 321 408 322 // ------------------------------------------- 409 // -------------------------------------------------------------------- 323 void G4UIcommand::List() 410 void G4UIcommand::List() 324 { 411 { 325 G4cout << G4endl; 412 G4cout << G4endl; 326 G4cout << G4endl; 413 G4cout << G4endl; 327 if (commandPath.back() != '/') { << 414 if(commandPath.back() != '/') >> 415 { 328 G4cout << "Command " << commandPath << G4e 416 G4cout << "Command " << commandPath << G4endl; 329 } 417 } 330 if (workerThreadOnly) { << 418 if(workerThreadOnly) >> 419 { 331 G4cout << " ---- available only in work 420 G4cout << " ---- available only in worker thread" << G4endl; 332 } 421 } 333 << 334 G4cout << "Guidance :" << G4endl; 422 G4cout << "Guidance :" << G4endl; 335 for (const auto& i_thGuidance : commandGuida << 423 G4int n_guidanceEntry = commandGuidance.size(); 336 G4cout << i_thGuidance << G4endl; << 424 for(G4int i_thGuidance = 0; i_thGuidance < n_guidanceEntry; ++i_thGuidance) >> 425 { >> 426 G4cout << commandGuidance[i_thGuidance] << G4endl; 337 } 427 } 338 << 428 if(!rangeString.empty()) 339 if (!rangeExpression.empty()) { << 429 { 340 G4cout << " Range of parameters : " << ran << 430 G4cout << " Range of parameters : " << rangeString << G4endl; 341 } 431 } 342 << 432 G4int n_parameterEntry = parameter.size(); 343 for (const auto& i_thParameter : parameter) << 433 if(n_parameterEntry > 0) 344 i_thParameter->List(); << 434 { >> 435 for(G4int i_thParameter = 0; i_thParameter < n_parameterEntry; >> 436 ++i_thParameter) >> 437 { >> 438 parameter[i_thParameter]->List(); >> 439 } 345 } 440 } 346 G4cout << G4endl; 441 G4cout << G4endl; 347 } 442 } 348 443 349 // ------------------------------------------- 444 // -------------------------------------------------------------------- 350 G4String G4UIcommand::ConvertToString(G4bool b 445 G4String G4UIcommand::ConvertToString(G4bool boolVal) 351 { 446 { 352 return boolVal ? "1" : "0"; << 447 G4String vl = "0"; >> 448 if(boolVal) >> 449 vl = "1"; >> 450 return vl; 353 } 451 } 354 452 355 // ------------------------------------------- 453 // -------------------------------------------------------------------- 356 G4String G4UIcommand::ConvertToString(G4int in 454 G4String G4UIcommand::ConvertToString(G4int intValue) 357 { 455 { 358 return G4UIparsing::TtoS(intValue); << 456 std::ostringstream os; >> 457 os << intValue; >> 458 G4String vl = os.str(); >> 459 return vl; 359 } 460 } 360 461 361 // ------------------------------------------- 462 // -------------------------------------------------------------------- 362 G4String G4UIcommand::ConvertToString(G4long l 463 G4String G4UIcommand::ConvertToString(G4long longValue) 363 { 464 { 364 return G4UIparsing::TtoS(longValue); << 465 std::ostringstream os; >> 466 os << longValue; >> 467 G4String vl = os.str(); >> 468 return vl; 365 } 469 } 366 470 367 // ------------------------------------------- 471 // -------------------------------------------------------------------- 368 G4String G4UIcommand::ConvertToString(G4double 472 G4String G4UIcommand::ConvertToString(G4double doubleValue) 369 { 473 { 370 std::ostringstream os; 474 std::ostringstream os; 371 if (G4UImanager::DoublePrecisionStr()) { << 475 if(G4UImanager::DoublePrecisionStr()) 372 os << std::setprecision(17); << 476 { >> 477 os << std::setprecision(17) << doubleValue; 373 } 478 } 374 os << doubleValue; << 479 else 375 return os.str(); << 480 { >> 481 os << doubleValue; >> 482 } >> 483 G4String vl = os.str(); >> 484 return vl; 376 } 485 } 377 486 378 // ------------------------------------------- 487 // -------------------------------------------------------------------- 379 G4String G4UIcommand::ConvertToString(G4double << 488 G4String G4UIcommand::ConvertToString(G4double doubleValue, >> 489 const char* unitName) 380 { 490 { >> 491 G4String unt = unitName; >> 492 G4double uv = ValueOf(unitName); >> 493 381 std::ostringstream os; 494 std::ostringstream os; 382 if (G4UImanager::DoublePrecisionStr()) { << 495 if(G4UImanager::DoublePrecisionStr()) 383 os << std::setprecision(17); << 496 { >> 497 os << std::setprecision(17) << doubleValue / uv << " " << unitName; >> 498 } >> 499 else >> 500 { >> 501 os << doubleValue / uv << " " << unitName; 384 } 502 } 385 os << doubleValue / ValueOf(unitName) << " " << 503 G4String vl = os.str(); 386 return os.str(); << 504 return vl; 387 } 505 } 388 506 389 // ------------------------------------------- 507 // -------------------------------------------------------------------- 390 G4String G4UIcommand::ConvertToString(const G4 << 508 G4String G4UIcommand::ConvertToString(G4ThreeVector vec) 391 { 509 { 392 std::ostringstream os; 510 std::ostringstream os; 393 if (G4UImanager::DoublePrecisionStr()) { << 511 if(G4UImanager::DoublePrecisionStr()) 394 os << std::setprecision(17); << 512 { >> 513 os << std::setprecision(17) << vec.x() << " " << vec.y() << " " << vec.z(); 395 } 514 } 396 os << vec.x() << " " << vec.y() << " " << ve << 515 else 397 return os.str(); << 516 { >> 517 os << vec.x() << " " << vec.y() << " " << vec.z(); >> 518 } >> 519 G4String vl = os.str(); >> 520 return vl; 398 } 521 } 399 522 400 // ------------------------------------------- 523 // -------------------------------------------------------------------- 401 G4String G4UIcommand::ConvertToString(const G4 << 524 G4String G4UIcommand::ConvertToString(G4ThreeVector vec, const char* unitName) 402 { 525 { 403 G4double uv = ValueOf(unitName); << 526 G4String unt = unitName; >> 527 G4double uv = ValueOf(unitName); 404 528 405 std::ostringstream os; 529 std::ostringstream os; 406 if (G4UImanager::DoublePrecisionStr()) { << 530 if(G4UImanager::DoublePrecisionStr()) 407 os << std::setprecision(17); << 531 { >> 532 os << std::setprecision(17) << vec.x() / uv << " " << vec.y() / uv << " " >> 533 << vec.z() / uv << " " << unitName; 408 } 534 } 409 os << vec.x() / uv << " " << vec.y() / uv << << 535 else 410 return os.str(); << 536 { >> 537 os << vec.x() / uv << " " << vec.y() / uv << " " << vec.z() / uv << " " >> 538 << unitName; >> 539 } >> 540 G4String vl = os.str(); >> 541 return vl; 411 } 542 } 412 543 413 // ------------------------------------------- 544 // -------------------------------------------------------------------- 414 G4bool G4UIcommand::ConvertToBool(const char* 545 G4bool G4UIcommand::ConvertToBool(const char* st) 415 { 546 { 416 G4String v = G4StrUtil::to_upper_copy(st); 547 G4String v = G4StrUtil::to_upper_copy(st); 417 return (v == "Y" || v == "YES" || v == "1" | << 548 G4bool vl = false; >> 549 if(v == "Y" || v == "YES" || v == "1" || v == "T" || v == "TRUE") >> 550 { >> 551 vl = true; >> 552 } >> 553 return vl; 418 } 554 } 419 555 420 // ------------------------------------------- 556 // -------------------------------------------------------------------- 421 G4int G4UIcommand::ConvertToInt(const char* st 557 G4int G4UIcommand::ConvertToInt(const char* st) 422 { 558 { 423 return G4UIparsing::StoT<G4int>(st); << 559 G4int vl; >> 560 std::istringstream is(st); >> 561 is >> vl; >> 562 return vl; 424 } 563 } 425 564 426 // ------------------------------------------- 565 // -------------------------------------------------------------------- 427 G4long G4UIcommand::ConvertToLongInt(const cha 566 G4long G4UIcommand::ConvertToLongInt(const char* st) 428 { 567 { 429 return G4UIparsing::StoT<G4long>(st); << 568 G4long vl; >> 569 std::istringstream is(st); >> 570 is >> vl; >> 571 return vl; 430 } 572 } 431 573 432 // ------------------------------------------- 574 // -------------------------------------------------------------------- 433 G4double G4UIcommand::ConvertToDouble(const ch 575 G4double G4UIcommand::ConvertToDouble(const char* st) 434 { 576 { 435 return G4UIparsing::StoT<G4double>(st); << 577 G4double vl; >> 578 std::istringstream is(st); >> 579 is >> vl; >> 580 return vl; 436 } 581 } 437 582 438 // ------------------------------------------- 583 // -------------------------------------------------------------------- 439 G4double G4UIcommand::ConvertToDimensionedDoub 584 G4double G4UIcommand::ConvertToDimensionedDouble(const char* st) 440 { 585 { 441 G4double vl; 586 G4double vl; 442 char unts[30]; 587 char unts[30]; 443 588 444 std::istringstream is(st); 589 std::istringstream is(st); 445 is >> vl >> unts; 590 is >> vl >> unts; 446 G4String unt = unts; 591 G4String unt = unts; 447 592 448 return (vl * ValueOf(unt)); 593 return (vl * ValueOf(unt)); 449 } 594 } 450 595 451 // ------------------------------------------- 596 // -------------------------------------------------------------------- 452 G4ThreeVector G4UIcommand::ConvertTo3Vector(co 597 G4ThreeVector G4UIcommand::ConvertTo3Vector(const char* st) 453 { 598 { 454 G4double vx; 599 G4double vx; 455 G4double vy; 600 G4double vy; 456 G4double vz; 601 G4double vz; 457 std::istringstream is(st); 602 std::istringstream is(st); 458 is >> vx >> vy >> vz; 603 is >> vx >> vy >> vz; 459 return G4ThreeVector(vx, vy, vz); 604 return G4ThreeVector(vx, vy, vz); 460 } 605 } 461 606 462 // ------------------------------------------- 607 // -------------------------------------------------------------------- 463 G4ThreeVector G4UIcommand::ConvertToDimensione 608 G4ThreeVector G4UIcommand::ConvertToDimensioned3Vector(const char* st) 464 { 609 { 465 G4double vx; 610 G4double vx; 466 G4double vy; 611 G4double vy; 467 G4double vz; 612 G4double vz; 468 char unts[30]; 613 char unts[30]; 469 std::istringstream is(st); 614 std::istringstream is(st); 470 is >> vx >> vy >> vz >> unts; 615 is >> vx >> vy >> vz >> unts; 471 G4String unt = unts; 616 G4String unt = unts; 472 G4double uv = ValueOf(unt); << 617 G4double uv = ValueOf(unt); 473 return G4ThreeVector(vx * uv, vy * uv, vz * 618 return G4ThreeVector(vx * uv, vy * uv, vz * uv); 474 } 619 } 475 620 >> 621 // ----- the following is used by CheckNewValue() -------------------- >> 622 >> 623 #include <ctype.h> // isalpha(), toupper() >> 624 476 G4int G4UIcommand::CheckNewValue(const char* n 625 G4int G4UIcommand::CheckNewValue(const char* newValue) 477 { 626 { 478 if (!G4UIparsing::RangeCheck(*this, newValue << 627 yystype result; 479 return fParameterOutOfRange; << 628 // if( TypeCheck(newValue) == 0 ) return 1; >> 629 if(!rangeString.empty()) >> 630 { >> 631 if(RangeCheck(newValue) == 0) >> 632 return fParameterOutOfRange; 480 } 633 } 481 return 0; // succeeded 634 return 0; // succeeded >> 635 } >> 636 >> 637 // -------------------------------------------------------------------- >> 638 G4int G4UIcommand::TypeCheck(const char* t) >> 639 { >> 640 G4String aNewValue; >> 641 char type; >> 642 std::istringstream is(t); >> 643 for(unsigned i = 0; i < parameter.size(); ++i) >> 644 { >> 645 is >> aNewValue; >> 646 type = toupper(parameter[i]->GetParameterType()); >> 647 switch(type) >> 648 { >> 649 case 'D': >> 650 if(IsDouble(aNewValue) == 0) >> 651 { >> 652 G4cerr << aNewValue << ": double value expected." << G4endl; >> 653 return 0; >> 654 } >> 655 break; >> 656 case 'I': >> 657 if(IsInt(aNewValue, 10) == 0) >> 658 { >> 659 G4cerr << aNewValue << ": integer expected." << G4endl; >> 660 return 0; >> 661 } >> 662 break; >> 663 case 'L': >> 664 if(IsInt(aNewValue, 20) == 0) >> 665 { >> 666 G4cerr << aNewValue << ": long int expected." << G4endl; >> 667 return 0; >> 668 } >> 669 break; >> 670 case 'S': >> 671 break; >> 672 case 'B': >> 673 G4StrUtil::to_upper(aNewValue); >> 674 if(aNewValue == "Y" || aNewValue == "N" || aNewValue == "YES" || >> 675 aNewValue == "NO" || aNewValue == "1" || aNewValue == "0" || >> 676 aNewValue == "T" || aNewValue == "F" || aNewValue == "TRUE" || >> 677 aNewValue == "FALSE") >> 678 return 1; >> 679 else >> 680 return 0; >> 681 break; >> 682 default:; >> 683 } >> 684 } >> 685 return 1; >> 686 } >> 687 >> 688 // -------------------------------------------------------------------- >> 689 G4int G4UIcommand::IsInt(const char* buf, short maxDigits) >> 690 { >> 691 const char* p = buf; >> 692 G4int length = 0; >> 693 if(*p == '+' || *p == '-') >> 694 { >> 695 ++p; >> 696 } >> 697 if(isdigit((G4int)(*p))) >> 698 { >> 699 while(isdigit((G4int)(*p))) >> 700 { >> 701 ++p; >> 702 ++length; >> 703 } >> 704 if(*p == '\0') >> 705 { >> 706 if(length > maxDigits) >> 707 { >> 708 G4cerr << "digit length exceeds" << G4endl; >> 709 return 0; >> 710 } >> 711 return 1; >> 712 } >> 713 else >> 714 { >> 715 // G4cerr <<"illegal character after int:"<<buf<<G4endl; >> 716 } >> 717 } >> 718 else >> 719 { >> 720 // G4cerr <<"illegal int:"<<buf<<G4endl; >> 721 } >> 722 return 0; >> 723 } >> 724 >> 725 // -------------------------------------------------------------------- >> 726 G4int G4UIcommand::ExpectExponent(const char* str) // used only by IsDouble() >> 727 { >> 728 G4int maxExplength; >> 729 if(IsInt(str, maxExplength = 7)) >> 730 return 1; >> 731 else >> 732 return 0; >> 733 } >> 734 >> 735 // -------------------------------------------------------------------- >> 736 G4int G4UIcommand::IsDouble(const char* buf) >> 737 { >> 738 const char* p = buf; >> 739 switch(*p) >> 740 { >> 741 case '+': >> 742 case '-': >> 743 ++p; >> 744 if(isdigit(*p)) >> 745 { >> 746 while(isdigit((G4int)(*p))) >> 747 { >> 748 ++p; >> 749 } >> 750 switch(*p) >> 751 { >> 752 case '\0': >> 753 return 1; >> 754 // break; >> 755 case 'E': >> 756 case 'e': >> 757 return ExpectExponent(++p); >> 758 // break; >> 759 case '.': >> 760 ++p; >> 761 if(*p == '\0') >> 762 return 1; >> 763 if(*p == 'e' || *p == 'E') >> 764 return ExpectExponent(++p); >> 765 if(isdigit(*p)) >> 766 { >> 767 while(isdigit((G4int)(*p))) >> 768 { >> 769 ++p; >> 770 } >> 771 if(*p == '\0') >> 772 return 1; >> 773 if(*p == 'e' || *p == 'E') >> 774 return ExpectExponent(++p); >> 775 } >> 776 else >> 777 return 0; >> 778 break; >> 779 default: >> 780 return 0; >> 781 } >> 782 } >> 783 if(*p == '.') >> 784 { >> 785 ++p; >> 786 if(isdigit(*p)) >> 787 { >> 788 while(isdigit((G4int)(*p))) >> 789 { >> 790 ++p; >> 791 } >> 792 if(*p == '\0') >> 793 return 1; >> 794 if(*p == 'e' || *p == 'E') >> 795 return ExpectExponent(++p); >> 796 } >> 797 } >> 798 break; >> 799 case '.': >> 800 ++p; >> 801 if(isdigit(*p)) >> 802 { >> 803 while(isdigit((G4int)(*p))) >> 804 { >> 805 ++p; >> 806 } >> 807 if(*p == '\0') >> 808 return 1; >> 809 if(*p == 'e' || *p == 'E') >> 810 return ExpectExponent(++p); >> 811 } >> 812 break; >> 813 default: // digit is expected >> 814 if(isdigit(*p)) >> 815 { >> 816 while(isdigit((G4int)(*p))) >> 817 { >> 818 ++p; >> 819 } >> 820 if(*p == '\0') >> 821 return 1; >> 822 if(*p == 'e' || *p == 'E') >> 823 return ExpectExponent(++p); >> 824 if(*p == '.') >> 825 { >> 826 ++p; >> 827 if(*p == '\0') >> 828 return 1; >> 829 if(*p == 'e' || *p == 'E') >> 830 return ExpectExponent(++p); >> 831 if(isdigit(*p)) >> 832 { >> 833 while(isdigit((G4int)(*p))) >> 834 { >> 835 ++p; >> 836 } >> 837 if(*p == '\0') >> 838 return 1; >> 839 if(*p == 'e' || *p == 'E') >> 840 return ExpectExponent(++p); >> 841 } >> 842 } >> 843 } >> 844 } >> 845 return 0; >> 846 } >> 847 >> 848 // -------------------------------------------------------------------- >> 849 G4int G4UIcommand::RangeCheck(const char* t) >> 850 { >> 851 yystype result; >> 852 char type; >> 853 bp = 0; // reset buffer pointer for G4UIpGetc() >> 854 std::istringstream is(t); >> 855 for(unsigned i = 0; i < parameter.size(); ++i) >> 856 { >> 857 type = toupper(parameter[i]->GetParameterType()); >> 858 switch(type) >> 859 { >> 860 case 'D': >> 861 is >> newVal[i].D; >> 862 break; >> 863 case 'I': >> 864 is >> newVal[i].I; >> 865 break; >> 866 case 'L': >> 867 is >> newVal[i].L; >> 868 break; >> 869 case 'S': >> 870 is >> newVal[i].S; >> 871 break; >> 872 case 'B': >> 873 is >> newVal[i].C; >> 874 break; >> 875 default:; >> 876 } >> 877 } >> 878 // PrintToken(); // Print tokens (consumes all tokens) >> 879 token = Yylex(); >> 880 result = Expression(); >> 881 >> 882 if(paramERR == 1) >> 883 return 0; >> 884 if(result.type != CONSTINT) >> 885 { >> 886 G4cerr << "Illegal Expression in parameter range." << G4endl; >> 887 return 0; >> 888 } >> 889 if(result.I) >> 890 return 1; >> 891 G4cerr << "parameter out of range: " << rangeString << G4endl; >> 892 return 0; >> 893 } >> 894 >> 895 // ------------------ syntax node functions ------------------ >> 896 >> 897 yystype G4UIcommand::Expression(void) >> 898 { >> 899 yystype result; >> 900 #ifdef DEBUG >> 901 G4cerr << " Expression()" << G4endl; >> 902 #endif >> 903 result = LogicalORExpression(); >> 904 return result; >> 905 } >> 906 >> 907 // -------------------------------------------------------------------- >> 908 yystype G4UIcommand::LogicalORExpression(void) >> 909 { >> 910 yystype result; >> 911 yystype p; >> 912 p = LogicalANDExpression(); >> 913 if(token != LOGICALOR) >> 914 return p; >> 915 if(p.type == CONSTSTRING || p.type == IDENTIFIER) >> 916 { >> 917 G4cerr << "Parameter range: illegal type at '||'" << G4endl; >> 918 paramERR = 1; >> 919 } >> 920 result.I = p.I; >> 921 while(token == LOGICALOR) >> 922 { >> 923 token = Yylex(); >> 924 p = LogicalANDExpression(); >> 925 if(p.type == CONSTSTRING || p.type == IDENTIFIER) >> 926 { >> 927 G4cerr << "Parameter range: illegal type at '||'" << G4endl; >> 928 paramERR = 1; >> 929 } >> 930 switch(p.type) >> 931 { >> 932 case CONSTINT: >> 933 result.I += p.I; >> 934 result.type = CONSTINT; >> 935 break; >> 936 case CONSTLONG: >> 937 result.I += (p.L != 0L); >> 938 result.type = CONSTINT; >> 939 break; >> 940 case CONSTDOUBLE: >> 941 result.I += (p.D != 0.0); >> 942 result.type = CONSTINT; >> 943 break; >> 944 default: >> 945 G4cerr << "Parameter range: unknown type" << G4endl; >> 946 paramERR = 1; >> 947 } >> 948 } >> 949 return result; >> 950 } >> 951 >> 952 // -------------------------------------------------------------------- >> 953 yystype G4UIcommand::LogicalANDExpression(void) >> 954 { >> 955 yystype result; >> 956 yystype p; >> 957 p = EqualityExpression(); >> 958 if(token != LOGICALAND) >> 959 return p; >> 960 if(p.type == CONSTSTRING || p.type == IDENTIFIER) >> 961 { >> 962 G4cerr << "Parameter range: illegal type at '&&'" << G4endl; >> 963 paramERR = 1; >> 964 } >> 965 result.I = p.I; >> 966 while(token == LOGICALAND) >> 967 { >> 968 token = Yylex(); >> 969 p = EqualityExpression(); >> 970 if(p.type == CONSTSTRING || p.type == IDENTIFIER) >> 971 { >> 972 G4cerr << "Parameter range: illegal type at '&&'" << G4endl; >> 973 paramERR = 1; >> 974 } >> 975 switch(p.type) >> 976 { >> 977 case CONSTINT: >> 978 result.I *= p.I; >> 979 result.type = CONSTINT; >> 980 break; >> 981 case CONSTLONG: >> 982 result.I *= (p.L != 0L); >> 983 result.type = CONSTINT; >> 984 break; >> 985 case CONSTDOUBLE: >> 986 result.I *= (p.D != 0.0); >> 987 result.type = CONSTINT; >> 988 break; >> 989 default: >> 990 G4cerr << "Parameter range: unknown type." << G4endl; >> 991 paramERR = 1; >> 992 } >> 993 } >> 994 return result; >> 995 } >> 996 >> 997 // -------------------------------------------------------------------- >> 998 yystype G4UIcommand::EqualityExpression(void) >> 999 { >> 1000 yystype arg1, arg2; >> 1001 G4int operat; >> 1002 yystype result; >> 1003 #ifdef DEBUG >> 1004 G4cerr << " EqualityExpression()" << G4endl; >> 1005 #endif >> 1006 result = RelationalExpression(); >> 1007 if(token == EQ || token == NE) >> 1008 { >> 1009 operat = token; >> 1010 token = Yylex(); >> 1011 arg1 = result; >> 1012 arg2 = RelationalExpression(); >> 1013 result.I = Eval2(arg1, operat, arg2); // semantic action >> 1014 result.type = CONSTINT; >> 1015 #ifdef DEBUG >> 1016 G4cerr << " return code of Eval2(): " << result.I << G4endl; >> 1017 #endif >> 1018 } >> 1019 else >> 1020 { >> 1021 if(result.type != CONSTINT && result.type != CONSTDOUBLE) >> 1022 { >> 1023 G4cerr << "Parameter range: error at EqualityExpression" << G4endl; >> 1024 paramERR = 1; >> 1025 } >> 1026 } >> 1027 return result; >> 1028 } >> 1029 >> 1030 // -------------------------------------------------------------------- >> 1031 yystype G4UIcommand::RelationalExpression(void) >> 1032 { >> 1033 yystype arg1, arg2; >> 1034 G4int operat; >> 1035 yystype result; >> 1036 #ifdef DEBUG >> 1037 G4cerr << " RelationalExpression()" << G4endl; >> 1038 #endif >> 1039 >> 1040 arg1 = AdditiveExpression(); >> 1041 if(token == GT || token == GE || token == LT || token == LE) >> 1042 { >> 1043 operat = token; >> 1044 token = Yylex(); >> 1045 arg2 = AdditiveExpression(); >> 1046 result.I = Eval2(arg1, operat, arg2); // semantic action >> 1047 result.type = CONSTINT; >> 1048 #ifdef DEBUG >> 1049 G4cerr << " return code of Eval2(): " << result.I << G4endl; >> 1050 #endif >> 1051 } >> 1052 else >> 1053 { >> 1054 result = arg1; >> 1055 } >> 1056 #ifdef DEBUG >> 1057 G4cerr << " return RelationalExpression()" << G4endl; >> 1058 #endif >> 1059 return result; >> 1060 } >> 1061 >> 1062 // -------------------------------------------------------------------- >> 1063 yystype G4UIcommand::AdditiveExpression(void) >> 1064 { >> 1065 yystype result; >> 1066 result = MultiplicativeExpression(); >> 1067 if(token != '+' && token != '-') >> 1068 return result; >> 1069 G4cerr << "Parameter range: operator " << (char) token << " is not supported." >> 1070 << G4endl; >> 1071 paramERR = 1; >> 1072 return result; >> 1073 } >> 1074 >> 1075 // -------------------------------------------------------------------- >> 1076 yystype G4UIcommand::MultiplicativeExpression(void) >> 1077 { >> 1078 yystype result; >> 1079 result = UnaryExpression(); >> 1080 if(token != '*' && token != '/' && token != '%') >> 1081 return result; >> 1082 G4cerr << "Parameter range: operator " << (char) token << " is not supported." >> 1083 << G4endl; >> 1084 paramERR = 1; >> 1085 return result; >> 1086 } >> 1087 >> 1088 // -------------------------------------------------------------------- >> 1089 yystype G4UIcommand::UnaryExpression(void) >> 1090 { >> 1091 yystype result; >> 1092 yystype p; >> 1093 #ifdef DEBUG >> 1094 G4cerr << " UnaryExpression" << G4endl; >> 1095 #endif >> 1096 switch(token) >> 1097 { >> 1098 case '-': >> 1099 token = Yylex(); >> 1100 p = UnaryExpression(); >> 1101 if(p.type == CONSTINT) >> 1102 { >> 1103 result.I = -p.I; >> 1104 result.type = CONSTINT; >> 1105 } >> 1106 if(p.type == CONSTLONG) >> 1107 { >> 1108 result.L = -p.L; >> 1109 result.type = CONSTLONG; >> 1110 } >> 1111 if(p.type == CONSTDOUBLE) >> 1112 { >> 1113 result.D = -p.D; >> 1114 result.type = CONSTDOUBLE; >> 1115 } >> 1116 break; >> 1117 case '+': >> 1118 token = Yylex(); >> 1119 result = UnaryExpression(); >> 1120 break; >> 1121 case '!': >> 1122 token = Yylex(); >> 1123 G4cerr << "Parameter range error: " >> 1124 << "operator '!' is not supported (sorry)." << G4endl; >> 1125 paramERR = 1; >> 1126 result = UnaryExpression(); >> 1127 break; >> 1128 default: >> 1129 result = PrimaryExpression(); >> 1130 } >> 1131 return result; >> 1132 } >> 1133 >> 1134 // -------------------------------------------------------------------- >> 1135 yystype G4UIcommand::PrimaryExpression(void) >> 1136 { >> 1137 yystype result; >> 1138 #ifdef DEBUG >> 1139 G4cerr << " primary_exp" << G4endl; >> 1140 #endif >> 1141 switch(token) >> 1142 { >> 1143 case IDENTIFIER: >> 1144 result.S = yylval.S; >> 1145 result.type = token; >> 1146 token = Yylex(); >> 1147 break; >> 1148 case CONSTINT: >> 1149 result.I = yylval.I; >> 1150 result.type = token; >> 1151 token = Yylex(); >> 1152 break; >> 1153 case CONSTLONG: >> 1154 result.L = yylval.L; >> 1155 result.type = token; >> 1156 token = Yylex(); >> 1157 break; >> 1158 case CONSTDOUBLE: >> 1159 result.D = yylval.D; >> 1160 result.type = token; >> 1161 token = Yylex(); >> 1162 break; >> 1163 case '(': >> 1164 token = Yylex(); >> 1165 result = Expression(); >> 1166 if(token != ')') >> 1167 { >> 1168 G4cerr << " ')' expected" << G4endl; >> 1169 paramERR = 1; >> 1170 } >> 1171 token = Yylex(); >> 1172 break; >> 1173 default: >> 1174 return result; >> 1175 } >> 1176 return result; // never executed >> 1177 } >> 1178 >> 1179 //---------------- semantic routines ---------------------------------- >> 1180 >> 1181 G4int G4UIcommand::Eval2(yystype arg1, G4int op, yystype arg2) >> 1182 { >> 1183 char newValtype; >> 1184 if((arg1.type != IDENTIFIER) && (arg2.type != IDENTIFIER)) >> 1185 { >> 1186 G4cerr << commandName << ": meaningless comparison" << G4endl; >> 1187 paramERR = 1; >> 1188 } >> 1189 >> 1190 if(arg1.type == IDENTIFIER) >> 1191 { >> 1192 unsigned i = IndexOf(arg1.S); >> 1193 newValtype = toupper(parameter[i]->GetParameterType()); >> 1194 switch(newValtype) >> 1195 { >> 1196 case 'I': >> 1197 if(arg2.type == CONSTINT) >> 1198 { >> 1199 return CompareInt(newVal[i].I, op, arg2.I); >> 1200 //=================================================================== >> 1201 // MA - 2018.07.23 >> 1202 } >> 1203 else if(arg2.type == IDENTIFIER) >> 1204 { >> 1205 unsigned iii = IndexOf(arg2.S); >> 1206 char newValtype2 = toupper(parameter[iii]->GetParameterType()); >> 1207 if(newValtype2 == 'I') >> 1208 { >> 1209 return CompareInt(newVal[i].I, op, newVal[iii].I); >> 1210 } >> 1211 else if(newValtype2 == 'L') >> 1212 { >> 1213 G4cerr << "Warning : Integer is compared with long int : " >> 1214 << rangeString << G4endl; >> 1215 return CompareLong(newVal[i].I, op, newVal[iii].L); >> 1216 } >> 1217 else if(newValtype2 == 'D') >> 1218 { >> 1219 G4cerr << "Warning : Integer is compared with double : " >> 1220 << rangeString << G4endl; >> 1221 return CompareDouble(newVal[i].I, op, newVal[iii].D); >> 1222 } >> 1223 //=================================================================== >> 1224 } >> 1225 else >> 1226 { >> 1227 G4cerr << "integer operand expected for " << rangeString << '.' >> 1228 << G4endl; >> 1229 } >> 1230 break; >> 1231 case 'L': >> 1232 if(arg2.type == CONSTINT) >> 1233 { >> 1234 return CompareLong(newVal[i].L, op, arg2.I); >> 1235 } >> 1236 else if(arg2.type == CONSTLONG) >> 1237 { >> 1238 return CompareLong(newVal[i].L, op, arg2.L); >> 1239 } >> 1240 else if(arg2.type == IDENTIFIER) >> 1241 { >> 1242 unsigned iii = IndexOf(arg2.S); >> 1243 char newValtype2 = toupper(parameter[iii]->GetParameterType()); >> 1244 if(newValtype2 == 'I') >> 1245 { >> 1246 return CompareLong(newVal[i].L, op, newVal[iii].I); >> 1247 } >> 1248 if(newValtype2 == 'L') >> 1249 { >> 1250 return CompareLong(newVal[i].L, op, newVal[iii].L); >> 1251 } >> 1252 else if(newValtype2 == 'D') >> 1253 { >> 1254 G4cerr << "Warning : Long int is compared with double : " >> 1255 << rangeString << G4endl; >> 1256 return CompareDouble(newVal[i].L, op, newVal[iii].D); >> 1257 } >> 1258 //=================================================================== >> 1259 } >> 1260 else >> 1261 { >> 1262 G4cerr << "integer operand expected for " << rangeString << '.' >> 1263 << G4endl; >> 1264 } >> 1265 break; >> 1266 case 'D': >> 1267 if(arg2.type == CONSTDOUBLE) >> 1268 { >> 1269 return CompareDouble(newVal[i].D, op, arg2.D); >> 1270 } >> 1271 else if(arg2.type == CONSTINT) >> 1272 { // integral promotion >> 1273 return CompareDouble(newVal[i].D, op, arg2.I); >> 1274 //=================================================================== >> 1275 // MA - 2018.07.23 >> 1276 } >> 1277 else if(arg2.type == CONSTLONG) >> 1278 { >> 1279 return CompareDouble(newVal[i].D, op, arg2.L); >> 1280 } >> 1281 else if(arg2.type == IDENTIFIER) >> 1282 { >> 1283 unsigned iii = IndexOf(arg2.S); >> 1284 char newValtype2 = toupper(parameter[iii]->GetParameterType()); >> 1285 if(newValtype2 == 'I') >> 1286 { >> 1287 return CompareDouble(newVal[i].D, op, newVal[iii].I); >> 1288 } >> 1289 else if(newValtype2 == 'L') >> 1290 { >> 1291 return CompareDouble(newVal[i].D, op, newVal[iii].L); >> 1292 } >> 1293 else if(newValtype2 == 'D') >> 1294 { >> 1295 return CompareDouble(newVal[i].D, op, newVal[iii].D); >> 1296 } >> 1297 //=================================================================== >> 1298 } >> 1299 break; >> 1300 default:; >> 1301 } >> 1302 } >> 1303 if(arg2.type == IDENTIFIER) >> 1304 { >> 1305 unsigned i = IndexOf(arg2.S); >> 1306 newValtype = toupper(parameter[i]->GetParameterType()); >> 1307 switch(newValtype) >> 1308 { >> 1309 case 'I': >> 1310 if(arg1.type == CONSTINT) >> 1311 { >> 1312 return CompareInt(arg1.I, op, newVal[i].I); >> 1313 } >> 1314 else >> 1315 { >> 1316 G4cerr << "integer operand expected for " << rangeString << '.' >> 1317 << G4endl; >> 1318 } >> 1319 break; >> 1320 case 'L': >> 1321 if(arg1.type == CONSTLONG) >> 1322 { >> 1323 return CompareLong(arg1.L, op, newVal[i].L); >> 1324 } >> 1325 else >> 1326 { >> 1327 G4cerr << "long int operand expected for " << rangeString << '.' >> 1328 << G4endl; >> 1329 } >> 1330 break; >> 1331 case 'D': >> 1332 if(arg1.type == CONSTDOUBLE) >> 1333 { >> 1334 return CompareDouble(arg1.D, op, newVal[i].D); >> 1335 } >> 1336 else if(arg1.type == CONSTINT) >> 1337 { // integral promotion >> 1338 return CompareDouble(arg1.I, op, newVal[i].D); >> 1339 } >> 1340 break; >> 1341 default:; >> 1342 } >> 1343 } >> 1344 return 0; >> 1345 } >> 1346 >> 1347 // -------------------------------------------------------------------- >> 1348 G4int G4UIcommand::CompareInt(G4int arg1, G4int op, G4int arg2) >> 1349 { >> 1350 G4int result = -1; >> 1351 G4String opr; >> 1352 switch(op) >> 1353 { >> 1354 case GT: >> 1355 result = (arg1 > arg2); >> 1356 opr = ">"; >> 1357 break; >> 1358 case GE: >> 1359 result = (arg1 >= arg2); >> 1360 opr = ">="; >> 1361 break; >> 1362 case LT: >> 1363 result = (arg1 < arg2); >> 1364 opr = "<"; >> 1365 break; >> 1366 case LE: >> 1367 result = (arg1 <= arg2); >> 1368 opr = "<="; >> 1369 break; >> 1370 case EQ: >> 1371 result = (arg1 == arg2); >> 1372 opr = "=="; >> 1373 break; >> 1374 case NE: >> 1375 result = (arg1 != arg2); >> 1376 opr = "!="; >> 1377 break; >> 1378 default: >> 1379 G4cerr << "Parameter range: error at CompareInt" << G4endl; >> 1380 paramERR = 1; >> 1381 } >> 1382 #ifdef DEBUG >> 1383 G4cerr << "CompareInt " << arg1 << " " << opr << arg2 << " result: " << result >> 1384 << G4endl; >> 1385 #endif >> 1386 return result; >> 1387 } >> 1388 >> 1389 // -------------------------------------------------------------------- >> 1390 G4int G4UIcommand::CompareLong(G4long arg1, G4int op, G4long arg2) >> 1391 { >> 1392 G4int result = -1; >> 1393 G4String opr; >> 1394 switch(op) >> 1395 { >> 1396 case GT: >> 1397 result = (arg1 > arg2); >> 1398 opr = ">"; >> 1399 break; >> 1400 case GE: >> 1401 result = (arg1 >= arg2); >> 1402 opr = ">="; >> 1403 break; >> 1404 case LT: >> 1405 result = (arg1 < arg2); >> 1406 opr = "<"; >> 1407 break; >> 1408 case LE: >> 1409 result = (arg1 <= arg2); >> 1410 opr = "<="; >> 1411 break; >> 1412 case EQ: >> 1413 result = (arg1 == arg2); >> 1414 opr = "=="; >> 1415 break; >> 1416 case NE: >> 1417 result = (arg1 != arg2); >> 1418 opr = "!="; >> 1419 break; >> 1420 default: >> 1421 G4cerr << "Parameter range: error at CompareInt" << G4endl; >> 1422 paramERR = 1; >> 1423 } >> 1424 #ifdef DEBUG >> 1425 G4cerr << "CompareInt " << arg1 << " " << opr << arg2 << " result: " << result >> 1426 << G4endl; >> 1427 #endif >> 1428 return result; >> 1429 } >> 1430 >> 1431 // -------------------------------------------------------------------- >> 1432 G4int G4UIcommand::CompareDouble(G4double arg1, G4int op, G4double arg2) >> 1433 { >> 1434 G4int result = -1; >> 1435 G4String opr; >> 1436 switch(op) >> 1437 { >> 1438 case GT: >> 1439 result = (arg1 > arg2); >> 1440 opr = ">"; >> 1441 break; >> 1442 case GE: >> 1443 result = (arg1 >= arg2); >> 1444 opr = ">="; >> 1445 break; >> 1446 case LT: >> 1447 result = (arg1 < arg2); >> 1448 opr = "<"; >> 1449 break; >> 1450 case LE: >> 1451 result = (arg1 <= arg2); >> 1452 opr = "<="; >> 1453 break; >> 1454 case EQ: >> 1455 result = (arg1 == arg2); >> 1456 opr = "=="; >> 1457 break; >> 1458 case NE: >> 1459 result = (arg1 != arg2); >> 1460 opr = "!="; >> 1461 break; >> 1462 default: >> 1463 G4cerr << "Parameter range: error at CompareDouble" << G4endl; >> 1464 paramERR = 1; >> 1465 } >> 1466 #ifdef DEBUG >> 1467 G4cerr << "CompareDouble " << arg1 << " " << opr << " " << arg2 >> 1468 << " result: " << result << G4endl; >> 1469 #endif >> 1470 return result; >> 1471 } >> 1472 >> 1473 // -------------------------------------------------------------------- >> 1474 unsigned G4UIcommand::IndexOf(const char* nam) >> 1475 { >> 1476 unsigned i; >> 1477 G4String pname; >> 1478 for(i = 0; i < parameter.size(); ++i) >> 1479 { >> 1480 pname = parameter[i]->GetParameterName(); >> 1481 if(pname == nam) >> 1482 { >> 1483 return i; >> 1484 } >> 1485 } >> 1486 paramERR = 1; >> 1487 G4cerr << "parameter name:" << nam << " not found." << G4endl; >> 1488 return 0; >> 1489 } >> 1490 >> 1491 // -------------------------------------------------------------------- >> 1492 unsigned G4UIcommand::IsParameter(const char* nam) >> 1493 { >> 1494 G4String pname; >> 1495 for(unsigned i = 0; i < parameter.size(); ++i) >> 1496 { >> 1497 pname = parameter[i]->GetParameterName(); >> 1498 if(pname == nam) >> 1499 return 1; >> 1500 } >> 1501 return 0; >> 1502 } >> 1503 >> 1504 // --------------------- utility functions ---------------------------- >> 1505 >> 1506 tokenNum G4UIcommand::Yylex() // reads input and returns token number, KR486 >> 1507 { // (returns EOF) >> 1508 G4int c; >> 1509 G4String buf; >> 1510 >> 1511 while((c = G4UIpGetc()) == ' ' || c == '\t' || c == '\n') >> 1512 ; >> 1513 if(c == EOF) >> 1514 return (tokenNum) EOF; // KR488 >> 1515 buf = ""; >> 1516 if(isdigit(c) || c == '.') >> 1517 { // I or D >> 1518 do >> 1519 { >> 1520 buf += (unsigned char)c; >> 1521 c = G4UIpGetc(); >> 1522 } while(c == '.' || isdigit(c) || c == 'e' || c == 'E' || c == '+' || >> 1523 c == '-'); >> 1524 G4UIpUngetc(c); >> 1525 const char* t = buf; >> 1526 std::istringstream is(t); >> 1527 if(IsInt(buf.data(), 20)) >> 1528 { >> 1529 is >> yylval.I; >> 1530 return CONSTINT; >> 1531 } >> 1532 else if(IsDouble(buf.data())) >> 1533 { >> 1534 is >> yylval.D; >> 1535 return CONSTDOUBLE; >> 1536 } >> 1537 else >> 1538 { >> 1539 G4cerr << buf << ": numeric format error." << G4endl; >> 1540 } >> 1541 } >> 1542 buf = ""; >> 1543 if(isalpha(c) || c == '_') >> 1544 { // IDENTIFIER >> 1545 do >> 1546 { >> 1547 buf += (unsigned char) c; >> 1548 } while((c = G4UIpGetc()) != EOF && (isalnum(c) || c == '_')); >> 1549 G4UIpUngetc(c); >> 1550 if(IsParameter(buf)) >> 1551 { >> 1552 yylval.S = buf; >> 1553 return IDENTIFIER; >> 1554 } >> 1555 else >> 1556 { >> 1557 G4cerr << buf << " is not a parameter name." << G4endl; >> 1558 paramERR = 1; >> 1559 } >> 1560 } >> 1561 switch(c) >> 1562 { >> 1563 case '>': >> 1564 return (tokenNum) Follow('=', GE, GT); >> 1565 case '<': >> 1566 return (tokenNum) Follow('=', LE, LT); >> 1567 case '=': >> 1568 return (tokenNum) Follow('=', EQ, '='); >> 1569 case '!': >> 1570 return (tokenNum) Follow('=', NE, '!'); >> 1571 case '|': >> 1572 return (tokenNum) Follow('|', LOGICALOR, '|'); >> 1573 case '&': >> 1574 return (tokenNum) Follow('&', LOGICALAND, '&'); >> 1575 default: >> 1576 return (tokenNum) c; >> 1577 } >> 1578 } >> 1579 >> 1580 // -------------------------------------------------------------------- >> 1581 G4int G4UIcommand::Follow(G4int expect, G4int ifyes, G4int ifno) >> 1582 { >> 1583 G4int c = G4UIpGetc(); >> 1584 if(c == expect) >> 1585 return ifyes; >> 1586 G4UIpUngetc(c); >> 1587 return ifno; >> 1588 } >> 1589 >> 1590 //------------------ low level routines ------------------------------- >> 1591 >> 1592 G4int G4UIcommand::G4UIpGetc() >> 1593 { // emulation of getc() >> 1594 G4int length = rangeString.length(); >> 1595 if(bp < length) >> 1596 return rangeString[bp++]; >> 1597 else >> 1598 return EOF; >> 1599 } >> 1600 >> 1601 // -------------------------------------------------------------------- >> 1602 G4int G4UIcommand::G4UIpUngetc(G4int c) >> 1603 { // emulation of ungetc() >> 1604 if(c < 0) >> 1605 return -1; >> 1606 if(bp > 0 && c == rangeString[bp - 1]) >> 1607 { >> 1608 --bp; >> 1609 } >> 1610 else >> 1611 { >> 1612 G4cerr << "G4UIpUngetc() failed." << G4endl; >> 1613 G4cerr << "bp=" << bp << " c=" << c << " pR(bp-1)=" << rangeString[bp - 1] >> 1614 << G4endl; >> 1615 paramERR = 1; >> 1616 return -1; >> 1617 } >> 1618 return 0; 482 } 1619 } 483 1620