Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4GenericMessenger.cc
이 파일의 문서화 페이지로 가기
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 #include "G4Threading.hh"
37 
38 #include <iostream>
39 
40 class G4InvalidUICommand: public std::bad_cast {
41 public:
43  virtual const char* what() const throw() {
44  return "G4InvalidUICommand: command does not exists or is of invalid type";
45  }
46 };
47 
48 
49 G4GenericMessenger::G4GenericMessenger(void* obj, const G4String& dir, const G4String& doc): directory(dir), object(obj) {
50  // Check if parent commnand is already existing.
51  // In fact there is no way to check this. UImanager->GetTree()->FindPath() will always rerurn NULL is a dicrectory is given
52  size_t pos = dir.find_last_of('/', dir.size()-2);
53  while(pos != 0 && pos != std::string::npos) {
54  G4UIdirectory* d = new G4UIdirectory(dir.substr(0,pos+1).c_str());
55  G4String guidance = "Commands for ";
56  guidance += dir.substr(1,pos-1);
57  d->SetGuidance(guidance);
58  pos = dir.find_last_of('/', pos-1);
59  }
60  dircmd = new G4UIdirectory(dir);
61  dircmd->SetGuidance(doc);
62 }
63 
65  delete dircmd;
66  for (std::map<G4String, Property>::iterator i = properties.begin(); i != properties.end(); i++) delete i->second.command;
67  for (std::map<G4String, Method>::iterator i = methods.begin(); i != methods.end(); i++) delete i->second.command;
68 }
69 
70 
73  G4String fullpath = directory+name;
74  G4UIcommand* cmd = new G4UIcommand(fullpath.c_str(), this);
75  if(doc != "") cmd->SetGuidance(doc);
76  char ptype;
77  if(var.TypeInfo() == typeid(int) || var.TypeInfo() == typeid(long) ||
78  var.TypeInfo() == typeid(unsigned int) || var.TypeInfo() == typeid(unsigned long)) ptype = 'i';
79  else if(var.TypeInfo() == typeid(float) || var.TypeInfo() == typeid(double)) ptype = 'd';
80  else if(var.TypeInfo() == typeid(bool)) ptype = 'b';
81  else if(var.TypeInfo() == typeid(G4String)) ptype = 's';
82  else ptype = 's';
83  cmd->SetParameter(new G4UIparameter("value", ptype, false));
84  return properties[name] = Property(var, cmd);
85 }
86 
87 
89 (const G4String& name, const G4String& defaultUnit, const G4AnyType& var, const G4String& doc) {
90  if(var.TypeInfo()!=typeid(float) && var.TypeInfo()!=typeid(double) && var.TypeInfo()!= typeid(G4ThreeVector))
91  { return DeclareProperty(name,var,doc); }
92  G4String fullpath = directory+name;
93  G4UIcommand* cmd;
94  if(var.TypeInfo()==typeid(float) || var.TypeInfo()==typeid(double))
95  {
96  cmd = new G4UIcmdWithADoubleAndUnit(fullpath.c_str(), this);
97  (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetParameterName("value",false,false);
98  (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetDefaultUnit(defaultUnit);
99  }
100  else
101  {
102  cmd = new G4UIcmdWith3VectorAndUnit(fullpath.c_str(), this);
103  (static_cast<G4UIcmdWith3VectorAndUnit*>(cmd))->SetParameterName("valueX","valueY","valueZ",false,false);
104  (static_cast<G4UIcmdWith3VectorAndUnit*>(cmd))->SetDefaultUnit(defaultUnit);
105  }
106 
107  if(doc != "") cmd->SetGuidance(doc);
108  return properties[name] = Property(var, cmd);
109 }
110 
111 
113 G4GenericMessenger::DeclareMethod(const G4String& name, const G4AnyMethod& fun, const G4String& doc) {
114  G4String fullpath = directory+name;
115  G4UIcommand* cmd = new G4UIcommand(fullpath.c_str(), this);
116  if(doc != "") cmd->SetGuidance(doc);
117  for (size_t i = 0; i < fun.NArg(); i++) {
118  cmd->SetParameter(new G4UIparameter("arg", 's', false));
119  }
120  return methods[name] = Method(fun, object, cmd);
121 }
122 
124  (const G4String& name, const G4String& defaultUnit, const G4AnyMethod& fun, const G4String& doc) {
125  G4String fullpath = directory+name;
126  if(fun.NArg()!=1) {
128  ed<<"G4GenericMessenger::DeclareMethodWithUnit() does not support a method that has more than\n"
129  <<"one arguments (or no argument). Please use G4GenericMessenger::DeclareMethod method for\n"
130  <<"your command <"<<fullpath<<">.";
131  G4Exception("G4GenericMessenger::DeclareMethodWithUnit()","Intercom70002",FatalException,ed);
132  }
133  G4UIcommand* cmd = new G4UIcmdWithADoubleAndUnit(fullpath.c_str(), this);
134  (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetParameterName("value",false,false);
135  (static_cast<G4UIcmdWithADoubleAndUnit*>(cmd))->SetDefaultUnit(defaultUnit);
136  if(doc != "") cmd->SetGuidance(doc);
137  return methods[name] = Method(fun, object, cmd);
138 }
139 
140 
142  if ( properties.find(command->GetCommandName()) != properties.end()) {
143  Property& p = properties[command->GetCommandName()];
144  return p.variable.ToString();
145  }
146  else if ( methods.find(command->GetCommandName()) != methods.end()) {
147  G4cout<<" GetCurrentValue() is not available for a command defined by G4GenericMessenger::DeclareMethod()."<<G4endl;
148  return G4String();
149  }
150  else {
151  throw G4InvalidUICommand();
152  }
153 }
154 
156  // Check if there are units on this commands
157  if (typeid(*command) == typeid(G4UIcmdWithADoubleAndUnit)) {
159  }
160  else if (typeid(*command) == typeid(G4UIcmdWith3VectorAndUnit)) {
162  }
163 
164  if ( properties.find(command->GetCommandName()) != properties.end()) {
165  Property& p = properties[command->GetCommandName()];
166  p.variable.FromString(newValue);
167  }
168  else if (methods.find(command->GetCommandName()) != methods.end()) {
169  Method& m = methods[command->GetCommandName()];
170  if(m.method.NArg() == 0)
171  m.method.operator()(m.object);
172  else if (m.method.NArg() > 0) {
173  m.method.operator()(m.object,newValue);
174  }
175  else {
176  throw G4InvalidUICommand();
177  }
178  }
179 }
180 
181 
183  dircmd->SetGuidance(s);
184 }
185 
187  // Change the type of command (unfortunatelly this is done a posteriory)
188  // We need to delete the old command before creating the new one and therefore we need to recover the information
189  // before the deletetion
191  G4String cmdpath = command->GetCommandPath();
193  ed<<"G4GenericMessenger::Command::SetUnit() is thread-unsafe and should not be used\n"
194  <<"in multi-threaded mode. For your command <"<<cmdpath<<">, use\n"
195  <<" DeclarePropertyWithUnit(const G4String& name, const G4String& defaultUnit,\n"
196  <<" const G4AnyType& variable, const G4String& doc)\n"
197  <<"or\n"
198  <<" DeclareMethodWithUnit(const G4String& name, const G4String& defaultUnit,\n"
199  <<" const G4AnyType& variable, const G4String& doc)\n"
200  <<"to define a command with a unit <"<<unit<<">.";
201  if(spec!=UnitDefault) { ed<<"\nPlease use a default unit instead of unit category."; }
202  G4Exception("G4GenericMessenger::Command::SetUnit()","Intercom70001",FatalException,ed);
203  return *this;
204  }
205 
206  G4String cmdpath = command->GetCommandPath();
207  G4UImessenger* messenger = command->GetMessenger();
209  std::vector<G4String> guidance;
210  G4String par_name = command->GetParameter(0)->GetParameterName();
211  bool par_omitable = command->GetParameter(0)->IsOmittable();
212  for (G4int i = 0; i < command->GetGuidanceEntries(); i++) guidance.push_back(command->GetGuidanceLine(i));
213  // Before deleting the command we need to add a fake one to avoid deleting the directory entry and with its guidance
214  G4UIcommand tmp((cmdpath+"_tmp").c_str(), messenger);
215  delete command;
216 
217  if (*type == typeid(float) || *type == typeid(double) ) {
218  G4UIcmdWithADoubleAndUnit* cmd_t = new G4UIcmdWithADoubleAndUnit(cmdpath, messenger);
219  if(spec == UnitDefault) cmd_t->SetDefaultUnit(unit);
220  else if(spec == UnitCategory) cmd_t->SetUnitCategory(unit);
221  cmd_t->SetParameterName(par_name, par_omitable);
222  command = cmd_t;
223  }
224  else if (*type == typeid(G4ThreeVector)) {
225  G4UIcmdWith3VectorAndUnit* cmd_t = new G4UIcmdWith3VectorAndUnit(cmdpath, messenger);
226  if(spec == UnitDefault) cmd_t->SetDefaultUnit(unit);
227  else if(spec == UnitCategory) cmd_t->SetUnitCategory(unit);
228  command = cmd_t;
229  }
230  else {
231  G4cerr << "Only parameters of type <double> or <float> can be associated with units" << G4endl;
232  return *this;
233  }
234  for (size_t i = 0; i < guidance.size(); i++) command->SetGuidance(guidance[i]);
235  command->SetRange(range);
236  return *this;
237 }
238 
240  G4UIparameter* theParam = command->GetParameter(0);
241  theParam->SetParameterName(name);
242  theParam->SetOmittable(omittable);
243  theParam->SetCurrentAsDefault(currentAsDefault);
244  return *this;
245 }
246 
248  G4UIparameter * theParam = command->GetParameter(0);
249  theParam->SetParameterCandidates(candList);
250  return *this;
251 }
252 
254  G4UIparameter * theParam = command->GetParameter(0);
255  theParam->SetDefaultValue(defVal);
256  return *this;
257 }
258 
259 
260 
std::string ToString() const
Definition: G4AnyType.hh:114
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:161
void SetCurrentAsDefault(G4bool val)
const XML_Char * name
Definition: expat.h:151
CLHEP::Hep3Vector G4ThreeVector
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:45
Command & DeclarePropertyWithUnit(const G4String &name, const G4String &defaultUnit, const G4AnyType &variable, const G4String &doc="")
void SetParameterName(const char *theName, G4bool omittable, G4bool currentAsDefault=false)
static const G4double pos
void FromString(const std::string &val)
Definition: G4AnyType.hh:118
virtual const char * what() const
Command & DeclareMethodWithUnit(const G4String &name, const G4String &defaultUnit, const G4AnyMethod &fun, const G4String &doc="")
#define G4endl
Definition: G4ios.hh:61
G4UImessenger * GetMessenger() const
Definition: G4UIcommand.hh:149
const char * p
Definition: xmltok.h:285
std::map< G4String, Method > methods
const G4String & GetGuidanceLine(G4int i) const
Definition: G4UIcommand.hh:137
G4UIparameter * GetParameter(G4int i) const
Definition: G4UIcommand.hh:145
for(int i=0;i< 401;i++)
void SetRange(const char *rs)
Definition: G4UIcommand.hh:125
Command & SetDefaultValue(const G4String &)
Float_t tmp
G4int GetGuidanceEntries() const
Definition: G4UIcommand.hh:135
void SetUnitCategory(const char *unitCategory)
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:141
Command & DeclareMethod(const G4String &name, const G4AnyMethod &fun, const G4String &doc="")
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:152
void SetDefaultValue(const char *theDefaultValue)
static constexpr double m
Definition: G4SIunits.hh:129
const XML_Char * s
Definition: expat.h:262
bool G4bool
Definition: G4Types.hh:79
Command & SetUnit(const G4String &, UnitSpec=UnitDefault)
const G4ParticleDefinition const G4Material *G4double range
void SetUnitCategory(const char *unitCategory)
G4GenericMessenger(void *obj, const G4String &dir="", const G4String &doc="")
Contructor.
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
void SetGuidance(const G4String &s)
G4UIdirectory * dircmd
virtual ~G4GenericMessenger()
Destructor.
Float_t d
const std::type_info * type
std::map< G4String, Property > properties
const G4String & GetCommandPath() const
Definition: G4UIcommand.hh:139
G4GLOB_DLL std::ostream G4cerr
Command & SetCandidates(const G4String &)
virtual G4String GetCurrentValue(G4UIcommand *command)
The concrete, but generic implementation of this method.
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:374
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
Command & SetParameterName(const G4String &, G4bool, G4bool=false)
G4bool IsMultithreadedApplication()
Definition: G4Threading.cc:136
int G4int
Definition: G4Types.hh:78
TDirectory * dir
void SetParameterName(const char *theName)
size_t NArg() const
Definition: G4AnyMethod.hh:141
const std::type_info & TypeInfo() const
Definition: G4AnyType.hh:106
G4bool IsOmittable() const
G4GLOB_DLL std::ostream G4cout
virtual void SetNewValue(G4UIcommand *command, G4String newValue)
The concrete, generic implementation of this method converts the string &quot;newValue&quot; to action...
static G4ThreeVector ConvertToDimensioned3Vector(const char *st)
Definition: G4UIcommand.cc:487
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:465
void SetDefaultUnit(const char *defUnit)
void SetParameterCandidates(const char *theString)
void SetParameterName(const char *theNameX, const char *theNameY, const char *theNameZ, G4bool omittable, G4bool currentAsDefault=false)
void SetOmittable(G4bool om)
G4String GetParameterName() const
const G4String & GetRange() const
Definition: G4UIcommand.hh:133
Command & DeclareProperty(const G4String &name, const G4AnyType &variable, const G4String &doc="")
Declare Methods.
void SetDefaultUnit(const char *defUnit)