1 /*=========================================================================
4 Module: $RCSfile: bbtkPackage.cxx,v $
6 Date: $Date: 2008/04/22 08:29:09 $
7 Version: $Revision: 1.15 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See doc/license.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 *\brief Class bbtk::Package : registers black boxes descriptors and is able to create instances of the black boxes registered.
22 #include "bbtkPackage.h"
23 #include "bbtkMessageManager.h"
24 #include "bbtkConfigurationFile.h"
27 #include "bbtkUtilities.h"
34 //==========================================================================
35 /// Creates a new package
36 Package::Pointer Package::New(const std::string& name,
37 const std::string& author,
38 const std::string& description,
39 const std::string& version,
40 const std::string& BBTKVersion)
42 bbtkDebugMessage("object",1,"##> Package::New('"<<name<<"',...)"
44 Package::Pointer p = MakePointer(new Package(name,
49 bbtkDebugMessage("object",2,"<## Package::New('"<<name<<"',...)"
53 //==========================================================================
55 //==========================================================================
56 /// Ctor with the name of the package
57 Package::Package(const std::string& name,
58 const std::string& author,
59 const std::string& description,
60 const std::string& version,
61 const std::string& BBTKVersion)
63 mDynamicLibraryHandler(0),
66 mDescription(description),
69 bbtkDebugMessage("object",2,"==> Package::Package('"<<name<<"',...)"
71 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
72 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
73 std::string url = default_doc_dir;
74 if (c != '/' && c !='\\') url = url + "/";
75 url = url + "temp_dir/" + name + "/index.html";
78 SetDocRelativeURL("Relative url not set");
81 std::string relurl(BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH));
82 relurl += "/packages/"+name+"/bbdoc/index.html";
83 std::string url = ConfigurationFile::GetInstance().Get_url()
86 SetDocRelativeURL(relurl);
89 // std::cout << " url=["<<url<<"]"<<std::endl;
90 // std::cout << "relurl=["<<relurl<<"]"<<std::endl;
91 bbtkDebugMessage("object",2,"<== Package::Package('"<<name<<"',...) OK"
95 //==========================================================================
99 //==========================================================================
103 bbtkDebugMessage("object",2,"==> Package::~Package(\""<<mName<<"\")"<<bbtkendl);
104 bbtkDebugMessage("object",2,"<== Package::~Package(\""<<mName<<"\")"<<bbtkendl);
106 //==========================================================================
109 //==========================================================================
110 void PackageReleaseBlackBoxDescriptorInternal(Package::WeakPointer pack,
111 const std::string& descname)
113 // Try to release descriptor
114 bbtkDebugMessage("package",3,"--- Trying to release descriptor '"
115 <<descname<<"'"<<bbtkendl);
116 std::string packname = pack.lock()->GetName();
118 Package::BlackBoxMapType::iterator desc =
119 pack.lock()->GetBlackBoxMap().find(descname);
120 if (desc == pack.lock()->GetBlackBoxMap().end())
122 bbtkDebugMessage("package",3,
123 " Descriptor has already been released"
127 // bbtkDebugMessage("package",3,
128 // " Trying unreferencing it ... "<<std::endl);
129 BlackBoxDescriptor::WeakPointer pdesc = desc->second;
130 desc->second.reset();
131 // if it is dead : remove it
134 bbtkDebugMessage("package",2," '"<<packname<<"::"<<descname<<"' Descriptor expired"<<bbtkendl);
137 bbtkDebugMessage("package",2,
138 " ... and caused its package death"
142 desc = pack.lock()->GetBlackBoxMap().find(descname);
143 if (desc != pack.lock()->GetBlackBoxMap().end())
144 pack.lock()->GetBlackBoxMap().erase(desc);
148 bbtkDebugMessage("package",3," ... Descriptor still alive ("
149 <<pdesc.use_count()<<" refs)"
151 pack.lock()->GetBlackBoxMap()[descname] = pdesc.lock();
154 //==========================================================================
157 //==========================================================================
159 void Package::Release(Package::WeakPointer pack)
161 std::string packname = pack.lock()->mName;
162 bbtkDebugMessage("package",1,"==> Package::Release('"<<
163 packname<<"')"<<bbtkendl);
165 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
166 long ndesc = pack.lock()->GetBlackBoxMap().size();
167 long nrefs = pack.use_count();
169 bbtkDebugMessage("package",2," "<<nrefs<<" refs / "
170 <<ndesc<<" descr / dyn="
173 // A package is "free" from any external reference iff :
174 // i) It is not dynamically loaded and nrefs == ndesc
175 // (each desc references its package) or
176 // ii) It is dynamically loaded and nrefs == ndesc + 1
177 // (A dynamic library holds a static pointer on the package it contains
178 // which is allocated when the PACKAGENAMEGetPackage() func is called,
179 // and descallocated (reset) by PACKAGENAMEDeletePackage())
180 if (nrefs == ndesc + dyn)
182 bbtkDebugMessage("package",2,
183 " -> No more external ref : checking descriptors"
185 // We must take care that removing refs on descriptors
186 // can lead to their deletion which can in turn unref
187 // internal boxes which can release their descriptors hence
188 // call Package::ReleaseBlackBoxDescriptor
189 // As a consequence during descriptors release :
190 // 1) The map can change dynamically : we cannot iterate over it
191 // as any iterator can become invalid
192 // 2) The package can auto-destruct : we must test its existence
193 // after each release
194 // We must also take care of not locking the package pointer
195 // or any ref count check in Package::ReleaseBlackBoxDescriptor
198 // The list of descriptors names at start
199 std::vector<std::string> descnamelist;
200 BlackBoxMapType::iterator i;
201 for (i=pack.lock()->mBlackBoxMap.begin();
202 i!= pack.lock()->mBlackBoxMap.end();
204 descnamelist.push_back(i->first);
206 // Iterator over the initial names
207 std::vector<std::string>::iterator descname;
208 for (descname=descnamelist.begin();
209 descname!=descnamelist.end();
212 // Is package still alive ?
215 bbtkDebugMessage("package",2,"--- Package::Release('"<<
217 <<"') : package expired during release : bailing out"<<bbtkendl);
221 PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
226 UnLoadDynamicLibrary(pack);
227 // Unload orphan dl packages
228 Package::UnLoadReleasedDynamicallyLoadedPackages();
235 bbtkDebugMessage("package",1,"<== Package::Release('"<<
236 packname<<"')"<<bbtkendl);
239 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
240 long ndesc = pack.lock()->GetBlackBoxMap().size();
241 long nrefs = pack.use_count();
243 bbtkDebugMessage("package",1," ... Package still alive ("
245 <<ndesc<<" descr / dyn="
246 <<dyn<<")"<<std::endl);
250 bbtkDebugMessage("package",1," ... Package has been released"
254 //==========================================================================
256 //==========================================================================
257 /// "Releases" the package
258 /// Signals the package that it can free the given descriptor
259 /// if they are no more used and free itself if it is no
261 /// Note : Any non-weak pointer on the package must have been freed
262 void Package::ReleaseBlackBoxDescriptor(Package::WeakPointer pack,
263 BlackBoxDescriptor::WeakPointer descr)
265 std::string packname = pack.lock()->mName;
266 std::string dname = descr.lock()->GetTypeName();
267 bbtkDebugMessage("package",3,"==> Package::ReleaseBlackBoxDescriptor('"<<
268 packname<<"','"<<dname<<"')"<<bbtkendl);
270 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
271 long ndesc = pack.lock()->GetBlackBoxMap().size();
272 long nrefs = pack.use_count();
274 bbtkDebugMessage("package",3," "<<nrefs<<" refs / "
275 <<ndesc<<" descr / dynamically loaded = "
278 // A package is "free" from any external reference iff :
279 // i) It is not dynamically loaded and nrefs == ndesc
280 // (each desc references its package) or
281 // ii) It is dynamically loaded and nrefs == ndesc + 1
282 // (A dynamic library holds a static pointer on the package it contains
283 // which is allocated when the PACKAGENAMEGetPackage() func is called,
284 // and descallocated (reset) by PACKAGENAMEDeletePackage())
285 if (nrefs == ndesc + dyn)
287 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
290 // If the package is released and dynamically loaded
291 // then put it in the static list mReleasedDynamicallyLoadedPackages
292 UnLoadDynamicLibrary(pack,false);
294 bbtkDebugMessage("package",3,"<== Package::ReleaseBlackBoxDescriptor('"<<
295 packname<<"','"<<dname<<"')"<<bbtkendl);
299 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
300 long ndesc = pack.lock()->GetBlackBoxMap().size();
301 long nrefs = pack.use_count();
303 bbtkDebugMessage("package",3," ... Package still alive ("
305 <<ndesc<<" descr / dyn="
306 <<dyn<<")"<<std::endl);
310 bbtkDebugMessage("package",3," ... Package has been released"
315 //==========================================================================
317 //==========================================================================
318 /// Opens a dynamic library which contains a bbtk package
319 /// Returns the handler
320 /// Load the package management symbols from the lib
321 /// returns false if a problem occured hence can be used
322 /// to test that a dyn lib is a valid bbtk package lib
323 /// NB : The BBTK version exported from the library
324 /// is tested against the current bbtk version
325 DynamicLibraryHandler Package::OpenDynamicLibrary
326 ( const std::string& libname,
327 const std::string& package_name,
328 DLGetPackageFunction& getpack,
329 DLDeletePackageFunction& delpack)
331 bbtkDebugMessage("package",3,"==> Package::OpenDynamicLibrary("
332 <<libname<<")"<<std::endl);
333 #if defined(__GNUC__)
337 handler = dlopen(libname.c_str(),
338 BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
341 bbtkMessage("package",2,
342 "Could not open shared library [" <<libname<<"] : "
343 <<dlerror() << std::endl);
347 bbtkDebugMessage("package",3,"* Shared lib ["<<libname<<"] open"<<std::endl);
349 // Loads the Package bbtk version function
350 std::string getvername(package_name);
352 BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
353 DLGetPackageBBTKVersionFunction getbbtkversion
354 = (DLGetPackageBBTKVersionFunction)(dlsym(handler,getvername.c_str()));
357 bbtkDebugMessage("package",3,"***"<<std::endl);
358 bbtkMessage("package",2,
359 "Shared library ["<<libname
360 <<"] is not a valid bbtk package."
361 <<" Symbol ["<<getvername<<"] :"<<dlerror()<< std::endl);
366 bbtkDebugMessage("package",3,"* Symbol ["<<getvername
367 <<"] found"<<std::endl);
369 if (getbbtkversion() != bbtk::GetVersion())
371 bbtkMessage("package",2,
372 "Shared library ["<<libname
373 <<"] was build with bbtk version "
375 <<" but the current program runs with version "
376 <<bbtk::GetVersion()<<" : cannot load it"<<std::endl);
382 bbtkDebugMessage("package",3,"* Package bbtk version '"<<getbbtkversion()<<"' matches"<<std::endl);
383 // Loads the Package get function
384 std::string getpackname(package_name);
385 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
386 getpack = (DLGetPackageFunction)(dlsym(handler, getpackname.c_str()));
389 bbtkMessage("package",2,
390 "Shared library ["<<libname
391 <<"] is not a valid bbtk package."
392 <<" Symbol ["<<getpackname<<"] :"<<dlerror()<< std::endl);
397 bbtkDebugMessage("package",3,"* Symbol ["<<getpackname<<"] found"<<std::endl);
398 // Loads the Package delete function
400 std::string delpackname(package_name);
401 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
402 delpack = (DLDeletePackageFunction)(dlsym(handler, delpackname.c_str()));
405 bbtkMessage("package",2,
406 "Shared library ["<<libname
407 <<"] is not a valid bbtk package."
408 <<" Symbol ["<<delpackname<<"] :"<<dlerror()<< std::endl);
412 bbtkDebugMessage("package",3,"* Symbol ["<<delpackname<<"] found"<<std::endl);
413 #elif defined(_WIN32)
419 handler = LoadLibrary(libname.c_str());
422 bbtkMessage("package",2,
423 "Could not open shared library [" <<libname<<"]"
428 // Loads the Package bbtk version function
429 std::string getvername(package_name);
431 BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
432 DLGetPackageBBTKVersionFunction getbbtkversion
433 = (DLGetPackageBBTKVersionFunction)(GetProcAdress(handler,
434 getvername.c_str()));
437 FreeLibrary(handler);
438 bbtkMessage("package",2,
439 "Shared library ["<<libname
440 <<"] is not a valid bbtk package."
441 <<" Symbol ["<<getbbtkversion<<"] not found"<< std::endl);
446 if (getbbtkversion() != bbtk::GetVersion())
448 FreeLibrary(handler);
449 bbtkMessage("package",2,
450 "Shared library ["<<libname
451 <<"] was build with bbtk version "
453 <<" but the current program runs with version "
454 <<bbtk::GetVersion()<<" : cannot load it"<<std::endl);
459 // Loads the Package get function
460 std::string getpackname(package_name);
461 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
462 getpack = (DLGetPackageFunction)(GetProcAdress(handler, getpackname.c_str()));
465 FreeLibrary(handler);
466 bbtkMessage("package",2,
467 "Shared library ["<<libname
468 <<"] is not a valid bbtk package."
469 <<" Symbol ["<<getpackname<<"] not found"<< std::endl);
473 // Loads the Package delete function
474 std::string delpackname(package_name);
475 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
476 delpack = (DLDeletePackageFunction)(GetProcAdress(handler, delpackname.c_str()));
479 FreeLibrary(handler);
480 bbtkMessage("package",2,
481 "Shared library ["<<libname
482 <<"] is not a valid bbtk package."
483 <<" Symbol ["<<delpackname<<"] not found"<< std::endl);
488 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
493 //==========================================================================
495 //==========================================================================
496 /// Loads a package from a dynamic library
497 Package::Pointer Package::CreateFromDynamicLibrary(const std::string& libname,
498 const std::string& pkgname,
499 const std::string& path)
501 bbtkDebugMessage("package",1,"==> Package::CreateFromDynamicLibrary("
502 <<libname<<")"<<std::endl);
504 DLGetPackageFunction gf;
505 DLDeletePackageFunction df;
506 DynamicLibraryHandler h = Package::OpenDynamicLibrary(libname,
509 if (h==0) return Package::Pointer();
510 Package::Pointer p = gf();
511 p->mDynamicLibraryHandler = h;
512 p->mDLDeletePackageFunction = df;
514 std::string separator =
515 ConfigurationFile::GetInstance().Get_file_separator ();
516 //BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
517 std::string docreldoc =
518 separator + "bbdoc" + separator + pkgname + separator + "index.html";
520 ".." + separator + ".." + docreldoc;
521 std::string doc = path + separator + ".." + separator
522 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
526 p->SetDocRelativeURL(reldoc);
528 bbtkDebugMessage("package",1,"<== Package::CreateFromDynamicLibrary("
529 <<libname<<") .. OK"<<std::endl);
532 //==========================================================================
537 //==========================================================================
538 /// UnLoads the package dynamic library (if any)
539 void Package::UnLoadDynamicLibrary(Package::WeakPointer pack, bool doit)
541 if (pack.expired() || (!pack.lock()->mDynamicLibraryHandler))
545 std::string packname = pack.lock()->GetName();
546 bbtkDebugMessage("package",3,"==> Package::UnLoadDynamicLibrary('"
550 if (!pack.lock()->GetBlackBoxMap().empty())
553 bbtkDebugMessage("package",3," Package not empty ... abort"
557 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
558 <<"BlackBoxMap not empty "
559 <<BBTK_INTERNAL_ERROR_MESSAGE);
564 if (pack.use_count()!=1)
566 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
567 <<"empty dl package with external refs"
568 <<BBTK_INTERNAL_ERROR_MESSAGE);
574 bbtkDebugMessage("package",3," ... dynamic library closed"
579 mReleasedDynamicallyLoadedPackages.insert(pack);
580 bbtkDebugMessage("package",3,
581 " ... package put in the 'to unload' list"
586 bbtkDebugMessage("package",3,"<== Package::UnLoadDynamicLibrary('"
591 //==========================================================================
593 //==========================================================================
594 /// UnLoads released packages that were loaded dynamically
595 /// see UnLoadDynamicLibrary and ReleaseBlackBoxDescriptor
596 void Package::UnLoadReleasedDynamicallyLoadedPackages()
598 bbtkDebugMessage("package",2,"==> Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
600 std::set<Package::WeakPointer>::iterator i;
601 for (i=mReleasedDynamicallyLoadedPackages.begin();
602 i!=mReleasedDynamicallyLoadedPackages.end();
605 if (!i->expired()) UnLoad(*i);
607 bbtkDebugMessage("package",2,"<== Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
609 //==========================================================================
611 //==========================================================================
612 void Package::UnLoad(Package::WeakPointer pack)
614 std::string packname = pack.lock()->GetName();
615 bbtkDebugMessage("package",2,"==> Package::UnLoad("<<packname<<")"<<std::endl);
617 Package* p = pack.lock().get();
619 DynamicLibraryHandler h = p->mDynamicLibraryHandler;
621 // deletes the package
622 p->mDLDeletePackageFunction();
624 // closes the dl handler
625 #if defined(__GNUC__)
627 #elif defined(_WIN32)
631 bbtkDebugMessage("package",2," ... dynamic library unloaded"<<std::endl);
633 //==========================================================================
635 //==========================================================================
636 /// Creates an instance of a black box of type <type> with name <name>
637 BlackBox::Pointer Package::NewBlackBox(const std::string& type,
638 const std::string& name) const
640 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::NewBlackBox(\""<<type<<"\",\""<<name<<"\")"<<bbtkendl);
642 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(type);
643 if (i == mBlackBoxMap.end())
645 bbtkDebugDecTab("Kernel",8);
646 return BlackBox::Pointer();
648 BlackBox::Pointer bb =i->second->NewBlackBox(name);
649 bbtkDebugDecTab("Kernel",8);
653 //==========================================================================
657 //==========================================================================
658 /// Creates an instance of an adaptor of input type <typein> and
659 /// output type <typeout> with name <name>
660 BlackBox::Pointer Package::NewAdaptor(const DataInfo& typein,
661 const DataInfo& typeout,
662 const std::string& name) const
664 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
668 <<name<<"\")"<<bbtkendl);
670 AdaptorKey key(typein,typeout,
671 BlackBoxDescriptor::DEFAULT_ADAPTOR);
672 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
673 if (i == mAdaptorMap.end())
675 bbtkDebugDecTab("Kernel",8);
676 return BlackBox::Pointer();
678 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
679 bbtkDebugDecTab("Kernel",8);
683 //==========================================================================
685 //==========================================================================
686 /// Creates an instance of an adaptor of input type <typein> and
687 /// output type <typeout> with name <name>
688 BlackBox::Pointer Package::NewWidgetAdaptor(const DataInfo& typein,
689 const DataInfo& typeout,
690 const std::string& name) const
692 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
693 ">::NewWidgetAdaptor("
696 <<name<<"\")"<<bbtkendl);
698 AdaptorKey key(typein,typeout,
699 BlackBoxDescriptor::DEFAULT_GUI);
700 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
701 if (i == mAdaptorMap.end())
703 bbtkDebugDecTab("Kernel",8);
704 return BlackBox::Pointer();
706 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
707 bbtkDebugDecTab("Kernel",8);
711 //==========================================================================
715 //==========================================================================
716 /// Returns true is the package contains
717 /// an adaptor of input type <typein> and
718 /// output type <typeout>
719 /// If successfull then adaptor contains the black box type name
720 bool Package::FindWidgetAdaptor(const DataInfo& typein,
721 const DataInfo& typeout,
722 std::string& adaptor) const
724 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
725 ">::FindWidgetAdaptor("
727 <<typeout<<")"<<bbtkendl);
729 AdaptorKey key(/*typein*/
730 DataInfo(typeid(void),""),
732 BlackBoxDescriptor::DEFAULT_GUI);
733 // First try to find a single widget adaptor
734 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
735 if (i == mAdaptorMap.end())
737 bbtkDebugDecTab("Kernel",8);
740 adaptor = i->second.lock()->GetTypeName();
741 bbtkDebugDecTab("Kernel",8);
745 //==========================================================================
749 //==========================================================================
750 /// Returns true is the package contains
751 /// an adaptor of input type <typein> and
752 /// output type <typeout>
753 /// If successfull then adaptor contains the black box type name
754 bool Package::FindAdaptor(const DataInfo& typein,
755 const DataInfo& typeout,
756 std::string& adaptor) const
758 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
761 <<typeout<<")"<<bbtkendl);
763 AdaptorKey key(typein,typeout,
764 BlackBoxDescriptor::DEFAULT_ADAPTOR);
765 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
766 if (i == mAdaptorMap.end())
768 bbtkDebugDecTab("Kernel",8);
771 adaptor = i->second.lock()->GetTypeName();
772 bbtkDebugDecTab("Kernel",8);
776 //==========================================================================
779 //==========================================================================
780 /// Registers a black box descriptor in the package
781 bool Package::RegisterBlackBox(BlackBoxDescriptor::Pointer d)
783 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\")"<<std::endl);
785 BlackBoxMapType::iterator i = mBlackBoxMap.find(d->GetTypeName());
786 if (i!=mBlackBoxMap.end())
788 bbtkWarning("Package<"<<GetName()<<"> : Trying to register box type <"
789 <<d->GetTypeName()<<"> which is already in the package");
793 mBlackBoxMap[d->GetTypeName()] = d;
795 d->SetPackage(GetThisPointer<Package>());
797 // If it is a default adaptor, also register it in the adaptors map
798 if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR )
800 bbtkDebugMessage("Kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\") : The box is an adaptor, inserting it in adaptors map ..."<<std::endl);
802 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
803 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
804 DataInfo infoin(typein,d->GetInputDescriptor("In")->GetNature());
805 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
806 AdaptorKey key(infoin,infoout,d->GetKind());
808 AdaptorMapType::const_iterator i;
809 i = mAdaptorMap.find(key);
810 if (i == mAdaptorMap.end())
812 mAdaptorMap[key] = d;
814 // If already an adaptor registered : error
817 if (i->second.lock()->GetTypeName() != d->GetTypeName())
819 bbtkError("Package <"<<GetName()<<
820 "> : trying to register black box <"
822 <<"> as default adaptor but there is already a default adaptor registered (<"
823 <<i->second.lock()->GetTypeName()<<">)");
827 // If it is a default adaptor, also register it in the adaptors map
828 else if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_GUI)
830 bbtkDebugMessage("Kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\") : The box is a widget adaptor, inserting it in adaptors map ..."<<std::endl);
832 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
833 DataInfo infoin(typeid(void),"");
834 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
835 AdaptorKey key(infoin,infoout,d->GetKind());
837 AdaptorMapType::const_iterator i;
838 i = mAdaptorMap.find(key);
839 if (i == mAdaptorMap.end())
841 mAdaptorMap[key] = d;
843 // If already an adaptor registered : error
846 if (i->second.lock()->GetTypeName() != d->GetTypeName())
848 bbtkError("Package <"<<GetName()<<
849 "> : trying to register black box <"
851 <<"> as default widget adaptor but there is already a default adaptor registered (<"
852 <<i->second.lock()->GetTypeName()<<">)");
858 bbtkDebugDecTab("Kernel",8);
862 //==========================================================================
864 //===================================================================
865 void Package::CheckBoxes() const
867 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
868 <<" ["<<GetName()<<"]"<<std::endl);
869 BlackBoxMapType::const_iterator i;
870 for (i=mBlackBoxMap.begin();
871 i!=mBlackBoxMap.end();
874 i->second->Check(true);
876 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
877 <<" ["<<GetName()<<"] ... OK"<<std::endl);
879 //===================================================================
882 //==========================================================================
883 /// Changes the name of a black box type
884 void Package::ChangeBlackBoxName( const std::string& oldname, const std::string& newname )
886 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::ChangeBlackBoxName(\""<<oldname<<"\",\""<<newname<<"\")"<<std::endl);
887 // Looking into the bb map
888 BlackBoxMapType::iterator i = mBlackBoxMap.find(oldname);
889 if (i == mBlackBoxMap.end())
891 bbtkDebugDecTab("Kernel",8);
892 bbtkError("ChangeBlackBoxName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
895 i->second->SetTypeName(newname);
896 mBlackBoxMap[newname] = i->second;
897 mBlackBoxMap.erase(i);
899 bbtkDebugDecTab("Kernel",8);
901 //==========================================================================
905 //==========================================================================
906 /// Displays the list of black boxes of the package
907 void Package::PrintBlackBoxes(bool description, bool adaptors) const
909 unsigned int lmax = 0;
910 std::vector<std::string> names;
911 std::vector<std::string> kinds;
912 std::vector<std::string> descrs;
914 BlackBoxMapType::const_iterator i;
915 for (i=mBlackBoxMap.begin();
916 i!=mBlackBoxMap.end();
920 ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) )
922 std::string name(" ");
923 name += i->second->GetTypeName();
924 names.push_back(name);
927 if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR )
929 kind = std::string("[A]");
931 else if ( i->second->GetKind() ==
932 BlackBoxDescriptor::DEFAULT_ADAPTOR )
934 kind = std::string("[DA]");
936 kinds.push_back(kind);
938 unsigned int l = name.size()+kind.size();
939 if (l>lmax) lmax = l;
945 descr += i->second->GetDescription();
947 descrs.push_back(descr);
953 offs.append(lmax+3,' ');
954 std::vector<std::string>::iterator ni,ci,di;
955 for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin();
956 ni != names.end(); ++ni, ++ci, ++di)
959 space.append(lmax - ni->size() - ci->size(),' ');
960 bbtkMessage("Help",1,*ni << space << *ci );
962 unsigned int dmax = 75 - lmax;
963 // while (d.size() > dmax )
966 bbtkMessage("Help",1,d.substr(0,dmax) << "..." << std::endl);
968 bbtkMessage("Help",1,d << std::endl);
969 // d = d.substr(dmax,d.size());
974 //==========================================================================
976 //==========================================================================
977 /// Displays the list of adaptors of the package
978 void Package::PrintAdaptors(bool description) const
980 BlackBoxMapType::const_iterator i;
981 for (i=mBlackBoxMap.begin();
982 i!=mBlackBoxMap.end();
985 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD )
987 bbtkMessage("Help",1,
988 " "<<i->second->GetTypeName());
989 if ( i->second->GetKind() ==
990 BlackBoxDescriptor::DEFAULT_ADAPTOR )
992 bbtkMessage("Help",1,
997 bbtkMessage("Help",1,
998 " : "<<i->second->GetDescription());
1001 bbtkMessage("Help",1,std::endl);
1005 AdaptorMapType::const_iterator i;
1006 for (i=mAdaptorMap.begin();
1007 i!=mAdaptorMap.end();
1010 bbtkMessage("Help",1,
1011 " "<<i->second->GetTypeName());
1014 bbtkMessage("Help",1,
1015 " : "<<i->second->GetDescription());
1018 bbtkMessage("Help",1,std::endl);
1022 //==========================================================================
1024 //==========================================================================
1025 /// Prints help on a black box
1026 void Package::HelpBlackBox(const std::string& name, bool full) const
1028 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
1029 <<name<<"\")"<<bbtkendl);
1031 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
1032 if (i == mBlackBoxMap.end())
1034 bbtkDebugDecTab("Kernel",8);
1035 bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
1037 // bbtkMessage("Help",1,"["<<GetName()<<"] ");
1038 i->second->GetHelp(full);
1039 bbtkDebugDecTab("Kernel",8);
1042 //==========================================================================
1045 //==========================================================================
1046 /// Returns true iff the package contains the box of name boxname
1047 bool Package::ContainsBlackBox(const std::string& name) const
1049 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
1050 <<name<<"\")"<<bbtkendl);
1052 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
1053 if (i == mBlackBoxMap.end())
1055 bbtkDebugDecTab("Kernel",8);
1058 bbtkDebugDecTab("Kernel",8);
1061 //==========================================================================
1065 //==========================================================================
1066 void Package::CreateHtmlPage(const std::string& filename,
1067 const std::string& caller,
1068 const std::string& source,
1069 const std::string& custom_header,
1070 const std::string& custom_title,
1073 bool relative_link ) const
1075 bbtkDebugMessageInc("Kernel",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
1076 <<filename<<"\")"<<bbtkendl);
1078 //---------------------
1081 s.open(filename.c_str());
1084 bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
1087 //----------------------
1089 std::string title = "BBTK Package "+GetName()+" "+GetVersion();
1091 if (custom_title.length() != 0) title = custom_title;
1093 s << "<html lang=\"en\">\n";
1095 s << "<title>" << title << "</title>\n";
1096 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
1097 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
1098 s << "<meta name=\"generator\" content=\"\">\n";
1099 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
1100 //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
1101 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
1102 s << "pre.display { font-family:inherit }\n";
1103 s << "pre.format { font-family:inherit }\n";
1104 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
1105 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
1106 s << "pre.smallexample { font-size:smaller }\n";
1107 s << "pre.smalllisp { font-size:smaller }\n";
1108 s << "span.sc { font-variant:small-caps }\n";
1109 s << "span.roman { font-family:serif; font-weight:normal; } \n";
1110 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
1111 s << "--></style>\n";
1113 //----------------------
1115 //----------------------
1118 s << "<a name=\"Top\"></a>\n";
1120 //----------------------
1122 if ( custom_header.length() != 0)
1124 if ( custom_header != "none" )
1127 in.open(custom_header.c_str());
1130 bbtkError("Could not open file \""<<custom_header<<"\"");
1135 in.getline(buffer,512);
1136 std::string line(buffer);
1143 s << "<object data=\"" << custom_header
1144 << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
1145 << custom_header <<" could not be embedded.</object>\n";
1154 s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
1155 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1156 s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1157 << GetDescription() << "</TD></TR>\n";
1158 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1159 << GetAuthor() << "</TD></TR>\n";
1160 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1161 << GetCategory() << "</TD></TR>\n";
1162 s << "<TR><TD style='vertical-align: top;'><b> Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1163 << GetVersion() << "</TD></TR>\n";
1164 s << "<TR><TD style='vertical-align: top;'><b> bbtk Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1165 << bbtk::GetVersion() << "</TD></TR>\n";
1169 //-------------------
1170 // Table of contents
1172 // s << "<div class=\"contents\">\n";
1173 s << "<p><b> Black Boxes : </b>\n";
1176 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1178 BlackBoxMapType::const_iterator i;
1179 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i)
1181 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD)
1184 std::string name = i->second->GetTypeName();
1185 Utilities::html_format(name);
1186 std::string descr = i->second->GetDescription();
1187 Utilities::html_format(descr);
1190 s << "<TD style='vertical-align: top;'>";
1191 s << " <a name=\"toc_"<<name
1192 <<"\" href=\"#"<<name<<"\">"
1195 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1204 //-------------------
1206 if (mAdaptorMap.size()>0)
1208 // s << "<div class=\"contents\">\n";
1209 s << "<p><b> Adaptors : </b>\n";
1212 // BlackBoxMapType::const_iterator i;
1213 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1214 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end();++i)
1216 if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD)
1219 std::string name = i->second->GetTypeName();
1220 Utilities::html_format(name);
1221 std::string descr = i->second->GetDescription();
1224 s << "<TD style='vertical-align: top;'>";
1225 s << " <a name=\"toc_"<<name
1226 <<"\" href=\"#"<<name<<"\">"
1229 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1239 // s << "<div class=\"node\">\n";
1241 // s << "<p><hr>\n";
1242 // s << "<a name=\"Top\"></a>\n";
1243 // s << "Top: <a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
1244 // s << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
1245 // s << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
1249 //----------------------
1252 //-------------------
1253 // Computes output directory from filename to pass it to
1254 // BlackBoxDescriptor::InsertHtmlHelp
1257 std::string::size_type slash_position = filename.find_last_of("/\\");
1260 if (slash_position != std::string::npos) {
1261 if (slash_position == 0)
1263 dir = filename.substr(0,slash_position);
1266 for (i=mBlackBoxMap.begin();
1267 i!=mBlackBoxMap.end();
1270 i->second->InsertHtmlHelp(s,detail,level,dir,relative_link);
1273 //----------------------
1278 ptm = gmtime ( &rawtime );
1281 s << "Automatically generated by <b>"<<caller<<"</b> "//from <b>"
1284 << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900
1285 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
1286 s << "</body></html>\n";
1288 //----------------------
1291 bbtkDebugDecTab("Kernel",9);
1293 //==========================================================================
1295 //==========================================================================
1296 std::string Package::GetObjectName() const
1298 return std::string("Package '")+mName+std::string("'");
1300 //==========================================================================
1302 //==========================================================================
1303 std::string Package::GetObjectInfo() const
1305 std::stringstream i;
1306 i << " - "<<mBlackBoxMap.size() << " boxes" << std::endl;
1307 if (mDynamicLibraryHandler)
1309 i<< " - Loaded from dynamic library"<<std::endl;
1313 //==========================================================================
1316 //==========================================================================
1317 size_t Package::GetObjectSize() const
1319 size_t s = Superclass::GetObjectSize();
1320 s += Package::GetObjectInternalSize();
1323 //==========================================================================
1324 //==========================================================================
1325 size_t Package::GetObjectInternalSize() const
1327 size_t s = sizeof(Package);
1330 //==========================================================================
1331 //==========================================================================
1332 size_t Package::GetObjectRecursiveSize() const
1334 size_t s = Superclass::GetObjectRecursiveSize();
1335 s += Package::GetObjectInternalSize();
1337 BlackBoxMapType::const_iterator i;
1338 for (i = mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i )
1340 s += i->second->GetObjectRecursiveSize();
1344 //==========================================================================
1346 //==========================================================================
1347 std::set<Package::WeakPointer>
1348 Package::mReleasedDynamicallyLoadedPackages;
1349 //==========================================================================