]> Creatis software - bbtk.git/blob - kernel/src/bbtkBlackBoxDescriptor.cxx
Fixed Window deletion mechanism
[bbtk.git] / kernel / src / bbtkBlackBoxDescriptor.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkBlackBoxDescriptor.cxx,v $
4   Language:  C++
5   Date:      $Date: 2009/05/28 08:12:05 $
6   Version:   $Revision: 1.21 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31
32 /**
33  *  \file 
34  *  \brief Class bbtk::BlackBoxDescriptor : (abstract) describes a BlackBox (name, description, author) and is able to create an instance of it.
35  */
36 #include "bbtkBlackBoxDescriptor.h"
37 #include "bbtkMessageManager.h"
38 #include "bbtkPackage.h"
39 #include "bbtkUtilities.h"
40 #include "bbtkAtomicBlackBoxDescriptor.h"
41 #include "bbtkWxBlackBox.h"
42
43 namespace bbtk
44 {
45
46   typedef Package::Pointer PackagePointer;
47
48 #define bbtkDMessage(key,level,mess) \
49   bbtkMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
50 #define bbtkDDebugMessage(key,level,mess)       \
51   bbtkDebugMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
52
53
54   //=========================================================================
55   /// Default ctor
56   BlackBoxDescriptor::BlackBoxDescriptor()  
57     : mTypeName("Unknown"), 
58       mDescription(""), 
59       mAuthor(""),
60       mCategory(""),
61       mKind(STANDARD),
62       mPackage()
63   {
64     bbtkDDebugMessage("object",4,
65                      "==> BlackBoxDescriptor()"<<std::endl);
66     bbtkDDebugMessage("object",4,
67                      "<== BlackBoxDescriptor()"<<std::endl);
68   }
69   //=========================================================================
70
71   //=========================================================================
72   /// Default ctor
73   BlackBoxDescriptor::BlackBoxDescriptor(const BlackBoxDescriptor&)  
74   {
75   }
76   //=========================================================================
77   /// Dtor
78   BlackBoxDescriptor::~BlackBoxDescriptor()
79   {
80     bbtkDDebugMessage("object",4,
81                      "==> ~BlackBoxDescriptor()"
82                      <<std::endl);
83
84     // deletes all I/O descriptors
85     InputDescriptorMapType::iterator i;
86     for (i=mInput.begin(); i!=mInput.end(); ++i) delete i->second;
87     OutputDescriptorMapType::iterator o;
88     for (o=mOutput.begin(); o!=mOutput.end(); ++o) delete o->second;
89
90     bbtkDDebugMessage("object",4,
91                       "<== ~BlackBoxDescriptor()"
92                       <<std::endl);
93   }
94   //=========================================================================
95
96   /*
97   //=======================================================================
98   /// Release
99   void BlackBoxDescriptor::Release(BlackBoxDescriptor::WeakPointer desc)
100   {
101     bbtkMessage("object",2,"==> BlackBoxDescriptor::Release('"
102                 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
103     long c = desc.use_count();
104     bbtkMessage("object",3," - ref count = "<<c<<std::endl);
105     // If only one ref 
106     if ((c == 1) && (desc.lock()->mPackage))
107       {
108         bbtkMessage("object",2," --> No more instance alive = releasing from package"<<std::endl);
109         
110         Package::WeakPointer pack = desc.lock()->mPackage;
111         Package::ReleaseBlackBoxDescriptor(pack,desc);
112       }
113     else 
114       {
115         bbtkMessage("object",2," --> Still some instances alive = Keeping it alive"<<std::endl);
116       }    
117     bbtkMessage("object",2,"<== BlackBoxDescriptor::Release('"
118                 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
119  
120   }
121   //=========================================================================
122   */
123
124   /*
125   //=========================================================================
126   /// Dtor
127   void BlackBoxDescriptor::UnReference()
128   {
129     bbtkDDebugMessageInc("kernel",1,
130                         "BlackBoxDescriptor::UnReference() ["
131                         <<mTypeName<<"] #"<<mRefCount-1<<std::endl);
132     mRefCount--;
133     if (mRefCount<=0) 
134       {
135         bbtkDDebugMessage("kernel",1,"--> Destructing BlackBoxDescriptor ["<<mTypeName<<"]"<<std::endl);
136         delete this;
137       }
138   }
139   //=========================================================================
140   */
141   //=========================================================================
142   /// Check
143   void BlackBoxDescriptor::Check(bool) const
144   {
145     
146   }
147   //=========================================================================
148
149   //=========================================================================
150   /// Adds the string to the BlackBox description
151   void BlackBoxDescriptor::AddToDescription( const std::string& s, bool clear)
152   {
153     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToDescription(\""
154                       <<s<<"\")"<<std::endl);
155     if (clear) mDescription = s; 
156     else mDescription += s;
157    }
158   //=========================================================================
159
160   //=========================================================================   
161   /// Adds the string to the BlackBox author list
162   void BlackBoxDescriptor::AddToAuthor( const std::string& s, bool clear)
163   {
164     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToAuthor(\""
165                       <<s<<"\")"
166                       <<std::endl);
167     if (clear) mAuthor = s;
168     else mAuthor += s;
169   }
170   //=========================================================================
171
172   //=========================================================================  
173   /// Adds the string to the BlackBox category list
174   void BlackBoxDescriptor::AddToCategory( const std::string& s, bool clear)
175   {
176     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToCategory(\""
177                       <<s<<"\")"
178                       <<std::endl);  
179     if (clear) mCategory = s;
180     else mCategory += s;
181     mCategory += ";";
182   }  
183   //=========================================================================
184   
185   //=========================================================================
186   const BlackBoxDescriptor::InputDescriptor* 
187   BlackBoxDescriptor::GetInputDescriptor(const std::string & name) const
188   {
189     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetInputDescriptor('"
190                       <<name<<"')"<<std::endl);
191
192     InputDescriptorMapType::const_iterator i;
193     i = mInput.find(name);
194     if ( i == mInput.end() ) 
195     {
196             bbtkError("input '"<<name<<"' does not exist");
197     }
198
199     return i->second;
200   }
201   //=========================================================================
202
203   //=========================================================================
204   const BlackBoxDescriptor::OutputDescriptor* 
205   BlackBoxDescriptor::GetOutputDescriptor(const std::string & name) const
206   {
207     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetOutputDescriptor('"
208                       <<name<<"')"<<std::endl);
209
210     OutputDescriptorMapType::const_iterator i;
211     i = mOutput.find(name);
212     if ( i == mOutput.end() ) 
213     {
214       bbtkError("output '"<<name<<"' does not exist");
215     }
216
217     return i->second;
218   }
219   //=========================================================================
220
221   //=========================================================================
222   void BlackBoxDescriptor::GetHelp(bool full) const
223   {
224     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetHelp()"
225                       <<std::endl);
226     
227     bbtkMessage("help",1,"Black Box <"<<GetFullTypeName()<<">"<<std::endl);
228     bbtkMessage("help",1," "              <<GetDescription()<<std::endl);
229     bbtkMessage("help",1," By : "         <<GetAuthor()     <<std::endl);
230     bbtkMessage("help",1," Categories : " <<GetCategory()    <<std::endl);
231     if (mInput.size()) 
232       bbtkMessage("help",1," * Inputs : "<<std::endl);
233     else 
234       bbtkMessage("help",1," * No inputs"<<std::endl);
235     InputDescriptorMapType::const_iterator i;
236     unsigned int namelmax = 0;
237     unsigned int typelmax = 0;
238     unsigned int natlmax = 0;
239     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
240     {
241            if (i->second->GetName().size()>namelmax) 
242              namelmax = i->second->GetName().size();
243            if (i->second->GetHumanTypeName().size()>typelmax) 
244              typelmax = i->second->GetHumanTypeName().size();
245            if (i->second->GetNature().size()>natlmax) 
246              natlmax = i->second->GetNature().size();
247     }
248     OutputDescriptorMapType::const_iterator o;
249     for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
250     {
251            if (o->second->GetName().size()>namelmax) 
252              namelmax = o->second->GetName().size();
253            if (o->second->GetHumanTypeName().size()>typelmax) 
254              typelmax = o->second->GetHumanTypeName().size();
255            if (o->second->GetNature().size()>natlmax) 
256              natlmax = o->second->GetNature().size();
257     }
258     //
259     for ( i = mInput.begin();  i != mInput.end(); ++i ) 
260     {
261            std::string name(i->second->GetName());
262            name += "'";
263            name.append(1+namelmax-name.size(),' ');
264            std::string type(i->second->GetHumanTypeName());
265            type += ">";
266            type.append(1+typelmax-type.size(),' ');
267            std::string nature(i->second->GetNature());
268            nature += "]";
269            nature.append(1+natlmax-nature.size(),' ');
270            bbtkMessage("help",1,
271                        "    '"<<name
272                        <<" <"<<type
273                        <<" ["<<nature
274                        <<" : "<<i->second->GetDescription()<<std::endl);
275     }
276     if (mOutput.size()) 
277       bbtkMessage("help",1," * Outputs : "<<std::endl);
278     else 
279       bbtkMessage("help",1," * No outputs"<<std::endl);
280     for ( o = mOutput.begin();  o != mOutput.end(); ++o ) 
281     {
282            std::string name(o->second->GetName());
283            name += "'";
284            name.append(1+namelmax-name.size(),' ');
285            std::string type(o->second->GetHumanTypeName());
286            type += ">";
287            type.append(1+typelmax-type.size(),' ');
288            std::string nature(o->second->GetNature());
289            nature += "]";
290            nature.append(1+natlmax-nature.size(),' ');
291            bbtkMessage("help",1,
292                     "    '"<<name
293                        <<" <"<<type
294                        <<" ["<<nature
295                        <<" : "<<o->second->GetDescription()<<std::endl);
296       }
297    
298   
299   }
300   //=========================================================================
301    
302   //=========================================================================
303   /// Returns the full name of the **TYPE** of the black box (+package name)
304   std::string BlackBoxDescriptor::GetFullTypeName() const
305   {
306     if (GetPackage()!=0) return GetPackage()->GetName() + "::" + mTypeName;
307     return "::" + mTypeName;
308   }
309   //=========================================================================
310  
311   //=========================================================================
312   void BlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s, 
313                                             int detail, int level,
314                                             const std::string& output_dir,
315                                             bool relative_link )
316   {
317     bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::InsertHtmlHelp()"
318                       <<std::endl);
319     
320     //-------------
321     // General info 
322     std::string name = GetTypeName();
323     Utilities::html_format(name);
324
325     (s) << "<p><hr>\n";
326     (s) << "<a name=\""<<name<<"\"></a>\n";
327     (s) << //"Top:&nbsp;
328       "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
329     // (s) << "Previous:&nbsp;<a rel="previous" accesskey="p" href="#dir">(dir)</a>,
330     // (s) << "Up:&nbsp;<a rel="up" accesskey="u" href="#dir">(dir)</a>
331     (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
332
333
334     std::string descr = GetDescription();
335     //Utilities::html_format(descr);
336     std::string author = GetAuthor();
337     Utilities::html_format(author);
338
339     std::vector<std::string> categories;
340     // Split the category string 
341     std::string delimiters = ";,";
342     Utilities::SplitString(GetCategory(),
343                            delimiters,categories);
344
345     
346     (s) << "<p><TABLE cellspacing=0  cellpadding=3>\n";
347     (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> " 
348       << descr << "</TD></TR>\n";
349     (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  " 
350       << author << "</TD></TR>\n";
351     (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  ";
352     std::vector<std::string>::iterator ci;
353     for (ci=categories.begin(); ci!=categories.end(); ++ci)
354       {
355         s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci 
356           << "</a>&nbsp;\n";
357       }
358     s << "</TD></TR>\n";      
359
360     (s) << "<TR><TD style='vertical-align: top;'><b> To use it </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'>  include " 
361         << GetPackage()->GetName() << "</TD></TR>\n";
362     (s) << "</TABLE>\n";
363
364     //-------------
365     // Graph
366     //i->second->InsertHTMLGraph( &s , detail,level,dir);
367     
368     //-------------
369     // Inputs
370     std::string titlecol("#BBBBFF");
371     std::string usercol("#FFFFFF");
372     std::string ubbcol("#DDFFFF");
373     std::string wxbbcol("#EEFFFF");
374
375
376     //  (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
377     (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
378     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
379       <<"\">Inputs</TD></TR>\n";
380
381     std::vector<std::string> user_defined;
382     std::vector<std::string> ubb_defined;
383     std::vector<std::string> wxbb_defined;
384
385     const BlackBoxDescriptor::InputDescriptorMapType& imap = 
386       GetInputDescriptorMap();
387     InputDescriptorMapType::const_iterator in;
388     for ( in = imap.begin();  in != imap.end(); ++in ) 
389     {
390       // Skips system-defined inputs
391       std::string col(usercol);
392       int iotype = 0;
393       if (in->second->GetCreatorTypeInfo() == 
394           typeid(AtomicBlackBoxDescriptor))
395         {
396           col = ubbcol; 
397           iotype = 1;
398         }
399 #ifdef USE_WXWIDGETS
400       else if (in->second->GetCreatorTypeInfo() == 
401                typeid(WxBlackBoxDescriptor))
402         {
403           col = wxbbcol; 
404           iotype = 2;
405         }
406 #endif
407
408       std::string name(in->second->GetName());
409       Utilities::html_format(name);
410       
411       std::string type("<");
412       type += in->second->GetTypeName();    
413       type += ">";
414       Utilities::html_format(type);
415       
416       std::string descr(in->second->GetDescription());
417       //Utilities::html_format(descr);
418       
419       std::string out = 
420         "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
421         +"\"><B><PRE> "+name+" </PRE></B></TD>"
422         + "<TD style='vertical-align: top;' bgcolor=\""+col
423         +"\"><I><PRE> "+type+" </PRE></I></TD>"
424         + "<TD style='vertical-align: top;' bgcolor=\""+col
425         +"\">"+descr+"</TD></TR>\n";
426       
427       if (iotype==0) user_defined.push_back(out);
428       else if (iotype==1) ubb_defined.push_back(out);
429       else if (iotype==2) wxbb_defined.push_back(out);
430       
431     }
432
433     std::vector<std::string>::iterator hi;
434     for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
435     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
436     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
437
438     user_defined.clear();
439     ubb_defined.clear();
440     wxbb_defined.clear();
441
442     //-------------
443     // Outputs
444     //  (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
445     //  (s) << "<TABLE border=1 cellspacing=0>\n";
446     (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
447       <<"\">Outputs</TD></TR>\n";
448     
449     const BlackBoxDescriptor::OutputDescriptorMapType& omap = 
450       GetOutputDescriptorMap();
451     
452     BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
453     
454     for ( o = omap.begin();  o != omap.end(); ++o ) 
455       {
456         std::string col(usercol);
457         int iotype = 0;
458         if (o->second->GetCreatorTypeInfo() == 
459             typeid(AtomicBlackBoxDescriptor))
460           {
461             col = ubbcol; 
462             iotype = 1;
463           }
464 #ifdef USE_WXWIDGETS
465         else if (o->second->GetCreatorTypeInfo() == 
466                  typeid(WxBlackBoxDescriptor))
467           {
468             col = wxbbcol; 
469             iotype = 2;
470           }
471 #endif
472         
473         std::string name(o->second->GetName());
474         Utilities::html_format(name);
475         
476         std::string type("<");
477         type += o->second->GetTypeName();    
478         type += ">";
479         Utilities::html_format(type);
480         
481         std::string descr(o->second->GetDescription());
482         //Utilities::html_format(descr);
483         
484         std::string out = 
485           "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
486           +"\"><B><PRE> "+name+" </PRE></B></TD>"
487           + "<TD style='vertical-align: top;' bgcolor=\""+col
488           +"\"><I><PRE> "+type+" </PRE></I></TD>"
489           + "<TD style='vertical-align: top;' bgcolor=\""+col
490           +"\">"+descr+"</TD></TR>\n";
491         
492         if (iotype==0) user_defined.push_back(out);
493         else if (iotype==1) ubb_defined.push_back(out);
494         else if (iotype==2) wxbb_defined.push_back(out);
495         
496       }
497     
498     for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
499     for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
500     for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
501
502     (s) << "</TABLE>\n";
503     
504     //------------
505     // End
506
507
508    }
509   //=========================================================================
510  
511 }