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