// ========================================================================= // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co) // ========================================================================= $cmd=help;list;exit;create;describe;define;save;load;connect #include #include #include // ------------------------------------------------------------------------- class Console : public TinyCon::CompletionConsole { public: typedef Console Self; typedef TinyCon::CompletionConsole Superclass; typedef cpPlugins::Manager TManager; typedef cpPlugins::Pipeline TPipeline; typedef cpPlugins::ProcessObject TProcessObject; typedef cpPlugins::Filter TFilter; typedef cpPlugins::Functor TFunctor; typedef TManager::TPlugins TPlugins; public: Console( const std::string& prompt ); virtual ~Console( ); virtual int trigger( const std::vector< std::string >& args ) override; bool _cmd_help( const std::vector< std::string >& args ); bool _cmd_list( const std::vector< std::string >& args ); bool _cmd_exit( const std::vector< std::string >& args ); bool _cmd_create( const std::vector< std::string >& args ); bool _cmd_describe( const std::vector< std::string >& args ); bool _cmd_define( const std::vector< std::string >& args ); bool _cmd_save( const std::vector< std::string >& args ); bool _cmd_load( const std::vector< std::string >& args ); bool _cmd_connect( const std::vector< std::string >& args ); protected: TTrie* m_CommandsTrie; TTrie* m_PluginsTrie; TTrie* m_InputsTrie; TTrie* m_OutputsTrie; TManager m_Manager; TPipeline::SharedPtr m_Pipeline; }; // ------------------------------------------------------------------------- int main( int argc, char* argv[] ) { std::cout << "Welcome to cpPlugins' interactive creator" << std::endl; Console console( "> " ); console.run( ); return( 0 ); } // ------------------------------------------------------------------------- Console:: Console( const std::string& prompt ) : Superclass( prompt ) { // Add base commands and prepare help this->m_CommandsTrie = new TTrie( ); {{#cmd}} this->addCommand( "{{cmd}}" ); this->m_CommandsTrie->insert( "{{cmd}}" ); {{/cmd}} this->addCommand( "help", this->m_CommandsTrie ); // Add "create" options this->m_PluginsTrie = new TTrie( ); this->m_Manager.Configure( ); for( const TPlugins::value_type& p: this->m_Manager.GetPlugins( ) ) this->m_PluginsTrie->insert( p.first ); this->addCommand( "create", this->m_PluginsTrie ); // Add "connect" options this->m_InputsTrie = new TTrie( ); this->m_OutputsTrie = new TTrie( ); this->addCommand( "connect", this->m_OutputsTrie ); // Prepare pipeline this->m_Pipeline = TPipeline::New( ); } // ------------------------------------------------------------------------- Console:: ~Console( ) { /* TODO delete this->m_CommandsTrie; delete this->m_PluginsTrie; delete this->m_InputsTrie; delete this->m_OutputsTrie; */ } // ------------------------------------------------------------------------- int Console:: trigger( const std::vector< std::string >& args ) { {{#cmd}} if( args[ 0 ] == "{{cmd}}" ) return( ( this->_cmd_{{cmd}}( args ) )? 0: 1 ); else {{/cmd}} return( 0 ); } // ------------------------------------------------------------------------- bool Console:: _cmd_help( const std::vector< std::string >& args ) { return( true ); } // ------------------------------------------------------------------------- bool Console:: _cmd_list( const std::vector< std::string >& args ) { std::cout << this->m_Manager << std::endl; return( true ); } // ------------------------------------------------------------------------- bool Console:: _cmd_exit( const std::vector< std::string >& args ) { this->quit( ); return( false ); } // ------------------------------------------------------------------------- bool Console:: _cmd_create( const std::vector< std::string >& args ) { // Get arguments std::string nClass = ""; std::string nName = ""; if( args.size( ) > 1 ) { nClass = args[ 1 ]; if( args.size( ) > 2 ) nName = args[ 2 ]; TProcessObject* p = this->m_Pipeline->Cast< cpPlugins::Pipeline >( )-> CreateNode( nClass, nName ); if( p != NULL ) { // Prepare "describe" command this->addCommand( "describe", p->GetName( ) ); // Prepare "connect" command std::set< std::string > iNames = p->GetAllInputsNames( ); for( const std::string& s: iNames ) this->m_InputsTrie->insert( p->GetName( ) + "%" + s ); std::set< std::string > oNames = p->GetAllOutputsNames( ); for( const std::string& s: oNames ) this->m_OutputsTrie->insert( p->GetName( ) + "%" + s )-> setExtension( this->m_InputsTrie ); // Show some information std::cout << "Node \"" << p->GetName( ) << "\" of type \"" << p->GetClassName( ) << "\" created." << std::endl; } else std::cout << "Could not create node." << std::endl; } else std::cout << "Invalid \"create\" command: create nClass [nName]" << std::endl; return( true ); } // ------------------------------------------------------------------------- bool Console:: _cmd_describe( const std::vector< std::string >& args ) { if( args.size( ) > 1 ) { TProcessObject* p = this->m_Pipeline->Cast< cpPlugins::Pipeline >( )->GetNode( args[ 1 ] ); if( p != NULL ) p->Print( std::cout ); else std::cout << "Node \"" << args[ 1 ] << "\" does not exists." << std::endl; } else std::cout << "Invalid \"describe\" command: describe nName" << std::endl; return( true ); } // ------------------------------------------------------------------------- bool Console:: _cmd_define( const std::vector< std::string >& args ) { if( args.size( ) > 3 ) { if( args[ 1 ] == "input" ) { std::cout << "TODO input" << std::endl; } else if( args[ 1 ] == "output" ) { std::cout << "TODO output" << std::endl; } else std::cout << "Invalid \"define\" command: define [input/output] Port%Node node" << std::endl; /* TODO TProcessObject* p = this->m_Pipeline->Cast< cpPlugins::Pipeline >( )->GetNode( args[ 1 ] ); if( p != NULL ) p->Print( std::cout ); else std::cout << "Node \"" << args[ 1 ] << "\" does not exists." << std::endl; */ } else std::cout << "Invalid \"define\" command: define [input/output] Port%Node node" << std::endl; return( true ); } // ------------------------------------------------------------------------- bool Console:: _cmd_save( const std::vector< std::string >& args ) { if( args.size( ) > 1 ) { try { this->m_Pipeline->Cast< TPipeline >( )->SaveXML( args[ 1 ] ); } catch( std::exception& err ) { std::cout << "Error caught: " << err.what( ) << std::endl; } // end try } else std::cout << "Invalid \"save\" command: save filename" << std::endl; return( true ); } // ------------------------------------------------------------------------- bool Console:: _cmd_load( const std::vector< std::string >& args ) { if( args.size( ) > 1 ) { try { this->m_Pipeline->Cast< TPipeline >( )->LoadXML( args[ 1 ] ); // TODO: update tries!!! } catch( std::exception& err ) { std::cout << "Error caught: " << err.what( ) << std::endl; } // end try } else std::cout << "Invalid \"save\" command: save filename" << std::endl; return( true ); } // ------------------------------------------------------------------------- bool Console:: _cmd_connect( const std::vector< std::string >& args ) { std::size_t oPos = args[ 1 ].find_first_of( "%" ); std::size_t dPos = args[ 2 ].find_first_of( "%" ); std::string oObj = args[ 1 ].substr( 0, oPos ); std::string dObj = args[ 2 ].substr( 0, dPos ); std::string oPort = args[ 1 ].substr( oPos + 1 ); std::string dPort = args[ 2 ].substr( dPos + 1 ); try { this->m_Pipeline->Cast< TPipeline >( )->Connect( args[ 1 ].substr( 0, oPos ), args[ 2 ].substr( 0, dPos ), args[ 1 ].substr( oPos + 1 ), args[ 2 ].substr( dPos + 1 ) ); } catch( std::exception& err ) { std::cout << "Error caught: " << err.what( ) << std::endl; } // end try std::cout << "Connection between \"" << args[ 1 ] << "\" and \"" << args[ 2 ] << "\" done!" << std::endl; return( true ); } // eof - $RCSfile$