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<<"',...)"
90 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
91 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
92 std::string url = default_doc_dir;
93 if (c != '/' && c !='\\') url = url + "/";
94 url = url + "temp_dir/" + name + "/index.html";
97 SetDocRelativeURL("Relative url not set");
100 std::string relurl(BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH));
101 relurl += "/packages/"+name+"/bbdoc/index.html";
102 std::string url = ConfigurationFile::GetInstance().Get_url()
105 SetDocRelativeURL(relurl);
108 // std::cout << " url=["<<url<<"]"<<std::endl;
109 // std::cout << "relurl=["<<relurl<<"]"<<std::endl;
110 bbtkDebugMessage("object",2,"<== Package::Package('"<<name<<"',...) OK"
114 //==========================================================================
118 //==========================================================================
122 bbtkDebugMessage("object",2,"==> ~Package(\""<<mName<<"\")"<<bbtkendl);
124 //==========================================================================
127 //==========================================================================
128 void PackageReleaseBlackBoxDescriptorInternal(Package::WeakPointer pack,
129 const std::string& descname)
131 // Try to release descriptor
132 std::string packname = pack.lock()->GetName();
134 bbtkDebugMessage("package",5,"--- Releasing descriptor '"
135 <<packname<<"::"<<descname<<"'"<<bbtkendl);
138 Package::DescriptorMapType::iterator desc =
139 pack.lock()->GetDescriptorMap().find(descname);
140 if (desc == pack.lock()->GetDescriptorMap().end())
142 bbtkDebugMessage("package",5,
143 " Descriptor has already been released"
147 // bbtkDebugMessage("package",3,
148 // " Trying unreferencing it ... "<<std::endl);
149 BlackBoxDescriptor::WeakPointer pdesc = desc->second;
150 desc->second.reset();
151 // if it is dead : remove it
154 bbtkDebugMessage("package",2," ==> '"<<packname<<"::"<<descname<<"' Descriptor expired"<<bbtkendl);
157 bbtkDebugMessage("package",2,
158 " ... and caused its package death"
162 desc = pack.lock()->GetDescriptorMap().find(descname);
163 if (desc != pack.lock()->GetDescriptorMap().end()) pack.lock()->GetDescriptorMap().erase(desc);
164 } else { //pdesc.expired
165 bbtkDebugMessage("package",5," ... Descriptor still alive ("
166 <<pdesc.use_count()<<" refs)"
168 pack.lock()->GetDescriptorMap()[descname] = pdesc.lock();
171 //==========================================================================
174 //==========================================================================
176 void Package::Release(Package::WeakPointer pack)
178 std::string packname = pack.lock()->mName;
179 bbtkDebugMessage("package",1,"==> Package::Release('"<<
180 packname<<"')"<<bbtkendl);
182 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
183 long ndesc = pack.lock()->GetDescriptorMap().size();
184 long nrefs = pack.use_count();
186 bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
187 <<ndesc<<" descr / dyn="
190 // A package is "free" from any external reference iff :
191 // i) It is not dynamically loaded and nrefs == ndesc
192 // (each desc references its package) or
193 // ii) It is dynamically loaded and nrefs == ndesc + 1
194 // (A dynamic library holds a static pointer on the package it contains
195 // which is allocated when the PACKAGENAMEGetPackage() func is called,
196 // and descallocated (reset) by PACKAGENAMEDeletePackage())
197 if (nrefs == ndesc + dyn)
199 bbtkDebugMessage("package",5,
200 " -> No more external ref : checking descriptors"
202 // We must take care that removing refs on descriptors
203 // can lead to their deletion which can in turn unref
204 // internal boxes which can release their descriptors hence
205 // call Package::ReleaseBlackBoxDescriptor
206 // As a consequence during descriptors release :
207 // 1) The map can change dynamically : we cannot iterate over it
208 // as any iterator can become invalid
209 // 2) The package can auto-destruct : we must test its existence
210 // after each release
211 // We must also take care of not locking the package pointer
212 // or any ref count check in Package::ReleaseBlackBoxDescriptor
215 // The list of descriptors names at start
216 std::vector<std::string> descnamelist;
217 DescriptorMapType::iterator i;
218 for (i=pack.lock()->mDescriptorMap.begin();
219 i!= pack.lock()->mDescriptorMap.end();
221 descnamelist.push_back(i->first);
223 // Iterator over the initial names
224 std::vector<std::string>::iterator descname;
225 for (descname=descnamelist.begin();
226 descname!=descnamelist.end();
229 // Is package still alive ?
232 bbtkDebugMessage("package",1,"--- Package::Release('"<<
234 <<"') : package expired during release : bailing out"<<bbtkendl);
239 BlackBoxDescriptor::Pointer desc = pack.lock()->mDescriptorMap[*descname];
240 if ( (dyn==0) || (boost::dynamic_pointer_cast<ComplexBlackBoxDescriptor>(desc)) )
242 PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
245 PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
251 UnLoadDynamicLibrary(pack);
252 // Unload orphan dl packages
253 Package::UnLoadReleasedDynamicallyLoadedPackages();
257 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
259 bbtkDebugMessage("package",2,"<== Package::Release('"<<
260 packname<<"')"<<bbtkendl);
264 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
265 long ndesc = pack.lock()->GetDescriptorMap().size();
266 long nrefs = pack.use_count();
268 bbtkDebugMessage("package",1," ... Package still alive ("
270 <<ndesc<<" descr / dyn="
271 <<dyn<<")"<<std::endl);
275 bbtkDebugMessage("package",1," ... Package has been released"
280 //==========================================================================
282 //==========================================================================
283 /// "Releases" the package
284 /// Signals the package that it can free the given descriptor
285 /// if they are no more used and free itself if it is no
287 /// Note : Any non-weak pointer on the package must have been freed
288 void Package::ReleaseBlackBoxDescriptor(Package::WeakPointer pack,
289 BlackBoxDescriptor::WeakPointer descr)
291 std::string packname = pack.lock()->mName;
292 std::string dname = descr.lock()->GetTypeName();
293 bbtkDebugMessage("package",3,"==> Package::ReleaseBlackBoxDescriptor('"<<
294 packname<<"','"<<dname<<"') : refs="
295 <<descr.use_count()<<bbtkendl);
297 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
298 long ndesc = pack.lock()->GetDescriptorMap().size();
299 long nrefs = pack.use_count();
301 bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
302 <<ndesc<<" descr / dynamically loaded = "
305 // A package is "free" from any external reference iff :
306 // i) It is not dynamically loaded and nrefs == ndesc
307 // (each desc references its package) or
308 // ii) It is dynamically loaded and nrefs == ndesc + 1
309 // (A dynamic library holds a static pointer on the package it contains
310 // which is allocated when the PACKAGENAMEGetPackage() func is called,
311 // and descallocated (reset) by PACKAGENAMEDeletePackage())
312 if (nrefs == ndesc + dyn)
317 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
319 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
323 // If the package is released and dynamically loaded
324 // then put it in the static list mReleasedDynamicallyLoadedPackages
325 UnLoadDynamicLibrary(pack,false);
327 bbtkDebugMessage("package",4,"<== Package::ReleaseBlackBoxDescriptor('"<<
328 packname<<"','"<<dname<<"'): refs="
329 <<descr.use_count()<<bbtkendl);
333 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
334 long ndesc = pack.lock()->GetDescriptorMap().size();
335 long nrefs = pack.use_count();
337 bbtkDebugMessage("package",3," ... Package still alive ("
339 <<ndesc<<" descr / dyn="
340 <<dyn<<")"<<std::endl);
344 bbtkDebugMessage("package",3," ... Package has been released"
349 //==========================================================================
351 //==========================================================================
352 /// Opens a dynamic library which contains a bbtk package
353 /// Returns the handler
354 /// Load the package management symbols from the lib
355 /// returns false if a problem occured hence can be used
356 /// to test that a dyn lib is a valid bbtk package lib
357 /// NB : The BBTK version exported from the library
358 /// is tested against the current bbtk version
359 DynamicLibraryHandler Package::OpenDynamicLibrary( const std::string& libname,const std::string& package_name,
360 DLGetPackageFunction& getpack, DLDeletePackageFunction& delpack)
362 bbtkDebugMessage("package",3,"==> Package::OpenDynamicLibrary("
363 <<libname<<")"<<std::endl);
364 #if defined(__GNUC__)
368 handler = dlopen(libname.c_str(), BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
369 //EED handler = dlopen(libname.c_str(), RTLD_LAZY | RTLD_LOCAL );
373 bbtkMessage("package",0,
374 "BBTK ..ERROR.. loading could not open shared library [" <<libname<<"] : "
375 <<dlerror() << std::endl);
379 bbtkDebugMessage("package",3,"* Shared lib ["<<libname<<"] open"<<std::endl);
381 // Loads the Package bbtk version function
382 std::string getvername(package_name);
383 getvername += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
384 DLGetPackageBBTKVersionFunction getbbtkversion = (DLGetPackageBBTKVersionFunction)(dlsym(handler,getvername.c_str()));
387 bbtkDebugMessage("package",3,"***"<<std::endl);
388 bbtkMessage("package",0,
389 "BBTK ..ERROR.. loading shared library ["<<libname
390 <<"] is not a valid bbtk package."
391 <<" Symbol ["<<getvername<<"] :"<<dlerror()<< std::endl);
396 bbtkDebugMessage("package",3,"* Symbol ["<<getvername
397 <<"] found"<<std::endl);
399 if (getbbtkversion() != bbtk::GetVersion())
401 bbtkMessage("package",0,
402 "BBTK ..ERROR.. loading: "<<package_name
403 <<" - Shared library ["<<libname
404 <<"] was build with bbtk version "
406 <<" but the current program runs with version "
407 <<bbtk::GetVersion()<<" : cannot load it. You have to recompile your BBTK-Package."<<std::endl);
413 bbtkDebugMessage("package",3,"* Package bbtk version '"<<getbbtkversion()<<"' matches"<<std::endl);
414 // Loads the Package get function
415 std::string getpackname(package_name);
416 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
417 getpack = (DLGetPackageFunction)(dlsym(handler, getpackname.c_str()));
420 bbtkMessage("package",0,
421 "BBTK ..ERROR.. loading shared library ["<<libname
422 <<"] is not a valid bbtk package."
423 <<" Symbol ["<<getpackname<<"] :"<<dlerror()<< std::endl);
428 bbtkDebugMessage("package",3,"* Symbol ["<<getpackname<<"] found"<<std::endl);
429 // Loads the Package delete function
431 std::string delpackname(package_name);
432 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
433 delpack = (DLDeletePackageFunction)(dlsym(handler, delpackname.c_str()));
436 bbtkMessage("package",0,
437 "BBTK ..ERROR.. loading shared library ["<<libname
438 <<"] is not a valid bbtk package."
439 <<" Symbol ["<<delpackname<<"] :"<<dlerror()<< std::endl);
443 bbtkDebugMessage("package",3,"* Symbol ["<<delpackname<<"] found"<<std::endl);
444 #elif defined(_WIN32)
450 handler = LoadLibrary(libname.c_str());
453 bbtkMessage("package",0,
454 "BBTK ..ERROR.. could not open shared library [" <<libname<<"]"
456 DWORD dwErrorCode = 0;
457 dwErrorCode = GetLastError();
458 bbtkMessage("package",2,
459 "Windows Error: [" << dwErrorCode <<"]"
465 // Loads the Package bbtk version function
466 std::string getvername(package_name);
467 getvername += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
468 DLGetPackageBBTKVersionFunction getbbtkversion = (DLGetPackageBBTKVersionFunction)(GetProcAddress(handler, getvername.c_str()));
472 FreeLibrary(handler);
473 bbtkMessage("package",0,
474 "BBTK ..ERROR.. loading shared library ["<<libname
475 <<"] is not a valid bbtk package."
476 <<" Symbol ["<<getbbtkversion<<"] not found"<< std::endl);
481 if (getbbtkversion() != bbtk::GetVersion())
483 FreeLibrary(handler);
484 bbtkMessage("package",0,
485 "BBTK ..ERROR.. loading: "<<package_name
486 <<" - Shared library ["<<libname
487 <<"] was build with bbtk version "
489 <<" but the current program runs with version "
490 <<bbtk::GetVersion()<<" : cannot load it. You have to recompile your BBTK-Package."<<std::endl);
494 // Loads the Package get function
495 std::string getpackname(package_name);
496 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
497 getpack = (DLGetPackageFunction)(GetProcAddress(handler, getpackname.c_str()));
500 FreeLibrary(handler);
501 bbtkMessage("package",0,
502 "BBTK ..ERROR.. loading shared library ["<<libname
503 <<"] is not a valid bbtk package."
504 <<" Symbol ["<<getpackname<<"] not found"<< std::endl);
508 // Loads the Package delete function
509 std::string delpackname(package_name);
510 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
511 delpack = (DLDeletePackageFunction)(GetProcAddress(handler, delpackname.c_str()));
514 FreeLibrary(handler);
515 bbtkMessage("package",0,
516 "BBTK ..ERROR.. loading shared library ["<<libname
517 <<"] is not a valid bbtk package."
518 <<" Symbol ["<<delpackname<<"] not found"<< std::endl);
523 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
528 //==========================================================================
530 //==========================================================================
531 /// Loads a package from a dynamic library
532 Package::Pointer Package::CreateFromDynamicLibrary(const std::string& libname,
533 const std::string& pkgname,
534 const std::string& path)
536 bbtkDebugMessage("package",1,"==> Package::CreateFromDynamicLibrary("
537 <<libname<<")"<<std::endl);
539 DLGetPackageFunction gf;
540 DLDeletePackageFunction df;
541 DynamicLibraryHandler h = Package::OpenDynamicLibrary(libname,
544 if (h==0) return Package::Pointer();
545 Package::Pointer p = gf();
546 p->mDynamicLibraryHandler = h;
547 p->mDLDeletePackageFunction = df;
549 std::string separator =
550 ConfigurationFile::GetInstance().Get_file_separator ();
551 //BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
552 std::string docreldoc =
553 separator + "bbdoc" + separator + pkgname + separator + "index.html";
555 ".." + separator + ".." + docreldoc;
556 std::string doc = path + separator + ".." + separator
557 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
561 p->SetDocRelativeURL(reldoc);
563 bbtkDebugMessage("package",2,"<== Package::CreateFromDynamicLibrary("
564 <<libname<<") .. OK"<<std::endl);
567 //==========================================================================
572 //==========================================================================
573 /// UnLoads the package dynamic library (if any)
574 void Package::UnLoadDynamicLibrary(Package::WeakPointer pack, bool doit)
576 if (pack.expired() || (!pack.lock()->mDynamicLibraryHandler))
579 std::string packname = pack.lock()->GetName();
580 bbtkDebugMessage("package",5,"==> Package::UnLoadDynamicLibrary('"
584 if (!pack.lock()->GetDescriptorMap().empty())
587 bbtkDebugMessage("package",5," Package not empty ... abort"
591 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
592 <<"DescriptorMap not empty "
593 <<BBTK_INTERNAL_ERROR_MESSAGE);
597 if (pack.use_count()!=1)
599 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
600 <<"empty dl package with external refs"
601 <<BBTK_INTERNAL_ERROR_MESSAGE);
607 bbtkDebugMessage("package",5,"==> dynamic library for package '"
608 <<packname<<"' closed"
613 mReleasedDynamicallyLoadedPackages.insert(pack);
614 bbtkDebugMessage("package",1,"==> package '"<<packname
615 <<"' put in the 'to unload' list"
619 bbtkDebugMessage("package",5,"<== Package::UnLoadDynamicLibrary('"
623 //==========================================================================
625 //==========================================================================
626 /// UnLoads released packages that were loaded dynamically
627 /// see UnLoadDynamicLibrary and ReleaseBlackBoxDescriptor
628 void Package::UnLoadReleasedDynamicallyLoadedPackages()
630 bbtkDebugMessage("package",5,"==> Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
632 std::set<Package::WeakPointer>::iterator i;
635 if(mReleasedDynamicallyLoadedPackages.size()>0){
636 for (i=mReleasedDynamicallyLoadedPackages.begin();
637 i!=mReleasedDynamicallyLoadedPackages.end();
640 if (!i->expired()) UnLoad(*i);
644 bbtkDebugMessage("package",5,"<== Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
646 //==========================================================================
648 //==========================================================================
649 void Package::UnLoad(Package::WeakPointer pack)
651 std::string packname = pack.lock()->GetName();
652 bbtkDebugMessage("package",6,"==> Package::UnLoad("<<packname<<")"<<std::endl);
654 Package* p = pack.lock().get();
656 DynamicLibraryHandler h = p->mDynamicLibraryHandler;
658 // deletes the package
659 p->mDLDeletePackageFunction();
661 // closes the dl handler
662 #if defined(__GNUC__)
665 printf("EED Package::UnLoad ERROR %s\n", packname.c_str() );
666 bbtkWarning("Failed to close dynamic library for package '"<<packname
670 #elif defined(_WIN32)
674 bbtkDebugMessage("package",1,"==> dynamic library for package '"
675 <<packname<<"' closed"
677 bbtkDebugMessage("package",6," ... dynamic library unloaded"<<std::endl);
679 //==========================================================================
681 bool Package::ifBoxExist( std::string type)
684 DescriptorMapType::const_iterator i = mDescriptorMap.find(type);
685 if (i != mDescriptorMap.end())
692 //==========================================================================
693 /// Creates an instance of a black box of type <type> with name <name>
694 BlackBox::Pointer Package::NewBlackBox(const std::string& type,
695 const std::string& name) const
697 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::NewBlackBox(\""<<type<<"\",\""<<name<<"\")"<<bbtkendl);
699 DescriptorMapType::const_iterator i = mDescriptorMap.find(type);
700 if (i == mDescriptorMap.end())
702 bbtkDebugDecTab("kernel",8);
703 return BlackBox::Pointer();
705 BlackBox::Pointer bb =i->second->NewBlackBox(name);
706 bbtkDebugDecTab("kernel",8);
710 //==========================================================================
714 //==========================================================================
715 /// Creates an instance of an adaptor of input type <typein> and
716 /// output type <typeout> with name <name>
717 BlackBox::Pointer Package::NewAdaptor(const DataInfo& typein,
718 const DataInfo& typeout,
719 const std::string& name) const
721 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
725 <<name<<"\")"<<bbtkendl);
727 AdaptorKey key(typein,typeout,
728 BlackBoxDescriptor::DEFAULT_ADAPTOR);
729 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
730 if (i == mAdaptorMap.end())
732 bbtkDebugDecTab("kernel",8);
733 return BlackBox::Pointer();
735 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
736 bbtkDebugDecTab("kernel",8);
740 //==========================================================================
742 //==========================================================================
743 /// Creates an instance of an adaptor of input type <typein> and
744 /// output type <typeout> with name <name>
745 BlackBox::Pointer Package::NewWidgetAdaptor(const DataInfo& typein,
746 const DataInfo& typeout,
747 const std::string& name) const
749 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
750 ">::NewWidgetAdaptor("
753 <<name<<"\")"<<bbtkendl);
755 AdaptorKey key(typein,typeout,
756 BlackBoxDescriptor::DEFAULT_GUI);
757 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
758 if (i == mAdaptorMap.end())
760 bbtkDebugDecTab("kernel",8);
761 return BlackBox::Pointer();
763 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
764 bbtkDebugDecTab("kernel",8);
768 //==========================================================================
772 //==========================================================================
773 /// Returns true is the package contains
774 /// an adaptor of input type <typein> and
775 /// output type <typeout>
776 /// If successfull then adaptor contains the black box type name
777 bool Package::FindWidgetAdaptor(const DataInfo& typein,
778 const DataInfo& typeout,
779 std::string& adaptor) const
781 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
782 ">::FindWidgetAdaptor("
784 <<typeout<<")"<<bbtkendl);
786 AdaptorKey key(/*typein*/
787 DataInfo(typeid(void),""),
789 BlackBoxDescriptor::DEFAULT_GUI);
790 // First try to find a single widget adaptor
791 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
792 if (i == mAdaptorMap.end())
794 bbtkDebugDecTab("kernel",8);
797 adaptor = i->second.lock()->GetTypeName();
798 bbtkDebugDecTab("kernel",8);
802 //==========================================================================
806 //==========================================================================
807 /// Returns true is the package contains
808 /// an adaptor of input type <typein> and
809 /// output type <typeout>
810 /// If successfull then adaptor contains the black box type name
811 bool Package::FindAdaptor(const DataInfo& typein,
812 const DataInfo& typeout,
813 std::string& adaptor) const
815 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
818 <<typeout<<")"<<bbtkendl);
820 AdaptorKey key(typein,typeout,
821 BlackBoxDescriptor::DEFAULT_ADAPTOR);
822 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
823 if (i == mAdaptorMap.end())
825 bbtkDebugDecTab("kernel",8);
828 adaptor = i->second.lock()->GetTypeName();
829 bbtkDebugDecTab("kernel",8);
833 //==========================================================================
836 //==========================================================================
837 /// Registers a black box descriptor in the package
838 bool Package::Register(BlackBoxDescriptor::Pointer d)
840 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\")"<<std::endl);
842 DescriptorMapType::iterator i = mDescriptorMap.find(d->GetTypeName());
843 if (i!=mDescriptorMap.end())
845 bbtkWarning("Package<"<<GetName()<<"> : Trying to register box type <"
846 <<d->GetTypeName()<<"> which is already in the package");
850 mDescriptorMap[d->GetTypeName()] = d;
852 d->SetPackage(GetThisPointer<Package>());
854 // If it is a default adaptor, also register it in the adaptors map
855 if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR )
857 bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\") : The box is an adaptor, inserting it in adaptors map ..."<<std::endl);
859 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
860 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
861 DataInfo infoin(typein,d->GetInputDescriptor("In")->GetNature());
862 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
863 AdaptorKey key(infoin,infoout,d->GetKind());
865 AdaptorMapType::const_iterator i;
866 i = mAdaptorMap.find(key);
867 if (i == mAdaptorMap.end())
869 mAdaptorMap[key] = d;
871 // If already an adaptor registered : error
874 if (i->second.lock()->GetTypeName() != d->GetTypeName())
876 bbtkError("Package <"<<GetName()<<
877 "> : trying to register black box <"
879 <<"> as default adaptor but there is already a default adaptor registered (<"
880 <<i->second.lock()->GetTypeName()<<">)");
884 // If it is a default adaptor, also register it in the adaptors map
885 else if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_GUI)
887 bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\") : The box is a widget adaptor, inserting it in adaptors map ..."<<std::endl);
889 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
890 DataInfo infoin(typeid(void),"");
891 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
892 AdaptorKey key(infoin,infoout,d->GetKind());
894 AdaptorMapType::const_iterator i;
895 i = mAdaptorMap.find(key);
896 if (i == mAdaptorMap.end())
898 mAdaptorMap[key] = d;
900 // If already an adaptor registered : error
903 if (i->second.lock()->GetTypeName() != d->GetTypeName())
905 bbtkError("Package <"<<GetName()<<
906 "> : trying to register black box <"
908 <<"> as default widget adaptor but there is already a default adaptor registered (<"
909 <<i->second.lock()->GetTypeName()<<">)");
915 bbtkDebugDecTab("kernel",8);
919 //==========================================================================
921 //===================================================================
922 void Package::Check() const
924 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
925 <<" ["<<GetName()<<"]"<<std::endl);
926 DescriptorMapType::const_iterator i;
927 for (i=mDescriptorMap.begin();
928 i!=mDescriptorMap.end();
931 i->second->Check(true);
933 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
934 <<" ["<<GetName()<<"] ... OK"<<std::endl);
936 //===================================================================
939 //==========================================================================
940 /// Changes the name of a black box type
941 void Package::ChangeDescriptorName( const std::string& oldname, const std::string& newname )
943 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
944 <<">::ChangeDescriptorName(\""<<oldname
945 <<"\",\""<<newname<<"\")"<<std::endl);
946 // Looking into the bb map
947 DescriptorMapType::iterator i = mDescriptorMap.find(oldname);
948 if (i == mDescriptorMap.end())
950 bbtkDebugDecTab("kernel",8);
951 bbtkError("ChangeDescriptorName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
954 i->second->SetTypeName(newname);
955 mDescriptorMap[newname] = i->second;
956 mDescriptorMap.erase(i);
958 bbtkDebugDecTab("kernel",8);
960 //==========================================================================
964 //==========================================================================
965 void Package::PrintHelpListDescriptors(bool description, bool adaptors) const
967 unsigned int lmax = 0;
968 std::vector<std::string> names;
969 std::vector<std::string> kinds;
970 std::vector<std::string> descrs;
972 DescriptorMapType::const_iterator i;
973 for (i=mDescriptorMap.begin();
974 i!=mDescriptorMap.end();
978 ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) )
980 std::string name(" ");
981 name += i->second->GetTypeName();
982 names.push_back(name);
985 if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR )
987 kind = std::string("[A]");
989 else if ( i->second->GetKind() ==
990 BlackBoxDescriptor::DEFAULT_ADAPTOR )
992 kind = std::string("[DA]");
994 kinds.push_back(kind);
996 unsigned int l = name.size()+kind.size();
997 if (l>lmax) lmax = l;
1003 descr += i->second->GetDescription();
1005 descrs.push_back(descr);
1011 offs.append(lmax+3,' ');
1012 std::vector<std::string>::iterator ni,ci,di;
1013 for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin();
1014 ni != names.end(); ++ni, ++ci, ++di)
1017 space.append(lmax - ni->size() - ci->size(),' ');
1018 bbtkMessage("help",1,*ni << space << *ci );
1020 unsigned int dmax = 75 - lmax;
1021 // while (d.size() > dmax )
1024 bbtkMessage("help",1,d.substr(0,dmax) << "..." << std::endl);
1026 bbtkMessage("help",1,d << std::endl);
1027 // d = d.substr(dmax,d.size());
1032 //==========================================================================
1034 //==========================================================================
1035 /// Displays the list of adaptors of the package
1036 void Package::PrintHelpListAdaptors(bool description) const
1038 DescriptorMapType::const_iterator i;
1039 for (i=mDescriptorMap.begin();
1040 i!=mDescriptorMap.end();
1043 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD )
1045 bbtkMessage("help",1,
1046 " "<<i->second->GetTypeName());
1047 if ( i->second->GetKind() ==
1048 BlackBoxDescriptor::DEFAULT_ADAPTOR )
1050 bbtkMessage("help",1,
1055 bbtkMessage("help",1,
1056 " : "<<i->second->GetDescription());
1059 bbtkMessage("help",1,std::endl);
1063 AdaptorMapType::const_iterator i;
1064 for (i=mAdaptorMap.begin();
1065 i!=mAdaptorMap.end();
1068 bbtkMessage("help",1,
1069 " "<<i->second->GetTypeName());
1072 bbtkMessage("help",1,
1073 " : "<<i->second->GetDescription());
1076 bbtkMessage("help",1,std::endl);
1080 //==========================================================================
1082 //==========================================================================
1083 /// Prints help on a black box descriptor
1084 void Package::PrintHelpDescriptor(const std::string& name, bool full) const
1086 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
1087 <<">::PrintHelpDescriptor(\""
1088 <<name<<"\")"<<bbtkendl);
1090 DescriptorMapType::const_iterator i = mDescriptorMap.find(name);
1091 if (i == mDescriptorMap.end())
1093 bbtkDebugDecTab("kernel",8);
1094 bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
1096 // bbtkMessage("help",1,"["<<GetName()<<"] ");
1097 i->second->GetHelp(full);
1098 bbtkDebugDecTab("kernel",8);
1101 //==========================================================================
1104 //==========================================================================
1105 /// Returns true iff the package contains the box of name boxname
1106 bool Package::ContainsDescriptor(const std::string& name) const
1108 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
1109 <<">::ContainsDescriptor(\""
1110 <<name<<"\")"<<bbtkendl);
1112 DescriptorMapType::const_iterator i = mDescriptorMap.find(name);
1113 if (i == mDescriptorMap.end())
1115 bbtkDebugDecTab("kernel",8);
1118 bbtkDebugDecTab("kernel",8);
1121 //==========================================================================
1125 //==========================================================================
1126 void Package::CreateHtmlPage(const std::string& filename,
1127 const std::string& caller,
1128 const std::string& source,
1129 const std::string& custom_header,
1130 const std::string& custom_title,
1133 bool relative_link ) const
1135 bbtkDebugMessageInc("kernel",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
1136 <<filename<<"\")"<<bbtkendl);
1138 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1133"<<std::endl;
1140 //---------------------
1143 s.open(filename.c_str());
1146 bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
1149 //----------------------
1151 std::string title = "BBTK Package "+GetName()+" "+GetVersion();
1153 if (custom_title.length() != 0) title = custom_title;
1155 s << "<html lang=\"en\">\n";
1157 s << "<title>" << title << "</title>\n";
1158 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
1159 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
1160 s << "<meta name=\"generator\" content=\"\">\n";
1161 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
1162 //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
1163 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
1164 s << "pre.display { font-family:inherit }\n";
1165 s << "pre.format { font-family:inherit }\n";
1166 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
1167 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
1168 s << "pre.smallexample { font-size:smaller }\n";
1169 s << "pre.smalllisp { font-size:smaller }\n";
1170 s << "span.sc { font-variant:small-caps }\n";
1171 s << "span.roman { font-family:serif; font-weight:normal; } \n";
1172 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
1173 s << "--></style>\n";
1175 //----------------------
1177 //----------------------
1180 s << "<a name=\"Top\"></a>\n";
1182 //----------------------
1184 if ( custom_header.length() != 0)
1186 if ( custom_header != "none" )
1189 in.open(custom_header.c_str());
1192 bbtkError("Could not open file \""<<custom_header<<"\"");
1197 in.getline(buffer,512);
1198 std::string line(buffer);
1205 s << "<object data=\"" << custom_header
1206 << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
1207 << 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 //==========================================================================
1408 //==========================================================================
1409 std::set<Package::WeakPointer>
1410 Package::mReleasedDynamicallyLoadedPackages;
1411 //==========================================================================