]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkConnection.cxx
=== MAJOR RELEASE ====
[bbtk.git] / kernel / src / bbtkConnection.cxx
index 8d60b38699afa0f73b65fa34d6243c95155a5d09..1e84edfa3d0395e3afa7da5b930913703c8d0ecf 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkConnection.cxx,v $
   Language:  C++
-  Date:      $Date: 2008/03/18 12:51:26 $
-  Version:   $Revision: 1.4 $
+  Date:      $Date: 2008/04/18 12:59:15 $
+  Version:   $Revision: 1.7 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See doc/license.txt or
 
 namespace bbtk
 {
-  
+  Connection::Pointer Connection::New(BlackBox::Pointer from, 
+                                     const std::string& output,
+                                     BlackBox::Pointer to, 
+                                     const std::string& input ,
+                                     const Factory::Pointer f  )
+  {
+    bbtkDebugMessage("object",1,"##> Connection::Connection(\""
+                    <<from->bbGetName()<<"\",\""<<output<<"\",\""
+                    <<to->bbGetName()<<"\",\""<<input<<"\")"
+                    <<std::endl);
+    Connection::Pointer p = 
+      MakePointer(new Connection(from,output,to,input,f));
+    bbtkDebugMessage("object",1,"<## Connection::Connection(\""
+                    <<from->bbGetName()<<"\",\""<<output<<"\",\""
+                    <<to->bbGetName()<<"\",\""<<input<<"\")"
+                    <<std::endl);
+    return p;
+  }
+
   //==================================================================
   /// Ctor with the black box from and to and their input and output.
 /// Check the input and output compatibility
-  Connection::Connection(BlackBox* from, const std::string& output,
-                        BlackBox* to, const std::string& input ,
-                        const Factory* f  )
-    : mAdaptor(0),
+Connection::Connection(BlackBox::Pointer from, const std::string& output,
+                      BlackBox::Pointer to, const std::string& input ,
+                      const Factory::Pointer f  )
+    : mAdaptor(),
       mFactory(f),
       mFromAny(false),
       mToAny(false)
   {
-    bbtkDebugMessageInc("Kernel",7,"Connection::Connection(\""
-                       <<from->bbGetName()<<"\",\""<<output<<"\",\""
-                       <<to->bbGetName()<<"\",\""<<input<<"\")"
-                       <<std::endl);    
+    bbtkDebugMessage("object",2,"==> Connection::Connection(\""
+                    <<from->bbGetName()<<"\",\""<<output<<"\",\""
+                    <<to->bbGetName()<<"\",\""<<input<<"\")"
+                    <<std::endl);    
 
-    mFrom = from;
-    mTo = to;
-    mInput = input;
-    mOutput = output;
     
 
     if (! from->bbHasOutput(output) )
@@ -99,9 +113,10 @@ namespace bbtk
            std::string name;
            name = from->bbGetName() + "." + output + "-" 
              + to->bbGetName() + "." + input; 
-           mAdaptor = mFactory->NewAdaptor(from->bbGetOutputType(output),
-                                 to->bbGetInputType(input),
-                                 name);
+           mAdaptor = mFactory.lock()
+             ->NewAdaptor(from->bbGetOutputType(output),
+                          to->bbGetInputType(input),
+                          name);
            if (!mAdaptor)  
              {  
                bbtkError("did not find any <"
@@ -113,29 +128,60 @@ namespace bbtk
          }
       }
 
-     // 
-    from->bbConnectOutput(output,this);
-    to->bbConnectInput(input,this);
+     // Lock this pointer !!!
+    Pointer p = MakePointer(this,true);
+    from->bbConnectOutput(output,p);
+    to->bbConnectInput(input,p);
+    mFrom = from;
+    mOriginalFrom = from;
+    mTo = to;
+    mOriginalTo = to;
+    mInput = mOriginalInput = input;
+    mOutput = mOriginalOutput = output;
+
     
-    bbtkDebugDecTab("Kernel",7);
+    bbtkDebugMessage("object",2,"==> Connection::Connection(\""
+                    <<from->bbGetName()<<"\",\""<<output<<"\",\""
+                    <<to->bbGetName()<<"\",\""<<input<<"\")"
+                    <<std::endl);    
   }
-
-  //==================================================================
-  
-  
+ //==================================================================
   //==================================================================
   /// Dtor 
   Connection::~Connection()
   {
-    bbtkDebugMessageInc("Kernel",7,
-                       "Connection::~Connection() ["
-                       <<GetFullName()<<"]"<<std::endl);
+    bbtkDebugMessage("object",2,
+                    "==> Connection::~Connection() ["
+                    <<GetFullName()<<"]"<<std::endl);
 
-    mFrom->bbDisconnectOutput(mOutput,this);
-    mTo->bbDisconnectInput(mInput,this);
-    if (mAdaptor) mAdaptor->bbDelete();
+    if (mAdaptor) mAdaptor.reset();
+    mOriginalFrom.reset();
+    mOriginalTo.reset();
+    if (mFrom!=0) 
+      {
+       mFrom->bbDisconnectOutput(mOutput,
+                                 GetThisPointer<Connection>());
+      }
+    else 
+      {
+       bbtkInternalError("Connection::~Connection() : invalid initial box pointer");
+      }
+    if (mTo!=0) 
+      {
+       mTo->bbDisconnectInput(mInput,
+                              GetThisPointer<Connection>());
+      }
+    else 
+      {
+       bbtkInternalError("Connection::~Connection() : invalid final box pointer");
+      }
+   mFrom.reset();
+    mTo.reset();
 
-    bbtkDebugDecTab("Kernel",7);
+    bbtkDebugMessage("object",2,
+                    "<== Connection::~Connection() ["
+                    <<GetFullName()<<"]"<<std::endl);
   }
   //==================================================================
   
@@ -148,7 +194,7 @@ namespace bbtk
                        <<GetFullName()<<"]"<<std::endl);
 
     IOStatus s = UPTODATE;
-    s = mFrom->bbBackwardUpdate(this);
+    s = mFrom->bbBackwardUpdate(GetThisPointer<Connection>());
 
     TransferData();
 
@@ -228,12 +274,13 @@ namespace bbtk
        else 
          {
            // 2) Look for an adaptor
-           bbtk::BlackBox* adaptor = 0;
+           bbtk::BlackBox::Pointer adaptor;
            try 
              {
-               adaptor = mFactory->NewAdaptor(mFrom->bbGetOutput(mOutput).type(),
-                                    mTo->bbGetInputType(mInput),
-                                    "");
+               adaptor = mFactory.lock()
+                 ->NewAdaptor(mFrom->bbGetOutput(mOutput).type(),
+                              mTo->bbGetInputType(mInput),
+                              "");
              }
            catch (...)
              {
@@ -246,7 +293,7 @@ namespace bbtk
                adaptor->bbExecute();
                // LG : Connection Update does not set mTo as modified
                mTo->bbSetInput(mInput, adaptor->bbGetOutput("Out"),false);
-               adaptor->bbDelete();
+               //      adaptor->bbDelete();
              }
            // 3) If no adaptor found but the any content is a pointer
            //    and target type is also a pointer : we try run-time cast
@@ -314,14 +361,171 @@ namespace bbtk
   
   //==================================================================
   std::string Connection::GetFullName() const {
-    if (mFrom && mTo)
-      return  mFrom->bbGetName()+"."+mOutput+"--"
-       +mTo->bbGetName()+"."+mInput;
+    if (mFrom && mTo) 
+      {
+       std::string res = mFrom->bbGetName()+"."+mOutput+"--"
+         +mTo->bbGetName()+"."+mInput;
+       if ((!mOriginalFrom.expired()) && (!mOriginalTo.expired()) &&
+           ((mFrom!=mOriginalFrom.lock())||(mTo!=mOriginalTo.lock())))
+         {
+           res += "("+mOriginalFrom.lock()->bbGetName()
+             +"."+mOriginalOutput+"--"
+             + mOriginalTo.lock()->bbGetName()+"."+mOriginalInput+")";
+         }
+       return res;
+      }
     return "***Invalid Connection***";
   }
   //==================================================================
 
+  //==================================================================
+  void Connection::Check() const
+  {
+    bbtkMessage("Debug",1,"** Checking Connection "<<(void*)this<<" ["<<GetFullName()<<"]"
+               <<std::endl);
+    if (mFrom==0) 
+      {
+       bbtkMessage("Debug",2," - From = 0"<<std::endl);
+      }
+    else
+      {
+       bbtkMessage("Debug",2," - From : "<<mFrom->bbGetFullName()<<std::endl);
+       if (!mFrom->bbHasOutput(mOutput))
+         {
+           bbtkError("** Checking Connection "<<(void*)this
+                      <<" ["<<GetFullName()<<"] : "
+                     << mFrom->bbGetFullName()<<" does not have output '"
+                     <<mOutput<<"'");
+         }     
+       bbtkMessage("Debug",2," - From : Output '"<<mOutput<<"' exists"<<std::endl);
+       BlackBox::OutputConnectorMapType::const_iterator i 
+         = mFrom->bbGetOutputConnectorMap().find(mOutput);
+       if (i== mFrom->bbGetOutputConnectorMap().end())
+         {
+            bbtkError("** Checking Connection "<<(void*)this
+                      <<" ["<<GetFullName()<<"] : "
+                      <<mFrom->bbGetFullName()<<" output '"
+                      <<mOutput<<"' is not in OutputConnectorMap");
+         }
+       bbtkMessage("Debug",2," - From : Output '"<<mOutput
+                   <<"' is in OutputConnectorMap"<<std::endl);
 
+        std::vector< Connection::WeakPointer >::const_iterator j;
+       for (j  = i->second->GetConnectionVector().begin();
+            j != i->second->GetConnectionVector().end();
+            ++j)
+         {
+           if ((*j).lock()==GetThisPointer<Connection>()) break;
+         }
+       /*
+       j = find(i->second->GetConnectionVector().begin(),
+                i->second->GetConnectionVector().end(),
+                GetThisPointer<Connection>());
+       */
+       if (j==i->second->GetConnectionVector().end())
+         {
+           bbtkError("** Checking Connection "<<(void*)this
+                     <<" ["<<GetFullName()<<"] : "
+                     << "Connection ["<<GetFullName()<<"] : "
+                     <<" OutputConnector '"
+                     <<mOutput<<"' of "<<mFrom->bbGetFullName()
+                     <<" does not point to this connection");
+           
+         }
+       bbtkMessage("Debug",2," - From : This connection is in OutputConnector connection vector"<<std::endl);
+       bbtkMessage("Debug",1," * Box from : Check successfull"<<std::endl);
+
+      }
+
+    if (mTo==0) 
+      {
+       bbtkMessage("Debug",2," - To   = 0"<<std::endl);
+      }
+    else
+      {
+       bbtkMessage("Debug",2," - To   : "<<mTo->bbGetName()<<std::endl);
+       //      std::cout << mTo << std::endl;
+       //      std::cout << mTo->bbGetDescriptor() << std::endl;
+       //      std::cout << mTo->bbGetDescriptor()->GetTypeName() << std::endl;
+       //      mTo->bbGetFullName();
+       bbtkMessage("Debug",2," - To   : "<<mTo->bbGetFullName()<<std::endl);
+       if (!mTo->bbHasInput(mInput))
+         {
+           bbtkError("** Checking Connection "<<(void*)this
+                     <<" ["<<GetFullName()<<"] : "
+                     <<mTo->bbGetFullName()<<" does not have input '"
+                     <<mInput<<"'");
+         }     
+       bbtkMessage("Debug",2," - To   : Input '"<<mInput<<"' exists"<<std::endl);
+       BlackBox::InputConnectorMapType::const_iterator i 
+         = mTo->bbGetInputConnectorMap().find(mInput);
+       if (i== mTo->bbGetInputConnectorMap().end())
+         {
+            bbtkError("** Checking Connection "<<(void*)this
+                      <<" ["<<GetFullName()<<"] : "
+                      <<mTo->bbGetFullName()<<" input '"
+                      <<mInput<<"' is not in InputConnectorMap");
+         }
+       bbtkMessage("Debug",2," - To   : Input '"<<mInput
+                   <<"' is in InputConnectorMap"<<std::endl);
+
+       if (i->second->GetConnection()==0)
+         {
+           bbtkError("** Checking Connection "<<(void*)this
+                     <<" ["<<GetFullName()<<"] : "
+                     <<"Connection "<<GetFullName()<<" : "
+                     <<" InputConnector '"
+                     <<mInput<<"' of "<<mTo->bbGetFullName()
+                     <<" does not point to this connection");
+    
+         }
+       bbtkMessage("Debug",2," - To   : This connection is in InputConnector connection vector"<<std::endl);
+       bbtkMessage("Debug",1," * Box to   : Check successfull"<<std::endl);
+
+      }
+  }
+  //==================================================================
+ //==========================================================================
+  std::string Connection::GetObjectName() const
+  {
+    std::string s("Connection '");
+    s += GetFullName();
+    s += "'";
+    return s;
+  }
+  //==========================================================================
+  
+  //==========================================================================
+  std::string  Connection::GetObjectInfo() const 
+  {
+    std::stringstream i;
+    return i.str();
+  }
+  //==========================================================================
+
+ //==========================================================================
+size_t  Connection::GetObjectSize() const 
+{
+  size_t s = Superclass::GetObjectSize();
+  s += Connection::GetObjectInternalSize();
+  return s;
+  }
+  //==========================================================================
+  //==========================================================================
+size_t  Connection::GetObjectInternalSize() const 
+{
+  size_t s = sizeof(Connection);
+  return s;
+  }
+  //==========================================================================
+  //==========================================================================
+  size_t  Connection::GetObjectRecursiveSize() const 
+  {
+    size_t s = Superclass::GetObjectRecursiveSize();
+    s += Connection::GetObjectInternalSize();
+    return s;
+  }
+  //==========================================================================
 
 }// namespace bbtk