Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Data Structures | Public Member Functions
G4GenericMessenger Class Reference

This class is generic messenger. More...

#include <G4GenericMessenger.hh>

Inheritance diagram for G4GenericMessenger:
G4UImessenger

Data Structures

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
 

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
 

Detailed Description

This class is generic messenger.

Definition at line 46 of file G4GenericMessenger.hh.

Constructor & Destructor Documentation

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

Contructor.

Definition at line 48 of file G4GenericMessenger.cc.

References G4UIcommand::SetGuidance().

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:161
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

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

Definition at line 112 of file G4GenericMessenger.cc.

References cmd, G4AnyMethod::NArg(), G4UIcommand::SetGuidance(), and G4UIcommand::SetParameter().

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
char cmd[1024]
Definition: tracer.cxx:25
const XML_Char * name
size_t NArg() const
Definition: G4AnyMethod.hh:126
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
G4GenericMessenger::Command & G4GenericMessenger::DeclareMethodWithUnit ( const G4String name,
const G4String defaultUnit,
const G4AnyMethod fun,
const G4String doc = "" 
)

Definition at line 123 of file G4GenericMessenger.cc.

References cmd, FatalException, G4Exception(), G4AnyMethod::NArg(), G4UIcommand::SetGuidance(), and G4UIcmdWithADoubleAndUnit::SetParameterName().

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 }
char cmd[1024]
Definition: tracer.cxx:25
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
const XML_Char * name
size_t NArg() const
Definition: G4AnyMethod.hh:126
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)
G4GenericMessenger::Command & G4GenericMessenger::DeclareProperty ( const G4String name,
const G4AnyType variable,
const G4String doc = "" 
)

Declare Methods.

Definition at line 71 of file G4GenericMessenger.cc.

References cmd, int(), G4UIcommand::SetGuidance(), G4UIcommand::SetParameter(), and G4AnyType::TypeInfo().

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
char cmd[1024]
Definition: tracer.cxx:25
real *8 function var(A, B, C, D)
Definition: dpm25nuc1.f:4649
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
const XML_Char * name
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
G4GenericMessenger::Command & G4GenericMessenger::DeclarePropertyWithUnit ( const G4String name,
const G4String defaultUnit,
const G4AnyType variable,
const G4String doc = "" 
)

Definition at line 88 of file G4GenericMessenger.cc.

References cmd, G4UIcommand::SetGuidance(), G4UIcmdWithADoubleAndUnit::SetParameterName(), G4UIcmdWith3VectorAndUnit::SetParameterName(), and G4AnyType::TypeInfo().

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;
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 }
char cmd[1024]
Definition: tracer.cxx:25
real *8 function var(A, B, C, D)
Definition: dpm25nuc1.f:4649
CLHEP::Hep3Vector G4ThreeVector
Command & DeclareProperty(const G4String &name, const G4AnyType &variable, const G4String &doc="")
Declare Methods.
const XML_Char * name
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
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
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.

References G4UIcommand::GetCommandName(), G4AnyType::ToString(), and G4GenericMessenger::Property::variable.

140  {
141  if ( properties.find(command->GetCommandName()) != properties.end()) {
142  Property& p = properties[command->GetCommandName()];
143  return p.variable.ToString();
144  }
145  else {
146  throw G4InvalidUICommand();
147  }
148 }
const char * p
Definition: xmltok.h:285
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:141
void G4GenericMessenger::SetDirectory ( const G4String dir)
inline

Definition at line 100 of file G4GenericMessenger.hh.

100 {directory = dir;}
void G4GenericMessenger::SetGuidance ( const G4String s)

Definition at line 177 of file G4GenericMessenger.cc.

References G4UIcommand::SetGuidance().

177  {
178  dircmd->SetGuidance(s);
179 }
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
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 150 of file G4GenericMessenger.cc.

References G4UIcommand::ConvertToDimensioned3Vector(), G4UIcommand::ConvertToDimensionedDouble(), G4UIcommand::ConvertToString(), G4AnyType::FromString(), G4UIcommand::GetCommandName(), python.hepunit::m, G4GenericMessenger::Method::method, G4AnyMethod::NArg(), G4GenericMessenger::Method::object, and G4GenericMessenger::Property::variable.

150  {
151  // Check if there are units on this commands
152  if (typeid(*command) == typeid(G4UIcmdWithADoubleAndUnit)) {
154  }
155  else if (typeid(*command) == typeid(G4UIcmdWith3VectorAndUnit)) {
157  }
158 
159  if ( properties.find(command->GetCommandName()) != properties.end()) {
160  Property& p = properties[command->GetCommandName()];
161  p.variable.FromString(newValue);
162  }
163  else if (methods.find(command->GetCommandName()) != methods.end()) {
164  Method& m = methods[command->GetCommandName()];
165  if(m.method.NArg() == 0)
166  m.method.operator()(m.object);
167  else if (m.method.NArg() > 0) {
168  m.method.operator()(m.object,newValue);
169  }
170  else {
171  throw G4InvalidUICommand();
172  }
173  }
174 }
const char * p
Definition: xmltok.h:285
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:357
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:437
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:141
static G4ThreeVector ConvertToDimensioned3Vector(const char *st)
Definition: G4UIcommand.cc:459

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