1 /*=========================================================================
4 Module: $RCSfile: bbtkPackage.cxx,v $
6 Date: $Date: 2008/04/09 11:16:57 $
7 Version: $Revision: 1.12 $
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();
81 i->second->UnReference();
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 //==========================================================================
179 //==========================================================================
180 /// Returns true is the package contains
181 /// an adaptor of input type <typein> and
182 /// output type <typeout>
183 /// If successfull then adaptor contains the black box type name
184 bool Package::FindWidgetAdaptor(const DataInfo& typein,
185 const DataInfo& typeout,
186 std::string& adaptor) const
188 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
189 ">::FindWidgetAdaptor("
191 <<typeout<<")"<<bbtkendl);
193 AdaptorKey key(typein,typeout,
194 BlackBoxDescriptor::DEFAULT_WIDGET_ADAPTOR);
195 // First try to find a single widget adaptor
196 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
197 if (i == mAdaptorMap.end())
199 bbtkDebugDecTab("Kernel",8);
202 adaptor = i->second->GetTypeName();
203 bbtkDebugDecTab("Kernel",8);
207 //==========================================================================
211 //==========================================================================
212 /// Returns true is the package contains
213 /// an adaptor of input type <typein> and
214 /// output type <typeout>
215 /// If successfull then adaptor contains the black box type name
216 bool Package::FindAdaptor(const DataInfo& typein,
217 const DataInfo& typeout,
218 std::string& adaptor) const
220 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<
223 <<typeout<<")"<<bbtkendl);
225 AdaptorKey key(typein,typeout,
226 BlackBoxDescriptor::DEFAULT_ADAPTOR);
227 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
228 if (i == mAdaptorMap.end())
230 bbtkDebugDecTab("Kernel",8);
233 adaptor = i->second->GetTypeName();
234 bbtkDebugDecTab("Kernel",8);
238 //==========================================================================
241 //==========================================================================
242 /// Registers a black box descriptor in the package
243 bool Package::RegisterBlackBox(BlackBoxDescriptor* d)
245 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\")"<<std::endl);
247 BlackBoxMapType::iterator i = mBlackBoxMap.find(d->GetTypeName());
248 if (i!=mBlackBoxMap.end())
250 bbtkWarning("Package<"<<GetName()<<"> : Trying to register box type <"
251 <<d->GetTypeName()<<"> which is already in the package");
255 mBlackBoxMap[d->GetTypeName()] = d;
259 // If it is a default adaptor, also register it in the adaptors map
260 if ( (d->GetKind() == BlackBoxDescriptor::DEFAULT_ADAPTOR) ||
261 (d->GetKind() == BlackBoxDescriptor::DEFAULT_WIDGET_ADAPTOR) )
263 bbtkDebugMessage("Kernel",8,"Package<"<<GetName()<<">::RegisterBlackBox(\""<<d->GetTypeName()<<"\") : The box is an adaptor, inserting it in adaptors map ..."<<std::endl);
264 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
265 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
266 DataInfo infoin(typein,d->GetInputDescriptor("In")->GetNature());
267 DataInfo infoout(typeout,d->GetOutputDescriptor("Out")->GetNature());
268 bbtkDebugMessage("Kernel",9,"Adaptor "<<infoin<<" to "<<infoout
269 <<" - kind="<<d->GetKind()<<std::endl);
271 AdaptorKey key(infoin,infoout,d->GetKind());
273 AdaptorMapType::const_iterator i = mAdaptorMap.find(key);
274 if (i == mAdaptorMap.end())
276 mAdaptorMap[key] = d;
278 // If already an adaptor registered : error
281 if (i->second->GetTypeName() != d->GetTypeName())
283 bbtkError("Package <"<<GetName()<<
284 "> : trying to register black box <"
286 <<"> as default adaptor but there is already a default adaptor registered (<"
287 <<i->second->GetTypeName()<<">)");
292 bbtkDebugDecTab("Kernel",8);
296 //==========================================================================
298 //===================================================================
299 void Package::CheckBoxes() const
301 bbtkMessage("Debug",1,"****** Checking Package "<<(void*)this
302 <<" ["<<GetName()<<"]"<<std::endl);
303 BlackBoxMapType::const_iterator i;
304 for (i=mBlackBoxMap.begin();
305 i!=mBlackBoxMap.end();
308 i->second->Check(true);
310 bbtkMessage("Debug",1,"****** Checking Package "<<(void*)this
311 <<" ["<<GetName()<<"] ... OK"<<std::endl);
313 //===================================================================
315 //==========================================================================
316 /// UnRegisters a black box descriptor from the package
317 void Package::UnRegisterBlackBox(const std::string& name)
319 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::UnRegisterBlackBox(\""<<name<<"\")"<<std::endl);
320 // Looking into the bb map
321 BlackBoxMapType::iterator i = mBlackBoxMap.find(name);
322 if (i == mBlackBoxMap.end())
324 bbtkDebugDecTab("Kernel",8);
325 bbtkError("UnRegister : The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
327 mBlackBoxMap.erase(i);
329 // Is it also in the adaptors map ?
331 AdaptorMapType::iterator j = mAdaptorMap.find(name);
332 if (j != mAdaptorMap.end())
334 mAdaptorMap.erase(j);
338 i->second->UnReference();
340 bbtkDebugDecTab("Kernel",8);
342 //==========================================================================
344 //==========================================================================
345 /// Changes the name of a black box type
346 void Package::ChangeBlackBoxName( const std::string& oldname, const std::string& newname )
348 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::ChangeBlackBoxName(\""<<oldname<<"\",\""<<newname<<"\")"<<std::endl);
349 // Looking into the bb map
350 BlackBoxMapType::iterator i = mBlackBoxMap.find(oldname);
351 if (i == mBlackBoxMap.end())
353 bbtkDebugDecTab("Kernel",8);
354 bbtkError("ChangeBlackBoxName : The package <"<<GetName()<<"> does not contains the black box <"<<oldname<<">");
357 i->second->SetTypeName(newname);
358 mBlackBoxMap[newname] = i->second;
359 mBlackBoxMap.erase(i);
361 bbtkDebugDecTab("Kernel",8);
363 //==========================================================================
367 //==========================================================================
368 /// Registers an adaptor descriptor in the package
369 bool Package::RegisterAdaptor(BlackBoxDescriptor* d)
371 bbtkDebugMessage("Kernel",8,"Package<"<<GetName()<<">::RegisterAdaptor(\""<<d->GetTypeName()<<"\")"<<std::endl);
373 TypeInfo typein = d->GetInputDescriptor("In")->GetTypeInfo();
374 TypeInfo typeout = d->GetOutputDescriptor("Out")->GetTypeInfo();
375 AdaptorKey key(typein,typeout);
377 mAdaptorMap[key] = d;
380 //==========================================================================
384 //==========================================================================
385 /// Displays the list of black boxes of the package
386 void Package::PrintBlackBoxes(bool description, bool adaptors) const
388 unsigned int lmax = 0;
389 std::vector<std::string> names;
390 std::vector<std::string> kinds;
391 std::vector<std::string> descrs;
393 BlackBoxMapType::const_iterator i;
394 for (i=mBlackBoxMap.begin();
395 i!=mBlackBoxMap.end();
399 ( i->second->GetKind() == BlackBoxDescriptor::STANDARD) )
401 std::string name(" ");
402 name += i->second->GetTypeName();
403 names.push_back(name);
406 if ( i->second->GetKind() == BlackBoxDescriptor::ADAPTOR )
408 kind = std::string("[A]");
410 else if ( i->second->GetKind() ==
411 BlackBoxDescriptor::DEFAULT_ADAPTOR )
413 kind = std::string("[DA]");
415 kinds.push_back(kind);
417 unsigned int l = name.size()+kind.size();
418 if (l>lmax) lmax = l;
424 descr += i->second->GetDescription();
426 descrs.push_back(descr);
432 offs.append(lmax+3,' ');
433 std::vector<std::string>::iterator ni,ci,di;
434 for (ni = names.begin(), ci = kinds.begin(), di = descrs.begin();
435 ni != names.end(); ++ni, ++ci, ++di)
438 space.append(lmax - ni->size() - ci->size(),' ');
439 bbtkMessage("Help",1,*ni << space << *ci );
441 unsigned int dmax = 75 - lmax;
442 // while (d.size() > dmax )
445 bbtkMessage("Help",1,d.substr(0,dmax) << "..." << std::endl);
447 bbtkMessage("Help",1,d << std::endl);
448 // d = d.substr(dmax,d.size());
453 //==========================================================================
455 //==========================================================================
456 /// Displays the list of adaptors of the package
457 void Package::PrintAdaptors(bool description) const
459 BlackBoxMapType::const_iterator i;
460 for (i=mBlackBoxMap.begin();
461 i!=mBlackBoxMap.end();
464 if ( i->second->GetKind() != BlackBoxDescriptor::STANDARD )
466 bbtkMessage("Help",1,
467 " "<<i->second->GetTypeName());
468 if ( i->second->GetKind() ==
469 BlackBoxDescriptor::DEFAULT_ADAPTOR )
471 bbtkMessage("Help",1,
476 bbtkMessage("Help",1,
477 " : "<<i->second->GetDescription());
480 bbtkMessage("Help",1,std::endl);
484 AdaptorMapType::const_iterator i;
485 for (i=mAdaptorMap.begin();
486 i!=mAdaptorMap.end();
489 bbtkMessage("Help",1,
490 " "<<i->second->GetTypeName());
493 bbtkMessage("Help",1,
494 " : "<<i->second->GetDescription());
497 bbtkMessage("Help",1,std::endl);
501 //==========================================================================
503 //==========================================================================
504 /// Prints help on a black box
505 void Package::HelpBlackBox(const std::string& name, bool full) const
507 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
508 <<name<<"\")"<<bbtkendl);
510 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
511 if (i == mBlackBoxMap.end())
513 bbtkDebugDecTab("Kernel",8);
514 bbtkError("The package <"<<GetName()<<"> does not contains the black box <"<<name<<">");
516 // bbtkMessage("Help",1,"["<<GetName()<<"] ");
517 i->second->GetHelp(full);
518 bbtkDebugDecTab("Kernel",8);
521 //==========================================================================
524 //==========================================================================
525 /// Returns true iff the package contains the box of name boxname
526 bool Package::ContainsBlackBox(const std::string& name) const
528 bbtkDebugMessageInc("Kernel",8,"Package<"<<GetName()<<">::HelpBlackBox(\""
529 <<name<<"\")"<<bbtkendl);
531 BlackBoxMapType::const_iterator i = mBlackBoxMap.find(name);
532 if (i == mBlackBoxMap.end())
534 bbtkDebugDecTab("Kernel",8);
537 bbtkDebugDecTab("Kernel",8);
540 //==========================================================================
544 //==========================================================================
545 void Package::CreateHtmlPage(const std::string& filename,
546 const std::string& caller,
547 const std::string& source,
548 const std::string& custom_header,
549 const std::string& custom_title,
552 bool relative_link ) const
554 bbtkDebugMessageInc("Kernel",9,"Package<"<<GetName()<<">::CreateHtmlPage(\""
555 <<filename<<"\")"<<bbtkendl);
557 //---------------------
560 s.open(filename.c_str());
563 bbtkError("Package "<<GetName()<<" : CreateHtmlPage : could not open file '"<<filename<<"'");
566 //----------------------
568 std::string title = "BBTK Package "+GetName()+" "+GetVersion();
570 if (custom_title.length() != 0) title = custom_title;
572 s << "<html lang=\"en\">\n";
574 s << "<title>" << title << "</title>\n";
575 s << "<meta http-equiv=\"Content-Type\" content=\"text/html\">\n";
576 s << "<meta name=\"description\" content=\""<<title<<"\">\n";
577 s << "<meta name=\"generator\" content=\"\">\n";
578 s << "<link title=\"Top\" rel=\"top\" href=\"#Top\">\n";
579 //<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
580 s << "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\"><style type=\"text/css\"><!--\n";
581 s << "pre.display { font-family:inherit }\n";
582 s << "pre.format { font-family:inherit }\n";
583 s << "pre.smalldisplay { font-family:inherit; font-size:smaller }\n";
584 s << "pre.smallformat { font-family:inherit; font-size:smaller }\n";
585 s << "pre.smallexample { font-size:smaller }\n";
586 s << "pre.smalllisp { font-size:smaller }\n";
587 s << "span.sc { font-variant:small-caps }\n";
588 s << "span.roman { font-family:serif; font-weight:normal; } \n";
589 s << "span.sansserif { font-family:sans-serif; font-weight:normal; }\n";
590 s << "--></style>\n";
592 //----------------------
594 //----------------------
597 s << "<a name=\"Top\"></a>\n";
599 //----------------------
601 if ( custom_header.length() != 0)
603 if ( custom_header != "none" )
606 in.open(custom_header.c_str());
609 bbtkError("Could not open file \""<<custom_header<<"\"");
614 in.getline(buffer,512);
615 std::string line(buffer);
622 s << "<object data=\"" << custom_header
623 << "\" type = \"text/html\"\"style=\"width: 1200px; height: 400px;\"> Warning: "
624 << custom_header <<" could not be embedded.</object>\n";
633 s << "<h1 class=\"settitle\">"<<title<<"</h1>\n";
634 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
635 s << "<TR><TD style='vertical-align: top;'><b> Description </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
636 << GetDescription() << "</TD></TR>\n";
637 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
638 << GetAuthor() << "</TD></TR>\n";
639 s << "<TR><TD style='vertical-align: top;'><b> Author(s) </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
640 << GetCategory() << "</TD></TR>\n";
641 s << "<TR><TD style='vertical-align: top;'><b> Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
642 << GetVersion() << "</TD></TR>\n";
643 s << "<TR><TD style='vertical-align: top;'><b> bbtk Version </b></TD><TD style='vertical-align: top;'> : </TD><TD style='vertical-align: top;'> "
644 << GetBBTKVersion() << "</TD></TR>\n";
648 //-------------------
651 // s << "<div class=\"contents\">\n";
652 s << "<p><b> Black Boxes : </b>\n";
655 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
657 BlackBoxMapType::const_iterator i;
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();
666 Utilities::html_format(descr);
669 s << "<TD style='vertical-align: top;'>";
670 s << " <a name=\"toc_"<<name
671 <<"\" href=\"#"<<name<<"\">"
674 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
683 //-------------------
685 if (mAdaptorMap.size()>0)
687 // s << "<div class=\"contents\">\n";
688 s << "<p><b> Adaptors : </b>\n";
691 // BlackBoxMapType::const_iterator i;
692 s << "<p><TABLE cellspacing=0 cellpadding=3>\n";
693 for (i=mBlackBoxMap.begin(); i!=mBlackBoxMap.end();++i)
695 if ( i->second->GetKind() == BlackBoxDescriptor::STANDARD)
698 std::string name = i->second->GetTypeName();
699 Utilities::html_format(name);
700 std::string descr = i->second->GetDescription();
703 s << "<TD style='vertical-align: top;'>";
704 s << " <a name=\"toc_"<<name
705 <<"\" href=\"#"<<name<<"\">"
708 s << " <TD style='vertical-align: top;'>" << descr << " </TD>";
718 // s << "<div class=\"node\">\n";
721 // s << "<a name=\"Top\"></a>\n";
722 // s << "Top: <a rel=\"top\" accesskey=\"t\" href=\"#Top\">Top</a>\n";
723 // s << "Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>,
724 // s << "Up: <a rel="up" accesskey="u" href="#dir">(dir)</a>
728 //----------------------
731 //-------------------
732 // Computes output directory from filename to pass it to
733 // BlackBoxDescriptor::InsertHtmlHelp
736 std::string::size_type slash_position = filename.find_last_of("/\\");
739 if (slash_position != std::string::npos) {
740 if (slash_position == 0)
742 dir = filename.substr(0,slash_position);
745 for (i=mBlackBoxMap.begin();
746 i!=mBlackBoxMap.end();
749 i->second->InsertHtmlHelp(s,detail,level,dir,relative_link);
752 //----------------------
757 ptm = gmtime ( &rawtime );
760 s << "Automatically generated by <b>"<<caller<<"</b> "//from <b>"
763 << ptm->tm_mday << "/" << ptm->tm_mon << "/" << ptm->tm_year+1900
764 << " - " << ptm->tm_hour << ":" << ptm->tm_min << " GMT\n";
765 s << "</body></html>\n";
767 //----------------------
770 bbtkDebugDecTab("Kernel",9);
772 //==========================================================================