Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4GenericMessenger Class Reference

#include <G4GenericMessenger.hh>

Inheritance diagram for G4GenericMessenger:

Classes

struct  Command
struct  Property
struct  Method

Public Member Functions

 G4GenericMessenger (void *obj, const G4String &dir="", const G4String &doc="")
 ~G4GenericMessenger () override
G4String GetCurrentValue (G4UIcommand *command) override
void SetNewValue (G4UIcommand *command, G4String newValue) override
CommandDeclareProperty (const G4String &name, const G4AnyType &variable, const G4String &doc="")
CommandDeclarePropertyWithUnit (const G4String &name, const G4String &defaultUnit, const G4AnyType &variable, const G4String &doc="")
CommandDeclareMethod (const G4String &name, const G4AnyMethod &fun, const G4String &doc="")
CommandDeclareMethodWithUnit (const G4String &name, const G4String &defaultUnit, const G4AnyMethod &fun, const G4String &doc="")
void SetDirectory (const G4String &dir)
void SetGuidance (const G4String &s)
void Sort (G4bool val=true)
Public Member Functions inherited from G4UImessenger
 G4UImessenger ()=default
 G4UImessenger (const G4String &path, const G4String &dsc, G4bool commandsToBeBroadcasted=true)
virtual ~G4UImessenger ()
G4bool CommandsShouldBeInMaster () const

Additional Inherited Members

Protected Member Functions inherited from G4UImessenger
G4String ItoS (G4int i)
G4String LtoS (G4long l)
G4String DtoS (G4double a)
G4String BtoS (G4bool b)
G4int StoI (const G4String &s)
G4long StoL (const G4String &s)
G4double StoD (const G4String &s)
G4bool StoB (const G4String &s)
void AddUIcommand (G4UIcommand *newCommand)
void CreateDirectory (const G4String &path, const G4String &dsc, G4bool commandsToBeBroadcasted=true)
template<typename T>
T * CreateCommand (const G4String &cname, const G4String &dsc)
Protected Attributes inherited from G4UImessenger
G4UIdirectorybaseDir = nullptr
G4String baseDirName = ""
G4bool commandsShouldBeInMaster = false

Detailed Description

Definition at line 48 of file G4GenericMessenger.hh.

Constructor & Destructor Documentation

◆ G4GenericMessenger()

G4GenericMessenger::G4GenericMessenger ( void * obj,
const G4String & dir = "",
const G4String & doc = "" )

Definition at line 69 of file G4GenericMessenger.cc.

70 : directory(dir), object(obj)
71{
72 dircmd = new G4UIdirectory(dir);
73 dircmd->SetGuidance(doc);
74}

◆ ~G4GenericMessenger()

G4GenericMessenger::~G4GenericMessenger ( )
override

Definition at line 76 of file G4GenericMessenger.cc.

77{
78 delete dircmd;
79 for (const auto& propertie : properties) {
80 delete propertie.second.command;
81 }
82 for (const auto& method : methods) {
83 delete method.second.command;
84 }
85}

Member Function Documentation

◆ DeclareMethod()

G4GenericMessenger::Command & G4GenericMessenger::DeclareMethod ( const G4String & name,
const G4AnyMethod & fun,
const G4String & doc = "" )

Definition at line 155 of file G4GenericMessenger.cc.

156{
157 G4String fullpath = directory + name;
158 auto* cmd = new G4UIcommand(fullpath.c_str(), this);
159 if (!doc.empty()) {
160 cmd->SetGuidance(doc);
161 }
162 for (std::size_t i = 0; i < fun.NArg(); ++i) {
163 G4String argNam = "arg" + ItoS((G4int)i);
164 char ptype = 's';
165 auto& tInfo = fun.ArgType(i);
166 if (tInfo == typeid(int) || tInfo == typeid(long) || tInfo == typeid(unsigned int)
167 || tInfo == typeid(unsigned long))
168 {
169 ptype = 'i';
170 }
171 else if (tInfo == typeid(float) || tInfo == typeid(double)) {
172 ptype = 'd';
173 }
174 else if (tInfo == typeid(bool)) {
175 ptype = 'b';
176 }
177 else if (tInfo == typeid(G4String)) {
178 ptype = 's';
179 }
180 else {
181 ptype = 's';
182 }
183 cmd->SetParameter(new G4UIparameter(argNam, ptype, false));
184 }
185 return methods[name] = Method(fun, object, cmd);
186}
int G4int
Definition G4Types.hh:85
std::size_t NArg() const
const std::type_info & ArgType(size_t n=0) const
G4String ItoS(G4int i)
const char * name(G4int ptype)

◆ DeclareMethodWithUnit()

G4GenericMessenger::Command & G4GenericMessenger::DeclareMethodWithUnit ( const G4String & name,
const G4String & defaultUnit,
const G4AnyMethod & fun,
const G4String & doc = "" )

Definition at line 188 of file G4GenericMessenger.cc.

192{
193 G4String fullpath = directory + name;
194 if (fun.NArg() != 1) {
196 ed << "G4GenericMessenger::DeclareMethodWithUnit() does not support a "
197 "method that has more than\n"
198 << "one arguments (or no argument). Please use "
199 "G4GenericMessenger::DeclareMethod method for\n"
200 << "your command <" << fullpath << ">.";
201 G4Exception("G4GenericMessenger::DeclareMethodWithUnit()", "Intercom70002", FatalException, ed);
202 }
203 G4UIcommand* cmd = new G4UIcmdWithADoubleAndUnit(fullpath.c_str(), this);
204 (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetParameterName("value", false, false);
205 (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetDefaultUnit(defaultUnit);
206 if (!doc.empty()) {
207 cmd->SetGuidance(doc);
208 }
209 return methods[name] = Method(fun, object, cmd);
210}
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
void SetGuidance(const char *aGuidance)

◆ DeclareProperty()

G4GenericMessenger::Command & G4GenericMessenger::DeclareProperty ( const G4String & name,
const G4AnyType & variable,
const G4String & doc = "" )

Definition at line 88 of file G4GenericMessenger.cc.

89{
90 G4String fullpath = directory + name;
91 G4UIcommand* cmd = nullptr;
92 if (var.TypeInfo() == typeid(G4ThreeVector)) {
93 cmd = new G4UIcmdWith3Vector(fullpath.c_str(), this);
94 (static_cast<G4UIcmdWith3Vector*>(cmd))
95 ->SetParameterName("valueX", "valueY", "valueZ", false, false);
96 }
97 else {
98 cmd = new G4UIcommand(fullpath.c_str(), this);
99 char ptype;
100 if (var.TypeInfo() == typeid(int) || var.TypeInfo() == typeid(long)
101 || var.TypeInfo() == typeid(unsigned int) || var.TypeInfo() == typeid(unsigned long))
102 {
103 ptype = 'i';
104 }
105 else if (var.TypeInfo() == typeid(float) || var.TypeInfo() == typeid(double)) {
106 ptype = 'd';
107 }
108 else if (var.TypeInfo() == typeid(bool)) {
109 ptype = 'b';
110 }
111 else if (var.TypeInfo() == typeid(G4String)) {
112 ptype = 's';
113 }
114 else {
115 ptype = 's';
116 }
117 cmd->SetParameter(new G4UIparameter("value", ptype, false));
118 }
119 if (!doc.empty()) {
120 cmd->SetGuidance(doc);
121 }
122 return properties[name] = Property(var, cmd);
123}
CLHEP::Hep3Vector G4ThreeVector
void SetParameter(G4UIparameter *const newParameter)

Referenced by DeclarePropertyWithUnit().

◆ DeclarePropertyWithUnit()

G4GenericMessenger::Command & G4GenericMessenger::DeclarePropertyWithUnit ( const G4String & name,
const G4String & defaultUnit,
const G4AnyType & variable,
const G4String & doc = "" )

Definition at line 126 of file G4GenericMessenger.cc.

128{
129 if (var.TypeInfo() != typeid(float) && var.TypeInfo() != typeid(double)
130 && var.TypeInfo() != typeid(G4ThreeVector))
131 {
132 return DeclareProperty(name, var, doc);
133 }
134 G4String fullpath = directory + name;
135 G4UIcommand* cmd;
136 if (var.TypeInfo() == typeid(float) || var.TypeInfo() == typeid(double)) {
137 cmd = new G4UIcmdWithADoubleAndUnit(fullpath.c_str(), this);
138 (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetParameterName("value", false, false);
139 (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetDefaultUnit(defaultUnit);
140 }
141 else {
142 cmd = new G4UIcmdWith3VectorAndUnit(fullpath.c_str(), this);
143 (static_cast<G4UIcmdWith3VectorAndUnit*>(cmd))
144 ->SetParameterName("valueX", "valueY", "valueZ", false, false);
145 (static_cast<G4UIcmdWith3VectorAndUnit*>(cmd))->SetDefaultUnit(defaultUnit);
146 }
147
148 if (!doc.empty()) {
149 cmd->SetGuidance(doc);
150 }
151 return properties[name] = Property(var, cmd);
152}
Command & DeclareProperty(const G4String &name, const G4AnyType &variable, const G4String &doc="")

◆ GetCurrentValue()

G4String G4GenericMessenger::GetCurrentValue ( G4UIcommand * command)
overridevirtual

Reimplemented from G4UImessenger.

Definition at line 212 of file G4GenericMessenger.cc.

213{
214 if (properties.find(command->GetCommandName()) != properties.cend()) {
215 Property& p = properties[command->GetCommandName()];
216 return p.variable.ToString();
217 }
218 if (methods.find(command->GetCommandName()) != methods.cend()) {
219 G4cout << " GetCurrentValue() is not available for a command defined by "
220 "G4GenericMessenger::DeclareMethod()."
221 << G4endl;
222 return G4String();
223 }
224
225 throw G4InvalidUICommand();
226}
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout
std::string ToString() const
Definition G4AnyType.hh:124
const G4String & GetCommandName() const

◆ SetDirectory()

void G4GenericMessenger::SetDirectory ( const G4String & dir)
inline

Definition at line 171 of file G4GenericMessenger.hh.

171{ directory = dir; }

◆ SetGuidance()

void G4GenericMessenger::SetGuidance ( const G4String & s)

Definition at line 276 of file G4GenericMessenger.cc.

277{
278 dircmd->SetGuidance(s);
279}

◆ SetNewValue()

void G4GenericMessenger::SetNewValue ( G4UIcommand * command,
G4String newValue )
overridevirtual

Reimplemented from G4UImessenger.

Definition at line 228 of file G4GenericMessenger.cc.

229{
230 // Check if there are units on this commands
231 if (typeid(*command) == typeid(G4UIcmdWithADoubleAndUnit)) {
233 }
234 else if (typeid(*command) == typeid(G4UIcmdWith3VectorAndUnit)) {
236 }
237 else if (typeid(*command) == typeid(G4UIcmdWithABool)) {
238 if(StoB(newValue)) {
239 newValue = "1";
240 } else {
241 newValue = "0";
242 }
243 }
244
245 if (properties.find(command->GetCommandName()) != properties.cend()) {
246 Property& p = properties[command->GetCommandName()];
247 p.variable.FromString(newValue);
248 }
249 else if (methods.find(command->GetCommandName()) != methods.cend()) {
250 Method& m = methods[command->GetCommandName()];
251 if (m.method.NArg() == 0) {
252 m.method.operator()(m.object);
253 }
254 else if (m.method.NArg() > 0) {
255 G4Tokenizer tokens(newValue);
256 G4String paraValue;
257 for (std::size_t i = 0; i < m.method.NArg(); ++i) {
258 G4String aToken = tokens();
259 if(m.method.ArgType(i)==typeid(bool)) {
260 if(StoB(aToken)) {
261 aToken = "1";
262 } else {
263 aToken = "0";
264 }
265 }
266 paraValue += aToken + " ";
267 }
268 m.method.operator()(m.object, paraValue);
269 }
270 else {
271 throw G4InvalidUICommand();
272 }
273 }
274}
void FromString(const std::string &val)
Definition G4AnyType.hh:126
static G4String ConvertToString(G4bool boolVal)
static G4double ConvertToDimensionedDouble(const char *st)
static G4ThreeVector ConvertToDimensioned3Vector(const char *st)
G4bool StoB(const G4String &s)

◆ Sort()

void G4GenericMessenger::Sort ( G4bool val = true)
inline

Definition at line 173 of file G4GenericMessenger.hh.

174 {
175 if (dircmd != nullptr) {
176 dircmd->Sort(val);
177 }
178 }

The documentation for this class was generated from the following files: