2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
29 /*=========================================================================
31 Module: $RCSfile: bbtkPackage.cxx,v $
33 Date: $Date: 2012/11/16 08:49:01 $
34 Version: $Revision: 1.37 $
35 =========================================================================*/
41 *\brief Class bbtk::Package : registers black boxes descriptors and is able to create instances of the black boxes registered.
43 #include "bbtkPackage.h"
44 #include "bbtkComplexBlackBoxDescriptor.h"
45 #include "bbtkMessageManager.h"
46 #include "bbtkConfigurationFile.h"
49 #include "bbtkUtilities.h"
56 //==========================================================================
57 /// Creates a new package
58 Package::Pointer Package::New(const std::string& name,
59 const std::string& author,
60 const std::string& description,
61 const std::string& version)
63 bbtkDebugMessage("object",1,"##> Package::New('"<<name<<"',...)"
65 Package::Pointer p = MakePointer(new Package(name,
69 bbtkDebugMessage("object",2,"<## Package::New('"<<name<<"',...)"
73 //==========================================================================
75 //==========================================================================
76 /// Ctor with the name of the package
77 Package::Package(const std::string& name,
78 const std::string& author,
79 const std::string& description,
80 const std::string& version)
82 mDynamicLibraryHandler(0),
85 mDescription(description),
89 bbtkDebugMessage("object",2,"==> Package('"<<name<<"',...)"
92 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
93 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
94 std::string url = default_doc_dir;
95 if (c != '/' && c !='\\') url = url + "/";
96 url = url + "temp_dir/" + name + "/index.html";
99 SetDocRelativeURL("Relative url not set");
102 std::string relurl(BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH));
103 relurl += "/packages/"+name+"/bbdoc/index.html";
104 std::string url = ConfigurationFile::GetInstance().Get_url()
107 SetDocRelativeURL(relurl);
110 // std::cout << " url=["<<url<<"]"<<std::endl;
111 // std::cout << "relurl=["<<relurl<<"]"<<std::endl;
112 bbtkDebugMessage("object",2,"<== Package::Package('"<<name<<"',...) OK"
116 //==========================================================================
120 //==========================================================================
124 bbtkDebugMessage("object",2,"==> ~Package(\""<<mName<<"\")"<<bbtkendl);
126 //==========================================================================
129 //==========================================================================
130 void PackageReleaseBlackBoxDescriptorInternal(Package::WeakPointer pack,
131 const std::string& descname)
133 // Try to release descriptor
134 std::string packname = pack.lock()->GetName();
136 bbtkDebugMessage("package",5,"--- Releasing descriptor '"
137 <<packname<<"::"<<descname<<"'"<<bbtkendl);
140 Package::DescriptorMapType::iterator desc =
141 pack.lock()->GetDescriptorMap().find(descname);
142 if (desc == pack.lock()->GetDescriptorMap().end())
144 bbtkDebugMessage("package",5,
145 " Descriptor has already been released"
149 // bbtkDebugMessage("package",3,
150 // " Trying unreferencing it ... "<<std::endl);
151 BlackBoxDescriptor::WeakPointer pdesc = desc->second;
152 desc->second.reset();
153 // if it is dead : remove it
156 bbtkDebugMessage("package",2," ==> '"<<packname<<"::"<<descname<<"' Descriptor expired"<<bbtkendl);
159 bbtkDebugMessage("package",2,
160 " ... and caused its package death"
164 desc = pack.lock()->GetDescriptorMap().find(descname);
165 if (desc != pack.lock()->GetDescriptorMap().end()) pack.lock()->GetDescriptorMap().erase(desc);
166 } else { //pdesc.expired
167 bbtkDebugMessage("package",5," ... Descriptor still alive ("
168 <<pdesc.use_count()<<" refs)"
170 pack.lock()->GetDescriptorMap()[descname] = pdesc.lock();
173 //==========================================================================
176 //==========================================================================
178 void Package::Release(Package::WeakPointer pack)
180 std::string packname = pack.lock()->mName;
181 bbtkDebugMessage("package",1,"==> Package::Release('"<<
182 packname<<"')"<<bbtkendl);
184 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
185 long ndesc = pack.lock()->GetDescriptorMap().size();
186 long nrefs = pack.use_count();
188 bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
189 <<ndesc<<" descr / dyn="
192 // A package is "free" from any external reference iff :
193 // i) It is not dynamically loaded and nrefs == ndesc
194 // (each desc references its package) or
195 // ii) It is dynamically loaded and nrefs == ndesc + 1
196 // (A dynamic library holds a static pointer on the package it contains
197 // which is allocated when the PACKAGENAMEGetPackage() func is called,
198 // and descallocated (reset) by PACKAGENAMEDeletePackage())
199 if (nrefs == ndesc + dyn)
201 bbtkDebugMessage("package",5,
202 " -> No more external ref : checking descriptors"
204 // We must take care that removing refs on descriptors
205 // can lead to their deletion which can in turn unref
206 // internal boxes which can release their descriptors hence
207 // call Package::ReleaseBlackBoxDescriptor
208 // As a consequence during descriptors release :
209 // 1) The map can change dynamically : we cannot iterate over it
210 // as any iterator can become invalid
211 // 2) The package can auto-destruct : we must test its existence
212 // after each release
213 // We must also take care of not locking the package pointer
214 // or any ref count check in Package::ReleaseBlackBoxDescriptor
217 // The list of descriptors names at start
218 std::vector<std::string> descnamelist;
219 DescriptorMapType::iterator i;
220 for (i=pack.lock()->mDescriptorMap.begin();
221 i!= pack.lock()->mDescriptorMap.end();
223 descnamelist.push_back(i->first);
225 // Iterator over the initial names
226 std::vector<std::string>::iterator descname;
227 for (descname=descnamelist.begin();
228 descname!=descnamelist.end();
231 // Is package still alive ?
234 bbtkDebugMessage("package",1,"--- Package::Release('"<<
236 <<"') : package expired during release : bailing out"<<bbtkendl);
241 BlackBoxDescriptor::Pointer desc = pack.lock()->mDescriptorMap[*descname];
242 if ( (dyn==0) || (boost::dynamic_pointer_cast<ComplexBlackBoxDescriptor>(desc)) )
244 PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
247 PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
253 UnLoadDynamicLibrary(pack);
254 // Unload orphan dl packages
255 Package::UnLoadReleasedDynamicallyLoadedPackages();
259 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
261 bbtkDebugMessage("package",2,"<== Package::Release('"<<
262 packname<<"')"<<bbtkendl);
266 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
267 long ndesc = pack.lock()->GetDescriptorMap().size();
268 long nrefs = pack.use_count();
270 bbtkDebugMessage("package",1," ... Package still alive ("
272 <<ndesc<<" descr / dyn="
273 <<dyn<<")"<<std::endl);
277 bbtkDebugMessage("package",1," ... Package has been released"
282 //==========================================================================
284 //==========================================================================
285 /// "Releases" the package
286 /// Signals the package that it can free the given descriptor
287 /// if they are no more used and free itself if it is no
289 /// Note : Any non-weak pointer on the package must have been freed
290 void Package::ReleaseBlackBoxDescriptor(Package::WeakPointer pack,
291 BlackBoxDescriptor::WeakPointer descr)
293 std::string packname = pack.lock()->mName;
294 std::string dname = descr.lock()->GetTypeName();
295 bbtkDebugMessage("package",3,"==> Package::ReleaseBlackBoxDescriptor('"<<
296 packname<<"','"<<dname<<"') : refs="
297 <<descr.use_count()<<bbtkendl);
299 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
300 long ndesc = pack.lock()->GetDescriptorMap().size();
301 long nrefs = pack.use_count();
303 bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
304 <<ndesc<<" descr / dynamically loaded = "
307 // A package is "free" from any external reference iff :
308 // i) It is not dynamically loaded and nrefs == ndesc
309 // (each desc references its package) or
310 // ii) It is dynamically loaded and nrefs == ndesc + 1
311 // (A dynamic library holds a static pointer on the package it contains
312 // which is allocated when the PACKAGENAMEGetPackage() func is called,
313 // and descallocated (reset) by PACKAGENAMEDeletePackage())
314 if (nrefs == ndesc + dyn)
319 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
321 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
325 // If the package is released and dynamically loaded
326 // then put it in the static list mReleasedDynamicallyLoadedPackages
327 UnLoadDynamicLibrary(pack,false);
329 bbtkDebugMessage("package",4,"<== Package::ReleaseBlackBoxDescriptor('"<<
330 packname<<"','"<<dname<<"'): refs="
331 <<descr.use_count()<<bbtkendl);
335 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
336 long ndesc = pack.lock()->GetDescriptorMap().size();
337 long nrefs = pack.use_count();
339 bbtkDebugMessage("package",3," ... Package still alive ("
341 <<ndesc<<" descr / dyn="
342 <<dyn<<")"<<std::endl);
346 bbtkDebugMessage("package",3," ... Package has been released"
351 //==========================================================================
353 //==========================================================================
354 /// Opens a dynamic library which contains a bbtk package
355 /// Returns the handler
356 /// Load the package management symbols from the lib
357 /// returns false if a problem occured hence can be used
358 /// to test that a dyn lib is a valid bbtk package lib
359 /// NB : The BBTK version exported from the library
360 /// is tested against the current bbtk version
361 DynamicLibraryHandler Package::OpenDynamicLibrary( const std::string& libname,const std::string& package_name,
362 DLGetPackageFunction& getpack, DLDeletePackageFunction& delpack)
364 bbtkDebugMessage("package",3,"==> Package::OpenDynamicLibrary("
365 <<libname<<")"<<std::endl);
366 #if defined(__GNUC__)
370 handler = dlopen(libname.c_str(), BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
371 //EED handler = dlopen(libname.c_str(), RTLD_LAZY | RTLD_LOCAL );
375 bbtkMessage("package",0,
376 "BBTK ..ERROR.. loading could not open shared library [" <<libname<<"] : "
377 <<dlerror() << std::endl);
381 bbtkDebugMessage("package",3,"* Shared lib ["<<libname<<"] open"<<std::endl);
383 // Loads the Package bbtk version function
384 std::string getvername(package_name);
385 getvername += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
386 DLGetPackageBBTKVersionFunction getbbtkversion = (DLGetPackageBBTKVersionFunction)(dlsym(handler,getvername.c_str()));
389 bbtkDebugMessage("package",3,"***"<<std::endl);
390 bbtkMessage("package",0,
391 "BBTK ..ERROR.. loading shared library ["<<libname
392 <<"] is not a valid bbtk package."
393 <<" Symbol ["<<getvername<<"] :"<<dlerror()<< std::endl);
398 bbtkDebugMessage("package",3,"* Symbol ["<<getvername
399 <<"] found"<<std::endl);
401 if (getbbtkversion() != bbtk::GetVersion())
403 bbtkMessage("package",0,
404 "BBTK ..ERROR.. loading: "<<package_name
405 <<" - Shared library ["<<libname
406 <<"] was build with bbtk version "
408 <<" but the current program runs with version "
409 <<bbtk::GetVersion()<<" : cannot load it. You have to recompile your BBTK-Package."<<std::endl);
415 bbtkDebugMessage("package",3,"* Package bbtk version '"<<getbbtkversion()<<"' matches"<<std::endl);
416 // Loads the Package get function
417 std::string getpackname(package_name);
418 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
419 getpack = (DLGetPackageFunction)(dlsym(handler, getpackname.c_str()));
422 bbtkMessage("package",0,
423 "BBTK ..ERROR.. loading shared library ["<<libname
424 <<"] is not a valid bbtk package."
425 <<" Symbol ["<<getpackname<<"] :"<<dlerror()<< std::endl);
430 bbtkDebugMessage("package",3,"* Symbol ["<<getpackname<<"] found"<<std::endl);
431 // Loads the Package delete function
433 std::string delpackname(package_name);
434 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
435 delpack = (DLDeletePackageFunction)(dlsym(handler, delpackname.c_str()));
438 bbtkMessage("package",0,
439 "BBTK ..ERROR.. loading shared library ["<<libname
440 <<"] is not a valid bbtk package."
441 <<" Symbol ["<<delpackname<<"] :"<<dlerror()<< std::endl);
445 bbtkDebugMessage("package",3,"* Symbol ["<<delpackname<<"] found"<<std::endl);
446 #elif defined(_WIN32)
452 handler = LoadLibrary(libname.c_str());
455 bbtkMessage("package",0,
456 "BBTK ..ERROR.. could not open shared library [" <<libname<<"]"
458 DWORD dwErrorCode = 0;
459 dwErrorCode = GetLastError();
460 bbtkMessage("package",2,
461 "Windows Error: [" << dwErrorCode <<"]"
467 // Loads the Package bbtk version function
468 std::string getvername(package_name);
469 getvername += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
470 DLGetPackageBBTKVersionFunction getbbtkversion = (DLGetPackageBBTKVersionFunction)(GetProcAddress(handler, getvername.c_str()));
474 FreeLibrary(handler);
475 bbtkMessage("package",0,
476 "BBTK ..ERROR.. loading shared library ["<<libname
477 <<"] is not a valid bbtk package."
478 <<" Symbol ["<<getbbtkversion<<"] not found"<< std::endl);
483 if (getbbtkversion() != bbtk::GetVersion())
485 FreeLibrary(handler);
486 bbtkMessage("package",0,
487 "BBTK ..ERROR.. loading: "<<package_name
488 <<" - Shared library ["<<libname
489 <<"] was build with bbtk version "
491 <<" but the current program runs with version "
492 <<bbtk::GetVersion()<<" : cannot load it. You have to recompile your BBTK-Package."<<std::endl);
496 // Loads the Package get function
497 std::string getpackname(package_name);
498 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
499 getpack = (DLGetPackageFunction)(GetProcAddress(handler, getpackname.c_str()));
502 FreeLibrary(handler);
503 bbtkMessage("package",0,
504 "BBTK ..ERROR.. loading shared library ["<<libname
505 <<"] is not a valid bbtk package."
506 <<" Symbol ["<<getpackname<<"] not found"<< std::endl);
510 // Loads the Package delete function
511 std::string delpackname(package_name);
512 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
513 delpack = (DLDeletePackageFunction)(GetProcAddress(handler, delpackname.c_str()));
516 FreeLibrary(handler);
517 bbtkMessage("package",0,
518 "BBTK ..ERROR.. loading shared library ["<<libname
519 <<"] is not a valid bbtk package."
520 <<" Symbol ["<<delpackname<<"] not found"<< std::endl);
525 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
530 //==========================================================================
532 //==========================================================================
533 /// Loads a package from a dynamic library
534 Package::Pointer Package::CreateFromDynamicLibrary(const std::string& libname,
535 const std::string& pkgname,
536 const std::string& path)
538 bbtkDebugMessage("package",1,"==> Package::CreateFromDynamicLibrary("
539 <<libname<<")"<<std::endl);
541 DLGetPackageFunction gf;
542 DLDeletePackageFunction df;
543 DynamicLibraryHandler h = Package::OpenDynamicLibrary(libname,
546 if (h==0) return Package::Pointer();
547 Package::Pointer p = gf();
548 p->mDynamicLibraryHandler = h;
549 p->mDLDeletePackageFunction = df;
551 std::string separator =
552 ConfigurationFile::GetInstance().Get_file_separator ();
553 //BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
554 std::string docreldoc =
555 separator + "bbdoc" + separator + pkgname + separator + "index.html";
557 ".." + separator + ".." + docreldoc;
558 std::string doc = path + separator + ".." + separator
559 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
563 p->SetDocRelativeURL(reldoc);
565 bbtkDebugMessage("package",2,"<== Package::CreateFromDynamicLibrary("
566 <<libname<<") .. OK"<<std::endl);
569 //==========================================================================
574 //==========================================================================
575 /// UnLoads the package dynamic library (if any)
576 void Package::UnLoadDynamicLibrary(Package::WeakPointer pack, bool doit)
578 if (pack.expired() || (!pack.lock()->mDynamicLibraryHandler))
581 std::string packname = pack.lock()->GetName();
582 bbtkDebugMessage("package",5,"==> Package::UnLoadDynamicLibrary('"
586 if (!pack.lock()->GetDescriptorMap().empty())
589 bbtkDebugMessage("package",5," Package not empty ... abort"
593 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
594 <<"DescriptorMap not empty "
595 <<BBTK_INTERNAL_ERROR_MESSAGE);
599 if (pack.use_count()!=1)
601 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
602 <<"empty dl package with external refs"
603 <<BBTK_INTERNAL_ERROR_MESSAGE);
609 bbtkDebugMessage("package",5,"==> dynamic library for package '"
610 <<packname<<"' closed"
615 mReleasedDynamicallyLoadedPackages.insert(pack);
616 bbtkDebugMessage("package",1,"==> package '"<<packname
617 <<"' put in the 'to unload' list"
621 bbtkDebugMessage("package",5,"<== Package::UnLoadDynamicLibrary('"
625 //==========================================================================
627 //==========================================================================
628 /// UnLoads released packages that were loaded dynamically
629 /// see UnLoadDynamicLibrary and ReleaseBlackBoxDescriptor
630 void Package::UnLoadReleasedDynamicallyLoadedPackages()
632 bbtkDebugMessage("package",5,"==> Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
634 std::set<Package::WeakPointer>::iterator i;
637 if(mReleasedDynamicallyLoadedPackages.size()>0){
638 for (i=mReleasedDynamicallyLoadedPackages.begin();
639 i!=mReleasedDynamicallyLoadedPackages.end();
642 if (!i->expired()) UnLoad(*i);
646 bbtkDebugMessage("package",5,"<== Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
648 //==========================================================================
650 //==========================================================================
651 void Package::UnLoad(Package::WeakPointer pack)
653 std::string packname = pack.lock()->GetName();
654 bbtkDebugMessage("package",6,"==> Package::UnLoad("<<packname<<")"<<std::endl);
656 Package* p = pack.lock().get();
658 DynamicLibraryHandler h = p->mDynamicLibraryHandler;
660 // deletes the package
661 p->mDLDeletePackageFunction();
663 // closes the dl handler
664 #if defined(__GNUC__)
667 printf("EED Package::UnLoad ERROR %s\n", packname.c_str() );
668 bbtkWarning("Failed to close dynamic library for package '"<<packname
672 #elif defined(_WIN32)
676 bbtkDebugMessage("package",1,"==> dynamic library for package '"
677 <<packname<<"' closed"
679 bbtkDebugMessage("package",6," ... dynamic library unloaded"<<std::endl);
681 //==========================================================================
683 bool Package::ifBoxExist( std::string type)
686 DescriptorMapType::const_iterator i = mDescriptorMap.find(type);
687 if (i != mDescriptorMap.end())
694 //==========================================================================
695 /// Creates an instance of a black box of type <type> with name <name>
696 BlackBox::Pointer Package::NewBlackBox(const std::string& type,
697 const std::string& name) const
699 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::NewBlackBox(\""<<type<<"\",\""<<name<<"\")"<<bbtkendl);
701 DescriptorMapType::const_iterator i = mDescriptorMap.find(type);
702 if (i == mDescriptorMap.end())
704 bbtkDebugDecTab("kernel",8);
705 return BlackBox::Pointer();
707 BlackBox::Pointer bb =i->second->NewBlackBox(name);
708 bbtkDebugDecTab("kernel",8);
712 //==========================================================================
716 //==========================================================================
717 /// Creates an instance of an adaptor of input type <typein> and
718 /// output type <typeout> with name <name>
719 BlackBox::Pointer Package::NewAdaptor(const DataInfo& typein,
720 const DataInfo& typeout,
721 const std::string& name) const
723 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
727 <<name<<"\")"<<bbtkendl);
729 AdaptorKey key(typein,typeout,
730 BlackBoxDescriptor::DEFAULT_ADAPTOR);
731 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
732 if (i == mAdaptorMap.end())
734 bbtkDebugDecTab("kernel",8);
735 return BlackBox::Pointer();
737 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
738 bbtkDebugDecTab("kernel",8);
742 //==========================================================================
744 //==========================================================================
745 /// Creates an instance of an adaptor of input type <typein> and
746 /// output type <typeout> with name <name>
747 BlackBox::Pointer Package::NewWidgetAdaptor(const DataInfo& typein,
748 const DataInfo& typeout,
749 const std::string& name) const
751 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
752 ">::NewWidgetAdaptor("
755 <<name<<"\")"<<bbtkendl);
757 AdaptorKey key(typein,typeout,
758 BlackBoxDescriptor::DEFAULT_GUI);
759 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
760 if (i == mAdaptorMap.end())
762 bbtkDebugDecTab("kernel",8);
763 return BlackBox::Pointer();
765 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
766 bbtkDebugDecTab("kernel",8);
770 //==========================================================================
774 //==========================================================================
775 /// Returns true is the package contains
776 /// an adaptor of input type <typein> and
777 /// output type <typeout>
778 /// If successfull then adaptor contains the black box type name
779 bool Package::FindWidgetAdaptor(const DataInfo& typein,
780 const DataInfo& typeout,
781 std::string& adaptor) const
783 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
784 ">::FindWidgetAdaptor("
786 <<typeout<<")"<<bbtkendl);
788 AdaptorKey key(/*typein*/
789 DataInfo(typeid(void),""),
791 BlackBoxDescriptor::DEFAULT_GUI);
792 // First try to find a single widget adaptor
793 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
794 if (i == mAdaptorMap.end())
796 bbtkDebugDecTab("kernel",8);
799 adaptor = i->second.lock()->GetTypeName();
800 bbtkDebugDecTab("kernel",8);
804 //==========================================================================
808 //==========================================================================
809 /// Returns true is the package contains
810 /// an adaptor of input type <typein> and
811 /// output type <typeout>
812 /// If successfull then adaptor contains the black box type name
813 bool Package::FindAdaptor(const DataInfo& typein,
814 const DataInfo& typeout,
815 std::string& adaptor) const
817 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
820 <<typeout<<")"<<bbtkendl);
822 AdaptorKey key(typein,typeout,
823 BlackBoxDescriptor::DEFAULT_ADAPTOR);
824 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
825 if (i == mAdaptorMap.end())
827 bbtkDebugDecTab("kernel",8);
830 adaptor = i->second.lock()->GetTypeName();
831 bbtkDebugDecTab("kernel",8);
835 //==========================================================================
838 //==========================================================================
839 /// Registers a black box descriptor in the package
840 bool Package::Register(BlackBoxDescriptor::Pointer d)
842 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\")"<<std::endl);
844 DescriptorMapType::iterator i = mDescriptorMap.find(d->GetTypeName());
845 if (i!=mDescriptorMap.end())
847 bbtkWarning("Package<"<<GetName()<<"> : Trying to register box type <"
848 <<d->GetTypeName()<<"> which is already in the package");
852 mDescriptorMap[d->GetTypeName()] = d;
854 d->SetPackage(GetThisPointer<Package>());
856 // If it is a default adaptor, also register it in the adaptors map
857 if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR )
859 bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\") : The box is an adaptor, inserting it in adaptors map ..."<<std::endl);
861 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
862 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
863 DataInfo infoin(typein,d->GetInputDescriptor("In")->GetNature());
864 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
865 AdaptorKey key(infoin,infoout,d->GetKind());
867 AdaptorMapType::const_iterator i;
868 i = mAdaptorMap.find(key);
869 if (i == mAdaptorMap.end())
871 mAdaptorMap[key] = d;
873 // If already an adaptor registered : error
876 if (i->second.lock()->GetTypeName() != d->GetTypeName())
878 bbtkError("Package <"<<GetName()<<
879 "> : trying to register black box <"
881 <<"> as default adaptor but there is already a default adaptor registered (<"
882 <<i->second.lock()->GetTypeName()<<">)");
886 // If it is a default adaptor, also register it in the adaptors map
887 else if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_GUI)
889 bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\") : The box is a widget adaptor, inserting it in adaptors map ..."<<std::endl);
891 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
892 DataInfo infoin(typeid(void),"");
893 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
894 AdaptorKey key(infoin,infoout,d->GetKind());
896 AdaptorMapType::const_iterator i;
897 i = mAdaptorMap.find(key);
898 if (i == mAdaptorMap.end())
900 mAdaptorMap[key] = d;
902 // If already an adaptor registered : error
905 if (i->second.lock()->GetTypeName() != d->GetTypeName())
907 bbtkError("Package <"<<GetName()<<
908 "> : trying to register black box <"
910 <<"> as default widget adaptor but there is already a default adaptor registered (<"
911 <<i->second.lock()->GetTypeName()<<">)");
917 bbtkDebugDecTab("kernel",8);
921 //==========================================================================
923 //===================================================================
924 void Package::Check() const
926 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
927 <<" ["<<GetName()<<"]"<<std::endl);
928 DescriptorMapType::const_iterator i;
929 for (i=mDescriptorMap.begin();
930 i!=mDescriptorMap.end();
933 i->second->Check(true);
935 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
936 <<" ["<<GetName()<<"] ... OK"<<std::endl);
938 //===================================================================
941 //==========================================================================
942 /// Changes the name of a black box type
943 void Package::ChangeDescriptorName( const std::string& oldname, const std::string& newname )
945 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
946 <<">::ChangeDescriptorName(\""<<oldname
947 <<"\",\""<<newname<<"\")"<<std::endl);
948 // Looking into the bb map
949 DescriptorMapType::iterator i = mDescriptorMap.find(oldname);
950 if (i == mDescriptorMap.end())
952 bbtkDebugDecTab("kernel",8);
953 bbtkError("ChangeDescriptorName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
956 i->second->SetTypeName(newname);
957 mDescriptorMap[newname] = i->second;
958 mDescriptorMap.erase(i);
960 bbtkDebugDecTab("kernel",8);
962 //==========================================================================
966 //==========================================================================
967 void Package::PrintHelpListDescriptors(bool description, bool adaptors) const
969 unsigned int lmax = 0;
970 std::vector<std::string> names;
971 std::vector<std::string> kinds;
972 std::vector<std::string> descrs;
974 DescriptorMapType::const_iterator i;
975 for (i=mDescriptorMap.begin();
976 i!=mDescriptorMap.end();
980 ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) )
982 std::string name(" ");
983 name += i->second->GetTypeName();
984 names.push_back(name);
987 if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR )
989 kind = std::string("[A]");
991 else if ( i->second->GetKind() ==
992 BlackBoxDescriptor::DEFAULT_ADAPTOR )
994 kind = std::string("[DA]");
996 kinds.push_back(kind);
998 unsigned int l = name.size()+kind.size();
999 if (l>lmax) lmax = l;
1005 descr += i->second->GetDescription();
1007 descrs.push_back(descr);
1013 offs.append(lmax+3,' ');
1014 std::vector<std::string>::iterator ni,ci,di;
1015 for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin();
1016 ni != names.end(); ++ni, ++ci, ++di)
1019 space.append(lmax - ni->size() - ci->size(),' ');
1020 bbtkMessage("help",1,*ni << space << *ci );
1022 unsigned int dmax = 75 - lmax;
1023 // while (d.size() > dmax )
1026 bbtkMessage("help",1,d.substr(0,dmax) << "..." << std::endl);
1028 bbtkMessage("help",1,d << std::endl);
1029 // d = d.substr(dmax,d.size());
1034 //==========================================================================
1036 //==========================================================================
1037 /// Displays the list of adaptors of the package
1038 void Package::PrintHelpListAdaptors(bool description) const
1040 DescriptorMapType::const_iterator i;
1041 for (i=mDescriptorMap.begin();
1042 i!=mDescriptorMap.end();
1045 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD )
1047 bbtkMessage("help",1,
1048 " "<<i->second->GetTypeName());
1049 if ( i->second->GetKind() ==
1050 BlackBoxDescriptor::DEFAULT_ADAPTOR )
1052 bbtkMessage("help",1,
1057 bbtkMessage("help",1,
1058 " : "<<i->second->GetDescription());
1061 bbtkMessage("help",1,std::endl);
1065 AdaptorMapType::const_iterator i;
1066 for (i=mAdaptorMap.begin();
1067 i!=mAdaptorMap.end();
1070 bbtkMessage("help",1,
1071 " "<<i->second->GetTypeName());
1074 bbtkMessage("help",1,
1075 " : "<<i->second->GetDescription());
1078 bbtkMessage("help",1,std::endl);
1082 //==========================================================================
1084 //==========================================================================
1085 /// Prints help on a black box descriptor
1086 void Package::PrintHelpDescriptor(const std::string& name, bool full) const
1088 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
1089 <<">::PrintHelpDescriptor(\""
1090 <<name<<"\")"<<bbtkendl);
1092 DescriptorMapType::const_iterator i = mDescriptorMap.find(name);
1093 if (i == mDescriptorMap.end())
1095 bbtkDebugDecTab("kernel",8);
1096 bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
1098 // bbtkMessage("help",1,"["<<GetName()<<"] ");
1099 i->second->GetHelp(full);
1100 bbtkDebugDecTab("kernel",8);
1103 //==========================================================================
1106 //==========================================================================
1107 /// Returns true iff the package contains the box of name boxname
1108 bool Package::ContainsDescriptor(const std::string& name) const
1110 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
1111 <<">::ContainsDescriptor(\""
1112 <<name<<"\")"<<bbtkendl);
1114 DescriptorMapType::const_iterator i = mDescriptorMap.find(name);
1115 if (i == mDescriptorMap.end())
1117 bbtkDebugDecTab("kernel",8);
1120 bbtkDebugDecTab("kernel",8);
1123 //==========================================================================
1127 //==========================================================================
1128 void Package::CreateHtmlPage(const std::string& filename,
1129 const std::string& caller,
1130 const std::string& source,
1131 const std::string& custom_header,
1132 const std::string& custom_title,
1135 bool relative_link ) const
1137 bbtkDebugMessageInc("kernel",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
1138 <<filename<<"\")"<<bbtkendl);
1140 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1133"<<std::endl;
1142 //---------------------
1145 s.open(filename.c_str());
1148 bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
1151 //----------------------
1153 std::string title = "BBTK Package "+GetName()+" "+GetVersion();
1155 if (custom_title.length() != 0) title = custom_title;
1157 s << "<html lang=\"en\">\n";
1159 s << "<title>" << title << "</title>\n";
1160 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
1161 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
1162 s << "<meta name=\"generator\" content=\"\">\n";
1163 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
1164 //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
1165 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
1166 s << "pre.display { font-family:inherit }\n";
1167 s << "pre.format { font-family:inherit }\n";
1168 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
1169 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
1170 s << "pre.smallexample { font-size:smaller }\n";
1171 s << "pre.smalllisp { font-size:smaller }\n";
1172 s << "span.sc { font-variant:small-caps }\n";
1173 s << "span.roman { font-family:serif; font-weight:normal; } \n";
1174 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
1175 s << "--></style>\n";
1177 //----------------------
1179 //----------------------
1182 s << "<a name=\"Top\"></a>\n";
1184 //----------------------
1186 if ( custom_header.length() != 0)
1188 if ( custom_header != "none" )
1191 in.open(custom_header.c_str());
1194 bbtkError("Could not open file \""<<custom_header<<"\"");
1199 in.getline(buffer,512);
1200 std::string line(buffer);
1207 s << "<object data=\"" << custom_header
1208 << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
1209 << custom_header <<" could not be embedded.</object>\n";
1218 s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
1219 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1220 s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1221 << GetDescription() << "</TD></TR>\n";
1222 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1223 << GetAuthor() << "</TD></TR>\n";
1224 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1225 << GetCategory() << "</TD></TR>\n";
1226 s << "<TR><TD style='vertical-align: top;'><b> Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1227 << GetVersion() << "</TD></TR>\n";
1228 s << "<TR><TD style='vertical-align: top;'><b> bbtk Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1229 << bbtk::GetVersion() << "</TD></TR>\n";
1232 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1225"<<std::endl;
1233 //-------------------
1234 // Table of contents
1236 // s << "<div class=\"contents\">\n";
1237 s << "<p><b> Black Boxes : </b>\n";
1240 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1242 DescriptorMapType::const_iterator i;
1243 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1236"<<std::endl;
1244 for (i=mDescriptorMap.begin(); i!=mDescriptorMap.end(); ++i) {
1245 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD)
1248 std::string name = i->second->GetTypeName();
1249 Utilities::html_format(name);
1250 std::string descr = i->second->GetDescription();
1251 //Utilities::html_format(descr);
1252 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1246"<<std::endl;
1254 s << "<TD style='vertical-align: top;'>";
1255 s << " <a name=\"toc_"<<name
1256 <<"\" href=\"#"<<name<<"\">"
1259 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1268 //-------------------
1270 if (mAdaptorMap.size()>0)
1272 // s << "<div class=\"contents\">\n";
1273 s << "<p><b> Adaptors : </b>\n";
1275 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1268"<<std::endl;
1276 // DescriptorMapType::const_iterator i;
1277 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1278 for (i=mDescriptorMap.begin(); i!=mDescriptorMap.end();++i)
1280 if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD)
1283 std::string name = i->second->GetTypeName();
1284 Utilities::html_format(name);
1285 std::string descr = i->second->GetDescription();
1288 s << "<TD style='vertical-align: top;'>";
1289 s << " <a name=\"toc_"<<name
1290 <<"\" href=\"#"<<name<<"\">"
1293 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1303 // s << "<div class=\"node\">\n";
1305 // s << "<p><hr>\n";
1306 // s << "<a name=\"Top\"></a>\n";
1307 // s << "Top: <a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
1308 // s << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
1309 // s << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
1313 //----------------------
1316 //-------------------
1317 // Computes output directory from filename to pass it to
1318 // BlackBoxDescriptor::InsertHtmlHelp
1321 std::string::size_type slash_position = filename.find_last_of("/\\");
1324 if (slash_position != std::string::npos) {
1325 if (slash_position == 0)
1327 dir = filename.substr(0,slash_position);
1330 for (i=mDescriptorMap.begin();
1331 i!=mDescriptorMap.end();
1334 i->second->InsertHtmlHelp(s,detail,level,dir,relative_link);
1337 //----------------------
1342 ptm = gmtime ( &rawtime );
1345 s << "Automatically generated by <b>"<<caller<<"</b> "//from <b>"
1348 << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900
1349 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
1350 s << "</body></html>\n";
1352 //----------------------
1355 bbtkDebugDecTab("kernel",9);
1357 //==========================================================================
1359 //==========================================================================
1360 std::string Package::GetObjectName() const
1362 return std::string("Package '")+mName+std::string("'");
1364 //==========================================================================
1366 //==========================================================================
1367 std::string Package::GetObjectInfo() const
1369 std::stringstream i;
1370 i << " - "<<mDescriptorMap.size() << " boxes" << std::endl;
1371 if (mDynamicLibraryHandler)
1373 i<< " - Loaded from dynamic library"<<std::endl;
1377 //==========================================================================
1380 //==========================================================================
1381 size_t Package::GetObjectSize() const
1383 size_t s = Superclass::GetObjectSize();
1384 s += Package::GetObjectInternalSize();
1387 //==========================================================================
1388 //==========================================================================
1389 size_t Package::GetObjectInternalSize() const
1391 size_t s = sizeof(Package);
1394 //==========================================================================
1395 //==========================================================================
1396 size_t Package::GetObjectRecursiveSize() const
1398 size_t s = Superclass::GetObjectRecursiveSize();
1399 s += Package::GetObjectInternalSize();
1401 DescriptorMapType::const_iterator i;
1402 for (i = mDescriptorMap.begin(); i!=mDescriptorMap.end(); ++i )
1404 s += i->second->GetObjectRecursiveSize();
1408 //==========================================================================
1409 void Package::GetBoxesInside(NodeTreeC& tree, int cont)
1411 DescriptorMapType::const_iterator i;
1412 std::cout<<"*********a********"<<std::endl;
1413 for (i=mDescriptorMap.begin(); i!=mDescriptorMap.end(); ++i)
1415 i->second->GetBoxesInside(tree, cont);
1416 std::cout<<"*****************"<<std::endl;
1419 //==========================================================================
1420 //==========================================================================
1421 std::set<Package::WeakPointer>
1422 Package::mReleasedDynamicallyLoadedPackages;
1423 //==========================================================================