From ff6a5b3c34c3948c9174fa9b189562b2e99556bb Mon Sep 17 00:00:00 2001 From: asl Date: Wed, 25 Nov 2009 12:40:13 +0000 Subject: [PATCH] exceptions are applied --- src/Notebook/SALOME_Parameter.cxx | 123 +++++++++++++++++++++++++++--- src/Notebook/SALOME_Parameter.hxx | 6 ++ 2 files changed, 119 insertions(+), 10 deletions(-) diff --git a/src/Notebook/SALOME_Parameter.cxx b/src/Notebook/SALOME_Parameter.cxx index 39b461858..e99d259f8 100644 --- a/src/Notebook/SALOME_Parameter.cxx +++ b/src/Notebook/SALOME_Parameter.cxx @@ -50,7 +50,7 @@ SALOME_Parameter::SALOME_Parameter( SALOME_Notebook* theNotebook, const std::str { if( isExpr ) { - myExpr.setExpression( theData ); + InternalSetExpression( theData ); Update( SALOME::Notebook::_nil() ); } else @@ -104,19 +104,15 @@ void SALOME_Parameter::Update( SALOME::Notebook_ptr /*theNotebook*/ ) std::string aName = *it; SALOME_Parameter* aParam = myNotebook->GetParameterPtr( const_cast( aName.c_str() ) ); if( aParam ) - { //printf( "\tset %s = %lf\n", aName.c_str(), aParam->AsReal() ); myExpr.parser()->setParameter( aName, aParam->myResult ); - } else - { - myResult = SALOME_EvalVariant(); - return; - } + ThrowError( false, arg( "Parameter '%1' is not declared yet", aName ) ); } //2. Calculate myResult = myExpr.calculate(); + AnalyzeError(); //printf( "\tresult = %lf\n", AsReal() ); } } @@ -128,7 +124,7 @@ void SALOME_Parameter::SetExpression( const char* theExpr ) else { - myExpr.setExpression( theExpr ); + InternalSetExpression( theExpr ); myIsCalculable = true; myNotebook->SetToUpdate( _this() ); } @@ -216,13 +212,19 @@ char* SALOME_Parameter::AsString() CORBA::Long SALOME_Parameter::AsInteger() { bool ok; - return myResult.toInt( &ok ); + int aRes = myResult.toInt( &ok ); + if( !ok ) + ThrowTypeError( "SALOME_Parameter::AsInteger(): " ); + return aRes; } CORBA::Double SALOME_Parameter::AsReal() { bool ok; - return myResult.toDouble( &ok ); + double aRes = myResult.toDouble( &ok ); + if( !ok ) + ThrowTypeError( "SALOME_Parameter::AsReal(): " ); + return aRes; } CORBA::Boolean SALOME_Parameter::AsBoolean() @@ -299,3 +301,104 @@ void SALOME_Parameter::Substitute( const std::string& theName, const SALOME_Eval myName = aNewName; } } + +void SALOME_Parameter::ThrowTypeError( const std::string& theMsg ) +{ + std::string aMsg = theMsg; + aMsg += "type is "; + switch( GetType() ) + { + case SALOME::TBoolean: + aMsg += "boolean"; + break; + case SALOME::TInteger: + aMsg += "integer"; + break; + case SALOME::TReal: + aMsg += "real"; + break; + case SALOME::TString: + aMsg += "string"; + break; + default: + aMsg += "unknown"; + break; + } + + SALOME::TypeError anError; + anError.Reason = CORBA::string_dup( aMsg.c_str() ); + throw anError; +} + +void SALOME_Parameter::InternalSetExpression( const std::string& theExpr ) +{ + myExpr.setExpression( theExpr ); + AnalyzeError(); +} + +void SALOME_Parameter::AnalyzeError() +{ + std::string aMsg; + SALOME_EvalExprError anError = myExpr.parser()->error(); + bool isCalcError = true; + switch( anError ) + { + case EvalExpr_OperandsNotMatch: + aMsg = "Types of arguments are invalid"; + break; + case EvalExpr_InvalidResult: + aMsg = "Invalid result"; + break; + case EvalExpr_InvalidOperation: + aMsg = "Invalid operation"; + isCalcError = false; + break; + case EvalExpr_OperationsNull: + aMsg = "Internal error"; + isCalcError = false; + break; + case EvalExpr_InvalidToken: + aMsg = "Invalid token"; + isCalcError = false; + break; + case EvalExpr_CloseExpected: + aMsg = "A closing bracket is expecting"; + isCalcError = false; + break; + case EvalExpr_ExcessClose: + aMsg = "There is an excess closing bracket"; + isCalcError = false; + break; + case EvalExpr_BracketsNotMatch: + aMsg = "Brackets of different types"; + isCalcError = false; + break; + case EvalExpr_StackUnderflow: + aMsg = "Stack underflow (missing arguments of operation?)"; + isCalcError = false; + break; + case EvalExpr_ExcessData: + aMsg = "Several expressions"; + isCalcError = false; + break; + } + + if( aMsg.length() > 0 ) + ThrowError( isCalcError, aMsg ); +} + +void SALOME_Parameter::ThrowError( bool isCalc, const std::string& theMsg ) +{ + if( isCalc ) + { + SALOME::CalculationError anError; + anError.Reason = CORBA::string_dup( theMsg.c_str() ); + throw anError; + } + else + { + SALOME::ExpressionError anError; + anError.Reason = CORBA::string_dup( theMsg.c_str() ); + throw anError; + } +} diff --git a/src/Notebook/SALOME_Parameter.hxx b/src/Notebook/SALOME_Parameter.hxx index a4f96b629..1630a0435 100644 --- a/src/Notebook/SALOME_Parameter.hxx +++ b/src/Notebook/SALOME_Parameter.hxx @@ -82,6 +82,12 @@ public: std::string Expression() const; void Substitute( const std::string& theName, const SALOME_EvalExpr& theExpr ); +private: + void ThrowTypeError( const std::string& theMsg ); + void ThrowError( bool isCalc, const std::string& theMsg ); + void InternalSetExpression( const std::string& theExpr ); + void AnalyzeError(); + private: SALOME_Notebook* myNotebook; std::string myName; -- 2.39.2