]> SALOME platform Git repositories - modules/yacs.git/blob - src/runtime/CppNode.cxx
Salome HOME
1bd37c23fb01a2f1ed74febf83d92f3ed2f247c4
[modules/yacs.git] / src / runtime / CppNode.cxx
1
2 #include "CppNode.hxx"
3 #include "InputPort.hxx"
4 #include "OutputPort.hxx"
5 #include "CppPorts.hxx"
6 #include "CppContainer.hxx"
7 #include "CppComponent.hxx"
8
9 #include <iostream>
10 #include <set>
11 #include <sstream>
12
13 //#define _DEVDEBUG_
14 #include "YacsTrace.hxx"
15
16 using namespace YACS::ENGINE;
17 using namespace std;
18
19 const char CppNode::IMPL_NAME[]="Cpp";
20 const char CppNode::KIND[]="Cpp";
21
22 CppNode::CppNode(const CppNode& other,ComposedNode *father):ServiceNode(other,father),
23                                                             _run(other._run),
24                                                             _componentName(other._componentName)
25 {
26   DEBTRACE("CppNode::CppNode");
27   _implementation=IMPL_NAME;
28 }
29
30 CppNode::CppNode(const std::string & name) : ServiceNode(name), _run(NULL)
31 {
32   _implementation=IMPL_NAME;
33 }
34
35 void CppNode::setCode(const std::string & componentName, const std::string & method)
36 {
37   _method = method;
38   _componentName = componentName;
39   _run = NULL;
40 }
41
42 void CppNode::setFunc(MYRUN fonc) { 
43   
44   if (_component) 
45     {
46       _component->decrRef();
47       _component = NULL;
48       _componentName = "";
49       _method = "";
50       _component = NULL;
51     }
52   _run = fonc;
53 }
54
55 void CppNode::load()
56 {
57   if (_run) return;
58   
59   if (!_component) {
60     setRef(_componentName);
61   }
62   ServiceNode::load();
63 }
64
65 void CppNode::execute()
66 {
67   std::list<InputPort *>::iterator iter1;
68   int nIn, nOut, it;
69   
70   nIn = _setOfInputPort.size();
71   nOut = _setOfOutputPort.size();
72   
73   Any ** In = new Any * [nIn], ** Out = new Any * [nOut];
74   
75   try 
76     {
77       
78       for(iter1 = _setOfInputPort.begin(), it = 0; iter1 != _setOfInputPort.end(); 
79           iter1++, it++)
80         {
81           InputCppPort *p = dynamic_cast<InputCppPort *> (*iter1);
82           In[it] = p->getCppObj();
83         }
84       
85       if (_component) 
86         {
87           CppComponent * _componentC = dynamic_cast<CppComponent *>(_component);
88           if (!_componentC)
89             throw YACS::Exception("CppNode::execute : bad type of component");
90           _componentC->run(_method.c_str(), nIn, nOut, In, Out);
91         }
92       else if (_run)
93         _run(nIn, nOut, In, Out);
94       
95       //output parameters
96       std::list<OutputPort *>::iterator iter2;
97       for(iter2 = _setOfOutputPort.begin(), it=0; iter2 != _setOfOutputPort.end(); iter2++, it++)
98         {
99           OutputCppPort *p = dynamic_cast<OutputCppPort *> (*iter2);
100           p->put(Out[it]);
101           //decref it, we don't need it more
102           Out[it]->decrRef();
103           DEBTRACE("ref count: " << Out[it]->getRefCnt());
104         }
105     }
106   catch (YACS::Exception & e) {
107     delete [] In;
108     delete [] Out;
109     throw e;
110   }
111   
112   delete [] In;
113   delete [] Out;
114 }
115
116 ServiceNode* CppNode::createNode(const std::string& name)
117 {
118   CppNode* node=  new CppNode(name);
119   node->setComponent(_component);
120   return node;
121 }
122
123 //! Clone the node : must also clone the component instance ?
124 Node * CppNode::simpleClone(ComposedNode *father, bool editionOnly) const
125 {
126   return new CppNode(*this,father);
127 }
128
129 //! Create a new node of same type with a given name
130 CppNode* CppNode::cloneNode(const std::string& name)
131 {
132   DEBTRACE("CppNode::cloneNode");
133   CppNode* n=new CppNode(name);
134   
135   if (_run)
136     n->setFunc(_run);
137   
138   if (_component)
139     n->setCode(_componentName, _method);
140   
141   list<InputPort *>::iterator iter;
142   for(iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
143     {
144       InputCppPort *p=(InputCppPort *)*iter;
145       DEBTRACE("port name: " << p->getName());
146       DEBTRACE("port kind: " << p->edGetType()->kind());
147       n->edAddInputPort(p->getName(),p->edGetType());
148     }
149   list<OutputPort *>::iterator iter2;
150   for(iter2 = _setOfOutputPort.begin(); iter2 != _setOfOutputPort.end(); iter2++)
151     {
152       OutputCppPort *p=(OutputCppPort *)*iter2;
153       DEBTRACE("port name: " << p->getName());
154       DEBTRACE("port kind: " << p->edGetType()->kind());
155       n->edAddOutputPort(p->getName(),p->edGetType());
156     }
157   return n;
158 }
159