2 /*=========================================================================
4 Module: $RCSfile: bbtkPackage.cxx,v $
6 Date: $Date: 2010/01/14 13:17:27 $
7 Version: $Revision: 1.33 $
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();
250 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
252 bbtkDebugMessage("package",2,"<== Package::Release('"<<
253 packname<<"')"<<bbtkendl);
257 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
258 long ndesc = pack.lock()->GetDescriptorMap().size();
259 long nrefs = pack.use_count();
261 bbtkDebugMessage("package",1," ... Package still alive ("
263 <<ndesc<<" descr / dyn="
264 <<dyn<<")"<<std::endl);
268 bbtkDebugMessage("package",1," ... Package has been released"
273 //==========================================================================
275 //==========================================================================
276 /// "Releases" the package
277 /// Signals the package that it can free the given descriptor
278 /// if they are no more used and free itself if it is no
280 /// Note : Any non-weak pointer on the package must have been freed
281 void Package::ReleaseBlackBoxDescriptor(Package::WeakPointer pack,
282 BlackBoxDescriptor::WeakPointer descr)
284 std::string packname = pack.lock()->mName;
285 std::string dname = descr.lock()->GetTypeName();
286 bbtkDebugMessage("package",3,"==> Package::ReleaseBlackBoxDescriptor('"<<
287 packname<<"','"<<dname<<"') : refs="
288 <<descr.use_count()<<bbtkendl);
290 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
291 long ndesc = pack.lock()->GetDescriptorMap().size();
292 long nrefs = pack.use_count();
294 bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
295 <<ndesc<<" descr / dynamically loaded = "
298 // A package is "free" from any external reference iff :
299 // i) It is not dynamically loaded and nrefs == ndesc
300 // (each desc references its package) or
301 // ii) It is dynamically loaded and nrefs == ndesc + 1
302 // (A dynamic library holds a static pointer on the package it contains
303 // which is allocated when the PACKAGENAMEGetPackage() func is called,
304 // and descallocated (reset) by PACKAGENAMEDeletePackage())
305 if (nrefs == ndesc + dyn)
310 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
312 PackageReleaseBlackBoxDescriptorInternal(pack,dname);
316 // If the package is released and dynamically loaded
317 // then put it in the static list mReleasedDynamicallyLoadedPackages
318 UnLoadDynamicLibrary(pack,false);
320 bbtkDebugMessage("package",4,"<== Package::ReleaseBlackBoxDescriptor('"<<
321 packname<<"','"<<dname<<"'): refs="
322 <<descr.use_count()<<bbtkendl);
326 long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0;
327 long ndesc = pack.lock()->GetDescriptorMap().size();
328 long nrefs = pack.use_count();
330 bbtkDebugMessage("package",3," ... Package still alive ("
332 <<ndesc<<" descr / dyn="
333 <<dyn<<")"<<std::endl);
337 bbtkDebugMessage("package",3," ... Package has been released"
342 //==========================================================================
344 //==========================================================================
345 /// Opens a dynamic library which contains a bbtk package
346 /// Returns the handler
347 /// Load the package management symbols from the lib
348 /// returns false if a problem occured hence can be used
349 /// to test that a dyn lib is a valid bbtk package lib
350 /// NB : The BBTK version exported from the library
351 /// is tested against the current bbtk version
352 DynamicLibraryHandler Package::OpenDynamicLibrary
353 ( const std::string& libname,
354 const std::string& package_name,
355 DLGetPackageFunction& getpack,
356 DLDeletePackageFunction& delpack)
358 bbtkDebugMessage("package",3,"==> Package::OpenDynamicLibrary("
359 <<libname<<")"<<std::endl);
360 #if defined(__GNUC__)
364 handler = dlopen(libname.c_str(), BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
365 //EED handler = dlopen(libname.c_str(), RTLD_LAZY | RTLD_LOCAL );
369 bbtkMessage("package",2,
370 "Could not open shared library [" <<libname<<"] : "
371 <<dlerror() << std::endl);
375 bbtkDebugMessage("package",3,"* Shared lib ["<<libname<<"] open"<<std::endl);
377 // Loads the Package bbtk version function
378 std::string getvername(package_name);
380 BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
381 DLGetPackageBBTKVersionFunction getbbtkversion
382 = (DLGetPackageBBTKVersionFunction)(dlsym(handler,getvername.c_str()));
385 bbtkDebugMessage("package",3,"***"<<std::endl);
386 bbtkMessage("package",2,
387 "Shared library ["<<libname
388 <<"] is not a valid bbtk package."
389 <<" Symbol ["<<getvername<<"] :"<<dlerror()<< std::endl);
394 bbtkDebugMessage("package",3,"* Symbol ["<<getvername
395 <<"] found"<<std::endl);
397 if (getbbtkversion() != bbtk::GetVersion())
399 bbtkMessage("package",2,
400 "Shared library ["<<libname
401 <<"] was build with bbtk version "
403 <<" but the current program runs with version "
404 <<bbtk::GetVersion()<<" : cannot load it"<<std::endl);
410 bbtkDebugMessage("package",3,"* Package bbtk version '"<<getbbtkversion()<<"' matches"<<std::endl);
411 // Loads the Package get function
412 std::string getpackname(package_name);
413 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
414 getpack = (DLGetPackageFunction)(dlsym(handler, getpackname.c_str()));
417 bbtkMessage("package",2,
418 "Shared library ["<<libname
419 <<"] is not a valid bbtk package."
420 <<" Symbol ["<<getpackname<<"] :"<<dlerror()<< std::endl);
425 bbtkDebugMessage("package",3,"* Symbol ["<<getpackname<<"] found"<<std::endl);
426 // Loads the Package delete function
428 std::string delpackname(package_name);
429 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
430 delpack = (DLDeletePackageFunction)(dlsym(handler, delpackname.c_str()));
433 bbtkMessage("package",2,
434 "Shared library ["<<libname
435 <<"] is not a valid bbtk package."
436 <<" Symbol ["<<delpackname<<"] :"<<dlerror()<< std::endl);
440 bbtkDebugMessage("package",3,"* Symbol ["<<delpackname<<"] found"<<std::endl);
441 #elif defined(_WIN32)
447 handler = LoadLibrary(libname.c_str());
450 bbtkMessage("package",2,
451 "Could not open shared library [" <<libname<<"]"
453 DWORD dwErrorCode = 0;
454 dwErrorCode = GetLastError();
455 bbtkMessage("package",2,
456 "Windows Error: [" << dwErrorCode <<"]"
462 // Loads the Package bbtk version function
463 std::string getvername(package_name);
465 BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
466 DLGetPackageBBTKVersionFunction getbbtkversion
467 = (DLGetPackageBBTKVersionFunction)(GetProcAddress(handler,
468 getvername.c_str()));
471 FreeLibrary(handler);
472 bbtkMessage("package",2,
473 "Shared library ["<<libname
474 <<"] is not a valid bbtk package."
475 <<" Symbol ["<<getbbtkversion<<"] not found"<< std::endl);
480 if (getbbtkversion() != bbtk::GetVersion())
482 FreeLibrary(handler);
483 bbtkMessage("package",2,
484 "Shared library ["<<libname
485 <<"] was build with bbtk version "
487 <<" but the current program runs with version "
488 <<bbtk::GetVersion()<<" : cannot load it"<<std::endl);
493 // Loads the Package get function
494 std::string getpackname(package_name);
495 getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
496 getpack = (DLGetPackageFunction)(GetProcAddress(handler, getpackname.c_str()));
499 FreeLibrary(handler);
500 bbtkMessage("package",2,
501 "Shared library ["<<libname
502 <<"] is not a valid bbtk package."
503 <<" Symbol ["<<getpackname<<"] not found"<< std::endl);
507 // Loads the Package delete function
508 std::string delpackname(package_name);
509 delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
510 delpack = (DLDeletePackageFunction)(GetProcAddress(handler, delpackname.c_str()));
513 FreeLibrary(handler);
514 bbtkMessage("package",2,
515 "Shared library ["<<libname
516 <<"] is not a valid bbtk package."
517 <<" Symbol ["<<delpackname<<"] not found"<< std::endl);
522 bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
527 //==========================================================================
529 //==========================================================================
530 /// Loads a package from a dynamic library
531 Package::Pointer Package::CreateFromDynamicLibrary(const std::string& libname,
532 const std::string& pkgname,
533 const std::string& path)
535 bbtkDebugMessage("package",1,"==> Package::CreateFromDynamicLibrary("
536 <<libname<<")"<<std::endl);
538 DLGetPackageFunction gf;
539 DLDeletePackageFunction df;
540 DynamicLibraryHandler h = Package::OpenDynamicLibrary(libname,
543 if (h==0) return Package::Pointer();
544 Package::Pointer p = gf();
545 p->mDynamicLibraryHandler = h;
546 p->mDLDeletePackageFunction = df;
548 std::string separator =
549 ConfigurationFile::GetInstance().Get_file_separator ();
550 //BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
551 std::string docreldoc =
552 separator + "bbdoc" + separator + pkgname + separator + "index.html";
554 ".." + separator + ".." + docreldoc;
555 std::string doc = path + separator + ".." + separator
556 + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
560 p->SetDocRelativeURL(reldoc);
562 bbtkDebugMessage("package",2,"<== Package::CreateFromDynamicLibrary("
563 <<libname<<") .. OK"<<std::endl);
566 //==========================================================================
571 //==========================================================================
572 /// UnLoads the package dynamic library (if any)
573 void Package::UnLoadDynamicLibrary(Package::WeakPointer pack, bool doit)
575 if (pack.expired() || (!pack.lock()->mDynamicLibraryHandler))
579 std::string packname = pack.lock()->GetName();
580 bbtkDebugMessage("package",5,"==> Package::UnLoadDynamicLibrary('"
584 if (!pack.lock()->GetDescriptorMap().empty())
587 bbtkDebugMessage("package",5," Package not empty ... abort"
591 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
592 <<"DescriptorMap not empty "
593 <<BBTK_INTERNAL_ERROR_MESSAGE);
598 if (pack.use_count()!=1)
600 bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
601 <<"empty dl package with external refs"
602 <<BBTK_INTERNAL_ERROR_MESSAGE);
608 bbtkDebugMessage("package",5,"==> dynamic library for package '"
609 <<packname<<"' closed"
614 mReleasedDynamicallyLoadedPackages.insert(pack);
615 bbtkDebugMessage("package",1,"==> package '"<<packname
616 <<"' put in the 'to unload' list"
621 bbtkDebugMessage("package",5,"<== Package::UnLoadDynamicLibrary('"
626 //==========================================================================
628 //==========================================================================
629 /// UnLoads released packages that were loaded dynamically
630 /// see UnLoadDynamicLibrary and ReleaseBlackBoxDescriptor
631 void Package::UnLoadReleasedDynamicallyLoadedPackages()
633 bbtkDebugMessage("package",5,"==> Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
635 std::set<Package::WeakPointer>::iterator i;
638 if(mReleasedDynamicallyLoadedPackages.size()>0){
639 for (i=mReleasedDynamicallyLoadedPackages.begin();
640 i!=mReleasedDynamicallyLoadedPackages.end();
643 if (!i->expired()) UnLoad(*i);
647 bbtkDebugMessage("package",5,"<== Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
649 //==========================================================================
651 //==========================================================================
652 void Package::UnLoad(Package::WeakPointer pack)
654 std::string packname = pack.lock()->GetName();
655 bbtkDebugMessage("package",6,"==> Package::UnLoad("<<packname<<")"<<std::endl);
657 Package* p = pack.lock().get();
659 DynamicLibraryHandler h = p->mDynamicLibraryHandler;
661 // deletes the package
662 p->mDLDeletePackageFunction();
664 // closes the dl handler
665 #if defined(__GNUC__)
668 printf("EED Package::UnLoad ERROR %s\n", packname.c_str() );
669 bbtkWarning("Failed to close dynamic library for package '"<<packname
673 #elif defined(_WIN32)
677 bbtkDebugMessage("package",1,"==> dynamic library for package '"
678 <<packname<<"' closed"
680 bbtkDebugMessage("package",6," ... dynamic library unloaded"<<std::endl);
682 //==========================================================================
684 //==========================================================================
685 /// Creates an instance of a black box of type <type> with name <name>
686 BlackBox::Pointer Package::NewBlackBox(const std::string& type,
687 const std::string& name) const
689 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::NewBlackBox(\""<<type<<"\",\""<<name<<"\")"<<bbtkendl);
691 DescriptorMapType::const_iterator i = mDescriptorMap.find(type);
692 if (i == mDescriptorMap.end())
694 bbtkDebugDecTab("kernel",8);
695 return BlackBox::Pointer();
697 BlackBox::Pointer bb =i->second->NewBlackBox(name);
698 bbtkDebugDecTab("kernel",8);
702 //==========================================================================
706 //==========================================================================
707 /// Creates an instance of an adaptor of input type <typein> and
708 /// output type <typeout> with name <name>
709 BlackBox::Pointer Package::NewAdaptor(const DataInfo& typein,
710 const DataInfo& typeout,
711 const std::string& name) const
713 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
717 <<name<<"\")"<<bbtkendl);
719 AdaptorKey key(typein,typeout,
720 BlackBoxDescriptor::DEFAULT_ADAPTOR);
721 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
722 if (i == mAdaptorMap.end())
724 bbtkDebugDecTab("kernel",8);
725 return BlackBox::Pointer();
727 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
728 bbtkDebugDecTab("kernel",8);
732 //==========================================================================
734 //==========================================================================
735 /// Creates an instance of an adaptor of input type <typein> and
736 /// output type <typeout> with name <name>
737 BlackBox::Pointer Package::NewWidgetAdaptor(const DataInfo& typein,
738 const DataInfo& typeout,
739 const std::string& name) const
741 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
742 ">::NewWidgetAdaptor("
745 <<name<<"\")"<<bbtkendl);
747 AdaptorKey key(typein,typeout,
748 BlackBoxDescriptor::DEFAULT_GUI);
749 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
750 if (i == mAdaptorMap.end())
752 bbtkDebugDecTab("kernel",8);
753 return BlackBox::Pointer();
755 BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
756 bbtkDebugDecTab("kernel",8);
760 //==========================================================================
764 //==========================================================================
765 /// Returns true is the package contains
766 /// an adaptor of input type <typein> and
767 /// output type <typeout>
768 /// If successfull then adaptor contains the black box type name
769 bool Package::FindWidgetAdaptor(const DataInfo& typein,
770 const DataInfo& typeout,
771 std::string& adaptor) const
773 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
774 ">::FindWidgetAdaptor("
776 <<typeout<<")"<<bbtkendl);
778 AdaptorKey key(/*typein*/
779 DataInfo(typeid(void),""),
781 BlackBoxDescriptor::DEFAULT_GUI);
782 // First try to find a single widget adaptor
783 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
784 if (i == mAdaptorMap.end())
786 bbtkDebugDecTab("kernel",8);
789 adaptor = i->second.lock()->GetTypeName();
790 bbtkDebugDecTab("kernel",8);
794 //==========================================================================
798 //==========================================================================
799 /// Returns true is the package contains
800 /// an adaptor of input type <typein> and
801 /// output type <typeout>
802 /// If successfull then adaptor contains the black box type name
803 bool Package::FindAdaptor(const DataInfo& typein,
804 const DataInfo& typeout,
805 std::string& adaptor) const
807 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
810 <<typeout<<")"<<bbtkendl);
812 AdaptorKey key(typein,typeout,
813 BlackBoxDescriptor::DEFAULT_ADAPTOR);
814 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
815 if (i == mAdaptorMap.end())
817 bbtkDebugDecTab("kernel",8);
820 adaptor = i->second.lock()->GetTypeName();
821 bbtkDebugDecTab("kernel",8);
825 //==========================================================================
828 //==========================================================================
829 /// Registers a black box descriptor in the package
830 bool Package::Register(BlackBoxDescriptor::Pointer d)
832 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\")"<<std::endl);
834 DescriptorMapType::iterator i = mDescriptorMap.find(d->GetTypeName());
835 if (i!=mDescriptorMap.end())
837 bbtkWarning("Package<"<<GetName()<<"> : Trying to register box type <"
838 <<d->GetTypeName()<<"> which is already in the package");
842 mDescriptorMap[d->GetTypeName()] = d;
844 d->SetPackage(GetThisPointer<Package>());
846 // If it is a default adaptor, also register it in the adaptors map
847 if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR )
849 bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\") : The box is an adaptor, inserting it in adaptors map ..."<<std::endl);
851 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
852 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
853 DataInfo infoin(typein,d->GetInputDescriptor("In")->GetNature());
854 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
855 AdaptorKey key(infoin,infoout,d->GetKind());
857 AdaptorMapType::const_iterator i;
858 i = mAdaptorMap.find(key);
859 if (i == mAdaptorMap.end())
861 mAdaptorMap[key] = d;
863 // If already an adaptor registered : error
866 if (i->second.lock()->GetTypeName() != d->GetTypeName())
868 bbtkError("Package <"<<GetName()<<
869 "> : trying to register black box <"
871 <<"> as default adaptor but there is already a default adaptor registered (<"
872 <<i->second.lock()->GetTypeName()<<">)");
876 // If it is a default adaptor, also register it in the adaptors map
877 else if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_GUI)
879 bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\") : The box is a widget adaptor, inserting it in adaptors map ..."<<std::endl);
881 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
882 DataInfo infoin(typeid(void),"");
883 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
884 AdaptorKey key(infoin,infoout,d->GetKind());
886 AdaptorMapType::const_iterator i;
887 i = mAdaptorMap.find(key);
888 if (i == mAdaptorMap.end())
890 mAdaptorMap[key] = d;
892 // If already an adaptor registered : error
895 if (i->second.lock()->GetTypeName() != d->GetTypeName())
897 bbtkError("Package <"<<GetName()<<
898 "> : trying to register black box <"
900 <<"> as default widget adaptor but there is already a default adaptor registered (<"
901 <<i->second.lock()->GetTypeName()<<">)");
907 bbtkDebugDecTab("kernel",8);
911 //==========================================================================
913 //===================================================================
914 void Package::Check() const
916 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
917 <<" ["<<GetName()<<"]"<<std::endl);
918 DescriptorMapType::const_iterator i;
919 for (i=mDescriptorMap.begin();
920 i!=mDescriptorMap.end();
923 i->second->Check(true);
925 bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
926 <<" ["<<GetName()<<"] ... OK"<<std::endl);
928 //===================================================================
931 //==========================================================================
932 /// Changes the name of a black box type
933 void Package::ChangeDescriptorName( const std::string& oldname, const std::string& newname )
935 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
936 <<">::ChangeDescriptorName(\""<<oldname
937 <<"\",\""<<newname<<"\")"<<std::endl);
938 // Looking into the bb map
939 DescriptorMapType::iterator i = mDescriptorMap.find(oldname);
940 if (i == mDescriptorMap.end())
942 bbtkDebugDecTab("kernel",8);
943 bbtkError("ChangeDescriptorName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
946 i->second->SetTypeName(newname);
947 mDescriptorMap[newname] = i->second;
948 mDescriptorMap.erase(i);
950 bbtkDebugDecTab("kernel",8);
952 //==========================================================================
956 //==========================================================================
957 void Package::PrintHelpListDescriptors(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 DescriptorMapType::const_iterator i;
965 for (i=mDescriptorMap.begin();
966 i!=mDescriptorMap.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::PrintHelpListAdaptors(bool description) const
1030 DescriptorMapType::const_iterator i;
1031 for (i=mDescriptorMap.begin();
1032 i!=mDescriptorMap.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 descriptor
1076 void Package::PrintHelpDescriptor(const std::string& name, bool full) const
1078 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
1079 <<">::PrintHelpDescriptor(\""
1080 <<name<<"\")"<<bbtkendl);
1082 DescriptorMapType::const_iterator i = mDescriptorMap.find(name);
1083 if (i == mDescriptorMap.end())
1085 bbtkDebugDecTab("kernel",8);
1086 bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
1088 // bbtkMessage("help",1,"["<<GetName()<<"] ");
1089 i->second->GetHelp(full);
1090 bbtkDebugDecTab("kernel",8);
1093 //==========================================================================
1096 //==========================================================================
1097 /// Returns true iff the package contains the box of name boxname
1098 bool Package::ContainsDescriptor(const std::string& name) const
1100 bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
1101 <<">::ContainsDescriptor(\""
1102 <<name<<"\")"<<bbtkendl);
1104 DescriptorMapType::const_iterator i = mDescriptorMap.find(name);
1105 if (i == mDescriptorMap.end())
1107 bbtkDebugDecTab("kernel",8);
1110 bbtkDebugDecTab("kernel",8);
1113 //==========================================================================
1117 //==========================================================================
1118 void Package::CreateHtmlPage(const std::string& filename,
1119 const std::string& caller,
1120 const std::string& source,
1121 const std::string& custom_header,
1122 const std::string& custom_title,
1125 bool relative_link ) const
1127 bbtkDebugMessageInc("kernel",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
1128 <<filename<<"\")"<<bbtkendl);
1130 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1133"<<std::endl;
1132 //---------------------
1135 s.open(filename.c_str());
1138 bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
1141 //----------------------
1143 std::string title = "BBTK Package "+GetName()+" "+GetVersion();
1145 if (custom_title.length() != 0) title = custom_title;
1147 s << "<html lang=\"en\">\n";
1149 s << "<title>" << title << "</title>\n";
1150 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
1151 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
1152 s << "<meta name=\"generator\" content=\"\">\n";
1153 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
1154 //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
1155 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
1156 s << "pre.display { font-family:inherit }\n";
1157 s << "pre.format { font-family:inherit }\n";
1158 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
1159 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
1160 s << "pre.smallexample { font-size:smaller }\n";
1161 s << "pre.smalllisp { font-size:smaller }\n";
1162 s << "span.sc { font-variant:small-caps }\n";
1163 s << "span.roman { font-family:serif; font-weight:normal; } \n";
1164 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
1165 s << "--></style>\n";
1167 //----------------------
1169 //----------------------
1172 s << "<a name=\"Top\"></a>\n";
1174 //----------------------
1176 if ( custom_header.length() != 0)
1178 if ( custom_header != "none" )
1181 in.open(custom_header.c_str());
1184 bbtkError("Could not open file \""<<custom_header<<"\"");
1189 in.getline(buffer,512);
1190 std::string line(buffer);
1197 s << "<object data=\"" << custom_header
1198 << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
1199 << custom_header <<" could not be embedded.</object>\n";
1208 s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
1209 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1210 s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1211 << GetDescription() << "</TD></TR>\n";
1212 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1213 << GetAuthor() << "</TD></TR>\n";
1214 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1215 << GetCategory() << "</TD></TR>\n";
1216 s << "<TR><TD style='vertical-align: top;'><b> Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1217 << GetVersion() << "</TD></TR>\n";
1218 s << "<TR><TD style='vertical-align: top;'><b> bbtk Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
1219 << bbtk::GetVersion() << "</TD></TR>\n";
1222 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1225"<<std::endl;
1223 //-------------------
1224 // Table of contents
1226 // s << "<div class=\"contents\">\n";
1227 s << "<p><b> Black Boxes : </b>\n";
1230 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1232 DescriptorMapType::const_iterator i;
1233 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1236"<<std::endl;
1234 for (i=mDescriptorMap.begin(); i!=mDescriptorMap.end(); ++i) {
1235 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD)
1238 std::string name = i->second->GetTypeName();
1239 Utilities::html_format(name);
1240 std::string descr = i->second->GetDescription();
1241 //Utilities::html_format(descr);
1242 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1246"<<std::endl;
1244 s << "<TD style='vertical-align: top;'>";
1245 s << " <a name=\"toc_"<<name
1246 <<"\" href=\"#"<<name<<"\">"
1249 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1258 //-------------------
1260 if (mAdaptorMap.size()>0)
1262 // s << "<div class=\"contents\">\n";
1263 s << "<p><b> Adaptors : </b>\n";
1265 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1268"<<std::endl;
1266 // DescriptorMapType::const_iterator i;
1267 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
1268 for (i=mDescriptorMap.begin(); i!=mDescriptorMap.end();++i)
1270 if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD)
1273 std::string name = i->second->GetTypeName();
1274 Utilities::html_format(name);
1275 std::string descr = i->second->GetDescription();
1278 s << "<TD style='vertical-align: top;'>";
1279 s << " <a name=\"toc_"<<name
1280 <<"\" href=\"#"<<name<<"\">"
1283 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1293 // s << "<div class=\"node\">\n";
1295 // s << "<p><hr>\n";
1296 // s << "<a name=\"Top\"></a>\n";
1297 // s << "Top: <a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
1298 // s << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
1299 // s << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
1303 //----------------------
1306 //-------------------
1307 // Computes output directory from filename to pass it to
1308 // BlackBoxDescriptor::InsertHtmlHelp
1311 std::string::size_type slash_position = filename.find_last_of("/\\");
1314 if (slash_position != std::string::npos) {
1315 if (slash_position == 0)
1317 dir = filename.substr(0,slash_position);
1320 for (i=mDescriptorMap.begin();
1321 i!=mDescriptorMap.end();
1324 i->second->InsertHtmlHelp(s,detail,level,dir,relative_link);
1327 //----------------------
1332 ptm = gmtime ( &rawtime );
1335 s << "Automatically generated by <b>"<<caller<<"</b> "//from <b>"
1338 << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900
1339 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
1340 s << "</body></html>\n";
1342 //----------------------
1345 bbtkDebugDecTab("kernel",9);
1347 //==========================================================================
1349 //==========================================================================
1350 std::string Package::GetObjectName() const
1352 return std::string("Package '")+mName+std::string("'");
1354 //==========================================================================
1356 //==========================================================================
1357 std::string Package::GetObjectInfo() const
1359 std::stringstream i;
1360 i << " - "<<mDescriptorMap.size() << " boxes" << std::endl;
1361 if (mDynamicLibraryHandler)
1363 i<< " - Loaded from dynamic library"<<std::endl;
1367 //==========================================================================
1370 //==========================================================================
1371 size_t Package::GetObjectSize() const
1373 size_t s = Superclass::GetObjectSize();
1374 s += Package::GetObjectInternalSize();
1377 //==========================================================================
1378 //==========================================================================
1379 size_t Package::GetObjectInternalSize() const
1381 size_t s = sizeof(Package);
1384 //==========================================================================
1385 //==========================================================================
1386 size_t Package::GetObjectRecursiveSize() const
1388 size_t s = Superclass::GetObjectRecursiveSize();
1389 s += Package::GetObjectInternalSize();
1391 DescriptorMapType::const_iterator i;
1392 for (i = mDescriptorMap.begin(); i!=mDescriptorMap.end(); ++i )
1394 s += i->second->GetObjectRecursiveSize();
1398 //==========================================================================
1400 //==========================================================================
1401 std::set<Package::WeakPointer>
1402 Package::mReleasedDynamicallyLoadedPackages;
1403 //==========================================================================