1 /*=========================================================================
4 Module: $RCSfile: bbtkBlackBoxDescriptor.cxx,v $
6 Date: $Date: 2008/04/18 12:59:15 $
7 Version: $Revision: 1.16 $
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()
58 bbtkDebugMessage("object",4,
59 "==> BlackBoxDescriptor::~BlackBoxDescriptor() ["
60 <<mTypeName<<"]"<<std::endl);
62 // deletes all I/O descriptors
63 InputDescriptorMapType::iterator i;
64 for (i=mInput.begin(); i!=mInput.end(); ++i) delete i->second;
65 OutputDescriptorMapType::iterator o;
66 for (o=mOutput.begin(); o!=mOutput.end(); ++o) delete o->second;
68 bbtkDebugMessage("object",4,
69 "<== BlackBoxDescriptor::~BlackBoxDescriptor() ["
70 <<mTypeName<<"]"<<std::endl);
72 //=========================================================================
75 //=======================================================================
77 void BlackBoxDescriptor::Release(BlackBoxDescriptor::WeakPointer desc)
79 bbtkMessage("object",2,"==> BlackBoxDescriptor::Release('"
80 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
81 long c = desc.use_count();
82 bbtkMessage("object",3," - ref count = "<<c<<std::endl);
84 if ((c == 1) && (desc.lock()->mPackage))
86 bbtkMessage("object",2," --> No more instance alive = releasing from package"<<std::endl);
88 Package::WeakPointer pack = desc.lock()->mPackage;
89 Package::ReleaseBlackBoxDescriptor(pack,desc);
93 bbtkMessage("object",2," --> Still some instances alive = Keeping it alive"<<std::endl);
95 bbtkMessage("object",2,"<== BlackBoxDescriptor::Release('"
96 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
99 //=========================================================================
103 //=========================================================================
105 void BlackBoxDescriptor::UnReference()
107 bbtkDebugMessageInc("Kernel",1,
108 "BlackBoxDescriptor::UnReference() ["
109 <<mTypeName<<"] #"<<mRefCount-1<<std::endl);
113 bbtkDebugMessage("Kernel",1,"--> Destructing BlackBoxDescriptor ["<<mTypeName<<"]"<<std::endl);
117 //=========================================================================
119 //=========================================================================
121 void BlackBoxDescriptor::Check(bool) const
125 //=========================================================================
127 //=========================================================================
128 /// Adds the string to the BlackBox description
129 void BlackBoxDescriptor::AddToDescription( const std::string& s, bool clear)
131 bbtkDebugMessage("Kernel",9,"BlackBoxDescriptor::AddToDescription(\""<<s<<
132 "\") ["<<GetFullTypeName()<<"]"<<std::endl);
133 if (clear) mDescription = s;
134 else mDescription += s;
136 //=========================================================================
138 //=========================================================================
139 /// Adds the string to the BlackBox author list
140 void BlackBoxDescriptor::AddToAuthor( const std::string& s, bool clear)
142 bbtkDebugMessage("Kernel",9,"BlackBoxDescriptor::AddToAuthor(\""<<s<<"\") ["
143 <<GetFullTypeName()<<"]"<<std::endl);
144 if (clear) mAuthor = s;
147 //=========================================================================
149 //=========================================================================
150 /// Adds the string to the BlackBox category list
151 void BlackBoxDescriptor::AddToCategory( const std::string& s, bool clear)
153 bbtkDebugMessage("Kernel",9,"BlackBoxDescriptor::AddToCategory(\""<<s<<"\") ["
154 <<GetFullTypeName()<<"]"<<std::endl);
155 if (clear) mCategory = s;
159 //=========================================================================
161 //=========================================================================
162 const BlackBoxDescriptor::InputDescriptor*
163 BlackBoxDescriptor::GetInputDescriptor(const std::string & name) const
165 bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::GetInputDescriptor('"
166 <<name<<"') ["<<GetFullTypeName()<<"]"<<std::endl);
168 InputDescriptorMapType::const_iterator i;
169 i = mInput.find(name);
170 if ( i == mInput.end() )
172 bbtkError("input '"<<name<<"' does not exist");
174 bbtkDebugDecTab("Kernel",9);
177 //=========================================================================
179 //=========================================================================
180 const BlackBoxDescriptor::OutputDescriptor*
181 BlackBoxDescriptor::GetOutputDescriptor(const std::string & name) const
183 bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::GetOutputDescriptor('"
184 <<name<<"') ["<<GetFullTypeName()<<"]"<<std::endl);
186 OutputDescriptorMapType::const_iterator i;
187 i = mOutput.find(name);
188 if ( i == mOutput.end() )
190 bbtkError("output '"<<name<<"' does not exist");
192 bbtkDebugDecTab("Kernel",9);
195 //=========================================================================
197 //=========================================================================
198 void BlackBoxDescriptor::GetHelp(bool full) const
200 bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::GetHelp() ["<<GetFullTypeName()<<"]"<<std::endl);
202 bbtkMessage("Help",1,"Black Box <"<<GetFullTypeName()<<">"<<std::endl);
203 bbtkMessage("Help",1," " <<GetDescription()<<std::endl);
204 bbtkMessage("Help",1," By : " <<GetAuthor() <<std::endl);
205 bbtkMessage("Help",1," Categories : " <<GetCategory() <<std::endl);
207 bbtkMessage("Help",1," * Inputs : "<<std::endl);
209 bbtkMessage("Help",1," * No inputs"<<std::endl);
210 InputDescriptorMapType::const_iterator i;
211 unsigned int namelmax = 0;
212 unsigned int typelmax = 0;
213 unsigned int natlmax = 0;
214 for ( i = mInput.begin(); i != mInput.end(); ++i )
216 if (i->second->GetName().size()>namelmax)
217 namelmax = i->second->GetName().size();
218 if (i->second->GetHumanTypeName().size()>typelmax)
219 typelmax = i->second->GetHumanTypeName().size();
220 if (i->second->GetNature().size()>natlmax)
221 natlmax = i->second->GetNature().size();
223 OutputDescriptorMapType::const_iterator o;
224 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
226 if (o->second->GetName().size()>namelmax)
227 namelmax = o->second->GetName().size();
228 if (o->second->GetHumanTypeName().size()>typelmax)
229 typelmax = o->second->GetHumanTypeName().size();
230 if (o->second->GetNature().size()>natlmax)
231 natlmax = o->second->GetNature().size();
234 for ( i = mInput.begin(); i != mInput.end(); ++i )
236 std::string name(i->second->GetName());
238 name.append(1+namelmax-name.size(),' ');
239 std::string type(i->second->GetHumanTypeName());
241 type.append(1+typelmax-type.size(),' ');
242 std::string nature(i->second->GetNature());
244 nature.append(1+natlmax-nature.size(),' ');
245 bbtkMessage("Help",1,
249 <<" : "<<i->second->GetDescription()<<std::endl);
252 bbtkMessage("Help",1," * Outputs : "<<std::endl);
254 bbtkMessage("Help",1," * No outputs"<<std::endl);
255 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
257 std::string name(o->second->GetName());
259 name.append(1+namelmax-name.size(),' ');
260 std::string type(o->second->GetHumanTypeName());
262 type.append(1+typelmax-type.size(),' ');
263 std::string nature(o->second->GetNature());
265 nature.append(1+natlmax-nature.size(),' ');
266 bbtkMessage("Help",1,
270 <<" : "<<o->second->GetDescription()<<std::endl);
273 bbtkDebugDecTab("Kernel",9);
277 //=========================================================================
279 //=========================================================================
280 /// Returns the full name of the **TYPE** of the black box (+package name)
281 std::string BlackBoxDescriptor::GetFullTypeName() const
283 if (GetPackage()!=0) return GetPackage()->GetName() + "::" + mTypeName;
284 return "::" + mTypeName;
286 //=========================================================================
288 //=========================================================================
289 void BlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s,
290 int detail, int level,
291 const std::string& output_dir,
294 bbtkDebugMessageInc("Kernel",9,"BlackBoxDescriptor::InsertHtmlHelp() ["<<GetFullTypeName()<<"]"<<std::endl);
298 std::string name = GetTypeName();
299 Utilities::html_format(name);
302 (s) << "<a name=\""<<name<<"\"></a>\n";
304 "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
305 // (s) << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
306 // (s) << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
307 (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
310 std::string descr = GetDescription();
311 Utilities::html_format(descr);
312 std::string author = GetAuthor();
313 Utilities::html_format(author);
315 std::vector<std::string> categories;
316 // Split the category string
317 std::string delimiters = ";,";
318 Utilities::SplitString(GetCategory(),
319 delimiters,categories);
322 (s) << "<p><TABLE cellspacing=0 cellpadding=3>\n";
323 (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
324 << descr << "</TD></TR>\n";
325 (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
326 << author << "</TD></TR>\n";
327 (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
328 std::vector<std::string>::iterator ci;
329 for (ci=categories.begin(); ci!=categories.end(); ++ci)
331 s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci
336 (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 "
337 << GetPackage()->GetName() << "</TD></TR>\n";
342 //i->second->InsertHTMLGraph( &s , detail,level,dir);
346 std::string titlecol("#BBBBFF");
347 std::string usercol("#FFFFFF");
348 std::string ubbcol("#DDFFFF");
349 std::string wxbbcol("#EEFFFF");
352 // (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
353 (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
354 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
355 <<"\">Inputs</TD></TR>\n";
357 std::vector<std::string> user_defined;
358 std::vector<std::string> ubb_defined;
359 std::vector<std::string> wxbb_defined;
361 const BlackBoxDescriptor::InputDescriptorMapType& imap =
362 GetInputDescriptorMap();
363 InputDescriptorMapType::const_iterator in;
364 for ( in = imap.begin(); in != imap.end(); ++in )
366 // Skips system-defined inputs
367 std::string col(usercol);
369 if (in->second->GetCreatorTypeInfo() ==
370 typeid(AtomicBlackBoxDescriptor))
375 else if (in->second->GetCreatorTypeInfo() ==
376 typeid(WxBlackBoxDescriptor))
382 std::string name(in->second->GetName());
383 Utilities::html_format(name);
385 std::string type("<");
386 type += in->second->GetTypeName();
388 Utilities::html_format(type);
390 std::string descr(in->second->GetDescription());
391 Utilities::html_format(descr);
394 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
395 +"\"><B><PRE> "+name+" </PRE></B></TD>"
396 + "<TD style='vertical-align: top;' bgcolor=\""+col
397 +"\"><I><PRE> "+type+" </PRE></I></TD>"
398 + "<TD style='vertical-align: top;' bgcolor=\""+col
399 +"\">"+descr+"</TD></TR>\n";
401 if (iotype==0) user_defined.push_back(out);
402 else if (iotype==1) ubb_defined.push_back(out);
403 else if (iotype==2) wxbb_defined.push_back(out);
407 std::vector<std::string>::iterator hi;
408 for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
409 for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
410 for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
412 user_defined.clear();
414 wxbb_defined.clear();
418 // (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
419 // (s) << "<TABLE border=1 cellspacing=0>\n";
420 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
421 <<"\">Outputs</TD></TR>\n";
423 const BlackBoxDescriptor::OutputDescriptorMapType& omap =
424 GetOutputDescriptorMap();
426 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
428 for ( o = omap.begin(); o != omap.end(); ++o )
430 std::string col(usercol);
432 if (o->second->GetCreatorTypeInfo() ==
433 typeid(AtomicBlackBoxDescriptor))
438 else if (o->second->GetCreatorTypeInfo() ==
439 typeid(WxBlackBoxDescriptor))
445 std::string name(o->second->GetName());
446 Utilities::html_format(name);
448 std::string type("<");
449 type += o->second->GetTypeName();
451 Utilities::html_format(type);
453 std::string descr(o->second->GetDescription());
454 Utilities::html_format(descr);
457 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
458 +"\"><B><PRE> "+name+" </PRE></B></TD>"
459 + "<TD style='vertical-align: top;' bgcolor=\""+col
460 +"\"><I><PRE> "+type+" </PRE></I></TD>"
461 + "<TD style='vertical-align: top;' bgcolor=\""+col
462 +"\">"+descr+"</TD></TR>\n";
464 if (iotype==0) user_defined.push_back(out);
465 else if (iotype==1) ubb_defined.push_back(out);
466 else if (iotype==2) wxbb_defined.push_back(out);
470 for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
471 for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
472 for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
479 bbtkDebugDecTab("Kernel",9);
481 //=========================================================================