1 /*=========================================================================
4 Module: $RCSfile: bbtkPackage.cxx,v $
6 Date: $Date: 2008/04/08 06:59:30 $
7 Version: $Revision: 1.11 $
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(const DataInfo& typein,
125 const DataInfo& typeout,
126 const std::string& name) const
128 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
132 <<name<<"\")"<<bbtkendl);
134 AdaptorKey key(typein,typeout,
135 BlackBoxDescriptor::DEFAULT_ADAPTOR);
136 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
137 if (i == mAdaptorMap.end())
139 bbtkDebugDecTab("Kernel",8);
142 BlackBox* bb =i->second->CreateInstance(name);
143 bbtkDebugDecTab("Kernel",8);
147 //==========================================================================
149 //==========================================================================
150 /// Creates an instance of an adaptor of input type <typein> and
151 /// output type <typeout> with name <name>
152 BlackBox* Package::NewWidgetAdaptor(const DataInfo& typein,
153 const DataInfo& typeout,
154 const std::string& name) const
156 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
157 ">::NewWidgetAdaptor("
160 <<name<<"\")"<<bbtkendl);
162 AdaptorKey key(typein,typeout,
163 BlackBoxDescriptor::DEFAULT_WIDGET_ADAPTOR);
164 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
165 if (i == mAdaptorMap.end())
167 bbtkDebugDecTab("Kernel",8);
170 BlackBox* bb =i->second->CreateInstance(name);
171 bbtkDebugDecTab("Kernel",8);
175 //==========================================================================
177 //==========================================================================
178 /// Returns true is the package contains
179 /// an adaptor of input type <typein> and
180 /// output type <typeout>
181 /// If successfull then adaptor contains the black box type name
182 bool Package::FindWidgetAdaptor(const DataInfo& typein,
183 const DataInfo& typeout,
184 std::string& adaptor) const
186 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
187 ">::FindWidgetAdaptor("
189 <<typeout<<")"<<bbtkendl);
191 AdaptorKey key(typein,typeout,
192 BlackBoxDescriptor::DEFAULT_WIDGET_ADAPTOR);
193 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
194 if (i == mAdaptorMap.end())
196 bbtkDebugDecTab("Kernel",8);
199 adaptor = i->second->GetTypeName();
200 bbtkDebugDecTab("Kernel",8);
204 //==========================================================================
208 //==========================================================================
209 /// Returns true is the package contains
210 /// an adaptor of input type <typein> and
211 /// output type <typeout>
212 /// If successfull then adaptor contains the black box type name
213 bool Package::FindAdaptor(const DataInfo& typein,
214 const DataInfo& typeout,
215 std::string& adaptor) const
217 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
220 <<typeout<<")"<<bbtkendl);
222 AdaptorKey key(typein,typeout,
223 BlackBoxDescriptor::DEFAULT_ADAPTOR);
224 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
225 if (i == mAdaptorMap.end())
227 bbtkDebugDecTab("Kernel",8);
230 adaptor = i->second->GetTypeName();
231 bbtkDebugDecTab("Kernel",8);
235 //==========================================================================
238 //==========================================================================
239 /// Registers a black box descriptor in the package
240 bool Package::RegisterBlackBox(BlackBoxDescriptor* d)
242 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\")"<<std::endl);
244 mBlackBoxMap[d->GetTypeName()] = d;
247 // If it is a default adaptor, also register it in the adaptors map
248 if ( (d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR) ||
249 (d->GetKind() == BlackBoxDescriptor::DEFAULT_WIDGET_ADAPTOR) )
251 bbtkDebugMessage("Kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\") : The box is an adaptor, inserting it in adaptors map ..."<<std::endl);
252 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
253 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
254 DataInfo infoin(typein,d->GetInputDescriptor("In")->GetNature());
255 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
256 bbtkDebugMessage("Kernel",8,"LG Adaptor "<<infoin<<" to "<<infoout
257 <<" - kind="<<d->GetKind()<<std::endl);
259 AdaptorKey key(infoin,infoout,d->GetKind());
261 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
262 if (i == mAdaptorMap.end())
264 mAdaptorMap[key] = d;
266 // If already an adaptor registered : error
269 bbtkError("Package <"<<GetName()<<
270 "> : trying to register black box <"
272 <<"> as default adaptor but there is already a default adaptor registered (<"
273 <<i->second->GetTypeName()<<">)");
277 bbtkDebugDecTab("Kernel",8);
281 //==========================================================================
284 //==========================================================================
285 /// UnRegisters a black box descriptor from the package
286 void Package::UnRegisterBlackBox(const std::string& name)
288 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::UnRegisterBlackBox(\""<<name<<"\")"<<std::endl);
289 // Looking into the bb map
290 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
291 if (i == mBlackBoxMap.end())
293 bbtkDebugDecTab("Kernel",8);
294 bbtkError("UnRegister : The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
296 mBlackBoxMap.erase(i);
297 // Is it also in the adaptors map ?
299 AdaptorMapType::iterator j = mAdaptorMap.find(name);
300 if (j != mAdaptorMap.end())
302 mAdaptorMap.erase(j);
305 bbtkDebugDecTab("Kernel",8);
307 //==========================================================================
309 //==========================================================================
310 /// Changes the name of a black box type
311 void Package::ChangeBlackBoxName( const std::string& oldname, const std::string& newname )
313 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::ChangeBlackBoxName(\""<<oldname<<"\",\""<<newname<<"\")"<<std::endl);
314 // Looking into the bb map
315 BlackBoxMapType::iterator i = mBlackBoxMap.find(oldname);
316 if (i == mBlackBoxMap.end())
318 bbtkDebugDecTab("Kernel",8);
319 bbtkError("ChangeBlackBoxName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
322 i->second->SetTypeName(newname);
323 mBlackBoxMap[newname] = i->second;
324 mBlackBoxMap.erase(i);
326 bbtkDebugDecTab("Kernel",8);
328 //==========================================================================
332 //==========================================================================
333 /// Registers an adaptor descriptor in the package
334 bool Package::RegisterAdaptor(BlackBoxDescriptor* d)
336 bbtkDebugMessage("Kernel",8,"Package<"<<GetName()<<">::RegisterAdaptor(\""<<d->GetTypeName()<<"\")"<<std::endl);
338 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
339 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
340 AdaptorKey key(typein,typeout);
342 mAdaptorMap[key] = d;
345 //==========================================================================
349 //==========================================================================
350 /// Displays the list of black boxes of the package
351 void Package::PrintBlackBoxes(bool description, bool adaptors) const
353 unsigned int lmax = 0;
354 std::vector<std::string> names;
355 std::vector<std::string> kinds;
356 std::vector<std::string> descrs;
358 BlackBoxMapType::const_iterator i;
359 for (i=mBlackBoxMap.begin();
360 i!=mBlackBoxMap.end();
364 ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) )
366 std::string name(" ");
367 name += i->second->GetTypeName();
368 names.push_back(name);
371 if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR )
373 kind = std::string("[A]");
375 else if ( i->second->GetKind() ==
376 BlackBoxDescriptor::DEFAULT_ADAPTOR )
378 kind = std::string("[DA]");
380 kinds.push_back(kind);
382 unsigned int l = name.size()+kind.size();
383 if (l>lmax) lmax = l;
389 descr += i->second->GetDescription();
391 descrs.push_back(descr);
397 offs.append(lmax+3,' ');
398 std::vector<std::string>::iterator ni,ci,di;
399 for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin();
400 ni != names.end(); ++ni, ++ci, ++di)
403 space.append(lmax - ni->size() - ci->size(),' ');
404 bbtkMessage("Help",1,*ni << space << *ci );
406 unsigned int dmax = 75 - lmax;
407 // while (d.size() > dmax )
410 bbtkMessage("Help",1,d.substr(0,dmax) << "..." << std::endl);
412 bbtkMessage("Help",1,d << std::endl);
413 // d = d.substr(dmax,d.size());
418 //==========================================================================
420 //==========================================================================
421 /// Displays the list of adaptors of the package
422 void Package::PrintAdaptors(bool description) const
424 BlackBoxMapType::const_iterator i;
425 for (i=mBlackBoxMap.begin();
426 i!=mBlackBoxMap.end();
429 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD )
431 bbtkMessage("Help",1,
432 " "<<i->second->GetTypeName());
433 if ( i->second->GetKind() ==
434 BlackBoxDescriptor::DEFAULT_ADAPTOR )
436 bbtkMessage("Help",1,
441 bbtkMessage("Help",1,
442 " : "<<i->second->GetDescription());
445 bbtkMessage("Help",1,std::endl);
449 AdaptorMapType::const_iterator i;
450 for (i=mAdaptorMap.begin();
451 i!=mAdaptorMap.end();
454 bbtkMessage("Help",1,
455 " "<<i->second->GetTypeName());
458 bbtkMessage("Help",1,
459 " : "<<i->second->GetDescription());
462 bbtkMessage("Help",1,std::endl);
466 //==========================================================================
468 //==========================================================================
469 /// Prints help on a black box
470 void Package::HelpBlackBox(const std::string& name, bool full) const
472 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
473 <<name<<"\")"<<bbtkendl);
475 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
476 if (i == mBlackBoxMap.end())
478 bbtkDebugDecTab("Kernel",8);
479 bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
481 // bbtkMessage("Help",1,"["<<GetName()<<"] ");
482 i->second->GetHelp(full);
483 bbtkDebugDecTab("Kernel",8);
486 //==========================================================================
489 //==========================================================================
490 /// Returns true iff the package contains the box of name boxname
491 bool Package::ContainsBlackBox(const std::string& name) const
493 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
494 <<name<<"\")"<<bbtkendl);
496 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
497 if (i == mBlackBoxMap.end())
499 bbtkDebugDecTab("Kernel",8);
502 bbtkDebugDecTab("Kernel",8);
505 //==========================================================================
509 //==========================================================================
510 void Package::CreateHtmlPage(const std::string& filename,
511 const std::string& caller,
512 const std::string& source,
513 const std::string& custom_header,
514 const std::string& custom_title,
517 bool relative_link ) const
519 bbtkDebugMessageInc("Kernel",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
520 <<filename<<"\")"<<bbtkendl);
522 //---------------------
525 s.open(filename.c_str());
528 bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
531 //----------------------
533 std::string title = "BBTK Package "+GetName()+" "+GetVersion();
535 if (custom_title.length() != 0) title = custom_title;
537 s << "<html lang=\"en\">\n";
539 s << "<title>" << title << "</title>\n";
540 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
541 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
542 s << "<meta name=\"generator\" content=\"\">\n";
543 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
544 //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
545 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
546 s << "pre.display { font-family:inherit }\n";
547 s << "pre.format { font-family:inherit }\n";
548 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
549 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
550 s << "pre.smallexample { font-size:smaller }\n";
551 s << "pre.smalllisp { font-size:smaller }\n";
552 s << "span.sc { font-variant:small-caps }\n";
553 s << "span.roman { font-family:serif; font-weight:normal; } \n";
554 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
555 s << "--></style>\n";
557 //----------------------
559 //----------------------
562 s << "<a name=\"Top\"></a>\n";
564 //----------------------
566 if ( custom_header.length() != 0)
568 if ( custom_header != "none" )
571 in.open(custom_header.c_str());
574 bbtkError("Could not open file \""<<custom_header<<"\"");
579 in.getline(buffer,512);
580 std::string line(buffer);
587 s << "<object data=\"" << custom_header
588 << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
589 << custom_header <<" could not be embedded.</object>\n";
598 s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
599 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
600 s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
601 << GetDescription() << "</TD></TR>\n";
602 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
603 << GetAuthor() << "</TD></TR>\n";
604 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
605 << GetCategory() << "</TD></TR>\n";
606 s << "<TR><TD style='vertical-align: top;'><b> Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
607 << GetVersion() << "</TD></TR>\n";
608 s << "<TR><TD style='vertical-align: top;'><b> bbtk Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
609 << GetBBTKVersion() << "</TD></TR>\n";
613 //-------------------
616 // s << "<div class=\"contents\">\n";
617 s << "<p><b> Black Boxes : </b>\n";
620 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
622 BlackBoxMapType::const_iterator i;
623 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end(); ++i)
625 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD)
628 std::string name = i->second->GetTypeName();
629 Utilities::html_format(name);
630 std::string descr = i->second->GetDescription();
631 Utilities::html_format(descr);
634 s << "<TD style='vertical-align: top;'>";
635 s << " <a name=\"toc_"<<name
636 <<"\" href=\"#"<<name<<"\">"
639 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
648 //-------------------
650 if (mAdaptorMap.size()>0)
652 // s << "<div class=\"contents\">\n";
653 s << "<p><b> Adaptors : </b>\n";
656 // BlackBoxMapType::const_iterator i;
657 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
658 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end();++i)
660 if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD)
663 std::string name = i->second->GetTypeName();
664 Utilities::html_format(name);
665 std::string descr = i->second->GetDescription();
668 s << "<TD style='vertical-align: top;'>";
669 s << " <a name=\"toc_"<<name
670 <<"\" href=\"#"<<name<<"\">"
673 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
683 // s << "<div class=\"node\">\n";
686 // s << "<a name=\"Top\"></a>\n";
687 // s << "Top: <a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
688 // s << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
689 // s << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
693 //----------------------
696 //-------------------
697 // Computes output directory from filename to pass it to
698 // BlackBoxDescriptor::InsertHtmlHelp
701 std::string::size_type slash_position = filename.find_last_of("/\\");
704 if (slash_position != std::string::npos) {
705 if (slash_position == 0)
707 dir = filename.substr(0,slash_position);
710 for (i=mBlackBoxMap.begin();
711 i!=mBlackBoxMap.end();
714 i->second->InsertHtmlHelp(s,detail,level,dir,relative_link);
717 //----------------------
722 ptm = gmtime ( &rawtime );
725 s << "Automatically generated by <b>"<<caller<<"</b> "//from <b>"
728 << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900
729 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
730 s << "</body></html>\n";
732 //----------------------
735 bbtkDebugDecTab("Kernel",9);
737 //==========================================================================