2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
28 /*=========================================================================
30 Module: $RCSfile: bbtkBlackBoxDescriptor.cxx,v $
32 Date: $Date: 2012/11/16 08:49:01 $
33 Version: $Revision: 1.24 $
34 =========================================================================*/
40 * \brief Class bbtk::BlackBoxDescriptor : (abstract) describes a BlackBox (name, description, author) and is able to create an instance of it.
42 #include "bbtkBlackBoxDescriptor.h"
43 #include "bbtkMessageManager.h"
44 #include "bbtkPackage.h"
45 #include "bbtkUtilities.h"
46 #include "bbtkAtomicBlackBoxDescriptor.h"
47 #include "bbtkWxBlackBox.h"
52 typedef Package::Pointer PackagePointer;
54 #define bbtkDMessage(key,level,mess) \
55 bbtkMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
56 #define bbtkDDebugMessage(key,level,mess) \
57 bbtkDebugMessage(key,level,"["<<GetFullTypeName()<<"] "<<mess)
60 //=========================================================================
62 BlackBoxDescriptor::BlackBoxDescriptor()
63 : mTypeName("Unknown"),
68 mTypeOfScript(TS_BINARY),
71 bbtkDDebugMessage("object",4,
72 "==> BlackBoxDescriptor()"<<std::endl);
73 bbtkDDebugMessage("object",4,
74 "<== BlackBoxDescriptor()"<<std::endl);
76 //=========================================================================
78 //=========================================================================
80 BlackBoxDescriptor::BlackBoxDescriptor(const BlackBoxDescriptor&)
84 //=========================================================================
86 BlackBoxDescriptor::~BlackBoxDescriptor()
88 bbtkDDebugMessage("object",4,
89 "==> ~BlackBoxDescriptor()"
92 // deletes all I/O descriptors
93 InputDescriptorMapType::iterator i;
94 for (i=mInput.begin(); i!=mInput.end(); ++i) delete i->second;
95 OutputDescriptorMapType::iterator o;
96 for (o=mOutput.begin(); o!=mOutput.end(); ++o) delete o->second;
98 bbtkDDebugMessage("object",4,
99 "<== ~BlackBoxDescriptor()"
102 //=========================================================================
105 //=======================================================================
107 void BlackBoxDescriptor::Release(BlackBoxDescriptor::WeakPointer desc)
109 bbtkMessage("object",2,"==> BlackBoxDescriptor::Release('"
110 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
111 long c = desc.use_count();
112 bbtkMessage("object",3," - ref count = "<<c<<std::endl);
114 if ((c == 1) && (desc.lock()->mPackage))
116 bbtkMessage("object",2," --> No more instance alive = releasing from package"<<std::endl);
118 Package::WeakPointer pack = desc.lock()->mPackage;
119 Package::ReleaseBlackBoxDescriptor(pack,desc);
123 bbtkMessage("object",2," --> Still some instances alive = Keeping it alive"<<std::endl);
125 bbtkMessage("object",2,"<== BlackBoxDescriptor::Release('"
126 <<desc.lock()->GetTypeName()<<"')"<<std::endl);
129 //=========================================================================
133 //=========================================================================
135 void BlackBoxDescriptor::UnReference()
137 bbtkDDebugMessageInc("kernel",1,
138 "BlackBoxDescriptor::UnReference() ["
139 <<mTypeName<<"] #"<<mRefCount-1<<std::endl);
143 bbtkDDebugMessage("kernel",1,"--> Destructing BlackBoxDescriptor ["<<mTypeName<<"]"<<std::endl);
147 //=========================================================================
149 //=========================================================================
151 void BlackBoxDescriptor::Check(bool) const
155 //=========================================================================
157 //=========================================================================
158 /// Adds the string to the BlackBox description
159 void BlackBoxDescriptor::AddToDescription( const std::string& s, bool clear)
161 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToDescription(\""
162 <<s<<"\")"<<std::endl);
163 if (clear) mDescription = s;
164 else mDescription += s;
166 //=========================================================================
168 //=========================================================================
169 /// Adds the string to the BlackBox author list
170 void BlackBoxDescriptor::AddToAuthor( const std::string& s, bool clear)
172 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToAuthor(\""
175 if (clear) mAuthor = s;
178 //=========================================================================
180 //=========================================================================
181 /// Adds the string to the BlackBox category list
182 void BlackBoxDescriptor::AddToCategory( const std::string& s, bool clear)
184 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::AddToCategory(\""
187 if (clear) mCategory = s;
191 //=========================================================================
193 //=========================================================================
194 const BlackBoxDescriptor::InputDescriptor*
195 BlackBoxDescriptor::GetInputDescriptor(const std::string & name) const
197 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetInputDescriptor('"
198 <<name<<"')"<<std::endl);
200 InputDescriptorMapType::const_iterator i;
201 i = mInput.find(name);
202 if ( i == mInput.end() )
204 bbtkError("input '"<<name<<"' does not exist");
209 //=========================================================================
211 //=========================================================================
212 const BlackBoxDescriptor::OutputDescriptor*
213 BlackBoxDescriptor::GetOutputDescriptor(const std::string & name) const
215 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetOutputDescriptor('"
216 <<name<<"')"<<std::endl);
218 OutputDescriptorMapType::const_iterator i;
219 i = mOutput.find(name);
220 if ( i == mOutput.end() )
222 bbtkError("output '"<<name<<"' does not exist");
227 //=========================================================================
229 //=========================================================================
230 void BlackBoxDescriptor::GetHelp(bool full) const
232 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::GetHelp()"
235 bbtkMessage("help",1,"Black Box <"<<GetFullTypeName()<<">"<<std::endl);
236 bbtkMessage("help",1," " <<GetDescription()<<std::endl);
237 bbtkMessage("help",1," By : " <<GetAuthor() <<std::endl);
238 bbtkMessage("help",1," Categories : " <<GetCategory() <<std::endl);
240 bbtkMessage("help",1," * Inputs : "<<std::endl);
242 bbtkMessage("help",1," * No inputs"<<std::endl);
243 InputDescriptorMapType::const_iterator i;
244 unsigned int namelmax = 0;
245 unsigned int typelmax = 0;
246 unsigned int natlmax = 0;
247 for ( i = mInput.begin(); i != mInput.end(); ++i )
249 if (i->second->GetName().size()>namelmax)
250 namelmax = i->second->GetName().size();
251 if (i->second->GetHumanTypeName().size()>typelmax)
252 typelmax = i->second->GetHumanTypeName().size();
253 if (i->second->GetNature().size()>natlmax)
254 natlmax = i->second->GetNature().size();
256 OutputDescriptorMapType::const_iterator o;
257 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
259 if (o->second->GetName().size()>namelmax)
260 namelmax = o->second->GetName().size();
261 if (o->second->GetHumanTypeName().size()>typelmax)
262 typelmax = o->second->GetHumanTypeName().size();
263 if (o->second->GetNature().size()>natlmax)
264 natlmax = o->second->GetNature().size();
267 for ( i = mInput.begin(); i != mInput.end(); ++i )
269 std::string name(i->second->GetName());
271 name.append(1+namelmax-name.size(),' ');
272 std::string type(i->second->GetHumanTypeName());
274 type.append(1+typelmax-type.size(),' ');
275 std::string nature(i->second->GetNature());
277 nature.append(1+natlmax-nature.size(),' ');
278 bbtkMessage("help",1,
282 <<" : "<<i->second->GetDescription()<<std::endl);
285 bbtkMessage("help",1," * Outputs : "<<std::endl);
287 bbtkMessage("help",1," * No outputs"<<std::endl);
288 for ( o = mOutput.begin(); o != mOutput.end(); ++o )
290 std::string name(o->second->GetName());
292 name.append(1+namelmax-name.size(),' ');
293 std::string type(o->second->GetHumanTypeName());
295 type.append(1+typelmax-type.size(),' ');
296 std::string nature(o->second->GetNature());
298 nature.append(1+natlmax-nature.size(),' ');
299 bbtkMessage("help",1,
303 <<" : "<<o->second->GetDescription()<<std::endl);
308 //=========================================================================
310 //=========================================================================
311 /// Returns the full name of the **TYPE** of the black box (+package name)
312 std::string BlackBoxDescriptor::GetFullTypeName() const
314 if (GetPackage()!=0) return GetPackage()->GetName() + "::" + mTypeName;
315 return "::" + mTypeName;
317 //=========================================================================
319 //=========================================================================
320 void BlackBoxDescriptor::InsertHtmlHelp ( std::ofstream& s,
321 int detail, int level,
322 const std::string& output_dir,
325 bbtkDDebugMessage("kernel",9,"BlackBoxDescriptor::InsertHtmlHelp()"
330 std::string name = GetTypeName();
331 Utilities::html_format(name);
334 (s) << "<a name=\""<<name<<"\"></a>\n";
336 "<a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
337 // (s) << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
338 // (s) << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
339 (s) << "<h2 class=\"section\">"<<name<<"</h2>\n";
342 std::string descr = GetDescription();
343 //Utilities::html_format(descr);
344 std::string author = GetAuthor();
345 Utilities::html_format(author);
347 std::vector<std::string> categories;
348 // Split the category string
349 std::string delimiters = ";,";
350 Utilities::SplitString(GetCategory(),
351 delimiters,categories);
354 (s) << "<p><TABLE cellspacing=0 cellpadding=3>\n";
355 (s) << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
356 << descr << "</TD></TR>\n";
357 (s) << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
358 << author << "</TD></TR>\n";
359 (s) << "<TR><TD style='vertical-align: top;'><b> Category(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> ";
360 std::vector<std::string>::iterator ci;
361 for (ci=categories.begin(); ci!=categories.end(); ++ci)
363 s << "<a href=\"../index-category.html#"<< *ci <<"\">" << *ci
368 (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 "
369 << GetPackage()->GetName() << "</TD></TR>\n";
374 //i->second->InsertHTMLGraph( &s , detail,level,dir);
378 std::string titlecol("#BBBBFF");
379 std::string usercol("#FFFFFF");
380 std::string ubbcol("#DDFFFF");
381 std::string wxbbcol("#EEFFFF");
384 // (s) << "<h3 class=\"subsection\">Inputs</h3>\n";
385 (s) << "<p><TABLE border=1 cellspacing=0 cellpadding=3>\n";
386 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
387 <<"\">Inputs</TD></TR>\n";
389 std::vector<std::string> user_defined;
390 std::vector<std::string> ubb_defined;
391 std::vector<std::string> wxbb_defined;
393 const BlackBoxDescriptor::InputDescriptorMapType& imap =
394 GetInputDescriptorMap();
395 InputDescriptorMapType::const_iterator in;
396 for ( in = imap.begin(); in != imap.end(); ++in )
398 // Skips system-defined inputs
399 std::string col(usercol);
401 if (in->second->GetCreatorTypeInfo() ==
402 typeid(AtomicBlackBoxDescriptor))
408 else if (in->second->GetCreatorTypeInfo() ==
409 typeid(WxBlackBoxDescriptor))
416 std::string name(in->second->GetName());
417 Utilities::html_format(name);
419 std::string type("<");
420 type += in->second->GetTypeName();
422 Utilities::html_format(type);
424 std::string descr(in->second->GetDescription());
425 //Utilities::html_format(descr);
429 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
430 +"\"><B><PRE> "+name+" </PRE></B></TD>"
431 + "<TD style='vertical-align: top;' bgcolor=\""+col
432 +"\"><I><PRE> "+type+" </PRE></I></TD>"
433 + "<TD style='vertical-align: top;' bgcolor=\""+col
434 +"\">"+descr+"</TD></TR>\n";
438 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
439 +"\"><B><PRE> "+name+" </PRE></B></TD>"
440 + "<TD style='vertical-align: top;' bgcolor=\""+col
441 +"\"><I><PRE> "+descr+" </PRE></I></TD>"
442 + "<TD style='vertical-align: top;' bgcolor=\""+col
443 +"\">"+type+"</TD></TR>\n";
445 if (iotype==0) user_defined.push_back(out);
446 else if (iotype==1) ubb_defined.push_back(out);
447 else if (iotype==2) wxbb_defined.push_back(out);
451 std::vector<std::string>::iterator hi;
452 for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
453 for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
454 for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
456 user_defined.clear();
458 wxbb_defined.clear();
462 // (s) << "<h3 class=\"subsection\">Outputs</h3>\n";
463 // (s) << "<TABLE border=1 cellspacing=0>\n";
464 (s) << "<TR><TD colspan=3 align=center bgcolor=\""<<titlecol
465 <<"\">Outputs</TD></TR>\n";
467 const BlackBoxDescriptor::OutputDescriptorMapType& omap =
468 GetOutputDescriptorMap();
470 BlackBoxDescriptor::OutputDescriptorMapType::const_iterator o;
472 for ( o = omap.begin(); o != omap.end(); ++o )
474 std::string col(usercol);
476 if (o->second->GetCreatorTypeInfo() ==
477 typeid(AtomicBlackBoxDescriptor))
483 else if (o->second->GetCreatorTypeInfo() ==
484 typeid(WxBlackBoxDescriptor))
491 std::string name(o->second->GetName());
492 Utilities::html_format(name);
494 std::string type("<");
495 type += o->second->GetTypeName();
497 Utilities::html_format(type);
499 std::string descr(o->second->GetDescription());
500 //Utilities::html_format(descr);
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> "+type+" </PRE></I></TD>"
508 + "<TD style='vertical-align: top;' bgcolor=\""+col
509 +"\">"+descr+"</TD></TR>\n";
512 "<TR><TD style='vertical-align: top;' bgcolor=\"" + col
513 +"\"><B><PRE> "+name+" </PRE></B></TD>"
514 + "<TD style='vertical-align: top;' bgcolor=\""+col
515 +"\"><I><PRE> "+descr+" </PRE></I></TD>"
516 + "<TD style='vertical-align: top;' bgcolor=\""+col
517 +"\">"+type+"</TD></TR>\n";
519 if (iotype==0) user_defined.push_back(out);
520 else if (iotype==1) ubb_defined.push_back(out);
521 else if (iotype==2) wxbb_defined.push_back(out);
525 for (hi=user_defined.begin();hi!=user_defined.end();++hi) s << *hi;
526 for (hi=wxbb_defined.begin();hi!=wxbb_defined.end();++hi) s << *hi;
527 for (hi=ubb_defined.begin();hi!=ubb_defined.end();++hi) s << *hi;
536 //=========================================================================
539 //==========================================================================
540 void BlackBoxDescriptor::SetTypeOfScript_Application()
542 SetTypeOfScript(TS_SCRIPT_APPLICATION);
544 //==========================================================================
547 //==========================================================================
548 bool BlackBoxDescriptor::IsTypeOfScript_Application()
550 return (GetTypeOfScript()==TS_SCRIPT_APPLICATION);
552 //==========================================================================
555 //==========================================================================
556 bool BlackBoxDescriptor::IsTypeOfScript_ComplexBox()
558 return (GetTypeOfScript()==TS_SCRIPT_COMPLEXBOX);
560 //==========================================================================