00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "G4GenericMessenger.hh"
00030 #include "G4Types.hh"
00031 #include "G4UImessenger.hh"
00032 #include "G4UIcommand.hh"
00033 #include "G4UIcmdWithADoubleAndUnit.hh"
00034 #include "G4UIcmdWith3VectorAndUnit.hh"
00035 #include "G4UIdirectory.hh"
00036
00037 #include <iostream>
00038
00039 class G4InvalidUICommand: public std::bad_cast {
00040 public:
00041 G4InvalidUICommand() {}
00042 virtual const char* what() const throw() {
00043 return "G4InvalidUICommand: command does not exists or is of invalid type";
00044 }
00045 };
00046
00047
00048 G4GenericMessenger::G4GenericMessenger(void* obj, const G4String& dir, const G4String& doc): directory(dir), object(obj) {
00049
00050
00051 size_t pos = dir.find_last_of('/', dir.size()-2);
00052 while(pos != 0 && pos != std::string::npos) {
00053 G4UIdirectory* d = new G4UIdirectory(dir.substr(0,pos+1).c_str());
00054 G4String guidance = "Commands for ";
00055 guidance += dir.substr(1,pos-1);
00056 d->SetGuidance(guidance);
00057 pos = dir.find_last_of('/', pos-1);
00058 }
00059 dircmd = new G4UIdirectory(dir);
00060 dircmd->SetGuidance(doc);
00061 }
00062
00063 G4GenericMessenger::~G4GenericMessenger() {
00064 delete dircmd;
00065 for (std::map<G4String, Property>::iterator i = properties.begin(); i != properties.end(); i++) delete i->second.command;
00066 for (std::map<G4String, Method>::iterator i = methods.begin(); i != methods.end(); i++) delete i->second.command;
00067 }
00068
00069
00070 G4GenericMessenger::Command&
00071 G4GenericMessenger::DeclareProperty(const G4String& name, const G4AnyType& var, const G4String& doc) {
00072 G4String fullpath = directory+name;
00073 G4UIcommand* cmd = new G4UIcommand(fullpath.c_str(), this);
00074 if(doc != "") cmd->SetGuidance(doc);
00075 char ptype;
00076 if(var.TypeInfo() == typeid(int) || var.TypeInfo() == typeid(long) ||
00077 var.TypeInfo() == typeid(unsigned int) || var.TypeInfo() == typeid(unsigned long)) ptype = 'i';
00078 else if(var.TypeInfo() == typeid(float) || var.TypeInfo() == typeid(double)) ptype = 'd';
00079 else if(var.TypeInfo() == typeid(bool)) ptype = 'b';
00080 else if(var.TypeInfo() == typeid(G4String)) ptype = 's';
00081 else ptype = 's';
00082 cmd->SetParameter(new G4UIparameter("value", ptype, false));
00083 return properties[name] = Property(var, cmd);
00084 }
00085
00086
00087 G4GenericMessenger::Command&
00088 G4GenericMessenger::DeclareMethod(const G4String& name, const G4AnyMethod& fun, const G4String& doc) {
00089 G4String fullpath = directory+name;
00090 G4UIcommand* cmd = new G4UIcommand(fullpath.c_str(), this);
00091 if(doc != "") cmd->SetGuidance(doc);
00092 for (size_t i = 0; i < fun.NArg(); i++) {
00093 cmd->SetParameter(new G4UIparameter("arg", 's', false));
00094 }
00095 return methods[name] = Method(fun, object, cmd);
00096 }
00097
00098 G4String G4GenericMessenger::GetCurrentValue(G4UIcommand* command) {
00099 if ( properties.find(command->GetCommandName()) != properties.end()) {
00100 Property& p = properties[command->GetCommandName()];
00101 return p.variable.ToString();
00102 }
00103 else {
00104 throw G4InvalidUICommand();
00105 }
00106 }
00107
00108 void G4GenericMessenger::SetNewValue(G4UIcommand* command, G4String newValue) {
00109
00110 if (typeid(*command) == typeid(G4UIcmdWithADoubleAndUnit)) {
00111 newValue = G4UIcommand::ConvertToString(G4UIcommand::ConvertToDimensionedDouble(newValue));
00112 }
00113 else if (typeid(*command) == typeid(G4UIcmdWith3VectorAndUnit)) {
00114 newValue = G4UIcommand::ConvertToString(G4UIcommand::ConvertToDimensioned3Vector(newValue));
00115 }
00116
00117 if ( properties.find(command->GetCommandName()) != properties.end()) {
00118 Property& p = properties[command->GetCommandName()];
00119 p.variable.FromString(newValue);
00120 }
00121 else if (methods.find(command->GetCommandName()) != methods.end()) {
00122 Method& m = methods[command->GetCommandName()];
00123 if(m.method.NArg() == 0)
00124 m.method.operator()(m.object);
00125 else if (m.method.NArg() > 0) {
00126 m.method.operator()(m.object,newValue);
00127 }
00128 else {
00129 throw G4InvalidUICommand();
00130 }
00131 }
00132 }
00133
00134
00135 void G4GenericMessenger::SetGuidance(const G4String& s) {
00136 dircmd->SetGuidance(s);
00137 }
00138
00139
00140 G4GenericMessenger::Command& G4GenericMessenger::Command::SetUnit(const G4String& unit, UnitSpec spec) {
00141
00142
00143
00144 G4String cmdpath = command->GetCommandPath();
00145 G4UImessenger* messenger = command->GetMessenger();
00146 G4String range = command->GetRange();
00147 std::vector<G4String> guidance;
00148 for (G4int i = 0; i < command->GetGuidanceEntries(); i++) guidance.push_back(command->GetGuidanceLine(i));
00149
00150 G4UIcommand tmp((cmdpath+"_tmp").c_str(), messenger);
00151 delete command;
00152
00153 if (*type == typeid(float) || *type == typeid(double) ) {
00154 G4UIcmdWithADoubleAndUnit* cmd_t = new G4UIcmdWithADoubleAndUnit(cmdpath, messenger);
00155 if(spec == UnitDefault) cmd_t->SetDefaultUnit(unit);
00156 else if(spec == UnitCategory) cmd_t->SetUnitCategory(unit);
00157 command = cmd_t;
00158 }
00159 else if (*type == typeid(G4ThreeVector)) {
00160 G4UIcmdWith3VectorAndUnit* cmd_t = new G4UIcmdWith3VectorAndUnit(cmdpath, messenger);
00161 if(spec == UnitDefault) cmd_t->SetDefaultUnit(unit);
00162 else if(spec == UnitCategory) cmd_t->SetUnitCategory(unit);
00163 command = cmd_t;
00164 }
00165 else {
00166 G4cerr << "Only parameters of type <double> or <float> can be associated with units" << G4endl;
00167 return *this;
00168 }
00169 for (size_t i = 0; i < guidance.size(); i++) command->SetGuidance(guidance[i]);
00170 command->SetRange(range);
00171 return *this;
00172 }
00173
00174 G4GenericMessenger::Command& G4GenericMessenger::Command::SetParameterName(const G4String& name,G4bool omittable, G4bool currentAsDefault) {
00175 G4UIparameter* theParam = command->GetParameter(0);
00176 theParam->SetParameterName(name);
00177 theParam->SetOmittable(omittable);
00178 theParam->SetCurrentAsDefault(currentAsDefault);
00179 return *this;
00180 }
00181
00182 G4GenericMessenger::Command& G4GenericMessenger::Command::SetCandidates(const G4String& candList) {
00183 G4UIparameter * theParam = command->GetParameter(0);
00184 theParam->SetParameterCandidates(candList);
00185 return *this;
00186 }
00187
00188 G4GenericMessenger::Command& G4GenericMessenger::Command::SetDefaultValue(const G4String& defVal) {
00189 G4UIparameter * theParam = command->GetParameter(0);
00190 theParam->SetDefaultValue(defVal);
00191 return *this;
00192 }
00193
00194
00195