Geant4_10
G4GenericMessenger.cc
Go to the documentation of this file.
1 // ********************************************************************
2 // * License and Disclaimer *
3 // * *
4 // * The Geant4 software is copyright of the Copyright Holders of *
5 // * the Geant4 Collaboration. It is provided under the terms and *
6 // * conditions of the Geant4 Software License, included in the file *
7 // * LICENSE and available at http://cern.ch/geant4/license . These *
8 // * include a list of copyright holders. *
9 // * *
10 // * Neither the authors of this software system, nor their employing *
11 // * institutes,nor the agencies providing financial support for this *
12 // * work make any representation or warranty, express or implied, *
13 // * regarding this software system or assume any liability for its *
14 // * use. Please see the license in the file LICENSE and URL above *
15 // * for the full disclaimer and the limitation of liability. *
16 // * *
17 // * This code implementation is the result of the scientific and *
18 // * technical work of the GEANT4 collaboration. *
19 // * By using, copying, modifying or distributing the software (or *
20 // * any work based on the software) you agree to acknowledge its *
21 // * use in resulting scientific publications, and indicate your *
22 // * acceptance of all terms of the Geant4 Software license. *
23 // ********************************************************************
24 //
25 //
26 // $Id: G4UIaliasList.cc,v 1.6 2006-06-29 19:08:33 gunter Exp $
27 //
28 
29 #include "G4GenericMessenger.hh"
30 #include "G4Types.hh"
31 #include "G4UImessenger.hh"
32 #include "G4UIcommand.hh"
35 #include "G4UIdirectory.hh"
36 
37 #include <iostream>
38 
39 class G4InvalidUICommand: public std::bad_cast {
40 public:
42  virtual const char* what() const throw() {
43  return "G4InvalidUICommand: command does not exists or is of invalid type";
44  }
45 };
46 
47 
48 G4GenericMessenger::G4GenericMessenger(void* obj, const G4String& dir, const G4String& doc): 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 }
62 
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 }
68 
69 
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 }
85 
86 
88 (const G4String& name, const G4String& defaultUnit, const G4AnyType& var, const G4String& doc) {
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 }
109 
110 
112 G4GenericMessenger::DeclareMethod(const G4String& name, const G4AnyMethod& fun, const G4String& doc) {
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 }
121 
123  (const G4String& name, const G4String& defaultUnit, const G4AnyMethod& fun, const G4String& doc) {
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 }
138 
139 
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 }
149 
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 }
175 
176 
178  dircmd->SetGuidance(s);
179 }
180 
181 
183  // Change the type of command (unfortunatelly this is done a posteriory)
184  // We need to delete the old command before creating the new one and therefore we need to recover the information
185  // before the deletetion
186 #ifdef G4MULTITHREADED
187  G4String cmdpath = command->GetCommandPath();
189  ed<<"G4GenericMessenger::Command::SetUnit() is thread-unsafe and should not be used\n"
190  <<"in multi-threaded mode. For your command <"<<cmdpath<<">, use\n"
191  <<" DeclarePropertyWithUnit(const G4String& name, const G4String& defaultUnit,\n"
192  <<" const G4AnyType& variable, const G4String& doc)\n"
193  <<"or\n"
194  <<" DeclareMethodWithUnit(const G4String& name, const G4String& defaultUnit,\n"
195  <<" const G4AnyType& variable, const G4String& doc)\n"
196  <<"to define a command with a unit <"<<unit<<">.";
197  if(spec!=UnitDefault) { ed<<"\nPlease use a default unit instead of unit category."; }
198  G4Exception("G4GenericMessenger::Command::SetUnit()","Intercom70001",FatalException,ed);
199  return *this;
200 #else
201  G4String cmdpath = command->GetCommandPath();
202  G4UImessenger* messenger = command->GetMessenger();
204  std::vector<G4String> guidance;
205  G4String par_name = command->GetParameter(0)->GetParameterName();
206  bool par_omitable = command->GetParameter(0)->IsOmittable();
207  for (G4int i = 0; i < command->GetGuidanceEntries(); i++) guidance.push_back(command->GetGuidanceLine(i));
208  // Before deleting the command we need to add a fake one to avoid deleting the directory entry and with its guidance
209  G4UIcommand tmp((cmdpath+"_tmp").c_str(), messenger);
210  delete command;
211 
212  if (*type == typeid(float) || *type == typeid(double) ) {
213  G4UIcmdWithADoubleAndUnit* cmd_t = new G4UIcmdWithADoubleAndUnit(cmdpath, messenger);
214  if(spec == UnitDefault) cmd_t->SetDefaultUnit(unit);
215  else if(spec == UnitCategory) cmd_t->SetUnitCategory(unit);
216  cmd_t->SetParameterName(par_name, par_omitable);
217  command = cmd_t;
218  }
219  else if (*type == typeid(G4ThreeVector)) {
220  G4UIcmdWith3VectorAndUnit* cmd_t = new G4UIcmdWith3VectorAndUnit(cmdpath, messenger);
221  if(spec == UnitDefault) cmd_t->SetDefaultUnit(unit);
222  else if(spec == UnitCategory) cmd_t->SetUnitCategory(unit);
223  command = cmd_t;
224  }
225  else {
226  G4cerr << "Only parameters of type <double> or <float> can be associated with units" << G4endl;
227  return *this;
228  }
229  for (size_t i = 0; i < guidance.size(); i++) command->SetGuidance(guidance[i]);
230  command->SetRange(range);
231  return *this;
232 #endif
233 }
234 
236  G4UIparameter* theParam = command->GetParameter(0);
237  theParam->SetParameterName(name);
238  theParam->SetOmittable(omittable);
239  theParam->SetCurrentAsDefault(currentAsDefault);
240  return *this;
241 }
242 
244  G4UIparameter * theParam = command->GetParameter(0);
245  theParam->SetParameterCandidates(candList);
246  return *this;
247 }
248 
250  G4UIparameter * theParam = command->GetParameter(0);
251  theParam->SetDefaultValue(defVal);
252  return *this;
253 }
254 
255 
256 
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:152
virtual G4String GetCurrentValue(G4UIcommand *command)
The concrete, but generic implementation of this method.
Float_t tmp
Definition: plot.C:37
char cmd[1024]
Definition: tracer.cxx:25
const G4String & GetRange() const
Definition: G4UIcommand.hh:133
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
Float_t d
Definition: plot.C:237
CLHEP::Hep3Vector G4ThreeVector
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
const XML_Char * s
Definition: expat.h:262
void SetOmittable(G4bool om)
void SetParameterCandidates(const char *theString)
const std::type_info * type
for(Int_t i=0;i< nentries;i++)
Definition: comparison.C:30
Command & DeclareProperty(const G4String &name, const G4AnyType &variable, const G4String &doc="")
Declare Methods.
const char * p
Definition: xmltok.h:285
G4String GetParameterName() const
void SetDefaultUnit(const char *defUnit)
void SetUnitCategory(const char *unitCategory)
Command & DeclareMethod(const G4String &name, const G4AnyMethod &fun, const G4String &doc="")
void SetDefaultValue(const char *theDefaultValue)
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:357
const XML_Char * name
Definition: expat.h:151
virtual const char * what() const
Command & DeclareMethodWithUnit(const G4String &name, const G4String &defaultUnit, const G4AnyMethod &fun, const G4String &doc="")
void SetUnitCategory(const char *unitCategory)
void SetParameterName(const char *theName)
const std::type_info & TypeInfo() const
Definition: G4AnyType.hh:106
int G4int
Definition: G4Types.hh:78
void SetParameterName(const char *theNameX, const char *theNameY, const char *theNameZ, G4bool omittable, G4bool currentAsDefault=false)
const G4String & GetGuidanceLine(G4int i) const
Definition: G4UIcommand.hh:137
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:437
void SetCurrentAsDefault(G4bool val)
virtual void SetNewValue(G4UIcommand *command, G4String newValue)
The concrete, generic implementation of this method converts the string &quot;newValue&quot; to action...
G4bool IsOmittable() const
Command & SetDefaultValue(const G4String &)
void FromString(const std::string &val)
Definition: G4AnyType.hh:118
REAL *8 function var(A, B, C, D)
Definition: dpm25nuc1.f:4649
std::string ToString() const
Definition: G4AnyType.hh:114
size_t NArg() const
Definition: G4AnyMethod.hh:126
const G4ParticleDefinition const G4Material *G4double range
G4GenericMessenger(void *obj, const G4String &dir="", const G4String &doc="")
Contructor.
bool G4bool
Definition: G4Types.hh:79
void SetRange(const char *rs)
Definition: G4UIcommand.hh:125
virtual ~G4GenericMessenger()
Destructor.
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
G4UIparameter * GetParameter(G4int i) const
Definition: G4UIcommand.hh:145
const G4String & GetCommandPath() const
Definition: G4UIcommand.hh:139
void SetGuidance(const G4String &s)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:141
Command & SetCandidates(const G4String &)
void SetDefaultUnit(const char *defUnit)
G4UImessenger * GetMessenger() const
Definition: G4UIcommand.hh:149
Command & SetParameterName(const G4String &, G4bool, G4bool=false)
#define G4endl
Definition: G4ios.hh:61
G4int GetGuidanceEntries() const
Definition: G4UIcommand.hh:135
Command & DeclarePropertyWithUnit(const G4String &name, const G4String &defaultUnit, const G4AnyType &variable, const G4String &doc="")
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
TDirectory * dir
Definition: macro.C:5
G4GLOB_DLL std::ostream G4cerr
static G4ThreeVector ConvertToDimensioned3Vector(const char *st)
Definition: G4UIcommand.cc:459
Command & SetUnit(const G4String &, UnitSpec=UnitDefault)