1 /*=========================================================================
4 Module: $RCSfile: bbtkPackage.cxx,v $
6 Date: $Date: 2008/03/07 08:40:14 $
7 Version: $Revision: 1.10 $
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 =========================================================================*/
20 *\brief Class bbtk::Package : registers black boxes descriptors and is able to create instances of the black boxes registered.
22 #include "bbtkPackage.h"
23 #include "bbtkMessageManager.h"
24 #include "bbtkConfigurationFile.h"
27 #include "bbtkUtilities.h"
31 //==========================================================================
32 /// Ctor with the name of the package
33 Package::Package(const std::string& name,
34 const std::string& author,
35 const std::string& description,
36 const std::string& version,
37 const std::string& BBTKVersion)
41 mDescription(description),
43 mBBTKVersion(BBTKVersion)
45 std::string default_doc_dir = ConfigurationFile::GetInstance().Get_default_temp_dir();
46 char c = default_doc_dir.c_str()[strlen(default_doc_dir.c_str())-1];
47 std::string url = default_doc_dir;
48 if (c != '/' && c !='\\') url = url + "/";
49 url = url + "temp_dir/" + name + "/index.html";
52 SetDocRelativeURL("Relative url not set");
55 std::string relurl(BBTK_STRINGIFY_SYMBOL(BBTK_DOC_REL_PATH));
56 relurl += "/packages/"+name+"/bbdoc/index.html";
57 std::string url = ConfigurationFile::GetInstance().Get_url()
60 SetDocRelativeURL(relurl);
63 // std::cout << " url=["<<url<<"]"<<std::endl;
64 // std::cout << "relurl=["<<relurl<<"]"<<std::endl;
65 bbtkDebugMessage("Kernel",7,"Package::Package(\""<<name<<"\")"<<bbtkendl);
67 //==========================================================================
71 //==========================================================================
75 bbtkDebugMessageInc("Kernel",7,"Package::~Package(\""<<mName<<"\")"<<bbtkendl);
76 BlackBoxMapType::const_iterator i;
77 for (i=mBlackBoxMap.begin();
78 i!=mBlackBoxMap.end();
83 // Adaptors are also stored in the black box map : hence already deleted
85 AdaptorMapType::const_iterator j;
86 for (j=mAdaptorMap.begin();
93 bbtkDebugDecTab("Kernel",7);
95 //==========================================================================
99 //==========================================================================
100 /// Creates an instance of a black box of type <type> with name <name>
101 BlackBox* Package::NewBlackBox(const std::string& type,
102 const std::string& name) const
104 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::NewBlackBox(\""<<type<<"\",\""<<name<<"\")"<<bbtkendl);
106 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(type);
107 if (i == mBlackBoxMap.end())
109 bbtkDebugDecTab("Kernel",8);
112 BlackBox* bb =i->second->CreateInstance(name);
113 bbtkDebugDecTab("Kernel",8);
117 //==========================================================================
121 //==========================================================================
122 /// Creates an instance of an adaptor of input type <typein> and
123 /// output type <typeout> with name <name>
124 BlackBox* Package::NewAdaptor(TypeInfo typein,
126 const std::string& name) const
128 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
130 <<TypeName(typein)<<">,<"
131 <<TypeName(typeout)<<">,\""
132 <<name<<"\")"<<bbtkendl);
134 AdaptorKey key(typein,typeout);
135 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
136 if (i == mAdaptorMap.end())
138 bbtkDebugDecTab("Kernel",8);
141 BlackBox* bb =i->second->CreateInstance(name);
142 bbtkDebugDecTab("Kernel",8);
146 //==========================================================================
151 //==========================================================================
152 /// Registers a black box descriptor in the package
153 bool Package::RegisterBlackBox(BlackBoxDescriptor* d)
155 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\")"<<std::endl);
157 mBlackBoxMap[d->GetTypeName()] = d;
160 // If it is a default adaptor, also register it in the adaptors map
161 if ( d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR)
163 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
164 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
165 AdaptorKey key(typein,typeout);
166 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
167 if (i == mAdaptorMap.end())
169 bbtkDebugMessage("Kernel",8,"The box is an adaptor, inserting it in adaptors map ..."<<std::endl);
170 mAdaptorMap[key] = d;
172 // If already an adaptor registered : error
175 bbtkError("Package <"<<GetName()<<
176 "> : trying to register black box <"
178 <<"> as default adaptor but there is already a default adaptor registered (<"
179 <<i->second->GetTypeName()<<">)");
183 bbtkDebugDecTab("Kernel",8);
187 //==========================================================================
190 //==========================================================================
191 /// UnRegisters a black box descriptor from the package
192 void Package::UnRegisterBlackBox(const std::string& name)
194 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::UnRegisterBlackBox(\""<<name<<"\")"<<std::endl);
195 // Looking into the bb map
196 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
197 if (i == mBlackBoxMap.end())
199 bbtkDebugDecTab("Kernel",8);
200 bbtkError("UnRegister : The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
202 mBlackBoxMap.erase(i);
203 // Is it also in the adaptors map ?
205 AdaptorMapType::iterator j = mAdaptorMap.find(name);
206 if (j != mAdaptorMap.end())
208 mAdaptorMap.erase(j);
211 bbtkDebugDecTab("Kernel",8);
213 //==========================================================================
215 //==========================================================================
216 /// Changes the name of a black box type
217 void Package::ChangeBlackBoxName( const std::string& oldname, const std::string& newname )
219 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::ChangeBlackBoxName(\""<<oldname<<"\",\""<<newname<<"\")"<<std::endl);
220 // Looking into the bb map
221 BlackBoxMapType::iterator i = mBlackBoxMap.find(oldname);
222 if (i == mBlackBoxMap.end())
224 bbtkDebugDecTab("Kernel",8);
225 bbtkError("ChangeBlackBoxName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
228 i->second->SetTypeName(newname);
229 mBlackBoxMap[newname] = i->second;
230 mBlackBoxMap.erase(i);
232 bbtkDebugDecTab("Kernel",8);
234 //==========================================================================
238 //==========================================================================
239 /// Registers an adaptor descriptor in the package
240 bool Package::RegisterAdaptor(BlackBoxDescriptor* d)
242 bbtkDebugMessage("Kernel",8,"Package<"<<GetName()<<">::RegisterAdaptor(\""<<d->GetTypeName()<<"\")"<<std::endl);
244 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
245 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
246 AdaptorKey key(typein,typeout);
248 mAdaptorMap[key] = d;
251 //==========================================================================
255 //==========================================================================
256 /// Displays the list of black boxes of the package
257 void Package::PrintBlackBoxes(bool description, bool adaptors) const
259 unsigned int lmax = 0;
260 std::vector<std::string> names;
261 std::vector<std::string> kinds;
262 std::vector<std::string> descrs;
264 BlackBoxMapType::const_iterator i;
265 for (i=mBlackBoxMap.begin();
266 i!=mBlackBoxMap.end();
270 ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) )
272 std::string name(" ");
273 name += i->second->GetTypeName();
274 names.push_back(name);
277 if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR )
279 kind = std::string("[A]");
281 else if ( i->second->GetKind() ==
282 BlackBoxDescriptor::DEFAULT_ADAPTOR )
284 kind = std::string("[DA]");
286 kinds.push_back(kind);
288 unsigned int l = name.size()+kind.size();
289 if (l>lmax) lmax = l;
295 descr += i->second->GetDescription();
297 descrs.push_back(descr);
303 offs.append(lmax+3,' ');
304 std::vector<std::string>::iterator ni,ci,di;
305 for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin();
306 ni != names.end(); ++ni, ++ci, ++di)
309 space.append(lmax - ni->size() - ci->size(),' ');
310 bbtkMessage("Help",1,*ni << space << *ci );
312 unsigned int dmax = 75 - lmax;
313 // while (d.size() > dmax )
316 bbtkMessage("Help",1,d.substr(0,dmax) << "..." << std::endl);
318 bbtkMessage("Help",1,d << std::endl);
319 // d = d.substr(dmax,d.size());
324 //==========================================================================
326 //==========================================================================
327 /// Displays the list of adaptors of the package
328 void Package::PrintAdaptors(bool description) const
330 BlackBoxMapType::const_iterator i;
331 for (i=mBlackBoxMap.begin();
332 i!=mBlackBoxMap.end();
335 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD )
337 bbtkMessage("Help",1,
338 " "<<i->second->GetTypeName());
339 if ( i->second->GetKind() ==
340 BlackBoxDescriptor::DEFAULT_ADAPTOR )
342 bbtkMessage("Help",1,
347 bbtkMessage("Help",1,
348 " : "<<i->second->GetDescription());
351 bbtkMessage("Help",1,std::endl);
355 AdaptorMapType::const_iterator i;
356 for (i=mAdaptorMap.begin();
357 i!=mAdaptorMap.end();
360 bbtkMessage("Help",1,
361 " "<<i->second->GetTypeName());
364 bbtkMessage("Help",1,
365 " : "<<i->second->GetDescription());
368 bbtkMessage("Help",1,std::endl);
372 //==========================================================================
374 //==========================================================================
375 /// Prints help on a black box
376 void Package::HelpBlackBox(const std::string& name, bool full) const
378 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
379 <<name<<"\")"<<bbtkendl);
381 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
382 if (i == mBlackBoxMap.end())
384 bbtkDebugDecTab("Kernel",8);
385 bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
387 // bbtkMessage("Help",1,"["<<GetName()<<"] ");
388 i->second->GetHelp(full);
389 bbtkDebugDecTab("Kernel",8);
392 //==========================================================================
395 //==========================================================================
396 /// Returns true iff the package contains the box of name boxname
397 bool Package::ContainsBlackBox(const std::string& name) const
399 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
400 <<name<<"\")"<<bbtkendl);
402 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
403 if (i == mBlackBoxMap.end())
405 bbtkDebugDecTab("Kernel",8);
408 bbtkDebugDecTab("Kernel",8);
411 //==========================================================================
415 //==========================================================================
416 void Package::CreateHtmlPage(const std::string& filename,
417 const std::string& caller,
418 const std::string& source,
419 const std::string& custom_header,
420 const std::string& custom_title,
423 bool relative_link ) const
425 bbtkDebugMessageInc("Kernel",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
426 <<filename<<"\")"<<bbtkendl);
428 //---------------------
431 s.open(filename.c_str());
434 bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
437 //----------------------
439 std::string title = "BBTK Package "+GetName()+" "+GetVersion();
441 if (custom_title.length() != 0) title = custom_title;
443 s << "<html lang=\"en\">\n";
445 s << "<title>" << title << "</title>\n";
446 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
447 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
448 s << "<meta name=\"generator\" content=\"\">\n";
449 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
450 //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
451 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
452 s << "pre.display { font-family:inherit }\n";
453 s << "pre.format { font-family:inherit }\n";
454 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
455 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
456 s << "pre.smallexample { font-size:smaller }\n";
457 s << "pre.smalllisp { font-size:smaller }\n";
458 s << "span.sc { font-variant:small-caps }\n";
459 s << "span.roman { font-family:serif; font-weight:normal; } \n";
460 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
461 s << "--></style>\n";
463 //----------------------
465 //----------------------
468 s << "<a name=\"Top\"></a>\n";
470 //----------------------
472 if ( custom_header.length() != 0)
474 if ( custom_header != "none" )
477 in.open(custom_header.c_str());
480 bbtkError("Could not open file \""<<custom_header<<"\"");
485 in.getline(buffer,512);
486 std::string line(buffer);
493 s << "<object data=\"" << custom_header
494 << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
495 << custom_header <<" could not be embedded.</object>\n";
504 s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
505 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
506 s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
507 << GetDescription() << "</TD></TR>\n";
508 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
509 << GetAuthor() << "</TD></TR>\n";
510 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
511 << GetCategory() << "</TD></TR>\n";
512 s << "<TR><TD style='vertical-align: top;'><b> Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
513 << GetVersion() << "</TD></TR>\n";
514 s << "<TR><TD style='vertical-align: top;'><b> bbtk Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
515 << GetBBTKVersion() << "</TD></TR>\n";
519 //-------------------
522 // s << "<div class=\"contents\">\n";
523 s << "<p><b> Black Boxes : </b>\n";
526 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
528 BlackBoxMapType::const_iterator i;
529 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i)
531 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD)
534 std::string name = i->second->GetTypeName();
535 Utilities::html_format(name);
536 std::string descr = i->second->GetDescription();
537 Utilities::html_format(descr);
540 s << "<TD style='vertical-align: top;'>";
541 s << " <a name=\"toc_"<<name
542 <<"\" href=\"#"<<name<<"\">"
545 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
554 //-------------------
556 if (mAdaptorMap.size()>0)
558 // s << "<div class=\"contents\">\n";
559 s << "<p><b> Adaptors : </b>\n";
562 // BlackBoxMapType::const_iterator i;
563 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
564 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end();++i)
566 if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD)
569 std::string name = i->second->GetTypeName();
570 Utilities::html_format(name);
571 std::string descr = i->second->GetDescription();
574 s << "<TD style='vertical-align: top;'>";
575 s << " <a name=\"toc_"<<name
576 <<"\" href=\"#"<<name<<"\">"
579 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
589 // s << "<div class=\"node\">\n";
592 // s << "<a name=\"Top\"></a>\n";
593 // s << "Top: <a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
594 // s << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
595 // s << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
599 //----------------------
602 //-------------------
603 // Computes output directory from filename to pass it to
604 // BlackBoxDescriptor::InsertHtmlHelp
607 std::string::size_type slash_position = filename.find_last_of("/\\");
610 if (slash_position != std::string::npos) {
611 if (slash_position == 0)
613 dir = filename.substr(0,slash_position);
616 for (i=mBlackBoxMap.begin();
617 i!=mBlackBoxMap.end();
620 i->second->InsertHtmlHelp(s,detail,level,dir,relative_link);
623 //----------------------
628 ptm = gmtime ( &rawtime );
631 s << "Automatically generated by <b>"<<caller<<"</b> "//from <b>"
634 << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900
635 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
636 s << "</body></html>\n";
638 //----------------------
641 bbtkDebugDecTab("Kernel",9);
643 //==========================================================================