Salome HOME
145b61bc33427bac36052e8824a2982614b868d3
[modules/yacs.git] / src / engine / InPort.cxx
1 // Copyright (C) 2006-2020  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, 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 "InPort.hxx"
21 #include "OutPort.hxx"
22 #include "ComposedNode.hxx"
23
24 #include <iostream>
25 #include <algorithm>
26
27 using namespace YACS::ENGINE;
28 using namespace std;
29
30 InPort::InPort(const InPort& other, Node *newHelder):
31   DataPort(other,newHelder),Port(other,newHelder)
32 {
33 }
34
35 InPort::InPort(const std::string& name, Node *node, TypeCode* type):
36   DataPort(name,node,type),Port(node)
37 {
38 }
39
40 InPort::~InPort()
41 {
42 }
43
44 //! Returns number of \b physical backlinks \b NOT number of user backlinks.
45 int InPort::edGetNumberOfLinks() const
46 {
47   return _backLinks.size();
48 }
49
50 void InPort::edRemoveAllLinksLinkedWithMe() 
51 {
52   set< std::pair<OutPort *,bool> > temp(_backLinks);//edRemoveLink called after causes invalidation of set iterator.
53   for(auto iter : temp)
54     {
55       set<OutPort *> trueBackOutputs;
56       iter.first->getAllRepresented(trueBackOutputs);
57       for( auto iter2 : trueBackOutputs )
58         _node->getRootNode()->edRemoveLink(iter2,this);
59     }
60   _backLinks.clear();
61   modified();
62 }
63
64 //! Returns \b physical backlinks \b NOT user backlinks.
65 std::set<OutPort *> InPort::edSetOutPort() const
66 {
67   std::set<OutPort *> ret;
68   for( auto iter : _backLinks )
69     ret.insert(iter.first);
70   return ret;
71 }
72
73 bool InPort::canSafelySqueezeMemory() const
74 {
75   if(!isBackLinked())
76     return false;
77   for(auto bl : _backLinks)
78     {
79       if(!bl.second)
80         return false;
81     }
82   return true;
83 }
84
85 /*!
86  * \param [in] isLoopProof - Tells if the data coming from \a fromPort will be send again in case of \a this is initialized. This value is
87  *                           important if \a this is an InPort of a Node contained directly or not inside a Loop, ForEachLoop, OptimizerLoop.
88  *                           In this case, to optimize memory consumption (see squeezeMemory method), we need to know if data coming from \a fromPort
89  *                           will be generated again in case.
90  *                           If true (the default) it means that for that link is a link loop proof so no need to take care. If false, the link is not loop proof so
91  *                           event in the context of agressive memory management the data can't be safely released.
92  */
93 void InPort::edNotifyReferencedBy(OutPort *fromPort, bool isLoopProof)
94 {
95   auto it(std::find_if(_backLinks.begin(),_backLinks.end(),[fromPort](const std::pair<OutPort *,bool>& p){ return p.first==fromPort; }));
96   if(it!=_backLinks.end())
97     _backLinks.erase(it);
98   _backLinks.insert(std::pair<OutPort *,bool>(fromPort,isLoopProof));
99   modified();
100 }
101
102 void InPort::edNotifyDereferencedBy(OutPort *fromPort)
103 {
104   auto it(std::find_if(_backLinks.begin(),_backLinks.end(),[fromPort](const std::pair<OutPort *,bool>& p){ return p.first==fromPort; }));
105   if(it!=_backLinks.end())
106     _backLinks.erase(it);
107   modified();
108 }
109
110 void InPort::getAllRepresentants(std::set<InPort *>& repr) const
111 {
112   repr.insert((InPort *)this);
113 }