Geant4  10.02.p03
G4GenericMessenger Class Reference

This class is generic messenger. More...

#include <G4GenericMessenger.hh>

Inheritance diagram for G4GenericMessenger:
Collaboration diagram for G4GenericMessenger:

Classes

struct  Command
 
struct  Method
 
struct  Property
 

Public Member Functions

 G4GenericMessenger (void *obj, const G4String &dir="", const G4String &doc="")
 Contructor. More...
 
virtual ~G4GenericMessenger ()
 Destructor. More...
 
virtual G4String GetCurrentValue (G4UIcommand *command)
 The concrete, but generic implementation of this method. More...
 
virtual void SetNewValue (G4UIcommand *command, G4String newValue)
 The concrete, generic implementation of this method converts the string "newValue" to action. More...
 
CommandDeclareProperty (const G4String &name, const G4AnyType &variable, const G4String &doc="")
 Declare Methods. More...
 
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 ()
 
G4bool operator== (const G4UImessenger &messenger) const
 
G4bool CommandsShouldBeInMaster () const
 

Private Attributes

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

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, G4bool commandsToBeBroadcasted=true)
 
template<typename T >
T * CreateCommand (const G4String &cname, const G4String &dsc)
 
- Protected Attributes inherited from G4UImessenger
G4UIdirectorybaseDir
 
G4String baseDirName
 
G4bool commandsShouldBeInMaster
 

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 }
Float_t d
G4UIdirectory * dircmd
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
static const G4double pos
Here is the call graph for this function:

◆ ~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 }
std::map< G4String, Method > methods
for(Int_t i=0;i< nentries;i++)
Definition: comparison.C:30
G4UIdirectory * dircmd
std::map< G4String, Property > properties
Here is the call graph for this function:

Member Function Documentation

◆ DeclareMethod()

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

Definition at line 112 of file G4GenericMessenger.cc.

112  {
113  G4String fullpath = directory+name;
114  G4UIcommand* cmd = new G4UIcommand(fullpath.c_str(), this);
115  if(doc != "") cmd->SetGuidance(doc);
116  for (size_t i = 0; i < fun.NArg(); i++) {
117  cmd->SetParameter(new G4UIparameter("arg", 's', false));
118  }
119  return methods[name] = Method(fun, object, cmd);
120 }
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:152
std::map< G4String, Method > methods
G4String name
Definition: TRTMaterials.hh:40
size_t NArg() const
Definition: G4AnyMethod.hh:139
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DeclareMethodWithUnit()

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

Definition at line 123 of file G4GenericMessenger.cc.

123  {
124  G4String fullpath = directory+name;
125  if(fun.NArg()!=1) {
127  ed<<"G4GenericMessenger::DeclareMethodWithUnit() does not support a method that has more than\n"
128  <<"one arguments (or no argument). Please use G4GenericMessenger::DeclareMethod method for\n"
129  <<"your command <"<<fullpath<<">.";
130  G4Exception("G4GenericMessenger::DeclareMethodWithUnit()","Intercom70002",FatalException,ed);
131  }
132  G4UIcommand* cmd = new G4UIcmdWithADoubleAndUnit(fullpath.c_str(), this);
133  (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetParameterName("value",false,false);
134  (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetDefaultUnit(defaultUnit);
135  if(doc != "") cmd->SetGuidance(doc);
136  return methods[name] = Method(fun, object, cmd);
137 }
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
std::map< G4String, Method > methods
G4String name
Definition: TRTMaterials.hh:40
size_t NArg() const
Definition: G4AnyMethod.hh:139
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 }
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:152
G4String name
Definition: TRTMaterials.hh:40
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
std::map< G4String, Property > properties
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DeclarePropertyWithUnit()

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

Definition at line 88 of file G4GenericMessenger.cc.

88  {
89  if(var.TypeInfo()!=typeid(float) && var.TypeInfo()!=typeid(double) && var.TypeInfo()!= typeid(G4ThreeVector))
90  { return DeclareProperty(name,var,doc); }
91  G4String fullpath = directory+name;
92  G4UIcommand* cmd;
93  if(var.TypeInfo()==typeid(float) || var.TypeInfo()==typeid(double))
94  {
95  cmd = new G4UIcmdWithADoubleAndUnit(fullpath.c_str(), this);
96  (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetParameterName("value",false,false);
97  (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetDefaultUnit(defaultUnit);
98  }
99  else
100  {
101  cmd = new G4UIcmdWith3VectorAndUnit(fullpath.c_str(), this);
102  (static_cast<G4UIcmdWith3VectorAndUnit*>(cmd))->SetParameterName("valueX","valueY","valueZ",false,false);
103  (static_cast<G4UIcmdWith3VectorAndUnit*>(cmd))->SetDefaultUnit(defaultUnit);
104  }
105 
106  if(doc != "") cmd->SetGuidance(doc);
107  return properties[name] = Property(var, cmd);
108 }
CLHEP::Hep3Vector G4ThreeVector
Command & DeclareProperty(const G4String &name, const G4AnyType &variable, const G4String &doc="")
Declare Methods.
G4String name
Definition: TRTMaterials.hh:40
void SetParameterName(const char *theNameX, const char *theNameY, const char *theNameZ, G4bool omittable, G4bool currentAsDefault=false)
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
std::map< G4String, Property > properties
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetCurrentValue()

G4String G4GenericMessenger::GetCurrentValue ( G4UIcommand command)
virtual

The concrete, but generic implementation of this method.

Reimplemented from G4UImessenger.

Definition at line 140 of file G4GenericMessenger.cc.

140  {
141  if ( properties.find(command->GetCommandName()) != properties.end()) {
142  Property& p = properties[command->GetCommandName()];
143  return p.variable.ToString();
144  }
145  else if ( methods.find(command->GetCommandName()) != methods.end()) {
146  G4cout<<" GetCurrentValue() is not available for a command defined by G4GenericMessenger::DeclareMethod()."<<G4endl;
147  return G4String();
148  }
149  else {
150  throw G4InvalidUICommand();
151  }
152 }
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:141
std::map< G4String, Method > methods
G4GLOB_DLL std::ostream G4cout
std::map< G4String, Property > properties
#define G4endl
Definition: G4ios.hh:61
Here is the call graph for this function:

◆ SetDirectory()

void G4GenericMessenger::SetDirectory ( const G4String dir)
inline

Definition at line 103 of file G4GenericMessenger.hh.

103 {directory = dir;}
TDirectory * dir
Here is the call graph for this function:

◆ SetGuidance()

void G4GenericMessenger::SetGuidance ( const G4String s)

Definition at line 181 of file G4GenericMessenger.cc.

181  {
182  dircmd->SetGuidance(s);
183 }
G4UIdirectory * dircmd
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
Here is the call graph for this function:

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

154  {
155  // Check if there are units on this commands
156  if (typeid(*command) == typeid(G4UIcmdWithADoubleAndUnit)) {
158  }
159  else if (typeid(*command) == typeid(G4UIcmdWith3VectorAndUnit)) {
161  }
162 
163  if ( properties.find(command->GetCommandName()) != properties.end()) {
164  Property& p = properties[command->GetCommandName()];
165  p.variable.FromString(newValue);
166  }
167  else if (methods.find(command->GetCommandName()) != methods.end()) {
168  Method& m = methods[command->GetCommandName()];
169  if(m.method.NArg() == 0)
170  m.method.operator()(m.object);
171  else if (m.method.NArg() > 0) {
172  m.method.operator()(m.object,newValue);
173  }
174  else {
175  throw G4InvalidUICommand();
176  }
177  }
178 }
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:141
std::map< G4String, Method > methods
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:371
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:451
std::map< G4String, Property > properties
static const double m
Definition: G4SIunits.hh:128
static G4ThreeVector ConvertToDimensioned3Vector(const char *st)
Definition: G4UIcommand.cc:473
Here is the call graph for this function:

Member Data Documentation

◆ dircmd

G4UIdirectory* G4GenericMessenger::dircmd
private

Definition at line 109 of file G4GenericMessenger.hh.

◆ directory

G4String G4GenericMessenger::directory
private

Definition at line 110 of file G4GenericMessenger.hh.

◆ methods

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

Definition at line 108 of file G4GenericMessenger.hh.

◆ object

void* G4GenericMessenger::object
private

Definition at line 111 of file G4GenericMessenger.hh.

◆ properties

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

Definition at line 107 of file G4GenericMessenger.hh.


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