2 !!!202626.cpp!!! bbfy(in filename : std::string, in package : std::string = "PACKAGE_NAME", in output_path : std::string = "", in verbose : bool = false)
7 mNamespace = "bb" + mPackage;
9 mOutputPath = output_path;
13 !!!202754.cpp!!! CreateBlackBox() : void
14 // Parse XML input file
16 // Create output files
19 !!!202882.cpp!!! ParseXML() : void
20 XMLResults* res = new XMLResults;
21 XMLNode BB = XMLNode::parseFile(mFilename.c_str(),"blackbox",res);
23 if ( res->error != eXMLErrorNone )
25 std::ostringstream str;
26 str << XMLNode::getError(res->error);
27 str << " [line " << res->nLine << ", col "<<res->nColumn<<"]";
29 throw bbfyException(str.str());
34 if (!BB.isAttributeSet("name"))
36 throw bbfyException("Error : <blackbox> tag : no 'name' attribute found (mandatory)");
38 mName = BB.getAttribute("name");
40 if (mVerbose) std::cout << "* Creating BlackBox '"<<mName<<"'"<<std::endl;
47 if (BB.isAttributeSet("type"))
49 std::string bbtype = BB.getAttribute("type");
50 if (bbtype=="standard")
55 else if (bbtype==itkImageToImageFilterString)
57 mType = itkImageToImageFilter;
58 // Looks for <itkparent> tag
59 if (!BB.nChildNode("itkparent"))
61 throw bbfyException("Error : blackbox type '"+itkImageToImageFilterString+"' but no <itkparent> tag found (mandatory)");
63 bbtk::GetTextOrClear(BB.getChildNode("itkparent"),mItkParent);
66 if (BB.isAttributeSet("generic")) mGeneric=true;
68 else if (bbtype == vtkImageAlgorithmString)
70 mType = vtkImageAlgorithm;
71 // Looks for <vtkobject> tag
72 if (!BB.nChildNode("vtkobject"))
74 throw bbfyException("Error : blackbox type '"
75 +vtkImageAlgorithmString
76 +"' but no <vtkobject> tag found (mandatory)");
78 bbtk::GetTextOrClear(BB.getChildNode("vtkobject"),mVtkObject);
81 else if (bbtype == vtkPolyDataAlgorithmString )
83 mType = vtkPolyDataAlgorithm;
84 // Looks for <vtkobject> tag
85 if (!BB.nChildNode("vtkobject"))
87 throw bbfyException("Error : blackbox type '"
88 +vtkPolyDataAlgorithmString
89 +"' but no <vtkobject> tag found (mandatory)");
91 bbtk::GetTextOrClear(BB.getChildNode("vtkobject"),mVtkObject);
96 std::string mess("Error : blackbox type '");
98 mess += "' unknown. Known types :";
99 mess += "'" + itkImageToImageFilterString + "' ";
100 mess += "'" + vtkImageAlgorithmString + "' ";
101 mess += "'" + vtkPolyDataAlgorithmString + "' ";
102 throw bbfyException(mess);
107 if (BB.isAttributeSet("widget"))
110 mParentBlackBox = "bbtk::WxBlackBox";
111 mInclude.push_back("bbtkWxBlackBox.h");
116 mParentBlackBox = "bbtk::AtomicBlackBox";
117 mInclude.push_back("bbtkAtomicBlackBox.h");
122 for (i=0,j=0; i<BB.nChildNode("author"); i++)
125 bbtk::GetTextOrClear(BB.getChildNode("author",&j),val);
130 for (i=0,j=0; i<BB.nChildNode("description"); i++)
133 bbtk::GetTextOrClear(BB.getChildNode("description",&j),val);
138 for (i=0,j=0; i<BB.nChildNode("category"); i++)
141 bbtk::GetTextOrClear(BB.getChildNode("category",&j),val);
146 if (BB.nChildNode("namespace"))
148 bbtk::GetTextOrClear(BB.getChildNode("namespace"),mNamespace);
151 // UserSetDefaultValues body
152 if (BB.nChildNode("defaultValues"))
154 bbtk::GetTextOrClear(BB.getChildNode("defaultValues"),
155 mUserSetDefaultValues);
158 // UserInitializeProcessing body
159 if (BB.nChildNode("initializeProcessing"))
161 bbtk::GetTextOrClear(BB.getChildNode("initializeProcessing"),
162 mUserInitializeProcessing);
165 // UserFinalizeProcessing body
166 if (BB.nChildNode("finalizeProcessing"))
168 bbtk::GetTextOrClear(BB.getChildNode("finalizeProcessing"),
169 mUserFinalizeProcessing);
174 // Template parameters
175 // mNbTemplateParam = BB.nChildNode("template");
177 if ( BB.nChildNode("template") > 0)
179 mTemplateDeclaration = "<";
180 mTemplateImplementation = "<";
182 for (i=0,j=0; i<BB.nChildNode("template")-1; i++)
184 mTemplateDeclaration += "class ";
186 bbtk::GetTextOrClear(BB.getChildNode("template",&j),val);
187 mTemplateDeclaration += val;
188 mTemplateDeclaration += ",";
189 mTemplateImplementation += val;
190 mTemplateImplementation += ",";
191 mTemplateParam.push_back(val);
193 mTemplateDeclaration += "class ";
195 bbtk::GetTextOrClear(BB.getChildNode("template",&j),val);
196 mTemplateDeclaration += val;
197 mTemplateDeclaration += ">";
198 mTemplateImplementation += val;
199 mTemplateImplementation += ">";
200 mTemplateParam.push_back(val);
204 for (i=0,j=0; i<BB.nChildNode("include"); i++)
207 bbtk::GetTextOrClear(BB.getChildNode("include",&j),val);
208 mInclude.push_back(val);
211 for (i=0,j=0; i<BB.nChildNode("typedef"); i++)
214 bbtk::GetTextOrClear(BB.getChildNode("typedef",&j),val);
215 mTypedef.push_back(val);
219 for (i=0,j=0; i<BB.nChildNode("input"); i++)
222 XMLNode n = BB.getChildNode("input",&j);
223 if (!n.isAttributeSet("name"))
225 throw bbfyException("Error : <input> attribute 'name' not found (mandatory)");
227 io.name = n.getAttribute("name");
228 if (!n.isAttributeSet("type"))
230 throw bbfyException("Error : <input name=\""+io.name+"\"> attribute 'type' not found (mandatory)");
232 io.type = n.getAttribute("type");
233 if (!n.isAttributeSet("description"))
235 throw bbfyException("Error : <input name=\""+io.name+"\"> attribute 'description' not found (mandatory)");
237 io.descr = n.getAttribute("description");
239 if (n.isAttributeSet("special"))
241 io.special = n.getAttribute("special");
244 if (n.isAttributeSet("nature"))
246 io.nature = n.getAttribute("nature");
249 if (n.isAttributeSet("generic_type"))
251 io.generic_type = n.getAttribute("generic_type");
254 mInput.push_back(io);
258 for (i=0,j=0; i<BB.nChildNode("output"); i++)
261 XMLNode n = BB.getChildNode("output",&j);
262 if (!n.isAttributeSet("name"))
264 throw bbfyException("Error : <output> attribute 'name' not found (mandatory)");
266 io.name = n.getAttribute("name");
267 if (!n.isAttributeSet("type"))
269 throw bbfyException("Error : <output name=\""+io.name+"\"> attribute 'type' not found (mandatory)");
271 io.type = n.getAttribute("type");
272 if (!n.isAttributeSet("description"))
274 throw bbfyException("Error : <output name=\""+io.name+"\"> attribute 'description' not found (mandatory)");
276 io.descr = n.getAttribute("description");
278 if (n.isAttributeSet("special"))
280 io.special = n.getAttribute("special");
283 if (n.isAttributeSet("nature"))
285 io.nature = n.getAttribute("nature");
288 if (n.isAttributeSet("generic_type"))
290 io.generic_type = n.getAttribute("generic_type");
293 mOutput.push_back(io);
298 // process tag given ?
299 if (BB.nChildNode("process"))
301 bbtk::GetTextOrClear(BB.getChildNode("process"),mProcess);
305 // createwidget tag given ?
306 if (BB.nChildNode("createwidget"))
308 bbtk::GetTextOrClear(BB.getChildNode("createwidget"),mCreateWidget);
317 // OBSOLETE/UNSUPPORTED TAGS
318 // WARN IF OBSOLETE TAGS PROVIDED
319 if (BB.nChildNode("constructor"))
321 std::cout << "WARNING !!! The tag <constructor> is obsolete !!"<<std::endl;
323 if (BB.nChildNode("destructor"))
325 std::cout << "WARNING !!! The tag <destructor> is obsolete !!"<<std::endl;
327 if (BB.nChildNode("copy_constructor"))
329 std::cout << "WARNING !!! The tag <copy_constructor> is obsolete !!"<<std::endl;
334 !!!203010.cpp!!! CreateHeader() : void
340 if (mVerbose) std::cout << " - Creating header '"<<mHName<<"'"<<std::endl;
341 std::string fullname = mOutputPath + mHName;
342 mFile.open(fullname.c_str());
345 std::string mess("Error : could not open file \"");
346 mess += fullname + "\"";
347 throw bbfyException(mess);
353 mFile << "#ifdef _USE_WXWIDGETS_\n";
356 // Prevent multiple inclusions
357 std::string included("__bb");
358 included += mPackage + mName + "_h_INCLUDED__";
359 mFile << "#ifndef " << included <<"\n";
360 mFile << "#define " << included <<"\n";
363 mFile << "#include \"bb" << mPackage << "_EXPORT.h\"\n";
364 std::vector<std::string>::iterator i;
365 for (i=mInclude.begin(); i!=mInclude.end(); ++i)
367 mFile << "#include \"" << *i <<"\"\n";
369 if (mGeneric) mFile << "#include \"bbitkImage.h\"\n";
372 if (mType == itkImageToImageFilter )
374 mFile << "#include \"bbtkItkBlackBoxMacros.h\"\n";
376 else if ( (mType == vtkImageAlgorithm) ||
377 (mType == vtkPolyDataAlgorithm) )
379 mFile << "#include \"bbtkVtkBlackBoxMacros.h\"\n";
386 // If it is a template class
387 if (mTemplateParam.size() > 0)
389 mFile << "template " << mTemplateDeclaration <<"\n";
392 // Class declaration and parents
393 mFile << "class bb"<<mPackage<<"_EXPORT "<<mName<<"\n";
397 if (mBB.nChildNode("inherits"))
400 for (i=0,j=0; i<mBB.nChildNode("inherits")-1; i++)
403 << mBB.getChildNode("inherits",&j).getText()
407 << mBB.getChildNode("Inherits",&j).getText()
412 if (mType == itkImageToImageFilter )
414 mFile << " public " << mItkParent <<",\n";
417 mFile << " public "<<mParentBlackBox << "\n";
424 if (mType == itkImageToImageFilter)
426 mFile << " BBTK_ITK_BLACK_BOX_INTERFACE("
428 << mParentBlackBox << ","
433 else if ( (mType == vtkImageAlgorithm) ||
434 (mType == vtkPolyDataAlgorithm) )
436 mFile << " BBTK_VTK_BLACK_BOX_INTERFACE("
438 << mParentBlackBox << ","
446 mFile << " BBTK_BLACK_BOX_INTERFACE("
448 << mParentBlackBox << ");\n";
451 for (i=mTypedef.begin(); i!=mTypedef.end(); ++i)
459 std::vector<IO>::iterator ioi;
460 for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi)
462 if (ioi->special=="")
464 mFile << " BBTK_DECLARE_INPUT("
470 else if (ioi->special=="itk input")
472 mFile << " BBTK_DECLARE_ITK_INPUT("
478 else if (ioi->special=="vtk input")
480 if (mType == vtkImageAlgorithm) {
481 mFile << " BBTK_DECLARE_VTK_IMAGE_ALGORITHM_INPUT("
487 else if (mType == vtkPolyDataAlgorithm) {
488 mFile << " BBTK_DECLARE_POLY_DATA_ALGORITHM_INPUT("
495 else if (ioi->special=="itk parameter")
497 mFile << " BBTK_DECLARE_ITK_PARAM("
503 else if (ioi->special=="vtk parameter")
505 mFile << " BBTK_DECLARE_VTK_PARAM("
513 std::string mess("Error : input '");
515 mess += "', 'special' attribute '";
516 mess += ioi->special;
518 throw bbfyException(mess);
523 for (ioi=mOutput.begin(); ioi!=mOutput.end(); ++ioi)
525 if (ioi->special=="")
527 mFile << " BBTK_DECLARE_OUTPUT("
533 else if (ioi->special=="itk output")
535 mFile << " BBTK_DECLARE_ITK_OUTPUT("
541 else if (ioi->special=="vtk output")
543 mFile << " BBTK_DECLARE_VTK_OUTPUT("
551 std::string mess("Error : output '");
553 mess += "', 'special' attribute '";
554 mess += ioi->special;
556 throw bbfyException(mess);
561 if ((mType == STD)||(mProcess.size()))
563 mFile << " BBTK_PROCESS(Process);\n" ;
564 mFile << " void Process();\n";
566 else if (mType == itkImageToImageFilter)
568 mFile << " BBTK_ITK_PROCESS();\n" ;
570 else if ((mType == vtkImageAlgorithm) ||
571 (mType == vtkPolyDataAlgorithm) )
574 mFile << " BBTK_VTK_PROCESS();\n" ;
580 mFile << " BBTK_CREATE_WIDGET(CreateWidget);\n" ;
581 mFile << " void CreateWidget(wxWindow*);\n";
585 // EO black box declaration
588 // BO black box description
589 if (mTemplateParam.size()==0)
591 mFile << "BBTK_BEGIN_DESCRIBE_BLACK_BOX("
593 << mParentBlackBox << ");\n";
594 mFile << "BBTK_NAME(\"" << mName <<"\");\n";
596 else if (mTemplateParam.size()==1)
598 mFile << "BBTK_BEGIN_DESCRIBE_TEMPLATE_BLACK_BOX("
600 //<< mParentBlackBox //<< ","
601 // << mTemplateParam[0]
603 mFile << "BBTK_NAME(\"" << mName
604 << "<\"+bbtk::TypeName<" << mTemplateParam[0]
609 throw bbfyException("template bb with more than 1 templ param not impl");
613 mFile << "BBTK_AUTHOR(\""<<mAuthor<< "\");\n";
616 mFile << "BBTK_DESCRIPTION(\""<<mDescription<< "\");\n";
619 mFile << "BBTK_CATEGORY(\""<<mCategory<< "\");\n";
621 for (i=mTypedef.begin(); i!=mTypedef.end(); ++i)
627 for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi)
629 if (mTemplateParam.size()>0)
631 mFile << "BBTK_TEMPLATE_INPUT(";
635 mFile << "BBTK_INPUT(";
637 mFile << mName << "," << ioi->name << ",\""
638 << ioi->descr << "\"," << ioi->type << ",\""
639 << ioi->nature<<"\");\n";
643 for (ioi=mOutput.begin(); ioi!=mOutput.end(); ++ioi)
645 if (mTemplateParam.size()>0)
647 mFile << "BBTK_TEMPLATE_OUTPUT(";
651 mFile << "BBTK_OUTPUT(";
653 mFile << mName << "," << ioi->name << ",\""
654 << ioi->descr << "\"," << ioi->type << ",\""
655 << ioi->nature<<"\");\n";
658 // EO black box description
659 if (mTemplateParam.size()==0)
661 mFile << "BBTK_END_DESCRIBE_BLACK_BOX("
664 else if (mTemplateParam.size()==1)
666 mFile << "BBTK_END_DESCRIBE_TEMPLATE_BLACK_BOX("
668 // << mTemplateParam[0]
673 throw bbfyException("template bb with more than 1 templ param not impl");
677 // Untemplatization of itk filters
680 WriteGenericITKFilterHeader();
687 // Prevent multiple inclusions
688 mFile << "#endif // " << included <<"\n";
692 mFile << "#endif // _USE_WXWIDGETS_\n";
699 !!!203138.cpp!!! CreateCode() : void
701 mCxxName += mPackage;
704 if (mVerbose) std::cout << " - Creating code '"<<mCxxName<<"'"<<std::endl;
705 std::string fullname = mOutputPath + mCxxName;
706 mFile.open(fullname.c_str());
709 std::string mess("Error : could not open file \"");
712 throw bbfyException(mess);
716 // Header of the class
717 mFile << "#include \"" << mHName << "\"\n";
719 // Include Package header
720 mFile << "#include \"bb"<<mPackage << "Package.h\"\n";
727 if (mTemplateParam.size()>0)
730 mFile << "BBTK_BLACK_BOX_TEMPLATE_IMPLEMENTATION("
732 << mParentBlackBox << ");\n";
737 mFile << "BBTK_BLACK_BOX_IMPLEMENTATION("
738 << mName << "Generic,bbtk::AtomicBlackBox);\n";
740 mFile << "BBTK_ADD_BLACK_BOX_TO_PACKAGE("
742 << mName << "Generic)\n";
747 // Non template class
749 mFile << "BBTK_ADD_BLACK_BOX_TO_PACKAGE("
754 mFile << "BBTK_BLACK_BOX_IMPLEMENTATION("
756 << mParentBlackBox << ");\n";
759 if ((mType == STD)||(mProcess.size()))
761 mFile << "void "<<mName<<"::Process()\n{\n";
762 mFile << mProcess << "\n";
768 mFile << "void "<<mName<<"::CreateWidget(wxWindow* parent)\n{\n";
769 mFile << mCreateWidget << "\n";
774 // User Set Default Values
775 mFile <<"void "<<mName<<"::bbUserSetDefaultValues()"<<std::endl;
776 mFile << "{"<<std::endl;
777 if ( (mType == vtkImageAlgorithm) || (mType == vtkPolyDataAlgorithm) )
779 mFile << " BBTK_VTK_SET_DEFAULT_VALUES();\n";
781 mFile << mUserSetDefaultValues << std::endl;
782 mFile << "}" << std::endl;
784 // User Initialize Processing
785 mFile <<"void "<<mName<<"::bbUserInitializeProcessing()"
787 mFile << "{"<<std::endl;
788 if ( (mType == vtkImageAlgorithm) || (mType == vtkPolyDataAlgorithm) )
790 mFile << " BBTK_VTK_INITIALIZE_PROCESSING();\n";
792 mFile << mUserInitializeProcessing << std::endl;
793 mFile << "}" << std::endl;
795 // User Finalize Processing
796 mFile <<"void "<<mName<<"::bbUserFinalizeProcessing()"<<std::endl;
797 mFile << "{"<<std::endl;
798 if ( (mType == vtkImageAlgorithm) || (mType == vtkPolyDataAlgorithm) )
800 mFile << " BBTK_VTK_FINALIZE_PROCESSING();\n";
802 mFile << mUserFinalizeProcessing << std::endl;
803 mFile << "}" << std::endl;
814 !!!203266.cpp!!! WriteGenericITKFilterHeader() : void
815 mFile << "\n//===================================================\n";
816 mFile << "// Generic \"untemplatized\" filter\n";
817 mFile << "//===================================================\n";
819 // Class declaration and parents
820 mFile << "class /*BBTK_EXPORT*/ "<<mName<<"Generic\n";
822 mFile << " public bbtk::AtomicBlackBox\n";
826 mFile << " BBTK_BLACK_BOX_INTERFACE("
827 << mName << "Generic,bbtk::AtomicBlackBox);\n";
830 std::vector<IO>::iterator ioi;
831 for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi)
833 mFile << " BBTK_DECLARE_INPUT("
841 for (ioi=mOutput.begin(); ioi!=mOutput.end(); ++ioi)
843 mFile << " BBTK_DECLARE_OUTPUT("
851 mFile << " BBTK_PROCESS(ProcessSwitch);\n";
852 mFile << " private :\n";
853 mFile << " inline void ProcessSwitch();\n";
854 mFile << " template <class T, unsigned int D> void Process();\n";
855 // EO black box declaration
860 // BO black box description
861 mFile << "BBTK_BEGIN_DESCRIBE_BLACK_BOX("
862 << mName << "Generic,bbtk::AtomicBlackBox);\n";
863 mFile << "BBTK_NAME(\"" << mName <<"\");\n";
866 mFile << "BBTK_AUTHOR(\""<<mAuthor<< "\");\n";
869 mFile << "BBTK_DESCRIPTION(\""<<mDescription<< "\");\n";
872 for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi)
874 mFile << "BBTK_INPUT(";
875 mFile << mName << "Generic," << ioi->name << ",\""
876 << ioi->descr << "\"," << ioi->generic_type <<");\n";
880 for (ioi=mOutput.begin(); ioi!=mOutput.end(); ++ioi)
882 mFile << "BBTK_OUTPUT(";
883 mFile << mName << "Generic," << ioi->name << ",\""
884 << ioi->descr << "\"," << ioi->generic_type <<");\n";
887 // EO black box description
888 mFile << "BBTK_END_DESCRIBE_BLACK_BOX("
889 << mName << "Generic);\n";
892 //=================================================================
893 // ProcessSwitch implementation
894 mFile << "void "<< mName <<"Generic::ProcessSwitch()\n"
896 << "CALL_FOR_ALL_TYPES_AND_DIM(bbGetInputIn()->GetType(),\n"
897 << " bbGetInputIn()->GetDimension(),\n"
900 //=================================================================
903 //=================================================================
904 // Template process implementation
905 mFile << "template <class T, unsigned int D>\n"
906 << "void "<<mName<<"Generic::Process()\n"
908 << " bbtkDebugMessageInc(\"Kernel\",9,\n"
910 << "Generic::Process<\"<<TypeName<T>()<<\",\"<<D<<\">()\"<<std::endl);\n"
912 << " typedef itk::Image<T,D> ImageType;\n"
913 << " typedef "<<mName<<"<ImageType> FilterType;\n"
915 << " FilterType* f = new FilterType(\"Temp\");\n"
917 << " f->bbSetInputIn( this->bbGetInputIn()->GetImage<T,D>() );\n";
919 for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi)
921 if (ioi->name == "In") continue;
922 mFile << " f->bbSetInput"<<ioi->name<<" ( this->bbGetInput"
923 << ioi->name << "() );\n";
926 mFile << " f->bbUpdate();\n"
927 << " this->bbSetOutputOut( new itkImage( f->bbGetOutputOut() ) );\n"
928 << " f->UnRegister();\n"
929 << " bbtkDebugDecTab(\"Kernel\",9);\n"
931 //=================================================================
934 !!!203394.cpp!!! BeginNamespace() : void
935 // if (mIsInNamespace)
937 mFile << "namespace "<<mNamespace <<"\n{\n\n";
939 !!!203522.cpp!!! EndNamespace() : void
940 // if (mIsInNamespace)
942 mFile << "}\n// EO namespace "<<mNamespace<<"\n\n";