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),
88 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"
115 //==========================================================================
119 //==========================================================================
123 bbtkDebugMessage("object",2,"==> ~Package(\""<<mName<<"\")"<<bbtkendl);
125 //==========================================================================
128 //==========================================================================
129 void PackageReleaseBlackBoxDescriptorInternal(Package::WeakPointer pack,
130 const std::string& descname)
132 // Try to release descriptor
133 std::string packname = pack.lock()->GetName();
135 bbtkDebugMessage("package",5,"--- Releasing descriptor '"
136 <<packname<<"::"<<descname<<"'"<<bbtkendl);
139 Package::DescriptorMapType::iterator desc =
140 pack.lock()->GetDescriptorMap().find(descname);
141 if (desc == pack.lock()->GetDescriptorMap().end())
143 bbtkDebugMessage("package",5,
144 " Descriptor has already been released"
148 // bbtkDebugMessage("package",3,
149 // " Trying unreferencing it ... "<<std::endl);
150 BlackBoxDescriptor::WeakPointer pdesc = desc->second;
151 desc->second.reset();
152 // if it is dead : remove it
155 bbtkDebugMessage("package",2," ==> '"<<packname<<"::"<<descname<<"' Descriptor expired"<<bbtkendl);
158 bbtkDebugMessage("package",2,
159 " ... and caused its package death"
163 desc = pack.lock()->GetDescriptorMap().find(descname);
164 if (desc != pack.lock()->GetDescriptorMap().end()) pack.lock()->GetDescriptorMap().erase(desc);
165 } else { //pdesc.expired
166 bbtkDebugMessage("package",5," ... Descriptor still alive ("
167 <<pdesc.use_count()<<" refs)"
169 pack.lock()->GetDescriptorMap()[descname] = pdesc.lock();
172 //==========================================================================
175 //==========================================================================
177 void Package::Release(Package::WeakPointer pack)
179 std::string packname = pack.lock()->mName;
180 bbtkDebugMessage("package",1,"==> Package::Release('"<<
181 packname<<"')"<<bbtkendl);
183 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
184 long ndesc = pack.lock()->GetDescriptorMap().size();
185 long nrefs = pack.use_count();
187 bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
188 <<ndesc<<" descr / dyn="
191 // A package is "free" from any external reference iff :
192 // i) It is not dynamically loaded and nrefs == ndesc
193 // (each desc references its package) or
194 // ii) It is dynamically loaded and nrefs == ndesc + 1
195 // (A dynamic library holds a static pointer on the package it contains
196 // which is allocated when the PACKAGENAMEGetPackage() func is called,
197 // and descallocated (reset) by PACKAGENAMEDeletePackage())
198 if (nrefs == ndesc + dyn)
200 bbtkDebugMessage("package",5,
201 " -> No more external ref : checking descriptors"
203 // We must take care that removing refs on descriptors
204 // can lead to their deletion which can in turn unref
205 // internal boxes which can release their descriptors hence
206 // call Package::ReleaseBlackBoxDescriptor
207 // As a consequence during descriptors release :
208 // 1) The map can change dynamically : we cannot iterate over it
209 // as any iterator can become invalid
210 // 2) The package can auto-destruct : we must test its existence
211 // after each release
212 // We must also take care of not locking the package pointer
213 // or any ref count check in Package::ReleaseBlackBoxDescriptor
216 // The list of descriptors names at start
217 std::vector<std::string> descnamelist;
218 DescriptorMapType::iterator i;
219 for (i=pack.lock()->mDescriptorMap.begin();
220 i!= pack.lock()->mDescriptorMap.end();
222 descnamelist.push_back(i->first);
224 // Iterator over the initial names
225 std::vector<std::string>::iterator descname;
226 for (descname=descnamelist.begin();
227 descname!=descnamelist.end();
230 // Is package still alive ?
233 bbtkDebugMessage("package",1,"--- Package::Release('"<<
235 <<"') : package expired during release : bailing out"<<bbtkendl);
240 BlackBoxDescriptor::Pointer desc = pack.lock()->mDescriptorMap[*descname];
241 if ( (dyn==0) || (boost::dynamic_pointer_cast<ComplexBlackBoxDescriptor>(desc)) )
243 PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
246 PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
252 UnLoadDynamicLibrary(pack);
253 // Unload orphan dl packages
254 Package::UnLoadReleasedDynamicallyLoadedPackages();
258 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
260 bbtkDebugMessage("package",2,"<== Package::Release('"<<
261 packname<<"')"<<bbtkendl);
265 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
266 long ndesc = pack.lock()->GetDescriptorMap().size();
267 long nrefs = pack.use_count();
269 bbtkDebugMessage("package",1," ... Package still alive ("
271 <<ndesc<<" descr / dyn="
272 <<dyn<<")"<<std::endl);
276 bbtkDebugMessage("package",1," ... Package has been released"
281 //==========================================================================
283 //==========================================================================
284 /// "Releases" the package
285 /// Signals the package that it can free the given descriptor
286 /// if they are no more used and free itself if it is no
288 /// Note : Any non-weak pointer on the package must have been freed
289 void Package::ReleaseBlackBoxDescriptor(Package::WeakPointer pack,
290 BlackBoxDescriptor::WeakPointer descr)
292 std::string packname = pack.lock()->mName;
293 std::string dname = descr.lock()->GetTypeName();
294 bbtkDebugMessage("package",3,"==> Package::ReleaseBlackBoxDescriptor('"<<
295 packname<<"','"<<dname<<"') : refs="
296 <<descr.use_count()<<bbtkendl);
298 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
299 long ndesc = pack.lock()->GetDescriptorMap().size();
300 long nrefs = pack.use_count();
302 bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
303 <<ndesc<<" descr / dynamically loaded = "
306 // A package is "free" from any external reference iff :
307 // i) It is not dynamically loaded and nrefs == ndesc
308 // (each desc references its package) or
309 // ii) It is dynamically loaded and nrefs == ndesc + 1
310 // (A dynamic library holds a static pointer on the package it contains
311 // which is allocated when the PACKAGENAMEGetPackage() func is called,
312 // and descallocated (reset) by PACKAGENAMEDeletePackage())
313 if (nrefs == ndesc + dyn)
318 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
320 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
324 // If the package is released and dynamically loaded
325 // then put it in the static list mReleasedDynamicallyLoadedPackages
326 UnLoadDynamicLibrary(pack,false);
328 bbtkDebugMessage("package",4,"<== Package::ReleaseBlackBoxDescriptor('"<<
329 packname<<"','"<<dname<<"'): refs="
330 <<descr.use_count()<<bbtkendl);
334 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
335 long ndesc = pack.lock()->GetDescriptorMap().size();
336 long nrefs = pack.use_count();
338 bbtkDebugMessage("package",3," ... Package still alive ("
340 <<ndesc<<" descr / dyn="
341 <<dyn<<")"<<std::endl);
345 bbtkDebugMessage("package",3," ... Package has been released"
350 //==========================================================================
352 //==========================================================================
353 /// Opens a dynamic library which contains a bbtk package
354 /// Returns the handler
355 /// Load the package management symbols from the lib
356 /// returns false if a problem occured hence can be used
357 /// to test that a dyn lib is a valid bbtk package lib
358 /// NB : The BBTK version exported from the library
359 /// is tested against the current bbtk version
360 DynamicLibraryHandler Package::OpenDynamicLibrary( const std::string& libname,const std::string& package_name,
361 DLGetPackageFunction& getpack, DLDeletePackageFunction& delpack)
364 printf("EED Package::OpenDynamicLibrary %s %s \n", libname.c_str(), package_name.c_str() );
366 bbtkDebugMessage("package",3,"==> Package::OpenDynamicLibrary("
367 <<libname<<")"<<std::endl);
368 #if defined(__GNUC__)
372 handler = dlopen(libname.c_str(), BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
373 // handler = dlopen(libname.c_str(), RTLD_LAZY | RTLD_LOCAL );
376 bbtkMessage("package",0,
377 "BBTK ..ERROR.. loading could not open shared library [" <<libname<<"] : "
378 <<dlerror() << std::endl);
382 bbtkDebugMessage("package",3,"* Shared lib ["<<libname<<"] open"<<std::endl);
384 // Loads the Package bbtk version function
385 std::string getvername(package_name);
386 getvername += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
387 DLGetPackageBBTKVersionFunction getbbtkversion = (DLGetPackageBBTKVersionFunction)(dlsym(handler,getvername.c_str()));
390 bbtkDebugMessage("package",3,"***"<<std::endl);
391 bbtkMessage("package",0,
392 "BBTK ..ERROR.. loading shared library ["<<libname
393 <<"] is not a valid bbtk package."
394 <<" Symbol ["<<getvername<<"] :"<<dlerror()<< std::endl);
399 bbtkDebugMessage("package",3,"* Symbol ["<<getvername
400 <<"] found"<<std::endl);
403 if (getbbtkversion() != bbtk::GetVersion())
405 bbtkMessage("package",0,
406 "BBTK ..ERROR.. loading: "<<package_name
407 <<" - Shared library ["<<libname
408 <<"] was build with bbtk version "
410 <<" but the current program runs with version "
411 <<bbtk::GetVersion()<<" : cannot load it. You have to recompile your BBTK-Package."<<std::endl);
417 bbtkDebugMessage("package",3,"* Package bbtk version '"<<getbbtkversion()<<"' matches"<<std::endl);
418 // Loads the Package get function
419 std::string getpackname(package_name);
420 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
421 getpack = (DLGetPackageFunction)(dlsym(handler, getpackname.c_str()));
424 bbtkMessage("package",0,
425 "BBTK ..ERROR.. loading shared library ["<<libname
426 <<"] is not a valid bbtk package."
427 <<" Symbol ["<<getpackname<<"] :"<<dlerror()<< std::endl);
432 bbtkDebugMessage("package",3,"* Symbol ["<<getpackname<<"] found"<<std::endl);
433 // Loads the Package delete function
435 std::string delpackname(package_name);
436 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
437 delpack = (DLDeletePackageFunction)(dlsym(handler, delpackname.c_str()));
440 bbtkMessage("package",0,
441 "BBTK ..ERROR.. loading shared library ["<<libname
442 <<"] is not a valid bbtk package."
443 <<" Symbol ["<<delpackname<<"] :"<<dlerror()<< std::endl);
447 bbtkDebugMessage("package",3,"* Symbol ["<<delpackname<<"] found"<<std::endl);
448 #elif defined(_WIN32)
454 handler = LoadLibrary(libname.c_str());
457 bbtkMessage("package",0,
458 "BBTK ..ERROR.. could not open shared library [" <<libname<<"]"
460 DWORD dwErrorCode = 0;
461 dwErrorCode = GetLastError();
462 bbtkMessage("package",2,
463 "Windows Error: [" << dwErrorCode <<"]"
469 // Loads the Package bbtk version function
470 std::string getvername(package_name);
471 getvername += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
472 DLGetPackageBBTKVersionFunction getbbtkversion = (DLGetPackageBBTKVersionFunction)(GetProcAddress(handler, getvername.c_str()));
476 FreeLibrary(handler);
477 bbtkMessage("package",0,
478 "BBTK ..ERROR.. loading shared library ["<<libname
479 <<"] is not a valid bbtk package."
480 <<" Symbol ["<<getbbtkversion<<"] not found"<< std::endl);
485 if (getbbtkversion() != bbtk::GetVersion())
487 FreeLibrary(handler);
488 bbtkMessage("package",0,
489 "BBTK ..ERROR.. loading: "<<package_name
490 <<" - Shared library ["<<libname
491 <<"] was build with bbtk version "
493 <<" but the current program runs with version "
494 <<bbtk::GetVersion()<<" : cannot load it. You have to recompile your BBTK-Package."<<std::endl);
498 // Loads the Package get function
499 std::string getpackname(package_name);
500 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
501 getpack = (DLGetPackageFunction)(GetProcAddress(handler, getpackname.c_str()));
504 FreeLibrary(handler);
505 bbtkMessage("package",0,
506 "BBTK ..ERROR.. loading shared library ["<<libname
507 <<"] is not a valid bbtk package."
508 <<" Symbol ["<<getpackname<<"] not found"<< std::endl);
512 // Loads the Package delete function
513 std::string delpackname(package_name);
514 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
515 delpack = (DLDeletePackageFunction)(GetProcAddress(handler, delpackname.c_str()));
518 FreeLibrary(handler);
519 bbtkMessage("package",0,
520 "BBTK ..ERROR.. loading shared library ["<<libname
521 <<"] is not a valid bbtk package."
522 <<" Symbol ["<<delpackname<<"] not found"<< std::endl);
527 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
532 //==========================================================================
534 //==========================================================================
535 /// Loads a package from a dynamic library
536 Package::Pointer Package::CreateFromDynamicLibrary(const std::string& libname,
537 const std::string& pkgname,
538 const std::string& path)
540 bbtkDebugMessage("package",1,"==> Package::CreateFromDynamicLibrary("
541 <<libname<<")"<<std::endl);
543 DLGetPackageFunction gf;
544 DLDeletePackageFunction df;
545 DynamicLibraryHandler h = Package::OpenDynamicLibrary(libname,
548 if (h==0) return Package::Pointer();
549 Package::Pointer p = gf();
550 p->mDynamicLibraryHandler = h;
551 p->mDLDeletePackageFunction = df;
553 std::string separator =
554 ConfigurationFile::GetInstance().Get_file_separator ();
555 //BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
556 std::string docreldoc =
557 separator + "bbdoc" + separator + pkgname + separator + "index.html";
559 ".." + separator + ".." + docreldoc;
560 std::string doc = path + separator + ".." + separator
561 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
565 p->SetDocRelativeURL(reldoc);
567 bbtkDebugMessage("package",2,"<== Package::CreateFromDynamicLibrary("
568 <<libname<<") .. OK"<<std::endl);
571 //==========================================================================
576 //==========================================================================
577 /// UnLoads the package dynamic library (if any)
578 void Package::UnLoadDynamicLibrary(Package::WeakPointer pack, bool doit)
580 if (pack.expired() || (!pack.lock()->mDynamicLibraryHandler))
583 std::string packname = pack.lock()->GetName();
584 bbtkDebugMessage("package",5,"==> Package::UnLoadDynamicLibrary('"
588 if (!pack.lock()->GetDescriptorMap().empty())
591 bbtkDebugMessage("package",5," Package not empty ... abort"
595 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
596 <<"DescriptorMap not empty "
597 <<BBTK_INTERNAL_ERROR_MESSAGE);
601 if (pack.use_count()!=1)
603 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
604 <<"empty dl package with external refs"
605 <<BBTK_INTERNAL_ERROR_MESSAGE);
611 bbtkDebugMessage("package",5,"==> dynamic library for package '"
612 <<packname<<"' closed"
617 mReleasedDynamicallyLoadedPackages.insert(pack);
618 bbtkDebugMessage("package",1,"==> package '"<<packname
619 <<"' put in the 'to unload' list"
623 bbtkDebugMessage("package",5,"<== Package::UnLoadDynamicLibrary('"
627 //==========================================================================
629 //==========================================================================
630 /// UnLoads released packages that were loaded dynamically
631 /// see UnLoadDynamicLibrary and ReleaseBlackBoxDescriptor
632 void Package::UnLoadReleasedDynamicallyLoadedPackages()
634 bbtkDebugMessage("package",5,"==> Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
636 std::set<Package::WeakPointer>::iterator i;
639 if(mReleasedDynamicallyLoadedPackages.size()>0){
640 for (i=mReleasedDynamicallyLoadedPackages.begin();
641 i!=mReleasedDynamicallyLoadedPackages.end();
644 if (!i->expired()) UnLoad(*i);
648 bbtkDebugMessage("package",5,"<== Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
650 //==========================================================================
652 //==========================================================================
653 void Package::UnLoad(Package::WeakPointer pack)
655 std::string packname = pack.lock()->GetName();
656 bbtkDebugMessage("package",6,"==> Package::UnLoad("<<packname<<")"<<std::endl);
658 Package* p = pack.lock().get();
660 DynamicLibraryHandler h = p->mDynamicLibraryHandler;
662 // deletes the package
663 p->mDLDeletePackageFunction();
665 // closes the dl handler
666 #if defined(__GNUC__)
669 printf("EED Package::UnLoad ERROR %s\n", packname.c_str() );
670 bbtkWarning("Failed to close dynamic library for package '"<<packname
674 #elif defined(_WIN32)
678 bbtkDebugMessage("package",1,"==> dynamic library for package '"
679 <<packname<<"' closed"
681 bbtkDebugMessage("package",6," ... dynamic library unloaded"<<std::endl);
683 //==========================================================================
685 bool Package::ifBoxExist( std::string type)
688 DescriptorMapType::const_iterator i = mDescriptorMap.find(type);
689 if (i != mDescriptorMap.end())
696 //==========================================================================
697 /// Creates an instance of a black box of type <type> with name <name>
698 BlackBox::Pointer Package::NewBlackBox(const std::string& type,
699 const std::string& name) const
701 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::NewBlackBox(\""<<type<<"\",\""<<name<<"\")"<<bbtkendl);
703 DescriptorMapType::const_iterator i = mDescriptorMap.find(type);
704 if (i == mDescriptorMap.end())
706 bbtkDebugDecTab("kernel",8);
707 return BlackBox::Pointer();
709 BlackBox::Pointer bb =i->second->NewBlackBox(name);
710 bbtkDebugDecTab("kernel",8);
714 //==========================================================================
718 //==========================================================================
719 /// Creates an instance of an adaptor of input type <typein> and
720 /// output type <typeout> with name <name>
721 BlackBox::Pointer Package::NewAdaptor(const DataInfo& typein,
722 const DataInfo& typeout,
723 const std::string& name) const
725 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
729 <<name<<"\")"<<bbtkendl);
731 AdaptorKey key(typein,typeout,
732 BlackBoxDescriptor::DEFAULT_ADAPTOR);
733 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
734 if (i == mAdaptorMap.end())
736 bbtkDebugDecTab("kernel",8);
737 return BlackBox::Pointer();
739 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
740 bbtkDebugDecTab("kernel",8);
743 //==========================================================================
745 //==========================================================================
746 /// Creates an instance of an adaptor of input type <typein> and
747 /// output type <typeout> with name <name>
748 BlackBox::Pointer Package::NewWidgetAdaptor(const DataInfo& typein,
749 const DataInfo& typeout,
750 const std::string& name) const
752 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
753 ">::NewWidgetAdaptor("
756 <<name<<"\")"<<bbtkendl);
758 AdaptorKey key(typein,typeout,
759 BlackBoxDescriptor::DEFAULT_GUI);
760 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
761 if (i == mAdaptorMap.end())
763 bbtkDebugDecTab("kernel",8);
764 return BlackBox::Pointer();
766 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
767 bbtkDebugDecTab("kernel",8);
771 //==========================================================================
775 //==========================================================================
776 /// Returns true is the package contains
777 /// an adaptor of input type <typein> and
778 /// output type <typeout>
779 /// If successfull then adaptor contains the black box type name
780 bool Package::FindWidgetAdaptor(const DataInfo& typein,
781 const DataInfo& typeout,
782 std::string& adaptor) const
784 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
785 ">::FindWidgetAdaptor("
787 <<typeout<<")"<<bbtkendl);
789 AdaptorKey key(/*typein*/
790 DataInfo(typeid(void),""),
792 BlackBoxDescriptor::DEFAULT_GUI);
793 // First try to find a single widget adaptor
794 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
795 if (i == mAdaptorMap.end())
797 bbtkDebugDecTab("kernel",8);
800 adaptor = i->second.lock()->GetTypeName();
801 bbtkDebugDecTab("kernel",8);
805 //==========================================================================
809 //==========================================================================
810 /// Returns true is the package contains
811 /// an adaptor of input type <typein> and
812 /// output type <typeout>
813 /// If successfull then adaptor contains the black box type name
814 bool Package::FindAdaptor(const DataInfo& typein,
815 const DataInfo& typeout,
816 std::string& adaptor) const
818 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
821 <<typeout<<")"<<bbtkendl);
823 AdaptorKey key(typein,typeout,
824 BlackBoxDescriptor::DEFAULT_ADAPTOR);
825 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
826 if (i == mAdaptorMap.end())
828 bbtkDebugDecTab("kernel",8);
831 adaptor = i->second.lock()->GetTypeName();
832 bbtkDebugDecTab("kernel",8);
836 //==========================================================================
839 //==========================================================================
840 /// Registers a black box descriptor in the package
841 bool Package::Register(BlackBoxDescriptor::Pointer d)
843 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\")"<<std::endl);
845 DescriptorMapType::iterator i = mDescriptorMap.find(d->GetTypeName());
846 if (i!=mDescriptorMap.end())
848 bbtkWarning("Package<"<<GetName()<<"> : Trying to register box type <"
849 <<d->GetTypeName()<<"> which is already in the package");
853 mDescriptorMap[d->GetTypeName()] = d;
855 d->SetPackage(GetThisPointer<Package>());
857 // If it is a default adaptor, also register it in the adaptors map
858 if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR )
860 bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\") : The box is an adaptor, inserting it in adaptors map ..."<<std::endl);
862 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
863 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
864 DataInfo infoin(typein,d->GetInputDescriptor("In")->GetNature());
865 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
866 AdaptorKey key(infoin,infoout,d->GetKind());
868 AdaptorMapType::const_iterator i;
869 i = mAdaptorMap.find(key);
870 if (i == mAdaptorMap.end())
872 mAdaptorMap[key] = d;
874 // If already an adaptor registered : error
877 if (i->second.lock()->GetTypeName() != d->GetTypeName())
879 bbtkError("Package <"<<GetName()<<
880 "> : trying to register black box <"
882 <<"> as default adaptor but there is already a default adaptor registered (<"
883 <<i->second.lock()->GetTypeName()<<">)");
887 // If it is a default adaptor, also register it in the adaptors map
888 else if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_GUI)
890 bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\") : The box is a widget adaptor, inserting it in adaptors map ..."<<std::endl);
892 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
893 DataInfo infoin(typeid(void),"");
894 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
895 AdaptorKey key(infoin,infoout,d->GetKind());
897 AdaptorMapType::const_iterator i;
898 i = mAdaptorMap.find(key);
899 if (i == mAdaptorMap.end())
901 mAdaptorMap[key] = d;
903 // If already an adaptor registered : error
906 if (i->second.lock()->GetTypeName() != d->GetTypeName())
908 bbtkError("Package <"<<GetName()<<
909 "> : trying to register black box <"
911 <<"> as default widget adaptor but there is already a default adaptor registered (<"
912 <<i->second.lock()->GetTypeName()<<">)");
918 bbtkDebugDecTab("kernel",8);
922 //==========================================================================
924 //===================================================================
925 void Package::Check() const
927 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
928 <<" ["<<GetName()<<"]"<<std::endl);
929 DescriptorMapType::const_iterator i;
930 for (i=mDescriptorMap.begin();
931 i!=mDescriptorMap.end();
934 i->second->Check(true);
936 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
937 <<" ["<<GetName()<<"] ... OK"<<std::endl);
939 //===================================================================
942 //==========================================================================
943 /// Changes the name of a black box type
944 void Package::ChangeDescriptorName( const std::string& oldname, const std::string& newname )
946 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
947 <<">::ChangeDescriptorName(\""<<oldname
948 <<"\",\""<<newname<<"\")"<<std::endl);
949 // Looking into the bb map
950 DescriptorMapType::iterator i = mDescriptorMap.find(oldname);
951 if (i == mDescriptorMap.end())
953 bbtkDebugDecTab("kernel",8);
954 bbtkError("ChangeDescriptorName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
957 i->second->SetTypeName(newname);
958 mDescriptorMap[newname] = i->second;
959 mDescriptorMap.erase(i);
961 bbtkDebugDecTab("kernel",8);
963 //==========================================================================
967 //==========================================================================
968 void Package::PrintHelpListDescriptors(bool description, bool adaptors) const
970 unsigned int lmax = 0;
971 std::vector<std::string> names;
972 std::vector<std::string> kinds;
973 std::vector<std::string> descrs;
975 DescriptorMapType::const_iterator i;
976 for (i=mDescriptorMap.begin();
977 i!=mDescriptorMap.end();
981 ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) )
983 std::string name(" ");
984 name += i->second->GetTypeName();
985 names.push_back(name);
988 if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR )
990 kind = std::string("[A]");
992 else if ( i->second->GetKind() ==
993 BlackBoxDescriptor::DEFAULT_ADAPTOR )
995 kind = std::string("[DA]");
997 kinds.push_back(kind);
999 unsigned int l = name.size()+kind.size();
1000 if (l>lmax) lmax = l;
1006 descr += i->second->GetDescription();
1008 descrs.push_back(descr);
1014 offs.append(lmax+3,' ');
1015 std::vector<std::string>::iterator ni,ci,di;
1016 for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin();
1017 ni != names.end(); ++ni, ++ci, ++di)
1020 space.append(lmax - ni->size() - ci->size(),' ');
1021 bbtkMessage("help",1,*ni << space << *ci );
1023 unsigned int dmax = 75 - lmax;
1024 // while (d.size() > dmax )
1027 bbtkMessage("help",1,d.substr(0,dmax) << "..." << std::endl);
1029 bbtkMessage("help",1,d << std::endl);
1030 // d = d.substr(dmax,d.size());
1035 //==========================================================================
1037 //==========================================================================
1038 /// Displays the list of adaptors of the package
1039 void Package::PrintHelpListAdaptors(bool description) const
1041 DescriptorMapType::const_iterator i;
1042 for (i=mDescriptorMap.begin();
1043 i!=mDescriptorMap.end();
1046 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD )
1048 bbtkMessage("help",1,
1049 " "<<i->second->GetTypeName());
1050 if ( i->second->GetKind() ==
1051 BlackBoxDescriptor::DEFAULT_ADAPTOR )
1053 bbtkMessage("help",1,
1058 bbtkMessage("help",1,
1059 " : "<<i->second->GetDescription());
1062 bbtkMessage("help",1,std::endl);
1066 AdaptorMapType::const_iterator i;
1067 for (i=mAdaptorMap.begin();
1068 i!=mAdaptorMap.end();
1071 bbtkMessage("help",1,
1072 " "<<i->second->GetTypeName());
1075 bbtkMessage("help",1,
1076 " : "<<i->second->GetDescription());
1079 bbtkMessage("help",1,std::endl);
1083 //==========================================================================
1085 //==========================================================================
1086 /// Prints help on a black box descriptor
1087 void Package::PrintHelpDescriptor(const std::string& name, bool full) const
1089 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
1090 <<">::PrintHelpDescriptor(\""
1091 <<name<<"\")"<<bbtkendl);
1093 DescriptorMapType::const_iterator i = mDescriptorMap.find(name);
1094 if (i == mDescriptorMap.end())
1096 bbtkDebugDecTab("kernel",8);
1097 bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
1099 // bbtkMessage("help",1,"["<<GetName()<<"] ");
1100 i->second->GetHelp(full);
1101 bbtkDebugDecTab("kernel",8);
1104 //==========================================================================
1107 //==========================================================================
1108 /// Returns true iff the package contains the box of name boxname
1109 bool Package::ContainsDescriptor(const std::string& name) const
1111 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
1112 <<">::ContainsDescriptor(\""
1113 <<name<<"\")"<<bbtkendl);
1115 DescriptorMapType::const_iterator i = mDescriptorMap.find(name);
1116 if (i == mDescriptorMap.end())
1118 bbtkDebugDecTab("kernel",8);
1121 bbtkDebugDecTab("kernel",8);
1124 //==========================================================================
1128 //==========================================================================
1129 void Package::CreateHtmlPage(const std::string& filename,
1130 const std::string& caller,
1131 const std::string& source,
1132 const std::string& custom_header,
1133 const std::string& custom_title,
1136 bool relative_link ) const
1138 bbtkDebugMessageInc("kernel",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
1139 <<filename<<"\")"<<bbtkendl);
1141 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1133"<<std::endl;
1143 //---------------------
1146 s.open(filename.c_str());
1149 bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
1152 //----------------------
1154 std::string title = "BBTK Package "+GetName()+" "+GetVersion();
1156 if (custom_title.length() != 0) title = custom_title;
1158 s << "<html lang=\"en\">\n";
1160 s << "<title>" << title << "</title>\n";
1161 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
1162 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
1163 s << "<meta name=\"generator\" content=\"\">\n";
1164 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
1165 //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
1166 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
1167 s << "pre.display { font-family:inherit }\n";
1168 s << "pre.format { font-family:inherit }\n";
1169 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
1170 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
1171 s << "pre.smallexample { font-size:smaller }\n";
1172 s << "pre.smalllisp { font-size:smaller }\n";
1173 s << "span.sc { font-variant:small-caps }\n";
1174 s << "span.roman { font-family:serif; font-weight:normal; } \n";
1175 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
1176 s << "--></style>\n";
1178 //----------------------
1180 //----------------------
1183 s << "<a name=\"Top\"></a>\n";
1185 //----------------------
1187 if ( custom_header.length() != 0)
1189 if ( custom_header != "none" )
1192 in.open(custom_header.c_str());
1195 bbtkError("Could not open file \""<<custom_header<<"\"");
1200 in.getline(buffer,512);
1201 std::string line(buffer);
1208 s << "<object data=\"" << custom_header
1209 << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
1210 << custom_header <<" could not be embedded.</object>\n";
1216 s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
1217 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1218 s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1219 << GetDescription() << "</TD></TR>\n";
1220 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1221 << GetAuthor() << "</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 << GetCategory() << "</TD></TR>\n";
1224 s << "<TR><TD style='vertical-align: top;'><b> Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1225 << GetVersion() << "</TD></TR>\n";
1226 s << "<TR><TD style='vertical-align: top;'><b> bbtk Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1227 << bbtk::GetVersion() << "</TD></TR>\n";
1230 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1225"<<std::endl;
1231 //-------------------
1232 // Table of contents
1234 // s << "<div class=\"contents\">\n";
1235 s << "<p><b> Black Boxes : </b>\n";
1238 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1240 DescriptorMapType::const_iterator i;
1241 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1236"<<std::endl;
1242 for (i=mDescriptorMap.begin(); i!=mDescriptorMap.end(); ++i) {
1243 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD)
1246 std::string name = i->second->GetTypeName();
1247 Utilities::html_format(name);
1248 std::string descr = i->second->GetDescription();
1249 //Utilities::html_format(descr);
1250 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1246"<<std::endl;
1252 s << "<TD style='vertical-align: top;'>";
1253 s << " <a name=\"toc_"<<name
1254 <<"\" href=\"#"<<name<<"\">"
1257 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1266 //-------------------
1268 if (mAdaptorMap.size()>0)
1270 // s << "<div class=\"contents\">\n";
1271 s << "<p><b> Adaptors : </b>\n";
1273 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1268"<<std::endl;
1274 // DescriptorMapType::const_iterator i;
1275 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1276 for (i=mDescriptorMap.begin(); i!=mDescriptorMap.end();++i)
1278 if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD)
1281 std::string name = i->second->GetTypeName();
1282 Utilities::html_format(name);
1283 std::string descr = i->second->GetDescription();
1286 s << "<TD style='vertical-align: top;'>";
1287 s << " <a name=\"toc_"<<name
1288 <<"\" href=\"#"<<name<<"\">"
1291 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1301 // s << "<div class=\"node\">\n";
1303 // s << "<p><hr>\n";
1304 // s << "<a name=\"Top\"></a>\n";
1305 // s << "Top: <a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
1306 // s << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
1307 // s << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
1311 //----------------------
1314 //-------------------
1315 // Computes output directory from filename to pass it to
1316 // BlackBoxDescriptor::InsertHtmlHelp
1319 std::string::size_type slash_position = filename.find_last_of("/\\");
1322 if (slash_position != std::string::npos) {
1323 if (slash_position == 0)
1325 dir = filename.substr(0,slash_position);
1328 for (i=mDescriptorMap.begin();
1329 i!=mDescriptorMap.end();
1332 i->second->InsertHtmlHelp(s,detail,level,dir,relative_link);
1335 //----------------------
1340 ptm = gmtime ( &rawtime );
1343 s << "Automatically generated by <b>"<<caller<<"</b> "//from <b>"
1346 << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900
1347 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
1348 s << "</body></html>\n";
1350 //----------------------
1353 bbtkDebugDecTab("kernel",9);
1355 //==========================================================================
1357 //==========================================================================
1358 std::string Package::GetObjectName() const
1360 return std::string("Package '")+mName+std::string("'");
1362 //==========================================================================
1364 //==========================================================================
1365 std::string Package::GetObjectInfo() const
1367 std::stringstream i;
1368 i << " - "<<mDescriptorMap.size() << " boxes" << std::endl;
1369 if (mDynamicLibraryHandler)
1371 i<< " - Loaded from dynamic library"<<std::endl;
1375 //==========================================================================
1378 //==========================================================================
1379 size_t Package::GetObjectSize() const
1381 size_t s = Superclass::GetObjectSize();
1382 s += Package::GetObjectInternalSize();
1385 //==========================================================================
1386 //==========================================================================
1387 size_t Package::GetObjectInternalSize() const
1389 size_t s = sizeof(Package);
1392 //==========================================================================
1393 //==========================================================================
1394 size_t Package::GetObjectRecursiveSize() const
1396 size_t s = Superclass::GetObjectRecursiveSize();
1397 s += Package::GetObjectInternalSize();
1399 DescriptorMapType::const_iterator i;
1400 for (i = mDescriptorMap.begin(); i!=mDescriptorMap.end(); ++i )
1402 s += i->second->GetObjectRecursiveSize();
1406 //==========================================================================
1407 void Package::GetBoxesInside(NodeTreeC& tree, int cont)
1409 DescriptorMapType::const_iterator i;
1410 std::cout<<"*********a********"<<std::endl;
1411 for (i=mDescriptorMap.begin(); i!=mDescriptorMap.end(); ++i)
1413 i->second->GetBoxesInside(tree, cont);
1414 std::cout<<"*****************"<<std::endl;
1417 //==========================================================================
1418 //==========================================================================
1419 std::set<Package::WeakPointer>
1420 Package::mReleasedDynamicallyLoadedPackages;
1421 //==========================================================================