1 /*=========================================================================
4 Module: $RCSfile: bbtkConnection.cxx,v $
6 Date: $Date: 2008/04/22 14:30:25 $
7 Version: $Revision: 1.10 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See doc/license.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 *\brief Class bbtk::Connection
23 #include "bbtkConnection.h"
24 #include "bbtkFactory.h"
25 #include "bbtkBlackBox.h"
26 #include "bbtkMessageManager.h"
30 Connection::Pointer Connection::New(BlackBox::Pointer from,
31 const std::string& output,
33 const std::string& input ,
34 const Factory::Pointer f )
36 bbtkDebugMessage("object",1,"##> Connection::Connection(\""
37 <<from->bbGetName()<<"\",\""<<output<<"\",\""
38 <<to->bbGetName()<<"\",\""<<input<<"\")"
40 Connection::Pointer p =
41 MakePointer(new Connection(from,output,to,input,f));
42 bbtkDebugMessage("object",1,"<## Connection::Connection(\""
43 <<from->bbGetName()<<"\",\""<<output<<"\",\""
44 <<to->bbGetName()<<"\",\""<<input<<"\")"
49 //==================================================================
50 /// Ctor with the black box from and to and their input and output.
51 /// Check the input and output compatibility
52 Connection::Connection(BlackBox::Pointer from, const std::string& output,
53 BlackBox::Pointer to, const std::string& input ,
54 const Factory::Pointer f )
60 bbtkDebugMessage("object",2,"==> Connection::Connection(\""
61 <<from->bbGetName()<<"\",\""<<output<<"\",\""
62 <<to->bbGetName()<<"\",\""<<input<<"\")"
65 bbtkDebugMessage("connection",1,"==> Connection::Connection(\""
66 <<from->bbGetFullName()<<"\",\""<<output<<"\",\""
67 <<to->bbGetFullName()<<"\",\""<<input<<"\")"
72 if (! from->bbHasOutput(output) )
74 bbtkError("The box \""<<from->bbGetTypeName()<<
75 "\" has no output \""<<output<<"\"");
77 if (! to->bbHasInput(input) )
79 bbtkError("The box \""<<to->bbGetTypeName()<<
80 "\" has no input \""<<input<<"\"");
83 if (to->bbGetInputConnectorMap().find(input)->second->IsConnected())
85 bbtkError("The input \""<<input<<"\" of the box \""<<to->bbGetName()
86 <<"\" is already connected");
89 // std::string t1 ( from->bbGetOutputType(output).name() );
90 // std::string t2 ( to->bbGetInputType(input).name() );
92 if ( from->bbGetOutputType(output) !=
93 to->bbGetInputType(input) )
95 if ( from->bbGetOutputType(output) == typeid(Data) )
97 bbtkWarning("Connection '"
99 <<"' : '"<<from->bbGetName()<<"."<<output
101 <<HumanTypeName<Data>()
102 <<"> : type compatibility with '"
103 <<to->bbGetName()<<"."<<input
104 <<"' will be resolved at run time"
108 else if ( to->bbGetInputType(input) == typeid(Data) )
110 bbtkDebugMessage("Kernel",8," -> '"<<input<<"' type is "
111 <<TypeName<Data>()<<" : can receive any data"
117 // std::cout << "Adaptive connection "<<std::endl;
119 name = from->bbGetName() + "." + output + "-"
120 + to->bbGetName() + "." + input;
121 mAdaptor = mFactory.lock()
122 ->NewAdaptor(from->bbGetOutputType(output),
123 to->bbGetInputType(input),
127 bbtkError("did not find any <"
128 <<TypeName(from->bbGetOutputType(output))
130 <<TypeName(to->bbGetInputType(input))
138 mOriginalFrom = from;
141 mInput = mOriginalInput = input;
142 mOutput = mOriginalOutput = output;
144 // Lock this pointer !!!
145 //Pointer p = MakePointer(this,true);
146 from->bbConnectOutput(output,this);
147 to->bbConnectInput(input,this);
150 bbtkDebugMessage("connection",1,"<== Connection::Connection(\""
151 <<from->bbGetFullName()<<"\",\""<<output<<"\",\""
152 <<to->bbGetFullName()<<"\",\""<<input<<"\")"
155 bbtkDebugMessage("object",2,"==> Connection::Connection(\""
156 <<from->bbGetName()<<"\",\""<<output<<"\",\""
157 <<to->bbGetName()<<"\",\""<<input<<"\")"
160 //==================================================================
162 //==================================================================
164 Connection::~Connection()
166 bbtkDebugMessage("object",2,
167 "==> Connection::~Connection() ["
168 <<GetFullName()<<"]"<<std::endl);
170 if (mAdaptor) mAdaptor.reset();
173 mFrom->bbDisconnectOutput(mOutput,this);
174 // GetThisPointer<Connection>());
179 bbtkInternalError("Connection::~Connection() : invalid initial box pointer");
183 mTo->bbDisconnectInput(mInput,this);// GetThisPointer<Connection>());
188 bbtkInternalError("Connection::~Connection() : invalid final box pointer");
192 bbtkDebugMessage("object",2,
193 "<== Connection::~Connection() ["
194 <<GetFullName()<<"]"<<std::endl);
196 //==================================================================
198 //==================================================================
200 IOStatus Connection::BackwardUpdate()
202 bbtkDebugMessageInc("Process",2,
203 "Connection::BackwardUpdate() ["
204 <<GetFullName()<<"]"<<std::endl);
206 IOStatus s = UPTODATE;
207 s = mFrom->bbBackwardUpdate(GetThisPointer<Connection>());
211 if (mAdaptor && (s==MODIFIED)) mAdaptor->bbSetModifiedStatus();
213 bbtkDebugDecTab("Process",2);
217 //==================================================================
220 //==================================================================
222 void Connection::ForwardUpdate()
224 bbtkDebugMessageInc("Process",2,
225 "Connection::ForwardUpdate() ["
226 <<GetFullName()<<"]"<<std::endl);
231 mTo->bbForwardUpdate(this);
233 bbtkDebugDecTab("Process",2);
235 //==================================================================
238 //==================================================================
239 /// Transfers the data from the source output to the target input
240 /// doing necessary conversions (adaptation or pointer cast)
241 void Connection::TransferData()
243 bbtkDebugMessageInc("Process",3,
244 "Connection::TransferData() ["
245 <<GetFullName()<<"]"<<std::endl);
248 // If an adaptor was created we need to adapt the data
251 mAdaptor->bbSetInput("In",mFrom->bbGetOutput(mOutput),false);
252 mAdaptor->bbExecute();
253 // LG : Connection Update does not set mTo as modified
254 mTo->bbSetInput(mInput, mAdaptor->bbGetOutput("Out"),false);
257 // If no adaptor but source type is an any and target is not an any
258 else if ( mFromAny && (! mToAny) )
260 bbtkDebugMessage("Data",3,
261 "Connection::TransferData() ["
262 <<GetFullName()<<"]"<<std::endl);
263 bbtkDebugMessage("Data",3,
264 " * Source type is an "
265 <<HumanTypeName<Data>()
266 <<" which contains a <"
267 <<HumanTypeName(mFrom->bbGetOutput(mOutput).type())
269 bbtkDebugMessage("Data",3,
270 " * Target type is <"
271 <<HumanTypeName(mTo->bbGetInputType(mInput))
274 // 1) Test strict type matching between any content and target
275 if (mFrom->bbGetOutput(mOutput)
276 .contains( mTo->bbGetInputType(mInput) ) )
278 bbtkDebugMessage("Data",3,
279 " -> Equal types : transfer ok"<<std::endl);
280 mTo->bbSetInput( mInput,
281 mFrom->bbGetOutput(mOutput),
286 // 2) Look for an adaptor
287 bbtk::BlackBox::Pointer adaptor;
290 adaptor = mFactory.lock()
291 ->NewAdaptor(mFrom->bbGetOutput(mOutput).type(),
292 mTo->bbGetInputType(mInput),
300 bbtkDebugMessage("Data",3," -> Adaptor found : using it"
302 adaptor->bbSetInput("In",mFrom->bbGetOutput(mOutput),false);
303 adaptor->bbExecute();
304 // LG : Connection Update does not set mTo as modified
305 mTo->bbSetInput(mInput, adaptor->bbGetOutput("Out"),false);
306 // adaptor->bbDelete();
308 // 3) If no adaptor found but the any content is a pointer
309 // and target type is also a pointer : we try run-time cast
310 else if ( (mFrom->bbGetOutput(mOutput).contains_pointer()) &&
311 (mTo->bbGetDescriptor()->GetInputDescriptor(mInput)
314 bbtkDebugMessage("Data",3,
315 " -> No adaptor found but source and target types are both pointers : trying up or down cast"<<std::endl);
318 mFrom->bbGetOutput(mOutput)
319 .get_pointer_to(mTo->bbGetInput(mInput).pointed_type());
322 bbtkError("Connection '"
325 <<HumanTypeName(mFrom->bbGetOutput(mOutput).type())
327 <<HumanTypeName(mTo->bbGetInputType(mInput))
328 <<"> : no adaptor available and run-time up and down cast failed");
330 mTo->bbBruteForceSetInputPointer(mInput, nptr, false);
332 // 4) Nothing worked : error
335 bbtkError("Connection '"<<GetFullName()<<"' "
336 <<"no adaptor found to convert <"
337 <<HumanTypeName(mFrom->bbGetOutput(mOutput).type())
339 <<HumanTypeName(mTo->bbGetInputType(mInput))<<">");
343 // EO : mFromAny && ! mToAny
344 // Default case : types are the same; we use simple get-set
347 // LG : Connection Update does not set mTo as modified
348 mTo->bbSetInput(mInput, mFrom->bbGetOutput(mOutput),false);
351 bbtkDebugDecTab("Process",3);
353 //==================================================================
355 //==================================================================
357 void Connection::SetModifiedStatus()
359 bbtkDebugMessageInc("Process",5,
360 "Connection::SetModifiedStatus() ["
361 <<GetFullName()<<"]"<<std::endl);
363 if (mAdaptor) mAdaptor->bbSetModifiedStatus();
365 mTo->bbSetModifiedStatus( mTo->bbGetInputConnectorMap().find(mInput)->second );
367 bbtkDebugDecTab("Process",5);
369 //==================================================================
372 //==================================================================
373 std::string Connection::GetFullName() const {
376 std::string res = mFrom->bbGetName()+"."+mOutput+"--"
377 +mTo->bbGetName()+"."+mInput;
378 if ((!mOriginalFrom.expired()) && (!mOriginalTo.expired()) &&
379 ((mFrom!=mOriginalFrom.lock())||(mTo!=mOriginalTo.lock())))
381 res += "("+mOriginalFrom.lock()->bbGetName()
382 +"."+mOriginalOutput+"--"
383 + mOriginalTo.lock()->bbGetName()+"."+mOriginalInput+")";
387 return "***Invalid Connection***";
389 //==================================================================
391 //==================================================================
392 void Connection::Check() const
394 bbtkMessage("debug",1,"** Checking Connection "<<(void*)this<<" ["<<GetFullName()<<"]"
398 bbtkMessage("debug",2," - From = 0"<<std::endl);
402 bbtkMessage("debug",2," - From : "<<mFrom->bbGetFullName()<<std::endl);
403 if (!mFrom->bbHasOutput(mOutput))
405 bbtkError("** Checking Connection "<<(void*)this
406 <<" ["<<GetFullName()<<"] : "
407 << mFrom->bbGetFullName()<<" does not have output '"
410 bbtkMessage("debug",2," - From : Output '"<<mOutput<<"' exists"<<std::endl);
411 BlackBox::OutputConnectorMapType::const_iterator i
412 = mFrom->bbGetOutputConnectorMap().find(mOutput);
413 if (i== mFrom->bbGetOutputConnectorMap().end())
415 bbtkError("** Checking Connection "<<(void*)this
416 <<" ["<<GetFullName()<<"] : "
417 <<mFrom->bbGetFullName()<<" output '"
418 <<mOutput<<"' is not in OutputConnectorMap");
420 bbtkMessage("debug",2," - From : Output '"<<mOutput
421 <<"' is in OutputConnectorMap"<<std::endl);
423 std::vector< Connection* >::const_iterator j;
425 for (j = i->second->GetConnectionVector().begin();
426 j != i->second->GetConnectionVector().end();
429 if ((*j)==this) break;
432 j = find(i->second->GetConnectionVector().begin(),
433 i->second->GetConnectionVector().end(),
436 if (j==i->second->GetConnectionVector().end())
438 bbtkError("** Checking Connection "<<(void*)this
439 <<" ["<<GetFullName()<<"] : "
440 << "Connection ["<<GetFullName()<<"] : "
441 <<" OutputConnector '"
442 <<mOutput<<"' of "<<mFrom->bbGetFullName()
443 <<" does not point to this connection");
446 bbtkMessage("debug",2," - From : This connection is in OutputConnector connection vector"<<std::endl);
447 bbtkMessage("debug",1," * Box from : Check successfull"<<std::endl);
453 bbtkMessage("debug",2," - To = 0"<<std::endl);
457 bbtkMessage("debug",2," - To : "<<mTo->bbGetName()<<std::endl);
458 // std::cout << mTo << std::endl;
459 // std::cout << mTo->bbGetDescriptor() << std::endl;
460 // std::cout << mTo->bbGetDescriptor()->GetTypeName() << std::endl;
461 // mTo->bbGetFullName();
462 bbtkMessage("debug",2," - To : "<<mTo->bbGetFullName()<<std::endl);
463 if (!mTo->bbHasInput(mInput))
465 bbtkError("** Checking Connection "<<(void*)this
466 <<" ["<<GetFullName()<<"] : "
467 <<mTo->bbGetFullName()<<" does not have input '"
470 bbtkMessage("debug",2," - To : Input '"<<mInput<<"' exists"<<std::endl);
471 BlackBox::InputConnectorMapType::const_iterator i
472 = mTo->bbGetInputConnectorMap().find(mInput);
473 if (i== mTo->bbGetInputConnectorMap().end())
475 bbtkError("** Checking Connection "<<(void*)this
476 <<" ["<<GetFullName()<<"] : "
477 <<mTo->bbGetFullName()<<" input '"
478 <<mInput<<"' is not in InputConnectorMap");
480 bbtkMessage("debug",2," - To : Input '"<<mInput
481 <<"' is in InputConnectorMap"<<std::endl);
483 if (i->second->GetConnection()==0)
485 bbtkError("** Checking Connection "<<(void*)this
486 <<" ["<<GetFullName()<<"] : "
487 <<"Connection "<<GetFullName()<<" : "
488 <<" InputConnector '"
489 <<mInput<<"' of "<<mTo->bbGetFullName()
490 <<" does not point to this connection");
493 bbtkMessage("debug",2," - To : This connection is in InputConnector connection vector"<<std::endl);
494 bbtkMessage("debug",1," * Box to : Check successfull"<<std::endl);
498 //==================================================================
499 //==========================================================================
500 std::string Connection::GetObjectName() const
502 std::string s("Connection '");
507 //==========================================================================
509 //==========================================================================
510 std::string Connection::GetObjectInfo() const
515 //==========================================================================
517 //==========================================================================
518 size_t Connection::GetObjectSize() const
520 size_t s = Superclass::GetObjectSize();
521 s += Connection::GetObjectInternalSize();
524 //==========================================================================
525 //==========================================================================
526 size_t Connection::GetObjectInternalSize() const
528 size_t s = sizeof(Connection);
531 //==========================================================================
532 //==========================================================================
533 size_t Connection::GetObjectRecursiveSize() const
535 size_t s = Superclass::GetObjectRecursiveSize();
536 s += Connection::GetObjectInternalSize();
539 //==========================================================================