]> Creatis software - bbtk.git/blob - kernel/appli/bbfy/bbfy.cpp
Some comments updated
[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 mVtkParent;
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 mUserConstructor;
83   std::string mUserCopyConstructor;
84   std::string mUserDestructor;
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 <vtkparent> tag
200           if (!BB.nChildNode("vtkparent")) 
201             {
202               throw bbfyException("Error : blackbox type '"
203                                   +vtkImageAlgorithmString
204                                   +"' but no <vtkparent> tag found (mandatory)");
205             }
206           bbtk::GetTextOrClear(BB.getChildNode("vtkparent"),mVtkParent);
207           // 
208         }
209     else if (bbtype == vtkPolyDataAlgorithmString )
210         {
211           mType = vtkPolyDataAlgorithm;
212           // Looks for <vtkparent> tag
213           if (!BB.nChildNode("vtkparent")) 
214             {
215               throw bbfyException("Error : blackbox type '"
216                                   +vtkPolyDataAlgorithmString
217                                   +"' but no <vtkparent> tag found (mandatory)");
218             }
219           bbtk::GetTextOrClear(BB.getChildNode("vtkparent"),mVtkParent);
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   // UserConstructor body
280   if (BB.nChildNode("constructor"))
281     {
282       bbtk::GetTextOrClear(BB.getChildNode("constructor"),mUserConstructor);
283     }
284   // UserCopyConstructor body
285   if (BB.nChildNode("copyconstructor"))
286     {
287       bbtk::GetTextOrClear(BB.getChildNode("copyconstructor"),mUserCopyConstructor);
288     }
289   // UserDestructor body
290   if (BB.nChildNode("destructor"))
291     {
292       bbtk::GetTextOrClear(BB.getChildNode("destructor"),mUserDestructor);
293     }
294  
295     // Template parameters
296   //  mNbTemplateParam = BB.nChildNode("template");
297
298   if ( BB.nChildNode("template") > 0)
299     {
300       mTemplateDeclaration = "<";
301       mTemplateImplementation = "<";
302       
303       for (i=0,j=0; i<BB.nChildNode("template")-1; i++) 
304         {
305           mTemplateDeclaration += "class ";
306           std::string val;
307           bbtk::GetTextOrClear(BB.getChildNode("template",&j),val);
308           mTemplateDeclaration += val;
309           mTemplateDeclaration +=  ",";
310           mTemplateImplementation += val;
311           mTemplateImplementation +=  ",";
312           mTemplateParam.push_back(val);
313         }
314       mTemplateDeclaration += "class ";
315       std::string val;
316       bbtk::GetTextOrClear(BB.getChildNode("template",&j),val);
317       mTemplateDeclaration += val;
318       mTemplateDeclaration +=  ">";
319       mTemplateImplementation += val;
320       mTemplateImplementation +=  ">";
321       mTemplateParam.push_back(val);
322     }
323
324   // Includes 
325   for (i=0,j=0; i<BB.nChildNode("include"); i++) 
326     {
327       std::string val;
328       bbtk::GetTextOrClear(BB.getChildNode("include",&j),val);
329       mInclude.push_back(val);
330     }
331   // Typedef
332   for (i=0,j=0; i<BB.nChildNode("typedef"); i++) 
333     {
334       std::string val;
335       bbtk::GetTextOrClear(BB.getChildNode("typedef",&j),val);
336       mTypedef.push_back(val);
337     }
338   
339   // Inputs
340   for (i=0,j=0; i<BB.nChildNode("input"); i++) 
341     {
342       IO io;
343       XMLNode n = BB.getChildNode("input",&j); 
344       if (!n.isAttributeSet("name"))
345         {
346           throw bbfyException("Error : <input> attribute 'name' not found (mandatory)");
347         }
348       io.name = n.getAttribute("name");
349       if (!n.isAttributeSet("type"))
350         {
351           throw bbfyException("Error : <input name=\""+io.name+"\"> attribute 'type' not found (mandatory)");
352         }
353       io.type = n.getAttribute("type"); 
354       if (!n.isAttributeSet("description"))
355         {
356           throw bbfyException("Error : <input name=\""+io.name+"\"> attribute 'description' not found (mandatory)");
357         }
358       io.descr = n.getAttribute("description"); 
359
360       if (n.isAttributeSet("special")) 
361         {
362           io.special =  n.getAttribute("special");  
363         }
364
365       if (n.isAttributeSet("nature")) 
366         {
367           io.nature =  n.getAttribute("nature");  
368         }
369
370       if (n.isAttributeSet("generic_type")) 
371         {
372           io.generic_type =  n.getAttribute("generic_type");  
373         }
374
375       mInput.push_back(io);
376     }
377   
378   // Outputs
379   for (i=0,j=0; i<BB.nChildNode("output"); i++) 
380     {
381       IO io;
382       XMLNode n = BB.getChildNode("output",&j); 
383       if (!n.isAttributeSet("name"))
384         {
385           throw bbfyException("Error : <output> attribute 'name' not found (mandatory)");
386         }
387       io.name = n.getAttribute("name"); 
388       if (!n.isAttributeSet("type"))
389         {
390           throw bbfyException("Error : <output name=\""+io.name+"\"> attribute 'type' not found (mandatory)");
391         }
392       io.type = n.getAttribute("type"); 
393       if (!n.isAttributeSet("description"))
394         {
395           throw bbfyException("Error : <output name=\""+io.name+"\"> attribute 'description' not found (mandatory)");
396         }
397       io.descr = n.getAttribute("description"); 
398
399       if (n.isAttributeSet("special")) 
400         {
401           io.special =  n.getAttribute("special");  
402         }
403
404       if (n.isAttributeSet("nature")) 
405         {
406           io.nature =  n.getAttribute("nature");  
407         }
408
409       if (n.isAttributeSet("generic_type")) 
410         {
411           io.generic_type =  n.getAttribute("generic_type");  
412         }
413
414       mOutput.push_back(io);
415     }
416
417
418   // Process
419   // process tag given ?
420    if (BB.nChildNode("process"))
421      {
422        bbtk::GetTextOrClear(BB.getChildNode("process"),mProcess);
423      }
424   // CreateWidget
425   // createwidget tag given ?
426    if (BB.nChildNode("createwidget"))
427      {
428        bbtk::GetTextOrClear(BB.getChildNode("createwidget"),mCreateWidget);
429      }
430 }
431 //==========================================================================
432
433
434 //==========================================================================
435 void bbfy::BeginNamespace()
436 {
437   //  if (mIsInNamespace)
438   // {
439   mFile << "namespace "<<mNamespace <<"\n{\n\n";
440   //  }
441 }
442 //==========================================================================
443
444 //==========================================================================
445 void bbfy::EndNamespace()
446 {
447   // if (mIsInNamespace)
448   //  {
449   mFile << "}\n// EO namespace "<<mNamespace<<"\n\n";
450   //  }
451 }
452 //==========================================================================
453
454
455 //==========================================================================
456 void bbfy::CreateHeader()
457 {
458
459   mHName = "bb";
460   mHName += mPackage;
461   mHName += mName;
462   mHName += ".h";
463   if (mVerbose) std::cout << " - Creating header '"<<mHName<<"'"<<std::endl;
464   std::string fullname = mOutputPath + mHName;
465   mFile.open(fullname.c_str());
466   if (!mFile.good())
467     {
468       std::string mess("Error : could not open file \"");
469       mess += fullname + "\"";
470       throw bbfyException(mess);
471     }
472   
473   // If is widget 
474   if (mIsWidget)
475     {
476       mFile << "#ifdef _USE_WXWIDGETS_\n";
477     }
478
479   // Prevent multiple inclusions
480   std::string included("__bb");
481   included += mPackage + mName + "_h_INCLUDED__";
482   mFile << "#ifndef " << included <<"\n";
483   mFile << "#define " << included <<"\n";
484
485   // Includes 
486   //  mFile << "#include \"bbtkAtomicBlackBox.h\"\n";
487   std::vector<std::string>::iterator i;
488   for (i=mInclude.begin(); i!=mInclude.end(); ++i) 
489     {
490       mFile << "#include \"" << *i <<"\"\n";
491     }
492   if (mGeneric) mFile << "#include \"bbitkImage.h\"\n";
493   mFile << "\n";
494
495   if (mType == itkImageToImageFilter )
496     {
497       mFile << "#include \"bbtkItkBlackBoxMacros.h\"\n";
498     }
499   else if ( (mType == vtkImageAlgorithm) ||
500             (mType == vtkPolyDataAlgorithm) )
501     {
502       mFile << "#include \"bbtkVtkBlackBoxMacros.h\"\n";
503     }
504   // Namespace
505   BeginNamespace();
506
507   // Interface
508
509   // If it is a template class
510   if (mTemplateParam.size() > 0)
511     {
512       mFile << "template " << mTemplateDeclaration <<"\n";
513     }
514   
515   // Class declaration and parents
516   mFile << "class /*BBTK_EXPORT*/ "<<mName<<"\n";
517   mFile << " : \n";
518
519   /*
520   if (mBB.nChildNode("inherits"))
521     {
522       mFile << ",\n";
523       for (i=0,j=0; i<mBB.nChildNode("inherits")-1; i++) 
524         {
525           mFile << "   public " 
526                 << mBB.getChildNode("inherits",&j).getText()
527                 << ",\n";
528         }
529       mFile << "   public " 
530             << mBB.getChildNode("Inherits",&j).getText()
531             <<"\n";
532     }
533   */
534
535   if (mType == itkImageToImageFilter )
536     {
537       mFile << "   public " << mItkParent <<",\n";
538     }
539   else if ( (mType == vtkImageAlgorithm) ||
540             (mType == vtkPolyDataAlgorithm) )
541     {
542       mFile << "   public " << mVtkParent <<",\n";
543     }
544
545   mFile << "   public "<<mParentBlackBox << "\n";
546
547   mFile << "{\n";
548
549   // Interface
550
551   // ITK 
552   if (mType == itkImageToImageFilter)
553     {
554       mFile << "  BBTK_ITK_BLACK_BOX_INTERFACE("
555             << mName << ","
556             << mParentBlackBox << ","
557             << mItkParent 
558             << ");\n";
559     }
560   // VTK
561   else if ( (mType == vtkImageAlgorithm) ||
562        (mType == vtkPolyDataAlgorithm) )
563     {
564       mFile << "  BBTK_VTK_BLACK_BOX_INTERFACE("
565             << mName << ","
566             << mParentBlackBox << ","
567             << mVtkParent 
568             << ");\n";
569     }
570   // Default
571   else 
572     {
573       mFile << "  BBTK_BLACK_BOX_INTERFACE("
574             << mName << ","
575             << mParentBlackBox << ");\n";
576     }
577
578   for (i=mTypedef.begin(); i!=mTypedef.end(); ++i) 
579     {
580       mFile << *i <<"\n";
581     }
582
583   // Declare user constructor / copy cons /destr 
584   mFile << "//=================================================================="<<std::endl;
585   mFile << "/// User callback called in the box contructor"<<std::endl;
586
587   mFile << "virtual void bbUserConstructor();"<<std::endl;
588   mFile << "/// User callback called in the box copy constructor"<<std::endl;
589   mFile << "virtual void bbUserCopyConstructor();"<<std::endl;
590   mFile << "/// User callback called in the box destructor"<<std::endl;
591   mFile << "virtual void bbUserDestructor();"<<std::endl;
592   mFile << "//=================================================================="<<std::endl; 
593
594
595
596   // Inputs
597   std::vector<IO>::iterator ioi;
598   for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi) 
599     {
600       if (ioi->special=="") 
601         {
602           mFile << "  BBTK_DECLARE_INPUT(" 
603                 << ioi->name
604                 << ","
605                 << ioi->type
606                 << ");\n";
607         }
608       else if (ioi->special=="itk input")
609         {
610           mFile << "  BBTK_DECLARE_ITK_INPUT(" 
611                 << ioi->name
612                 << ","
613                 << ioi->type
614                 << ");\n";
615         }
616       else if (ioi->special=="vtk input")
617         {
618           if (mType == vtkImageAlgorithm) {
619           mFile << "  BBTK_DECLARE_VTK_IMAGE_ALGORITHM_INPUT(" 
620                 << ioi->name
621                 << ","
622                 << ioi->type
623                 << ");\n";
624           } 
625           else if (mType == vtkPolyDataAlgorithm) {
626           mFile << "  BBTK_DECLARE_POLY_DATA_ALGORITHM_INPUT(" 
627                 << ioi->name
628                 << ","
629                 << ioi->type
630                 << ");\n";
631           }
632         }
633       else if (ioi->special=="itk parameter")
634         {
635           mFile << "  BBTK_DECLARE_ITK_PARAM(" 
636                 << ioi->name
637                 << ","
638                 << ioi->type
639                 << ");\n";
640         }
641       else if (ioi->special=="vtk parameter")
642         {
643           mFile << "  BBTK_DECLARE_VTK_PARAM(" 
644                 << ioi->name
645                 << ","
646                 << ioi->type
647                 << ");\n";
648         }
649       else 
650         {
651           std::string mess("Error : input '");
652           mess += ioi->name;
653           mess += "', 'special' attribute '";
654           mess += ioi->special;
655           mess += "' unknown";
656           throw bbfyException(mess);
657         }
658     }
659   
660   // Outputs
661   for (ioi=mOutput.begin(); ioi!=mOutput.end(); ++ioi) 
662     {
663       if (ioi->special=="") 
664         {
665           mFile << "  BBTK_DECLARE_OUTPUT(" 
666                 << ioi->name
667                 << ","
668                 << ioi->type
669                 << ");\n";
670         }
671       else if (ioi->special=="itk output")
672         {
673           mFile << "  BBTK_DECLARE_ITK_OUTPUT(" 
674                 << ioi->name
675                 << ","
676                 << ioi->type
677                 << ");\n";
678         }  
679       else if (ioi->special=="vtk output")
680         {
681           mFile << "  BBTK_DECLARE_VTK_OUTPUT(" 
682                 << ioi->name
683                 << ","
684                 << ioi->type
685                 << ");\n";
686         }  
687       else 
688         {
689           std::string mess("Error : output '");
690           mess += ioi->name;
691           mess += "', 'special' attribute '";
692           mess += ioi->special;
693           mess += "' unknown";
694           throw bbfyException(mess);
695         }
696     }
697   
698   // Process
699   if ((mType == STD)||(mProcess.size()))
700     {
701       mFile << "  BBTK_PROCESS(Process);\n" ;
702       mFile << "  void Process();\n";
703     }
704   else if (mType == itkImageToImageFilter)
705     {   
706       mFile << "  BBTK_ITK_PROCESS();\n" ;
707     }
708   else if ((mType == vtkImageAlgorithm) ||
709            (mType == vtkPolyDataAlgorithm) )
710
711     {   
712       mFile << "  BBTK_VTK_PROCESS();\n" ;
713     }
714
715   // CreateWidget
716   if (mIsWidget) 
717     {
718        mFile << "  BBTK_CREATE_WIDGET(CreateWidget);\n" ;
719        mFile << "  void CreateWidget();\n";
720     }
721
722
723   // EO black box declaration
724   mFile << "};\n\n";
725
726   // BO black box description
727   if (mTemplateParam.size()==0)
728     {
729       mFile << "BBTK_BEGIN_DESCRIBE_BLACK_BOX("
730             << mName << ","
731             << mParentBlackBox << ");\n";
732       mFile << "BBTK_NAME(\"" << mName <<"\");\n";
733     }
734   else if (mTemplateParam.size()==1)
735     {
736       mFile << "BBTK_BEGIN_DESCRIBE_TEMPLATE_BLACK_BOX("
737             << mName //<< ","
738         //<< mParentBlackBox //<< ","
739         //   << mTemplateParam[0] 
740             << ");\n";
741       mFile << "BBTK_NAME(\"" << mName 
742             << "<\"+bbtk::TypeName<" << mTemplateParam[0]
743             <<">()+\">\");\n";
744     }
745  else 
746     {
747       throw bbfyException("template bb with more than 1 templ param not impl");
748     } 
749   
750   // Author
751   mFile << "BBTK_AUTHOR(\""<<mAuthor<< "\");\n";
752
753   // Description
754   mFile << "BBTK_DESCRIPTION(\""<<mDescription<< "\");\n"; 
755   
756   // Category
757   mFile << "BBTK_CATEGORY(\""<<mCategory<< "\");\n"; 
758
759   for (i=mTypedef.begin(); i!=mTypedef.end(); ++i) 
760     {
761       mFile << *i <<"\n";
762     }
763   
764   // Inputs
765   for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi) 
766     {
767       if (mTemplateParam.size()>0)
768         {
769           mFile << "BBTK_TEMPLATE_INPUT(";
770         } 
771       else 
772         {
773           mFile << "BBTK_INPUT(";
774         } 
775       mFile << mName << "," << ioi->name << ",\""
776             << ioi->descr << "\"," <<  ioi->type << ",\"" 
777             << ioi->nature<<"\");\n";
778     }
779   
780   // Outputs
781   for (ioi=mOutput.begin(); ioi!=mOutput.end(); ++ioi) 
782     {
783       if (mTemplateParam.size()>0)
784         {
785           mFile << "BBTK_TEMPLATE_OUTPUT(";
786         } 
787       else 
788         {
789           mFile << "BBTK_OUTPUT(";
790         } 
791       mFile << mName << "," << ioi->name << ",\""
792             << ioi->descr << "\"," <<  ioi->type << ",\"" 
793             << ioi->nature<<"\");\n";
794     }
795   
796   // EO black box description
797   if (mTemplateParam.size()==0)
798     {
799       mFile << "BBTK_END_DESCRIBE_BLACK_BOX("
800             << mName << ");\n";
801     }
802   else if (mTemplateParam.size()==1)
803     {
804       mFile << "BBTK_END_DESCRIBE_TEMPLATE_BLACK_BOX("
805             << mName //<< ","
806         // << mTemplateParam[0] 
807             << ");\n";
808     }
809   else 
810     {
811       throw bbfyException("template bb with more than 1 templ param not impl");
812      
813     } 
814   
815   // Untemplatization of itk filters
816   if ( mGeneric )
817     {
818       WriteGenericITKFilterHeader();
819     }
820
821
822   // EO namespace
823   EndNamespace();
824   
825   // Prevent multiple inclusions
826   mFile << "#endif // " << included <<"\n";
827   // If is widget 
828   if (mIsWidget)
829     {
830       mFile << "#endif // _USE_WXWIDGETS_\n";
831     }
832
833   // EOF
834   mFile << "\n";
835
836   mFile.close();
837 }
838 //==========================================================================
839
840
841
842 //==========================================================================
843 void bbfy::WriteGenericITKFilterHeader()
844 {
845   mFile << "\n//===================================================\n";
846   mFile << "// Generic \"untemplatized\" filter\n";
847   mFile << "//===================================================\n";
848
849   // Class declaration and parents
850   mFile << "class /*BBTK_EXPORT*/ "<<mName<<"Generic\n";
851   mFile << " : \n";
852   mFile << "   public bbtk::AtomicBlackBox\n";
853   mFile << "{\n";
854
855   // Interface
856   mFile << "  BBTK_BLACK_BOX_INTERFACE("
857         << mName << "Generic,bbtk::AtomicBlackBox);\n";
858
859   // Inputs
860   std::vector<IO>::iterator ioi;
861   for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi) 
862     {
863       mFile << "  BBTK_DECLARE_INPUT(" 
864             << ioi->name
865             << ","
866             << ioi->generic_type
867             << ");\n";
868     }
869   
870   // Outputs
871   for (ioi=mOutput.begin(); ioi!=mOutput.end(); ++ioi) 
872     {
873       mFile << "  BBTK_DECLARE_OUTPUT(" 
874             << ioi->name
875             << ","
876             << ioi->generic_type
877             << ");\n";
878     }
879     
880   // Process
881   mFile << "  BBTK_PROCESS(ProcessSwitch);\n";
882   mFile << "  private :\n";
883   mFile << "    inline void ProcessSwitch();\n";
884   mFile << "    template <class T, unsigned int D> void Process();\n";
885   // EO black box declaration
886   mFile << "};\n\n";
887
888
889
890   // BO black box description
891   mFile << "BBTK_BEGIN_DESCRIBE_BLACK_BOX("
892         << mName << "Generic,bbtk::AtomicBlackBox);\n";
893   mFile << "BBTK_NAME(\"" << mName <<"\");\n";
894
895   // Author
896   mFile << "BBTK_AUTHOR(\""<<mAuthor<< "\");\n";
897
898   // Description
899   mFile << "BBTK_DESCRIPTION(\""<<mDescription<< "\");\n"; 
900   
901   // Inputs
902   for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi) 
903     {
904       mFile << "BBTK_INPUT(";
905       mFile << mName << "Generic," << ioi->name << ",\""
906             << ioi->descr << "\"," <<  ioi->generic_type <<");\n";
907     }
908   
909   // Outputs
910   for (ioi=mOutput.begin(); ioi!=mOutput.end(); ++ioi) 
911     {
912       mFile << "BBTK_OUTPUT(";
913       mFile << mName << "Generic," << ioi->name << ",\""
914             << ioi->descr << "\"," <<  ioi->generic_type <<");\n";
915     }
916   
917   // EO black box description
918   mFile << "BBTK_END_DESCRIBE_BLACK_BOX("
919         << mName << "Generic);\n";
920
921
922   //=================================================================
923   // ProcessSwitch implementation
924   mFile << "void "<< mName <<"Generic::ProcessSwitch()\n"
925         << "{\n"
926         << "CALL_FOR_ALL_TYPES_AND_DIM(bbGetInputIn()->GetType(),\n"
927         << "                           bbGetInputIn()->GetDimension(),\n"
928         << "                           Process);\n"
929         << "}\n";
930   //=================================================================
931
932
933   //=================================================================
934   // Template process implementation
935   mFile << "template <class T, unsigned int D>\n"
936         << "void "<<mName<<"Generic::Process()\n"
937         << "{\n"
938         << "  bbtkDebugMessageInc(\"Kernel\",9,\n"
939         << "      \""<<mName 
940         << "Generic::Process<\"<<TypeName<T>()<<\",\"<<D<<\">()\"<<std::endl);\n"
941     
942         << "  typedef itk::Image<T,D> ImageType;\n"
943         << "  typedef "<<mName<<"<ImageType> FilterType;\n"
944     
945         << "  FilterType* f = new FilterType(\"Temp\");\n"
946     
947         << "  f->bbSetInputIn( this->bbGetInputIn()->GetImage<T,D>() );\n";
948   
949   for (ioi=mInput.begin(); ioi!=mInput.end(); ++ioi) 
950     {
951       if (ioi->name == "In") continue;
952       mFile << "  f->bbSetInput"<<ioi->name<<" ( this->bbGetInput" 
953             << ioi->name << "() );\n";
954     }
955   
956   mFile << "  f->bbUpdate();\n"
957         << "  this->bbSetOutputOut( new itkImage( f->bbGetOutputOut() ) );\n"
958         << "  f->UnRegister();\n"
959         << "  bbtkDebugDecTab(\"Kernel\",9);\n"
960         << "}\n\n";
961   //=================================================================
962
963
964 }
965 //==========================================================================
966
967
968 //==========================================================================
969 void bbfy::CreateCode()
970 {
971   mCxxName = "bb";
972   mCxxName += mPackage;
973   mCxxName += mName;
974   mCxxName += ".cxx";
975   if (mVerbose) std::cout << " - Creating code   '"<<mCxxName<<"'"<<std::endl;
976   std::string fullname = mOutputPath + mCxxName;
977   mFile.open(fullname.c_str());
978   if (!mFile.good()) 
979     {
980       std::string mess("Error : could not open file \"");
981       mess += fullname;
982       mess += "\"";
983       throw bbfyException(mess);
984     }
985   
986   // Includes
987   // Header of the class
988   mFile << "#include \"" << mHName << "\"\n";
989
990   // Include Package header
991   mFile << "#include \"bb"<<mPackage << "Package.h\"\n";
992
993   // BO namespace
994   BeginNamespace();
995  
996   
997   // Template class ?
998   if (mTemplateParam.size()>0) 
999     {
1000       // Implementation
1001       mFile << "BBTK_BLACK_BOX_TEMPLATE_IMPLEMENTATION("
1002             << mName << ","  
1003             << mParentBlackBox << ");\n";
1004      
1005       if (mGeneric) 
1006         {       
1007           // Implementation
1008           mFile << "BBTK_BLACK_BOX_IMPLEMENTATION("
1009                 << mName << "Generic,bbtk::AtomicBlackBox);\n";
1010           // Package
1011           mFile << "BBTK_ADD_BLACK_BOX_TO_PACKAGE("
1012                 << mPackage << ","
1013                 << mName << "Generic)\n";
1014         }
1015     }
1016   else 
1017     {
1018       // Non template class
1019       // Package
1020       mFile << "BBTK_ADD_BLACK_BOX_TO_PACKAGE("
1021             << mPackage << ","
1022             << mName << ")\n";
1023
1024       // Implementation
1025       mFile << "BBTK_BLACK_BOX_IMPLEMENTATION("
1026             << mName << ","  
1027             << mParentBlackBox << ");\n"; 
1028     }
1029   // Process
1030   if ((mType == STD)||(mProcess.size()))
1031     {
1032       mFile << "void "<<mName<<"::Process()\n{\n";
1033       mFile << mProcess << "\n";
1034       mFile << "}\n";
1035     }
1036   // CreateWidget
1037   if (mIsWidget)
1038     {
1039       mFile << "void "<<mName<<"::CreateWidget()\n{\n";
1040       mFile << mCreateWidget << "\n";
1041       mFile << "}\n";
1042     }
1043
1044   // User constr / copy constr / destr implementation
1045   mFile <<"void "<<mName<<"::bbUserConstructor()"<<std::endl;
1046   mFile << "{"<<std::endl;
1047   //mFile<<"bbtkDebugMessage(\"Kernel\",9,\""<<mName<<::bbUserConstructor()"<<std::endl);"<<std::endl;
1048   
1049   mFile << mUserConstructor << std::endl;
1050   mFile << "}" << std::endl;
1051
1052   mFile <<"void "<<mName<<"::bbUserCopyConstructor()"<<std::endl;
1053   mFile << "{"<<std::endl;
1054   //mFile<<"bbtkDebugMessage(\"Kernel\",9,\""<<mName<<::bbUserCopyConstructor()"<<std::endl);"<<std::endl;
1055   mFile << mUserCopyConstructor << std::endl;
1056   mFile << "}" << std::endl;
1057
1058   mFile <<"void "<<mName<<"::bbUserDestructor()"<<std::endl;
1059   mFile << "{"<<std::endl;
1060   //mFile<<"bbtkDebugMessage(\"Kernel\",9,\""<<mName<<::bbUserDestructor()"<<std::endl);"<<std::endl;
1061   mFile << mUserDestructor << std::endl;
1062   mFile << "}" << std::endl;
1063
1064
1065   // EO namespace
1066   EndNamespace();
1067
1068   mFile << "\n";
1069   
1070   // EOF
1071   mFile.close();
1072   
1073 }
1074 //==========================================================================
1075  
1076
1077
1078
1079
1080 //==========================================================================
1081 int main(int argc, char **argv)
1082 {
1083   
1084   if (argc<2 || argc>5) 
1085     {
1086       std::cerr << "usage : "<< argv[0] <<" xml_file [package_name] [output_path] [-q]" << std::endl;
1087       return 1;
1088     }
1089
1090   try 
1091     {
1092       std::string package("PACKAGE_NAME");
1093       std::string output_path("");
1094       bool verbose = true;
1095       if (argc>2) package = argv[2];
1096       if (argc>3) output_path = argv[3];
1097       if (argc>4) verbose = false;
1098       
1099       bbfy B(argv[1],package,output_path,verbose);
1100     }
1101   catch (bbfyException e)
1102     {
1103       std::cerr << argv[0] << "  " << argv[1] << std::endl
1104                 << e.mMessage << std::endl;
1105       return 1;
1106     }
1107   return 0;
1108 }
1109 //==========================================================================
1110
1111