7 #include <appli/bash/Utility.h>
9 // -------------------------------------------------------------------------
10 typedef std::map< char, std::vector< std::string > > TCommands;
11 typedef std::map< std::string, std::vector< std::string > > TDefinitions;
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
22 // -------------------------------------------------------------------------
23 int main( int argc, char* argv[] )
25 // Get parameters from input
29 << "Usage: " << argv[ 0 ]
30 << " definitions library_name source_prefix number_of_files" << std::endl;
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;
42 // Read input instances
44 if( !cpPlugins_bash::Read( buffer, input_definitions ) )
47 << "Error reading input instances from \""
48 << input_definitions << "\""
54 // Put it in a line-by-line structure
55 std::deque< std::string > lines;
56 cpPlugins_bash::Tokenize( lines, buffer, "\n" );
60 Lines( commands, lines );
64 InitDefinitions( defs );
65 auto dIt = commands.find( 'd' );
66 if( dIt != commands.end( ) )
68 for( auto vIt = dIt->second.begin( ); vIt != dIt->second.end( ); ++vIt )
69 AddDefinitions( defs, *vIt );
74 ExpandDefinitions( defs );
77 std::vector< std::string > before;
78 auto bIt = commands.find( 'b' );
79 if( bIt != commands.end( ) )
81 auto templates = Expand( commands[ 't' ], defs );
82 auto includes = Expand( commands[ 'i' ], defs );
83 auto classes = Expand( commands[ 'c' ], defs );
86 std::stringstream 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;
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;
99 for( auto i = classes.begin( ); i != classes.end( ); ++i )
101 << "extern template class " << *i << ";" << std::endl;
103 << std::endl << "#endif // __" << library_name << "__H__" << std::endl;
104 if( !( cpPlugins_bash::Write( header.str( ), header_file ) ) )
106 std::cerr << "Error writing header" << std::endl;
112 unsigned int classes_per_file =
114 std::floor( double( classes.size( ) ) / double( number_of_files ) )
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 )
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 >( ) );
128 while( all_lines.size( ) > number_of_files )
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( )
135 all_lines.pop_back( );
138 while( all_lines.size( ) < number_of_files )
139 all_lines.push_back( std::vector< std::string >( ) );
142 for( unsigned int f_id = 0; f_id < all_lines.size( ); ++f_id )
144 std::stringstream source;
145 source << "#define ITK_MANUAL_INSTANTIATION" << std::endl;
147 << "#include <" << library_name << "_Export.h>"
149 for( auto i = before.begin( ); i != before.end( ); ++i )
150 source << std::endl << *i;
152 if( templates.size( ) > 0 )
154 for( auto i = includes.begin( ); i != includes.end( ); ++i )
155 source << "#include <" << *i << ">" << std::endl;
157 for( auto i = templates.begin( ); i != templates.end( ); ++i )
159 source << "#include <" << *i << ".h>" << std::endl;
160 source << "#include <" << *i << ".hxx>" << std::endl;
167 auto c_it = all_lines[ f_id ].begin( );
168 c_it != all_lines[ f_id ].end( );
172 << "template class " << library_name << "_EXPORT "
173 << *c_it << ";" << std::endl;
174 source << std::endl << "// eof" << std::endl;
175 std::stringstream source_file;
177 << source_prefix << "_" << f_id << ".cxx";
178 if( !( cpPlugins_bash::Write( source.str( ), source_file.str( ) ) ) )
180 std::cerr << "Error writing header" << std::endl;
189 // -------------------------------------------------------------------------
190 void AddDefinitions( TDefinitions& defs, const std::string& cmd )
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 ] );
198 // -------------------------------------------------------------------------
199 void InitDefinitions( TDefinitions& defs )
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#" );
213 // -------------------------------------------------------------------------
214 void Lines( TCommands& commands, const std::deque< std::string >& lines )
218 for( auto l = lines.begin( ); l != lines.end( ); ++l )
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 ) )
229 // -------------------------------------------------------------------------
230 void ExpandDefinitions( TDefinitions& defs )
232 for( auto d = defs.begin( ); d != defs.end( ); ++d )
234 std::deque< std::string > q;
235 q.insert( q.end( ), d->second.begin( ), d->second.end( ) );
237 std::vector< std::string > n;
238 while( q.size( ) > 0 )
243 auto pos = v.find( "#" );
244 if( pos != std::string::npos )
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 ) );
263 // -------------------------------------------------------------------------
264 std::vector< std::string > Expand(
265 const std::vector< std::string >& commands, const TDefinitions& defs
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 )
276 auto d_pos = v.find( "$" );
277 auto n_pos = v.find( "#" );
279 if( d_pos != std::string::npos )
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 )
288 q.push_front( cpPlugins_bash::Replace( v, tok, "" ) );
290 q.push_front( cpPlugins_bash::Replace( v, tok, *t ) );
294 else if( n_pos != std::string::npos )
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 ) );
305 lines.push_back( v );