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