2 /*=========================================================================
4 Module: $RCSfile: bbtkPackage.cxx,v $
6 Date: $Date: 2009/10/16 08:52:12 $
7 Version: $Revision: 1.32 $
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::DescriptorMapType::iterator desc =
132 pack.lock()->GetDescriptorMap().find(descname);
133 if (desc == pack.lock()->GetDescriptorMap().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()->GetDescriptorMap().find(descname);
156 if (desc != pack.lock()->GetDescriptorMap().end()) pack.lock()->GetDescriptorMap().erase(desc);
157 } else { //pdesc.expired
158 bbtkDebugMessage("package",5," ... Descriptor still alive ("
159 <<pdesc.use_count()<<" refs)"
161 pack.lock()->GetDescriptorMap()[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()->GetDescriptorMap().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 DescriptorMapType::iterator i;
211 for (i=pack.lock()->mDescriptorMap.begin();
212 i!= pack.lock()->mDescriptorMap.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()->mDescriptorMap[*descname];
233 if ( (dyn==0) || (boost::dynamic_pointer_cast<ComplexBlackBoxDescriptor>(desc)) )
235 PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
238 PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
244 UnLoadDynamicLibrary(pack);
245 // Unload orphan dl packages
246 Package::UnLoadReleasedDynamicallyLoadedPackages();
253 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
255 bbtkDebugMessage("package",2,"<== Package::Release('"<<
256 packname<<"')"<<bbtkendl);
260 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
261 long ndesc = pack.lock()->GetDescriptorMap().size();
262 long nrefs = pack.use_count();
264 bbtkDebugMessage("package",1," ... Package still alive ("
266 <<ndesc<<" descr / dyn="
267 <<dyn<<")"<<std::endl);
271 bbtkDebugMessage("package",1," ... Package has been released"
276 //==========================================================================
278 //==========================================================================
279 /// "Releases" the package
280 /// Signals the package that it can free the given descriptor
281 /// if they are no more used and free itself if it is no
283 /// Note : Any non-weak pointer on the package must have been freed
284 void Package::ReleaseBlackBoxDescriptor(Package::WeakPointer pack,
285 BlackBoxDescriptor::WeakPointer descr)
287 std::string packname = pack.lock()->mName;
288 std::string dname = descr.lock()->GetTypeName();
289 bbtkDebugMessage("package",3,"==> Package::ReleaseBlackBoxDescriptor('"<<
290 packname<<"','"<<dname<<"') : refs="
291 <<descr.use_count()<<bbtkendl);
293 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
294 long ndesc = pack.lock()->GetDescriptorMap().size();
295 long nrefs = pack.use_count();
297 bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
298 <<ndesc<<" descr / dynamically loaded = "
301 // A package is "free" from any external reference iff :
302 // i) It is not dynamically loaded and nrefs == ndesc
303 // (each desc references its package) or
304 // ii) It is dynamically loaded and nrefs == ndesc + 1
305 // (A dynamic library holds a static pointer on the package it contains
306 // which is allocated when the PACKAGENAMEGetPackage() func is called,
307 // and descallocated (reset) by PACKAGENAMEDeletePackage())
308 if (nrefs == ndesc + dyn)
313 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
315 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
319 // If the package is released and dynamically loaded
320 // then put it in the static list mReleasedDynamicallyLoadedPackages
321 UnLoadDynamicLibrary(pack,false);
323 bbtkDebugMessage("package",4,"<== Package::ReleaseBlackBoxDescriptor('"<<
324 packname<<"','"<<dname<<"'): refs="
325 <<descr.use_count()<<bbtkendl);
329 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
330 long ndesc = pack.lock()->GetDescriptorMap().size();
331 long nrefs = pack.use_count();
333 bbtkDebugMessage("package",3," ... Package still alive ("
335 <<ndesc<<" descr / dyn="
336 <<dyn<<")"<<std::endl);
340 bbtkDebugMessage("package",3," ... Package has been released"
345 //==========================================================================
347 //==========================================================================
348 /// Opens a dynamic library which contains a bbtk package
349 /// Returns the handler
350 /// Load the package management symbols from the lib
351 /// returns false if a problem occured hence can be used
352 /// to test that a dyn lib is a valid bbtk package lib
353 /// NB : The BBTK version exported from the library
354 /// is tested against the current bbtk version
355 DynamicLibraryHandler Package::OpenDynamicLibrary
356 ( const std::string& libname,
357 const std::string& package_name,
358 DLGetPackageFunction& getpack,
359 DLDeletePackageFunction& delpack)
361 bbtkDebugMessage("package",3,"==> Package::OpenDynamicLibrary("
362 <<libname<<")"<<std::endl);
363 #if defined(__GNUC__)
367 handler = dlopen(libname.c_str(), BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
368 //EED handler = dlopen(libname.c_str(), RTLD_LAZY | RTLD_LOCAL );
372 bbtkMessage("package",2,
373 "Could not open shared library [" <<libname<<"] : "
374 <<dlerror() << std::endl);
378 bbtkDebugMessage("package",3,"* Shared lib ["<<libname<<"] open"<<std::endl);
380 // Loads the Package bbtk version function
381 std::string getvername(package_name);
383 BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
384 DLGetPackageBBTKVersionFunction getbbtkversion
385 = (DLGetPackageBBTKVersionFunction)(dlsym(handler,getvername.c_str()));
388 bbtkDebugMessage("package",3,"***"<<std::endl);
389 bbtkMessage("package",2,
390 "Shared library ["<<libname
391 <<"] is not a valid bbtk package."
392 <<" Symbol ["<<getvername<<"] :"<<dlerror()<< std::endl);
397 bbtkDebugMessage("package",3,"* Symbol ["<<getvername
398 <<"] found"<<std::endl);
400 if (getbbtkversion() != bbtk::GetVersion())
402 bbtkMessage("package",2,
403 "Shared library ["<<libname
404 <<"] was build with bbtk version "
406 <<" but the current program runs with version "
407 <<bbtk::GetVersion()<<" : cannot load it"<<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",2,
421 "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",2,
437 "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",2,
454 "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);
468 BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
469 DLGetPackageBBTKVersionFunction getbbtkversion
470 = (DLGetPackageBBTKVersionFunction)(GetProcAddress(handler,
471 getvername.c_str()));
474 FreeLibrary(handler);
475 bbtkMessage("package",2,
476 "Shared library ["<<libname
477 <<"] is not a valid bbtk package."
478 <<" Symbol ["<<getbbtkversion<<"] not found"<< std::endl);
483 if (getbbtkversion() != bbtk::GetVersion())
485 FreeLibrary(handler);
486 bbtkMessage("package",2,
487 "Shared library ["<<libname
488 <<"] was build with bbtk version "
490 <<" but the current program runs with version "
491 <<bbtk::GetVersion()<<" : cannot load it"<<std::endl);
496 // Loads the Package get function
497 std::string getpackname(package_name);
498 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
499 getpack = (DLGetPackageFunction)(GetProcAddress(handler, getpackname.c_str()));
502 FreeLibrary(handler);
503 bbtkMessage("package",2,
504 "Shared library ["<<libname
505 <<"] is not a valid bbtk package."
506 <<" Symbol ["<<getpackname<<"] not found"<< std::endl);
510 // Loads the Package delete function
511 std::string delpackname(package_name);
512 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
513 delpack = (DLDeletePackageFunction)(GetProcAddress(handler, delpackname.c_str()));
516 FreeLibrary(handler);
517 bbtkMessage("package",2,
518 "Shared library ["<<libname
519 <<"] is not a valid bbtk package."
520 <<" Symbol ["<<delpackname<<"] not found"<< std::endl);
525 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
530 //==========================================================================
532 //==========================================================================
533 /// Loads a package from a dynamic library
534 Package::Pointer Package::CreateFromDynamicLibrary(const std::string& libname,
535 const std::string& pkgname,
536 const std::string& path)
538 bbtkDebugMessage("package",1,"==> Package::CreateFromDynamicLibrary("
539 <<libname<<")"<<std::endl);
541 DLGetPackageFunction gf;
542 DLDeletePackageFunction df;
543 DynamicLibraryHandler h = Package::OpenDynamicLibrary(libname,
546 if (h==0) return Package::Pointer();
547 Package::Pointer p = gf();
548 p->mDynamicLibraryHandler = h;
549 p->mDLDeletePackageFunction = df;
551 std::string separator =
552 ConfigurationFile::GetInstance().Get_file_separator ();
553 //BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
554 std::string docreldoc =
555 separator + "bbdoc" + separator + pkgname + separator + "index.html";
557 ".." + separator + ".." + docreldoc;
558 std::string doc = path + separator + ".." + separator
559 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
563 p->SetDocRelativeURL(reldoc);
565 bbtkDebugMessage("package",2,"<== Package::CreateFromDynamicLibrary("
566 <<libname<<") .. OK"<<std::endl);
569 //==========================================================================
574 //==========================================================================
575 /// UnLoads the package dynamic library (if any)
576 void Package::UnLoadDynamicLibrary(Package::WeakPointer pack, bool doit)
578 if (pack.expired() || (!pack.lock()->mDynamicLibraryHandler))
582 std::string packname = pack.lock()->GetName();
583 bbtkDebugMessage("package",5,"==> Package::UnLoadDynamicLibrary('"
587 if (!pack.lock()->GetDescriptorMap().empty())
590 bbtkDebugMessage("package",5," Package not empty ... abort"
594 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
595 <<"DescriptorMap not empty "
596 <<BBTK_INTERNAL_ERROR_MESSAGE);
601 if (pack.use_count()!=1)
603 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
604 <<"empty dl package with external refs"
605 <<BBTK_INTERNAL_ERROR_MESSAGE);
611 bbtkDebugMessage("package",5,"==> dynamic library for package '"
612 <<packname<<"' closed"
617 mReleasedDynamicallyLoadedPackages.insert(pack);
618 bbtkDebugMessage("package",1,"==> package '"<<packname
619 <<"' put in the 'to unload' list"
624 bbtkDebugMessage("package",5,"<== Package::UnLoadDynamicLibrary('"
629 //==========================================================================
631 //==========================================================================
632 /// UnLoads released packages that were loaded dynamically
633 /// see UnLoadDynamicLibrary and ReleaseBlackBoxDescriptor
634 void Package::UnLoadReleasedDynamicallyLoadedPackages()
636 bbtkDebugMessage("package",5,"==> Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
638 std::set<Package::WeakPointer>::iterator i;
641 if(mReleasedDynamicallyLoadedPackages.size()>0){
642 for (i=mReleasedDynamicallyLoadedPackages.begin();
643 i!=mReleasedDynamicallyLoadedPackages.end();
646 if (!i->expired()) UnLoad(*i);
650 bbtkDebugMessage("package",5,"<== Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
652 //==========================================================================
654 //==========================================================================
655 void Package::UnLoad(Package::WeakPointer pack)
657 std::string packname = pack.lock()->GetName();
658 bbtkDebugMessage("package",6,"==> Package::UnLoad("<<packname<<")"<<std::endl);
660 Package* p = pack.lock().get();
662 DynamicLibraryHandler h = p->mDynamicLibraryHandler;
664 // deletes the package
665 p->mDLDeletePackageFunction();
667 // closes the dl handler
668 #if defined(__GNUC__)
671 printf("EED Package::UnLoad ERROR %s\n", packname.c_str() );
672 bbtkWarning("Failed to close dynamic library for package '"<<packname
676 #elif defined(_WIN32)
680 bbtkDebugMessage("package",1,"==> dynamic library for package '"
681 <<packname<<"' closed"
683 bbtkDebugMessage("package",6," ... dynamic library unloaded"<<std::endl);
685 //==========================================================================
687 //==========================================================================
688 /// Creates an instance of a black box of type <type> with name <name>
689 BlackBox::Pointer Package::NewBlackBox(const std::string& type,
690 const std::string& name) const
692 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::NewBlackBox(\""<<type<<"\",\""<<name<<"\")"<<bbtkendl);
694 DescriptorMapType::const_iterator i = mDescriptorMap.find(type);
695 if (i == mDescriptorMap.end())
697 bbtkDebugDecTab("kernel",8);
698 return BlackBox::Pointer();
700 BlackBox::Pointer bb =i->second->NewBlackBox(name);
701 bbtkDebugDecTab("kernel",8);
705 //==========================================================================
709 //==========================================================================
710 /// Creates an instance of an adaptor of input type <typein> and
711 /// output type <typeout> with name <name>
712 BlackBox::Pointer Package::NewAdaptor(const DataInfo& typein,
713 const DataInfo& typeout,
714 const std::string& name) const
716 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
720 <<name<<"\")"<<bbtkendl);
722 AdaptorKey key(typein,typeout,
723 BlackBoxDescriptor::DEFAULT_ADAPTOR);
724 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
725 if (i == mAdaptorMap.end())
727 bbtkDebugDecTab("kernel",8);
728 return BlackBox::Pointer();
730 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
731 bbtkDebugDecTab("kernel",8);
735 //==========================================================================
737 //==========================================================================
738 /// Creates an instance of an adaptor of input type <typein> and
739 /// output type <typeout> with name <name>
740 BlackBox::Pointer Package::NewWidgetAdaptor(const DataInfo& typein,
741 const DataInfo& typeout,
742 const std::string& name) const
744 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
745 ">::NewWidgetAdaptor("
748 <<name<<"\")"<<bbtkendl);
750 AdaptorKey key(typein,typeout,
751 BlackBoxDescriptor::DEFAULT_GUI);
752 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
753 if (i == mAdaptorMap.end())
755 bbtkDebugDecTab("kernel",8);
756 return BlackBox::Pointer();
758 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
759 bbtkDebugDecTab("kernel",8);
763 //==========================================================================
767 //==========================================================================
768 /// Returns true is the package contains
769 /// an adaptor of input type <typein> and
770 /// output type <typeout>
771 /// If successfull then adaptor contains the black box type name
772 bool Package::FindWidgetAdaptor(const DataInfo& typein,
773 const DataInfo& typeout,
774 std::string& adaptor) const
776 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
777 ">::FindWidgetAdaptor("
779 <<typeout<<")"<<bbtkendl);
781 AdaptorKey key(/*typein*/
782 DataInfo(typeid(void),""),
784 BlackBoxDescriptor::DEFAULT_GUI);
785 // First try to find a single widget adaptor
786 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
787 if (i == mAdaptorMap.end())
789 bbtkDebugDecTab("kernel",8);
792 adaptor = i->second.lock()->GetTypeName();
793 bbtkDebugDecTab("kernel",8);
797 //==========================================================================
801 //==========================================================================
802 /// Returns true is the package contains
803 /// an adaptor of input type <typein> and
804 /// output type <typeout>
805 /// If successfull then adaptor contains the black box type name
806 bool Package::FindAdaptor(const DataInfo& typein,
807 const DataInfo& typeout,
808 std::string& adaptor) const
810 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
813 <<typeout<<")"<<bbtkendl);
815 AdaptorKey key(typein,typeout,
816 BlackBoxDescriptor::DEFAULT_ADAPTOR);
817 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
818 if (i == mAdaptorMap.end())
820 bbtkDebugDecTab("kernel",8);
823 adaptor = i->second.lock()->GetTypeName();
824 bbtkDebugDecTab("kernel",8);
828 //==========================================================================
831 //==========================================================================
832 /// Registers a black box descriptor in the package
833 bool Package::Register(BlackBoxDescriptor::Pointer d)
835 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\")"<<std::endl);
837 DescriptorMapType::iterator i = mDescriptorMap.find(d->GetTypeName());
838 if (i!=mDescriptorMap.end())
840 bbtkWarning("Package<"<<GetName()<<"> : Trying to register box type <"
841 <<d->GetTypeName()<<"> which is already in the package");
845 mDescriptorMap[d->GetTypeName()] = d;
847 d->SetPackage(GetThisPointer<Package>());
849 // If it is a default adaptor, also register it in the adaptors map
850 if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR )
852 bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\") : The box is an adaptor, inserting it in adaptors map ..."<<std::endl);
854 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
855 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
856 DataInfo infoin(typein,d->GetInputDescriptor("In")->GetNature());
857 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
858 AdaptorKey key(infoin,infoout,d->GetKind());
860 AdaptorMapType::const_iterator i;
861 i = mAdaptorMap.find(key);
862 if (i == mAdaptorMap.end())
864 mAdaptorMap[key] = d;
866 // If already an adaptor registered : error
869 if (i->second.lock()->GetTypeName() != d->GetTypeName())
871 bbtkError("Package <"<<GetName()<<
872 "> : trying to register black box <"
874 <<"> as default adaptor but there is already a default adaptor registered (<"
875 <<i->second.lock()->GetTypeName()<<">)");
879 // If it is a default adaptor, also register it in the adaptors map
880 else if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_GUI)
882 bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\") : The box is a widget adaptor, inserting it in adaptors map ..."<<std::endl);
884 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
885 DataInfo infoin(typeid(void),"");
886 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
887 AdaptorKey key(infoin,infoout,d->GetKind());
889 AdaptorMapType::const_iterator i;
890 i = mAdaptorMap.find(key);
891 if (i == mAdaptorMap.end())
893 mAdaptorMap[key] = d;
895 // If already an adaptor registered : error
898 if (i->second.lock()->GetTypeName() != d->GetTypeName())
900 bbtkError("Package <"<<GetName()<<
901 "> : trying to register black box <"
903 <<"> as default widget adaptor but there is already a default adaptor registered (<"
904 <<i->second.lock()->GetTypeName()<<">)");
910 bbtkDebugDecTab("kernel",8);
914 //==========================================================================
916 //===================================================================
917 void Package::Check() const
919 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
920 <<" ["<<GetName()<<"]"<<std::endl);
921 DescriptorMapType::const_iterator i;
922 for (i=mDescriptorMap.begin();
923 i!=mDescriptorMap.end();
926 i->second->Check(true);
928 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
929 <<" ["<<GetName()<<"] ... OK"<<std::endl);
931 //===================================================================
934 //==========================================================================
935 /// Changes the name of a black box type
936 void Package::ChangeDescriptorName( const std::string& oldname, const std::string& newname )
938 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
939 <<">::ChangeDescriptorName(\""<<oldname
940 <<"\",\""<<newname<<"\")"<<std::endl);
941 // Looking into the bb map
942 DescriptorMapType::iterator i = mDescriptorMap.find(oldname);
943 if (i == mDescriptorMap.end())
945 bbtkDebugDecTab("kernel",8);
946 bbtkError("ChangeDescriptorName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
949 i->second->SetTypeName(newname);
950 mDescriptorMap[newname] = i->second;
951 mDescriptorMap.erase(i);
953 bbtkDebugDecTab("kernel",8);
955 //==========================================================================
959 //==========================================================================
960 void Package::PrintHelpListDescriptors(bool description, bool adaptors) const
962 unsigned int lmax = 0;
963 std::vector<std::string> names;
964 std::vector<std::string> kinds;
965 std::vector<std::string> descrs;
967 DescriptorMapType::const_iterator i;
968 for (i=mDescriptorMap.begin();
969 i!=mDescriptorMap.end();
973 ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) )
975 std::string name(" ");
976 name += i->second->GetTypeName();
977 names.push_back(name);
980 if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR )
982 kind = std::string("[A]");
984 else if ( i->second->GetKind() ==
985 BlackBoxDescriptor::DEFAULT_ADAPTOR )
987 kind = std::string("[DA]");
989 kinds.push_back(kind);
991 unsigned int l = name.size()+kind.size();
992 if (l>lmax) lmax = l;
998 descr += i->second->GetDescription();
1000 descrs.push_back(descr);
1006 offs.append(lmax+3,' ');
1007 std::vector<std::string>::iterator ni,ci,di;
1008 for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin();
1009 ni != names.end(); ++ni, ++ci, ++di)
1012 space.append(lmax - ni->size() - ci->size(),' ');
1013 bbtkMessage("help",1,*ni << space << *ci );
1015 unsigned int dmax = 75 - lmax;
1016 // while (d.size() > dmax )
1019 bbtkMessage("help",1,d.substr(0,dmax) << "..." << std::endl);
1021 bbtkMessage("help",1,d << std::endl);
1022 // d = d.substr(dmax,d.size());
1027 //==========================================================================
1029 //==========================================================================
1030 /// Displays the list of adaptors of the package
1031 void Package::PrintHelpListAdaptors(bool description) const
1033 DescriptorMapType::const_iterator i;
1034 for (i=mDescriptorMap.begin();
1035 i!=mDescriptorMap.end();
1038 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD )
1040 bbtkMessage("help",1,
1041 " "<<i->second->GetTypeName());
1042 if ( i->second->GetKind() ==
1043 BlackBoxDescriptor::DEFAULT_ADAPTOR )
1045 bbtkMessage("help",1,
1050 bbtkMessage("help",1,
1051 " : "<<i->second->GetDescription());
1054 bbtkMessage("help",1,std::endl);
1058 AdaptorMapType::const_iterator i;
1059 for (i=mAdaptorMap.begin();
1060 i!=mAdaptorMap.end();
1063 bbtkMessage("help",1,
1064 " "<<i->second->GetTypeName());
1067 bbtkMessage("help",1,
1068 " : "<<i->second->GetDescription());
1071 bbtkMessage("help",1,std::endl);
1075 //==========================================================================
1077 //==========================================================================
1078 /// Prints help on a black box descriptor
1079 void Package::PrintHelpDescriptor(const std::string& name, bool full) const
1081 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
1082 <<">::PrintHelpDescriptor(\""
1083 <<name<<"\")"<<bbtkendl);
1085 DescriptorMapType::const_iterator i = mDescriptorMap.find(name);
1086 if (i == mDescriptorMap.end())
1088 bbtkDebugDecTab("kernel",8);
1089 bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
1091 // bbtkMessage("help",1,"["<<GetName()<<"] ");
1092 i->second->GetHelp(full);
1093 bbtkDebugDecTab("kernel",8);
1096 //==========================================================================
1099 //==========================================================================
1100 /// Returns true iff the package contains the box of name boxname
1101 bool Package::ContainsDescriptor(const std::string& name) const
1103 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
1104 <<">::ContainsDescriptor(\""
1105 <<name<<"\")"<<bbtkendl);
1107 DescriptorMapType::const_iterator i = mDescriptorMap.find(name);
1108 if (i == mDescriptorMap.end())
1110 bbtkDebugDecTab("kernel",8);
1113 bbtkDebugDecTab("kernel",8);
1116 //==========================================================================
1120 //==========================================================================
1121 void Package::CreateHtmlPage(const std::string& filename,
1122 const std::string& caller,
1123 const std::string& source,
1124 const std::string& custom_header,
1125 const std::string& custom_title,
1128 bool relative_link ) const
1130 bbtkDebugMessageInc("kernel",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
1131 <<filename<<"\")"<<bbtkendl);
1133 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1133"<<std::endl;
1135 //---------------------
1138 s.open(filename.c_str());
1141 bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
1144 //----------------------
1146 std::string title = "BBTK Package "+GetName()+" "+GetVersion();
1148 if (custom_title.length() != 0) title = custom_title;
1150 s << "<html lang=\"en\">\n";
1152 s << "<title>" << title << "</title>\n";
1153 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
1154 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
1155 s << "<meta name=\"generator\" content=\"\">\n";
1156 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
1157 //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
1158 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
1159 s << "pre.display { font-family:inherit }\n";
1160 s << "pre.format { font-family:inherit }\n";
1161 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
1162 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
1163 s << "pre.smallexample { font-size:smaller }\n";
1164 s << "pre.smalllisp { font-size:smaller }\n";
1165 s << "span.sc { font-variant:small-caps }\n";
1166 s << "span.roman { font-family:serif; font-weight:normal; } \n";
1167 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
1168 s << "--></style>\n";
1170 //----------------------
1172 //----------------------
1175 s << "<a name=\"Top\"></a>\n";
1177 //----------------------
1179 if ( custom_header.length() != 0)
1181 if ( custom_header != "none" )
1184 in.open(custom_header.c_str());
1187 bbtkError("Could not open file \""<<custom_header<<"\"");
1192 in.getline(buffer,512);
1193 std::string line(buffer);
1200 s << "<object data=\"" << custom_header
1201 << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
1202 << custom_header <<" could not be embedded.</object>\n";
1211 s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
1212 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1213 s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1214 << GetDescription() << "</TD></TR>\n";
1215 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1216 << GetAuthor() << "</TD></TR>\n";
1217 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1218 << GetCategory() << "</TD></TR>\n";
1219 s << "<TR><TD style='vertical-align: top;'><b> Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1220 << GetVersion() << "</TD></TR>\n";
1221 s << "<TR><TD style='vertical-align: top;'><b> bbtk Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1222 << bbtk::GetVersion() << "</TD></TR>\n";
1225 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1225"<<std::endl;
1226 //-------------------
1227 // Table of contents
1229 // s << "<div class=\"contents\">\n";
1230 s << "<p><b> Black Boxes : </b>\n";
1233 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1235 DescriptorMapType::const_iterator i;
1236 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1236"<<std::endl;
1237 for (i=mDescriptorMap.begin(); i!=mDescriptorMap.end(); ++i) {
1238 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD)
1241 std::string name = i->second->GetTypeName();
1242 Utilities::html_format(name);
1243 std::string descr = i->second->GetDescription();
1244 //Utilities::html_format(descr);
1245 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1246"<<std::endl;
1247 s << "<TD style='vertical-align: top;'>";
1248 s << " <a name=\"toc_"<<name
1249 <<"\" href=\"#"<<name<<"\">"
1252 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1261 //-------------------
1263 if (mAdaptorMap.size()>0)
1265 // s << "<div class=\"contents\">\n";
1266 s << "<p><b> Adaptors : </b>\n";
1268 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1268"<<std::endl;
1269 // DescriptorMapType::const_iterator i;
1270 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1271 for (i=mDescriptorMap.begin(); i!=mDescriptorMap.end();++i)
1273 if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD)
1276 std::string name = i->second->GetTypeName();
1277 Utilities::html_format(name);
1278 std::string descr = i->second->GetDescription();
1281 s << "<TD style='vertical-align: top;'>";
1282 s << " <a name=\"toc_"<<name
1283 <<"\" href=\"#"<<name<<"\">"
1286 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1296 // s << "<div class=\"node\">\n";
1298 // s << "<p><hr>\n";
1299 // s << "<a name=\"Top\"></a>\n";
1300 // s << "Top: <a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
1301 // s << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
1302 // s << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
1306 //----------------------
1309 //-------------------
1310 // Computes output directory from filename to pass it to
1311 // BlackBoxDescriptor::InsertHtmlHelp
1314 std::string::size_type slash_position = filename.find_last_of("/\\");
1317 if (slash_position != std::string::npos) {
1318 if (slash_position == 0)
1320 dir = filename.substr(0,slash_position);
1323 for (i=mDescriptorMap.begin();
1324 i!=mDescriptorMap.end();
1327 i->second->InsertHtmlHelp(s,detail,level,dir,relative_link);
1330 //----------------------
1335 ptm = gmtime ( &rawtime );
1338 s << "Automatically generated by <b>"<<caller<<"</b> "//from <b>"
1341 << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900
1342 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
1343 s << "</body></html>\n";
1345 //----------------------
1348 bbtkDebugDecTab("kernel",9);
1350 //==========================================================================
1352 //==========================================================================
1353 std::string Package::GetObjectName() const
1355 return std::string("Package '")+mName+std::string("'");
1357 //==========================================================================
1359 //==========================================================================
1360 std::string Package::GetObjectInfo() const
1362 std::stringstream i;
1363 i << " - "<<mDescriptorMap.size() << " boxes" << std::endl;
1364 if (mDynamicLibraryHandler)
1366 i<< " - Loaded from dynamic library"<<std::endl;
1370 //==========================================================================
1373 //==========================================================================
1374 size_t Package::GetObjectSize() const
1376 size_t s = Superclass::GetObjectSize();
1377 s += Package::GetObjectInternalSize();
1380 //==========================================================================
1381 //==========================================================================
1382 size_t Package::GetObjectInternalSize() const
1384 size_t s = sizeof(Package);
1387 //==========================================================================
1388 //==========================================================================
1389 size_t Package::GetObjectRecursiveSize() const
1391 size_t s = Superclass::GetObjectRecursiveSize();
1392 s += Package::GetObjectInternalSize();
1394 DescriptorMapType::const_iterator i;
1395 for (i = mDescriptorMap.begin(); i!=mDescriptorMap.end(); ++i )
1397 s += i->second->GetObjectRecursiveSize();
1401 //==========================================================================
1403 //==========================================================================
1404 std::set<Package::WeakPointer>
1405 Package::mReleasedDynamicallyLoadedPackages;
1406 //==========================================================================