]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/OutputPort.cxx
Salome HOME
8bf0cd2c014ee34caf47434c70ec97f2f3959e61
[modules/yacs.git] / src / engine / OutputPort.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 "OutputPort.hxx"
20 #include "ComposedNode.hxx"
21 #include "InputPort.hxx"
22 #include "Runtime.hxx"
23 #include "Node.hxx"
24
25 #include <sstream>
26 #include <iostream>
27
28 //#define _DEVDEBUG_
29 #include "YacsTrace.hxx"
30
31 using namespace YACS::ENGINE;
32 using namespace std;
33
34 const char OutputPort::NAME[]="OutputPort";
35
36 OutputPort::OutputPort(const OutputPort& other, Node *newHelder):DataFlowPort(other,newHelder),OutPort(other,newHelder),
37                                                                  DataPort(other,newHelder),Port(other,newHelder)
38 {
39 }
40
41 OutputPort::OutputPort(const std::string& name, Node *node, TypeCode* type):DataFlowPort(name,node,type),OutPort(name,node,type),
42                                                                        DataPort(name,node,type),Port(node)
43 {
44 }
45
46 string OutputPort::getNameOfTypeOfCurrentInstance() const
47 {
48   return NAME;
49 }
50
51 void OutputPort::edRemoveAllLinksLinkedWithMe() throw(Exception)
52 {
53   set<InputPort *>::iterator iter;
54   set<InputPort *> vec(_setOfInputPort);
55   for( set<InputPort *>::iterator iter2=vec.begin();iter2!=vec.end();iter2++)
56     edRemoveInputPort(*iter2,true);
57   _setOfInputPort.clear();
58 }
59
60 void OutputPort::exInit()
61 {
62 }
63
64 void OutputPort::put(const void *data) throw(ConversionException)
65 {
66   for(set<InputPort *>::iterator iter = _setOfInputPort.begin(); iter != _setOfInputPort.end(); iter++)
67     (*iter)->put(data);
68 }
69
70 /**
71  * check if output type is an input type and if a data converter exists before link
72  */
73 bool OutputPort::edAddInputPort(InputPort *phyPort) throw(Exception)
74 {
75   DEBTRACE("OutputPort::edAddInputPort");
76   if(!isAlreadyInSet(phyPort))
77     {
78       InputPort *pwrap = getRuntime()->adapt(phyPort,
79                                              _node->getImplementation(),
80                                              this->edGetType());
81       _setOfInputPort.insert(pwrap);
82       modified();
83       phyPort->modified();
84       return true;
85     }
86   else
87     return false;
88 }
89
90 /**
91  * Remove a link by performing not only the deletion in _setOfInputPort but also dereference to the target inputPort.
92  * If 'forward' == true the forward deletion 
93  * If 'forward' == false no forward deletion performed, oneway deletion without update 'inputPort' side.
94  */
95 int OutputPort::edRemoveInputPort(InputPort *inputPort, bool forward) throw(Exception)
96 {
97   if(forward)
98     {
99       set<InPort *> s;
100       inputPort->getAllRepresentants(s);
101       DEBTRACE("+++");
102       for(set<InPort *>::iterator iter=s.begin();iter!=s.end();iter++)
103         {
104           DEBTRACE("---");
105           _node->getRootNode()->edRemoveLink(this,*iter);
106         }
107       return -1;
108     }
109   else
110     {
111 #ifdef NOCOVARIANT
112       InPort *publicRepr=inputPort->getPublicRepresentant();
113 #else
114       InputPort *publicRepr=inputPort->getPublicRepresentant();
115 #endif
116       set<InputPort *>::iterator iter;
117       for(iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
118         if((*iter)->getPublicRepresentant()==publicRepr)
119           break;
120       if(iter!=_setOfInputPort.end())
121         {
122           (*iter)->modified();
123           if((*iter)->isIntermediate())
124             delete (*iter);
125           _setOfInputPort.erase(iter);
126           modified();
127           return edGetNumberOfOutLinks();
128         }
129       else
130         throw Exception("OutputPort::edRemoveInputPort : link does not exist, unable to remove it");
131     }
132 }
133
134 OutputPort::~OutputPort()
135 {
136   set<InputPort *>::iterator iter;
137   for(iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
138     {
139       delete (*iter);
140     }
141 }
142
143 bool OutputPort::isAlreadyLinkedWith(InPort *with) const
144 {
145   InPort *publicRepr=with->getPublicRepresentant();
146   set<InPort *> s;
147   set<InputPort *>::iterator iter;
148   for(iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
149     {
150     if((*iter)->getPublicRepresentant() == publicRepr)
151       return true;
152     }
153   for(iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
154     (*iter)->getAllRepresentants(s);
155   for(set<InPort *>::iterator iter2=s.begin();iter2!=s.end();iter2++)
156     {
157     if((*iter2)->getPublicRepresentant() == publicRepr)
158       return true;
159     }
160   return false;
161 }
162
163 bool OutputPort::isAlreadyInSet(InputPort *inputPort) const
164 {
165 #ifdef NOCOVARIANT
166   InPort *publicRepr=inputPort->getPublicRepresentant();
167 #else
168   InputPort *publicRepr=inputPort->getPublicRepresentant();
169 #endif
170   for(set<InputPort *>::const_iterator iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
171     if((*iter)->getPublicRepresentant()==publicRepr)
172       return true;
173   return false;
174 }
175
176 /**
177  * check compatibility of port class ( an inputPort ) before trying to create the link.
178  */
179 bool OutputPort::addInPort(InPort *inPort) throw(Exception)
180 {
181   DEBTRACE("OutputPort::addInPort");
182   if(inPort->getNameOfTypeOfCurrentInstance()!=InputPort::NAME)
183     {
184       string what="not compatible type of port requested during building of link FROM ";
185       what+=NAME; what+=" TO "; what+=inPort->getNameOfTypeOfCurrentInstance();
186       throw Exception(what);
187     }
188   return edAddInputPort(static_cast<InputPort*>(inPort));
189 }
190
191 /**
192  * check compatibility of port class ( an inputPort ) before trying to remove link WITHOUT forward.
193  */
194 int OutputPort::removeInPort(InPort *inPort, bool forward) throw(Exception)
195 {
196   if(inPort->getNameOfTypeOfCurrentInstance()!=InputPort::NAME && !forward)
197     {
198       string what="not compatible type of port requested during destruction of for link FROM ";
199       what+=NAME; what+=" TO "; what+=inPort->getNameOfTypeOfCurrentInstance();
200       throw Exception(what);
201     }
202   return edRemoveInputPort(static_cast<InputPort*>(inPort),forward);
203 }
204
205 std::set<InPort *> OutputPort::edSetInPort() const
206 {
207   set<InPort *> s;
208   for(set<InputPort *>::iterator iter=_setOfInputPort.begin();iter!=_setOfInputPort.end();iter++)
209     (*iter)->getAllRepresentants(s);
210   return s;
211 }
212
213 std::string OutputPort::dump()
214 {
215   string xmldump = "<value><error> NO_SERIALISATION_AVAILABLE </error></value>";
216   return xmldump;
217 }
218
219
220 //! Returns physical links linked to this. Contrary to edSetInPort that returns semantic links.
221 const std::set<InputPort *>& OutputPort::getSetOfPhyLinks() const
222 {
223   return _setOfInputPort;
224 }
225
226 //! Check validity of output port. Nothing on base class
227 void OutputPort::checkBasicConsistency() const throw(Exception)
228 {
229 }