10 // -------------------------------------------------------------------------
11 typedef std::map< std::string, std::string > TPair;
12 typedef std::map< std::string, TPair > TInfo;
14 // -------------------------------------------------------------------------
15 bool is_valid_class( const std::string& str )
17 return( str.find( "cpPlugins_Id_Macro" ) != std::string::npos );
20 // -------------------------------------------------------------------------
21 void process_header( TInfo& info, const std::string& file_name )
23 // Load file into a buffer
24 std::ifstream file_stream( file_name.c_str( ) );
28 file_stream.seekg( 0, std::ios::end );
29 buf.reserve( file_stream.tellg( ) );
30 file_stream.seekg( 0, std::ios::beg );
32 ( std::istreambuf_iterator< char >( file_stream ) ),
33 std::istreambuf_iterator< char >( )
37 // Replace separators with spaces
38 std::replace( buf.begin( ), buf.end( ), ',', ' ' );
39 std::replace( buf.begin( ), buf.end( ), ';', ' ' );
40 std::replace( buf.begin( ), buf.end( ), ':', ' ' );
41 std::replace( buf.begin( ), buf.end( ), '(', ' ' );
42 std::replace( buf.begin( ), buf.end( ), ')', ' ' );
43 std::replace( buf.begin( ), buf.end( ), '{', ' ' );
44 std::replace( buf.begin( ), buf.end( ), '}', ' ' );
47 std::istringstream tokenizer( buf );
48 std::vector< std::string > tokens;
50 std::istream_iterator< std::string >( tokenizer ),
51 std::istream_iterator< std::string >( ),
52 std::back_inserter( tokens )
56 auto p = std::find_if( tokens.begin( ), tokens.end( ), is_valid_class );
57 if( p != tokens.end( ) )
59 // Find class name and category
60 auto cls_it = p; cls_it++;
61 auto cat_it = cls_it; cat_it++;
64 typedef std::reverse_iterator< std::vector< std::string >::iterator > _RIt;
65 _RIt r_end( tokens.begin( ) );
66 _RIt r_begin( cls_it );
67 auto ns_it = std::find( r_begin, r_end, "namespace" ); ns_it--;
70 info[ *cat_it ][ *cls_it ] = *ns_it;
75 // -------------------------------------------------------------------------
76 int main( int argc, char* argv[] )
81 << "Usage: " << argv[ 0 ]
82 << " output_file header_file_0.h header_file_0.h ..."
88 // Parse all header files
90 for( int i = 2; i < argc; ++i )
91 process_header( info, argv[ i ] );
92 if( info.size( ) == 0 )
94 std::cerr << "ERROR: No valid input headers." << std::endl;
100 std::ofstream out_stream( argv[ 1 ] );
102 // Write include section
104 << "#include <map>" << std::endl
105 << "#include <set>" << std::endl;
106 for( int i = 2; i < argc; ++i )
107 out_stream << "#include \"" << argv[ i ] << "\"" <<std::endl;
109 // Write init/finish section
112 << "void __attribute__ ((constructor)) _initialize( )" << std::endl
113 << "{" << std::endl << "}" << std::endl << std::endl
114 << "void __attribute__ ((destructor)) _finalize( )" << std::endl
115 << "{" << std::endl << "}" << std::endl << std::endl;
117 // Write access function
119 << "extern \"C\" std::map< std::string, std::set< std::string > > "
120 << "LoadedFilters( )" << std::endl << "{" << std::endl
121 << " std::map< std::string, std::set< std::string > > classes;"
124 for( auto iIt = info.begin( ); iIt != info.end( ); ++iIt )
126 for( auto jIt = iIt->second.begin( ); jIt != iIt->second.end( ); ++jIt )
129 << " auto f" << i << " = "
130 << jIt->second << "::" << jIt->first
131 << "::New( );" << std::endl
132 << " classes[ f" << i << "->GetClassCategory( ) ].insert( "
133 << "f" << i << "->GetClassName( ) );" << std::endl;
140 << " return( classes );" << std::endl
141 << "}" << std::endl << std::endl;
144 for( auto iIt = info.begin( ); iIt != info.end( ); ++iIt )
146 for( auto jIt = iIt->second.begin( ); jIt != iIt->second.end( ); ++jIt )
149 << "extern \"C\" cpPlugins::ProcessObject::Pointer "
150 << iIt->first << "_" << jIt->first
151 << "( )" << std::endl << "{" << std::endl
153 << " cpPlugins::ProcessObject::Pointer p;" << std::endl
154 << " " << jIt->second << "::" << jIt->first << "::Pointer f = "
155 << jIt->second << "::" << jIt->first << "::New( );" << std::endl
156 << " p = f.GetPointer( );" << std::endl << " return( p );"
159 << "}" << std::endl << std::endl;