]> Creatis software - bbtk.git/blob - kernel/appli/bbfy/bbfy.cpp
*** empty log message ***
[bbtk.git] / kernel / appli / bbfy / bbfy.cpp
1 #ifdef WIN32
2 #define _CRT_SECURE_NO_DEPRECATE
3 #endif
4
5 #include <stdio.h>
6 #include "bbtkXML.h"
7 #include <iostream>
8 #include <fstream>
9 #include <sstream>
10 #include <vector>
11
12 //==========================================================================
13 class bbfyException
14 {
15 public: 
16   bbfyException(const std::string& message) : mMessage(message) {}
17
18   std::string mMessage;
19 };
20 //==========================================================================
21
22 const std::string itkImageToImageFilterString = "ITK_ImageToImageFilter";
23 const std::string vtkImageAlgorithmString     = "VTK_ImageAlgorithm";
24 const std::string vtkPolyDataAlgorithmString  = "VTK_PolyDataAlgorithm";
25
26 //==========================================================================
27 class bbfy
28 {
29 public:
30   bbfy(const std::string& filename, 
31        const std::string& package = "PACKAGE_NAME",
32        const std::string& output_path = "",
33        bool verbose = false);
34   
35   void CreateBlackBox();
36   void ParseXML();
37   void CreateHeader();
38   void CreateCode();
39
40   void WriteGenericITKFilterHeader();
41
42   void BeginNamespace();
43   void EndNamespace();
44
45 private:
46   //
47   std::string mFilename;
48   std::string mOutputPath;
49   bool mVerbose;
50
51   //
52   std::string mName;
53   typedef enum 
54     {
55       STD,
56       itkImageToImageFilter,
57       vtkImageAlgorithm,
58       vtkPolyDataAlgorithm,
59     }
60     BoxType;
61
62   BoxType mType;
63   bool mIsWidget;
64   std::string mParentBlackBox;
65   std::string mItkParent;
66   std::string mVtkObject;
67   bool mGeneric;
68   std::string mAuthor;
69   std::string mDescription;
70   std::string mCategory;
71   std::string mPackage;
72   // bool mIsInNamespace;
73   std::string mNamespace;
74   // int mNbTemplateParam;
75   std::vector<std::string> mTemplateParam;
76   std::string mTemplateDeclaration;
77   std::string mTemplateImplementation;
78
79   std::vector<std::string> mInclude;
80   std::vector<std::string> mTypedef;
81
82   std::string mUserSetDefaultValues;
83   std::string mUserInitializeProcessing;
84   std::string mUserFinalizeProcessing;
85
86   typedef struct
87   {
88     std::string name;
89     std::string type;
90     std::string nature;
91     std::string descr;
92     std::string special;
93     std::string generic_type;
94   }
95     IO;
96   
97   std::vector<IO> mInput;
98   std::vector<IO> mOutput;
99
100   std::string mProcess;
101   std::string mCreateWidget;
102
103   //
104   std::ofstream mFile;
105   std::string mHName;
106   std::string mCxxName;
107
108 };
109 //==========================================================================
110
111
112
113 //==========================================================================
114 bbfy::bbfy(const std::string& filename, 
115            const std::string& package,
116            const std::string& output_path,
117            bool verbose)
118 {
119   mIsWidget = false;
120   
121   mFilename = filename;
122   mPackage = package;
123   mNamespace = "bb" + mPackage;
124
125   mOutputPath = output_path;
126   mVerbose = verbose;
127
128   CreateBlackBox();
129 }
130 //==========================================================================
131
132 //==========================================================================
133 void bbfy::CreateBlackBox()
134 {
135   // Parse XML input file
136   ParseXML();
137   // Create output files
138   CreateHeader();
139   CreateCode();
140 }
141 //==========================================================================
142
143
144
145 //==========================================================================
146 void bbfy::ParseXML()
147 {
148   XMLResults* res = new XMLResults;
149   XMLNode BB = XMLNode::parseFile(mFilename.c_str(),"blackbox",res);
150
151   if ( res->error != eXMLErrorNone ) 
152     {
153       std::ostringstream str;
154       str << XMLNode::getError(res->error);
155       str << " [line " << res->nLine << ", col "<<res->nColumn<<"]"; 
156       delete res;
157       throw bbfyException(str.str());
158     }
159   delete res;
160
161   // Name
162   if (!BB.isAttributeSet("name")) 
163     {
164       throw bbfyException("Error : <blackbox> tag : no 'name' attribute found (mandatory)");
165     }
166   mName = BB.getAttribute("name");
167
168   if (mVerbose) std::cout << "* Creating BlackBox '"<<mName<<"'"<<std::endl;
169
170   // Type 
171   mGeneric = false;
172   mType = STD;
173
174
175   if (BB.isAttributeSet("type")) 
176     {
177       std::string bbtype = BB.getAttribute("type");
178       if (bbtype=="standard")
179         {
180           mGeneric = false;
181           mType = STD;
182         }
183       else if (bbtype==itkImageToImageFilterString)
184         {
185           mType = itkImageToImageFilter;
186           // Looks for <itkparent> tag
187           if (!BB.nChildNode("itkparent")) 
188             {
189               throw bbfyException("Error : blackbox type '"+itkImageToImageFilterString+"' but no <itkparent> tag found (mandatory)");
190             }
191           bbtk::GetTextOrClear(BB.getChildNode("itkparent"),mItkParent);
192           // 
193           mGeneric = false;
194           if (BB.isAttributeSet("generic")) mGeneric=true;
195         }
196       else if (bbtype == vtkImageAlgorithmString)
197         {
198           mType = vtkImageAlgorithm;
199           // Looks for <vtkobject> tag
200           if (!BB.nChildNode("vtkobject")) 
201             {
202               throw bbfyException("Error : blackbox type '"
203                                   +vtkImageAlgorithmString
204                                   +"' but no <vtkobject> tag found (mandatory)");
205             }
206           bbtk::GetTextOrClear(BB.getChildNode("vtkobject"),mVtkObject);
207           // 
208         }
209     else if (bbtype == vtkPolyDataAlgorithmString )
210         {
211           mType = vtkPolyDataAlgorithm;
212           // Looks for <vtkobject> tag
213           if (!BB.nChildNode("vtkobject")) 
214             {
215               throw bbfyException("Error : blackbox type '"
216                                   +vtkPolyDataAlgorithmString
217                                   +"' but no <vtkobject> tag found (mandatory)");
218             }
219           bbtk::GetTextOrClear(BB.getChildNode("vtkobject"),mVtkObject);
220           // 
221         }
222      else 
223         {
224           std::string mess("Error : blackbox type '");
225           mess += bbtype;
226           mess += "' unknown. Known types :";
227           mess += "'" + itkImageToImageFilterString + "' ";
228           mess += "'" + vtkImageAlgorithmString + "' ";
229           mess += "'" + vtkPolyDataAlgorithmString + "' ";
230           throw bbfyException(mess);
231         }
232     }
233
234   // Is a widget box ?
235   if (BB.isAttributeSet("widget")) 
236     {
237       mIsWidget = true;
238       mParentBlackBox = "bbtk::WxBlackBox";
239       mInclude.push_back("bbtkWxBlackBox.h");
240     }
241   else 
242     {
243       mIsWidget = false;
244       mParentBlackBox = "bbtk::AtomicBlackBox";
245       mInclude.push_back("bbtkAtomicBlackBox.h");
246     }
247
248   // Author
249   int i,j;
250   for (i=0,j=0; i<BB.nChildNode("author"); i++) 
251     {
252       std::string val;
253       bbtk::GetTextOrClear(BB.getChildNode("author",&j),val);
254       mAuthor += val;
255     }
256
257   // Description
258   for (i=0,j=0; i<BB.nChildNode("description"); i++) 
259     {
260       std::string val;
261       bbtk::GetTextOrClear(BB.getChildNode("description",&j),val);
262       mDescription += val;
263     }
264   
265   // Category
266   for (i=0,j=0; i<BB.nChildNode("category"); i++) 
267     {
268       std::string val;
269       bbtk::GetTextOrClear(BB.getChildNode("category",&j),val);
270       mCategory += val;
271     }
272
273   // Namespace
274   if (BB.nChildNode("namespace"))
275     {
276       bbtk::GetTextOrClear(BB.getChildNode("namespace"),mNamespace);
277     }
278
279   // UserSetDefaultValues body
280   if (BB.nChildNode("defaultValues"))
281     {
282       bbtk::GetTextOrClear(BB.getChildNode("defaultValues"),
283                            mUserSetDefaultValues);
284     }
285     
286   // UserInitializeProcessing body
287   if (BB.nChildNode("initializeProcessing"))
288     {
289       bbtk::GetTextOrClear(BB.getChildNode("initializeProcessing"),
290                            mUserInitializeProcessing);
291     }
292     
293  // UserFinalizeProcessing body
294   if (BB.nChildNode("finalizeProcessing"))
295     {
296       bbtk::GetTextOrClear(BB.getChildNode("finalizeProcessing"),
297                            mUserFinalizeProcessing);
298     }
299
300
301
302      // Template parameters
303   //  mNbTemplateParam = BB.nChildNode("template");
304
305   if ( BB.nChildNode("template") > 0)
306     {
307       mTemplateDeclaration = "<";
308       mTemplateImplementation = "<";
309       
310       for (i=0,j=0; i<BB.nChildNode("template")-1; i++) 
311         {
312           mTemplateDeclaration += "class ";
313           std::string val;
314           bbtk::GetTextOrClear(BB.getChildNode("template",&j),val);
315           mTemplateDeclaration += val;
316           mTemplateDeclaration +=  ",";
317           mTemplateImplementation += val;
318           mTemplateImplementation +=  ",";
319           mTemplateParam.push_back(val);
320         }
321       mTemplateDeclaration += "class ";
322       std::string val;
323       bbtk::GetTextOrClear(BB.getChildNode("template",&j),val);
324       mTemplateDeclaration += val;
325       mTemplateDeclaration +=  ">";
326       mTemplateImplementation += val;
327       mTemplateImplementation +=  ">";
328       mTemplateParam.push_back(val);
329     }
330
331   // Includes 
332   for (i=0,j=0; i<BB.nChildNode("include"); i++) 
333     {
334       std::string val;
335       bbtk::GetTextOrClear(BB.getChildNode("include",&j),val);
336       mInclude.push_back(val);
337     }
338   // Typedef
339   for (i=0,j=0; i<BB.nChildNode("typedef"); i++) 
340     {
341       std::string val;
342       bbtk::GetTextOrClear(BB.getChildNode("typedef",&j),val);
343       mTypedef.push_back(val);
344     }
345   
346   // Inputs
347   for (i=0,j=0; i<BB.nChildNode("input"); i++) 
348     {
349       IO io;
350       XMLNode n = BB.getChildNode("input",&j); 
351       if (!n.isAttributeSet("name"))
352         {
353           throw bbfyException("Error : <input> attribute 'name' not found (mandatory)");
354         }
355       io.name = n.getAttribute("name");
356       if (!n.isAttributeSet("type"))
357         {
358           throw bbfyException("Error : <input name=\""+io.name+"\"> attribute 'type' not found (mandatory)");
359         }
360       io.type = n.getAttribute("type"); 
361       if (!n.isAttributeSet("description"))
362         {
363           throw bbfyException("Error : <input name=\""+io.name+"\"> attribute 'description' not found (mandatory)");
364         }
365       io.descr = n.getAttribute("description"); 
366
367       if (n.isAttributeSet("special")) 
368         {
369           io.special =  n.getAttribute("special");  
370         }
371
372       if (n.isAttributeSet("nature")) 
373         {
374           io.nature =  n.getAttribute("nature");  
375         }
376
377       if (n.isAttributeSet("generic_type")) 
378         {
379           io.generic_type =  n.getAttribute("generic_type");  
380         }
381
382       mInput.push_back(io);
383     }
384   
385   // Outputs
386   for (i=0,j=0; i<BB.nChildNode("output"); i++) 
387     {
388       IO io;
389       XMLNode n = BB.getChildNode("output",&j); 
390       if (!n.isAttributeSet("name"))
391         {
392           throw bbfyException("Error : <output> attribute 'name' not found (mandatory)");
393         }
394       io.name = n.getAttribute("name"); 
395       if (!n.isAttributeSet("type"))
396         {
397           throw bbfyException("Error : <output name=\""+io.name+"\"> attribute 'type' not found (mandatory)");
398         }
399       io.type = n.getAttribute("type"); 
400       if (!n.isAttributeSet("description"))
401         {
402           throw bbfyException("Error : <output name=\""+io.name+"\"> attribute 'description' not found (mandatory)");
403         }
404       io.descr = n.getAttribute("description"); 
405
406       if (n.isAttributeSet("special")) 
407         {
408           io.special =  n.getAttribute("special");  
409         }
410
411       if (n.isAttributeSet("nature")) 
412         {
413           io.nature =  n.getAttribute("nature");  
414         }
415
416       if (n.isAttributeSet("generic_type")) 
417         {
418           io.generic_type =  n.getAttribute("generic_type");  
419         }
420
421       mOutput.push_back(io);
422     }
423
424
425   // Process
426   // process tag given ?
427    if (BB.nChildNode("process"))
428      {
429        bbtk::GetTextOrClear(BB.getChildNode("process"),mProcess);
430      }
431      
432   // CreateWidget
433   // createwidget tag given ?
434    if (BB.nChildNode("createwidget"))
435      {
436        bbtk::GetTextOrClear(BB.getChildNode("createwidget"),mCreateWidget);
437      }
438
439
440
441
442
443
444
445    // OBSOLETE/UNSUPPORTED TAGS
446   // WARN IF OBSOLETE TAGS PROVIDED
447   if (BB.nChildNode("constructor"))
448     {
449       std::cout << "WARNING !!! The tag <constructor> is obsolete !!"<<std::endl;
450     }
451   if (BB.nChildNode("destructor"))
452     {
453       std::cout << "WARNING !!! The tag <destructor> is obsolete !!"<<std::endl;
454     }
455   if (BB.nChildNode("copy_constructor"))
456     {
457       std::cout << "WARNING !!! The tag <copy_constructor> is obsolete !!"<<std::endl;
458     }
459
460
461
462 }
463 //==========================================================================
464
465
466 //==========================================================================
467 void bbfy::BeginNamespace()
468 {
469   //  if (mIsInNamespace)
470   // {
471   mFile << "namespace "<<mNamespace <<"\n{\n\n";
472   //  }
473 }
474 //==========================================================================
475
476 //==========================================================================
477 void bbfy::EndNamespace()
478 {
479   // if (mIsInNamespace)
480   //  {
481   mFile << "}\n// EO namespace "<<mNamespace<<"\n\n";
482   //  }
483 }
484 //==========================================================================
485
486
487 //==========================================================================
488 void bbfy::CreateHeader()
489 {
490
491   mHName = "bb";
492   mHName += mPackage;
493   mHName += mName;
494   mHName += ".h";
495   if (mVerbose) std::cout << " - Creating header '"<<mHName<<"'"<<std::endl;
496   std::string fullname = mOutputPath + mHName;
497   mFile.open(fullname.c_str());
498   if (!mFile.good())
499     {
500       std::string mess("Error : could not open file \"");
501       mess += fullname + "\"";
502       throw bbfyException(mess);
503     }
504   
505   // If is widget 
506   if (mIsWidget)
507     {
508       mFile << "#ifdef _USE_WXWIDGETS_\n";
509     }
510
511   // Prevent multiple inclusions
512   std::string included("__bb");
513   included += mPackage + mName + "_h_INCLUDED__";
514   mFile << "#ifndef " << included <<"\n";
515   mFile << "#define " << included <<"\n";
516
517   // Includes 
518   mFile << "#include \"bb" << mPackage << "_EXPORT.h\"\n";
519   std::vector<std::string>::iterator i;
520   for (i=mInclude.begin(); i!=mInclude.end(); ++i) 
521     {
522       mFile << "#include \"" << *i <<"\"\n";
523     }
524   if (mGeneric) mFile << "#include \"bbitkImage.h\"\n";
525   mFile << "\n";
526
527   if (mType == itkImageToImageFilter )
528     {
529       mFile << "#include \"bbtkItkBlackBoxMacros.h\"\n";
530     }
531   else if ( (mType == vtkImageAlgorithm) ||
532             (mType == vtkPolyDataAlgorithm) )
533     {
534       mFile << "#include \"bbtkVtkBlackBoxMacros.h\"\n";
535     }
536   // Namespace
537   BeginNamespace();
538
539   // Interface
540
541   // If it is a template class
542   if (mTemplateParam.size() > 0)
543     {
544       mFile << "template " << mTemplateDeclaration <<"\n";
545     }
546   
547   // Class declaration and parents
548   mFile << "class bb"<<mPackage<<"_EXPORT "<<mName<<"\n";
549   mFile << " : \n";
550
551   /*
552   if (mBB.nChildNode("inherits"))
553     {
554       mFile << ",\n";
555       for (i=0,j=0; i<mBB.nChildNode("inherits")-1; i++) 
556         {
557           mFile << "   public " 
558                 << mBB.getChildNode("inherits",&j).getText()
559                 << ",\n";
560         }
561       mFile << "   public " 
562             << mBB.getChildNode("Inherits",&j).getText()
563             <<"\n";
564     }
565   */
566
567   if (mType == itkImageToImageFilter )
568     {
569       mFile << "   public " << mItkParent <<",\n";
570     }
571
572   mFile << "   public "<<mParentBlackBox << "\n";
573
574   mFile << "{\n";
575
576   // Interface
577
578   // ITK 
579   if (mType == itkImageToImageFilter)
580     {
581       mFile << "  BBTK_ITK_BLACK_BOX_INTERFACE("
582             << mName << ","
583             << mParentBlackBox << ","
584             << mItkParent 
585             << ");\n";
586     }
587   // VTK
588   else if ( (mType == vtkImageAlgorithm) ||
589        (mType == vtkPolyDataAlgorithm) )
590     {
591       mFile << "  BBTK_VTK_BLACK_BOX_INTERFACE("
592             << mName << ","
593             << mParentBlackBox << ","
594             << mVtkObject
595             << ");\n";
596     }
597         
598   // Default
599   else 
600     {
601       mFile << "  BBTK_BLACK_BOX_INTERFACE("
602             << mName << ","
603             << mParentBlackBox << ");\n";
604     }
605
606   for (i=mTypedef.begin(); i!=mTypedef.end(); ++i) 
607     {
608       mFile << *i <<"\n";
609     }
610
611
612
613   // Inputs
614   std::vector<IO>::iterator ioi;
615   for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi) 
616     {
617       if (ioi->special=="") 
618         {
619           mFile << "  BBTK_DECLARE_INPUT(" 
620                 << ioi->name
621                 << ","
622                 << ioi->type
623                 << ");\n";
624         }
625       else if (ioi->special=="itk input")
626         {
627           mFile << "  BBTK_DECLARE_ITK_INPUT(" 
628                 << ioi->name
629                 << ","
630                 << ioi->type
631                 << ");\n";
632         }
633       else if (ioi->special=="vtk input")
634         {
635           if (mType == vtkImageAlgorithm) {
636           mFile << "  BBTK_DECLARE_VTK_IMAGE_ALGORITHM_INPUT(" 
637                 << ioi->name
638                 << ","
639                 << ioi->type
640                 << ");\n";
641           } 
642           else if (mType == vtkPolyDataAlgorithm) {
643           mFile << "  BBTK_DECLARE_POLY_DATA_ALGORITHM_INPUT(" 
644                 << ioi->name
645                 << ","
646                 << ioi->type
647                 << ");\n";
648           }
649         }
650       else if (ioi->special=="itk parameter")
651         {
652           mFile << "  BBTK_DECLARE_ITK_PARAM(" 
653                 << ioi->name
654                 << ","
655                 << ioi->type
656                 << ");\n";
657         }
658       else if (ioi->special=="vtk parameter")
659         {
660           mFile << "  BBTK_DECLARE_VTK_PARAM(" 
661                 << ioi->name
662                 << ","
663                 << ioi->type
664                 << ");\n";
665         }
666       else 
667         {
668           std::string mess("Error : input '");
669           mess += ioi->name;
670           mess += "', 'special' attribute '";
671           mess += ioi->special;
672           mess += "' unknown";
673           throw bbfyException(mess);
674         }
675     }
676   
677   // Outputs
678   for (ioi=mOutput.begin(); ioi!=mOutput.end(); ++ioi) 
679     {
680       if (ioi->special=="") 
681         {
682           mFile << "  BBTK_DECLARE_OUTPUT(" 
683                 << ioi->name
684                 << ","
685                 << ioi->type
686                 << ");\n";
687         }
688       else if (ioi->special=="itk output")
689         {
690           mFile << "  BBTK_DECLARE_ITK_OUTPUT(" 
691                 << ioi->name
692                 << ","
693                 << ioi->type
694                 << ");\n";
695         }  
696       else if (ioi->special=="vtk output")
697         {
698           mFile << "  BBTK_DECLARE_VTK_OUTPUT(" 
699                 << ioi->name
700                 << ","
701                 << ioi->type
702                 << ");\n";
703         }  
704       else 
705         {
706           std::string mess("Error : output '");
707           mess += ioi->name;
708           mess += "', 'special' attribute '";
709           mess += ioi->special;
710           mess += "' unknown";
711           throw bbfyException(mess);
712         }
713     }
714   
715   // Process
716   if ((mType == STD)||(mProcess.size()))
717     {
718       mFile << "  BBTK_PROCESS(Process);\n" ;
719       mFile << "  void Process();\n";
720     }
721   else if (mType == itkImageToImageFilter)
722     {   
723       mFile << "  BBTK_ITK_PROCESS();\n" ;
724     }
725   else if ((mType == vtkImageAlgorithm) ||
726            (mType == vtkPolyDataAlgorithm) )
727
728     {   
729       mFile << "  BBTK_VTK_PROCESS();\n" ;
730     }
731
732   // CreateWidget
733   if (mIsWidget) 
734     {
735        mFile << "  BBTK_CREATE_WIDGET(CreateWidget);\n" ;
736        mFile << "  void CreateWidget(wxWindow*);\n";
737     }
738
739
740   // EO black box declaration
741   mFile << "};\n\n";
742
743   // BO black box description
744   if (mTemplateParam.size()==0)
745     {
746       mFile << "BBTK_BEGIN_DESCRIBE_BLACK_BOX("
747             << mName << ","
748             << mParentBlackBox << ");\n";
749       mFile << "BBTK_NAME(\"" << mName <<"\");\n";
750     }
751   else if (mTemplateParam.size()==1)
752     {
753       mFile << "BBTK_BEGIN_DESCRIBE_TEMPLATE_BLACK_BOX("
754             << mName //<< ","
755         //<< mParentBlackBox //<< ","
756         //   << mTemplateParam[0] 
757             << ");\n";
758       mFile << "BBTK_NAME(\"" << mName 
759             << "<\"+bbtk::TypeName<" << mTemplateParam[0]
760             <<">()+\">\");\n";
761     }
762  else 
763     {
764       throw bbfyException("template bb with more than 1 templ param not impl");
765     } 
766   
767   // Author
768   mFile << "BBTK_AUTHOR(\""<<mAuthor<< "\");\n";
769
770   // Description
771   mFile << "BBTK_DESCRIPTION(\""<<mDescription<< "\");\n"; 
772   
773   // Category
774   mFile << "BBTK_CATEGORY(\""<<mCategory<< "\");\n"; 
775
776   for (i=mTypedef.begin(); i!=mTypedef.end(); ++i) 
777     {
778       mFile << *i <<"\n";
779     }
780   
781   // Inputs
782   for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi) 
783     {
784       if (mTemplateParam.size()>0)
785         {
786           mFile << "BBTK_TEMPLATE_INPUT(";
787         } 
788       else 
789         {
790           mFile << "BBTK_INPUT(";
791         } 
792       mFile << mName << "," << ioi->name << ",\""
793             << ioi->descr << "\"," <<  ioi->type << ",\"" 
794             << ioi->nature<<"\");\n";
795     }
796   
797   // Outputs
798   for (ioi=mOutput.begin(); ioi!=mOutput.end(); ++ioi) 
799     {
800       if (mTemplateParam.size()>0)
801         {
802           mFile << "BBTK_TEMPLATE_OUTPUT(";
803         } 
804       else 
805         {
806           mFile << "BBTK_OUTPUT(";
807         } 
808       mFile << mName << "," << ioi->name << ",\""
809             << ioi->descr << "\"," <<  ioi->type << ",\"" 
810             << ioi->nature<<"\");\n";
811     }
812   
813   // EO black box description
814   if (mTemplateParam.size()==0)
815     {
816       mFile << "BBTK_END_DESCRIBE_BLACK_BOX("
817             << mName << ");\n";
818     }
819   else if (mTemplateParam.size()==1)
820     {
821       mFile << "BBTK_END_DESCRIBE_TEMPLATE_BLACK_BOX("
822             << mName //<< ","
823         // << mTemplateParam[0] 
824             << ");\n";
825     }
826   else 
827     {
828       throw bbfyException("template bb with more than 1 templ param not impl");
829      
830     } 
831   
832   // Untemplatization of itk filters
833   if ( mGeneric )
834     {
835       WriteGenericITKFilterHeader();
836     }
837
838
839   // EO namespace
840   EndNamespace();
841   
842   // Prevent multiple inclusions
843   mFile << "#endif // " << included <<"\n";
844   // If is widget 
845   if (mIsWidget)
846     {
847       mFile << "#endif // _USE_WXWIDGETS_\n";
848     }
849
850   // EOF
851   mFile << "\n";
852
853   mFile.close();
854 }
855 //==========================================================================
856
857
858
859 //==========================================================================
860 void bbfy::WriteGenericITKFilterHeader()
861 {
862   mFile << "\n//===================================================\n";
863   mFile << "// Generic \"untemplatized\" filter\n";
864   mFile << "//===================================================\n";
865
866   // Class declaration and parents
867   mFile << "class /*BBTK_EXPORT*/ "<<mName<<"Generic\n";
868   mFile << " : \n";
869   mFile << "   public bbtk::AtomicBlackBox\n";
870   mFile << "{\n";
871
872   // Interface
873   mFile << "  BBTK_BLACK_BOX_INTERFACE("
874         << mName << "Generic,bbtk::AtomicBlackBox);\n";
875
876   // Inputs
877   std::vector<IO>::iterator ioi;
878   for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi) 
879     {
880       mFile << "  BBTK_DECLARE_INPUT(" 
881             << ioi->name
882             << ","
883             << ioi->generic_type
884             << ");\n";
885     }
886   
887   // Outputs
888   for (ioi=mOutput.begin(); ioi!=mOutput.end(); ++ioi) 
889     {
890       mFile << "  BBTK_DECLARE_OUTPUT(" 
891             << ioi->name
892             << ","
893             << ioi->generic_type
894             << ");\n";
895     }
896     
897   // Process
898   mFile << "  BBTK_PROCESS(ProcessSwitch);\n";
899   mFile << "  private :\n";
900   mFile << "    inline void ProcessSwitch();\n";
901   mFile << "    template <class T, unsigned int D> void Process();\n";
902   // EO black box declaration
903   mFile << "};\n\n";
904
905
906
907   // BO black box description
908   mFile << "BBTK_BEGIN_DESCRIBE_BLACK_BOX("
909         << mName << "Generic,bbtk::AtomicBlackBox);\n";
910   mFile << "BBTK_NAME(\"" << mName <<"\");\n";
911
912   // Author
913   mFile << "BBTK_AUTHOR(\""<<mAuthor<< "\");\n";
914
915   // Description
916   mFile << "BBTK_DESCRIPTION(\""<<mDescription<< "\");\n"; 
917   
918   // Inputs
919   for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi) 
920     {
921       mFile << "BBTK_INPUT(";
922       mFile << mName << "Generic," << ioi->name << ",\""
923             << ioi->descr << "\"," <<  ioi->generic_type <<");\n";
924     }
925   
926   // Outputs
927   for (ioi=mOutput.begin(); ioi!=mOutput.end(); ++ioi) 
928     {
929       mFile << "BBTK_OUTPUT(";
930       mFile << mName << "Generic," << ioi->name << ",\""
931             << ioi->descr << "\"," <<  ioi->generic_type <<");\n";
932     }
933   
934   // EO black box description
935   mFile << "BBTK_END_DESCRIBE_BLACK_BOX("
936         << mName << "Generic);\n";
937
938
939   //=================================================================
940   // ProcessSwitch implementation
941   mFile << "void "<< mName <<"Generic::ProcessSwitch()\n"
942         << "{\n"
943         << "CALL_FOR_ALL_TYPES_AND_DIM(bbGetInputIn()->GetType(),\n"
944         << "                           bbGetInputIn()->GetDimension(),\n"
945         << "                           Process);\n"
946         << "}\n";
947   //=================================================================
948
949
950   //=================================================================
951   // Template process implementation
952   mFile << "template <class T, unsigned int D>\n"
953         << "void "<<mName<<"Generic::Process()\n"
954         << "{\n"
955         << "  bbtkDebugMessageInc(\"Kernel\",9,\n"
956         << "      \""<<mName 
957         << "Generic::Process<\"<<TypeName<T>()<<\",\"<<D<<\">()\"<<std::endl);\n"
958     
959         << "  typedef itk::Image<T,D> ImageType;\n"
960         << "  typedef "<<mName<<"<ImageType> FilterType;\n"
961     
962         << "  FilterType* f = new FilterType(\"Temp\");\n"
963     
964         << "  f->bbSetInputIn( this->bbGetInputIn()->GetImage<T,D>() );\n";
965   
966   for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi) 
967     {
968       if (ioi->name == "In") continue;
969       mFile << "  f->bbSetInput"<<ioi->name<<" ( this->bbGetInput" 
970             << ioi->name << "() );\n";
971     }
972   
973   mFile << "  f->bbUpdate();\n"
974         << "  this->bbSetOutputOut( new itkImage( f->bbGetOutputOut() ) );\n"
975         << "  f->UnRegister();\n"
976         << "  bbtkDebugDecTab(\"Kernel\",9);\n"
977         << "}\n\n";
978   //=================================================================
979
980
981 }
982 //==========================================================================
983
984
985 //==========================================================================
986 void bbfy::CreateCode()
987 {
988   mCxxName = "bb";
989   mCxxName += mPackage;
990   mCxxName += mName;
991   mCxxName += ".cxx";
992   if (mVerbose) std::cout << " - Creating code   '"<<mCxxName<<"'"<<std::endl;
993   std::string fullname = mOutputPath + mCxxName;
994   mFile.open(fullname.c_str());
995   if (!mFile.good()) 
996     {
997       std::string mess("Error : could not open file \"");
998       mess += fullname;
999       mess += "\"";
1000       throw bbfyException(mess);
1001     }
1002   
1003   // Includes
1004   // Header of the class
1005   mFile << "#include \"" << mHName << "\"\n";
1006
1007   // Include Package header
1008   mFile << "#include \"bb"<<mPackage << "Package.h\"\n";
1009
1010   // BO namespace
1011   BeginNamespace();
1012  
1013   
1014   // Template class ?
1015   if (mTemplateParam.size()>0) 
1016     {
1017       // Implementation
1018       mFile << "BBTK_BLACK_BOX_TEMPLATE_IMPLEMENTATION("
1019             << mName << ","  
1020             << mParentBlackBox << ");\n";
1021      
1022       if (mGeneric) 
1023         {       
1024           // Implementation
1025           mFile << "BBTK_BLACK_BOX_IMPLEMENTATION("
1026                 << mName << "Generic,bbtk::AtomicBlackBox);\n";
1027           // Package
1028           mFile << "BBTK_ADD_BLACK_BOX_TO_PACKAGE("
1029                 << mPackage << ","
1030                 << mName << "Generic)\n";
1031         }
1032     }
1033   else 
1034     {
1035       // Non template class
1036       // Package
1037       mFile << "BBTK_ADD_BLACK_BOX_TO_PACKAGE("
1038             << mPackage << ","
1039             << mName << ")\n";
1040
1041       // Implementation
1042       mFile << "BBTK_BLACK_BOX_IMPLEMENTATION("
1043             << mName << ","  
1044             << mParentBlackBox << ");\n"; 
1045     }
1046   // Process
1047   if ((mType == STD)||(mProcess.size()))
1048     {
1049       mFile << "void "<<mName<<"::Process()\n{\n";
1050       mFile << mProcess << "\n";
1051       mFile << "}\n";
1052     }
1053   // CreateWidget
1054   if (mIsWidget)
1055     {
1056       mFile << "void "<<mName<<"::CreateWidget(wxWindow* parent)\n{\n";
1057       mFile << mCreateWidget << "\n";
1058       mFile << "}\n";
1059     }
1060
1061                 
1062   // User Set Default Values  
1063   mFile <<"void "<<mName<<"::bbUserSetDefaultValues()"<<std::endl;
1064   mFile << "{"<<std::endl;  
1065         if ( (mType == vtkImageAlgorithm) || (mType == vtkPolyDataAlgorithm) )
1066         {
1067                 mFile << "   BBTK_VTK_SET_DEFAULT_VALUES();\n";
1068         }
1069         mFile << mUserSetDefaultValues << std::endl;
1070   mFile << "}" << std::endl;
1071
1072   // User Initialize Processing
1073   mFile <<"void "<<mName<<"::bbUserInitializeProcessing()"
1074         <<std::endl;
1075   mFile << "{"<<std::endl;
1076         if ( (mType == vtkImageAlgorithm) || (mType == vtkPolyDataAlgorithm) )
1077         {
1078                 mFile <<  "  BBTK_VTK_INITIALIZE_PROCESSING();\n";
1079         }
1080         mFile << mUserInitializeProcessing << std::endl;
1081   mFile << "}" << std::endl;
1082
1083         // User Finalize Processing
1084   mFile <<"void "<<mName<<"::bbUserFinalizeProcessing()"<<std::endl;
1085   mFile << "{"<<std::endl;
1086         if ( (mType == vtkImageAlgorithm) || (mType == vtkPolyDataAlgorithm) )
1087         {
1088                 mFile << "   BBTK_VTK_FINALIZE_PROCESSING();\n";
1089         }
1090         mFile << mUserFinalizeProcessing << std::endl;
1091   mFile << "}" << std::endl;
1092
1093
1094   // EO namespace
1095   EndNamespace();
1096
1097   mFile << "\n";
1098   
1099   // EOF
1100   mFile.close();
1101   
1102 }
1103 //==========================================================================
1104  
1105
1106
1107
1108
1109 //==========================================================================
1110 int main(int argc, char **argv)
1111 {
1112   
1113   if (argc<2 || argc>5) 
1114     {
1115       std::cerr << "usage : "<< argv[0] <<" xml_file [package_name] [output_path] [-q]" << std::endl;
1116       return 1;
1117     }
1118
1119   try 
1120     {
1121       std::string package("PACKAGE_NAME");
1122       std::string output_path("");
1123       bool verbose = true;
1124       if (argc>2) package = argv[2];
1125       if (argc>3) output_path = argv[3];
1126       if (argc>4) verbose = false;
1127       
1128       bbfy B(argv[1],package,output_path,verbose);
1129     }
1130   catch (bbfyException e)
1131     {
1132       std::cerr << argv[0] << "  " << argv[1] << std::endl
1133                 << e.mMessage << std::endl;
1134       return 1;
1135     }
1136   return 0;
1137 }
1138 //==========================================================================
1139
1140