G4GenericMessenger.cc

Go to the documentation of this file.
00001 // ********************************************************************
00002 // * License and Disclaimer                                           *
00003 // *                                                                  *
00004 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00005 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00006 // * conditions of the Geant4 Software License,  included in the file *
00007 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00008 // * include a list of copyright holders.                             *
00009 // *                                                                  *
00010 // * Neither the authors of this software system, nor their employing *
00011 // * institutes,nor the agencies providing financial support for this *
00012 // * work  make  any representation or  warranty, express or implied, *
00013 // * regarding  this  software system or assume any liability for its *
00014 // * use.  Please see the license in the file  LICENSE  and URL above *
00015 // * for the full disclaimer and the limitation of liability.         *
00016 // *                                                                  *
00017 // * This  code  implementation is the result of  the  scientific and *
00018 // * technical work of the GEANT4 collaboration.                      *
00019 // * By using,  copying,  modifying or  distributing the software (or *
00020 // * any work based  on the software)  you  agree  to acknowledge its *
00021 // * use  in  resulting  scientific  publications,  and indicate your *
00022 // * acceptance of all terms of the Geant4 Software license.          *
00023 // ********************************************************************
00024 //
00025 //
00026 // $Id: G4UIaliasList.cc,v 1.6 2006-06-29 19:08:33 gunter Exp $
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   // Check if parent commnand is already existing.
00050   // In fact there is no way to check this. UImanager->GetTree()->FindPath() will always rerurn NULL is a dicrectory is given
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   // Check if there are units on this commands
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   // Change the type of command (unfortunatelly this is done a posteriory)
00142   // We need to delete the old command before creating the new one and therefore we need to recover the information
00143   // before the deletetion
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   // Before deleting the command we need to add a fake one to avoid deleting the directory entry and with its guidance
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 

Generated on Mon May 27 17:48:21 2013 for Geant4 by  doxygen 1.4.7