]> Creatis software - bbtk.git/blobdiff - kernel/src/bbtkPackage.cxx
bbtkPackage.cxx
[bbtk.git] / kernel / src / bbtkPackage.cxx
index 29bd891d79008fd75b062b1c9800d51f3f03d622..2a1fc8d3772fdf05e8f6aa4c535c05eb6a6e9ddb 100644 (file)
@@ -1,25 +1,40 @@
-/*=========================================================================
-                                                                                
+
+/*=========================================================================                                                                               
   Program:   bbtk
   Module:    $RCSfile: bbtkPackage.cxx,v $
   Language:  C++
-  Date:      $Date: 2008/04/22 06:59:31 $
-  Version:   $Revision: 1.14 $
-                                                                                
-  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
-  l'Image). All rights reserved. See doc/license.txt or
-  http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
-                                                                                
-     This software is distributed WITHOUT ANY WARRANTY; without even
-     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-     PURPOSE.  See the above copyright notices for more information.
-                                                                                
+  Date:      $Date: 2009/05/28 12:18:51 $
+  Version:   $Revision: 1.28 $
 =========================================================================*/
+
+/* ---------------------------------------------------------------------
+
+* Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
+* Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
+*
+*  This software is governed by the CeCILL-B license under French law and 
+*  abiding by the rules of distribution of free software. You can  use, 
+*  modify and/ or redistribute the software under the terms of the CeCILL-B 
+*  license as circulated by CEA, CNRS and INRIA at the following URL 
+*  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
+*  or in the file LICENSE.txt.
+*
+*  As a counterpart to the access to the source code and  rights to copy,
+*  modify and redistribute granted by the license, users are provided only
+*  with a limited warranty  and the software's author,  the holder of the
+*  economic rights,  and the successive licensors  have only  limited
+*  liability. 
+*
+*  The fact that you are presently reading this means that you have had
+*  knowledge of the CeCILL-B license and that you accept its terms.
+* ------------------------------------------------------------------------ */                                                                         
+
 /**
  *\file
  *\brief Class bbtk::Package : registers black boxes descriptors and is able to create instances of the black boxes registered.
  */
 #include "bbtkPackage.h"
+#include "bbtkComplexBlackBoxDescriptor.h"
 #include "bbtkMessageManager.h"
 #include "bbtkConfigurationFile.h"
 #include <fstream>
@@ -36,16 +51,14 @@ namespace bbtk
   Package::Pointer Package::New(const std::string& name,
                                const std::string& author,
                                const std::string& description,
-                               const std::string& version,
-                               const std::string& BBTKVersion) 
+                               const std::string& version) 
   {
     bbtkDebugMessage("object",1,"##> Package::New('"<<name<<"',...)"
                     <<bbtkendl);
     Package::Pointer p = MakePointer(new Package(name,
                                               author,
                                               description,
-                                              version,
-                                              BBTKVersion));
+                                              version));
     bbtkDebugMessage("object",2,"<## Package::New('"<<name<<"',...)"
                     <<bbtkendl);
     return p;
@@ -57,8 +70,7 @@ namespace bbtk
   Package::Package(const std::string& name,
                   const std::string& author,
                   const std::string& description,
-                  const std::string& version,
-                  const std::string& BBTKVersion) 
+                  const std::string& version) 
     :
     mDynamicLibraryHandler(0),
     mName(name),
@@ -66,7 +78,7 @@ namespace bbtk
     mDescription(description),
     mVersion(version)
   {
-    bbtkDebugMessage("object",2,"==> Package::Package('"<<name<<"',...)"
+    bbtkDebugMessage("object",2,"==> Package('"<<name<<"',...)"
                     <<bbtkendl);
     std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
     char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
@@ -100,57 +112,55 @@ namespace bbtk
   /// Dtor
   Package::~Package()
   {
-    bbtkDebugMessage("object",2,"==> Package::~Package(\""<<mName<<"\")"<<bbtkendl);
-    bbtkDebugMessage("object",2,"<== Package::~Package(\""<<mName<<"\")"<<bbtkendl);
+    bbtkDebugMessage("object",2,"==> ~Package(\""<<mName<<"\")"<<bbtkendl);
   }
   //==========================================================================
 
 
   //==========================================================================
-  void PackageReleaseBlackBoxDescriptorInternal(Package::WeakPointer pack,
-                                               const std::string& descname)
-  {
-    // Try to release descriptor
-    bbtkDebugMessage("package",3,"--- Trying to release descriptor '"
-                    <<descname<<"'"<<bbtkendl);
-    std::string packname = pack.lock()->GetName();
-
-    Package::BlackBoxMapType::iterator desc = 
-      pack.lock()->GetBlackBoxMap().find(descname);
-    if (desc ==  pack.lock()->GetBlackBoxMap().end())
-      {
-       bbtkDebugMessage("package",3,
-                        "    Descriptor has already been released"
-                        <<bbtkendl);
-       return;
-      }
-    //    bbtkDebugMessage("package",3,
-    //              "    Trying unreferencing it ... "<<std::endl);
-    BlackBoxDescriptor::WeakPointer pdesc = desc->second;
-    desc->second.reset();
-    // if it is dead : remove it 
-    if (pdesc.expired()) 
-      {
-       bbtkDebugMessage("package",2,"   '"<<packname<<"::"<<descname<<"' Descriptor expired"<<bbtkendl);
-       if (pack.expired()) 
+         void PackageReleaseBlackBoxDescriptorInternal(Package::WeakPointer pack,
+                                                       const std::string& descname)
          {
-           bbtkDebugMessage("package",2,
-                            "    ... and caused its package death"
-                            <<bbtkendl);
-           return;
+               // Try to release descriptor
+               std::string packname = pack.lock()->GetName();
+
+               bbtkDebugMessage("package",5,"--- Releasing descriptor '"
+                                <<packname<<"::"<<descname<<"'"<<bbtkendl);
+
+
+               Package::BlackBoxMapType::iterator desc = 
+                 pack.lock()->GetBlackBoxMap().find(descname);
+               if (desc ==  pack.lock()->GetBlackBoxMap().end())
+                 {
+               bbtkDebugMessage("package",5,
+                                "    Descriptor has already been released"
+                                <<bbtkendl);
+               return;
+                 }
+               //    bbtkDebugMessage("package",3,
+               //                   "    Trying unreferencing it ... "<<std::endl);
+               BlackBoxDescriptor::WeakPointer pdesc = desc->second;
+               desc->second.reset();
+               // if it is dead : remove it 
+               if (pdesc.expired()) 
+               {
+                 bbtkDebugMessage("package",2," ==> '"<<packname<<"::"<<descname<<"' Descriptor expired"<<bbtkendl);
+                 if (pack.expired()) 
+                 {
+                       bbtkDebugMessage("package",2,
+                                        "     ... and caused its package death"
+                                        <<bbtkendl);
+                       return;
+                 } // pack.expired
+                 desc = pack.lock()->GetBlackBoxMap().find(descname);
+                 if (desc !=  pack.lock()->GetBlackBoxMap().end())       pack.lock()->GetBlackBoxMap().erase(desc);
+               }   else    {  //pdesc.expired
+                    bbtkDebugMessage("package",5,"    ... Descriptor still alive ("
+                                <<pdesc.use_count()<<" refs)"
+                                <<bbtkendl);
+                    pack.lock()->GetBlackBoxMap()[descname] = pdesc.lock();
+               } // pdesc.expired
          }
-       desc = pack.lock()->GetBlackBoxMap().find(descname);
-       if (desc !=  pack.lock()->GetBlackBoxMap().end())
-         pack.lock()->GetBlackBoxMap().erase(desc);
-      }
-    else 
-      {
-       bbtkDebugMessage("package",3,"    ... Descriptor still alive ("
-                        <<pdesc.use_count()<<" refs)"
-                        <<bbtkendl);
-       pack.lock()->GetBlackBoxMap()[descname] = pdesc.lock();
-      }
-  }
   //==========================================================================
 
 
@@ -166,7 +176,7 @@ namespace bbtk
     long ndesc = pack.lock()->GetBlackBoxMap().size();
     long nrefs = pack.use_count();
 
-    bbtkDebugMessage("package",2," "<<nrefs<<" refs / "
+    bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
                     <<ndesc<<" descr / dyn="
                     <<dyn<<std::endl);
 
@@ -179,7 +189,7 @@ namespace bbtk
     //  and descallocated (reset) by PACKAGENAMEDeletePackage())
     if (nrefs == ndesc + dyn) 
       {
-       bbtkDebugMessage("package",2,
+       bbtkDebugMessage("package",5,
                         " -> No more external ref : checking descriptors"
                         <<bbtkendl);
        // We must take care that removing refs on descriptors 
@@ -212,14 +222,20 @@ namespace bbtk
            // Is package still alive ?
            if (pack.expired()) 
              {
-               bbtkDebugMessage("package",2,"--- Package::Release('"<<
+               bbtkDebugMessage("package",1,"--- Package::Release('"<<
                                 packname
                                 <<"') : package expired during release : bailing out"<<bbtkendl);
                break;
              }
            
-           PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
-           
+#if defined(MACOSX)
+                 BlackBoxDescriptor::Pointer desc = pack.lock()->mBlackBoxMap[*descname];
+                 if ( (dyn==0) || (boost::dynamic_pointer_cast<ComplexBlackBoxDescriptor>(desc)) )
+                         PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
+#else
+                 PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
+#endif
+
          }
 
        //
@@ -232,8 +248,11 @@ namespace bbtk
        
       }
     
-    bbtkDebugMessage("package",1,"<== Package::Release('"<<
+#ifdef BBTK_COMPILE_DEBUG_MESSAGES
+
+    bbtkDebugMessage("package",2,"<== Package::Release('"<<
                     packname<<"')"<<bbtkendl);
+
     if (!pack.expired())
       {
        long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0; 
@@ -247,9 +266,10 @@ namespace bbtk
       }
     else 
       {
-       bbtkDebugMessage("package",1,"   ... Package has been released"
+       bbtkDebugMessage("package",1," ... Package has been released"
                         <<std::endl);
       }
+#endif
   }
   //==========================================================================
 
@@ -265,13 +285,14 @@ namespace bbtk
     std::string packname = pack.lock()->mName;
     std::string dname = descr.lock()->GetTypeName();    
     bbtkDebugMessage("package",3,"==> Package::ReleaseBlackBoxDescriptor('"<<
-                    packname<<"','"<<dname<<"')"<<bbtkendl);
+                    packname<<"','"<<dname<<"') : refs="
+                    <<descr.use_count()<<bbtkendl);
 
     long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0; 
     long ndesc = pack.lock()->GetBlackBoxMap().size();
     long nrefs = pack.use_count();
 
-    bbtkDebugMessage("package",3," "<<nrefs<<" refs / "
+    bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
                     <<ndesc<<" descr / dynamically loaded = "
                     <<dyn<<std::endl);
 
@@ -284,15 +305,22 @@ namespace bbtk
     //  and descallocated (reset) by PACKAGENAMEDeletePackage())
     if (nrefs == ndesc + dyn) 
       {
-       PackageReleaseBlackBoxDescriptorInternal(pack,dname);
+#if defined(MACOSX)
+                 //
+                 if (dyn==0)
+                         PackageReleaseBlackBoxDescriptorInternal(pack,dname);
+#else
+                 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
+#endif 
       }
     
     // If the package is released and dynamically loaded 
     // then put it in the static list mReleasedDynamicallyLoadedPackages
     UnLoadDynamicLibrary(pack,false);
         
-    bbtkDebugMessage("package",3,"<== Package::ReleaseBlackBoxDescriptor('"<<
-                    packname<<"','"<<dname<<"')"<<bbtkendl);
+    bbtkDebugMessage("package",4,"<== Package::ReleaseBlackBoxDescriptor('"<<
+                    packname<<"','"<<dname<<"'): refs="
+                    <<descr.use_count()<<bbtkendl);
     /*
     if (!pack.expired())
       {
@@ -334,8 +362,9 @@ namespace bbtk
 
     // Open shared lib
     void *handler;
-    handler = dlopen(libname.c_str(),
-                    BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
+    handler = dlopen(libname.c_str(), BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
+//EED     handler = dlopen(libname.c_str(), RTLD_LAZY | RTLD_LOCAL );
+         
     if (!handler)
       {
        bbtkMessage("package",2,
@@ -422,6 +451,12 @@ namespace bbtk
        bbtkMessage("package",2,
                    "Could not open shared library [" <<libname<<"]"
                    << std::endl);
+       DWORD dwErrorCode = 0;
+       dwErrorCode = GetLastError();
+       bbtkMessage("package",2,
+               "Windows Error: [" << dwErrorCode <<"]"
+               << std::endl);
+        
        return 0;
       }
     
@@ -430,7 +465,7 @@ namespace bbtk
     getvername += 
       BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
     DLGetPackageBBTKVersionFunction getbbtkversion 
-      = (DLGetPackageBBTKVersionFunction)(GetProcAdress(handler,
+      = (DLGetPackageBBTKVersionFunction)(GetProcAddress(handler,
                                                        getvername.c_str()));
     if (!getbbtkversion)
       {
@@ -459,7 +494,7 @@ namespace bbtk
      // Loads the Package get function
     std::string getpackname(package_name);
     getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
-    getpack = (DLGetPackageFunction)(GetProcAdress(handler, getpackname.c_str()));
+    getpack = (DLGetPackageFunction)(GetProcAddress(handler, getpackname.c_str()));
     if (!getpack)
       {
        FreeLibrary(handler);
@@ -473,7 +508,7 @@ namespace bbtk
     // Loads the Package delete function
     std::string delpackname(package_name);
     delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
-    delpack = (DLDeletePackageFunction)(GetProcAdress(handler, delpackname.c_str()));
+    delpack = (DLDeletePackageFunction)(GetProcAddress(handler, delpackname.c_str()));
     if (!delpack)
       {
        FreeLibrary(handler);
@@ -525,7 +560,7 @@ namespace bbtk
     p->SetDocURL(doc);
     p->SetDocRelativeURL(reldoc);
     
-    bbtkDebugMessage("package",1,"<== Package::CreateFromDynamicLibrary("
+    bbtkDebugMessage("package",2,"<== Package::CreateFromDynamicLibrary("
                     <<libname<<") .. OK"<<std::endl);
     return p;
   }
@@ -543,14 +578,14 @@ namespace bbtk
     
     
     std::string packname = pack.lock()->GetName();
-    bbtkDebugMessage("package",3,"==> Package::UnLoadDynamicLibrary('"
+    bbtkDebugMessage("package",5,"==> Package::UnLoadDynamicLibrary('"
                     <<packname<<"')"
                     <<std::endl);
     
     if (!pack.lock()->GetBlackBoxMap().empty())
       {
        
-       bbtkDebugMessage("package",3,"   Package not empty ... abort"
+       bbtkDebugMessage("package",5,"   Package not empty ... abort"
                         <<std::endl);
        return;
        /*
@@ -571,19 +606,20 @@ namespace bbtk
     if (doit) 
       {
        UnLoad(pack);
-       bbtkDebugMessage("package",3,"   ... dynamic library closed"
+       bbtkDebugMessage("package",5,"==> dynamic library for package '"
+                        <<packname<<"' closed"
                         <<std::endl);    
       }
     else 
       {
        mReleasedDynamicallyLoadedPackages.insert(pack);
-       bbtkDebugMessage("package",3,
-                        "   ... package put in the 'to unload' list"
+       bbtkDebugMessage("package",1,"==> package '"<<packname
+                        <<"' put in the 'to unload' list"
                         <<std::endl);
 
       }
 
-    bbtkDebugMessage("package",3,"<== Package::UnLoadDynamicLibrary('"
+    bbtkDebugMessage("package",5,"<== Package::UnLoadDynamicLibrary('"
                     <<packname<<"')"
                     <<std::endl);
     
@@ -595,16 +631,21 @@ namespace bbtk
   /// see UnLoadDynamicLibrary and ReleaseBlackBoxDescriptor
   void Package::UnLoadReleasedDynamicallyLoadedPackages()
   {
-    bbtkDebugMessage("package",2,"==> Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
+    bbtkDebugMessage("package",5,"==> Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
 
     std::set<Package::WeakPointer>::iterator i;
-    for (i=mReleasedDynamicallyLoadedPackages.begin();
-        i!=mReleasedDynamicallyLoadedPackages.end();
-        ++i)
-      {
-       if (!i->expired()) UnLoad(*i);
-      }
-    bbtkDebugMessage("package",2,"<== Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
+
+//JCP- 21-04-09
+       if(mReleasedDynamicallyLoadedPackages.size()>0){
+               for (i=mReleasedDynamicallyLoadedPackages.begin();
+               i!=mReleasedDynamicallyLoadedPackages.end();
+               ++i)
+               {
+               if (!i->expired()) UnLoad(*i);
+               }
+       }
+//JCP- 21-04-09
+    bbtkDebugMessage("package",5,"<== Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
   }
   //==========================================================================
 
@@ -612,7 +653,7 @@ namespace bbtk
   void Package::UnLoad(Package::WeakPointer pack)
   {
     std::string packname = pack.lock()->GetName();
-    bbtkDebugMessage("package",2,"==> Package::UnLoad("<<packname<<")"<<std::endl);
+    bbtkDebugMessage("package",6,"==> Package::UnLoad("<<packname<<")"<<std::endl);
 
     Package* p = pack.lock().get();
     
@@ -623,12 +664,21 @@ namespace bbtk
     
     // closes the dl handler
 #if defined(__GNUC__)  
-    dlclose(h);  
+         if (dlclose(h)!=0)
+         {
+                 printf("EED Package::UnLoad ERROR %s\n", packname.c_str() );
+                 bbtkWarning("Failed to close dynamic library for package '"<<packname
+                                         <<"'"<<std::endl);
+         }  
+         
 #elif defined(_WIN32)
     FreeLibrary(h);
 #endif
 
-    bbtkDebugMessage("package",2,"   ... dynamic library unloaded"<<std::endl);
+    bbtkDebugMessage("package",1,"==> dynamic library for package '"
+                    <<packname<<"' closed"
+                    <<std::endl);    
+    bbtkDebugMessage("package",6,"   ... dynamic library unloaded"<<std::endl);
   }
   //==========================================================================
 
@@ -864,7 +914,7 @@ namespace bbtk
   //===================================================================
   void Package::CheckBoxes() const
   {
-    bbtkMessage("Debug",1,"****** Checking Package "<<(void*)this
+    bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
                <<" ["<<GetName()<<"]"<<std::endl);
     BlackBoxMapType::const_iterator i;
     for (i=mBlackBoxMap.begin();
@@ -873,7 +923,7 @@ namespace bbtk
       {
        i->second->Check(true);
       }
-    bbtkMessage("Debug",1,"****** Checking Package "<<(void*)this
+    bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
                <<" ["<<GetName()<<"] ... OK"<<std::endl);
   }
   //===================================================================
@@ -1184,7 +1234,7 @@ namespace bbtk
        std::string name = i->second->GetTypeName();
        Utilities::html_format(name);
        std::string descr = i->second->GetDescription();
-       Utilities::html_format(descr);
+       //Utilities::html_format(descr);
 
         s << "<TR>";
        s << "<TD style='vertical-align: top;'>";