Problem 2606

Summary: commands from G4GenericMessenger::DeclareMethod wrongly convert valid boolean parameters
Product: Geant4 Reporter: Manuel <g4bz>
Component: intercomsAssignee: Makoto Asai <asai>
Status: RESOLVED FIXED    
Severity: minor    
Priority: P4    
Version: 11.1   
Hardware: All   
OS: All   
Attachments: example code to reproduce the issue.

Description Manuel 2024-04-11 22:16:06 CEST
Created attachment 864 [details]
example code to reproduce the issue.

=== Summary
boolean args passed to a command registered with G4GenericMessenger::DeclareMethod are not correctly converted to bools from the command string. (Correctly = consistent with the rest of G4's command parsing code). Only "0" and "1" are correctly converted to bools, "true"/"y"/"yes"/... are silently converted to false(!). 


=== Steps to reproduce:

See the attached example code (not actually compile tested, just stripped down from our real code). It has to be added to another Geant4-based application, i.e. an example, and will provide a simple macro command that takes one boolean argument. Testing it yields unexpected results:

```
/Test/Flag ABC
# -> error message (as expected, this is not a valid bool)
/Test/Flag 1
# -> true (ok)
/Test/Flag 0
# -> false (ok)
/Test/Flag true
# -> false (!)
/Test/Flag y
# -> false (!)
```

The check for valid boolean parameter is always performed correctly. If the specified parameter is not valid in the eyes of Geant4 (valid are i.e. yes/no/y/n/true/false/0/1...) an error message is correctly raised. So that "first layer" of parameter evaluation works correctly.

But then the actual casting of the string to bool before calling Test::SetFlag does not take into account any of Geant4's "special" bool handling. No warning message is output.

=== Notes

https://github.com/Geant4/geant4/blob/dda54bbdcfe0cc76177eaa03a657592aac0b1eb8/source/intercoms/include/G4AnyMethod.hh#L182-L184

I suppose that the line `strs >> a0` does not use any of G4's type conversion functionality, but just from the c++ stdlib. And that only converts "1" to true, and all other strings silently(!) to false... The same also applies to FuncRef2.
Comment 1 Makoto Asai 2024-04-24 20:14:58 CEST
Thanks for reporting this issue. The fix will be included in the next patch release.