X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=kernel%2Fsrc%2FbbtkPackage.cxx;h=2a1fc8d3772fdf05e8f6aa4c535c05eb6a6e9ddb;hb=2a9b694dca646ad9562f47a5bdb4f209059bd22a;hp=4acb0f16192f8351b1219b8f73e0e2abfba64834;hpb=a26195c366a89795288009cf7e20f11afa494970;p=bbtk.git diff --git a/kernel/src/bbtkPackage.cxx b/kernel/src/bbtkPackage.cxx index 4acb0f1..2a1fc8d 100644 --- a/kernel/src/bbtkPackage.cxx +++ b/kernel/src/bbtkPackage.cxx @@ -1,25 +1,40 @@ -/*========================================================================= - + +/*========================================================================= Program: bbtk Module: $RCSfile: bbtkPackage.cxx,v $ Language: C++ - Date: $Date: 2008/01/22 15:02:00 $ - Version: $Revision: 1.1 $ - - 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 @@ -28,24 +43,48 @@ namespace bbtk { + + + + //========================================================================== + /// Creates a new package + Package::Pointer Package::New(const std::string& name, + const std::string& author, + const std::string& description, + const std::string& version) + { + bbtkDebugMessage("object",1,"##> Package::New('"< Package('"< ~Package(\""<GetName(); + + bbtkDebugMessage("package",5,"--- Releasing descriptor '" + <GetBlackBoxMap().find(descname); + if (desc == pack.lock()->GetBlackBoxMap().end()) + { + bbtkDebugMessage("package",5, + " Descriptor has already been released" + <second; + desc->second.reset(); + // if it is dead : remove it + if (pdesc.expired()) + { + bbtkDebugMessage("package",2," ==> '"<GetBlackBoxMap().find(descname); + if (desc != pack.lock()->GetBlackBoxMap().end()) pack.lock()->GetBlackBoxMap().erase(desc); + } else { //pdesc.expired + bbtkDebugMessage("package",5," ... Descriptor still alive (" + <GetBlackBoxMap()[descname] = pdesc.lock(); + } // pdesc.expired + } + //========================================================================== + + + //========================================================================== + /// Release + void Package::Release(Package::WeakPointer pack) + { + std::string packname = pack.lock()->mName; + bbtkDebugMessage("package",1,"==> Package::Release('"<< + packname<<"')"<mDynamicLibraryHandler ? 1:0; + long ndesc = pack.lock()->GetBlackBoxMap().size(); + long nrefs = pack.use_count(); + + bbtkDebugMessage("package",5," "<second; - } - // Adaptors are also stored in the black box map : hence already deleted + bbtkDebugMessage("package",5, + " -> No more external ref : checking descriptors" + < descnamelist; + BlackBoxMapType::iterator i; + for (i=pack.lock()->mBlackBoxMap.begin(); + i!= pack.lock()->mBlackBoxMap.end(); + ++i) + descnamelist.push_back(i->first); + + // Iterator over the initial names + std::vector::iterator descname; + for (descname=descnamelist.begin(); + descname!=descnamelist.end(); + ++descname) + { + // Is package still alive ? + if (pack.expired()) + { + bbtkDebugMessage("package",1,"--- Package::Release('"<< + packname + <<"') : package expired during release : bailing out"<mBlackBoxMap[*descname]; + if ( (dyn==0) || (boost::dynamic_pointer_cast(desc)) ) + PackageReleaseBlackBoxDescriptorInternal(pack,*descname); +#else + PackageReleaseBlackBoxDescriptorInternal(pack,*descname); +#endif + + } + + // + UnLoadDynamicLibrary(pack); + // Unload orphan dl packages + Package::UnLoadReleasedDynamicallyLoadedPackages(); + + + + + } + +#ifdef BBTK_COMPILE_DEBUG_MESSAGES + + bbtkDebugMessage("package",2,"<== Package::Release('"<< + packname<<"')"<mDynamicLibraryHandler ? 1:0; + long ndesc = pack.lock()->GetBlackBoxMap().size(); + long nrefs = pack.use_count(); + + bbtkDebugMessage("package",1," ... Package still alive (" + <mName; + std::string dname = descr.lock()->GetTypeName(); + bbtkDebugMessage("package",3,"==> Package::ReleaseBlackBoxDescriptor('"<< + packname<<"','"< Package::OpenDynamicLibrary(" + <second; + bbtkMessage("package",2, + "Could not open shared library [" < Package::CreateFromDynamicLibrary(" + <mDynamicLibraryHandler = h; + p->mDLDeletePackageFunction = df; + + std::string separator = + ConfigurationFile::GetInstance().Get_file_separator (); + //BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH) + std::string docreldoc = + separator + "bbdoc" + separator + pkgname + separator + "index.html"; + std::string reldoc = + ".." + separator + ".." + docreldoc; + std::string doc = path + separator + ".." + separator + + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH) + + docreldoc; + + p->SetDocURL(doc); + p->SetDocRelativeURL(reldoc); + + bbtkDebugMessage("package",2,"<== Package::CreateFromDynamicLibrary(" + <mDynamicLibraryHandler)) + return; + + + std::string packname = pack.lock()->GetName(); + bbtkDebugMessage("package",5,"==> Package::UnLoadDynamicLibrary('" + <GetBlackBoxMap().empty()) + { + + bbtkDebugMessage("package",5," Package not empty ... abort" + < dynamic library for package '" + < package '"< Package::UnLoadReleasedDynamicallyLoadedPackages()"<::iterator i; + +//JCP- 21-04-09 + if(mReleasedDynamicallyLoadedPackages.size()>0){ + for (i=mReleasedDynamicallyLoadedPackages.begin(); + i!=mReleasedDynamicallyLoadedPackages.end(); + ++i) + { + if (!i->expired()) UnLoad(*i); + } } - */ - bbtkDebugDecTab("Core",7); +//JCP- 21-04-09 + bbtkDebugMessage("package",5,"<== Package::UnLoadReleasedDynamicallyLoadedPackages()"<GetName(); + bbtkDebugMessage("package",6,"==> Package::UnLoad("<mDynamicLibraryHandler; + + // deletes the package + p->mDLDeletePackageFunction(); + + // closes the dl handler +#if defined(__GNUC__) + if (dlclose(h)!=0) + { + printf("EED Package::UnLoad ERROR %s\n", packname.c_str() ); + bbtkWarning("Failed to close dynamic library for package '"< dynamic library for package '" + < with name - BlackBox* Package::NewBlackBox(const std::string& type, - const std::string& name) const + BlackBox::Pointer Package::NewBlackBox(const std::string& type, + const std::string& name) const { - bbtkDebugMessageInc("Core",8,"Package<"<::NewBlackBox(\""<::NewBlackBox(\""<second->CreateInstance(name); - bbtkDebugDecTab("Core",8); + { + bbtkDebugDecTab("Kernel",8); + return BlackBox::Pointer(); + } + BlackBox::Pointer bb =i->second->NewBlackBox(name); + bbtkDebugDecTab("Kernel",8); return bb; } @@ -120,137 +707,249 @@ namespace bbtk //========================================================================== /// Creates an instance of an adaptor of input type and /// output type with name - BlackBox* Package::NewAdaptor(TypeInfo typein, - TypeInfo typeout, - const std::string& name) const + BlackBox::Pointer Package::NewAdaptor(const DataInfo& typein, + const DataInfo& typeout, + const std::string& name) const { - bbtkDebugMessageInc("Core",8,"Package<"<::NewAdaptor(<" - <,<" - <,\"" + bbtkDebugMessageInc("Kernel",8,"Package<"<::NewAdaptor(" + <second->CreateInstance(name); - bbtkDebugDecTab("Core",8); + BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name); + bbtkDebugDecTab("Kernel",8); return bb; } //========================================================================== + //========================================================================== + /// Creates an instance of an adaptor of input type and + /// output type with name + BlackBox::Pointer Package::NewWidgetAdaptor(const DataInfo& typein, + const DataInfo& typeout, + const std::string& name) const + { + bbtkDebugMessageInc("Kernel",8,"Package<"<::NewWidgetAdaptor(" + <second.lock()->NewBlackBox(name); + bbtkDebugDecTab("Kernel",8); + return bb; + + } + //========================================================================== + + + + //========================================================================== + /// Returns true is the package contains + /// an adaptor of input type and + /// output type + /// If successfull then adaptor contains the black box type name + bool Package::FindWidgetAdaptor(const DataInfo& typein, + const DataInfo& typeout, + std::string& adaptor) const + { + bbtkDebugMessageInc("Kernel",8,"Package<"<::FindWidgetAdaptor(" + <second.lock()->GetTypeName(); + bbtkDebugDecTab("Kernel",8); + return true; + + } + //========================================================================== + + //========================================================================== + /// Returns true is the package contains + /// an adaptor of input type and + /// output type + /// If successfull then adaptor contains the black box type name + bool Package::FindAdaptor(const DataInfo& typein, + const DataInfo& typeout, + std::string& adaptor) const + { + bbtkDebugMessageInc("Kernel",8,"Package<"<::FindAdaptor(" + <second.lock()->GetTypeName(); + bbtkDebugDecTab("Kernel",8); + return true; + + } + //========================================================================== + //========================================================================== /// Registers a black box descriptor in the package - bool Package::RegisterBlackBox(BlackBoxDescriptor* d) + bool Package::RegisterBlackBox(BlackBoxDescriptor::Pointer d) { - bbtkDebugMessageInc("Core",8,"Package<"<::RegisterBlackBox(\""<GetTypeName()<<"\")"<::RegisterBlackBox(\""<GetTypeName()<<"\")"<GetTypeName()); + if (i!=mBlackBoxMap.end()) + { + bbtkWarning("Package<"< : Trying to register box type <" + <GetTypeName()<<"> which is already in the package"); + return false; + } + mBlackBoxMap[d->GetTypeName()] = d; - d->SetPackage(this); + // d->Reference(); + d->SetPackage(GetThisPointer()); // If it is a default adaptor, also register it in the adaptors map - if ( d->GetCategory() == BlackBoxDescriptor::DEFAULT_ADAPTOR) + if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR ) { + bbtkDebugMessage("Kernel",8,"Package<"<::RegisterBlackBox(\""<GetTypeName()<<"\") : The box is an adaptor, inserting it in adaptors map ..."<GetInputDescriptor("In")->GetTypeInfo(); TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo(); - AdaptorKey key(typein,typeout); - AdaptorMapType::const_iterator i = mAdaptorMap.find(key); + DataInfo infoin(typein,d->GetInputDescriptor("In")->GetNature()); + DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature()); + AdaptorKey key(infoin,infoout,d->GetKind()); + + AdaptorMapType::const_iterator i; + i = mAdaptorMap.find(key); if (i == mAdaptorMap.end()) { - bbtkDebugMessage("Core",8,"The box is an adaptor, inserting it in adaptors map ..."< : trying to register black box <" - <GetTypeName() - <<"> as default adaptor but there is already a default adaptor registered (<" - <second->GetTypeName()<<">)"); + if (i->second.lock()->GetTypeName() != d->GetTypeName()) + { + bbtkError("Package <"< : trying to register black box <" + <GetTypeName() + <<"> as default adaptor but there is already a default adaptor registered (<" + <second.lock()->GetTypeName()<<">)"); + } } + } + // If it is a default adaptor, also register it in the adaptors map + else if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_GUI) + { + bbtkDebugMessage("Kernel",8,"Package<"<::RegisterBlackBox(\""<GetTypeName()<<"\") : The box is a widget adaptor, inserting it in adaptors map ..."<GetOutputDescriptor("Out")->GetTypeInfo(); + DataInfo infoin(typeid(void),""); + DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature()); + AdaptorKey key(infoin,infoout,d->GetKind()); + AdaptorMapType::const_iterator i; + i = mAdaptorMap.find(key); + if (i == mAdaptorMap.end()) + { + mAdaptorMap[key] = d; + } + // If already an adaptor registered : error + else + { + if (i->second.lock()->GetTypeName() != d->GetTypeName()) + { + bbtkError("Package <"< : trying to register black box <" + <GetTypeName() + <<"> as default widget adaptor but there is already a default adaptor registered (<" + <second.lock()->GetTypeName()<<">)"); + } + } } - bbtkDebugDecTab("Core",8); + + bbtkDebugDecTab("Kernel",8); return true; } //========================================================================== - - //========================================================================== - /// UnRegisters a black box descriptor from the package - void Package::UnRegisterBlackBox(const std::string& name) + //=================================================================== + void Package::CheckBoxes() const { - bbtkDebugMessageInc("Core",8,"Package<"<::UnRegisterBlackBox(\""< does not contains the black box <"<"); - } - mBlackBoxMap.erase(i); - // Is it also in the adaptors map ? - /* - AdaptorMapType::iterator j = mAdaptorMap.find(name); - if (j != mAdaptorMap.end()) + bbtkMessage("debug",1,"****** Checking Package "<<(void*)this + <<" ["<second->Check(true); } - */ - bbtkDebugDecTab("Core",8); + bbtkMessage("debug",1,"****** Checking Package "<<(void*)this + <<" ["<::ChangeBlackBoxName(\""<::ChangeBlackBoxName(\""< does not contains the black box <"<"); + bbtkDebugDecTab("Kernel",8); + bbtkError("ChangeBlackBoxName : The package <"< does not contains the black box <"<"); } i->second->SetTypeName(newname); mBlackBoxMap[newname] = i->second; mBlackBoxMap.erase(i); - bbtkDebugDecTab("Core",8); + bbtkDebugDecTab("Kernel",8); } //========================================================================== - /* - - //========================================================================== - /// Registers an adaptor descriptor in the package - bool Package::RegisterAdaptor(BlackBoxDescriptor* d) - { - bbtkDebugMessage("Core",8,"Package<"<::RegisterAdaptor(\""<GetTypeName()<<"\")"<GetInputDescriptor("In")->GetTypeInfo(); - TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo(); - AdaptorKey key(typein,typeout); - - mAdaptorMap[key] = d; - return true; - } - //========================================================================== - */ //========================================================================== @@ -259,7 +958,7 @@ namespace bbtk { unsigned int lmax = 0; std::vector names; - std::vector categs; + std::vector kinds; std::vector descrs; BlackBoxMapType::const_iterator i; @@ -268,25 +967,25 @@ namespace bbtk ++i) { if ( adaptors || - ( i->second->GetCategory() == BlackBoxDescriptor::STANDARD) ) + ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) ) { std::string name(" "); name += i->second->GetTypeName(); names.push_back(name); - std::string categ; - if ( i->second->GetCategory() == BlackBoxDescriptor::ADAPTOR ) + std::string kind; + if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR ) { - categ = std::string("[A]"); + kind = std::string("[A]"); } - else if ( i->second->GetCategory() == + else if ( i->second->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR ) { - categ = std::string("[DA]"); + kind = std::string("[DA]"); } - categs.push_back(categ); + kinds.push_back(kind); - unsigned int l = name.size()+categ.size(); + unsigned int l = name.size()+kind.size(); if (l>lmax) lmax = l; std::string descr; @@ -303,7 +1002,7 @@ namespace bbtk std::string offs; offs.append(lmax+3,' '); std::vector::iterator ni,ci,di; - for (ni = names.begin(), ci = categs.begin(), di = descrs.begin(); + for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin(); ni != names.end(); ++ni, ++ci, ++di) { std::string space; @@ -318,7 +1017,7 @@ namespace bbtk else bbtkMessage("Help",1,d << std::endl); // d = d.substr(dmax,d.size()); - // } + // } } } @@ -333,11 +1032,11 @@ namespace bbtk i!=mBlackBoxMap.end(); ++i) { - if ( i->second->GetCategory() != BlackBoxDescriptor::STANDARD ) + if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD ) { bbtkMessage("Help",1, " "<second->GetTypeName()); - if ( i->second->GetCategory() == + if ( i->second->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR ) { bbtkMessage("Help",1, @@ -347,7 +1046,7 @@ namespace bbtk { bbtkMessage("Help",1, " : "<second->GetDescription()); - + } bbtkMessage("Help",1,std::endl); } @@ -364,7 +1063,7 @@ namespace bbtk { bbtkMessage("Help",1, " : "<second->GetDescription()); - + } bbtkMessage("Help",1,std::endl); } @@ -376,18 +1075,18 @@ namespace bbtk /// Prints help on a black box void Package::HelpBlackBox(const std::string& name, bool full) const { - bbtkDebugMessageInc("Core",8,"Package<"<::HelpBlackBox(\"" + bbtkDebugMessageInc("Kernel",8,"Package<"<::HelpBlackBox(\"" < does not contains the black box <"<"); } // bbtkMessage("Help",1,"["<second->GetHelp(full); - bbtkDebugDecTab("Core",8); + bbtkDebugDecTab("Kernel",8); } //========================================================================== @@ -397,16 +1096,16 @@ namespace bbtk /// Returns true iff the package contains the box of name boxname bool Package::ContainsBlackBox(const std::string& name) const { - bbtkDebugMessageInc("Core",8,"Package<"<::HelpBlackBox(\"" + bbtkDebugMessageInc("Kernel",8,"Package<"<::HelpBlackBox(\"" <::CreateHtmlPage(\"" + bbtkDebugMessageInc("Kernel",9,"Package<"<::CreateHtmlPage(\"" <\n"; s << " Author(s) : " << GetAuthor() << "\n"; - s << " Version : " + s << " Author(s) : " + << GetCategory() << "\n"; + s << " Version : " << GetVersion() << "\n"; s << " bbtk Version : " - << GetBBTKVersion() << "\n"; + << bbtk::GetVersion() << "\n"; s << "\n"; } @@ -527,16 +1228,17 @@ namespace bbtk BlackBoxMapType::const_iterator i; for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i) { - if ( i->second->GetCategory() != BlackBoxDescriptor::STANDARD) + if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD) continue; std::string name = i->second->GetTypeName(); Utilities::html_format(name); std::string descr = i->second->GetDescription(); - + //Utilities::html_format(descr); + s << ""; s << ""; - s << "
  • " <"; s << " "; @@ -546,7 +1248,7 @@ namespace bbtk s << "\n"; - s << "
  • \n"; + s << "\n"; s << "\n"; //------------------- @@ -556,21 +1258,21 @@ namespace bbtk // s << "
    \n"; s << "

    Adaptors : \n"; s << "

      \n"; - + // BlackBoxMapType::const_iterator i; s << "

      \n"; for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end();++i) { - if ( i->second->GetCategory() == BlackBoxDescriptor::STANDARD) + if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) continue; - + std::string name = i->second->GetTypeName(); Utilities::html_format(name); std::string descr = i->second->GetDescription(); - + s << ""; s << " "; @@ -578,8 +1280,8 @@ namespace bbtk s << "\n"; } s << "
      "; - s << "
    • " <"; s << "
    • \n"; - - s << "

    \n"; + + s << "\n"; s << "
    \n"; } @@ -601,11 +1303,13 @@ namespace bbtk // Computes output directory from filename to pass it to // BlackBoxDescriptor::InsertHtmlHelp std::string dir; - std::string::size_type slash_position = - filename.find_last_of(ConfigurationFile::GetInstance().Get_file_separator ()); - if (slash_position != std::string::npos) { + + std::string::size_type slash_position = filename.find_last_of("/\\"); + + + if (slash_position != std::string::npos) { if (slash_position == 0) - slash_position = 1; + slash_position = 1; dir = filename.substr(0,slash_position); } @@ -624,8 +1328,9 @@ namespace bbtk ptm = gmtime ( &rawtime ); s << "


    \n"; - s << "Automatically generated by "< from " - < on " + s << "Automatically generated by "< "//from " + // < + <<"on " << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n"; s << "\n"; @@ -633,8 +1338,64 @@ namespace bbtk //---------------------- // End - bbtkDebugDecTab("Core",9); + bbtkDebugDecTab("Kernel",9); + } + //========================================================================== + + //========================================================================== + std::string Package::GetObjectName() const + { + return std::string("Package '")+mName+std::string("'"); + } + //========================================================================== + + //========================================================================== + std::string Package::GetObjectInfo() const + { + std::stringstream i; + i << " - "<second->GetObjectRecursiveSize(); + } + return s; } //========================================================================== + + //========================================================================== + std::set + Package::mReleasedDynamicallyLoadedPackages; + //========================================================================== }