Geant4 11.1.1
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="")
 
 ~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 ()
 
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 (const G4String &s)
 
G4long StoL (const G4String &s)
 
G4double StoD (const 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 62 of file G4GenericMessenger.cc.

64 : directory(dir)
65 , object(obj)
66{
67 dircmd = new G4UIdirectory(dir);
68 dircmd->SetGuidance(doc);
69}
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:157

◆ ~G4GenericMessenger()

G4GenericMessenger::~G4GenericMessenger ( )
override

Definition at line 71 of file G4GenericMessenger.cc.

72{
73 delete dircmd;
74 for(const auto& propertie : properties)
75 {
76 delete propertie.second.command;
77 }
78 for(const auto& method : methods)
79 {
80 delete method.second.command;
81 }
82}

Member Function Documentation

◆ DeclareMethod()

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

Definition at line 151 of file G4GenericMessenger.cc.

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

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

◆ DeclareProperty()

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

Definition at line 84 of file G4GenericMessenger.cc.

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

Referenced by DeclarePropertyWithUnit(), and G4SteppingVerboseWithUnits::SetManager().

◆ DeclarePropertyWithUnit()

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

Definition at line 118 of file G4GenericMessenger.cc.

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

◆ GetCurrentValue()

G4String G4GenericMessenger::GetCurrentValue ( G4UIcommand command)
overridevirtual

Reimplemented from G4UImessenger.

Definition at line 209 of file G4GenericMessenger.cc.

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

◆ SetDirectory()

void G4GenericMessenger::SetDirectory ( const G4String dir)
inline

Definition at line 193 of file G4GenericMessenger.hh.

193{ directory = dir; }

◆ SetGuidance()

void G4GenericMessenger::SetGuidance ( const G4String s)

Definition at line 266 of file G4GenericMessenger.cc.

267{
268 dircmd->SetGuidance(s);
269}

◆ SetNewValue()

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

Reimplemented from G4UImessenger.

Definition at line 229 of file G4GenericMessenger.cc.

230{
231 // Check if there are units on this commands
232 if(typeid(*command) == typeid(G4UIcmdWithADoubleAndUnit))
233 {
236 }
237 else if(typeid(*command) == typeid(G4UIcmdWith3VectorAndUnit))
238 {
241 }
242
243 if(properties.find(command->GetCommandName()) != properties.cend())
244 {
245 Property& p = properties[command->GetCommandName()];
246 p.variable.FromString(newValue);
247 }
248 else if(methods.find(command->GetCommandName()) != methods.cend())
249 {
250 Method& m = methods[command->GetCommandName()];
251 if(m.method.NArg() == 0)
252 {
253 m.method.operator()(m.object);
254 }
255 else if(m.method.NArg() > 0)
256 {
257 m.method.operator()(m.object, newValue);
258 }
259 else
260 {
261 throw G4InvalidUICommand();
262 }
263 }
264}
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:446
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:588
static G4ThreeVector ConvertToDimensioned3Vector(const char *st)
Definition: G4UIcommand.cc:612

◆ Sort()

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

Definition at line 195 of file G4GenericMessenger.hh.

196 {
197 if(dircmd != nullptr)
198 {
199 dircmd->Sort(val);
200 }
201 }
void Sort(G4bool val=true)

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