X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=kernel%2Fsrc%2FbbtkPackage.cxx;h=921682c743a9922525d81940f17aab36241d43e9;hb=ab0cfcfdd031ff179ed1550874c648a54d8e1023;hp=3518a01cbebe48238dd81919c244777d549e5ca4;hpb=743fbf0471560aaff9644796ce1c4651407e0e9c;p=bbtk.git diff --git a/kernel/src/bbtkPackage.cxx b/kernel/src/bbtkPackage.cxx index 3518a01..921682c 100644 --- a/kernel/src/bbtkPackage.cxx +++ b/kernel/src/bbtkPackage.cxx @@ -1,20 +1,34 @@ -/*========================================================================= - + +/*========================================================================= Program: bbtk Module: $RCSfile: bbtkPackage.cxx,v $ Language: C++ - Date: $Date: 2008/02/06 10:51:28 $ - Version: $Revision: 1.6 $ - - 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/04/21 14:36:51 $ + Version: $Revision: 1.25 $ =========================================================================*/ + +/* --------------------------------------------------------------------- + +* 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. @@ -28,19 +42,43 @@ 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('"<second; - } - // Adaptors are also stored in the black box map : hence already deleted + bbtkDebugMessage("object",2,"==> 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 + { + bbtkDebugMessage("package",5," ... Descriptor still alive (" + <GetBlackBoxMap()[descname] = pdesc.lock(); + } + } + //========================================================================== + + + //========================================================================== + /// 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," "< 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"<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(" + < 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); + } + } +//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__) + dlclose(h); +#elif defined(_WIN32) + FreeLibrary(h); +#endif + + bbtkDebugMessage("package",1,"==> 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("Kernel",8,"Package<"<::NewBlackBox(\""<second->CreateInstance(name); + BlackBox::Pointer bb =i->second->NewBlackBox(name); bbtkDebugDecTab("Kernel",8); return bb; @@ -120,24 +691,53 @@ 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("Kernel",8,"Package<"<::NewAdaptor(<" - <,<" - <,\"" + ">::NewAdaptor(" + <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->CreateInstance(name); + BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name); bbtkDebugDecTab("Kernel",8); return bb; @@ -146,70 +746,172 @@ namespace bbtk + //========================================================================== + /// 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("Kernel",8,"Package<"<::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->GetKind() == 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()) + { + 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 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()) { - bbtkDebugMessage("Kernel",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 widget adaptor but there is already a default adaptor registered (<" + <second.lock()->GetTypeName()<<">)"); + } } } + 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("Kernel",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("Kernel",8); + bbtkMessage("debug",1,"****** Checking Package "<<(void*)this + <<" ["<::RegisterAdaptor(\""<GetTypeName()<<"\")"<GetInputDescriptor("In")->GetTypeInfo(); - TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo(); - AdaptorKey key(typein,typeout); - - mAdaptorMap[key] = d; - return true; - } - //========================================================================== - */ //========================================================================== @@ -511,7 +1196,7 @@ namespace bbtk s << " Version : " << GetVersion() << "\n"; s << " bbtk Version : " - << GetBBTKVersion() << "\n"; + << bbtk::GetVersion() << "\n"; s << "\n"; } @@ -533,6 +1218,7 @@ namespace bbtk std::string name = i->second->GetTypeName(); Utilities::html_format(name); std::string descr = i->second->GetDescription(); + //Utilities::html_format(descr); s << ""; s << ""; @@ -601,9 +1287,11 @@ 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; dir = filename.substr(0,slash_position); @@ -624,8 +1312,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"; @@ -636,5 +1325,61 @@ namespace bbtk 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; + //========================================================================== }