Salome HOME
[EDF27816] : fix all non regression tests
[modules/yacs.git] / src / runtime / CppNode.cxx
1 // Copyright (C) 2006-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "CppNode.hxx"
21 #include "InputPort.hxx"
22 #include "OutputPort.hxx"
23 #include "CppPorts.hxx"
24 #include "CppContainer.hxx"
25 #include "CppComponent.hxx"
26 #include "TypeCode.hxx"
27
28 #include <iostream>
29 #include <set>
30 #include <sstream>
31
32 //#define _DEVDEBUG_
33 #include "YacsTrace.hxx"
34
35 using namespace YACS::ENGINE;
36 using namespace std;
37
38 const char CppNode::IMPL_NAME[]="Cpp";
39 const char CppNode::KIND[]="Cpp";
40
41 CppNode::CppNode(const CppNode& other,ComposedNode *father):ServiceNode(other,father),
42                                                             _run(other._run),
43                                                             _componentName(other._componentName)
44 {
45   DEBTRACE("CppNode::CppNode");
46   _implementation=IMPL_NAME;
47 }
48
49 CppNode::CppNode(const std::string & name) : ServiceNode(name), _run(NULL)
50 {
51   _implementation=IMPL_NAME;
52 }
53
54 void CppNode::setCode(const std::string & componentName, const std::string & method)
55 {
56   _method = method;
57   _componentName = componentName;
58   _run = NULL;
59 }
60
61 void CppNode::setFunc(MYRUN fonc) { 
62   
63   if (_component) 
64     {
65       _component->decrRef();
66       _component = NULL;
67       _componentName = "";
68       _method = "";
69       _component = NULL;
70     }
71   _run = fonc;
72 }
73
74 void CppNode::load()
75 {
76   if (_run) return;
77   
78   if (!_component) {
79     setRef(_componentName);
80   }
81   ServiceNode::load();
82 }
83
84 void CppNode::execute()
85 {
86   std::list<InputPort *>::iterator iter1;
87   int nIn, nOut, it;
88   
89   nIn = _setOfInputPort.size();
90   nOut = _setOfOutputPort.size();
91   
92   Any ** In = new Any * [nIn], ** Out = new Any * [nOut];
93   
94   try 
95     {
96       
97       for(iter1 = _setOfInputPort.begin(), it = 0; iter1 != _setOfInputPort.end(); 
98           iter1++, it++)
99         {
100           InputCppPort *p = dynamic_cast<InputCppPort *> (*iter1);
101           In[it] = p->getCppObj();
102         }
103       
104       if (_component) 
105         {
106           CppComponent * _componentC = dynamic_cast<CppComponent *>(_component);
107           if (!_componentC)
108             throw YACS::Exception("CppNode::execute : bad type of component");
109           _componentC->run(_method.c_str(), nIn, nOut, In, Out);
110         }
111       else if (_run)
112         _run(nIn, nOut, In, Out);
113       
114       //output parameters
115       std::list<OutputPort *>::iterator iter2;
116       for(iter2 = _setOfOutputPort.begin(), it=0; iter2 != _setOfOutputPort.end(); iter2++, it++)
117         {
118           OutputCppPort *p = dynamic_cast<OutputCppPort *> (*iter2);
119           p->put(Out[it]);
120           //decref it, we don't need it more
121           Out[it]->decrRef();
122           DEBTRACE("ref count: " << Out[it]->getRefCnt());
123         }
124     }
125   catch (YACS::Exception & e) {
126     delete [] In;
127     delete [] Out;
128     throw e;
129   }
130   
131   delete [] In;
132   delete [] Out;
133 }
134
135 ServiceNode* CppNode::createNode(const std::string& name)
136 {
137   CppNode* node=  new CppNode(name);
138   node->setComponent(_component);
139   return node;
140 }
141
142 //! Clone the node : must also clone the component instance ?
143 Node * CppNode::simpleClone(ComposedNode *father, bool editionOnly) const
144 {
145   return new CppNode(*this,father);
146 }
147
148 //! Create a new node of same type with a given name
149 CppNode* CppNode::cloneNode(const std::string& name)
150 {
151   DEBTRACE("CppNode::cloneNode");
152   CppNode* n=new CppNode(name);
153   
154   if (_run)
155     n->setFunc(_run);
156   
157   if (_component)
158     n->setCode(_componentName, _method);
159   
160   list<InputPort *>::iterator iter;
161   for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
162     {
163       InputCppPort *p=(InputCppPort *)*iter;
164       DEBTRACE("port name: " << p->getName());
165       DEBTRACE("port kind: " << p->edGetType()->kind());
166       n->edAddInputPort(p->getName(),p->edGetType());
167     }
168   list<OutputPort *>::iterator iter2;
169   for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
170     {
171       OutputCppPort *p=(OutputCppPort *)*iter2;
172       DEBTRACE("port name: " << p->getName());
173       DEBTRACE("port kind: " << p->edGetType()->kind());
174       n->edAddOutputPort(p->getName(),p->edGetType());
175     }
176   return n;
177 }
178