From b2df0ed4f0edde04e03b929672d0787fd2a5769b Mon Sep 17 00:00:00 2001 From: vsr Date: Thu, 11 Feb 2016 16:53:18 +0300 Subject: [PATCH] Add cmd args C-like accessor class --- src/Qtx/Qtx.cxx | 58 ++++++++++++++++++ src/Qtx/Qtx.h | 14 +++++ src/SalomeApp/SalomeApp_Application.cxx | 14 +---- src/SalomeApp/SalomeApp_Engine_i.cxx | 14 +---- src/Session/Session_ServerCheck.cxx | 78 +++++++++++-------------- 5 files changed, 109 insertions(+), 69 deletions(-) diff --git a/src/Qtx/Qtx.cxx b/src/Qtx/Qtx.cxx index f43ec8504..cf3dc958f 100755 --- a/src/Qtx/Qtx.cxx +++ b/src/Qtx/Qtx.cxx @@ -2164,3 +2164,61 @@ Qt::HANDLE Qtx::getVisual() } #endif // WIN32 + +/*! + \class Qtx::CmdLineArgs + \brief Get access to the command line arguments in the C-like manner. + + This class translates command line arguments stored in QApplication in form of QStrlingList + to the char* array, in the same way as they specified to main() function. + + Constructor of class allocates required memory to store arguments; destructor deallocates it, + This allows using this class as a local variable: + + \code + Qtx::CmdLineArgs args; + some_function(args.argc(), args.argv()); // function that has main()-like syntax. + \endcode +*/ + +/*! + \brief Default constructor. +*/ +Qtx::CmdLineArgs::CmdLineArgs() +{ + QStringList args = QCoreApplication::arguments(); + myArgc = args.size(); + myArgv = new char*[myArgc]; + for ( int i = 0; i < myArgc; i++ ) { + QByteArray ba = args[i].toUtf8(); + myArgv[i] = qstrdup(ba.constData()); + } +} + +/*! + \brief Destructor. Deallocates the array with command line arguments +*/ +Qtx::CmdLineArgs::~CmdLineArgs() +{ + for ( int i = 0; i < myArgc; i++ ) + delete myArgv[i]; + delete[] myArgv; +} + +/*! + \brief Get number of command line arguments + \return number of arguments +*/ +int Qtx::CmdLineArgs::argc() const +{ + return myArgc; +} + +/*! + \brief Get command line arguments + \return command line arguments +*/ +char** Qtx::CmdLineArgs::argv() const +{ + return myArgv; +} diff --git a/src/Qtx/Qtx.h b/src/Qtx/Qtx.h index 423735c56..807002e74 100755 --- a/src/Qtx/Qtx.h +++ b/src/Qtx/Qtx.h @@ -154,6 +154,20 @@ public: QString myCurLocale; }; + class QTX_EXPORT CmdLineArgs + { + public: + CmdLineArgs(); + ~CmdLineArgs(); + + int argc() const; + char** argv() const; + + private: + int myArgc; + char** myArgv; + }; + class QTX_EXPORT BackgroundData { public: diff --git a/src/SalomeApp/SalomeApp_Application.cxx b/src/SalomeApp/SalomeApp_Application.cxx index 8927c1630..fd988572e 100644 --- a/src/SalomeApp/SalomeApp_Application.cxx +++ b/src/SalomeApp/SalomeApp_Application.cxx @@ -1318,19 +1318,9 @@ CORBA::ORB_var SalomeApp_Application::orb() static CORBA::ORB_var _orb; if ( CORBA::is_nil( _orb ) ) { - QStringList args = QApplication::arguments(); - int argc = args.size(); - std::vector args1(argc); - char** argv = new char*[argc]; - for ( int i = 0; i < argc; ++i ) { - args1[i] = args[i].toStdString(); - argv[i] = const_cast( args1[i].c_str() ); - } - + Qtx::CmdLineArgs args; ORB_INIT& init = *SINGLETON_::Instance(); - _orb = init( argc, argv ); - - delete [] argv; + _orb = init( args.argc(), args.argv() ); } return _orb; diff --git a/src/SalomeApp/SalomeApp_Engine_i.cxx b/src/SalomeApp/SalomeApp_Engine_i.cxx index 956993d33..a7bbf1af9 100644 --- a/src/SalomeApp/SalomeApp_Engine_i.cxx +++ b/src/SalomeApp/SalomeApp_Engine_i.cxx @@ -305,19 +305,9 @@ CORBA::ORB_var SalomeApp_Engine_i::orb() static CORBA::ORB_var _orb; if ( CORBA::is_nil( _orb ) ) { - QStringList args = QApplication::arguments(); - int argc = args.size(); - std::vector args1(argc); - char** argv = new char*[argc]; - for ( int i = 0; i < argc; ++i ) { - args1[i] = args[i].toStdString(); - argv[i] = const_cast( args1[i].c_str() ); - } - + Qtx::CmdLineArgs args; ORB_INIT& init = *SINGLETON_::Instance(); - _orb = init( argc, argv ); - - delete [] argv; + _orb = init( args.argc(), args.argv() ); } return _orb; diff --git a/src/Session/Session_ServerCheck.cxx b/src/Session/Session_ServerCheck.cxx index 3cb7fa8a5..c0d3203fa 100644 --- a/src/Session/Session_ServerCheck.cxx +++ b/src/Session/Session_ServerCheck.cxx @@ -37,6 +37,7 @@ #include "SALOME_NamingService.hxx" #include "Basics_Utils.hxx" #include "utilities.h" +#include "Qtx.h" #include #include @@ -261,27 +262,17 @@ void Session_ServerCheck::run() // start check servers int current = 0; QString error; - - QStringList args = QApplication::arguments(); - int argc = args.size(); - std::vector args1(argc); - char** argv = new char*[argc]; - for ( int i = 0; i < argc; ++i ) { - args1[i] = args[i].toStdString(); - argv[i] = const_cast( args1[i].c_str() ); - } - - bool OK = true; + Qtx::CmdLineArgs args; // 1. Check naming service - for ( int i = 0; (i < myAttempts) && OK; i++ ) { + for ( int i = 0; i < myAttempts; i++ ) { Locker locker( this ); setStep( current * myAttempts + i ); try { ORB_INIT& init = *SINGLETON_::Instance(); - CORBA::ORB_var orb = init( argc, argv ); + CORBA::ORB_var orb = init( args.argc(), args.argv() ); CORBA::Object_var obj = orb->resolve_initial_references( "NameService" ); CosNaming::NamingContext_var _root_context = CosNaming::NamingContext::_narrow( obj ); if ( !CORBA::is_nil( _root_context ) ) { @@ -298,19 +289,19 @@ void Session_ServerCheck::run() if ( i == myAttempts-1 ) { setError( tr( "Unable to contact the naming service.\n" ) ); - OK = false; - //return; + return; } } // 2. Check registry server - for ( int i = 0; (i < myAttempts) && OK ; i++ ) { + for ( int i = 0; i < myAttempts ; i++ ) { Locker locker( this ); setStep( current * myAttempts + i ); try { - CORBA::ORB_var orb = CORBA::ORB_init( argc, argv ); + ORB_INIT& init = *SINGLETON_::Instance(); + CORBA::ORB_var orb = init( args.argc(), args.argv() ); SALOME_NamingService &NS = *SINGLETON_::Instance(); ASSERT( SINGLETON_::IsAlreadyExisting() ); NS.init_orb( orb ); @@ -347,19 +338,19 @@ void Session_ServerCheck::run() if ( i == myAttempts-1 ) { setError( tr( "Registry server is not found.\n%1" ).arg ( error ) ); - OK = false; - //return; + return; } } // 3. Check data server - for ( int i = 0; (i < myAttempts) && OK ; i++ ) { + for ( int i = 0; i < myAttempts ; i++ ) { Locker locker( this ); setStep( current * myAttempts + i ); try { - CORBA::ORB_var orb = CORBA::ORB_init( argc, argv ); + ORB_INIT& init = *SINGLETON_::Instance(); + CORBA::ORB_var orb = init( args.argc(), args.argv() ); SALOME_NamingService &NS = *SINGLETON_::Instance(); ASSERT( SINGLETON_::IsAlreadyExisting() ); NS.init_orb( orb ); @@ -396,19 +387,19 @@ void Session_ServerCheck::run() if ( i == myAttempts-1 ) { setError( tr( "Study server is not found.\n%1" ).arg ( error ) ); - OK = false; - //return; + return; } } // 4. Check module catalogue server - for ( int i = 0; (i < myAttempts) && OK ; i++ ) { + for ( int i = 0; i < myAttempts ; i++ ) { Locker locker( this ); setStep( current * myAttempts + i ); try { - CORBA::ORB_var orb = CORBA::ORB_init( argc, argv ); + ORB_INIT& init = *SINGLETON_::Instance(); + CORBA::ORB_var orb = init( args.argc(), args.argv() ); SALOME_NamingService &NS = *SINGLETON_::Instance(); ASSERT( SINGLETON_::IsAlreadyExisting() ); NS.init_orb( orb ); @@ -445,19 +436,19 @@ void Session_ServerCheck::run() if ( i == myAttempts-1 ) { setError( tr( "Module catalogue server is not found.\n%1" ).arg ( error ) ); - OK = false; - //return; + return; } } // 5. Check data server - for ( int i = 0; (i < myAttempts) && OK ; i++ ) { + for ( int i = 0; i < myAttempts ; i++ ) { Locker locker( this ); setStep( current * myAttempts + i ); try { - CORBA::ORB_var orb = CORBA::ORB_init( argc, argv ); + ORB_INIT& init = *SINGLETON_::Instance(); + CORBA::ORB_var orb = init( args.argc(), args.argv() ); SALOME_NamingService &NS = *SINGLETON_::Instance(); ASSERT( SINGLETON_::IsAlreadyExisting() ); NS.init_orb( orb ); @@ -494,20 +485,20 @@ void Session_ServerCheck::run() if ( i == myAttempts-1 ) { setError( tr( "Session server is not found.\n%1" ).arg ( error ) ); - OK = false; - //return; + return; } } // 6. Check C++ container if ( myCheckCppContainer ) { - for ( int i = 0; (i < myAttempts) && OK ; i++ ) { + for ( int i = 0; i < myAttempts ; i++ ) { Locker locker( this ); setStep( current * myAttempts + i ); try { - CORBA::ORB_var orb = CORBA::ORB_init( argc, argv ); + ORB_INIT& init = *SINGLETON_::Instance(); + CORBA::ORB_var orb = init( args.argc(), args.argv() ); SALOME_NamingService &NS = *SINGLETON_::Instance(); ASSERT( SINGLETON_::IsAlreadyExisting() ); NS.init_orb( orb ); @@ -545,21 +536,21 @@ void Session_ServerCheck::run() if ( i == myAttempts-1 ) { setError( tr( "C++ container is not found.\n%1" ).arg ( error ) ); - OK = false; - //return; + return; } } } // 7. Check Python container if ( myCheckPyContainer ) { - for ( int i = 0; (i < myAttempts) && OK ; i++ ) { + for ( int i = 0; i < myAttempts ; i++ ) { Locker locker( this ); setStep( current * myAttempts + i ); try { - CORBA::ORB_var orb = CORBA::ORB_init( argc, argv ); + ORB_INIT& init = *SINGLETON_::Instance(); + CORBA::ORB_var orb = init( args.argc(), args.argv() ); SALOME_NamingService &NS = *SINGLETON_::Instance(); ASSERT( SINGLETON_::IsAlreadyExisting() ); NS.init_orb( orb ); @@ -597,21 +588,21 @@ void Session_ServerCheck::run() if ( i == myAttempts-1 ) { setError( tr( "Python container is not found.\n%1" ).arg ( error ) ); - OK = false; - //return; + return; } } } // 8. Check supervision container if ( myCheckSVContainer ) { - for ( int i = 0; (i < myAttempts) && OK ; i++ ) { + for ( int i = 0; i < myAttempts ; i++ ) { Locker locker( this ); setStep( current * myAttempts + i ); try { - CORBA::ORB_var orb = CORBA::ORB_init( argc, argv ); + ORB_INIT& init = *SINGLETON_::Instance(); + CORBA::ORB_var orb = init( args.argc(), args.argv() ); SALOME_NamingService &NS = *SINGLETON_::Instance(); ASSERT( SINGLETON_::IsAlreadyExisting() ); NS.init_orb( orb ); @@ -649,11 +640,8 @@ void Session_ServerCheck::run() if ( i == myAttempts-1 ) { setError( tr( "Supervision container is not found.\n%1" ).arg ( error ) ); - OK = false; - //return; + return; } } } - - delete [] argv; } -- 2.39.2