]> Creatis software - bbtk.git/blob - kernel/src/bbtkBlackBoxOutputConnector.cxx
f350f89d49b1839ae77502d4df8295bf7b7905b3
[bbtk.git] / kernel / src / bbtkBlackBoxOutputConnector.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbtkBlackBoxOutputConnector.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.14 $
34 =========================================================================*/
35
36
37 /**
38  *  \file 
39  *  \brief Class bbtk::BlackBoxOutputConnector : 
40  */
41
42 #include "bbtkBlackBoxOutputConnector.h"
43 #include "bbtkMessageManager.h"
44 #include "bbtkBlackBox.h"
45 //#include <algorithm>
46
47 namespace bbtk
48 {
49
50   //========================================================================
51   /// The Pimpl
52   class BlackBoxOutputConnector::Pimpl
53   {
54   public:
55     Pimpl() : mStatus(OUTOFDATE) {}
56     Pimpl(const Pimpl&) {}
57    /// 
58     BlackBoxWeakPointer mBox;
59     /// The vector of output connections
60     ConnectionVector mConnection;
61     /// The status of the output 
62     IOStatus mStatus;
63     /// The output changed signal 
64     BlackBox::OutputChangeSignalType mChangeSignal;
65
66
67   };
68   //========================================================================
69
70   //========================================================================
71   BBTK_IMPL_PIMPL(BlackBoxOutputConnector)
72   //========================================================================
73
74
75   //======================================================================
76   BlackBoxOutputConnector::BlackBoxOutputConnector(BlackBox::Pointer b) 
77   {
78     bbtkDebugMessage("kernel",9,
79                      "["<<b->bbGetName()
80                      <<"] BlackBoxOutputConnector()"<<std::endl);
81     PimplConstruct();
82     p->mBox = b;
83   }
84   //======================================================================
85
86
87   //======================================================================
88   BlackBoxOutputConnector::~BlackBoxOutputConnector() 
89   {
90     bbtkDebugMessage("kernel",9,
91                      "[DEAD BOX] ==> ~BlackBoxOutputConnector()"
92                      <<std::endl);
93     /*
94     ConnectionVector::iterator i;
95     for (i=mConnection.begin();
96          i!=mConnection.end();
97          ++i)
98       (*i) = Connection::WeakPointer();
99     */
100     p->mConnection.clear();
101     PimplDestruct();
102   
103     bbtkDebugMessage("kernel",9,
104                      "[DEAD BOX] <== ~BlackBoxOutputConnector()"
105                      <<std::endl);
106   }
107   //======================================================================
108
109   //======================================================================
110   ///
111   void BlackBoxOutputConnector::SetConnection(Connection* c)
112   {
113     bbtkDebugMessage("kernel",9,
114                      "["<<p->mBox.lock()->bbGetName()
115                      <<"] BlackBoxOutputConnector::SetConnection("
116                      <<c<<")"<<std::endl);
117     p->mConnection.push_back(c);
118     //    AddChangeObserver(boost::bind(&Connection::SignalChange,c));
119   }
120   //======================================================================
121
122
123   //======================================================================
124   void BlackBoxOutputConnector::UnsetConnection(Connection* c)
125   {
126     bbtkDebugMessage("kernel",9,"["<<p->mBox.lock()->bbGetName()
127                      <<"] BlackBoxOutputConnector::UnsetConnection("
128                      <<c<<")"<<std::endl);
129
130     if (!c) 
131       {
132         bbtkInternalError(p->mBox.lock()->bbGetFullName()
133                           <<": BlackBoxOutputConnector::UnsetConnection("
134                           <<c<<") : invalid connection");
135
136       }
137     //    c->Check();
138     //Connection::WeakPointer w(c);
139     ConnectionVector::iterator i;
140     //  = find(mConnection.begin(),mConnection.end(),c);
141     for (i=p->mConnection.begin();
142          i!=p->mConnection.end();
143          ++i)
144       {
145         if (*i==c) break;
146       }
147     if (i==p->mConnection.end())
148       {
149         bbtkInternalError(p->mBox.lock()->bbGetFullName()
150                           <<": BlackBoxOutputConnector::UnsetConnection("
151                           <<c
152                           <<") : connection is absent from connections list");
153       }
154     p->mConnection.erase(i);
155     //    RemoveChangeObserver(boost::bind(&Connection::SignalChange,c));
156
157
158   }
159   //======================================================================
160   
161   //======================================================================
162   const BlackBoxOutputConnector::ConnectionVector& 
163   BlackBoxOutputConnector::GetConnectionVector() const
164   { 
165     return p->mConnection; 
166   }
167   //======================================================================
168   
169   //======================================================================
170   /// Returns the status of the input 
171   IOStatus BlackBoxOutputConnector::GetStatus() const 
172   { 
173     return p->mStatus; 
174   }
175   //======================================================================
176
177   //======================================================================
178   /// Sets the status of the input 
179   void BlackBoxOutputConnector::SetStatus( IOStatus s ) 
180   {
181     p->mStatus = s; 
182   }
183   //======================================================================
184   
185   //======================================================================
186   void BlackBoxOutputConnector::SignalChange( BlackBox::Pointer box,
187                                               const std::string& output )
188   {
189     IOStatus s = OUTOFDATE; //mStatus;
190     //if (s==UPTODATE) s=MODIFIED;
191     
192     bbtkDebugMessage("change",2,
193                      "["<<box->bbGetName()
194                      <<"] ==> BlackBoxOutputConnector::SignalChange('"
195                      <<output<<"','"<<GetIOStatusString(s)<<"') ["
196                      <<this<<"]"
197                      <<std::endl);
198     
199     //    std::cout<<"BlackBoxOutputConnector::SignalChange("
200     //       <<box->bbGetName()<<",'"
201     //       <<output<<"')"<<std::endl;
202     p->mChangeSignal(box,output,s);
203     
204     bbtkDebugMessage("change",2,
205                      "["<<box->bbGetName()
206                      <<"] <== BlackBoxOutputConnector::SignalChange('"
207                      <<output<<"','"<<GetIOStatusString(s)<<"') ["
208                      <<this<<"]"
209                      <<std::endl);
210   }
211   //======================================================================
212
213   //======================================================================
214   void BlackBoxOutputConnector::AddChangeObserver(OutputChangeCallbackType f)
215   { 
216     //    std::cout << "BlackBoxOutputConnector::AddChangeObserver" << std::endl;
217     p->mChangeSignal.connect(f); 
218   }
219   //======================================================================
220   
221   //======================================================================
222   /// Removes the function f from the list of functions to call when 
223   /// the output changes (TO WRITE)
224   void BlackBoxOutputConnector::RemoveChangeObserver(OutputChangeCallbackType f)
225   { 
226     bbtkError("BlackBoxOutputConnector::RemoveChangeObserver not implemented");
227     
228     //mChangeSignal.disconnect(f);
229   }
230   //======================================================================
231 }
232 // namespace bbtk
233