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