Ignore:
Timestamp:
Apr 27, 2012, 12:34:31 AM (13 years ago)
Author:
ansari
Message:

Introduction de la classe interface CE_VarListInterface et modifs classe CExpressionEvaluator pour permettre l'inclusion de variables nommees dans les expressions traitees par CExpressionEvaluator, adaptation de la classe Commander afin que les variables de l'interpreteur soit visible par l'evaluateur CExpressionEvaluator, Reza 27/04/2012

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/SysTools/commander.cc

    r4034 r4063  
    442442
    443443// ------------------------------------------------------------
     444//         Classe Cmd_CE_VarList
     445// ------------------------------------------------------------
     446/*!
     447  \internal
     448  \class SOPHYA::Cmd_CE_VarList
     449  \ingroup SysTools
     450  This class, reserved for internal use by class Commander, enables the CExpressionEvaluator class
     451  to access the interpreter variable values.
     452*/
     453
     454class Cmd_CE_VarList : public CE_VarListInterface {
     455public:
     456  Cmd_CE_VarList(Commander& cmd) : cmd_(cmd) { }
     457  virtual void Update();
     458  virtual double* GetVarPointer(std::string const& name);
     459private:
     460  Commander& cmd_;
     461  std::map<std::string, double> namevals_;
     462};
     463
     464/* --Methode-- */
     465void Cmd_CE_VarList::Update()
     466{
     467  bool exist=false;
     468  string sval;
     469  std::map<std::string, double>::iterator it;
     470  for(it=namevals_.begin(); it!=namevals_.end(); it++) {
     471    exist=cmd_.GetVar((*it).first, sval);
     472    if (!exist) throw NotFoundExc("Cmd_CE_VarList::Update() missing variable !");
     473    (*it).second=atof(sval.c_str());
     474  }
     475  return;
     476}
     477
     478/* --Methode-- */
     479double* Cmd_CE_VarList::GetVarPointer(std::string const& name)
     480{
     481  std::map<std::string, double>::iterator it=namevals_.find(name);
     482  if (it!=namevals_.end())  return &((*it).second);
     483  bool exist=false;
     484  string sval;
     485  exist=cmd_.GetVar(name, sval);
     486  if (!exist) return NULL;
     487  namevals_[name]=atof(sval.c_str());
     488  it=namevals_.find(name);
     489  if (it!=namevals_.end())  return &((*it).second);
     490  return NULL;
     491}
     492
     493
     494// ------------------------------------------------------------
    444495//         Classe Commander
    445496// ------------------------------------------------------------
     
    492543hist.open("history.pic");
    493544histon = true;
    494 trace = false;   timing = false;
     545trace = false;   timing = false;   varcexp = true;
    495546gltimer = NULL;
    496547felevel_ = 0;
     
    561612usage += "  > sleep nsec  # sleep nsec seconds \n";   
    562613usage += "  > readstdin varname      # reads a line from stdin into $varname \n";
    563 usage += "  > timingon  timingoff traceon  traceoff \n";
     614usage += "  > timingon  timingoff traceon  traceoff varcexpon varcexpoff \n";
    564615RegisterHelp(kw, usage, grp);
    565616
     
    10851136    try {
    10861137      double res = 0.;
    1087       if (tokens.size() > 2) {
    1088         string sex = tokens[1];
     1138      string sex=tokens[1];
     1139      if (tokens.size() > 2)
    10891140        for(unsigned int js=2; js<tokens.size(); js++)  sex += tokens[js];
    1090         CExpressionEvaluator cex(sex);
     1141      if (varcexp) {  //--- Evaluation d'expression avec decodage de noms de variables 
     1142        Cmd_CE_VarList cevl(*this);
     1143        CExpressionEvaluator cex(sex,&cevl);
    10911144        res = cex.Value();
    10921145      }
    1093       else {
    1094         CExpressionEvaluator cex(tokens[1]);
     1146      else {  //--- Evaluation d'expression SANS decodage des noms de variables 
     1147        CExpressionEvaluator cex(sex);
    10951148        res = cex.Value();
    10961149      }
     
    19572010  if (gltimer)  delete gltimer;  gltimer = NULL;  timing = false;
    19582011  }
     2012else if (kw == "varcexpon") 
     2013  { cout << "Commander::Interpret()  -> Activating variable name decoding in expression evaluation " << endl; varcexp = true; }
     2014else if (kw == "varcexpoff")
     2015  { cout << "Commander::Interpret()  -> Deactivating variable name decoding in expression evaluation " << endl; varcexp = false; }
    19592016else if (kw == "exec") {
    19602017  if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: exec filename" << endl;  return(0); }
Note: See TracChangeset for help on using the changeset viewer.