Geant4 10.7.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  Method
 
struct  Property
 

Public Member Functions

 G4GenericMessenger (void *obj, const G4String &dir="", const G4String &doc="")
 
virtual ~G4GenericMessenger ()
 
virtual G4String GetCurrentValue (G4UIcommand *command)
 
virtual void SetNewValue (G4UIcommand *command, G4String newValue)
 
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)
 
- Public Member Functions inherited from G4UImessenger
 G4UImessenger ()
 
 G4UImessenger (const G4String &path, const G4String &dsc, G4bool commandsToBeBroadcasted=true)
 
virtual ~G4UImessenger ()
 
virtual G4String GetCurrentValue (G4UIcommand *command)
 
virtual void SetNewValue (G4UIcommand *command, G4String newValue)
 
G4bool operator== (const G4UImessenger &messenger) const
 
G4bool operator!= (const G4UImessenger &messenger) const
 
G4bool CommandsShouldBeInMaster () const
 

Additional Inherited Members

- Protected Member Functions inherited from G4UImessenger
G4String ItoS (G4int i)
 
G4String DtoS (G4double a)
 
G4String BtoS (G4bool b)
 
G4int StoI (G4String s)
 
G4long StoL (G4String s)
 
G4double StoD (G4String s)
 
G4bool StoB (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 52 of file G4GenericMessenger.cc.

54 : directory(dir)
55 , object(obj)
56{
57 // Check if parent commnand is already existing.
58 // In fact there is no way to check this. UImanager->GetTree()->FindPath()
59 // will always rerurn NULL is a dicrectory is given
60 std::size_t pos = dir.find_last_of('/', dir.size() - 2);
61 while(pos != 0 && pos != std::string::npos)
62 {
63 G4UIdirectory* d = new G4UIdirectory(dir.substr(0, pos + 1).c_str());
64 G4String guidance = "Commands for ";
65 guidance += dir.substr(1, pos - 1);
66 d->SetGuidance(guidance);
67 pos = dir.find_last_of('/', pos - 1);
68 }
69 dircmd = new G4UIdirectory(dir);
70 dircmd->SetGuidance(doc);
71}
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:156

◆ ~G4GenericMessenger()

G4GenericMessenger::~G4GenericMessenger ( )
virtual

Definition at line 73 of file G4GenericMessenger.cc.

74{
75 delete dircmd;
76 for(auto i = properties.cbegin(); i != properties.cend(); ++i)
77 delete i->second.command;
78 for(auto i = methods.cbegin(); i != methods.cend(); ++i)
79 delete i->second.command;
80}

Member Function Documentation

◆ DeclareMethod()

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

Definition at line 137 of file G4GenericMessenger.cc.

139{
140 G4String fullpath = directory + name;
141 G4UIcommand* cmd = new G4UIcommand(fullpath.c_str(), this);
142 if(doc != "")
143 cmd->SetGuidance(doc);
144 for(std::size_t i = 0; i < fun.NArg(); ++i)
145 {
146 cmd->SetParameter(new G4UIparameter("arg", 's', false));
147 }
148 return methods[name] = Method(fun, object, cmd);
149}
std::size_t NArg() const
Definition: G4AnyMethod.hh:158
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:146
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 151 of file G4GenericMessenger.cc.

154{
155 G4String fullpath = directory + name;
156 if(fun.NArg() != 1)
157 {
159 ed << "G4GenericMessenger::DeclareMethodWithUnit() does not support a "
160 "method that has more than\n"
161 << "one arguments (or no argument). Please use "
162 "G4GenericMessenger::DeclareMethod method for\n"
163 << "your command <" << fullpath << ">.";
164 G4Exception("G4GenericMessenger::DeclareMethodWithUnit()", "Intercom70002",
165 FatalException, ed);
166 }
167 G4UIcommand* cmd = new G4UIcmdWithADoubleAndUnit(fullpath.c_str(), this);
168 (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))
169 ->SetParameterName("value", false, false);
170 (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetDefaultUnit(defaultUnit);
171 if(doc != "")
172 cmd->SetGuidance(doc);
173 return methods[name] = Method(fun, object, cmd);
174}
@ FatalException
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40

◆ DeclareProperty()

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

Definition at line 82 of file G4GenericMessenger.cc.

84{
85 G4String fullpath = directory + name;
86 G4UIcommand* cmd = new G4UIcommand(fullpath.c_str(), this);
87 if(doc != "")
88 cmd->SetGuidance(doc);
89 char ptype;
90 if(var.TypeInfo() == typeid(int) || var.TypeInfo() == typeid(long) ||
91 var.TypeInfo() == typeid(unsigned int) ||
92 var.TypeInfo() == typeid(unsigned long))
93 ptype = 'i';
94 else if(var.TypeInfo() == typeid(float) || var.TypeInfo() == typeid(double))
95 ptype = 'd';
96 else if(var.TypeInfo() == typeid(bool))
97 ptype = 'b';
98 else if(var.TypeInfo() == typeid(G4String))
99 ptype = 's';
100 else
101 ptype = 's';
102 cmd->SetParameter(new G4UIparameter("value", ptype, false));
103 return properties[name] = Property(var, cmd);
104}

Referenced by DeclarePropertyWithUnit().

◆ DeclarePropertyWithUnit()

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

Definition at line 106 of file G4GenericMessenger.cc.

109{
110 if(var.TypeInfo() != typeid(float) && var.TypeInfo() != typeid(double) &&
111 var.TypeInfo() != typeid(G4ThreeVector))
112 {
113 return DeclareProperty(name, var, doc);
114 }
115 G4String fullpath = directory + name;
116 G4UIcommand* cmd;
117 if(var.TypeInfo() == typeid(float) || var.TypeInfo() == typeid(double))
118 {
119 cmd = new G4UIcmdWithADoubleAndUnit(fullpath.c_str(), this);
120 (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))
121 ->SetParameterName("value", false, false);
122 (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetDefaultUnit(defaultUnit);
123 }
124 else
125 {
126 cmd = new G4UIcmdWith3VectorAndUnit(fullpath.c_str(), this);
127 (static_cast<G4UIcmdWith3VectorAndUnit*>(cmd))
128 ->SetParameterName("valueX", "valueY", "valueZ", false, false);
129 (static_cast<G4UIcmdWith3VectorAndUnit*>(cmd))->SetDefaultUnit(defaultUnit);
130 }
131
132 if(doc != "")
133 cmd->SetGuidance(doc);
134 return properties[name] = Property(var, cmd);
135}
Command & DeclareProperty(const G4String &name, const G4AnyType &variable, const G4String &doc="")

◆ GetCurrentValue()

G4String G4GenericMessenger::GetCurrentValue ( G4UIcommand command)
virtual

Reimplemented from G4UImessenger.

Definition at line 176 of file G4GenericMessenger.cc.

177{
178 if(properties.find(command->GetCommandName()) != properties.cend())
179 {
180 Property& p = properties[command->GetCommandName()];
181 return p.variable.ToString();
182 }
183 else if(methods.find(command->GetCommandName()) != methods.cend())
184 {
185 G4cout << " GetCurrentValue() is not available for a command defined by "
186 "G4GenericMessenger::DeclareMethod()."
187 << G4endl;
188 return G4String();
189 }
190 else
191 {
192 throw G4InvalidUICommand();
193 }
194}
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:137

◆ SetDirectory()

void G4GenericMessenger::SetDirectory ( const G4String dir)
inline

Definition at line 190 of file G4GenericMessenger.hh.

190{ directory = dir; }

◆ SetGuidance()

void G4GenericMessenger::SetGuidance ( const G4String s)

Definition at line 231 of file G4GenericMessenger.cc.

232{
233 dircmd->SetGuidance(s);
234}

◆ SetNewValue()

void G4GenericMessenger::SetNewValue ( G4UIcommand command,
G4String  newValue 
)
virtual

Reimplemented from G4UImessenger.

Definition at line 196 of file G4GenericMessenger.cc.

197{
198 // Check if there are units on this commands
199 if(typeid(*command) == typeid(G4UIcmdWithADoubleAndUnit))
200 {
203 }
204 else if(typeid(*command) == typeid(G4UIcmdWith3VectorAndUnit))
205 {
208 }
209
210 if(properties.find(command->GetCommandName()) != properties.cend())
211 {
212 Property& p = properties[command->GetCommandName()];
213 p.variable.FromString(newValue);
214 }
215 else if(methods.find(command->GetCommandName()) != methods.cend())
216 {
217 Method& m = methods[command->GetCommandName()];
218 if(m.method.NArg() == 0)
219 m.method.operator()(m.object);
220 else if(m.method.NArg() > 0)
221 {
222 m.method.operator()(m.object, newValue);
223 }
224 else
225 {
226 throw G4InvalidUICommand();
227 }
228 }
229}
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:430
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:570
static G4ThreeVector ConvertToDimensioned3Vector(const char *st)
Definition: G4UIcommand.cc:594

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