1 /*=========================================================================
3 Module: $RCSfile: bbtkBlackBoxDescriptor.cxx,v $
5 Date: $Date: 2009/12/08 14:10:36 $
6 Version: $Revision: 1.22 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
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.
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
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 * ------------------------------------------------------------------------ */
34 * \brief Class bbtk::BlackBoxDescriptor : (abstract) describes a BlackBox (name, description, author) and is able to create an instance of it.
36 #include "bbtkBlackBoxDescriptor.h"
37 #include "bbtkMessageManager.h"
38 #include "bbtkPackage.h"
39 #include "bbtkUtilities.h"
40 #include "bbtkAtomicBlackBoxDescriptor.h"
41 #include "bbtkWxBlackBox.h"
46 typedef Package::Pointer PackagePointer;
48 #define bbtkDMessage(key,level,mess) \
49 bbtkMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
50 #define bbtkDDebugMessage(key,level,mess) \
51 bbtkDebugMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
54 //=========================================================================
56 BlackBoxDescriptor::BlackBoxDescriptor()
57 : mTypeName("Unknown"),
64 bbtkDDebugMessage("object",4,
65 "==> BlackBoxDescriptor()"<<std::endl);
66 bbtkDDebugMessage("object",4,
67 "<== BlackBoxDescriptor()"<<std::endl);
69 //=========================================================================
71 //=========================================================================
73 BlackBoxDescriptor::BlackBoxDescriptor(const BlackBoxDescriptor&)
76 //=========================================================================
78 BlackBoxDescriptor::~BlackBoxDescriptor()
80 bbtkDDebugMessage("object",4,
81 "==> ~BlackBoxDescriptor()"
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;
90 bbtkDDebugMessage("object",4,
91 "<== ~BlackBoxDescriptor()"
94 //=========================================================================
97 //=======================================================================
99 void BlackBoxDescriptor::Release(BlackBoxDescriptor::WeakPointer desc)
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);
106 if ((c == 1) && (desc.lock()->mPackage))
108 bbtkMessage("object",2," --> No more instance alive = releasing from package"<<std::endl);
110 Package::WeakPointer pack = desc.lock()->mPackage;
111 Package::ReleaseBlackBoxDescriptor(pack,desc);
115 bbtkMessage("object",2," --> Still some instances alive = Keeping it alive"<<std::endl);
117 bbtkMessage("object",2,"<== BlackBoxDescriptor::Release('"
118 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
121 //=========================================================================
125 //=========================================================================
127 void BlackBoxDescriptor::UnReference()
129 bbtkDDebugMessageInc("kernel",1,
130 "BlackBoxDescriptor::UnReference() ["
131 <<mTypeName<<"] #"<<mRefCount-1<<std::endl);
135 bbtkDDebugMessage("kernel",1,"--> Destructing BlackBoxDescriptor ["<<mTypeName<<"]"<<std::endl);
139 //=========================================================================
141 //=========================================================================
143 void BlackBoxDescriptor::Check(bool) const
147 //=========================================================================
149 //=========================================================================
150 /// Adds the string to the BlackBox description
151 void BlackBoxDescriptor::AddToDescription( const std::string& s, bool clear)
153 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToDescription(\""
154 <<s<<"\")"<<std::endl);
155 if (clear) mDescription = s;
156 else mDescription += s;
158 //=========================================================================
160 //=========================================================================
161 /// Adds the string to the BlackBox author list
162 void BlackBoxDescriptor::AddToAuthor( const std::string& s, bool clear)
164 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToAuthor(\""
167 if (clear) mAuthor = s;
170 //=========================================================================
172 //=========================================================================
173 /// Adds the string to the BlackBox category list
174 void BlackBoxDescriptor::AddToCategory( const std::string& s, bool clear)
176 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToCategory(\""
179 if (clear) mCategory = s;
183 //=========================================================================
185 //=========================================================================
186 const BlackBoxDescriptor::InputDescriptor*
187 BlackBoxDescriptor::GetInputDescriptor(const std::string & name) const
189 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetInputDescriptor('"
190 <<name<<"')"<<std::endl);
192 InputDescriptorMapType::const_iterator i;
193 i = mInput.find(name);
194 if ( i == mInput.end() )
196 bbtkError("input '"<<name<<"' does not exist");
201 //=========================================================================
203 //=========================================================================
204 const BlackBoxDescriptor::OutputDescriptor*
205 BlackBoxDescriptor::GetOutputDescriptor(const std::string & name) const
207 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetOutputDescriptor('"
208 <<name<<"')"<<std::endl);
210 OutputDescriptorMapType::const_iterator i;
211 i = mOutput.find(name);
212 if ( i == mOutput.end() )
214 bbtkError("output '"<<name<<"' does not exist");
219 //=========================================================================
221 //=========================================================================
222 void BlackBoxDescriptor::GetHelp(bool full) const
224 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetHelp()"
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);
232 bbtkMessage("help",1," * Inputs : "<<std::endl);
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 )
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();
248 OutputDescriptorMapType::const_iterator o;
249 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
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();
259 for ( i = mInput.begin(); i != mInput.end(); ++i )
261 std::string name(i->second->GetName());
263 name.append(1+namelmax-name.size(),' ');
264 std::string type(i->second->GetHumanTypeName());
266 type.append(1+typelmax-type.size(),' ');
267 std::string nature(i->second->GetNature());
269 nature.append(1+natlmax-nature.size(),' ');
270 bbtkMessage("help",1,
274 <<" : "<<i->second->GetDescription()<<std::endl);
277 bbtkMessage("help",1," * Outputs : "<<std::endl);
279 bbtkMessage("help",1," * No outputs"<<std::endl);
280 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
282 std::string name(o->second->GetName());
284 name.append(1+namelmax-name.size(),' ');
285 std::string type(o->second->GetHumanTypeName());
287 type.append(1+typelmax-type.size(),' ');
288 std::string nature(o->second->GetNature());
290 nature.append(1+natlmax-nature.size(),' ');
291 bbtkMessage("help",1,
295 <<" : "<<o->second->GetDescription()<<std::endl);
300 //=========================================================================
302 //=========================================================================
303 /// Returns the full name of the **TYPE** of the black box (+package name)
304 std::string BlackBoxDescriptor::GetFullTypeName() const
306 if (GetPackage()!=0) return GetPackage()->GetName() + "::" + mTypeName;
307 return "::" + mTypeName;
309 //=========================================================================
311 //=========================================================================
312 void BlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s,
313 int detail, int level,
314 const std::string& output_dir,
317 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::InsertHtmlHelp()"
322 std::string name = GetTypeName();
323 Utilities::html_format(name);
326 (s) << "<a name=\""<<name<<"\"></a>\n";
328 "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
329 // (s) << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
330 // (s) << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
331 (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
334 std::string descr = GetDescription();
335 //Utilities::html_format(descr);
336 std::string author = GetAuthor();
337 Utilities::html_format(author);
339 std::vector<std::string> categories;
340 // Split the category string
341 std::string delimiters = ";,";
342 Utilities::SplitString(GetCategory(),
343 delimiters,categories);
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)
355 s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci
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";
366 //i->second->InsertHTMLGraph( &s , detail,level,dir);
370 std::string titlecol("#BBBBFF");
371 std::string usercol("#FFFFFF");
372 std::string ubbcol("#DDFFFF");
373 std::string wxbbcol("#EEFFFF");
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";
381 std::vector<std::string> user_defined;
382 std::vector<std::string> ubb_defined;
383 std::vector<std::string> wxbb_defined;
385 const BlackBoxDescriptor::InputDescriptorMapType& imap =
386 GetInputDescriptorMap();
387 InputDescriptorMapType::const_iterator in;
388 for ( in = imap.begin(); in != imap.end(); ++in )
390 // Skips system-defined inputs
391 std::string col(usercol);
393 if (in->second->GetCreatorTypeInfo() ==
394 typeid(AtomicBlackBoxDescriptor))
400 else if (in->second->GetCreatorTypeInfo() ==
401 typeid(WxBlackBoxDescriptor))
408 std::string name(in->second->GetName());
409 Utilities::html_format(name);
411 std::string type("<");
412 type += in->second->GetTypeName();
414 Utilities::html_format(type);
416 std::string descr(in->second->GetDescription());
417 //Utilities::html_format(descr);
421 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
422 +"\"><B><PRE> "+name+" </PRE></B></TD>"
423 + "<TD style='vertical-align: top;' bgcolor=\""+col
424 +"\"><I><PRE> "+type+" </PRE></I></TD>"
425 + "<TD style='vertical-align: top;' bgcolor=\""+col
426 +"\">"+descr+"</TD></TR>\n";
430 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
431 +"\"><B><PRE> "+name+" </PRE></B></TD>"
432 + "<TD style='vertical-align: top;' bgcolor=\""+col
433 +"\"><I><PRE> "+descr+" </PRE></I></TD>"
434 + "<TD style='vertical-align: top;' bgcolor=\""+col
435 +"\">"+type+"</TD></TR>\n";
437 if (iotype==0) user_defined.push_back(out);
438 else if (iotype==1) ubb_defined.push_back(out);
439 else if (iotype==2) wxbb_defined.push_back(out);
443 std::vector<std::string>::iterator hi;
444 for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
445 for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
446 for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
448 user_defined.clear();
450 wxbb_defined.clear();
454 // (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
455 // (s) << "<TABLE border=1 cellspacing=0>\n";
456 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
457 <<"\">Outputs</TD></TR>\n";
459 const BlackBoxDescriptor::OutputDescriptorMapType& omap =
460 GetOutputDescriptorMap();
462 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
464 for ( o = omap.begin(); o != omap.end(); ++o )
466 std::string col(usercol);
468 if (o->second->GetCreatorTypeInfo() ==
469 typeid(AtomicBlackBoxDescriptor))
475 else if (o->second->GetCreatorTypeInfo() ==
476 typeid(WxBlackBoxDescriptor))
483 std::string name(o->second->GetName());
484 Utilities::html_format(name);
486 std::string type("<");
487 type += o->second->GetTypeName();
489 Utilities::html_format(type);
491 std::string descr(o->second->GetDescription());
492 //Utilities::html_format(descr);
496 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
497 +"\"><B><PRE> "+name+" </PRE></B></TD>"
498 + "<TD style='vertical-align: top;' bgcolor=\""+col
499 +"\"><I><PRE> "+type+" </PRE></I></TD>"
500 + "<TD style='vertical-align: top;' bgcolor=\""+col
501 +"\">"+descr+"</TD></TR>\n";
504 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
505 +"\"><B><PRE> "+name+" </PRE></B></TD>"
506 + "<TD style='vertical-align: top;' bgcolor=\""+col
507 +"\"><I><PRE> "+descr+" </PRE></I></TD>"
508 + "<TD style='vertical-align: top;' bgcolor=\""+col
509 +"\">"+type+"</TD></TR>\n";
511 if (iotype==0) user_defined.push_back(out);
512 else if (iotype==1) ubb_defined.push_back(out);
513 else if (iotype==2) wxbb_defined.push_back(out);
517 for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
518 for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
519 for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
528 //=========================================================================