2 /*=========================================================================
4 Module: $RCSfile: bbtkPackage.cxx,v $
6 Date: $Date: 2009/05/28 14:22:10 $
7 Version: $Revision: 1.29 $
8 =========================================================================*/
10 /* ---------------------------------------------------------------------
12 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
13 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
15 * This software is governed by the CeCILL-B license under French law and
16 * abiding by the rules of distribution of free software. You can use,
17 * modify and/ or redistribute the software under the terms of the CeCILL-B
18 * license as circulated by CEA, CNRS and INRIA at the following URL
19 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
20 * or in the file LICENSE.txt.
22 * As a counterpart to the access to the source code and rights to copy,
23 * modify and redistribute granted by the license, users are provided only
24 * with a limited warranty and the software's author, the holder of the
25 * economic rights, and the successive licensors have only limited
28 * The fact that you are presently reading this means that you have had
29 * knowledge of the CeCILL-B license and that you accept its terms.
30 * ------------------------------------------------------------------------ */
34 *\brief Class bbtk::Package : registers black boxes descriptors and is able to create instances of the black boxes registered.
36 #include "bbtkPackage.h"
37 #include "bbtkComplexBlackBoxDescriptor.h"
38 #include "bbtkMessageManager.h"
39 #include "bbtkConfigurationFile.h"
42 #include "bbtkUtilities.h"
49 //==========================================================================
50 /// Creates a new package
51 Package::Pointer Package::New(const std::string& name,
52 const std::string& author,
53 const std::string& description,
54 const std::string& version)
56 bbtkDebugMessage("object",1,"##> Package::New('"<<name<<"',...)"
58 Package::Pointer p = MakePointer(new Package(name,
62 bbtkDebugMessage("object",2,"<## Package::New('"<<name<<"',...)"
66 //==========================================================================
68 //==========================================================================
69 /// Ctor with the name of the package
70 Package::Package(const std::string& name,
71 const std::string& author,
72 const std::string& description,
73 const std::string& version)
75 mDynamicLibraryHandler(0),
78 mDescription(description),
81 bbtkDebugMessage("object",2,"==> Package('"<<name<<"',...)"
83 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
84 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
85 std::string url = default_doc_dir;
86 if (c != '/' && c !='\\') url = url + "/";
87 url = url + "temp_dir/" + name + "/index.html";
90 SetDocRelativeURL("Relative url not set");
93 std::string relurl(BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH));
94 relurl += "/packages/"+name+"/bbdoc/index.html";
95 std::string url = ConfigurationFile::GetInstance().Get_url()
98 SetDocRelativeURL(relurl);
101 // std::cout << " url=["<<url<<"]"<<std::endl;
102 // std::cout << "relurl=["<<relurl<<"]"<<std::endl;
103 bbtkDebugMessage("object",2,"<== Package::Package('"<<name<<"',...) OK"
107 //==========================================================================
111 //==========================================================================
115 bbtkDebugMessage("object",2,"==> ~Package(\""<<mName<<"\")"<<bbtkendl);
117 //==========================================================================
120 //==========================================================================
121 void PackageReleaseBlackBoxDescriptorInternal(Package::WeakPointer pack,
122 const std::string& descname)
124 // Try to release descriptor
125 std::string packname = pack.lock()->GetName();
127 bbtkDebugMessage("package",5,"--- Releasing descriptor '"
128 <<packname<<"::"<<descname<<"'"<<bbtkendl);
131 Package::BlackBoxMapType::iterator desc =
132 pack.lock()->GetBlackBoxMap().find(descname);
133 if (desc == pack.lock()->GetBlackBoxMap().end())
135 bbtkDebugMessage("package",5,
136 " Descriptor has already been released"
140 // bbtkDebugMessage("package",3,
141 // " Trying unreferencing it ... "<<std::endl);
142 BlackBoxDescriptor::WeakPointer pdesc = desc->second;
143 desc->second.reset();
144 // if it is dead : remove it
147 bbtkDebugMessage("package",2," ==> '"<<packname<<"::"<<descname<<"' Descriptor expired"<<bbtkendl);
150 bbtkDebugMessage("package",2,
151 " ... and caused its package death"
155 desc = pack.lock()->GetBlackBoxMap().find(descname);
156 if (desc != pack.lock()->GetBlackBoxMap().end()) pack.lock()->GetBlackBoxMap().erase(desc);
157 } else { //pdesc.expired
158 bbtkDebugMessage("package",5," ... Descriptor still alive ("
159 <<pdesc.use_count()<<" refs)"
161 pack.lock()->GetBlackBoxMap()[descname] = pdesc.lock();
164 //==========================================================================
167 //==========================================================================
169 void Package::Release(Package::WeakPointer pack)
171 std::string packname = pack.lock()->mName;
172 bbtkDebugMessage("package",1,"==> Package::Release('"<<
173 packname<<"')"<<bbtkendl);
175 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
176 long ndesc = pack.lock()->GetBlackBoxMap().size();
177 long nrefs = pack.use_count();
179 bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
180 <<ndesc<<" descr / dyn="
183 // A package is "free" from any external reference iff :
184 // i) It is not dynamically loaded and nrefs == ndesc
185 // (each desc references its package) or
186 // ii) It is dynamically loaded and nrefs == ndesc + 1
187 // (A dynamic library holds a static pointer on the package it contains
188 // which is allocated when the PACKAGENAMEGetPackage() func is called,
189 // and descallocated (reset) by PACKAGENAMEDeletePackage())
190 if (nrefs == ndesc + dyn)
192 bbtkDebugMessage("package",5,
193 " -> No more external ref : checking descriptors"
195 // We must take care that removing refs on descriptors
196 // can lead to their deletion which can in turn unref
197 // internal boxes which can release their descriptors hence
198 // call Package::ReleaseBlackBoxDescriptor
199 // As a consequence during descriptors release :
200 // 1) The map can change dynamically : we cannot iterate over it
201 // as any iterator can become invalid
202 // 2) The package can auto-destruct : we must test its existence
203 // after each release
204 // We must also take care of not locking the package pointer
205 // or any ref count check in Package::ReleaseBlackBoxDescriptor
208 // The list of descriptors names at start
209 std::vector<std::string> descnamelist;
210 BlackBoxMapType::iterator i;
211 for (i=pack.lock()->mBlackBoxMap.begin();
212 i!= pack.lock()->mBlackBoxMap.end();
214 descnamelist.push_back(i->first);
216 // Iterator over the initial names
217 std::vector<std::string>::iterator descname;
218 for (descname=descnamelist.begin();
219 descname!=descnamelist.end();
222 // Is package still alive ?
225 bbtkDebugMessage("package",1,"--- Package::Release('"<<
227 <<"') : package expired during release : bailing out"<<bbtkendl);
232 BlackBoxDescriptor::Pointer desc = pack.lock()->mBlackBoxMap[*descname];
233 if ( (dyn==0) || (boost::dynamic_pointer_cast<ComplexBlackBoxDescriptor>(desc)) )
234 PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
236 PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
242 UnLoadDynamicLibrary(pack);
243 // Unload orphan dl packages
244 Package::UnLoadReleasedDynamicallyLoadedPackages();
251 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
253 bbtkDebugMessage("package",2,"<== Package::Release('"<<
254 packname<<"')"<<bbtkendl);
258 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
259 long ndesc = pack.lock()->GetBlackBoxMap().size();
260 long nrefs = pack.use_count();
262 bbtkDebugMessage("package",1," ... Package still alive ("
264 <<ndesc<<" descr / dyn="
265 <<dyn<<")"<<std::endl);
269 bbtkDebugMessage("package",1," ... Package has been released"
274 //==========================================================================
276 //==========================================================================
277 /// "Releases" the package
278 /// Signals the package that it can free the given descriptor
279 /// if they are no more used and free itself if it is no
281 /// Note : Any non-weak pointer on the package must have been freed
282 void Package::ReleaseBlackBoxDescriptor(Package::WeakPointer pack,
283 BlackBoxDescriptor::WeakPointer descr)
285 std::string packname = pack.lock()->mName;
286 std::string dname = descr.lock()->GetTypeName();
287 bbtkDebugMessage("package",3,"==> Package::ReleaseBlackBoxDescriptor('"<<
288 packname<<"','"<<dname<<"') : refs="
289 <<descr.use_count()<<bbtkendl);
291 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
292 long ndesc = pack.lock()->GetBlackBoxMap().size();
293 long nrefs = pack.use_count();
295 bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
296 <<ndesc<<" descr / dynamically loaded = "
299 // A package is "free" from any external reference iff :
300 // i) It is not dynamically loaded and nrefs == ndesc
301 // (each desc references its package) or
302 // ii) It is dynamically loaded and nrefs == ndesc + 1
303 // (A dynamic library holds a static pointer on the package it contains
304 // which is allocated when the PACKAGENAMEGetPackage() func is called,
305 // and descallocated (reset) by PACKAGENAMEDeletePackage())
306 if (nrefs == ndesc + dyn)
311 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
313 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
317 // If the package is released and dynamically loaded
318 // then put it in the static list mReleasedDynamicallyLoadedPackages
319 UnLoadDynamicLibrary(pack,false);
321 bbtkDebugMessage("package",4,"<== Package::ReleaseBlackBoxDescriptor('"<<
322 packname<<"','"<<dname<<"'): refs="
323 <<descr.use_count()<<bbtkendl);
327 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
328 long ndesc = pack.lock()->GetBlackBoxMap().size();
329 long nrefs = pack.use_count();
331 bbtkDebugMessage("package",3," ... Package still alive ("
333 <<ndesc<<" descr / dyn="
334 <<dyn<<")"<<std::endl);
338 bbtkDebugMessage("package",3," ... Package has been released"
343 //==========================================================================
345 //==========================================================================
346 /// Opens a dynamic library which contains a bbtk package
347 /// Returns the handler
348 /// Load the package management symbols from the lib
349 /// returns false if a problem occured hence can be used
350 /// to test that a dyn lib is a valid bbtk package lib
351 /// NB : The BBTK version exported from the library
352 /// is tested against the current bbtk version
353 DynamicLibraryHandler Package::OpenDynamicLibrary
354 ( const std::string& libname,
355 const std::string& package_name,
356 DLGetPackageFunction& getpack,
357 DLDeletePackageFunction& delpack)
359 bbtkDebugMessage("package",3,"==> Package::OpenDynamicLibrary("
360 <<libname<<")"<<std::endl);
361 #if defined(__GNUC__)
365 handler = dlopen(libname.c_str(), BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
366 //EED handler = dlopen(libname.c_str(), RTLD_LAZY | RTLD_LOCAL );
370 bbtkMessage("package",2,
371 "Could not open shared library [" <<libname<<"] : "
372 <<dlerror() << std::endl);
376 bbtkDebugMessage("package",3,"* Shared lib ["<<libname<<"] open"<<std::endl);
378 // Loads the Package bbtk version function
379 std::string getvername(package_name);
381 BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
382 DLGetPackageBBTKVersionFunction getbbtkversion
383 = (DLGetPackageBBTKVersionFunction)(dlsym(handler,getvername.c_str()));
386 bbtkDebugMessage("package",3,"***"<<std::endl);
387 bbtkMessage("package",2,
388 "Shared library ["<<libname
389 <<"] is not a valid bbtk package."
390 <<" Symbol ["<<getvername<<"] :"<<dlerror()<< std::endl);
395 bbtkDebugMessage("package",3,"* Symbol ["<<getvername
396 <<"] found"<<std::endl);
398 if (getbbtkversion() != bbtk::GetVersion())
400 bbtkMessage("package",2,
401 "Shared library ["<<libname
402 <<"] was build with bbtk version "
404 <<" but the current program runs with version "
405 <<bbtk::GetVersion()<<" : cannot load it"<<std::endl);
411 bbtkDebugMessage("package",3,"* Package bbtk version '"<<getbbtkversion()<<"' matches"<<std::endl);
412 // Loads the Package get function
413 std::string getpackname(package_name);
414 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
415 getpack = (DLGetPackageFunction)(dlsym(handler, getpackname.c_str()));
418 bbtkMessage("package",2,
419 "Shared library ["<<libname
420 <<"] is not a valid bbtk package."
421 <<" Symbol ["<<getpackname<<"] :"<<dlerror()<< std::endl);
426 bbtkDebugMessage("package",3,"* Symbol ["<<getpackname<<"] found"<<std::endl);
427 // Loads the Package delete function
429 std::string delpackname(package_name);
430 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
431 delpack = (DLDeletePackageFunction)(dlsym(handler, delpackname.c_str()));
434 bbtkMessage("package",2,
435 "Shared library ["<<libname
436 <<"] is not a valid bbtk package."
437 <<" Symbol ["<<delpackname<<"] :"<<dlerror()<< std::endl);
441 bbtkDebugMessage("package",3,"* Symbol ["<<delpackname<<"] found"<<std::endl);
442 #elif defined(_WIN32)
448 handler = LoadLibrary(libname.c_str());
451 bbtkMessage("package",2,
452 "Could not open shared library [" <<libname<<"]"
454 DWORD dwErrorCode = 0;
455 dwErrorCode = GetLastError();
456 bbtkMessage("package",2,
457 "Windows Error: [" << dwErrorCode <<"]"
463 // Loads the Package bbtk version function
464 std::string getvername(package_name);
466 BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
467 DLGetPackageBBTKVersionFunction getbbtkversion
468 = (DLGetPackageBBTKVersionFunction)(GetProcAddress(handler,
469 getvername.c_str()));
472 FreeLibrary(handler);
473 bbtkMessage("package",2,
474 "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",2,
485 "Shared library ["<<libname
486 <<"] was build with bbtk version "
488 <<" but the current program runs with version "
489 <<bbtk::GetVersion()<<" : cannot load it"<<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",2,
502 "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",2,
516 "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))
580 std::string packname = pack.lock()->GetName();
581 bbtkDebugMessage("package",5,"==> Package::UnLoadDynamicLibrary('"
585 if (!pack.lock()->GetBlackBoxMap().empty())
588 bbtkDebugMessage("package",5," Package not empty ... abort"
592 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
593 <<"BlackBoxMap not empty "
594 <<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"
622 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 //==========================================================================
686 /// Creates an instance of a black box of type <type> with name <name>
687 BlackBox::Pointer Package::NewBlackBox(const std::string& type,
688 const std::string& name) const
690 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::NewBlackBox(\""<<type<<"\",\""<<name<<"\")"<<bbtkendl);
692 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(type);
693 if (i == mBlackBoxMap.end())
695 bbtkDebugDecTab("kernel",8);
696 return BlackBox::Pointer();
698 BlackBox::Pointer bb =i->second->NewBlackBox(name);
699 bbtkDebugDecTab("kernel",8);
703 //==========================================================================
707 //==========================================================================
708 /// Creates an instance of an adaptor of input type <typein> and
709 /// output type <typeout> with name <name>
710 BlackBox::Pointer Package::NewAdaptor(const DataInfo& typein,
711 const DataInfo& typeout,
712 const std::string& name) const
714 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
718 <<name<<"\")"<<bbtkendl);
720 AdaptorKey key(typein,typeout,
721 BlackBoxDescriptor::DEFAULT_ADAPTOR);
722 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
723 if (i == mAdaptorMap.end())
725 bbtkDebugDecTab("kernel",8);
726 return BlackBox::Pointer();
728 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
729 bbtkDebugDecTab("kernel",8);
733 //==========================================================================
735 //==========================================================================
736 /// Creates an instance of an adaptor of input type <typein> and
737 /// output type <typeout> with name <name>
738 BlackBox::Pointer Package::NewWidgetAdaptor(const DataInfo& typein,
739 const DataInfo& typeout,
740 const std::string& name) const
742 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
743 ">::NewWidgetAdaptor("
746 <<name<<"\")"<<bbtkendl);
748 AdaptorKey key(typein,typeout,
749 BlackBoxDescriptor::DEFAULT_GUI);
750 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
751 if (i == mAdaptorMap.end())
753 bbtkDebugDecTab("kernel",8);
754 return BlackBox::Pointer();
756 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
757 bbtkDebugDecTab("kernel",8);
761 //==========================================================================
765 //==========================================================================
766 /// Returns true is the package contains
767 /// an adaptor of input type <typein> and
768 /// output type <typeout>
769 /// If successfull then adaptor contains the black box type name
770 bool Package::FindWidgetAdaptor(const DataInfo& typein,
771 const DataInfo& typeout,
772 std::string& adaptor) const
774 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
775 ">::FindWidgetAdaptor("
777 <<typeout<<")"<<bbtkendl);
779 AdaptorKey key(/*typein*/
780 DataInfo(typeid(void),""),
782 BlackBoxDescriptor::DEFAULT_GUI);
783 // First try to find a single widget adaptor
784 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
785 if (i == mAdaptorMap.end())
787 bbtkDebugDecTab("kernel",8);
790 adaptor = i->second.lock()->GetTypeName();
791 bbtkDebugDecTab("kernel",8);
795 //==========================================================================
799 //==========================================================================
800 /// Returns true is the package contains
801 /// an adaptor of input type <typein> and
802 /// output type <typeout>
803 /// If successfull then adaptor contains the black box type name
804 bool Package::FindAdaptor(const DataInfo& typein,
805 const DataInfo& typeout,
806 std::string& adaptor) const
808 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
811 <<typeout<<")"<<bbtkendl);
813 AdaptorKey key(typein,typeout,
814 BlackBoxDescriptor::DEFAULT_ADAPTOR);
815 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
816 if (i == mAdaptorMap.end())
818 bbtkDebugDecTab("kernel",8);
821 adaptor = i->second.lock()->GetTypeName();
822 bbtkDebugDecTab("kernel",8);
826 //==========================================================================
829 //==========================================================================
830 /// Registers a black box descriptor in the package
831 bool Package::RegisterBlackBox(BlackBoxDescriptor::Pointer d)
833 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\")"<<std::endl);
835 BlackBoxMapType::iterator i = mBlackBoxMap.find(d->GetTypeName());
836 if (i!=mBlackBoxMap.end())
838 bbtkWarning("Package<"<<GetName()<<"> : Trying to register box type <"
839 <<d->GetTypeName()<<"> which is already in the package");
843 mBlackBoxMap[d->GetTypeName()] = d;
845 d->SetPackage(GetThisPointer<Package>());
847 // If it is a default adaptor, also register it in the adaptors map
848 if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR )
850 bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\") : The box is an adaptor, inserting it in adaptors map ..."<<std::endl);
852 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
853 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
854 DataInfo infoin(typein,d->GetInputDescriptor("In")->GetNature());
855 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
856 AdaptorKey key(infoin,infoout,d->GetKind());
858 AdaptorMapType::const_iterator i;
859 i = mAdaptorMap.find(key);
860 if (i == mAdaptorMap.end())
862 mAdaptorMap[key] = d;
864 // If already an adaptor registered : error
867 if (i->second.lock()->GetTypeName() != d->GetTypeName())
869 bbtkError("Package <"<<GetName()<<
870 "> : trying to register black box <"
872 <<"> as default adaptor but there is already a default adaptor registered (<"
873 <<i->second.lock()->GetTypeName()<<">)");
877 // If it is a default adaptor, also register it in the adaptors map
878 else if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_GUI)
880 bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\") : The box is a widget adaptor, inserting it in adaptors map ..."<<std::endl);
882 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
883 DataInfo infoin(typeid(void),"");
884 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
885 AdaptorKey key(infoin,infoout,d->GetKind());
887 AdaptorMapType::const_iterator i;
888 i = mAdaptorMap.find(key);
889 if (i == mAdaptorMap.end())
891 mAdaptorMap[key] = d;
893 // If already an adaptor registered : error
896 if (i->second.lock()->GetTypeName() != d->GetTypeName())
898 bbtkError("Package <"<<GetName()<<
899 "> : trying to register black box <"
901 <<"> as default widget adaptor but there is already a default adaptor registered (<"
902 <<i->second.lock()->GetTypeName()<<">)");
908 bbtkDebugDecTab("kernel",8);
912 //==========================================================================
914 //===================================================================
915 void Package::CheckBoxes() const
917 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
918 <<" ["<<GetName()<<"]"<<std::endl);
919 BlackBoxMapType::const_iterator i;
920 for (i=mBlackBoxMap.begin();
921 i!=mBlackBoxMap.end();
924 i->second->Check(true);
926 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
927 <<" ["<<GetName()<<"] ... OK"<<std::endl);
929 //===================================================================
932 //==========================================================================
933 /// Changes the name of a black box type
934 void Package::ChangeBlackBoxName( const std::string& oldname, const std::string& newname )
936 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::ChangeBlackBoxName(\""<<oldname<<"\",\""<<newname<<"\")"<<std::endl);
937 // Looking into the bb map
938 BlackBoxMapType::iterator i = mBlackBoxMap.find(oldname);
939 if (i == mBlackBoxMap.end())
941 bbtkDebugDecTab("kernel",8);
942 bbtkError("ChangeBlackBoxName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
945 i->second->SetTypeName(newname);
946 mBlackBoxMap[newname] = i->second;
947 mBlackBoxMap.erase(i);
949 bbtkDebugDecTab("kernel",8);
951 //==========================================================================
955 //==========================================================================
956 /// Displays the list of black boxes of the package
957 void Package::PrintBlackBoxes(bool description, bool adaptors) const
959 unsigned int lmax = 0;
960 std::vector<std::string> names;
961 std::vector<std::string> kinds;
962 std::vector<std::string> descrs;
964 BlackBoxMapType::const_iterator i;
965 for (i=mBlackBoxMap.begin();
966 i!=mBlackBoxMap.end();
970 ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) )
972 std::string name(" ");
973 name += i->second->GetTypeName();
974 names.push_back(name);
977 if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR )
979 kind = std::string("[A]");
981 else if ( i->second->GetKind() ==
982 BlackBoxDescriptor::DEFAULT_ADAPTOR )
984 kind = std::string("[DA]");
986 kinds.push_back(kind);
988 unsigned int l = name.size()+kind.size();
989 if (l>lmax) lmax = l;
995 descr += i->second->GetDescription();
997 descrs.push_back(descr);
1003 offs.append(lmax+3,' ');
1004 std::vector<std::string>::iterator ni,ci,di;
1005 for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin();
1006 ni != names.end(); ++ni, ++ci, ++di)
1009 space.append(lmax - ni->size() - ci->size(),' ');
1010 bbtkMessage("help",1,*ni << space << *ci );
1012 unsigned int dmax = 75 - lmax;
1013 // while (d.size() > dmax )
1016 bbtkMessage("help",1,d.substr(0,dmax) << "..." << std::endl);
1018 bbtkMessage("help",1,d << std::endl);
1019 // d = d.substr(dmax,d.size());
1024 //==========================================================================
1026 //==========================================================================
1027 /// Displays the list of adaptors of the package
1028 void Package::PrintAdaptors(bool description) const
1030 BlackBoxMapType::const_iterator i;
1031 for (i=mBlackBoxMap.begin();
1032 i!=mBlackBoxMap.end();
1035 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD )
1037 bbtkMessage("help",1,
1038 " "<<i->second->GetTypeName());
1039 if ( i->second->GetKind() ==
1040 BlackBoxDescriptor::DEFAULT_ADAPTOR )
1042 bbtkMessage("help",1,
1047 bbtkMessage("help",1,
1048 " : "<<i->second->GetDescription());
1051 bbtkMessage("help",1,std::endl);
1055 AdaptorMapType::const_iterator i;
1056 for (i=mAdaptorMap.begin();
1057 i!=mAdaptorMap.end();
1060 bbtkMessage("help",1,
1061 " "<<i->second->GetTypeName());
1064 bbtkMessage("help",1,
1065 " : "<<i->second->GetDescription());
1068 bbtkMessage("help",1,std::endl);
1072 //==========================================================================
1074 //==========================================================================
1075 /// Prints help on a black box
1076 void Package::HelpBlackBox(const std::string& name, bool full) const
1078 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
1079 <<name<<"\")"<<bbtkendl);
1081 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
1082 if (i == mBlackBoxMap.end())
1084 bbtkDebugDecTab("kernel",8);
1085 bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
1087 // bbtkMessage("help",1,"["<<GetName()<<"] ");
1088 i->second->GetHelp(full);
1089 bbtkDebugDecTab("kernel",8);
1092 //==========================================================================
1095 //==========================================================================
1096 /// Returns true iff the package contains the box of name boxname
1097 bool Package::ContainsBlackBox(const std::string& name) const
1099 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
1100 <<name<<"\")"<<bbtkendl);
1102 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
1103 if (i == mBlackBoxMap.end())
1105 bbtkDebugDecTab("kernel",8);
1108 bbtkDebugDecTab("kernel",8);
1111 //==========================================================================
1115 //==========================================================================
1116 void Package::CreateHtmlPage(const std::string& filename,
1117 const std::string& caller,
1118 const std::string& source,
1119 const std::string& custom_header,
1120 const std::string& custom_title,
1123 bool relative_link ) const
1125 bbtkDebugMessageInc("kernel",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
1126 <<filename<<"\")"<<bbtkendl);
1128 //---------------------
1131 s.open(filename.c_str());
1134 bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
1137 //----------------------
1139 std::string title = "BBTK Package "+GetName()+" "+GetVersion();
1141 if (custom_title.length() != 0) title = custom_title;
1143 s << "<html lang=\"en\">\n";
1145 s << "<title>" << title << "</title>\n";
1146 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
1147 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
1148 s << "<meta name=\"generator\" content=\"\">\n";
1149 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
1150 //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
1151 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
1152 s << "pre.display { font-family:inherit }\n";
1153 s << "pre.format { font-family:inherit }\n";
1154 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
1155 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
1156 s << "pre.smallexample { font-size:smaller }\n";
1157 s << "pre.smalllisp { font-size:smaller }\n";
1158 s << "span.sc { font-variant:small-caps }\n";
1159 s << "span.roman { font-family:serif; font-weight:normal; } \n";
1160 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
1161 s << "--></style>\n";
1163 //----------------------
1165 //----------------------
1168 s << "<a name=\"Top\"></a>\n";
1170 //----------------------
1172 if ( custom_header.length() != 0)
1174 if ( custom_header != "none" )
1177 in.open(custom_header.c_str());
1180 bbtkError("Could not open file \""<<custom_header<<"\"");
1185 in.getline(buffer,512);
1186 std::string line(buffer);
1193 s << "<object data=\"" << custom_header
1194 << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
1195 << custom_header <<" could not be embedded.</object>\n";
1204 s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
1205 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1206 s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1207 << GetDescription() << "</TD></TR>\n";
1208 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1209 << GetAuthor() << "</TD></TR>\n";
1210 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1211 << GetCategory() << "</TD></TR>\n";
1212 s << "<TR><TD style='vertical-align: top;'><b> Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1213 << GetVersion() << "</TD></TR>\n";
1214 s << "<TR><TD style='vertical-align: top;'><b> bbtk Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1215 << bbtk::GetVersion() << "</TD></TR>\n";
1219 //-------------------
1220 // Table of contents
1222 // s << "<div class=\"contents\">\n";
1223 s << "<p><b> Black Boxes : </b>\n";
1226 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1228 BlackBoxMapType::const_iterator i;
1229 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i)
1231 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD)
1234 std::string name = i->second->GetTypeName();
1235 Utilities::html_format(name);
1236 std::string descr = i->second->GetDescription();
1237 //Utilities::html_format(descr);
1240 s << "<TD style='vertical-align: top;'>";
1241 s << " <a name=\"toc_"<<name
1242 <<"\" href=\"#"<<name<<"\">"
1245 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1254 //-------------------
1256 if (mAdaptorMap.size()>0)
1258 // s << "<div class=\"contents\">\n";
1259 s << "<p><b> Adaptors : </b>\n";
1262 // BlackBoxMapType::const_iterator i;
1263 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1264 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end();++i)
1266 if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD)
1269 std::string name = i->second->GetTypeName();
1270 Utilities::html_format(name);
1271 std::string descr = i->second->GetDescription();
1274 s << "<TD style='vertical-align: top;'>";
1275 s << " <a name=\"toc_"<<name
1276 <<"\" href=\"#"<<name<<"\">"
1279 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1289 // s << "<div class=\"node\">\n";
1291 // s << "<p><hr>\n";
1292 // s << "<a name=\"Top\"></a>\n";
1293 // s << "Top: <a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
1294 // s << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
1295 // s << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
1299 //----------------------
1302 //-------------------
1303 // Computes output directory from filename to pass it to
1304 // BlackBoxDescriptor::InsertHtmlHelp
1307 std::string::size_type slash_position = filename.find_last_of("/\\");
1310 if (slash_position != std::string::npos) {
1311 if (slash_position == 0)
1313 dir = filename.substr(0,slash_position);
1316 for (i=mBlackBoxMap.begin();
1317 i!=mBlackBoxMap.end();
1320 i->second->InsertHtmlHelp(s,detail,level,dir,relative_link);
1323 //----------------------
1328 ptm = gmtime ( &rawtime );
1331 s << "Automatically generated by <b>"<<caller<<"</b> "//from <b>"
1334 << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900
1335 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
1336 s << "</body></html>\n";
1338 //----------------------
1341 bbtkDebugDecTab("kernel",9);
1343 //==========================================================================
1345 //==========================================================================
1346 std::string Package::GetObjectName() const
1348 return std::string("Package '")+mName+std::string("'");
1350 //==========================================================================
1352 //==========================================================================
1353 std::string Package::GetObjectInfo() const
1355 std::stringstream i;
1356 i << " - "<<mBlackBoxMap.size() << " boxes" << std::endl;
1357 if (mDynamicLibraryHandler)
1359 i<< " - Loaded from dynamic library"<<std::endl;
1363 //==========================================================================
1366 //==========================================================================
1367 size_t Package::GetObjectSize() const
1369 size_t s = Superclass::GetObjectSize();
1370 s += Package::GetObjectInternalSize();
1373 //==========================================================================
1374 //==========================================================================
1375 size_t Package::GetObjectInternalSize() const
1377 size_t s = sizeof(Package);
1380 //==========================================================================
1381 //==========================================================================
1382 size_t Package::GetObjectRecursiveSize() const
1384 size_t s = Superclass::GetObjectRecursiveSize();
1385 s += Package::GetObjectInternalSize();
1387 BlackBoxMapType::const_iterator i;
1388 for (i = mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i )
1390 s += i->second->GetObjectRecursiveSize();
1394 //==========================================================================
1396 //==========================================================================
1397 std::set<Package::WeakPointer>
1398 Package::mReleasedDynamicallyLoadedPackages;
1399 //==========================================================================