]> Creatis software - cpPlugins.git/blob - appli/bash/BuildInstances.cxx.in
...
[cpPlugins.git] / appli / bash / BuildInstances.cxx.in
1 #include <cmath>
2 #include <deque>
3 #include <iostream>
4 #include <map>
5 #include <sstream>
6 #include <vector>
7 #include <appli/bash/Utility.h>
8
9 // -------------------------------------------------------------------------
10 typedef std::map< char, std::vector< std::string > >        TCommands;
11 typedef std::map< std::string, std::vector< std::string > > TDefinitions;
12
13 // -------------------------------------------------------------------------
14 void AddDefinitions( TDefinitions& defs, const std::string& cmd );
15 void InitDefinitions( TDefinitions& defs );
16 void Lines( TCommands& commands, const std::deque< std::string >& lines );
17 void ExpandDefinitions( TDefinitions& defs );
18 std::vector< std::string > Expand(
19   const std::vector< std::string >& commands, const TDefinitions& defs
20   );
21
22 // -------------------------------------------------------------------------
23 int main( int argc, char* argv[] )
24 {
25   // Get parameters from input
26   if( argc < 5 )
27   {
28     std::cerr
29       << "Usage: " << argv[ 0 ]
30       << " definitions library_name source_prefix number_of_files" << std::endl;
31     return( 1 );
32
33   } // fi
34   std::string input_definitions = argv[ 1 ];
35   std::string library_name = argv[ 2 ];
36   std::string source_prefix = argv[ 3 ];
37   std::string header_file = source_prefix + std::string( ".h" );
38   std::stringstream nfiles_str( argv[ 4 ] );
39   unsigned int number_of_files;
40   nfiles_str >> number_of_files;
41
42   // Read input instances
43   std::string buffer;
44   if( !cpPlugins_bash::Read( buffer, input_definitions ) )
45   {
46     std::cerr
47       << "Error reading input instances from \""
48       << input_definitions << "\""
49       << std::endl;
50     return( 1 );
51
52   } // fi
53
54   // Put it in a line-by-line structure
55   std::deque< std::string > lines;
56   cpPlugins_bash::Tokenize( lines, buffer, "\n" );
57
58   // Parse commands
59   TCommands commands;
60   Lines( commands, lines );
61
62   // Init definitions
63   TDefinitions defs;
64   InitDefinitions( defs );
65   auto dIt = commands.find( 'd' );
66   if( dIt != commands.end( ) )
67   {
68     for( auto vIt = dIt->second.begin( ); vIt != dIt->second.end( ); ++vIt )
69       AddDefinitions( defs, *vIt );
70
71   } // fi
72
73   // Expand definitions
74   ExpandDefinitions( defs );
75
76   // Expand code
77   std::vector< std::string > before;
78   auto bIt = commands.find( 'b' );
79   if( bIt != commands.end( ) )
80     before = bIt->second;
81   auto templates = Expand( commands[ 't' ], defs );
82   auto includes = Expand( commands[ 'i' ], defs );
83   auto classes = Expand( commands[ 'c' ], defs );
84
85   // Write header file
86   std::stringstream header;
87   header
88     << "#ifndef __" << library_name << "__h__" << std::endl
89     << "#define __" << library_name << "__h__" << std::endl << std::endl
90     << "#define ITK_MANUAL_INSTANTIATION" << std::endl;
91   for( auto i = before.begin( ); i != before.end( ); ++i )
92     header << std::endl << *i;
93   header << std::endl;
94   for( auto i = includes.begin( ); i != includes.end( ); ++i )
95     header << "#include <" << *i << ">" << std::endl;
96   for( auto i = templates.begin( ); i != templates.end( ); ++i )
97     header << "#include <" << *i << ".h>" << std::endl;
98   header << std::endl;
99   for( auto i = classes.begin( ); i != classes.end( ); ++i )
100     header
101       << "extern template class " << *i << ";" << std::endl;
102   header
103     << std::endl << "#endif // __" << library_name << "__H__" << std::endl;
104   if( !( cpPlugins_bash::Write( header.str( ), header_file ) ) )
105   {
106     std::cerr << "Error writing header" << std::endl;
107     return( 1 );
108
109   } // fi
110
111   // Write source code
112   unsigned int classes_per_file =
113     ( unsigned int )(
114       std::floor( double( classes.size( ) ) / double( number_of_files ) )
115       );
116   if( classes_per_file == 0 )
117     classes_per_file = 1;
118   std::vector< std::vector< std::string > > all_lines( 1 );
119   for( unsigned int c_id = 0; c_id < classes.size( ); ++c_id )
120   {
121     all_lines[ all_lines.size( ) - 1 ].push_back( classes[ c_id ] );
122     if( c_id % classes_per_file == classes_per_file - 1 )
123       all_lines.push_back( std::vector< std::string >( ) );
124
125   } // rof
126
127   // Paranoiac code
128   while( all_lines.size( ) > number_of_files )
129   {
130     all_lines[ all_lines.size( ) - 2 ].insert(
131       all_lines[ all_lines.size( ) - 2 ].end( ),
132       all_lines[ all_lines.size( ) - 1 ].begin( ),
133       all_lines[ all_lines.size( ) - 1 ].end( )
134       );
135     all_lines.pop_back( );
136
137   } // elihw
138   while( all_lines.size( ) < number_of_files )
139     all_lines.push_back( std::vector< std::string >( ) );
140
141   // Real write
142   for( unsigned int f_id = 0; f_id < all_lines.size( ); ++f_id )
143   {
144     std::stringstream source;
145     source << "#define ITK_MANUAL_INSTANTIATION" << std::endl;
146     source
147       << "#include <" << library_name << "_Export.h>"
148       << std::endl;
149     for( auto i = before.begin( ); i != before.end( ); ++i )
150       source << std::endl << *i;
151     source << std::endl;
152     if( templates.size( ) > 0 )
153     {
154       for( auto i = includes.begin( ); i != includes.end( ); ++i )
155         source << "#include <" << *i << ">" << std::endl;
156
157       for( auto i = templates.begin( ); i != templates.end( ); ++i )
158       {
159         source << "#include <" << *i << ".h>" << std::endl;
160         source << "#include <" << *i << ".hxx>" << std::endl;
161
162       } // rof
163       source << std::endl;
164
165     } // fi
166     for(
167       auto c_it = all_lines[ f_id ].begin( );
168       c_it != all_lines[ f_id ].end( );
169       ++c_it
170       )
171       source
172         << "template class " << library_name << "_EXPORT "
173         << *c_it << ";" << std::endl;
174     source << std::endl << "// eof" << std::endl;
175     std::stringstream source_file;
176     source_file
177       << source_prefix << "_" << f_id << ".cxx";
178     if( !( cpPlugins_bash::Write( source.str( ), source_file.str( ) ) ) )
179     {
180       std::cerr << "Error writing header" << std::endl;
181       return( 1 );
182
183     } // fi
184
185   } // rof
186   return( 0 );
187 }
188
189 // -------------------------------------------------------------------------
190 void AddDefinitions( TDefinitions& defs, const std::string& cmd )
191 {
192   std::vector< std::string > tokens;
193   cpPlugins_bash::Tokenize( tokens, cmd, "=;" );
194   for( unsigned int i = 1; i < tokens.size( ); ++i )
195     defs[ tokens[ 0 ] ].push_back( tokens[ i ] );
196 }
197
198 // -------------------------------------------------------------------------
199 void InitDefinitions( TDefinitions& defs )
200 {
201   AddDefinitions( defs, "integers=@cpPlugins_INTEGER_TYPES@" );
202   AddDefinitions( defs, "reals=@cpPlugins_REAL_TYPES@" );
203   AddDefinitions( defs, "colors=RGBPixel;RGBAPixel" );
204   AddDefinitions( defs, "vectors=CovariantVector;Point;SymmetricSecondRankTensor;Vector" );
205   AddDefinitions( defs, "tensors=DiffusionTensor3D" );
206   AddDefinitions( defs, "matrices=Matrix" );
207   AddDefinitions( defs, "process_dims=@cpPlugins_PROCESS_DIMENSIONS@" );
208   AddDefinitions( defs, "visual_dims=@cpPlugins_VISUAL_DIMENSIONS@" );
209   AddDefinitions( defs, "naturals=unsigned #integers#" );
210   AddDefinitions( defs, "pixels=#integers#;#naturals#;#reals#" );
211 }
212
213 // -------------------------------------------------------------------------
214 void Lines( TCommands& commands, const std::deque< std::string >& lines )
215 {
216   std::string buffer;
217
218   for( auto l = lines.begin( ); l != lines.end( ); ++l )
219   {
220     auto pos = l->find_first_not_of( " " );
221     char cmd = tolower( ( *l )[ pos ] );
222     commands[ cmd ].push_back(
223       l->substr( l->find_first_not_of( " ", pos + 1 ) )
224       );
225
226   } // elihw
227 }
228
229 // -------------------------------------------------------------------------
230 void ExpandDefinitions( TDefinitions& defs )
231 {
232   for( auto d = defs.begin( ); d != defs.end( ); ++d )
233   {
234     std::deque< std::string > q;
235     q.insert( q.end( ), d->second.begin( ), d->second.end( ) );
236
237     std::vector< std::string > n;
238     while( q.size( ) > 0 )
239     {
240       auto v = q.front( );
241       q.pop_front( );
242
243       auto pos = v.find( "#" );
244       if( pos != std::string::npos )
245       {
246         auto epos = v.find( "#", pos + 1 );
247         auto tok = v.substr( pos, epos - pos + 1 );
248         auto id = tok.substr( 1, tok.size( ) - 2 );
249         auto rd = defs.find( id );
250         if( rd != defs.end( ) )
251           for( auto r = rd->second.begin( ); r != rd->second.end( ); ++r )
252             q.push_back( cpPlugins_bash::Replace( v, tok, *r ) );
253       }
254       else
255         n.push_back( v );
256       
257     } // elihw
258     d->second = n;
259
260   } // rof
261 }
262
263 // -------------------------------------------------------------------------
264 std::vector< std::string > Expand(
265   const std::vector< std::string >& commands, const TDefinitions& defs
266   )
267 {
268   std::vector< std::string > lines;
269   std::deque< std::string > q;
270   q.insert( q.end( ), commands.begin( ), commands.end( ) );
271   while( q.size( ) > 0 )
272   {
273     auto v = q.front( );
274     q.pop_front( );
275
276     auto d_pos = v.find( "$" );
277     auto n_pos = v.find( "#" );
278
279     if( d_pos != std::string::npos )
280     {
281       auto e_pos = v.find( "$", d_pos + 1 );
282       auto tok = v.substr( d_pos, e_pos - d_pos + 1 );
283       std::vector< std::string > tokens;
284       cpPlugins_bash::Tokenize( tokens, tok, "$;" );
285       for( auto t = tokens.begin( ); t != tokens.end( ); ++t )
286       {
287         if( *t == " ")
288           q.push_front( cpPlugins_bash::Replace( v, tok, "" ) );
289         else
290           q.push_front( cpPlugins_bash::Replace( v, tok, *t ) );
291
292       } // rof
293     }
294     else if( n_pos != std::string::npos )
295     {
296       auto e_pos = v.find( "#", n_pos + 1 );
297       auto tok = v.substr( n_pos, e_pos - n_pos + 1 );
298       auto id = tok.substr( 1, tok.size( ) - 2 );
299       auto rd = defs.find( id );
300       if( rd != defs.end( ) )
301         for( auto r = rd->second.begin( ); r != rd->second.end( ); ++r )
302           q.push_front( cpPlugins_bash::Replace( v, tok, *r ) );
303     }
304     else
305       lines.push_back( v );
306
307   } // elihw
308
309   return( lines );
310 }
311
312 // eof - $RCSfile$