Geant4-11
Data Structures | Public Member Functions | Protected Member Functions | Protected Attributes | Private Attributes
G4GenericMessenger Class Reference

#include <G4GenericMessenger.hh>

Inheritance diagram for G4GenericMessenger:
G4UImessenger

Data Structures

struct  Command
 
struct  Method
 
struct  Property
 

Public Member Functions

G4bool CommandsShouldBeInMaster () const
 
CommandDeclareMethod (const G4String &name, const G4AnyMethod &fun, const G4String &doc="")
 
CommandDeclareMethodWithUnit (const G4String &name, const G4String &defaultUnit, const G4AnyMethod &fun, const G4String &doc="")
 
CommandDeclareProperty (const G4String &name, const G4AnyType &variable, const G4String &doc="")
 
CommandDeclarePropertyWithUnit (const G4String &name, const G4String &defaultUnit, const G4AnyType &variable, const G4String &doc="")
 
 G4GenericMessenger (void *obj, const G4String &dir="", const G4String &doc="")
 
virtual G4String GetCurrentValue (G4UIcommand *command)
 
G4bool operator!= (const G4UImessenger &messenger) const
 
G4bool operator== (const G4UImessenger &messenger) const
 
void SetDirectory (const G4String &dir)
 
void SetGuidance (const G4String &s)
 
virtual void SetNewValue (G4UIcommand *command, G4String newValue)
 
void Sort (G4bool val=true)
 
virtual ~G4GenericMessenger ()
 

Protected Member Functions

void AddUIcommand (G4UIcommand *newCommand)
 
G4String BtoS (G4bool b)
 
template<typename T >
T * CreateCommand (const G4String &cname, const G4String &dsc)
 
void CreateDirectory (const G4String &path, const G4String &dsc, G4bool commandsToBeBroadcasted=true)
 
G4String DtoS (G4double a)
 
G4String ItoS (G4int i)
 
G4bool StoB (G4String s)
 
G4double StoD (G4String s)
 
G4int StoI (G4String s)
 
G4long StoL (G4String s)
 

Protected Attributes

G4UIdirectorybaseDir = nullptr
 
G4String baseDirName = ""
 
G4bool commandsShouldBeInMaster = false
 

Private Attributes

G4UIdirectorydircmd = nullptr
 
G4String directory
 
std::map< G4String, Methodmethods
 
void * object = nullptr
 
std::map< G4String, Propertyproperties
 

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 63 of file G4GenericMessenger.cc.

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

References dircmd, and G4UIcommand::SetGuidance().

◆ ~G4GenericMessenger()

G4GenericMessenger::~G4GenericMessenger ( )
virtual

Definition at line 72 of file G4GenericMessenger.cc.

73{
74 delete dircmd;
75 for(auto i = properties.cbegin(); i != properties.cend(); ++i)
76 delete i->second.command;
77 for(auto i = methods.cbegin(); i != methods.cend(); ++i)
78 delete i->second.command;
79}
std::map< G4String, Method > methods
std::map< G4String, Property > properties

References dircmd, methods, and properties.

Member Function Documentation

◆ AddUIcommand()

void G4UImessenger::AddUIcommand ( G4UIcommand newCommand)
protectedinherited

Definition at line 149 of file G4UImessenger.cc.

150{
151 G4cerr << "Warning : Old style definition of G4UIcommand <"
152 << newCommand->GetCommandPath() << ">." << G4endl;
153}
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
const G4String & GetCommandPath() const
Definition: G4UIcommand.hh:136

References G4cerr, G4endl, and G4UIcommand::GetCommandPath().

◆ BtoS()

G4String G4UImessenger::BtoS ( G4bool  b)
protectedinherited

Definition at line 98 of file G4UImessenger.cc.

99{
100 G4String vl = "0";
101 if(b)
102 vl = "true";
103 return vl;
104}

◆ CommandsShouldBeInMaster()

G4bool G4UImessenger::CommandsShouldBeInMaster ( ) const
inlineinherited

Definition at line 77 of file G4UImessenger.hh.

78 {
80 }
G4bool commandsShouldBeInMaster

References G4UImessenger::commandsShouldBeInMaster.

Referenced by G4UIcommand::G4UIcommandCommonConstructorCode().

◆ CreateCommand()

template<typename T >
T * G4UImessenger::CreateCommand ( const G4String cname,
const G4String dsc 
)
protectedinherited

Definition at line 110 of file G4UImessenger.hh.

111{
112 G4String path;
113 if(cname[0] != '/')
114 {
115 path = baseDirName + cname;
116 if(path[0] != '/')
117 path = "/" + path;
118 }
119
120 T* command = new T(path.c_str(), this);
121 command->SetGuidance(dsc.c_str());
122
123 return command;
124}
G4String baseDirName

References G4UImessenger::baseDirName.

◆ CreateDirectory()

void G4UImessenger::CreateDirectory ( const G4String path,
const G4String dsc,
G4bool  commandsToBeBroadcasted = true 
)
protectedinherited

Definition at line 156 of file G4UImessenger.cc.

158{
160
161 G4String fullpath = path;
162 if(fullpath.back() != '/')
163 fullpath.append("/");
164
165 G4UIcommandTree* tree = ui->GetTree()->FindCommandTree(fullpath.c_str());
166 if(tree != nullptr)
167 {
168 baseDirName = tree->GetPathName();
169 }
170 else
171 {
172 baseDir = new G4UIdirectory(fullpath.c_str(), commandsToBeBroadcasted);
173 baseDirName = fullpath;
174 baseDir->SetGuidance(dsc.c_str());
175 }
176}
const G4String & GetPathName() const
G4UIcommandTree * FindCommandTree(const char *commandPath)
G4UIcommandTree * GetTree() const
Definition: G4UImanager.hh:186
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77
G4UIdirectory * baseDir

References G4UImessenger::baseDir, G4UImessenger::baseDirName, G4UIcommandTree::FindCommandTree(), G4UIcommandTree::GetPathName(), G4UImanager::GetTree(), G4UImanager::GetUIpointer(), and G4UIcommand::SetGuidance().

Referenced by G4MoleculeShootMessenger::G4MoleculeShootMessenger(), and G4UImessenger::G4UImessenger().

◆ DeclareMethod()

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

Definition at line 146 of file G4GenericMessenger.cc.

148{
149 G4String fullpath = directory + name;
150 G4UIcommand* cmd = new G4UIcommand(fullpath.c_str(), this);
151 if(doc != "")
152 cmd->SetGuidance(doc);
153 for(std::size_t i = 0; i < fun.NArg(); ++i)
154 {
155 G4String argNam = "arg" + ItoS(i);
156 char ptype = 's';
157 auto& tInfo = fun.ArgType(i);
158 if(tInfo == typeid(int) || tInfo == typeid(long) ||
159 tInfo == typeid(unsigned int) ||
160 tInfo == typeid(unsigned long))
161 { ptype = 'i'; }
162 else if(tInfo == typeid(float) || tInfo == typeid(double))
163 { ptype = 'd'; }
164 else if(tInfo == typeid(bool))
165 { ptype = 'b'; }
166 else if(tInfo == typeid(G4String))
167 { ptype = 's'; }
168 else
169 { ptype = 's'; }
170 cmd->SetParameter(new G4UIparameter(argNam, ptype, false));
171 }
172 return methods[name] = Method(fun, object, cmd);
173}
std::size_t NArg() const
Definition: G4AnyMethod.hh:158
const std::type_info & ArgType(size_t n=0) const
Definition: G4AnyMethod.hh:160
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:146
G4String ItoS(G4int i)
const char * name(G4int ptype)

References G4AnyMethod::ArgType(), directory, G4UImessenger::ItoS(), methods, G4InuclParticleNames::name(), G4AnyMethod::NArg(), G4UIcommand::SetGuidance(), and G4UIcommand::SetParameter().

◆ DeclareMethodWithUnit()

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

Definition at line 175 of file G4GenericMessenger.cc.

178{
179 G4String fullpath = directory + name;
180 if(fun.NArg() != 1)
181 {
183 ed << "G4GenericMessenger::DeclareMethodWithUnit() does not support a "
184 "method that has more than\n"
185 << "one arguments (or no argument). Please use "
186 "G4GenericMessenger::DeclareMethod method for\n"
187 << "your command <" << fullpath << ">.";
188 G4Exception("G4GenericMessenger::DeclareMethodWithUnit()", "Intercom70002",
189 FatalException, ed);
190 }
191 G4UIcommand* cmd = new G4UIcmdWithADoubleAndUnit(fullpath.c_str(), this);
192 (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))
193 ->SetParameterName("value", false, false);
194 (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetDefaultUnit(defaultUnit);
195 if(doc != "")
196 cmd->SetGuidance(doc);
197 return methods[name] = Method(fun, object, cmd);
198}
@ 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

References directory, FatalException, G4Exception(), methods, G4InuclParticleNames::name(), G4AnyMethod::NArg(), and G4UIcommand::SetGuidance().

◆ DeclareProperty()

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

Definition at line 81 of file G4GenericMessenger.cc.

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

References directory, G4InuclParticleNames::name(), properties, G4UIcommand::SetGuidance(), G4UIcommand::SetParameter(), and G4AnyType::TypeInfo().

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 115 of file G4GenericMessenger.cc.

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

References DeclareProperty(), directory, G4InuclParticleNames::name(), properties, G4UIcommand::SetGuidance(), and G4AnyType::TypeInfo().

◆ DtoS()

G4String G4UImessenger::DtoS ( G4double  a)
protectedinherited

Definition at line 90 of file G4UImessenger.cc.

91{
92 std::ostringstream os;
93 os << a;
94 return G4String(os.str());
95}

Referenced by G4ScoreQuantityMessenger::FilterCommands(), and G4UIcontrolMessenger::SetNewValue().

◆ GetCurrentValue()

G4String G4GenericMessenger::GetCurrentValue ( G4UIcommand command)
virtual

Reimplemented from G4UImessenger.

Definition at line 200 of file G4GenericMessenger.cc.

201{
202 if(properties.find(command->GetCommandName()) != properties.cend())
203 {
204 Property& p = properties[command->GetCommandName()];
205 return p.variable.ToString();
206 }
207 else if(methods.find(command->GetCommandName()) != methods.cend())
208 {
209 G4cout << " GetCurrentValue() is not available for a command defined by "
210 "G4GenericMessenger::DeclareMethod()."
211 << G4endl;
212 return G4String();
213 }
214 else
215 {
216 throw G4InvalidUICommand();
217 }
218}
G4GLOB_DLL std::ostream G4cout
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:137

References G4cout, G4endl, G4UIcommand::GetCommandName(), methods, properties, G4AnyType::ToString(), and G4GenericMessenger::Property::variable.

◆ ItoS()

G4String G4UImessenger::ItoS ( G4int  i)
protectedinherited

Definition at line 82 of file G4UImessenger.cc.

83{
84 std::ostringstream os;
85 os << i;
86 return G4String(os.str());
87}

Referenced by DeclareMethod(), and G4ParticleGunMessenger::GetCurrentValue().

◆ operator!=()

G4bool G4UImessenger::operator!= ( const G4UImessenger messenger) const
inherited

Definition at line 76 of file G4UImessenger.cc.

77{
78 return this != &messenger;
79}

◆ operator==()

G4bool G4UImessenger::operator== ( const G4UImessenger messenger) const
inherited

Definition at line 70 of file G4UImessenger.cc.

71{
72 return this == &messenger;
73}

◆ SetDirectory()

void G4GenericMessenger::SetDirectory ( const G4String dir)
inline

Definition at line 195 of file G4GenericMessenger.hh.

195{ directory = dir; }

References directory.

◆ SetGuidance()

void G4GenericMessenger::SetGuidance ( const G4String s)

Definition at line 255 of file G4GenericMessenger.cc.

256{
258}
static constexpr double s
Definition: G4SIunits.hh:154

References dircmd, s, and G4UIcommand::SetGuidance().

◆ SetNewValue()

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

Reimplemented from G4UImessenger.

Definition at line 220 of file G4GenericMessenger.cc.

221{
222 // Check if there are units on this commands
223 if(typeid(*command) == typeid(G4UIcmdWithADoubleAndUnit))
224 {
227 }
228 else if(typeid(*command) == typeid(G4UIcmdWith3VectorAndUnit))
229 {
232 }
233
234 if(properties.find(command->GetCommandName()) != properties.cend())
235 {
236 Property& p = properties[command->GetCommandName()];
237 p.variable.FromString(newValue);
238 }
239 else if(methods.find(command->GetCommandName()) != methods.cend())
240 {
241 Method& m = methods[command->GetCommandName()];
242 if(m.method.NArg() == 0)
243 m.method.operator()(m.object);
244 else if(m.method.NArg() > 0)
245 {
246 m.method.operator()(m.object, newValue);
247 }
248 else
249 {
250 throw G4InvalidUICommand();
251 }
252 }
253}
static constexpr double m
Definition: G4SIunits.hh:109
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:445
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:584
static G4ThreeVector ConvertToDimensioned3Vector(const char *st)
Definition: G4UIcommand.cc:608

References G4UIcommand::ConvertToDimensioned3Vector(), G4UIcommand::ConvertToDimensionedDouble(), G4UIcommand::ConvertToString(), G4AnyType::FromString(), G4UIcommand::GetCommandName(), m, methods, properties, and G4GenericMessenger::Property::variable.

◆ Sort()

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

Definition at line 197 of file G4GenericMessenger.hh.

198 { if(dircmd) dircmd->Sort(val); }
void Sort(G4bool val=true)

References dircmd, and G4UIdirectory::Sort().

◆ StoB()

G4bool G4UImessenger::StoB ( G4String  s)
protectedinherited

Definition at line 137 of file G4UImessenger.cc.

138{
140 G4bool vl = false;
141 if(v == "Y" || v == "YES" || v == "1" || v == "T" || v == "TRUE")
142 {
143 vl = true;
144 }
145 return vl;
146}
bool G4bool
Definition: G4Types.hh:86
G4String to_upper_copy(G4String str)
Return uppercase copy of string.

References G4StrUtil::to_upper_copy().

Referenced by G4LocalThreadCoutMessenger::SetNewValue(), G4CascadeParamMessenger::SetNewValue(), G4ScoreQuantityMessenger::SetNewValue(), and G4ScoringMessenger::SetNewValue().

◆ StoD()

G4double G4UImessenger::StoD ( G4String  s)
protectedinherited

◆ StoI()

G4int G4UImessenger::StoI ( G4String  s)
protectedinherited

◆ StoL()

G4long G4UImessenger::StoL ( G4String  s)
protectedinherited

Definition at line 117 of file G4UImessenger.cc.

118{
119 G4long vl;
120 const char* t = str;
121 std::istringstream is(t);
122 is >> vl;
123 return vl;
124}
long G4long
Definition: G4Types.hh:87

Referenced by G4RunMessenger::SetNewValue().

Field Documentation

◆ baseDir

G4UIdirectory* G4UImessenger::baseDir = nullptr
protectedinherited

◆ baseDirName

G4String G4UImessenger::baseDirName = ""
protectedinherited

◆ commandsShouldBeInMaster

G4bool G4UImessenger::commandsShouldBeInMaster = false
protectedinherited

◆ dircmd

G4UIdirectory* G4GenericMessenger::dircmd = nullptr
private

Definition at line 204 of file G4GenericMessenger.hh.

Referenced by G4GenericMessenger(), SetGuidance(), Sort(), and ~G4GenericMessenger().

◆ directory

G4String G4GenericMessenger::directory
private

◆ methods

std::map<G4String, Method> G4GenericMessenger::methods
private

◆ object

void* G4GenericMessenger::object = nullptr
private

Definition at line 206 of file G4GenericMessenger.hh.

◆ properties

std::map<G4String, Property> G4GenericMessenger::properties
private

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