]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkExecuter.cxx
=== MAJOR RELEASE ====
[bbtk.git] / kernel / src / bbtkExecuter.cxx
index cbb82eb059299bc981eba938cc0553101d42fb3a..67e4788268b216a4975a899b7403ee8b49d52e44 100644 (file)
@@ -3,8 +3,8 @@
   Program:   bbtk
   Module:    $RCSfile: bbtkExecuter.cxx,v $ $
   Language:  C++
-  Date:      $Date: 2008/04/09 11:16:57 $
-  Version:   $Revision: 1.17 $
+  Date:      $Date: 2008/04/18 12:59:15 $
+  Version:   $Revision: 1.18 $
 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 
 namespace bbtk
 {
+  //=======================================================================
+  Executer::Pointer Executer::New()
+  {
+    bbtkDebugMessage("Kernel",9,"Executer::New()"<<std::endl);
+    return MakePointer(new Executer());
+  }
+  //=======================================================================
+
   //=======================================================================
   Executer::Executer()
     : 
-    mFactory(0),
-    mRootPackage(0),
-    mRootCBB(0),
+    mFactory(),
+    mRootPackage(),
+    mRootCBB(),
     mNoExecMode(false),
     mDialogMode(NoDialog)
   {
     bbtkDebugMessageInc("Kernel",9,"Executer::Executer()" <<std::endl);
-    mFactory = new Factory;
-    mFactory->SetExecuter(this);
+    mFactory = Factory::New();
+    // The smart pointer on this is not made yet (is made by New) 
+    // -> create it to pass it to the factory
+    // We have to "lock" the smart pointer because the factory
+    // only keeps a weak pointer on the executer
+    // -> this would auto-destroy !!
+    mFactory->SetExecuter(MakePointer(this,true));
     Reset();
     bbtkDebugDecTab("Kernel",9);
   }
@@ -56,18 +69,11 @@ namespace bbtk
   //=======================================================================
   Executer::~Executer()
   {
-     bbtkDebugMessageInc("Kernel",9,"Executer::~Executer()" <<std::endl);
-     if (mRootCBB) 
-     {
-        mRootPackage->UnRegisterBlackBox("workspace");
-        mRootCBB->UnReference();
-     }
-     if (mRootPackage)
-     {
-        GetFactory()->UnLoadPackage("user");
-     }
-     delete mFactory;
-
+     bbtkDebugMessageInc("Kernel",9,"==> Executer::~Executer()" <<std::endl);
+     mOpenDefinition.clear();
+     mOpenPackage.clear();
+     mFactory->Reset();
+     mFactory.reset();
      bbtkDebugDecTab("Kernel",9);
   }
   //=======================================================================
@@ -94,45 +100,37 @@ namespace bbtk
     bbtkDebugMessageInc("Kernel",9,"Executer::Reset()" <<std::endl);
 
     GetFactory()->CheckPackages();
-
-    // The 'user' package must be closed before all other 
-    // because box destructors must not be unloaded when bb are deleted!
-    // Similarly, the 'workspace' CBB must be destroyed before 
-    // all user defined CBB otherwise any instance 
-    // of a user CBB that is in the 'workspace' would try to 
-    // access a user CBB descriptor which has been previously freed
-    if (mRootCBB)
-    {
-      mRootPackage->UnRegisterBlackBox(mRootCBB->GetTypeName());
-      mRootCBB->UnReference();
-    }
-    if (mRootPackage)
-    {
-       GetFactory()->UnLoadPackage("user");
-    }
+    mOpenDefinition.clear();
+    mOpenPackage.clear();
     GetFactory()->Reset();
 
     // Create user package
-    mRootPackage = new Package("user","internal to bbi",
-                           "User defined black boxes",
-                           "",
-                           BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
-    // Create user workspace
-    mRootCBB = new ComplexBlackBoxDescriptor("workspace"); //,f);
-    mRootCBB->Reference();
-    mRootCBB->SetFactory(GetFactory());
-    mRootCBB->AddToAuthor("bbi (internal)");
-    mRootCBB->AddToDescription("User's workspace");
-    mOpenDefinition.push_back(CBBDefinition(mRootCBB,"user"));
-    // Register it into the user package
-    mRootPackage->RegisterBlackBox(mRootCBB);
+    Package::Pointer p =
+      Package::New("user","internal to bbi",
+                  "User defined black boxes",
+                  "",
+                  BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
     // Insert the user package in the factory
-    GetFactory()->InsertPackage(mRootPackage);
+    GetFactory()->InsertPackage(p);
     // And in the list of open packages
-    mOpenPackage.push_back(mRootPackage);
+    mOpenPackage.push_back(p);
+    mRootPackage = p;
 
+    // Create user workspace
+    ComplexBlackBoxDescriptor::Pointer r = 
+      ComplexBlackBoxDescriptor::New("workspace"); 
+    //    mRootCBB->Reference();
+    r->SetFactory(GetFactory());
+    r->AddToAuthor("bbi (internal)");
+    r->AddToDescription("User's workspace");
+    mOpenDefinition.push_back(CBBDefinition(r,"user"));
+    // Register it into the user package
+    p->RegisterBlackBox(r);
+    mRootCBB = r;
 
-    GetFactory()->CheckPackages();
+    //    Object::PrintObjectListInfo();
+    //  GetFactory()->CheckPackages();
 
     bbtkDebugDecTab("Kernel",9);
   }
@@ -142,7 +140,7 @@ namespace bbtk
   /// changes the workspace name
   void Executer::SetWorkspaceName( const std::string& n )
   {
-    mRootPackage->ChangeBlackBoxName( mRootCBB->GetTypeName(), n );
+    GetUserPackage()->ChangeBlackBoxName( GetWorkspace()->GetTypeName(), n );
   }
   //=======================================================================
 
@@ -151,19 +149,19 @@ namespace bbtk
   {
      bbtkDebugMessageInc("Kernel",9,"Executer::BeginPackage(\""<<name<<"\")"
                         <<std::endl);
-     Package* p;
+     Package::Pointer p;
      try 
       {
          p = GetFactory()->GetPackage(name);
       }
     catch (Exception e)
       {
-         p = new Package(name,
+       p = Package::New(name,
                          "",
                          "",
                          "",
                          BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
-         GetFactory()->InsertPackage(p);
+       GetFactory()->InsertPackage(p);
       }
      mOpenPackage.push_back(p);
   }
@@ -185,7 +183,8 @@ namespace bbtk
                         ","<<pack<<"\")"
                         <<std::endl);
 
-    ComplexBlackBoxDescriptor* b = new ComplexBlackBoxDescriptor(name);
+    ComplexBlackBoxDescriptor::Pointer b 
+      = ComplexBlackBoxDescriptor::New(name);
     b->SetFactory(GetFactory());
     b->SetScriptFileName(scriptfilename);
     mOpenDefinition.push_back( CBBDefinition( b, pack ) );
@@ -210,7 +209,7 @@ namespace bbtk
                         <<Current()->GetTypeName()<<"\")" 
                         <<std::endl);
     // Does current package exist ?
-    Package* p;
+    Package::Pointer p;
     std::string pname(mOpenDefinition.back().package);
     if (pname.size()>0)
       {
@@ -220,17 +219,17 @@ namespace bbtk
          }
        catch (Exception e)
          {
-           p = new Package(pname,
-                           "",
-                           "",
-                           "",
-                           BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
+           p = Package::New(pname,
+                            "",
+                            "",
+                            "",
+                            BBTK_STRINGIFY_SYMBOL(BBTK_VERSION));
            GetFactory()->InsertPackage(p);
          }
       }
     else
       {
-       p = mOpenPackage.back();
+       p = mOpenPackage.back().lock();
       }
     p->RegisterBlackBox(Current());
     
@@ -281,7 +280,7 @@ namespace bbtk
   //=======================================================================
   void Executer::Destroy(const std::string &boxName)
   {
-    bbtkError("Executer::Destroy : NOT IMPLEMENTED !");
+    Current()->Remove(boxName,true);
   }
   //=======================================================================
 
@@ -299,7 +298,7 @@ namespace bbtk
   void Executer::Execute (const std::string &nodeName) 
   {
     // if in root
-     if (Current()==mRootCBB
+    if (Current()==GetWorkspace()
      {
         if (!mNoExecMode) 
         {
@@ -320,7 +319,7 @@ namespace bbtk
                                const std::string& help)
   {
     // If the input is defined in the Root box
-    if (Current()==mRootCBB
+    if (Current()==GetWorkspace()
       {
       // If the dialog mode is set to NoDialog
       // and the user passed the name in the Inputs map 
@@ -382,12 +381,12 @@ namespace bbtk
                       const std::string &input,
                       const std::string &value)
   {
-    BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
+    BlackBox::Pointer b = Current()->GetPrototype()->bbGetBlackBox(box);
     // Looks for the adaptor
 
     if ( b->bbGetInputType(input) !=  typeid(std::string) ) 
       {
-         BlackBox* a =
+       BlackBox::Pointer a =
           GetFactory()->NewAdaptor(typeid(std::string),
                                    b->bbGetInputType(input),
                                    "tmp");
@@ -401,7 +400,7 @@ namespace bbtk
          a->bbSetInput("In",v);
          a->bbExecute();
          b->bbSetInput(input,a->bbGetOutput("Out"));
-         a->bbDelete();
+        //         a->Delete();
       }
     else 
       {
@@ -415,11 +414,11 @@ namespace bbtk
   std::string Executer::Get(const std::string &box,
                             const std::string &output)
   {
-    BlackBox* b = Current()->GetPrototype()->bbGetBlackBox(box);
+    BlackBox::Pointer b = Current()->GetPrototype()->bbGetBlackBox(box);
     // Looks for the adaptor
     if (b->bbGetOutputType(output) != typeid(std::string)) 
       {
-       BlackBox* a =
+       BlackBox::Pointer a =
           GetFactory()->NewAdaptor(
                                   b->bbGetOutputType(output),
                                   typeid(std::string),
@@ -440,7 +439,7 @@ namespace bbtk
        //             << std::endl;
        //std::string v(value);
        //b->bbSetInput(input,a->bbGetOutput("Out"));
-        a->bbDelete();
+       //        a->bbDelete();
         return r;
       }
     else
@@ -458,21 +457,21 @@ namespace bbtk
   //=======================================================================
   void Executer::Author(const std::string &authorName)
   {
-    Current()->AddToAuthor(authorName,Current()==mRootCBB);
+    Current()->AddToAuthor(authorName,Current()==GetWorkspace());
   }
   //=======================================================================
 
   //=======================================================================
   void Executer::Category(const std::string &category)
   {
-    Current()->AddToCategory(category,Current()==mRootCBB);
+    Current()->AddToCategory(category,Current()==GetWorkspace());
   }
   //=======================================================================
 
   //=======================================================================
   void Executer::Description(const std::string &d)
   {
-    Current()->AddToDescription(d,Current()==mRootCBB);
+    Current()->AddToDescription(d,Current()==GetWorkspace());
   }
   //=======================================================================
 
@@ -526,14 +525,14 @@ namespace bbtk
        relative_link = false;
       }
 
-    Package* p;
+    Package::Pointer p;
     try
     {
        p = GetFactory()->GetPackage(nameblackbox);
     }
     catch (Exception e)
     {
-       p = mRootPackage;
+      p = GetUserPackage();
     }
     // Generating documentation-help of workspace
     p->SetDocURL(filename_rootHtml);
@@ -563,10 +562,10 @@ namespace bbtk
                                            bool system_display)
   {
 
-    BlackBox* blackbox=NULL;
+    BlackBox::Pointer blackbox;
     if (nameblackbox==".")
     {
-       blackbox=Current()->GetPrototype();
+       blackbox = Current()->GetPrototype();
     }
     else
     {
@@ -638,7 +637,7 @@ namespace bbtk
     
     int detail = atoi(detailStr.c_str());
     int level  = atoi(levelStr.c_str());
-    BlackBox* blackbox=NULL;
+    BlackBox::Pointer blackbox;
     if (nameblackbox.compare(".")==0)
       {
        blackbox=Current()->GetPrototype();
@@ -682,8 +681,8 @@ namespace bbtk
   ///
   void Executer::Print(const std::string &str)
   {  
-    if (GetNoExecMode() &&  (Current()==mRootCBB) ) return;
-    if (Current()!=mRootCBB) return;
+    if (GetNoExecMode() &&  (Current()==GetWorkspace()) ) return;
+    if (Current()!=GetWorkspace()) return;
 
     bbtkDebugMessageInc("Interpreter",9,"Interpreter::Print(\""<<str<<"\")"<<std::endl);
 
@@ -748,5 +747,44 @@ namespace bbtk
       }
     bbtkMessage("Output",1,std::endl);
   }
+  //==========================================================================
 
+  //==========================================================================
+  std::string Executer::GetObjectName() const
+  {
+    return std::string("Executer");
+  }
+  //==========================================================================
+  
+  //==========================================================================
+  std::string  Executer::GetObjectInfo() const 
+  {
+    std::stringstream i;
+    return i.str();
+  }
+  //==========================================================================
+  //==========================================================================
+size_t  Executer::GetObjectSize() const 
+{
+  size_t s = Superclass::GetObjectSize();
+  s += Executer::GetObjectInternalSize();
+  return s;
+  }
+  //==========================================================================
+  //==========================================================================
+size_t  Executer::GetObjectInternalSize() const 
+{
+  size_t s = sizeof(Executer);
+  return s;
+  }
+  //==========================================================================
+  //==========================================================================
+  size_t  Executer::GetObjectRecursiveSize() const 
+  {
+    size_t s = Superclass::GetObjectRecursiveSize();
+    s += Executer::GetObjectInternalSize();
+    s += mFactory->GetObjectRecursiveSize();
+    return s;
+  }
+  //==========================================================================
 }//namespace