Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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
39class G4InvalidUICommand: public std::bad_cast {
40public:
42 virtual const char* what() const throw() {
43 return "G4InvalidUICommand: command does not exists or is of invalid type";
44 }
45};
46
47
48G4GenericMessenger::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
71G4GenericMessenger::DeclareProperty(const G4String& name, const G4AnyType& var, const G4String& doc) {
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
88G4GenericMessenger::DeclareMethod(const G4String& name, const G4AnyMethod& fun, const G4String& doc) {
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}
97
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}
107
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}
133
134
136 dircmd->SetGuidance(s);
137}
138
139
141 // Change the type of command (unfortunatelly this is done a posteriory)
142 // We need to delete the old command before creating the new one and therefore we need to recover the information
143 // before the deletetion
144 G4String cmdpath = command->GetCommandPath();
145 G4UImessenger* messenger = command->GetMessenger();
146 G4String range = command->GetRange();
147 std::vector<G4String> guidance;
148 for (G4int i = 0; i < command->GetGuidanceEntries(); i++) guidance.push_back(command->GetGuidanceLine(i));
149 // Before deleting the command we need to add a fake one to avoid deleting the directory entry and with its guidance
150 G4UIcommand tmp((cmdpath+"_tmp").c_str(), messenger);
151 delete command;
152
153 if (*type == typeid(float) || *type == typeid(double) ) {
154 G4UIcmdWithADoubleAndUnit* cmd_t = new G4UIcmdWithADoubleAndUnit(cmdpath, messenger);
155 if(spec == UnitDefault) cmd_t->SetDefaultUnit(unit);
156 else if(spec == UnitCategory) cmd_t->SetUnitCategory(unit);
157 command = cmd_t;
158 }
159 else if (*type == typeid(G4ThreeVector)) {
160 G4UIcmdWith3VectorAndUnit* cmd_t = new G4UIcmdWith3VectorAndUnit(cmdpath, messenger);
161 if(spec == UnitDefault) cmd_t->SetDefaultUnit(unit);
162 else if(spec == UnitCategory) cmd_t->SetUnitCategory(unit);
163 command = cmd_t;
164 }
165 else {
166 G4cerr << "Only parameters of type <double> or <float> can be associated with units" << G4endl;
167 return *this;
168 }
169 for (size_t i = 0; i < guidance.size(); i++) command->SetGuidance(guidance[i]);
170 command->SetRange(range);
171 return *this;
172}
173
175 G4UIparameter* theParam = command->GetParameter(0);
176 theParam->SetParameterName(name);
177 theParam->SetOmittable(omittable);
178 theParam->SetCurrentAsDefault(currentAsDefault);
179 return *this;
180}
181
183 G4UIparameter * theParam = command->GetParameter(0);
184 theParam->SetParameterCandidates(candList);
185 return *this;
186}
187
189 G4UIparameter * theParam = command->GetParameter(0);
190 theParam->SetDefaultValue(defVal);
191 return *this;
192}
193
194
195
int G4int
Definition: G4Types.hh:66
bool G4bool
Definition: G4Types.hh:67
#define G4endl
Definition: G4ios.hh:52
G4DLLIMPORT std::ostream G4cerr
size_t NArg() const
Definition: G4AnyMethod.hh:126
void FromString(const std::string &val)
Definition: G4AnyType.hh:116
const std::type_info & TypeInfo() const
Definition: G4AnyType.hh:104
std::string ToString() const
Definition: G4AnyType.hh:112
Command & DeclareMethod(const G4String &name, const G4AnyMethod &fun, const G4String &doc="")
void SetGuidance(const G4String &s)
virtual void SetNewValue(G4UIcommand *command, G4String newValue)
The concrete, generic implementation of this method converts the string "newValue" to action.
Command & DeclareProperty(const G4String &name, const G4AnyType &variable, const G4String &doc="")
Declare Methods.
virtual G4String GetCurrentValue(G4UIcommand *command)
The concrete, but generic implementation of this method.
virtual ~G4GenericMessenger()
Destructor.
G4GenericMessenger(void *obj, const G4String &dir="", const G4String &doc="")
Contructor.
virtual const char * what() const
void SetDefaultUnit(const char *defUnit)
void SetUnitCategory(const char *unitCategory)
void SetUnitCategory(const char *unitCategory)
void SetDefaultUnit(const char *defUnit)
G4UImessenger * GetMessenger() const
Definition: G4UIcommand.hh:144
const G4String & GetGuidanceLine(G4int i) const
Definition: G4UIcommand.hh:132
G4int GetGuidanceEntries() const
Definition: G4UIcommand.hh:130
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:349
const G4String & GetCommandPath() const
Definition: G4UIcommand.hh:134
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:147
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:156
void SetRange(const char *rs)
Definition: G4UIcommand.hh:120
static G4double ConvertToDimensionedDouble(const char *st)
Definition: G4UIcommand.cc:429
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:136
const G4String & GetRange() const
Definition: G4UIcommand.hh:128
static G4ThreeVector ConvertToDimensioned3Vector(const char *st)
Definition: G4UIcommand.cc:451
void SetDefaultValue(const char *theDefaultValue)
void SetOmittable(G4bool om)
void SetParameterName(const char *theName)
void SetParameterCandidates(const char *theString)
void SetCurrentAsDefault(G4bool val)
Command & SetParameterName(const G4String &, G4bool, G4bool=false)
Command & SetCandidates(const G4String &)
Command & SetDefaultValue(const G4String &)
const std::type_info * type
Command & SetUnit(const G4String &, UnitSpec=UnitDefault)