]> Creatis software - cpPlugins.git/blob - appli/bash/BuildDemangleHeader.cxx.in
8eb395c913678a27d4a9dfb23a4bb83456c7b45a
[cpPlugins.git] / appli / bash / BuildDemangleHeader.cxx.in
1 #include <iostream>
2 #include <sstream>
3 #include <vector>
4 #include <appli/bash/Utility.h>
5
6 // -------------------------------------------------------------------------
7 void Output(
8   std::stringstream& out,
9   const std::vector< std::string >& defs
10   )
11 {
12   if( defs.size( ) > 0 )
13   {
14     for( unsigned int k = 0; k < defs.size( ); ++k )
15     {
16       std::string cast_str = "dynamic_cast< ";
17       cast_str += defs[ k ];
18       cast_str += "* >( _obj )";
19       if( k == 0 )
20         out
21           << "  if( " << cast_str << " != NULL ) \\" << std::endl
22           << "    this->_fun( " << cast_str << " )";
23       else
24         out
25           << "  else if( " << cast_str << " != NULL ) \\" << std::endl
26           << "    this->_fun( " << cast_str << " )";
27       if( k < defs.size( ) - 1 )
28         out << "; \\";
29       out << std::endl;
30
31     } // rof
32
33   } // fi
34 }
35
36 // -------------------------------------------------------------------------
37 void Dims(
38   std::stringstream& out,
39   const std::string& macro,
40   const std::vector< std::string >& dims
41   )
42 {
43   for( unsigned int k = 0; k < dims.size( ); ++k )
44   {
45     if( k == 0 )
46       out
47         << "  " << macro << dims[ k ] << " )";
48     else
49       out
50         << "  else " << macro << dims[ k ] << " )";
51     if( k < dims.size( ) - 1 )
52       out << "; \\";
53     out << std::endl;
54
55   } // rof
56 }
57
58 // -------------------------------------------------------------------------
59 int main( int argc, char* argv[] )
60 {
61   // Get parameters from input
62   if( argc < 2 )
63   {
64     std::cerr
65       << "Usage: " << argv[ 0 ]
66       << " output_filename" << std::endl;
67     return( 1 );
68
69   } // fi
70   std::string output_filename = argv[ 1 ];
71   std::stringstream out;
72
73   out << "#ifndef __cpPlugins__DEMANGLEBASE_MACROS__h__" << std::endl;
74   out << "#define __cpPlugins__DEMANGLEBASE_MACROS__h__" << std::endl << std::endl;
75
76   // Get base types
77   std::vector< std::string > int_types, real_types, process_dims, visual_dims;
78   cpPlugins_bash::Tokenize( int_types, "@cpPlugins_INTEGER_TYPES@", ";" );
79   cpPlugins_bash::Tokenize( real_types, "@cpPlugins_REAL_TYPES@", ";" );
80   cpPlugins_bash::Tokenize( process_dims, "@cpPlugins_PROCESS_DIMENSIONS@", ";" );
81   cpPlugins_bash::Tokenize( visual_dims, "@cpPlugins_VISUAL_DIMENSIONS@", ";" );
82
83   // Process image base
84   std::string op = "itk::ImageBase< ";
85   std::string cl = " >";
86   std::vector< std::string > process_dims_tokens;
87   for( auto i = process_dims.begin( ); i != process_dims.end( ); ++i )
88     process_dims_tokens.push_back( op + *i + cl );
89
90   out
91     << "#define cpPlugins_Demangle_ImageProcessDims( _obj, _fun ) \\"
92     << std::endl;
93   Output( out, process_dims_tokens );
94   out << std::endl;
95
96   // Visual image base
97   std::vector< std::string > visual_dims_tokens;
98   for( auto i = visual_dims.begin( ); i != visual_dims.end( ); ++i )
99     visual_dims_tokens.push_back( op + *i + cl );
100   out
101     << "#define cpPlugins_Demangle_ImageVisualDims( _obj, _fun ) \\"
102     << std::endl;
103   Output( out, visual_dims_tokens );
104   out << std::endl;
105
106   // Integer types
107   op = "itk::Image< ";
108   cl = ", _dim >";
109   std::vector< std::string > int_tokens;
110   for( auto i = int_types.begin( ); i != int_types.end( ); ++i )
111   {
112     int_tokens.push_back( op + *i + cl );
113     int_tokens.push_back( op + std::string( "unsigned " ) + *i + cl );
114
115   } // rof
116   out
117     << "#define cpPlugins_Demangle_ImageIntegers( _obj, _fun, _dim ) \\"
118     << std::endl;
119   Output( out, int_tokens );
120   out << std::endl;
121   out
122     << "#define cpPlugins_Demangle_ImageIntegers_Dims( _obj, _fun ) \\"
123     << std::endl;
124   Dims( out, "cpPlugins_Demangle_ImageIntegers( _obj, _fun, ", process_dims );
125   out << std::endl;
126
127   // Real types
128   std::vector< std::string > real_tokens;
129   for( auto i = real_types.begin( ); i != real_types.end( ); ++i )
130     real_tokens.push_back( op + *i + cl );
131   out
132     << "#define cpPlugins_Demangle_ImageReals( _obj, _fun, _dim ) \\"
133     << std::endl;
134   Output( out, real_tokens );
135   out << std::endl;
136   out
137     << "#define cpPlugins_Demangle_ImageReals_Dims( _obj, _fun ) \\"
138     << std::endl;
139   Dims( out, "cpPlugins_Demangle_ImageReals( _obj, _fun, ", process_dims );
140   out << std::endl;
141
142   // Complex types
143   op = "itk::Image< std::complex< ";
144   cl = " >, _dim >";
145   std::vector< std::string > complex_tokens;
146   for( auto i = real_types.begin( ); i != real_types.end( ); ++i )
147     complex_tokens.push_back( op + *i + cl );
148   out
149     << "#define cpPlugins_Demangle_ImageComplexes( _obj, _fun, _dim ) \\"
150     << std::endl;
151   Output( out, complex_tokens );
152   out << std::endl;
153   out
154     << "#define cpPlugins_Demangle_ImageComplexes_Dims( _obj, _fun ) \\"
155     << std::endl;
156   Dims( out, "cpPlugins_Demangle_ImageComplexes( _obj, _fun, ", process_dims );
157   out << std::endl;
158
159   // Color types
160   op = "itk::Image< itk::RGBPixel< ";
161   cl = " >, _dim >";
162   std::vector< std::string > color_tokens;
163   for( auto i = int_types.begin( ); i != int_types.end( ); ++i )
164     color_tokens.push_back( op + *i + cl );
165   for( auto i = int_types.begin( ); i != int_types.end( ); ++i )
166     color_tokens.push_back( op + std::string( "unsigned " ) + *i + cl );
167   for( auto i = real_types.begin( ); i != real_types.end( ); ++i )
168     color_tokens.push_back( op + *i + cl );
169   op = "itk::Image< itk::RGBAPixel< ";
170   for( auto i = int_types.begin( ); i != int_types.end( ); ++i )
171     color_tokens.push_back( op + *i + cl );
172   for( auto i = int_types.begin( ); i != int_types.end( ); ++i )
173     color_tokens.push_back( op + std::string( "unsigned " ) + *i + cl );
174   for( auto i = real_types.begin( ); i != real_types.end( ); ++i )
175     color_tokens.push_back( op + *i + cl );
176   out
177     << "#define cpPlugins_Demangle_ImageColors( _obj, _fun, _dim ) \\"
178     << std::endl;
179   Output( out, color_tokens );
180   out << std::endl;
181   out
182     << "#define cpPlugins_Demangle_ImageColors_Dims( _obj, _fun ) \\"
183     << std::endl;
184   Dims( out, "cpPlugins_Demangle_ImageColors( _obj, _fun, ", process_dims );
185   out << std::endl;
186
187   // FixedArray types
188   op = "itk::Image< itk::FixedArray< ";
189   cl = ", _dim >, _dim >";
190   std::vector< std::string > fixedarray_tokens;
191   for( auto i = int_types.begin( ); i != int_types.end( ); ++i )
192     fixedarray_tokens.push_back( op + *i + cl );
193   for( auto i = int_types.begin( ); i != int_types.end( ); ++i )
194     fixedarray_tokens.push_back( op + "unsigned " + *i + cl );
195   for( auto i = real_types.begin( ); i != real_types.end( ); ++i )
196     fixedarray_tokens.push_back( op + *i + cl );
197   out
198     << "#define cpPlugins_Demangle_ImageFixedArrays( _obj, _fun, _dim ) \\"
199     << std::endl;
200   Output( out, fixedarray_tokens );
201   out << std::endl;
202   out
203     << "#define cpPlugins_Demangle_ImageFixedArrays_Dims( _obj, _fun ) \\"
204     << std::endl;
205   Dims( out, "cpPlugins_Demangle_ImageFixedArrays( _obj, _fun, ", process_dims );
206   out << std::endl;
207
208   // Covariant types
209   op = "itk::Image< itk::CovariantVector< ";
210   cl = ", _dim >, _dim >";
211   std::vector< std::string > cov_tokens;
212   for( auto i = real_types.begin( ); i != real_types.end( ); ++i )
213     cov_tokens.push_back( op + *i + cl );
214   out
215     << "#define cpPlugins_Demangle_ImageCovariantVectors( _obj, _fun, _dim ) \\"
216     << std::endl;
217   Output( out, cov_tokens );
218   out << std::endl;
219   out
220     << "#define cpPlugins_Demangle_ImageCovariantVectors_Dims( _obj, _fun ) \\"
221     << std::endl;
222   Dims(
223     out, "cpPlugins_Demangle_ImageCovariantVectors( _obj, _fun, ", process_dims
224     );
225   out << std::endl;
226
227   // Vector types
228   op = "itk::Image< itk::Vector< ";
229   cl = ", _dim >, _dim >";
230   std::vector< std::string > vec_tokens;
231   for( auto i = real_types.begin( ); i != real_types.end( ); ++i )
232     vec_tokens.push_back( op + *i + cl );
233   out
234     << "#define cpPlugins_Demangle_ImageVectors( _obj, _fun, _dim ) \\"
235     << std::endl;
236   Output( out, vec_tokens );
237   out << std::endl;
238   out
239     << "#define cpPlugins_Demangle_ImageVectors_Dims( _obj, _fun ) \\"
240     << std::endl;
241   Dims( out, "cpPlugins_Demangle_ImageVectors( _obj, _fun, ", process_dims );
242   out << std::endl;
243
244   // Point types
245   op = "itk::Image< itk::Point< ";
246   cl = ", _dim >, _dim >";
247   std::vector< std::string > pnt_tokens;
248   for( auto i = real_types.begin( ); i != real_types.end( ); ++i )
249     pnt_tokens.push_back( op + *i + cl );
250   out
251     << "#define cpPlugins_Demangle_ImagePoints( _obj, _fun, _dim ) \\"
252     << std::endl;
253   Output( out, pnt_tokens );
254   out << std::endl;
255   out
256     << "#define cpPlugins_Demangle_ImagePoints_Dims( _obj, _fun ) \\"
257     << std::endl;
258   Dims( out, "cpPlugins_Demangle_ImagePoints( _obj, _fun, ", process_dims );
259   out << std::endl;
260
261   // 2nd rank types
262   op = "itk::Image< itk::SymmetricSecondRankTensor< ";
263   cl = ", _dim >, _dim >";
264   std::vector< std::string > sym_tokens;
265   for( auto i = real_types.begin( ); i != real_types.end( ); ++i )
266     sym_tokens.push_back( op + *i + cl );
267   out
268     << "#define cpPlugins_Demangle_ImageSymmetricSecondRankTensors( _obj, _fun, _dim ) \\"
269     << std::endl;
270   Output( out, sym_tokens );
271   out << std::endl;
272   out
273     << "#define cpPlugins_Demangle_ImageSymmetricSecondRankTensors_Dims( _obj, _fun ) \\"
274     << std::endl;
275   Dims( out, "cpPlugins_Demangle_ImageSymmetricSecondRankTensors( _obj, _fun, ", process_dims );
276   out << std::endl;
277
278   // Diffusion tensors
279   op = "itk::Image< itk::DiffusionTensor3D< ";
280   cl = " >, 3 >";
281   std::vector< std::string > diff_tokens;
282   for( auto i = real_types.begin( ); i != real_types.end( ); ++i )
283     diff_tokens.push_back( op + *i + cl );
284   out
285     << "#define cpPlugins_Demangle_ImageDiffusionTensors3D( _obj, _fun ) \\"
286     << std::endl;
287   Output( out, diff_tokens );
288   out << std::endl;
289
290   // Matrix types
291   op = "itk::Image< itk::Matrix< ";
292   cl = ", _dim, _dim >, _dim >";
293   std::vector< std::string > mat_tokens;
294   for( auto i = real_types.begin( ); i != real_types.end( ); ++i )
295     mat_tokens.push_back( op + *i + cl );
296   out
297     << "#define cpPlugins_Demangle_ImageMatrices( _obj, _fun, _dim ) \\"
298     << std::endl;
299   Output( out, mat_tokens );
300   out << std::endl;
301   out
302     << "#define cpPlugins_Demangle_ImageMatrices_Dims( _obj, _fun ) \\"
303     << std::endl;
304   Dims( out, "cpPlugins_Demangle_ImageMatrices( _obj, _fun, ", process_dims );
305   out << std::endl;
306
307   // Offset types
308   op = "itk::Image< itk::Offset< ";
309   cl = " > >";
310   std::vector< std::string > off_tokens;
311   for( auto i = process_dims.begin( ); i != process_dims.end( ); ++i )
312     off_tokens.push_back( op + *i + cl );
313   out
314     << "#define cpPlugins_Demangle_ImageOffsets( _obj, _fun ) \\"
315     << std::endl;
316   Output( out, off_tokens );
317   out << std::endl;
318
319   // Real types
320   op = "itk::Mesh< ";
321   cl = ", _dim >";
322   std::vector< std::string > mesh_tokens;
323   for( auto i = real_types.begin( ); i != real_types.end( ); ++i )
324     mesh_tokens.push_back( op + *i + cl );
325   out
326     << "#define cpPlugins_Demangle_Meshes( _obj, _fun, _dim ) \\"
327     << std::endl;
328   Output( out, mesh_tokens );
329   out << std::endl;
330   out
331     << "#define cpPlugins_Demangle_Meshes_Dims( _obj, _fun ) \\"
332     << std::endl;
333   Dims( out, "cpPlugins_Demangle_Meshes( _obj, _fun, ", visual_dims );
334   out << std::endl;
335
336   out << std::endl;
337   out << "#endif // __cpPlugins__DEMANGLEBASE_MACROS__h__" << std::endl << std::endl;
338
339   if( !( cpPlugins_bash::Write( out.str( ), output_filename ) ) )
340   {
341     std::cerr << "Error writing header" << std::endl;
342     return( 1 );
343
344   } // fi
345   return( 0 );
346 }
347
348 // eof - $RCSfile$