1 /*=========================================================================
3 Module: $RCSfile: bbtkBlackBoxDescriptor.cxx,v $
5 Date: $Date: 2008/10/17 08:18:12 $
6 Version: $Revision: 1.19 $
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;
50 //=========================================================================
52 BlackBoxDescriptor::BlackBoxDescriptor()
53 : mTypeName("Unknown"),
60 bbtkDebugMessage("object",4,
61 "==> BlackBoxDescriptor::BlackBoxDescriptor()"<<std::endl);
62 bbtkDebugMessage("object",4,
63 "<== BlackBoxDescriptor::BlackBoxDescriptor()"<<std::endl);
65 //=========================================================================
67 //=========================================================================
69 BlackBoxDescriptor::BlackBoxDescriptor(const BlackBoxDescriptor&)
72 //=========================================================================
74 BlackBoxDescriptor::~BlackBoxDescriptor()
76 bbtkDebugMessage("object",4,
77 "==> BlackBoxDescriptor::~BlackBoxDescriptor() ["
78 <<mTypeName<<"]"<<std::endl);
80 // deletes all I/O descriptors
81 InputDescriptorMapType::iterator i;
82 for (i=mInput.begin(); i!=mInput.end(); ++i) delete i->second;
83 OutputDescriptorMapType::iterator o;
84 for (o=mOutput.begin(); o!=mOutput.end(); ++o) delete o->second;
86 bbtkDebugMessage("object",4,
87 "<== BlackBoxDescriptor::~BlackBoxDescriptor() ["
88 <<mTypeName<<"]"<<std::endl);
90 //=========================================================================
93 //=======================================================================
95 void BlackBoxDescriptor::Release(BlackBoxDescriptor::WeakPointer desc)
97 bbtkMessage("object",2,"==> BlackBoxDescriptor::Release('"
98 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
99 long c = desc.use_count();
100 bbtkMessage("object",3," - ref count = "<<c<<std::endl);
102 if ((c == 1) && (desc.lock()->mPackage))
104 bbtkMessage("object",2," --> No more instance alive = releasing from package"<<std::endl);
106 Package::WeakPointer pack = desc.lock()->mPackage;
107 Package::ReleaseBlackBoxDescriptor(pack,desc);
111 bbtkMessage("object",2," --> Still some instances alive = Keeping it alive"<<std::endl);
113 bbtkMessage("object",2,"<== BlackBoxDescriptor::Release('"
114 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
117 //=========================================================================
121 //=========================================================================
123 void BlackBoxDescriptor::UnReference()
125 bbtkDebugMessageInc("Kernel",1,
126 "BlackBoxDescriptor::UnReference() ["
127 <<mTypeName<<"] #"<<mRefCount-1<<std::endl);
131 bbtkDebugMessage("Kernel",1,"--> Destructing BlackBoxDescriptor ["<<mTypeName<<"]"<<std::endl);
135 //=========================================================================
137 //=========================================================================
139 void BlackBoxDescriptor::Check(bool) const
143 //=========================================================================
145 //=========================================================================
146 /// Adds the string to the BlackBox description
147 void BlackBoxDescriptor::AddToDescription( const std::string& s, bool clear)
149 bbtkDebugMessage("Kernel",9,"BlackBoxDescriptor::AddToDescription(\""<<s<<
150 "\") ["<<GetFullTypeName()<<"]"<<std::endl);
151 if (clear) mDescription = s;
152 else mDescription += s;
154 //=========================================================================
156 //=========================================================================
157 /// Adds the string to the BlackBox author list
158 void BlackBoxDescriptor::AddToAuthor( const std::string& s, bool clear)
160 bbtkDebugMessage("Kernel",9,"BlackBoxDescriptor::AddToAuthor(\""<<s<<"\") ["
161 <<GetFullTypeName()<<"]"<<std::endl);
162 if (clear) mAuthor = s;
165 //=========================================================================
167 //=========================================================================
168 /// Adds the string to the BlackBox category list
169 void BlackBoxDescriptor::AddToCategory( const std::string& s, bool clear)
171 bbtkDebugMessage("Kernel",9,"BlackBoxDescriptor::AddToCategory(\""<<s<<"\") ["
172 <<GetFullTypeName()<<"]"<<std::endl);
173 if (clear) mCategory = s;
177 //=========================================================================
179 //=========================================================================
180 const BlackBoxDescriptor::InputDescriptor*
181 BlackBoxDescriptor::GetInputDescriptor(const std::string & name) const
183 bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::GetInputDescriptor('"
184 <<name<<"') ["<<GetFullTypeName()<<"]"<<std::endl);
186 InputDescriptorMapType::const_iterator i;
187 i = mInput.find(name);
188 if ( i == mInput.end() )
190 bbtkError("input '"<<name<<"' does not exist");
192 bbtkDebugDecTab("Kernel",9);
195 //=========================================================================
197 //=========================================================================
198 const BlackBoxDescriptor::OutputDescriptor*
199 BlackBoxDescriptor::GetOutputDescriptor(const std::string & name) const
201 bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::GetOutputDescriptor('"
202 <<name<<"') ["<<GetFullTypeName()<<"]"<<std::endl);
204 OutputDescriptorMapType::const_iterator i;
205 i = mOutput.find(name);
206 if ( i == mOutput.end() )
208 bbtkError("output '"<<name<<"' does not exist");
210 bbtkDebugDecTab("Kernel",9);
213 //=========================================================================
215 //=========================================================================
216 void BlackBoxDescriptor::GetHelp(bool full) const
218 bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::GetHelp() ["<<GetFullTypeName()<<"]"<<std::endl);
220 bbtkMessage("Help",1,"Black Box <"<<GetFullTypeName()<<">"<<std::endl);
221 bbtkMessage("Help",1," " <<GetDescription()<<std::endl);
222 bbtkMessage("Help",1," By : " <<GetAuthor() <<std::endl);
223 bbtkMessage("Help",1," Categories : " <<GetCategory() <<std::endl);
225 bbtkMessage("Help",1," * Inputs : "<<std::endl);
227 bbtkMessage("Help",1," * No inputs"<<std::endl);
228 InputDescriptorMapType::const_iterator i;
229 unsigned int namelmax = 0;
230 unsigned int typelmax = 0;
231 unsigned int natlmax = 0;
232 for ( i = mInput.begin(); i != mInput.end(); ++i )
234 if (i->second->GetName().size()>namelmax)
235 namelmax = i->second->GetName().size();
236 if (i->second->GetHumanTypeName().size()>typelmax)
237 typelmax = i->second->GetHumanTypeName().size();
238 if (i->second->GetNature().size()>natlmax)
239 natlmax = i->second->GetNature().size();
241 OutputDescriptorMapType::const_iterator o;
242 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
244 if (o->second->GetName().size()>namelmax)
245 namelmax = o->second->GetName().size();
246 if (o->second->GetHumanTypeName().size()>typelmax)
247 typelmax = o->second->GetHumanTypeName().size();
248 if (o->second->GetNature().size()>natlmax)
249 natlmax = o->second->GetNature().size();
252 for ( i = mInput.begin(); i != mInput.end(); ++i )
254 std::string name(i->second->GetName());
256 name.append(1+namelmax-name.size(),' ');
257 std::string type(i->second->GetHumanTypeName());
259 type.append(1+typelmax-type.size(),' ');
260 std::string nature(i->second->GetNature());
262 nature.append(1+natlmax-nature.size(),' ');
263 bbtkMessage("Help",1,
267 <<" : "<<i->second->GetDescription()<<std::endl);
270 bbtkMessage("Help",1," * Outputs : "<<std::endl);
272 bbtkMessage("Help",1," * No outputs"<<std::endl);
273 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
275 std::string name(o->second->GetName());
277 name.append(1+namelmax-name.size(),' ');
278 std::string type(o->second->GetHumanTypeName());
280 type.append(1+typelmax-type.size(),' ');
281 std::string nature(o->second->GetNature());
283 nature.append(1+natlmax-nature.size(),' ');
284 bbtkMessage("Help",1,
288 <<" : "<<o->second->GetDescription()<<std::endl);
291 bbtkDebugDecTab("Kernel",9);
295 //=========================================================================
297 //=========================================================================
298 /// Returns the full name of the **TYPE** of the black box (+package name)
299 std::string BlackBoxDescriptor::GetFullTypeName() const
301 if (GetPackage()!=0) return GetPackage()->GetName() + "::" + mTypeName;
302 return "::" + mTypeName;
304 //=========================================================================
306 //=========================================================================
307 void BlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s,
308 int detail, int level,
309 const std::string& output_dir,
312 bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::InsertHtmlHelp() ["<<GetFullTypeName()<<"]"<<std::endl);
316 std::string name = GetTypeName();
317 Utilities::html_format(name);
320 (s) << "<a name=\""<<name<<"\"></a>\n";
322 "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
323 // (s) << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
324 // (s) << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
325 (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
328 std::string descr = GetDescription();
329 //Utilities::html_format(descr);
330 std::string author = GetAuthor();
331 Utilities::html_format(author);
333 std::vector<std::string> categories;
334 // Split the category string
335 std::string delimiters = ";,";
336 Utilities::SplitString(GetCategory(),
337 delimiters,categories);
340 (s) << "<p><TABLE cellspacing=0 cellpadding=3>\n";
341 (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
342 << descr << "</TD></TR>\n";
343 (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
344 << author << "</TD></TR>\n";
345 (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
346 std::vector<std::string>::iterator ci;
347 for (ci=categories.begin(); ci!=categories.end(); ++ci)
349 s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci
354 (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 "
355 << GetPackage()->GetName() << "</TD></TR>\n";
360 //i->second->InsertHTMLGraph( &s , detail,level,dir);
364 std::string titlecol("#BBBBFF");
365 std::string usercol("#FFFFFF");
366 std::string ubbcol("#DDFFFF");
367 std::string wxbbcol("#EEFFFF");
370 // (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
371 (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
372 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
373 <<"\">Inputs</TD></TR>\n";
375 std::vector<std::string> user_defined;
376 std::vector<std::string> ubb_defined;
377 std::vector<std::string> wxbb_defined;
379 const BlackBoxDescriptor::InputDescriptorMapType& imap =
380 GetInputDescriptorMap();
381 InputDescriptorMapType::const_iterator in;
382 for ( in = imap.begin(); in != imap.end(); ++in )
384 // Skips system-defined inputs
385 std::string col(usercol);
387 if (in->second->GetCreatorTypeInfo() ==
388 typeid(AtomicBlackBoxDescriptor))
393 else if (in->second->GetCreatorTypeInfo() ==
394 typeid(WxBlackBoxDescriptor))
400 std::string name(in->second->GetName());
401 Utilities::html_format(name);
403 std::string type("<");
404 type += in->second->GetTypeName();
406 Utilities::html_format(type);
408 std::string descr(in->second->GetDescription());
409 //Utilities::html_format(descr);
412 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
413 +"\"><B><PRE> "+name+" </PRE></B></TD>"
414 + "<TD style='vertical-align: top;' bgcolor=\""+col
415 +"\"><I><PRE> "+type+" </PRE></I></TD>"
416 + "<TD style='vertical-align: top;' bgcolor=\""+col
417 +"\">"+descr+"</TD></TR>\n";
419 if (iotype==0) user_defined.push_back(out);
420 else if (iotype==1) ubb_defined.push_back(out);
421 else if (iotype==2) wxbb_defined.push_back(out);
425 std::vector<std::string>::iterator hi;
426 for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
427 for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
428 for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
430 user_defined.clear();
432 wxbb_defined.clear();
436 // (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
437 // (s) << "<TABLE border=1 cellspacing=0>\n";
438 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
439 <<"\">Outputs</TD></TR>\n";
441 const BlackBoxDescriptor::OutputDescriptorMapType& omap =
442 GetOutputDescriptorMap();
444 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
446 for ( o = omap.begin(); o != omap.end(); ++o )
448 std::string col(usercol);
450 if (o->second->GetCreatorTypeInfo() ==
451 typeid(AtomicBlackBoxDescriptor))
456 else if (o->second->GetCreatorTypeInfo() ==
457 typeid(WxBlackBoxDescriptor))
463 std::string name(o->second->GetName());
464 Utilities::html_format(name);
466 std::string type("<");
467 type += o->second->GetTypeName();
469 Utilities::html_format(type);
471 std::string descr(o->second->GetDescription());
472 //Utilities::html_format(descr);
475 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
476 +"\"><B><PRE> "+name+" </PRE></B></TD>"
477 + "<TD style='vertical-align: top;' bgcolor=\""+col
478 +"\"><I><PRE> "+type+" </PRE></I></TD>"
479 + "<TD style='vertical-align: top;' bgcolor=\""+col
480 +"\">"+descr+"</TD></TR>\n";
482 if (iotype==0) user_defined.push_back(out);
483 else if (iotype==1) ubb_defined.push_back(out);
484 else if (iotype==2) wxbb_defined.push_back(out);
488 for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
489 for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
490 for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
497 bbtkDebugDecTab("Kernel",9);
499 //=========================================================================