]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkConnection.cxx
=== MAJOR RELEASE ====
[bbtk.git] / kernel / src / bbtkConnection.cxx
index 29bab6cc40a63308c6c3fca425e676403cd1ce3a..1e84edfa3d0395e3afa7da5b930913703c8d0ecf 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkConnection.cxx,v $
   Language:  C++
-  Date:      $Date: 2008/04/09 11:16:57 $
-  Version:   $Revision: 1.6 $
+  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 = mOriginalFrom = from;
-    mTo = mOriginalTo = to;
-    mInput = mOriginalInput = input;
-    mOutput = mOriginalOutput = 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,36 +128,60 @@ namespace bbtk
          }
       }
 
-     // 
-    from->bbConnectOutput(output,this);
-    to->bbConnectInput(input,this);
-    
-    bbtkDebugDecTab("Kernel",7);
-  }
+     // 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;
 
-  //==================================================================
-  
-  void Connection::Clear()
-  {
-    mFrom = mOriginalFrom = 0;
-    mTo = mOriginalTo = 0;
-    mAdaptor = 0;
+    
+    bbtkDebugMessage("object",2,"==> Connection::Connection(\""
+                    <<from->bbGetName()<<"\",\""<<output<<"\",\""
+                    <<to->bbGetName()<<"\",\""<<input<<"\")"
+                    <<std::endl);    
   }
-
+ //==================================================================
   //==================================================================
   /// Dtor 
   Connection::~Connection()
   {
-    bbtkDebugMessageInc("Kernel",7,
-                       "Connection::~Connection() ["
-                       <<GetFullName()<<"]"<<std::endl);
-    std::cout << "~Connection() " << GetFullName() << std::endl;
-
-    if (mFrom!=0) mFrom->bbDisconnectOutput(mOutput,this);
-    if (mTo!=0) mTo->bbDisconnectInput(mInput,this);
-    if (mAdaptor!=0) mAdaptor->bbDelete();
+    bbtkDebugMessage("object",2,
+                    "==> Connection::~Connection() ["
+                    <<GetFullName()<<"]"<<std::endl);
+
+    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);
   }
   //==================================================================
   
@@ -155,7 +194,7 @@ namespace bbtk
                        <<GetFullName()<<"]"<<std::endl);
 
     IOStatus s = UPTODATE;
-    s = mFrom->bbBackwardUpdate(this);
+    s = mFrom->bbBackwardUpdate(GetThisPointer<Connection>());
 
     TransferData();
 
@@ -235,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 (...)
              {
@@ -253,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
@@ -325,10 +365,12 @@ namespace bbtk
       {
        std::string res = mFrom->bbGetName()+"."+mOutput+"--"
          +mTo->bbGetName()+"."+mInput;
-       if ((mFrom!=mOriginalFrom)||(mTo!=mOriginalTo))
+       if ((!mOriginalFrom.expired()) && (!mOriginalTo.expired()) &&
+           ((mFrom!=mOriginalFrom.lock())||(mTo!=mOriginalTo.lock())))
          {
-           res += "("+mOriginalFrom->bbGetName()+"."+mOriginalOutput+"--"
-             + mOriginalTo->bbGetName()+"."+mOriginalInput+")";
+           res += "("+mOriginalFrom.lock()->bbGetName()
+             +"."+mOriginalOutput+"--"
+             + mOriginalTo.lock()->bbGetName()+"."+mOriginalInput+")";
          }
        return res;
       }
@@ -350,7 +392,9 @@ namespace bbtk
        bbtkMessage("Debug",2," - From : "<<mFrom->bbGetFullName()<<std::endl);
        if (!mFrom->bbHasOutput(mOutput))
          {
-           bbtkError(mFrom->bbGetFullName()<<" does not have output '"
+           bbtkError("** Checking Connection "<<(void*)this
+                      <<" ["<<GetFullName()<<"] : "
+                     << mFrom->bbGetFullName()<<" does not have output '"
                      <<mOutput<<"'");
          }     
        bbtkMessage("Debug",2," - From : Output '"<<mOutput<<"' exists"<<std::endl);
@@ -358,19 +402,31 @@ namespace bbtk
          = mFrom->bbGetOutputConnectorMap().find(mOutput);
        if (i== mFrom->bbGetOutputConnectorMap().end())
          {
-            bbtkError(mFrom->bbGetFullName()<<" output '"
+            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 * >::const_iterator j;
+        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(),
-                this);
+                GetThisPointer<Connection>());
+       */
        if (j==i->second->GetConnectionVector().end())
          {
-           bbtkError("Connection ["<<GetFullName()<<"] : "
+           bbtkError("** Checking Connection "<<(void*)this
+                     <<" ["<<GetFullName()<<"] : "
+                     << "Connection ["<<GetFullName()<<"] : "
                      <<" OutputConnector '"
                      <<mOutput<<"' of "<<mFrom->bbGetFullName()
                      <<" does not point to this connection");
@@ -388,14 +444,16 @@ namespace bbtk
     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();
+       //      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(mTo->bbGetFullName()<<" does not have input '"
+           bbtkError("** Checking Connection "<<(void*)this
+                     <<" ["<<GetFullName()<<"] : "
+                     <<mTo->bbGetFullName()<<" does not have input '"
                      <<mInput<<"'");
          }     
        bbtkMessage("Debug",2," - To   : Input '"<<mInput<<"' exists"<<std::endl);
@@ -403,7 +461,9 @@ namespace bbtk
          = mTo->bbGetInputConnectorMap().find(mInput);
        if (i== mTo->bbGetInputConnectorMap().end())
          {
-            bbtkError(mTo->bbGetFullName()<<" input '"
+            bbtkError("** Checking Connection "<<(void*)this
+                      <<" ["<<GetFullName()<<"] : "
+                      <<mTo->bbGetFullName()<<" input '"
                       <<mInput<<"' is not in InputConnectorMap");
          }
        bbtkMessage("Debug",2," - To   : Input '"<<mInput
@@ -411,7 +471,9 @@ namespace bbtk
 
        if (i->second->GetConnection()==0)
          {
-           bbtkError("Connection "<<GetFullName()<<" : "
+           bbtkError("** Checking Connection "<<(void*)this
+                     <<" ["<<GetFullName()<<"] : "
+                     <<"Connection "<<GetFullName()<<" : "
                      <<" InputConnector '"
                      <<mInput<<"' of "<<mTo->bbGetFullName()
                      <<" does not point to this connection");
@@ -423,6 +485,47 @@ namespace bbtk
       }
   }
   //==================================================================
+ //==========================================================================
+  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