Salome HOME
3a0c80d5904160ff71711f21d09c66922b6ea9e0
[modules/yacs.git] / src / engine / OutGate.cxx
1 // Copyright (C) 2006-2023  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 "OutGate.hxx"
21 #include "InGate.hxx"
22
23 //#define _DEVDEBUG_
24 #include "YacsTrace.hxx"
25
26 #include <algorithm>
27
28 using namespace YACS::ENGINE;
29 using namespace std;
30
31 const char OutGate::NAME[]="OutGate";
32
33 OutGate::OutGate(Node *node):Port(node)
34 {
35 }
36
37 string OutGate::getNameOfTypeOfCurrentInstance() const
38 {
39   return NAME;
40 }
41
42 void OutGate::exReset()
43 {
44   for(list< pair< InGate *, bool> >::iterator iter=_setOfInGate.begin();iter!=_setOfInGate.end();iter++)
45     (*iter).second=false;
46 }
47
48 //! Notify this port that its node is finished
49 /*!
50  *  Calls (notify) all the connected ingates : InGate::exNotifyFromPrecursor
51  *
52  *  Called by Bloc::updateStateOnFinishedEventFrom
53  */
54
55 void OutGate::exNotifyDone()
56 {
57   DEBTRACE("OutGate::exNotifyDone");
58   for(list< pair<InGate *, bool> >::iterator iter=_setOfInGate.begin();iter!=_setOfInGate.end();iter++)
59     (*iter).first->exNotifyFromPrecursor(this);
60 }
61
62 //! Notify this port that its node has failed
63 /*!
64  *
65  */
66 void OutGate::exNotifyFailed()
67 {
68   for(list< pair<InGate *, bool> >::iterator iter=_setOfInGate.begin();iter!=_setOfInGate.end();iter++)
69     (*iter).first->exNotifyFailed();
70 }
71
72 //! Notify this port that its node has been disabled
73 /*!
74  *
75  */
76 void OutGate::exNotifyDisabled()
77 {
78   for(list< pair<InGate *, bool> >::iterator iter=_setOfInGate.begin();iter!=_setOfInGate.end();iter++)
79     (*iter).first->exNotifyDisabled();
80 }
81
82 void OutGate::edDisconnectAllLinksFromMe()
83 {
84   for(list< pair<InGate *, bool> >::iterator iter=_setOfInGate.begin();iter!=_setOfInGate.end();iter++)
85     (*iter).first->edRemovePrecursor(this);
86   _setOfInGate.clear();
87 }
88
89 class ItemCmp
90 {
91 private:
92   InGate *_itf;
93 public:
94   ItemCmp(InGate *itf):_itf(itf) { }
95   bool operator()(const std::pair<InGate *,bool>& elt) const { return elt.first==_itf; }
96 };
97
98 bool OutGate::edAddInGate(InGate *inGate)
99 {
100   if(!isAlreadyInSet(inGate))
101     {
102       inGate->edAppendPrecursor(this);
103       _setOfInGate.push_back(std::pair<InGate *,bool>(inGate,false));
104       modified();
105       inGate->modified();
106       return true;
107     }
108   else
109     return false;
110 }
111
112 std::list<InGate *> OutGate::edSetInGate() const
113 {
114   list<InGate *> ret;
115   for(list< pair<InGate *, bool> >::const_iterator iter=_setOfInGate.begin();iter!=_setOfInGate.end();iter++)
116     ret.push_back((*iter).first);
117   return ret;
118 }
119
120 void OutGate::edRemoveInGate(InGate *inGate, bool coherenceWithInGate)
121 {
122   std::list< pair<InGate* , bool> >::iterator iter(std::find_if(_setOfInGate.begin(),_setOfInGate.end(),ItemCmp(inGate)));
123   if(iter==_setOfInGate.end())
124     throw Exception("InGate not already connected to OutGate");
125   else
126     {
127       if(coherenceWithInGate)
128         inGate->edRemovePrecursor(this);
129       _setOfInGate.erase(iter);
130       inGate->modified();
131       modified();
132     }
133 }
134
135 //Idem OutGate::edRemoveInGate except that no exception thrown if CF not exists
136 void OutGate::edRemoveInGateOneWay(InGate *inGate)
137 {
138   bool found=false;
139   for(list< pair<InGate *, bool> >::iterator iter=_setOfInGate.begin();iter!=_setOfInGate.end() && !found;iter++)
140     if((*iter).first==inGate)
141       {
142         _setOfInGate.erase(iter);
143         inGate->edRemovePrecursor(this);
144         found=true;
145         modified();
146         inGate->modified();
147       }
148 }
149
150 bool OutGate::isAlreadyInSet(InGate *inGate) const
151 {
152   return find_if(_setOfInGate.begin(),_setOfInGate.end(),ItemCmp(inGate))!=_setOfInGate.end();
153 }
154
155 int OutGate::getNbOfInGatesConnected() const
156 {
157   return _setOfInGate.size();
158 }