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