Salome HOME
bos #26594 Failed KERNEL test: fix typemap for swig 4x
[modules/yacs.git] / src / yacsloader / loopParsers.hxx
index ee42b9ab32ad136025d0d99ddba5619e49acfe9e..335192d6ad0f4833e9f84fc32295cca49399473c 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2006-2019  CEA/DEN, EDF R&D
+// Copyright (C) 2006-2021  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -61,8 +61,7 @@ struct looptypeParser:parser
   virtual void remote (ENGINE::InlineNode* const& n);
   virtual void node (ENGINE::InlineNode* const& n);
   virtual void forloop (ENGINE::ForLoop* const& b);
-  virtual void foreach (ENGINE::ForEachLoop* const& b);
-  virtual void foreachdyn (ENGINE::ForEachLoopDyn* const& b);
+  virtual void foreach (ENGINE::ForEachLoopGen* const& b);
   virtual void optimizer (ENGINE::OptimizerLoop* const& b);
   virtual void while_ (ENGINE::WhileLoop* const& b);
   virtual void switch_ (ENGINE::Switch* const& b);
@@ -182,7 +181,7 @@ void looptypeParser<T>::forloop (ENGINE::ForLoop* const& b)
       currentProc->nodeMap[fullname]=b;
     }
 template <class T>
-void looptypeParser<T>::foreach (ENGINE::ForEachLoop* const& b)
+void looptypeParser<T>::foreach (ENGINE::ForEachLoopGen* const& b)
     {
       DEBTRACE("loop_foreach" << b->getName())
       _cnode->edSetNode(b);
@@ -192,16 +191,6 @@ void looptypeParser<T>::foreach (ENGINE::ForEachLoop* const& b)
       currentProc->nodeMap[fullname]=b->getChildByShortName("splitter");
     }
 template <class T>
-void looptypeParser<T>::foreachdyn (ENGINE::ForEachLoopDyn* const& b)
-{
-      DEBTRACE("loop_foreachdyn" << b->getName())
-      _cnode->edSetNode(b);
-      std::string fullname=currentProc->names.back()+ b->getName();
-      currentProc->nodeMap[fullname]=b;
-      fullname += ".splitter";
-      currentProc->nodeMap[fullname]=b->getChildByShortName("splitter");
-}
-template <class T>
 void looptypeParser<T>::optimizer (ENGINE::OptimizerLoop* const& b)
   {
     DEBTRACE( "loop_optimizer: " << b->getName() );
@@ -405,8 +394,8 @@ public:
   ENGINE::Node * getNode() { return _node; }
 
   // Those two methods should never be called
-  bool edAddLink(ENGINE::OutPort *start, ENGINE::InPort *end) throw(Exception) { YASSERT(false); }
-  bool edAddDFLink(ENGINE::OutPort *start, ENGINE::InPort *end) throw(Exception) { YASSERT(false); }
+  bool edAddLink(ENGINE::OutPort *start, ENGINE::InPort *end)  { YASSERT(false); }
+  bool edAddDFLink(ENGINE::OutPort *start, ENGINE::InPort *end)  { YASSERT(false); }
 
 protected:
   ENGINE::Node * _node;
@@ -501,102 +490,22 @@ namespace YACS
 {
   // Foreach loop specialization
 
-template <class T=ENGINE::ForEachLoop*>
-struct foreachlooptypeParser:dynparalooptypeParser<T>
-{
-  static foreachlooptypeParser<T> foreachloopParser;
+template<class T>
+T buildFrom(ENGINE::Runtime *theRuntime, const std::string& name, ENGINE::TypeCode *type);
 
-  virtual void buildAttr(const XML_Char** attr)
-    {
-      if (!attr)
-        return;
-      this->required("name",attr);
-      this->required("type",attr);
-      for (int i = 0; attr[i]; i += 2) 
-        {
-          if(std::string(attr[i]) == "name")name(attr[i+1]);
-          if(std::string(attr[i]) == "state")this->state(attr[i+1]);
-          if(std::string(attr[i]) == "nbranch")nbranch(atoi(attr[i+1]));
-          if(std::string(attr[i]) == "loopWeight")weight(atof(attr[i+1]));
-          if(std::string(attr[i]) == "type")datatype(attr[i+1]);
-        }
-      postAttr();
-    }
-  virtual void pre ()
-    {
-      _nbranch=0;
-      _weight=-1.;
-      this->looptypeParser<T>::pre();
-    }
-  virtual void name (const std::string& name)
-    {
-      DEBTRACE("foreach_name: " << name)
-      _name=name;
-      _fullname=currentProc->names.back()+name;
-    }
-  virtual void nbranch (const int& n)
-    {
-      DEBTRACE("foreach_nbranch: " << n )
-      _nbranch=n;
-    }
-  virtual void weight (const double& x)
-    {
-      DEBTRACE("foreach_weight: " << x )
-      _weight=x;
-    }
-  virtual void datatype (const std::string& type)
-    {
-      DEBTRACE("foreach_datatype: "<< type)
-      _datatype=type;
-    }
-  virtual void postAttr()
-    {
-      if(currentProc->typeMap.count(_datatype)==0)
-      {
-        //Check if the typecode is defined in the runtime
-        YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(_datatype);
-        if(t==0)
-        {
-          std::stringstream msg;
-          msg << "Type "<< _datatype <<" does not exist"<<" ("<<__FILE__<<":"<<__LINE__<< ")";
-          throw Exception(msg.str());
-        }
-        else
-        {
-          currentProc->typeMap[_datatype]=t;
-          t->incrRef();
-        }
-      }
-      this->_cnode=theRuntime->createForEachLoop(_name,currentProc->typeMap[_datatype]);
-      //set number of branches
-      if(_nbranch > 0)this->_cnode->edGetNbOfBranchesPort()->edInit(_nbranch);
-      if(_weight > 0)this->_cnode->setWeight(_weight);
-      this->_cnodes.push_back(this->_cnode);
-      currentProc->names.push_back(_fullname + '.');
-    }
-  virtual T post()
-    {
-      DEBTRACE("foreach_post" << this->_cnode->getName())
-      T b=this->_cnode;
-      this->_cnodes.pop_back();
-      currentProc->names.pop_back();
-      this->_cnode=this->_cnodes.empty() ? 0 : this->_cnodes.back();
-      return b;
-    }
-  int _nbranch;
-  double _weight;
-  std::string _fullname;
-  std::string _name;
-  std::string _datatype;
-};
+template<>
+ENGINE::ForEachLoop *buildFrom<ENGINE::ForEachLoop *>(ENGINE::Runtime *theRuntime, const std::string& name, ENGINE::TypeCode *type)
+{ return theRuntime->createForEachLoop(name,type); }
+
+template<>
+ENGINE::ForEachLoopDyn *buildFrom<ENGINE::ForEachLoopDyn *>(ENGINE::Runtime *theRuntime, const std::string& name, ENGINE::TypeCode *type)
+{ return theRuntime->createForEachLoopDyn(name,type); }
 
-template <class T> foreachlooptypeParser<T> foreachlooptypeParser<T>::foreachloopParser;
 
-//FIXME
-template <class T=ENGINE::ForEachLoopDyn*>
-struct foreachloopdyntypeParser:dynparalooptypeParser<T>
+template <class T>
+struct foreachlooptypeParser:dynparalooptypeParser<T>
 {
-  static foreachloopdyntypeParser<T> foreachloopdynParser;
+  static foreachlooptypeParser<T> foreachloopParser;
 
   virtual void buildAttr(const XML_Char** attr)
     {
@@ -659,7 +568,7 @@ struct foreachloopdyntypeParser:dynparalooptypeParser<T>
           t->incrRef();
         }
       }
-      this->_cnode=theRuntime->createForEachLoopDyn(_name,currentProc->typeMap[_datatype]);
+      this->_cnode=buildFrom<T>(theRuntime,_name,currentProc->typeMap[_datatype]);
       //set number of branches
       if(_nbranch > 0)this->_cnode->edGetNbOfBranchesPort()->edInit(_nbranch);
       if(_weight > 0)this->_cnode->setWeight(_weight);
@@ -682,7 +591,7 @@ struct foreachloopdyntypeParser:dynparalooptypeParser<T>
   std::string _datatype;
 };
 
-template <class T> foreachloopdyntypeParser<T> foreachloopdyntypeParser<T>::foreachloopdynParser;
+template <class T> foreachlooptypeParser<T> foreachlooptypeParser<T>::foreachloopParser;
 }
 
 namespace YACS
@@ -815,8 +724,8 @@ void looptypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
   else if(element == "node")pp=&nodetypeParser<>::nodeParser;
 
   else if(element == "forloop")pp=&forlooptypeParser<>::forloopParser;
-  else if(element == "foreach")pp=&foreachlooptypeParser<>::foreachloopParser;
-  else if(element == "foreachdyn")pp=&foreachloopdyntypeParser<>::foreachloopdynParser;
+  else if(element == "foreach")pp=&foreachlooptypeParser<ENGINE::ForEachLoop *>::foreachloopParser;
+  else if(element == "foreachdyn")pp=&foreachlooptypeParser<ENGINE::ForEachLoopDyn *>::foreachloopParser;
   else if(element == "optimizer")pp=&optimizerlooptypeParser<>::optimizerloopParser;
   else if(element == "while")pp=&whilelooptypeParser<>::whileloopParser;
   else if(element == "switch")pp=&switchtypeParser::switchParser;
@@ -842,8 +751,8 @@ void looptypeParser<T>::onEnd(const char *el,parser* child)
   else if(element == "node")node(((nodetypeParser<>*)child)->post());
 
   else if(element == "forloop")forloop(((forlooptypeParser<>*)child)->post());
-  else if(element == "foreach")foreach(((foreachlooptypeParser<>*)child)->post());
-  else if(element == "foreachdyn")foreachdyn(((foreachloopdyntypeParser<>*)child)->post());
+  else if(element == "foreach")foreach(((foreachlooptypeParser<ENGINE::ForEachLoop *>*)child)->post());
+  else if(element == "foreachdyn")foreach(((foreachlooptypeParser<ENGINE::ForEachLoopDyn *>*)child)->post());
   else if(element == "optimizer")optimizer(((optimizerlooptypeParser<>*)child)->post());
   else if(element == "while")while_(((whilelooptypeParser<>*)child)->post());
   else if(element == "switch")switch_(((switchtypeParser*)child)->post());