Salome HOME
d913899d4584d1f9ffc06b984435afc52b50f032
[modules/yacs.git] / src / engine / OutputDataStreamPort.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 "OutputDataStreamPort.hxx"
20 #include "InputDataStreamPort.hxx"
21 #include "ComposedNode.hxx"
22 #include "InPort.hxx"
23 #include "TypeCode.hxx"
24 #include <iostream>
25
26 //#define _DEVDEBUG_
27 #include "YacsTrace.hxx"
28
29 using namespace YACS::ENGINE;
30 using namespace std;
31
32 const char OutputDataStreamPort::NAME[]="OutputDataStreamPort";
33
34 OutputDataStreamPort::OutputDataStreamPort(const OutputDataStreamPort& other, Node *newHelder):DataStreamPort(other,newHelder),
35                                                                                                OutPort(other,newHelder),
36                                                                                                DataPort(other,newHelder),
37                                                                                                Port(other,newHelder)
38 {
39 }
40
41 OutputDataStreamPort::OutputDataStreamPort(const std::string& name, Node *node, TypeCode* type):DataStreamPort(name,node,type),
42 OutPort(name,node,type),
43 DataPort(name,node,type),
44 Port(node)
45 {
46 }
47
48 OutputDataStreamPort::~OutputDataStreamPort()
49 {
50 }
51
52 OutputDataStreamPort *OutputDataStreamPort::clone(Node *newHelder) const
53 {
54   return new OutputDataStreamPort(*this,newHelder);
55 }
56
57 std::set<InPort *> OutputDataStreamPort::edSetInPort() const
58 {
59   set<InPort *> s;
60   for(set<InputDataStreamPort *>::iterator iter=_setOfInputDataStreamPort.begin();iter!=_setOfInputDataStreamPort.end();iter++)
61     (*iter)->getAllRepresentants(s);
62   return s;
63 }
64
65 bool OutputDataStreamPort::isAlreadyLinkedWith(InPort *with) const
66 {
67   set<InPort *> s;
68   set<InputDataStreamPort *>::iterator iter;
69   for(iter=_setOfInputDataStreamPort.begin();iter!=_setOfInputDataStreamPort.end();iter++)
70     if(*iter==with)
71       return true;
72   for(iter=_setOfInputDataStreamPort.begin();iter!=_setOfInputDataStreamPort.end();iter++)
73     (*iter)->getAllRepresentants(s);
74   for(set<InPort *>::iterator iter2=s.begin();iter2!=s.end();iter2++)
75     if((*iter2)==with)
76       return true;
77   return false;
78 }
79
80 string OutputDataStreamPort::getNameOfTypeOfCurrentInstance() const
81 {
82   return NAME;
83 }
84
85 bool OutputDataStreamPort::edAddInputDataStreamPort(InputDataStreamPort *port)
86   throw(ConversionException)
87 {
88   DEBTRACE("OutputDataStreamPort::edAddInputDataStreamPort");
89   if(!isAlreadyInSet(port))
90     {
91       if(!port->edGetType()->isAdaptable(edGetType()))
92         {
93           string what="Can not connect 2 ports with incompatible types : ";
94           what=what+ port->edGetType()->id();
95           what=what+" is not a ";
96           what=what+ edGetType()->id();
97           throw ConversionException(what);
98         }
99       _setOfInputDataStreamPort.insert(port);
100       return true;
101     }
102   else
103     return false;
104 }
105
106 int OutputDataStreamPort::edRemoveInputDataStreamPort(InputDataStreamPort *inPort, bool forward) throw(Exception)
107 {
108   if(forward)
109     {
110       set<InPort *> s;
111       inPort->getAllRepresentants(s);
112       for(set<InPort *>::iterator iter=s.begin();iter!=s.end();iter++)
113         _node->getRootNode()->edRemoveLink(this,*iter);
114       return -1;
115     }
116   else    
117     {
118       set<InputDataStreamPort *>::iterator iter=_setOfInputDataStreamPort.find(inPort);
119       if(iter!=_setOfInputDataStreamPort.end())
120         {
121           (*iter)->modified();
122           _setOfInputDataStreamPort.erase(iter);
123           modified();
124           return edGetNumberOfOutLinks();
125         }
126       else
127         throw Exception("OutputDataStreamPort::edRemoveInputPort : link does not exist, unable to remove it");
128     }
129 }
130
131 bool OutputDataStreamPort::addInPort(InPort *inPort) throw(Exception)
132 {
133   DEBTRACE("OutputDataStreamPort::addInPort");
134   if(inPort->getNameOfTypeOfCurrentInstance()!=InputDataStreamPort::NAME)
135     {
136       string what="not compatible type of port requested during building of link FROM ";
137       what+=NAME; what+=" TO "; what+=inPort->getNameOfTypeOfCurrentInstance();
138       throw Exception(what);
139     }
140   return edAddInputDataStreamPort(static_cast<InputDataStreamPort*>(inPort));
141 }
142
143 void OutputDataStreamPort::edRemoveAllLinksLinkedWithMe() throw(Exception)
144 {
145   set<InputDataStreamPort *>::iterator iter;
146   set<InputDataStreamPort *> vec(_setOfInputDataStreamPort);
147   for( set<InputDataStreamPort *>::iterator iter2=vec.begin();iter2!=vec.end();iter2++)
148     edRemoveInputDataStreamPort(*iter2,true);
149   _setOfInputDataStreamPort.clear();
150 }
151
152 int OutputDataStreamPort::removeInPort(InPort *inPort, bool forward) throw(Exception)
153 {
154   DEBTRACE("OutputDataStreamPort::removeInPort");
155   if(inPort->getNameOfTypeOfCurrentInstance()!=InputDataStreamPort::NAME && !forward)
156     {
157       string what="not compatible type of port requested during destruction of for link FROM ";
158       what+=NAME; what+=" TO "; what+=inPort->getNameOfTypeOfCurrentInstance();
159       throw Exception(what);
160     }
161   return edRemoveInputDataStreamPort(static_cast<InputDataStreamPort *>(inPort),forward);
162 }
163
164 bool OutputDataStreamPort::isAlreadyInSet(InputDataStreamPort *inPort) const
165 {
166   return _setOfInputDataStreamPort.find(inPort)!=_setOfInputDataStreamPort.end();
167 }