G4GenericMessenger Class Reference

This class is generic messenger. More...

#include <G4GenericMessenger.hh>

Inheritance diagram for G4GenericMessenger:

G4UImessenger

Public Member Functions

 G4GenericMessenger (void *obj, const G4String &dir="", const G4String &doc="")
 Contructor.
virtual ~G4GenericMessenger ()
 Destructor.
virtual G4String GetCurrentValue (G4UIcommand *command)
 The concrete, but generic implementation of this method.
virtual void SetNewValue (G4UIcommand *command, G4String newValue)
 The concrete, generic implementation of this method converts the string "newValue" to action.
CommandDeclareProperty (const G4String &name, const G4AnyType &variable, const G4String &doc="")
 Declare Methods.
CommandDeclareMethod (const G4String &name, const G4AnyMethod &fun, const G4String &doc="")
void SetDirectory (const G4String &dir)
void SetGuidance (const G4String &s)

Data Structures

struct  Command
struct  Method
struct  Property

Detailed Description

This class is generic messenger.

Definition at line 46 of file G4GenericMessenger.hh.


Constructor & Destructor Documentation

G4GenericMessenger::G4GenericMessenger ( void *  obj,
const G4String dir = "",
const G4String doc = "" 
)

Contructor.

Definition at line 48 of file G4GenericMessenger.cc.

References G4UIcommand::SetGuidance().

00048                                                                                          : 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 }

G4GenericMessenger::~G4GenericMessenger (  )  [virtual]

Destructor.

Definition at line 63 of file G4GenericMessenger.cc.

00063                                         {
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 }


Member Function Documentation

G4GenericMessenger::Command & G4GenericMessenger::DeclareMethod ( const G4String name,
const G4AnyMethod fun,
const G4String doc = "" 
)

Definition at line 88 of file G4GenericMessenger.cc.

References G4AnyMethod::NArg(), G4UIcommand::SetGuidance(), and G4UIcommand::SetParameter().

00088                                                                                                    {
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 }

G4GenericMessenger::Command & G4GenericMessenger::DeclareProperty ( const G4String name,
const G4AnyType variable,
const G4String doc = "" 
)

Declare Methods.

Definition at line 71 of file G4GenericMessenger.cc.

References G4UIcommand::SetGuidance(), G4UIcommand::SetParameter(), and G4AnyType::TypeInfo().

00071                                                                                                    {
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 }

G4String G4GenericMessenger::GetCurrentValue ( G4UIcommand command  )  [virtual]

The concrete, but generic implementation of this method.

Reimplemented from G4UImessenger.

Definition at line 98 of file G4GenericMessenger.cc.

References G4UIcommand::GetCommandName().

00098                                                                  {
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 }

void G4GenericMessenger::SetDirectory ( const G4String dir  )  [inline]

Definition at line 98 of file G4GenericMessenger.hh.

00098 {directory = dir;}

void G4GenericMessenger::SetGuidance ( const G4String s  ) 

Definition at line 135 of file G4GenericMessenger.cc.

References G4UIcommand::SetGuidance().

00135                                                       {
00136   dircmd->SetGuidance(s);
00137 }

void G4GenericMessenger::SetNewValue ( G4UIcommand command,
G4String  newValue 
) [virtual]

The concrete, generic implementation of this method converts the string "newValue" to action.

Reimplemented from G4UImessenger.

Definition at line 108 of file G4GenericMessenger.cc.

References G4UIcommand::ConvertToDimensioned3Vector(), G4UIcommand::ConvertToDimensionedDouble(), and G4UIcommand::ConvertToString().

00108                                                                             {
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 }


The documentation for this class was generated from the following files:
Generated on Mon May 27 17:52:04 2013 for Geant4 by  doxygen 1.4.7