1 /*=========================================================================
4 Module: $RCSfile: bbtkBlackBoxDescriptor.cxx,v $
6 Date: $Date: 2008/05/14 10:26:29 $
7 Version: $Revision: 1.18 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
21 * \brief Class bbtk::BlackBoxDescriptor : (abstract) describes a BlackBox (name, description, author) and is able to create an instance of it.
23 #include "bbtkBlackBoxDescriptor.h"
24 #include "bbtkMessageManager.h"
25 #include "bbtkPackage.h"
26 #include "bbtkUtilities.h"
27 #include "bbtkAtomicBlackBoxDescriptor.h"
28 #include "bbtkWxBlackBox.h"
33 typedef Package::Pointer PackagePointer;
37 //=========================================================================
39 BlackBoxDescriptor::BlackBoxDescriptor()
40 : mTypeName("Unknown"),
47 bbtkDebugMessage("object",4,
48 "==> BlackBoxDescriptor::BlackBoxDescriptor()"<<std::endl);
49 bbtkDebugMessage("object",4,
50 "<== BlackBoxDescriptor::BlackBoxDescriptor()"<<std::endl);
52 //=========================================================================
54 //=========================================================================
56 BlackBoxDescriptor::BlackBoxDescriptor(const BlackBoxDescriptor&)
59 //=========================================================================
61 BlackBoxDescriptor::~BlackBoxDescriptor()
63 bbtkDebugMessage("object",4,
64 "==> BlackBoxDescriptor::~BlackBoxDescriptor() ["
65 <<mTypeName<<"]"<<std::endl);
67 // deletes all I/O descriptors
68 InputDescriptorMapType::iterator i;
69 for (i=mInput.begin(); i!=mInput.end(); ++i) delete i->second;
70 OutputDescriptorMapType::iterator o;
71 for (o=mOutput.begin(); o!=mOutput.end(); ++o) delete o->second;
73 bbtkDebugMessage("object",4,
74 "<== BlackBoxDescriptor::~BlackBoxDescriptor() ["
75 <<mTypeName<<"]"<<std::endl);
77 //=========================================================================
80 //=======================================================================
82 void BlackBoxDescriptor::Release(BlackBoxDescriptor::WeakPointer desc)
84 bbtkMessage("object",2,"==> BlackBoxDescriptor::Release('"
85 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
86 long c = desc.use_count();
87 bbtkMessage("object",3," - ref count = "<<c<<std::endl);
89 if ((c == 1) && (desc.lock()->mPackage))
91 bbtkMessage("object",2," --> No more instance alive = releasing from package"<<std::endl);
93 Package::WeakPointer pack = desc.lock()->mPackage;
94 Package::ReleaseBlackBoxDescriptor(pack,desc);
98 bbtkMessage("object",2," --> Still some instances alive = Keeping it alive"<<std::endl);
100 bbtkMessage("object",2,"<== BlackBoxDescriptor::Release('"
101 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
104 //=========================================================================
108 //=========================================================================
110 void BlackBoxDescriptor::UnReference()
112 bbtkDebugMessageInc("Kernel",1,
113 "BlackBoxDescriptor::UnReference() ["
114 <<mTypeName<<"] #"<<mRefCount-1<<std::endl);
118 bbtkDebugMessage("Kernel",1,"--> Destructing BlackBoxDescriptor ["<<mTypeName<<"]"<<std::endl);
122 //=========================================================================
124 //=========================================================================
126 void BlackBoxDescriptor::Check(bool) const
130 //=========================================================================
132 //=========================================================================
133 /// Adds the string to the BlackBox description
134 void BlackBoxDescriptor::AddToDescription( const std::string& s, bool clear)
136 bbtkDebugMessage("Kernel",9,"BlackBoxDescriptor::AddToDescription(\""<<s<<
137 "\") ["<<GetFullTypeName()<<"]"<<std::endl);
138 if (clear) mDescription = s;
139 else mDescription += s;
141 //=========================================================================
143 //=========================================================================
144 /// Adds the string to the BlackBox author list
145 void BlackBoxDescriptor::AddToAuthor( const std::string& s, bool clear)
147 bbtkDebugMessage("Kernel",9,"BlackBoxDescriptor::AddToAuthor(\""<<s<<"\") ["
148 <<GetFullTypeName()<<"]"<<std::endl);
149 if (clear) mAuthor = s;
152 //=========================================================================
154 //=========================================================================
155 /// Adds the string to the BlackBox category list
156 void BlackBoxDescriptor::AddToCategory( const std::string& s, bool clear)
158 bbtkDebugMessage("Kernel",9,"BlackBoxDescriptor::AddToCategory(\""<<s<<"\") ["
159 <<GetFullTypeName()<<"]"<<std::endl);
160 if (clear) mCategory = s;
164 //=========================================================================
166 //=========================================================================
167 const BlackBoxDescriptor::InputDescriptor*
168 BlackBoxDescriptor::GetInputDescriptor(const std::string & name) const
170 bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::GetInputDescriptor('"
171 <<name<<"') ["<<GetFullTypeName()<<"]"<<std::endl);
173 InputDescriptorMapType::const_iterator i;
174 i = mInput.find(name);
175 if ( i == mInput.end() )
177 bbtkError("input '"<<name<<"' does not exist");
179 bbtkDebugDecTab("Kernel",9);
182 //=========================================================================
184 //=========================================================================
185 const BlackBoxDescriptor::OutputDescriptor*
186 BlackBoxDescriptor::GetOutputDescriptor(const std::string & name) const
188 bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::GetOutputDescriptor('"
189 <<name<<"') ["<<GetFullTypeName()<<"]"<<std::endl);
191 OutputDescriptorMapType::const_iterator i;
192 i = mOutput.find(name);
193 if ( i == mOutput.end() )
195 bbtkError("output '"<<name<<"' does not exist");
197 bbtkDebugDecTab("Kernel",9);
200 //=========================================================================
202 //=========================================================================
203 void BlackBoxDescriptor::GetHelp(bool full) const
205 bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::GetHelp() ["<<GetFullTypeName()<<"]"<<std::endl);
207 bbtkMessage("Help",1,"Black Box <"<<GetFullTypeName()<<">"<<std::endl);
208 bbtkMessage("Help",1," " <<GetDescription()<<std::endl);
209 bbtkMessage("Help",1," By : " <<GetAuthor() <<std::endl);
210 bbtkMessage("Help",1," Categories : " <<GetCategory() <<std::endl);
212 bbtkMessage("Help",1," * Inputs : "<<std::endl);
214 bbtkMessage("Help",1," * No inputs"<<std::endl);
215 InputDescriptorMapType::const_iterator i;
216 unsigned int namelmax = 0;
217 unsigned int typelmax = 0;
218 unsigned int natlmax = 0;
219 for ( i = mInput.begin(); i != mInput.end(); ++i )
221 if (i->second->GetName().size()>namelmax)
222 namelmax = i->second->GetName().size();
223 if (i->second->GetHumanTypeName().size()>typelmax)
224 typelmax = i->second->GetHumanTypeName().size();
225 if (i->second->GetNature().size()>natlmax)
226 natlmax = i->second->GetNature().size();
228 OutputDescriptorMapType::const_iterator o;
229 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
231 if (o->second->GetName().size()>namelmax)
232 namelmax = o->second->GetName().size();
233 if (o->second->GetHumanTypeName().size()>typelmax)
234 typelmax = o->second->GetHumanTypeName().size();
235 if (o->second->GetNature().size()>natlmax)
236 natlmax = o->second->GetNature().size();
239 for ( i = mInput.begin(); i != mInput.end(); ++i )
241 std::string name(i->second->GetName());
243 name.append(1+namelmax-name.size(),' ');
244 std::string type(i->second->GetHumanTypeName());
246 type.append(1+typelmax-type.size(),' ');
247 std::string nature(i->second->GetNature());
249 nature.append(1+natlmax-nature.size(),' ');
250 bbtkMessage("Help",1,
254 <<" : "<<i->second->GetDescription()<<std::endl);
257 bbtkMessage("Help",1," * Outputs : "<<std::endl);
259 bbtkMessage("Help",1," * No outputs"<<std::endl);
260 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
262 std::string name(o->second->GetName());
264 name.append(1+namelmax-name.size(),' ');
265 std::string type(o->second->GetHumanTypeName());
267 type.append(1+typelmax-type.size(),' ');
268 std::string nature(o->second->GetNature());
270 nature.append(1+natlmax-nature.size(),' ');
271 bbtkMessage("Help",1,
275 <<" : "<<o->second->GetDescription()<<std::endl);
278 bbtkDebugDecTab("Kernel",9);
282 //=========================================================================
284 //=========================================================================
285 /// Returns the full name of the **TYPE** of the black box (+package name)
286 std::string BlackBoxDescriptor::GetFullTypeName() const
288 if (GetPackage()!=0) return GetPackage()->GetName() + "::" + mTypeName;
289 return "::" + mTypeName;
291 //=========================================================================
293 //=========================================================================
294 void BlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s,
295 int detail, int level,
296 const std::string& output_dir,
299 bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::InsertHtmlHelp() ["<<GetFullTypeName()<<"]"<<std::endl);
303 std::string name = GetTypeName();
304 Utilities::html_format(name);
307 (s) << "<a name=\""<<name<<"\"></a>\n";
309 "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
310 // (s) << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
311 // (s) << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
312 (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
315 std::string descr = GetDescription();
316 //Utilities::html_format(descr);
317 std::string author = GetAuthor();
318 Utilities::html_format(author);
320 std::vector<std::string> categories;
321 // Split the category string
322 std::string delimiters = ";,";
323 Utilities::SplitString(GetCategory(),
324 delimiters,categories);
327 (s) << "<p><TABLE cellspacing=0 cellpadding=3>\n";
328 (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
329 << descr << "</TD></TR>\n";
330 (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
331 << author << "</TD></TR>\n";
332 (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
333 std::vector<std::string>::iterator ci;
334 for (ci=categories.begin(); ci!=categories.end(); ++ci)
336 s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci
341 (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 "
342 << GetPackage()->GetName() << "</TD></TR>\n";
347 //i->second->InsertHTMLGraph( &s , detail,level,dir);
351 std::string titlecol("#BBBBFF");
352 std::string usercol("#FFFFFF");
353 std::string ubbcol("#DDFFFF");
354 std::string wxbbcol("#EEFFFF");
357 // (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
358 (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
359 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
360 <<"\">Inputs</TD></TR>\n";
362 std::vector<std::string> user_defined;
363 std::vector<std::string> ubb_defined;
364 std::vector<std::string> wxbb_defined;
366 const BlackBoxDescriptor::InputDescriptorMapType& imap =
367 GetInputDescriptorMap();
368 InputDescriptorMapType::const_iterator in;
369 for ( in = imap.begin(); in != imap.end(); ++in )
371 // Skips system-defined inputs
372 std::string col(usercol);
374 if (in->second->GetCreatorTypeInfo() ==
375 typeid(AtomicBlackBoxDescriptor))
380 else if (in->second->GetCreatorTypeInfo() ==
381 typeid(WxBlackBoxDescriptor))
387 std::string name(in->second->GetName());
388 Utilities::html_format(name);
390 std::string type("<");
391 type += in->second->GetTypeName();
393 Utilities::html_format(type);
395 std::string descr(in->second->GetDescription());
396 //Utilities::html_format(descr);
399 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
400 +"\"><B><PRE> "+name+" </PRE></B></TD>"
401 + "<TD style='vertical-align: top;' bgcolor=\""+col
402 +"\"><I><PRE> "+type+" </PRE></I></TD>"
403 + "<TD style='vertical-align: top;' bgcolor=\""+col
404 +"\">"+descr+"</TD></TR>\n";
406 if (iotype==0) user_defined.push_back(out);
407 else if (iotype==1) ubb_defined.push_back(out);
408 else if (iotype==2) wxbb_defined.push_back(out);
412 std::vector<std::string>::iterator hi;
413 for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
414 for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
415 for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
417 user_defined.clear();
419 wxbb_defined.clear();
423 // (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
424 // (s) << "<TABLE border=1 cellspacing=0>\n";
425 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
426 <<"\">Outputs</TD></TR>\n";
428 const BlackBoxDescriptor::OutputDescriptorMapType& omap =
429 GetOutputDescriptorMap();
431 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
433 for ( o = omap.begin(); o != omap.end(); ++o )
435 std::string col(usercol);
437 if (o->second->GetCreatorTypeInfo() ==
438 typeid(AtomicBlackBoxDescriptor))
443 else if (o->second->GetCreatorTypeInfo() ==
444 typeid(WxBlackBoxDescriptor))
450 std::string name(o->second->GetName());
451 Utilities::html_format(name);
453 std::string type("<");
454 type += o->second->GetTypeName();
456 Utilities::html_format(type);
458 std::string descr(o->second->GetDescription());
459 //Utilities::html_format(descr);
462 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
463 +"\"><B><PRE> "+name+" </PRE></B></TD>"
464 + "<TD style='vertical-align: top;' bgcolor=\""+col
465 +"\"><I><PRE> "+type+" </PRE></I></TD>"
466 + "<TD style='vertical-align: top;' bgcolor=\""+col
467 +"\">"+descr+"</TD></TR>\n";
469 if (iotype==0) user_defined.push_back(out);
470 else if (iotype==1) ubb_defined.push_back(out);
471 else if (iotype==2) wxbb_defined.push_back(out);
475 for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
476 for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
477 for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
484 bbtkDebugDecTab("Kernel",9);
486 //=========================================================================