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

This class is generic messenger. More...

#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="")
 Contructor.
 
virtual ~G4GenericMessenger ()
 Destructor.
 
virtual G4String GetCurrentValue (G4UIcommand *command)
 The concrete, but generic implementation of this method.
 
virtual void SetNewValue (G4UIcommand *command, G4String newValue)
 The concrete, generic implementation of this method converts the string "newValue" to action.
 
CommandDeclareProperty (const G4String &name, const G4AnyType &variable, const G4String &doc="")
 Declare Methods.
 
CommandDeclareMethod (const G4String &name, 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)
 
virtual ~G4UImessenger ()
 
virtual G4String GetCurrentValue (G4UIcommand *command)
 
virtual void SetNewValue (G4UIcommand *command, G4String newValue)
 
G4bool operator== (const G4UImessenger &messenger) 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)
 
G4double StoD (G4String s)
 
G4bool StoB (G4String s)
 
void AddUIcommand (G4UIcommand *newCommand)
 
void CreateDirectory (const G4String &path, const G4String &dsc)
 
template<typename T >
T * CreateCommand (const G4String &cname, const G4String &dsc)
 
- Protected Attributes inherited from G4UImessenger
G4UIdirectorybaseDir
 
G4String baseDirName
 

Detailed Description

This class is generic messenger.

Definition at line 46 of file G4GenericMessenger.hh.

Constructor & Destructor Documentation

◆ G4GenericMessenger()

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

Contructor.

Definition at line 48 of file G4GenericMessenger.cc.

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

◆ ~G4GenericMessenger()

G4GenericMessenger::~G4GenericMessenger ( )
virtual

Destructor.

Definition at line 63 of file G4GenericMessenger.cc.

63 {
64 delete dircmd;
65 for (std::map<G4String, Property>::iterator i = properties.begin(); i != properties.end(); i++) delete i->second.command;
66 for (std::map<G4String, Method>::iterator i = methods.begin(); i != methods.end(); i++) delete i->second.command;
67}

Member Function Documentation

◆ DeclareMethod()

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

Definition at line 88 of file G4GenericMessenger.cc.

88 {
89 G4String fullpath = directory+name;
90 G4UIcommand* cmd = new G4UIcommand(fullpath.c_str(), this);
91 if(doc != "") cmd->SetGuidance(doc);
92 for (size_t i = 0; i < fun.NArg(); i++) {
93 cmd->SetParameter(new G4UIparameter("arg", 's', false));
94 }
95 return methods[name] = Method(fun, object, cmd);
96}
size_t NArg() const
Definition: G4AnyMethod.hh:126
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:147

◆ DeclareProperty()

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

Declare Methods.

Definition at line 71 of file G4GenericMessenger.cc.

71 {
72 G4String fullpath = directory+name;
73 G4UIcommand* cmd = new G4UIcommand(fullpath.c_str(), this);
74 if(doc != "") cmd->SetGuidance(doc);
75 char ptype;
76 if(var.TypeInfo() == typeid(int) || var.TypeInfo() == typeid(long) ||
77 var.TypeInfo() == typeid(unsigned int) || var.TypeInfo() == typeid(unsigned long)) ptype = 'i';
78 else if(var.TypeInfo() == typeid(float) || var.TypeInfo() == typeid(double)) ptype = 'd';
79 else if(var.TypeInfo() == typeid(bool)) ptype = 'b';
80 else if(var.TypeInfo() == typeid(G4String)) ptype = 's';
81 else ptype = 's';
82 cmd->SetParameter(new G4UIparameter("value", ptype, false));
83 return properties[name] = Property(var, cmd);
84}

◆ GetCurrentValue()

G4String G4GenericMessenger::GetCurrentValue ( G4UIcommand command)
virtual

The concrete, but generic implementation of this method.

Reimplemented from G4UImessenger.

Definition at line 98 of file G4GenericMessenger.cc.

98 {
99 if ( properties.find(command->GetCommandName()) != properties.end()) {
100 Property& p = properties[command->GetCommandName()];
101 return p.variable.ToString();
102 }
103 else {
104 throw G4InvalidUICommand();
105 }
106}
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:136

◆ SetDirectory()

void G4GenericMessenger::SetDirectory ( const G4String dir)
inline

Definition at line 98 of file G4GenericMessenger.hh.

98{directory = dir;}

◆ SetGuidance()

void G4GenericMessenger::SetGuidance ( const G4String s)

Definition at line 135 of file G4GenericMessenger.cc.

135 {
136 dircmd->SetGuidance(s);
137}

◆ SetNewValue()

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

The concrete, generic implementation of this method converts the string "newValue" to action.

Reimplemented from G4UImessenger.

Definition at line 108 of file G4GenericMessenger.cc.

108 {
109 // Check if there are units on this commands
110 if (typeid(*command) == typeid(G4UIcmdWithADoubleAndUnit)) {
112 }
113 else if (typeid(*command) == typeid(G4UIcmdWith3VectorAndUnit)) {
115 }
116
117 if ( properties.find(command->GetCommandName()) != properties.end()) {
118 Property& p = properties[command->GetCommandName()];
119 p.variable.FromString(newValue);
120 }
121 else if (methods.find(command->GetCommandName()) != methods.end()) {
122 Method& m = methods[command->GetCommandName()];
123 if(m.method.NArg() == 0)
124 m.method.operator()(m.object);
125 else if (m.method.NArg() > 0) {
126 m.method.operator()(m.object,newValue);
127 }
128 else {
129 throw G4InvalidUICommand();
130 }
131 }
132}
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:349
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:429
static G4ThreeVector ConvertToDimensioned3Vector(const char *st)
Definition: G4UIcommand.cc:451

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