Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 // G4UIcommand 27 // 28 // Class description: 29 // 30 // This G4UIcommand is the "concrete" base class which represents a command 31 // used by Geant4 (G)UI. The user can use this class in case the parameter 32 // arguments of a command are not suitable with respect to the derived command 33 // classes. 34 // Some methods defined in this base class are used by the derived classes 35 36 // Author: Makoto Asai (SLAC), 1998 37 // -------------------------------------------------------------------- 38 #ifndef G4UIcommand_hh 39 #define G4UIcommand_hh 1 40 41 #include "G4ApplicationState.hh" 42 #include "G4ThreeVector.hh" 43 #include "G4UIparameter.hh" 44 #include "globals.hh" 45 46 #include <vector> 47 48 class G4UImessenger; 49 50 class G4UIcommand 51 { 52 public: 53 // Dummy default constructor 54 G4UIcommand() = default; 55 56 // Constructor. The command string with full path directory 57 // and the pointer to the messenger must be given. 58 // If tBB is set to false, this command won't be sent to worker threads. 59 // This tBB parameter could be changed with SetToBeBroadcasted() method 60 // except for G4UIdirectory 61 G4UIcommand(const char* theCommandPath, G4UImessenger* theMessenger, G4bool tBB = true); 62 63 virtual ~G4UIcommand(); 64 65 G4bool operator==(const G4UIcommand& right) const; 66 G4bool operator!=(const G4UIcommand& right) const; 67 68 virtual G4int DoIt(const G4String& parameterList); 69 70 G4String GetCurrentValue(); 71 72 // These methods define the states where the command is available. 73 // Once one of these commands is invoked, the command application will 74 // be denied when Geant4 is NOT in the assigned states 75 void AvailableForStates(G4ApplicationState s1); 76 void AvailableForStates(G4ApplicationState s1, G4ApplicationState s2); 77 void AvailableForStates(G4ApplicationState s1, G4ApplicationState s2, G4ApplicationState s3); 78 void AvailableForStates(G4ApplicationState s1, G4ApplicationState s2, G4ApplicationState s3, 79 G4ApplicationState s4); 80 void AvailableForStates(G4ApplicationState s1, G4ApplicationState s2, G4ApplicationState s3, 81 G4ApplicationState s4, G4ApplicationState s5); 82 83 G4bool IsAvailable(); 84 85 virtual void List(); 86 87 // Static methods for conversion from value(s) to a string. 88 // These methods are to be used by GetCurrentValues() methods 89 // of concrete messengers 90 static G4String ConvertToString(G4bool boolVal); 91 static G4String ConvertToString(G4int intValue); 92 static G4String ConvertToString(G4long longValue); 93 static G4String ConvertToString(G4double doubleValue); 94 static G4String ConvertToString(G4double doubleValue, const char* unitName); 95 static G4String ConvertToString(const G4ThreeVector& vec); 96 static G4String ConvertToString(const G4ThreeVector& vec, const char* unitName); 97 98 // Static methods for conversion from a string to a value of the returning 99 // type. These methods are to be used directly by SetNewValues() methods 100 // of concrete messengers, or GetNewXXXValue() of classes derived from 101 // this G4UIcommand class 102 static G4bool ConvertToBool(const char* st); 103 static G4int ConvertToInt(const char* st); 104 static G4long ConvertToLongInt(const char* st); 105 static G4double ConvertToDouble(const char* st); 106 static G4double ConvertToDimensionedDouble(const char* st); 107 static G4ThreeVector ConvertTo3Vector(const char* st); 108 static G4ThreeVector ConvertToDimensioned3Vector(const char* st); 109 110 // Static methods for unit and its category 111 static G4double ValueOf(const char* unitName); 112 static G4String CategoryOf(const char* unitName); 113 static G4String UnitsList(const char* unitCategory); 114 115 // Defines the range the command parameter(s) can take. 116 // The variable name(s) appear in the range expression must be the same 117 // as the name(s) of the parameter(s). 118 // All the C++ syntax of relational operators are allowed for the 119 // range expression 120 inline void SetRange(const char* rs) { rangeExpression = rs; } 121 122 inline const G4String& GetRange() const { return rangeExpression; } 123 inline std::size_t GetGuidanceEntries() const { return commandGuidance.size(); } 124 inline const G4String& GetGuidanceLine(G4int i) const { return commandGuidance[i]; } 125 inline const G4String& GetCommandPath() const { return commandPath; } 126 inline const G4String& GetCommandName() const { return commandName; } 127 inline std::size_t GetParameterEntries() const { return parameter.size(); } 128 inline G4UIparameter* GetParameter(G4int i) const { return parameter[i]; } 129 inline std::vector<G4ApplicationState>* GetStateList() { return &availabelStateList; } 130 inline G4UImessenger* GetMessenger() const { return messenger; } 131 132 // Defines a parameter. This method is used by the derived command 133 // classes but the user can directly use this command when defining 134 // a command, without using the derived class. For this case, the order 135 // of the parameters is the order of invoking this method 136 inline void SetParameter(G4UIparameter* const newParameter) 137 { 138 parameter.push_back(newParameter); 139 } 140 141 // Adds a guidance line. Unlimited times of invokation of this method is 142 // allowed. The given lines of guidance will appear for the help. 143 // The first line of the guidance will be used as the title of the 144 // command, i.e. one line list of the commands 145 inline void SetGuidance(const char* aGuidance) { commandGuidance.emplace_back(aGuidance); } 146 147 inline const G4String GetTitle() const 148 { 149 return (commandGuidance.empty()) ? G4String("...Title not available...") : commandGuidance[0]; 150 } 151 152 inline void SetToBeBroadcasted(G4bool val) { toBeBroadcasted = val; } 153 inline G4bool ToBeBroadcasted() const { return toBeBroadcasted; } 154 inline void SetToBeFlushed(G4bool val) { toBeFlushed = val; } 155 inline G4bool ToBeFlushed() const { return toBeFlushed; } 156 inline void SetWorkerThreadOnly(G4bool val = true) { workerThreadOnly = val; } 157 inline G4bool IsWorkerThreadOnly() const { return workerThreadOnly; } 158 159 inline void CommandFailed(G4int errCode, G4ExceptionDescription& ed) 160 { 161 commandFailureCode = errCode; 162 failureDescription = ed.str(); 163 } 164 inline void CommandFailed(G4ExceptionDescription& ed) 165 { 166 commandFailureCode = 1; 167 failureDescription = ed.str(); 168 } 169 inline G4int IfCommandFailed() { return commandFailureCode; } 170 inline const G4String& GetFailureDescription() { return failureDescription; } 171 inline void ResetFailure() 172 { 173 commandFailureCode = 0; 174 failureDescription = ""; 175 } 176 177 public: 178 enum CommandType 179 { 180 BaseClassCmd, 181 WithoutParameterCmd, 182 WithABoolCmd, 183 WithAnIntegerCmd, 184 WithALongIntCmd, 185 WithADoubleCmd, 186 WithADoubleAndUnitCmd, 187 With3VectorCmd, 188 With3VectorAndUnitCmd, 189 WithAStringCmd, 190 CmdDirectory = -1 191 }; 192 193 inline CommandType GetCommandType() const { return commandType; } 194 void SetCommandType(CommandType); 195 196 inline void SetDefaultSortFlag(G4bool val) { ifSort = val; } 197 198 protected: 199 G4int CheckNewValue(const char* newValue); 200 201 G4bool toBeBroadcasted = false; 202 G4bool toBeFlushed = false; 203 G4bool workerThreadOnly = false; 204 205 G4int commandFailureCode = 0; 206 G4String failureDescription = ""; 207 208 G4bool ifSort = false; 209 210 private: 211 void G4UIcommandCommonConstructorCode(const char* theCommandPath); 212 213 private: 214 CommandType commandType = BaseClassCmd; 215 G4UImessenger* messenger = nullptr; 216 G4String commandPath; 217 G4String commandName; 218 G4String rangeExpression; 219 std::vector<G4UIparameter*> parameter; 220 std::vector<G4String> commandGuidance; 221 std::vector<G4ApplicationState> availabelStateList; 222 }; 223 224 #endif 225