]> Creatis software - bbtk.git/blob - kernel/src/bbtkPackage.cxx
Merge branch 'wt-version'
[bbtk.git] / kernel / src / bbtkPackage.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28
29 /*=========================================================================                                                                               
30   Program:   bbtk
31   Module:    $RCSfile: bbtkPackage.cxx,v $
32   Language:  C++
33   Date:      $Date: 2012/11/16 08:49:01 $
34   Version:   $Revision: 1.37 $
35 =========================================================================*/
36
37
38
39 /**
40  *\file
41  *\brief Class bbtk::Package : registers black boxes descriptors and is able to create instances of the black boxes registered.
42  */
43 #include "bbtkPackage.h"
44 #include "bbtkComplexBlackBoxDescriptor.h"
45 #include "bbtkMessageManager.h"
46 #include "bbtkConfigurationFile.h"
47 #include <fstream>
48 #include <time.h>
49 #include "bbtkUtilities.h"
50
51 namespace bbtk
52 {
53   
54
55
56   //==========================================================================
57   /// Creates a new package
58   Package::Pointer Package::New(const std::string& name,
59                                 const std::string& author,
60                                 const std::string& description,
61                                 const std::string& version) 
62   {
63     bbtkDebugMessage("object",1,"##> Package::New('"<<name<<"',...)"
64                      <<bbtkendl);
65     Package::Pointer p = MakePointer(new Package(name,
66                                                author,
67                                                description,
68                                                version));
69     bbtkDebugMessage("object",2,"<## Package::New('"<<name<<"',...)"
70                      <<bbtkendl);
71     return p;
72   }
73   //==========================================================================
74
75   //==========================================================================
76   /// Ctor with the name of the package
77   Package::Package(const std::string& name,
78                    const std::string& author,
79                    const std::string& description,
80                    const std::string& version) 
81     :
82     mDynamicLibraryHandler(0),
83     mName(name),
84     mAuthor(author),
85     mDescription(description),
86     mVersion(version)
87   {
88
89     bbtkDebugMessage("object",2,"==> Package('"<<name<<"',...)"
90                      <<bbtkendl);
91
92     std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
93     char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
94     std::string url = default_doc_dir; 
95     if (c != '/' && c !='\\') url = url + "/";
96     url = url +  "temp_dir/" + name + "/index.html";    
97     
98     SetDocURL(url);
99     SetDocRelativeURL("Relative url not set");
100
101     /*
102     std::string relurl(BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH));
103     relurl += "/packages/"+name+"/bbdoc/index.html";
104     std::string url = ConfigurationFile::GetInstance().Get_url()
105       + relurl; 
106     SetDocURL(url);
107     SetDocRelativeURL(relurl);   
108     */
109
110     //    std::cout  << "   url=["<<url<<"]"<<std::endl;
111     //    std::cout  << "relurl=["<<relurl<<"]"<<std::endl;
112     bbtkDebugMessage("object",2,"<== Package::Package('"<<name<<"',...) OK"
113                      <<bbtkendl);
114
115   }
116   //==========================================================================
117
118
119
120   //==========================================================================
121   /// Dtor
122   Package::~Package()
123   {
124     bbtkDebugMessage("object",2,"==> ~Package(\""<<mName<<"\")"<<bbtkendl);
125   }
126   //==========================================================================
127
128
129   //==========================================================================
130   void PackageReleaseBlackBoxDescriptorInternal(Package::WeakPointer pack,
131                                                 const std::string& descname)
132   {
133     // Try to release descriptor
134     std::string packname = pack.lock()->GetName();
135     
136     bbtkDebugMessage("package",5,"--- Releasing descriptor '"
137                      <<packname<<"::"<<descname<<"'"<<bbtkendl);
138     
139     
140     Package::DescriptorMapType::iterator desc = 
141       pack.lock()->GetDescriptorMap().find(descname);
142     if (desc ==  pack.lock()->GetDescriptorMap().end())
143       {
144         bbtkDebugMessage("package",5,
145                          "    Descriptor has already been released"
146                         <<bbtkendl);
147         return;
148                   }
149     //    bbtkDebugMessage("package",3,
150     //               "    Trying unreferencing it ... "<<std::endl);
151     BlackBoxDescriptor::WeakPointer pdesc = desc->second;
152     desc->second.reset();
153     // if it is dead : remove it 
154     if (pdesc.expired()) 
155       {
156         bbtkDebugMessage("package",2," ==> '"<<packname<<"::"<<descname<<"' Descriptor expired"<<bbtkendl);
157         if (pack.expired()) 
158           {
159             bbtkDebugMessage("package",2,
160                              "     ... and caused its package death"
161                              <<bbtkendl);
162             return;
163           } // pack.expired
164         desc = pack.lock()->GetDescriptorMap().find(descname);
165         if (desc !=  pack.lock()->GetDescriptorMap().end())       pack.lock()->GetDescriptorMap().erase(desc);
166       }   else    {  //pdesc.expired
167       bbtkDebugMessage("package",5,"    ... Descriptor still alive ("
168                        <<pdesc.use_count()<<" refs)"
169                        <<bbtkendl);
170       pack.lock()->GetDescriptorMap()[descname] = pdesc.lock();
171     } // pdesc.expired
172   }
173   //==========================================================================
174
175
176   //==========================================================================
177   /// Release
178   void Package::Release(Package::WeakPointer pack)
179   {
180     std::string packname = pack.lock()->mName;
181     bbtkDebugMessage("package",1,"==> Package::Release('"<<
182                      packname<<"')"<<bbtkendl);
183
184     long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0; 
185     long ndesc = pack.lock()->GetDescriptorMap().size();
186     long nrefs = pack.use_count();
187
188     bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
189                      <<ndesc<<" descr / dyn="
190                      <<dyn<<std::endl);
191
192     // A package is "free" from any external reference iff :
193     // i) It is not dynamically loaded and nrefs == ndesc 
194     // (each desc references its package) or
195     // ii) It is dynamically loaded and nrefs == ndesc + 1
196     // (A dynamic library holds a static pointer on the package it contains
197     //  which is allocated when the PACKAGENAMEGetPackage() func is called,
198     //  and descallocated (reset) by PACKAGENAMEDeletePackage())
199     if (nrefs == ndesc + dyn) 
200       {
201         bbtkDebugMessage("package",5,
202                          " -> No more external ref : checking descriptors"
203                          <<bbtkendl);
204         // We must take care that removing refs on descriptors 
205         // can lead to their deletion which can in turn unref 
206         // internal boxes which can release their descriptors hence 
207         // call Package::ReleaseBlackBoxDescriptor 
208         // As a consequence during descriptors release :
209         // 1) The map can change dynamically : we cannot iterate over it 
210         //    as any iterator can become invalid
211         // 2) The package can auto-destruct : we must test its existence 
212         //    after each release
213         // We must also take care of not locking the package pointer 
214         // or any ref count check in Package::ReleaseBlackBoxDescriptor
215         // would be wrong
216
217         // The list of descriptors names at start
218         std::vector<std::string> descnamelist;
219         DescriptorMapType::iterator i;
220         for (i=pack.lock()->mDescriptorMap.begin();
221              i!= pack.lock()->mDescriptorMap.end();
222              ++i)
223           descnamelist.push_back(i->first);
224
225         // Iterator over the initial names
226         std::vector<std::string>::iterator descname;
227         for (descname=descnamelist.begin();
228              descname!=descnamelist.end();
229              ++descname)
230           {
231             // Is package still alive ?
232             if (pack.expired()) 
233               {
234                 bbtkDebugMessage("package",1,"--- Package::Release('"<<
235                                  packname
236                                  <<"') : package expired during release : bailing out"<<bbtkendl);
237                 break;
238               }
239   
240 #if defined(MACOSX)
241                   BlackBoxDescriptor::Pointer desc = pack.lock()->mDescriptorMap[*descname];
242                   if ( (dyn==0) || (boost::dynamic_pointer_cast<ComplexBlackBoxDescriptor>(desc)) )
243                   {
244                           PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
245                   } 
246 #else
247                   PackageReleaseBlackBoxDescriptorInternal(pack,*descname);
248 #endif
249
250           }
251
252         //
253         UnLoadDynamicLibrary(pack);
254         // Unload orphan dl packages 
255         Package::UnLoadReleasedDynamicallyLoadedPackages();
256
257       }
258     
259 #ifdef BBTK_COMPILE_DEBUG_MESSAGES
260
261     bbtkDebugMessage("package",2,"<== Package::Release('"<<
262                      packname<<"')"<<bbtkendl);
263
264     if (!pack.expired())
265       {
266         long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0; 
267         long ndesc = pack.lock()->GetDescriptorMap().size();
268         long nrefs = pack.use_count();
269         
270         bbtkDebugMessage("package",1," ... Package still alive ("
271                          <<nrefs<<" refs / "
272                          <<ndesc<<" descr / dyn="
273                          <<dyn<<")"<<std::endl);
274       }
275     else 
276       {
277         bbtkDebugMessage("package",1," ... Package has been released"
278                          <<std::endl);
279       }
280 #endif
281   }
282   //==========================================================================
283
284   //==========================================================================
285   /// "Releases" the package
286   /// Signals the package that it can free the given descriptor
287   /// if they are no more used and free itself if it is no
288   /// more used
289   /// Note : Any non-weak pointer on the package must have been freed
290   void Package::ReleaseBlackBoxDescriptor(Package::WeakPointer pack,
291                                           BlackBoxDescriptor::WeakPointer descr)
292   {
293     std::string packname = pack.lock()->mName;
294     std::string dname = descr.lock()->GetTypeName();    
295     bbtkDebugMessage("package",3,"==> Package::ReleaseBlackBoxDescriptor('"<<
296                      packname<<"','"<<dname<<"') : refs="
297                      <<descr.use_count()<<bbtkendl);
298
299     long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0; 
300     long ndesc = pack.lock()->GetDescriptorMap().size();
301     long nrefs = pack.use_count();
302
303     bbtkDebugMessage("package",5," "<<nrefs<<" refs / "
304                      <<ndesc<<" descr / dynamically loaded = "
305                      <<dyn<<std::endl);
306
307     // A package is "free" from any external reference iff :
308     // i) It is not dynamically loaded and nrefs == ndesc 
309     // (each desc references its package) or
310     // ii) It is dynamically loaded and nrefs == ndesc + 1
311     // (A dynamic library holds a static pointer on the package it contains
312     //  which is allocated when the PACKAGENAMEGetPackage() func is called,
313     //  and descallocated (reset) by PACKAGENAMEDeletePackage())
314     if (nrefs == ndesc + dyn) 
315       {
316 #if defined(MACOSX)
317                   //
318                   if (dyn==0)
319                           PackageReleaseBlackBoxDescriptorInternal(pack,dname);
320 #else
321                   PackageReleaseBlackBoxDescriptorInternal(pack,dname);
322 #endif  
323       }
324     
325     // If the package is released and dynamically loaded 
326     // then put it in the static list mReleasedDynamicallyLoadedPackages
327     UnLoadDynamicLibrary(pack,false);
328         
329     bbtkDebugMessage("package",4,"<== Package::ReleaseBlackBoxDescriptor('"<<
330                      packname<<"','"<<dname<<"'): refs="
331                      <<descr.use_count()<<bbtkendl);
332     /*
333     if (!pack.expired())
334       {
335         long dyn = pack.lock()->mDynamicLibraryHandler ? 1:0; 
336         long ndesc = pack.lock()->GetDescriptorMap().size();
337         long nrefs = pack.use_count();
338         
339         bbtkDebugMessage("package",3," ... Package still alive ("
340                          <<nrefs<<" refs / "
341                          <<ndesc<<" descr / dyn="
342                          <<dyn<<")"<<std::endl);
343       }
344     else 
345       {
346         bbtkDebugMessage("package",3,"   ... Package has been released"
347                          <<std::endl);
348       }  
349     */
350   }
351   //==========================================================================    
352
353   //==========================================================================
354   /// Opens a dynamic library which contains a bbtk package
355   /// Returns the handler 
356   /// Load the package management symbols from the lib
357   /// returns false if a problem occured hence can be used 
358   /// to test that a dyn lib is a valid bbtk package lib
359   /// NB : The BBTK version exported from the library 
360   ///      is tested against the current bbtk version
361   DynamicLibraryHandler Package::OpenDynamicLibrary( const std::string& libname,const std::string& package_name,
362                                                                                                          DLGetPackageFunction& getpack, DLDeletePackageFunction& delpack)
363   {
364     bbtkDebugMessage("package",3,"==> Package::OpenDynamicLibrary("
365                      <<libname<<")"<<std::endl);
366 #if defined(__GNUC__)
367
368     // Open shared lib
369     void *handler;
370     handler = dlopen(libname.c_str(), BBTK_RTLD_TIME | BBTK_RTLD_SCOPE );
371 //EED     handler = dlopen(libname.c_str(), RTLD_LAZY | RTLD_LOCAL );
372           
373     if (!handler)
374       {
375         bbtkMessage("package",0,
376                     "BBTK ..ERROR.. loading could not open shared library [" <<libname<<"] : "
377                     <<dlerror() << std::endl);
378         return 0;
379       }
380
381     bbtkDebugMessage("package",3,"* Shared lib ["<<libname<<"] open"<<std::endl);
382
383     // Loads the Package bbtk version function 
384     std::string getvername(package_name);
385     getvername += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
386     DLGetPackageBBTKVersionFunction getbbtkversion  = (DLGetPackageBBTKVersionFunction)(dlsym(handler,getvername.c_str()));
387     if (!getbbtkversion)
388       {
389         bbtkDebugMessage("package",3,"***"<<std::endl);
390         bbtkMessage("package",0,
391                     "BBTK ..ERROR.. loading shared library ["<<libname
392                     <<"] is not a valid bbtk package."
393                     <<" Symbol ["<<getvername<<"] :"<<dlerror()<< std::endl);
394         dlclose(handler);
395         return 0;
396       }
397
398     bbtkDebugMessage("package",3,"* Symbol ["<<getvername
399                      <<"] found"<<std::endl);
400     // version matches ?
401
402     if (getbbtkversion() != bbtk::GetVersion())
403       {
404         bbtkMessage("package",0,
405                     "BBTK ..ERROR.. loading: "<<package_name
406                         <<" - Shared library ["<<libname
407                     <<"] was build with bbtk version "
408                     <<getbbtkversion()
409                     <<" but the current program runs with version "
410                     <<bbtk::GetVersion()<<" : cannot load it. You have to recompile your BBTK-Package."<<std::endl);
411         dlclose(handler);
412         return 0;
413         
414       }
415
416     bbtkDebugMessage("package",3,"* Package bbtk version '"<<getbbtkversion()<<"' matches"<<std::endl);
417             // Loads the Package get function
418     std::string getpackname(package_name);
419     getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
420     getpack = (DLGetPackageFunction)(dlsym(handler, getpackname.c_str()));
421     if (!getpack)
422       {
423         bbtkMessage("package",0,
424                     "BBTK ..ERROR.. loading shared library ["<<libname
425                     <<"] is not a valid bbtk package."
426                     <<" Symbol ["<<getpackname<<"] :"<<dlerror()<< std::endl);
427         dlclose(handler);
428         return 0;
429       }
430   
431     bbtkDebugMessage("package",3,"* Symbol ["<<getpackname<<"] found"<<std::endl);
432     // Loads the Package delete function
433                                           
434     std::string delpackname(package_name);
435     delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
436     delpack = (DLDeletePackageFunction)(dlsym(handler, delpackname.c_str()));
437     if (!delpack)
438       {
439         bbtkMessage("package",0,
440                     "BBTK ..ERROR.. loading shared library ["<<libname
441                     <<"] is not a valid bbtk package."
442                     <<" Symbol ["<<delpackname<<"] :"<<dlerror()<< std::endl);
443         dlclose(handler);
444         return 0;
445       }
446     bbtkDebugMessage("package",3,"* Symbol ["<<delpackname<<"] found"<<std::endl);                
447 #elif defined(_WIN32)
448     
449     HINSTANCE handler;
450     
451     SetErrorMode(0);
452     // Open shared lib
453     handler = LoadLibrary(libname.c_str());
454     if (!handler)
455       {
456         bbtkMessage("package",0,
457                     "BBTK ..ERROR..  could not open shared library [" <<libname<<"]"
458                     << std::endl);
459         DWORD dwErrorCode = 0;
460         dwErrorCode = GetLastError();
461         bbtkMessage("package",2,
462                 "Windows Error: [" << dwErrorCode <<"]"
463                 << std::endl);
464          
465         return 0;
466       }
467     
468     // Loads the Package bbtk version function 
469     std::string getvername(package_name);
470     getvername += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_BBTK_VERSION_FUNCTION_NAME);
471     DLGetPackageBBTKVersionFunction getbbtkversion = (DLGetPackageBBTKVersionFunction)(GetProcAddress(handler, getvername.c_str()));
472           
473     if (!getbbtkversion)
474       {
475         FreeLibrary(handler);
476         bbtkMessage("package",0,
477                     "BBTK ..ERROR.. loading shared library ["<<libname
478                     <<"] is not a valid bbtk package."
479                     <<" Symbol ["<<getbbtkversion<<"] not found"<< std::endl);
480         return 0;
481       }
482     
483     // version matches ?
484     if (getbbtkversion() != bbtk::GetVersion())
485       {
486                   FreeLibrary(handler);
487                   bbtkMessage("package",0,
488                                 "BBTK ..ERROR.. loading: "<<package_name
489                                 <<" - Shared library ["<<libname
490                                 <<"] was build with bbtk version "
491                                 <<getbbtkversion()
492                                 <<" but the current program runs with version "
493                                 <<bbtk::GetVersion()<<" : cannot load it. You have to recompile your BBTK-Package."<<std::endl);
494                   return 0;
495       }
496
497      // Loads the Package get function
498     std::string getpackname(package_name);
499     getpackname += BBTK_STRINGIFY_SYMBOL(BBTK_GET_PACKAGE_FUNCTION_NAME);
500     getpack = (DLGetPackageFunction)(GetProcAddress(handler, getpackname.c_str()));
501     if (!getpack)
502       {
503         FreeLibrary(handler);
504         bbtkMessage("package",0,
505                     "BBTK ..ERROR.. loading shared library ["<<libname
506                     <<"] is not a valid bbtk package."
507                     <<" Symbol ["<<getpackname<<"] not found"<< std::endl);
508         return 0;
509       }
510     
511     // Loads the Package delete function
512     std::string delpackname(package_name);
513     delpackname += BBTK_STRINGIFY_SYMBOL(BBTK_DEL_PACKAGE_FUNCTION_NAME);
514     delpack = (DLDeletePackageFunction)(GetProcAddress(handler, delpackname.c_str()));
515     if (!delpack)
516       {
517         FreeLibrary(handler);
518         bbtkMessage("package",0,
519                     "BBTK ..ERROR.. loading shared library ["<<libname
520                     <<"] is not a valid bbtk package."
521                     <<" Symbol ["<<delpackname<<"] not found"<< std::endl);
522         return 0;
523       }
524                                          
525 #else
526     bbtkError("neither __GNUC__ nor _WIN32 ?!? How did you compile ?");
527 #endif
528     
529     return handler;
530   }
531   //==========================================================================
532   
533   //==========================================================================
534   /// Loads a package from a dynamic library
535   Package::Pointer Package::CreateFromDynamicLibrary(const std::string& libname,
536                                                      const std::string& pkgname,
537                                                      const std::string& path)
538   {
539     bbtkDebugMessage("package",1,"==> Package::CreateFromDynamicLibrary("
540                      <<libname<<")"<<std::endl);
541
542     DLGetPackageFunction gf;
543     DLDeletePackageFunction df;
544     DynamicLibraryHandler h = Package::OpenDynamicLibrary(libname,
545                                                           pkgname,
546                                                           gf,df);
547     if (h==0) return Package::Pointer(); 
548     Package::Pointer p = gf();
549     p->mDynamicLibraryHandler = h;
550     p->mDLDeletePackageFunction = df;
551     
552     std::string separator =
553       ConfigurationFile::GetInstance().Get_file_separator ();
554     //BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
555     std::string docreldoc = 
556       separator + "bbdoc" + separator + pkgname + separator + "index.html";
557     std::string reldoc = 
558       ".." + separator + ".." + docreldoc;
559     std::string doc = path + separator + ".." + separator
560       + BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH)
561       + docreldoc;
562     
563     p->SetDocURL(doc);
564     p->SetDocRelativeURL(reldoc);
565     
566     bbtkDebugMessage("package",2,"<== Package::CreateFromDynamicLibrary("
567                      <<libname<<") .. OK"<<std::endl);
568     return p;
569   }
570   //==========================================================================
571
572
573
574
575   //==========================================================================
576   /// UnLoads the package dynamic library (if any)
577   void Package::UnLoadDynamicLibrary(Package::WeakPointer pack, bool doit)
578   {
579     if (pack.expired() || (!pack.lock()->mDynamicLibraryHandler))
580       return;
581
582     std::string packname = pack.lock()->GetName();
583     bbtkDebugMessage("package",5,"==> Package::UnLoadDynamicLibrary('"
584                      <<packname<<"')"
585                      <<std::endl);
586
587     if (!pack.lock()->GetDescriptorMap().empty())
588       {
589
590         bbtkDebugMessage("package",5,"   Package not empty ... abort"
591                          <<std::endl);
592         return;
593         /*
594         bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
595                         <<"DescriptorMap not empty "
596                         <<BBTK_INTERNAL_ERROR_MESSAGE);
597         */
598       }
599
600     if (pack.use_count()!=1)
601       {
602         bbtkGlobalError("Package::UnLoadDynamicLibrary('"<<packname<<") : "
603                         <<"empty dl package with external refs"
604                         <<BBTK_INTERNAL_ERROR_MESSAGE);
605       } 
606
607     if (doit) 
608       {
609         UnLoad(pack);
610         bbtkDebugMessage("package",5,"==> dynamic library for package '"
611                          <<packname<<"' closed"
612                          <<std::endl);    
613       }
614     else 
615       {
616         mReleasedDynamicallyLoadedPackages.insert(pack);
617         bbtkDebugMessage("package",1,"==> package '"<<packname
618                          <<"' put in the 'to unload' list"
619                          <<std::endl);
620       }
621
622     bbtkDebugMessage("package",5,"<== Package::UnLoadDynamicLibrary('"
623                      <<packname<<"')"
624                      <<std::endl); 
625   }
626   //==========================================================================
627
628   //==========================================================================
629   /// UnLoads released packages that were loaded dynamically
630   /// see UnLoadDynamicLibrary and ReleaseBlackBoxDescriptor
631   void Package::UnLoadReleasedDynamicallyLoadedPackages()
632   {
633     bbtkDebugMessage("package",5,"==> Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
634
635     std::set<Package::WeakPointer>::iterator i;
636
637 //JCP- 21-04-09
638         if(mReleasedDynamicallyLoadedPackages.size()>0){
639                 for (i=mReleasedDynamicallyLoadedPackages.begin();
640                 i!=mReleasedDynamicallyLoadedPackages.end();
641                 ++i)
642                 {
643                 if (!i->expired()) UnLoad(*i);
644                 }
645         }
646 //JCP- 21-04-09
647     bbtkDebugMessage("package",5,"<== Package::UnLoadReleasedDynamicallyLoadedPackages()"<<std::endl);
648   }
649   //==========================================================================
650
651   //==========================================================================
652   void Package::UnLoad(Package::WeakPointer pack)
653   {
654     std::string packname = pack.lock()->GetName();
655     bbtkDebugMessage("package",6,"==> Package::UnLoad("<<packname<<")"<<std::endl);
656
657     Package* p = pack.lock().get();
658     
659     DynamicLibraryHandler h = p->mDynamicLibraryHandler;
660     
661     // deletes the package
662     p->mDLDeletePackageFunction();
663     
664     // closes the dl handler
665 #if defined(__GNUC__)  
666           if (dlclose(h)!=0)
667           {
668                   printf("EED Package::UnLoad ERROR %s\n", packname.c_str() );
669                   bbtkWarning("Failed to close dynamic library for package '"<<packname
670                                           <<"'"<<std::endl);
671           }  
672           
673 #elif defined(_WIN32)
674     FreeLibrary(h);
675 #endif
676
677     bbtkDebugMessage("package",1,"==> dynamic library for package '"
678                      <<packname<<"' closed"
679                      <<std::endl);    
680     bbtkDebugMessage("package",6,"   ... dynamic library unloaded"<<std::endl);
681   }
682   //==========================================================================
683
684         bool Package::ifBoxExist( std::string type)     
685     {
686                 bool ok=false;
687                 DescriptorMapType::const_iterator i = mDescriptorMap.find(type);
688                 if (i != mDescriptorMap.end())  
689                 {
690                         ok=true;
691                 }
692                 return ok;
693     }
694         
695   //==========================================================================
696   /// Creates an instance of a black box of type <type> with name <name>
697   BlackBox::Pointer Package::NewBlackBox(const std::string& type, 
698                                          const std::string& name) const
699   {
700     bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::NewBlackBox(\""<<type<<"\",\""<<name<<"\")"<<bbtkendl);
701     
702     DescriptorMapType::const_iterator i = mDescriptorMap.find(type);
703     if (i == mDescriptorMap.end())  
704     {
705            bbtkDebugDecTab("kernel",8);
706            return BlackBox::Pointer();
707     }
708     BlackBox::Pointer bb =i->second->NewBlackBox(name);
709     bbtkDebugDecTab("kernel",8);
710     return bb;   
711
712   }
713   //==========================================================================
714
715
716
717   //==========================================================================
718   /// Creates an instance of an adaptor of input type <typein> and 
719   /// output type <typeout>  with name <name>
720   BlackBox::Pointer Package::NewAdaptor(const DataInfo& typein,
721                                 const DataInfo& typeout,
722                                 const std::string& name) const
723   {
724     bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
725                         ">::NewAdaptor("
726                         <<typein<<","
727                         <<typeout<<",\""
728                         <<name<<"\")"<<bbtkendl);
729
730     AdaptorKey key(typein,typeout,
731                    BlackBoxDescriptor::DEFAULT_ADAPTOR);
732     AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
733     if (i == mAdaptorMap.end())  
734       {
735         bbtkDebugDecTab("kernel",8);
736         return BlackBox::Pointer();
737       }
738     BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
739     bbtkDebugDecTab("kernel",8);
740     return bb;   
741
742   }
743   //==========================================================================
744
745   //==========================================================================
746   /// Creates an instance of an adaptor of input type <typein> and 
747   /// output type <typeout>  with name <name>
748   BlackBox::Pointer Package::NewWidgetAdaptor(const DataInfo& typein,
749                                       const DataInfo& typeout,
750                                       const std::string& name) const
751   {
752     bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
753                         ">::NewWidgetAdaptor("
754                         <<typein<<","
755                         <<typeout<<",\""
756                         <<name<<"\")"<<bbtkendl);
757
758     AdaptorKey key(typein,typeout,
759                    BlackBoxDescriptor::DEFAULT_GUI);
760     AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
761     if (i == mAdaptorMap.end())  
762       {
763         bbtkDebugDecTab("kernel",8);
764         return BlackBox::Pointer();
765       }
766     BlackBox::Pointer bb =i->second.lock()->NewBlackBox(name);
767     bbtkDebugDecTab("kernel",8);
768     return bb;   
769
770   }
771   //==========================================================================
772
773
774
775   //==========================================================================
776   /// Returns true is the package contains 
777   /// an adaptor of input type <typein> and 
778   /// output type <typeout>
779   /// If successfull then adaptor contains the black box type name
780   bool Package::FindWidgetAdaptor(const DataInfo& typein,
781                                   const DataInfo& typeout,
782                                   std::string& adaptor) const
783   {
784     bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
785                         ">::FindWidgetAdaptor("
786                         <<typein<<","
787                         <<typeout<<")"<<bbtkendl);
788    
789     AdaptorKey key(/*typein*/
790                    DataInfo(typeid(void),""),
791                    typeout,
792                    BlackBoxDescriptor::DEFAULT_GUI);
793     // First try to find a single widget adaptor
794     AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
795     if (i == mAdaptorMap.end())  
796       {
797         bbtkDebugDecTab("kernel",8);
798         return false;
799       }
800     adaptor = i->second.lock()->GetTypeName();
801     bbtkDebugDecTab("kernel",8);
802     return true;   
803
804   }
805   //==========================================================================
806
807
808
809   //==========================================================================
810   /// Returns true is the package contains 
811   /// an adaptor of input type <typein> and 
812   /// output type <typeout>
813   /// If successfull then adaptor contains the black box type name
814   bool Package::FindAdaptor(const DataInfo& typein,
815                             const DataInfo& typeout,
816                             std::string& adaptor) const
817   {
818     bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<
819                         ">::FindAdaptor("
820                         <<typein<<","
821                         <<typeout<<")"<<bbtkendl);
822     
823     AdaptorKey key(typein,typeout,
824                    BlackBoxDescriptor::DEFAULT_ADAPTOR);
825     AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
826     if (i == mAdaptorMap.end())  
827       {
828         bbtkDebugDecTab("kernel",8);
829         return false;
830       }
831     adaptor = i->second.lock()->GetTypeName();
832     bbtkDebugDecTab("kernel",8);
833     return true;   
834
835   }
836   //==========================================================================
837
838
839   //==========================================================================
840   /// Registers a black box descriptor in the package
841   bool Package::Register(BlackBoxDescriptor::Pointer d) 
842   {
843     bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\")"<<std::endl);
844     
845     DescriptorMapType::iterator i = mDescriptorMap.find(d->GetTypeName());
846     if (i!=mDescriptorMap.end())
847       {
848         bbtkWarning("Package<"<<GetName()<<"> : Trying to register box type <"
849                     <<d->GetTypeName()<<"> which is already in the package");
850         return false;
851       }
852
853     mDescriptorMap[d->GetTypeName()] = d;
854     //    d->Reference();
855     d->SetPackage(GetThisPointer<Package>());
856     
857     // If it is a default adaptor, also register it in the adaptors map
858     if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR )
859       {
860         bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\") : The box is an adaptor, inserting it in adaptors map ..."<<std::endl);   
861         
862         TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
863         TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
864         DataInfo infoin(typein,d->GetInputDescriptor("In")->GetNature());
865         DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
866         AdaptorKey key(infoin,infoout,d->GetKind());
867         
868         AdaptorMapType::const_iterator i;
869         i = mAdaptorMap.find(key);        
870         if (i == mAdaptorMap.end())  
871           {
872             mAdaptorMap[key] = d;
873           }
874         // If already an adaptor registered : error
875         else 
876           {
877             if (i->second.lock()->GetTypeName() != d->GetTypeName()) 
878               {
879                 bbtkError("Package <"<<GetName()<<
880                           "> : trying to register black box <"
881                           <<d->GetTypeName()
882                           <<"> as default adaptor but there is already a default adaptor registered (<"
883                           <<i->second.lock()->GetTypeName()<<">)");
884               }
885           }
886       }
887     // If it is a default adaptor, also register it in the adaptors map
888     else if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_GUI)
889       {
890         bbtkDebugMessage("kernel",8,"Package<"<<GetName()<<">::Register(\""<<d->GetTypeName()<<"\") : The box is a widget adaptor, inserting it in adaptors map ..."<<std::endl);   
891         
892         TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
893         DataInfo infoin(typeid(void),"");
894         DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
895         AdaptorKey key(infoin,infoout,d->GetKind());
896
897         AdaptorMapType::const_iterator i;
898         i = mAdaptorMap.find(key);        
899         if (i == mAdaptorMap.end())  
900           {
901             mAdaptorMap[key] = d;
902           }
903         // If already an adaptor registered : error
904         else 
905           {
906             if (i->second.lock()->GetTypeName() != d->GetTypeName()) 
907               {
908                 bbtkError("Package <"<<GetName()<<
909                           "> : trying to register black box <"
910                           <<d->GetTypeName()
911                           <<"> as default widget adaptor but there is already a default adaptor registered (<"
912                           <<i->second.lock()->GetTypeName()<<">)");
913               }
914           }
915       }
916     
917
918     bbtkDebugDecTab("kernel",8);
919    
920     return true;
921   }
922   //==========================================================================
923   
924   //===================================================================
925   void Package::Check() const
926   {
927     bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
928                 <<" ["<<GetName()<<"]"<<std::endl);
929     DescriptorMapType::const_iterator i;
930     for (i=mDescriptorMap.begin();
931          i!=mDescriptorMap.end();
932          ++i) 
933       {
934         i->second->Check(true);
935       }
936     bbtkMessage("debug",1,"****** Checking Package "<<(void*)this
937                 <<" ["<<GetName()<<"] ... OK"<<std::endl);
938   }
939   //===================================================================
940
941
942   //==========================================================================
943   /// Changes the name of a black box type
944   void Package::ChangeDescriptorName( const std::string& oldname, const std::string& newname )
945   { 
946     bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
947                         <<">::ChangeDescriptorName(\""<<oldname
948                         <<"\",\""<<newname<<"\")"<<std::endl);
949     // Looking into the bb map
950     DescriptorMapType::iterator i = mDescriptorMap.find(oldname);
951     if (i == mDescriptorMap.end())  
952       {
953          bbtkDebugDecTab("kernel",8);
954          bbtkError("ChangeDescriptorName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
955       }
956
957     i->second->SetTypeName(newname);
958     mDescriptorMap[newname] = i->second;
959     mDescriptorMap.erase(i);
960
961     bbtkDebugDecTab("kernel",8);    
962   }
963   //==========================================================================
964
965
966
967   //==========================================================================
968   void Package::PrintHelpListDescriptors(bool description, bool adaptors) const
969   {
970     unsigned int lmax = 0;
971     std::vector<std::string> names;
972     std::vector<std::string> kinds;
973     std::vector<std::string> descrs;
974
975     DescriptorMapType::const_iterator i;
976     for (i=mDescriptorMap.begin();
977          i!=mDescriptorMap.end();
978          ++i) 
979       {
980         if ( adaptors || 
981              ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) ) 
982           {
983             std::string name("  ");
984             name += i->second->GetTypeName();
985             names.push_back(name);
986
987             std::string kind;
988             if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR )
989               {
990                 kind = std::string("[A]");
991               }
992             else if ( i->second->GetKind() == 
993                       BlackBoxDescriptor::DEFAULT_ADAPTOR )
994               {
995                 kind = std::string("[DA]");
996               }
997             kinds.push_back(kind);
998
999             unsigned int l = name.size()+kind.size();
1000             if (l>lmax) lmax = l;
1001
1002             std::string descr;
1003             if (description) 
1004               {
1005                 descr += " : ";
1006                 descr += i->second->GetDescription();
1007               } 
1008             descrs.push_back(descr);
1009           }
1010       } 
1011     
1012
1013     std::string offs;
1014     offs.append(lmax+3,' ');
1015     std::vector<std::string>::iterator ni,ci,di;
1016     for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin();
1017          ni != names.end(); ++ni, ++ci, ++di)
1018       {
1019         std::string space;
1020         space.append(lmax - ni->size() - ci->size(),' ');
1021         bbtkMessage("help",1,*ni << space << *ci );
1022         std::string d(*di);
1023         unsigned int dmax = 75 - lmax;
1024         //      while (d.size() > dmax ) 
1025         //  {
1026         if (d.size()>dmax) 
1027           bbtkMessage("help",1,d.substr(0,dmax) << "..." << std::endl);
1028         else 
1029           bbtkMessage("help",1,d << std::endl);
1030         //    d = d.substr(dmax,d.size());
1031         //  }
1032       }
1033
1034   }
1035   //==========================================================================
1036
1037   //==========================================================================
1038   /// Displays the list of adaptors of the package
1039   void Package::PrintHelpListAdaptors(bool description) const
1040   {
1041     DescriptorMapType::const_iterator i;
1042     for (i=mDescriptorMap.begin();
1043          i!=mDescriptorMap.end();
1044          ++i) 
1045       {
1046         if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD ) 
1047           {
1048             bbtkMessage("help",1,
1049                         "  "<<i->second->GetTypeName());
1050             if ( i->second->GetKind() == 
1051                  BlackBoxDescriptor::DEFAULT_ADAPTOR )
1052               {
1053                 bbtkMessage("help",1,
1054                             " [default]");
1055               }  
1056             if (description) 
1057               {
1058                 bbtkMessage("help",1,
1059                             " : "<<i->second->GetDescription());
1060
1061               } 
1062             bbtkMessage("help",1,std::endl);
1063           }
1064       } 
1065     /*
1066     AdaptorMapType::const_iterator i;
1067     for (i=mAdaptorMap.begin();
1068          i!=mAdaptorMap.end();
1069          ++i) 
1070       {
1071         bbtkMessage("help",1,
1072                     "  "<<i->second->GetTypeName());
1073         if (detail_level>0) 
1074           {
1075             bbtkMessage("help",1,
1076                         " : "<<i->second->GetDescription());
1077   
1078           } 
1079         bbtkMessage("help",1,std::endl);
1080       }
1081     */ 
1082   }
1083   //==========================================================================
1084
1085   //==========================================================================
1086   /// Prints help on a black box descriptor
1087   void Package::PrintHelpDescriptor(const std::string& name, bool full) const
1088   {
1089     bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
1090                         <<">::PrintHelpDescriptor(\""
1091                         <<name<<"\")"<<bbtkendl);
1092
1093     DescriptorMapType::const_iterator i = mDescriptorMap.find(name);
1094     if (i == mDescriptorMap.end())  
1095       {
1096         bbtkDebugDecTab("kernel",8);
1097         bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
1098       }
1099     //    bbtkMessage("help",1,"["<<GetName()<<"] ");
1100     i->second->GetHelp(full);
1101     bbtkDebugDecTab("kernel",8);
1102
1103   }
1104   //==========================================================================
1105
1106
1107   //==========================================================================
1108   /// Returns true iff the package contains the box of name boxname
1109   bool Package::ContainsDescriptor(const std::string& name) const 
1110   {
1111     bbtkDebugMessageInc("kernel",8,"Package<"<<GetName()
1112                         <<">::ContainsDescriptor(\""
1113                         <<name<<"\")"<<bbtkendl);
1114     
1115     DescriptorMapType::const_iterator i = mDescriptorMap.find(name);
1116     if (i == mDescriptorMap.end())  
1117     {
1118       bbtkDebugDecTab("kernel",8);
1119       return false;
1120     }
1121     bbtkDebugDecTab("kernel",8);
1122     return true;
1123   }
1124   //==========================================================================
1125
1126
1127  
1128   //==========================================================================
1129   void Package::CreateHtmlPage(const std::string& filename,
1130                                const std::string& caller,
1131                                const std::string& source,
1132                                const std::string& custom_header,
1133                                const std::string& custom_title,
1134                                int detail, 
1135                                int level,
1136                                bool relative_link ) const
1137   {
1138     bbtkDebugMessageInc("kernel",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
1139                         <<filename<<"\")"<<bbtkendl);
1140
1141 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1133"<<std::endl;
1142
1143     //---------------------
1144     // Open output file
1145     std::ofstream s;
1146     s.open(filename.c_str());
1147     if (!s.good()) 
1148     {
1149        bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
1150     }
1151     
1152     //----------------------
1153     // Html head
1154     std::string title = "BBTK Package "+GetName()+" "+GetVersion(); 
1155
1156     if (custom_title.length() != 0) title = custom_title;
1157
1158     s << "<html lang=\"en\">\n";
1159     s << "<head>\n";
1160     s << "<title>" << title << "</title>\n";
1161     s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
1162     s << "<meta name=\"description\" content=\""<<title<<"\">\n";
1163     s << "<meta name=\"generator\" content=\"\">\n";
1164     s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
1165     //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
1166     s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
1167     s << "pre.display { font-family:inherit }\n";
1168     s << "pre.format  { font-family:inherit }\n";
1169     s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
1170     s << "pre.smallformat  { font-family:inherit; font-size:smaller }\n";
1171     s << "pre.smallexample { font-size:smaller }\n";
1172     s << "pre.smalllisp    { font-size:smaller }\n";
1173     s << "span.sc    { font-variant:small-caps }\n";
1174     s << "span.roman { font-family:serif; font-weight:normal; } \n";
1175     s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n"; 
1176     s << "--></style>\n";
1177     s << "</head>\n";
1178     //----------------------
1179
1180     //----------------------
1181     // Html body
1182     s << "<body>\n";
1183     s << "<a name=\"Top\"></a>\n"; 
1184     
1185     //----------------------
1186     // Header
1187     if ( custom_header.length() != 0) 
1188       {
1189         if ( custom_header != "none" )
1190           { 
1191             std::ifstream in;
1192             in.open(custom_header.c_str());    
1193             if (!in.good()) 
1194               {
1195                 bbtkError("Could not open file \""<<custom_header<<"\"");
1196               }
1197             char buffer[512];
1198             while (!in.eof()) 
1199               {
1200                 in.getline(buffer,512);
1201                 std::string line(buffer);
1202                 s << line;
1203               }
1204             in.close();
1205             s << "<hr>\n";
1206            
1207             /*   
1208             s << "<object data=\"" << custom_header 
1209               << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
1210               << custom_header <<" could not be embedded.</object>\n";
1211             
1212             s << "<hr>\n";
1213             */
1214           }
1215       }
1216
1217     else 
1218       {
1219         s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
1220         s << "<p><TABLE cellspacing=0  cellpadding=3>\n";
1221         s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> " 
1222           << GetDescription() << "</TD></TR>\n";
1223         s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  " 
1224           << GetAuthor() << "</TD></TR>\n";
1225         s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  " 
1226           << GetCategory() << "</TD></TR>\n";
1227         s << "<TR><TD style='vertical-align: top;'><b> Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> " 
1228           << GetVersion() << "</TD></TR>\n";
1229         s << "<TR><TD style='vertical-align: top;'><b> bbtk Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> " 
1230           << bbtk::GetVersion() << "</TD></TR>\n";
1231         s << "</TABLE>\n";
1232       }
1233 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1225"<<std::endl;
1234     //-------------------
1235     // Table of contents
1236     // Black boxes list
1237     //  s << "<div class=\"contents\">\n";
1238     s << "<p><b> Black Boxes : </b>\n";
1239     s << "<ul>\n";
1240
1241     s << "<p><TABLE cellspacing=0  cellpadding=3>\n";
1242
1243     DescriptorMapType::const_iterator i;
1244 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1236"<<std::endl;
1245     for (i=mDescriptorMap.begin(); i!=mDescriptorMap.end(); ++i) {
1246                 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD) 
1247                         continue;
1248         
1249                 std::string name = i->second->GetTypeName();
1250                 Utilities::html_format(name);
1251                 std::string descr = i->second->GetDescription();
1252                 //Utilities::html_format(descr);
1253 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1246"<<std::endl;
1254                 s << "<TR>";
1255                 s << "<TD style='vertical-align: top;'>";
1256                 s << "&nbsp;&nbsp;&nbsp;<a name=\"toc_"<<name
1257                 <<"\" href=\"#"<<name<<"\">"
1258                 <<name<<"</a>";
1259                 s << "</TD> ";
1260                 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1261                 s << "</TR>\n";
1262                 }    
1263                 s << "</TABLE>\n";
1264
1265
1266                 s << "</ul>\n";
1267                 s << "</div>\n";
1268     
1269                 //-------------------
1270                 // Adaptors list
1271                 if (mAdaptorMap.size()>0) 
1272                 {
1273                         //  s << "<div class=\"contents\">\n";
1274                         s << "<p><b> Adaptors : </b>\n";
1275                         s << "<ul>\n";
1276 //std::cout<<"JCP bbtkPackage.cxx void Package::CreateHtmlPage() ln 1268"<<std::endl;
1277                         //    DescriptorMapType::const_iterator i;
1278                         s << "<p><TABLE cellspacing=0  cellpadding=3>\n";
1279                         for (i=mDescriptorMap.begin(); i!=mDescriptorMap.end();++i) 
1280                         {
1281                                 if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) 
1282                                         continue;
1283     
1284                                 std::string name = i->second->GetTypeName();
1285                                 Utilities::html_format(name);
1286                                 std::string descr = i->second->GetDescription();
1287                     
1288                                 s << "<TR>";
1289                                 s << "<TD style='vertical-align: top;'>";
1290                                 s << "&nbsp;&nbsp;&nbsp;<a name=\"toc_"<<name
1291                                   <<"\" href=\"#"<<name<<"\">"
1292                                   <<name<<"</a>";
1293                                 s << "</TD> ";
1294                                 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
1295                                 s << "</TR>\n";
1296                         }    
1297                         s << "</TABLE>\n";
1298
1299                         s << "</ul>\n";
1300                         s << "</div>\n";
1301                 }
1302     
1303     
1304     //  s << "<div class=\"node\">\n";
1305
1306     //    s << "<p><hr>\n";
1307     //    s << "<a name=\"Top\"></a>\n";
1308     //  s << "Top:&nbsp;<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
1309     // s << "Previous:&nbsp;<a rel="previous" accesskey="p" href="#dir">(dir)</a>,
1310     // s << "Up:&nbsp;<a rel="up" accesskey="u" href="#dir">(dir)</a>
1311     
1312     //    s << "</div>\n";
1313
1314     //----------------------
1315     // Boxes doc
1316
1317     //-------------------
1318     // Computes output directory from filename to pass it to 
1319     // BlackBoxDescriptor::InsertHtmlHelp
1320                 std::string dir;
1321
1322                 std::string::size_type slash_position = filename.find_last_of("/\\");
1323
1324
1325                 if (slash_position != std::string::npos) {
1326                         if (slash_position == 0)
1327                          slash_position = 1;  
1328                         dir = filename.substr(0,slash_position);
1329                 }
1330
1331                 for (i=mDescriptorMap.begin();
1332                  i!=mDescriptorMap.end();
1333                  ++i) 
1334                 {
1335                         i->second->InsertHtmlHelp(s,detail,level,dir,relative_link);
1336                 }    
1337
1338     //----------------------
1339     // Footer 
1340     time_t rawtime;
1341     tm * ptm;
1342     time ( &rawtime );
1343     ptm = gmtime ( &rawtime );
1344
1345     s << "<p><hr>\n";
1346     s << "Automatically generated by <b>"<<caller<<"</b> "//from <b>"
1347       //      <<source<<"</b>
1348       <<"on "
1349       << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900 
1350       << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
1351     s << "</body></html>\n"; 
1352     s.close();
1353     //----------------------
1354
1355     // End
1356     bbtkDebugDecTab("kernel",9);
1357   }
1358   //==========================================================================
1359   
1360   //==========================================================================
1361   std::string  Package::GetObjectName() const 
1362   { 
1363     return std::string("Package '")+mName+std::string("'"); 
1364   }
1365   //==========================================================================
1366
1367   //==========================================================================
1368   std::string Package::GetObjectInfo() const 
1369   {
1370     std::stringstream i;
1371     i << "  - "<<mDescriptorMap.size() << " boxes" << std::endl;
1372     if (mDynamicLibraryHandler) 
1373       {
1374         i<< "  - Loaded from dynamic library"<<std::endl;
1375       }
1376     return i.str();
1377   }
1378   //==========================================================================
1379
1380
1381   //==========================================================================
1382   size_t  Package::GetObjectSize() const 
1383   {
1384     size_t s = Superclass::GetObjectSize();
1385     s += Package::GetObjectInternalSize();
1386     return s;
1387   }
1388   //==========================================================================
1389   //==========================================================================
1390   size_t  Package::GetObjectInternalSize() const 
1391   {
1392     size_t s = sizeof(Package);
1393     return s;
1394   }
1395   //==========================================================================
1396   //==========================================================================
1397   size_t  Package::GetObjectRecursiveSize() const 
1398   {
1399     size_t s = Superclass::GetObjectRecursiveSize();
1400     s += Package::GetObjectInternalSize();
1401     
1402     DescriptorMapType::const_iterator i;
1403     for (i = mDescriptorMap.begin(); i!=mDescriptorMap.end(); ++i )
1404       {
1405         s += i->second->GetObjectRecursiveSize();
1406       }
1407     return s;
1408   }
1409   //==========================================================================
1410   void  Package::GetBoxesInside(NodeTreeC& tree, int cont) 
1411   {
1412                 DescriptorMapType::const_iterator i;
1413                 std::cout<<"*********a********"<<std::endl;
1414                 for (i=mDescriptorMap.begin(); i!=mDescriptorMap.end(); ++i) 
1415                 {
1416                         i->second->GetBoxesInside(tree, cont);
1417                         std::cout<<"*****************"<<std::endl;
1418                 }    
1419   }
1420         //==========================================================================
1421   //==========================================================================
1422   std::set<Package::WeakPointer> 
1423   Package::mReleasedDynamicallyLoadedPackages;
1424   //==========================================================================
1425
1426 }
1427