From 3b77b2b4a083625f411f1e1de1be89dc5d98a50a Mon Sep 17 00:00:00 2001 From: pkv Date: Thu, 12 Nov 2009 06:15:38 +0000 Subject: [PATCH] Implementation of expression evaluator --- src/Notebook/Makefile.am | 18 +- src/Notebook/SALOME_Eval.hxx | 62 ++ src/Notebook/SALOME_EvalExpr.cxx | 175 ++++ src/Notebook/SALOME_EvalExpr.hxx | 67 ++ src/Notebook/SALOME_EvalParser.cxx | 1270 ++++++++++++++++++++++++++ src/Notebook/SALOME_EvalParser.hxx | 172 ++++ src/Notebook/SALOME_EvalSet.cxx | 1281 +++++++++++++++++++++++++++ src/Notebook/SALOME_EvalSet.hxx | 276 ++++++ src/Notebook/SALOME_EvalVariant.cxx | 501 +++++++++++ src/Notebook/SALOME_EvalVariant.hxx | 263 ++++++ 10 files changed, 4080 insertions(+), 5 deletions(-) create mode 100755 src/Notebook/SALOME_Eval.hxx create mode 100755 src/Notebook/SALOME_EvalExpr.cxx create mode 100755 src/Notebook/SALOME_EvalExpr.hxx create mode 100755 src/Notebook/SALOME_EvalParser.cxx create mode 100755 src/Notebook/SALOME_EvalParser.hxx create mode 100755 src/Notebook/SALOME_EvalSet.cxx create mode 100755 src/Notebook/SALOME_EvalSet.hxx create mode 100755 src/Notebook/SALOME_EvalVariant.cxx create mode 100755 src/Notebook/SALOME_EvalVariant.hxx diff --git a/src/Notebook/Makefile.am b/src/Notebook/Makefile.am index bc521c199..8d002e0e0 100644 --- a/src/Notebook/Makefile.am +++ b/src/Notebook/Makefile.am @@ -32,10 +32,14 @@ include $(top_srcdir)/salome_adm/unix/make_common_starter.am # # header files salomeinclude_HEADERS = SALOME_ParameterizedObject.hxx \ - SALOME_Parameter.hxx \ - SALOME_Notebook.hxx \ - SALOME_IndexMap.hxx - + SALOME_Parameter.hxx \ + SALOME_Notebook.hxx \ + SALOME_Eval.hxx \ + SALOME_EvalExpr.hxx \ + SALOME_EvalParser.hxx \ + SALOME_EvalSet.hxx \ + SALOME_EvalVariant.hxx + # Scripts to be installed dist_salomescript_PYTHON= @@ -68,7 +72,11 @@ COMMON_LIBS = lib_LTLIBRARIES = libSalomeNotebook.la libSalomeNotebook_la_SOURCES = SALOME_ParameterizedObject.cxx \ SALOME_Parameter.cxx \ - SALOME_Notebook.cxx + SALOME_Notebook.cxx \ + SALOME_EvalExpr.cxx \ + SALOME_EvalParser.cxx \ + SALOME_EvalSet.cxx \ + SALOME_EvalVariant.cxx libSalomeNotebook_la_CPPFLAGS = $(COMMON_CPPFLAGS) libSalomeNotebook_la_LDFLAGS = -Wl,-E -no-undefined -version-info=0:0:0 @LDEXPDYNFLAGS@ diff --git a/src/Notebook/SALOME_Eval.hxx b/src/Notebook/SALOME_Eval.hxx new file mode 100755 index 000000000..77b3800da --- /dev/null +++ b/src/Notebook/SALOME_Eval.hxx @@ -0,0 +1,62 @@ +// Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File : SALOME_Eval.hxx +// Author : Peter KURNEV +// Module : SALOME + + +#ifndef SALOME_Eval_Header_File +#define SALOME_Eval_Header_File + +#ifdef WNT +#pragma warning(disable : 4786) +#endif + +#include +#include +#include +// +using namespace std; + +typedef string RString; +typedef list RStringList; +typedef RStringList::iterator ListIteratorOfRStringList; +typedef set SetOfRString; +typedef SetOfRString::iterator SetIteratorOfSetOfRString; +typedef pair< SetIteratorOfSetOfRString, bool > SetPair; + +typedef enum + { + SALOME_EvalExpr_OK, //!< No errors found + SALOME_EvalExpr_OperandsNotMatch, //!< Types of arguments are invalid for this operation + SALOME_EvalExpr_InvalidResult, //!< Operation cannot find result (for example, division by zero) + SALOME_EvalExpr_InvalidOperation, //!< Unknown operation + SALOME_EvalExpr_OperationsNull, //!< Internal operations pointer of parser is null + SALOME_EvalExpr_InvalidToken, //!< Invalid token (neither operation, nor parameter of value) + SALOME_EvalExpr_CloseExpected, //!< Closing bracket is expected + SALOME_EvalExpr_ExcessClose, //!< Extra closing bracket is found + SALOME_EvalExpr_BracketsNotMatch, //!< Opening and closing brackets are of different type, e.g. [) + SALOME_EvalExpr_StackUnderflow, //!< There are no arguments in the stack for the operation + SALOME_EvalExpr_ExcessData //!< The parsing is finished, but there are more then one value in the stack + } SALOME_EvalExprError; + +#endif diff --git a/src/Notebook/SALOME_EvalExpr.cxx b/src/Notebook/SALOME_EvalExpr.cxx new file mode 100755 index 000000000..177d4190b --- /dev/null +++ b/src/Notebook/SALOME_EvalExpr.cxx @@ -0,0 +1,175 @@ +// Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File : SALOME_EvalExpr.cxx +// Author : Peter KURNEV +// Module : SALOME + + + +#include + +//======================================================================= +//function : +//purpose : +//======================================================================= +SALOME_EvalExpr::SALOME_EvalExpr(const RString& expr) +{ + myParser=NULL; + intialize( true, expr ); +} +//======================================================================= +//function : +//purpose : +//======================================================================= +SALOME_EvalExpr::SALOME_EvalExpr(const bool stdSets, const RString& expr) +{ + myParser=NULL; + intialize( stdSets, expr ); +} +//======================================================================= +//function : ~ +//purpose : +//======================================================================= +SALOME_EvalExpr::~SALOME_EvalExpr() +{ + if (myParser) { + delete myParser; + } +} +//======================================================================= +//function : error +//purpose : +//======================================================================= +SALOME_EvalExprError SALOME_EvalExpr::error() const +{ + return myParser->error(); +} +//======================================================================= +//function : intialize +//purpose : +//======================================================================= +void SALOME_EvalExpr::intialize(const bool stdSets, const RString& expr) +{ + if (myParser) { + delete myParser; + } + + myParser = new SALOME_EvalParser(); + if ( stdSets ) { + myParser->setAutoDeleteOperationSets( true ); + myParser->insertOperationSet( new SALOME_EvalSetLogic() ); + myParser->insertOperationSet( new SALOME_EvalSetArithmetic() ); + myParser->insertOperationSet( new SALOME_EvalSetString() ); + myParser->insertOperationSet( new SALOME_EvalSetMath() ); + myParser->insertOperationSet( new SALOME_EvalSetSets() ); + myParser->insertOperationSet( new SALOME_EvalSetConst() ); + } + setExpression( expr ); +} +//======================================================================= +//function : calculate +//purpose : +//======================================================================= +SALOME_EvalVariant SALOME_EvalExpr::calculate(const RString& expr) +{ + if ( !expr.empty() ) { + setExpression( expr ); + } + return myParser->calculate(); +} +//======================================================================= +//function : expression +//purpose : +//======================================================================= +RString SALOME_EvalExpr::expression() const +{ + return myExpr; +} +//======================================================================= +//function : setExpression +//purpose : +//======================================================================= +void SALOME_EvalExpr::setExpression(const RString& expr) +{ + if ( expr == expression() ){ + return; + } + myExpr = expr; + myParser->setExpression( myExpr ); +} +//======================================================================= +//function : parser +//purpose : +//======================================================================= +SALOME_EvalParser* SALOME_EvalExpr::parser() const +{ + return myParser; +} + +//======================================================================= +//function : operationSets +//purpose : +//======================================================================= +SALOME_ListOfPEvalSet SALOME_EvalExpr::operationSets() const +{ + return myParser->operationSets(); +} +//======================================================================= +//function : insertOperationSet +//purpose : +//======================================================================= +void SALOME_EvalExpr::insertOperationSet(SALOME_EvalSet* theSet, const int idx) +{ + myParser->insertOperationSet(theSet, idx); +} +//======================================================================= +//function : removeOperationSet +//purpose : +//======================================================================= +void SALOME_EvalExpr::removeOperationSet(SALOME_EvalSet* theSet) +{ + myParser->removeOperationSet(theSet); +} +//======================================================================= +//function : operationSet +//purpose : +//======================================================================= +SALOME_EvalSet* SALOME_EvalExpr::operationSet(const RString& name) const +{ + return myParser->operationSet( name ); +} +//======================================================================= +//function : autoDeleteOperationSets +//purpose : +//======================================================================= +bool SALOME_EvalExpr::autoDeleteOperationSets() const +{ + return myParser->autoDeleteOperationSets(); +} +//======================================================================= +//function : setAutoDeleteOperationSets +//purpose : +//======================================================================= +void SALOME_EvalExpr::setAutoDeleteOperationSets(const bool on) +{ + myParser->setAutoDeleteOperationSets( on ); +} diff --git a/src/Notebook/SALOME_EvalExpr.hxx b/src/Notebook/SALOME_EvalExpr.hxx new file mode 100755 index 000000000..756e4b350 --- /dev/null +++ b/src/Notebook/SALOME_EvalExpr.hxx @@ -0,0 +1,67 @@ +// Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File : SALOME_EvalExpr.hxx +// Author : Peter KURNEV +// Module : SALOME + +#ifndef SALOME_EvalExpr_Header_File +#define SALOME_EvalExpr_Header_File + +#include +#include +#include +#include + +class SALOME_EvalExpr +{ +public: + SALOME_EvalExpr(const RString& = RString()); + SALOME_EvalExpr( const bool, const RString& = RString() ); + ~SALOME_EvalExpr(); + + SALOME_EvalVariant calculate( const RString& = RString() ); + + RString expression() const; + void setExpression( const RString& ); + + SALOME_EvalExprError error() const; + SALOME_EvalParser* parser() const; + + bool autoDeleteOperationSets() const; + void setAutoDeleteOperationSets( const bool ); + + SALOME_ListOfPEvalSet operationSets() const; + SALOME_EvalSet* operationSet( const RString& ) const; + void removeOperationSet(SALOME_EvalSet* ); + void insertOperationSet(SALOME_EvalSet*, + const int = -1 ); + +private: + void intialize(const bool, + const RString& ); + +private: + RString myExpr; + SALOME_EvalParser* myParser; +}; + +#endif diff --git a/src/Notebook/SALOME_EvalParser.cxx b/src/Notebook/SALOME_EvalParser.cxx new file mode 100755 index 000000000..8d9dfb180 --- /dev/null +++ b/src/Notebook/SALOME_EvalParser.cxx @@ -0,0 +1,1270 @@ +// Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File : SALOME_EvalParser.cxx +// Author : Peter KURNEV +// Module : SALOME + +#include + +#include +#include + +static + bool contains(const RStringList& aLS, + const RString& aS); +static + RString trimmed(const RString& str); + +static + RString trimmed(const RString& str, const char aWhat); + +static + bool isSpace(const char aC); + +static + int indexOf(const RStringList& aLS, + const RString& aS); + +//======================================================================= +//function : +//purpose : +//======================================================================= +SALOME_EvalParser::SALOME_EvalParser() +: myAutoDel( false ) +{ + setError( SALOME_EvalExpr_OK ); +} +//======================================================================= +//function : ~ +//purpose : +//======================================================================= +SALOME_EvalParser::~SALOME_EvalParser() +{ + if (autoDeleteOperationSets()) { + //qDeleteAll( mySets );// !!! + SALOME_ListOfPEvalSet::const_iterator aIt = mySets.begin(); + for (; aIt != mySets.end() ; ++aIt ) { + SALOME_EvalSet* pEvalSet=*aIt; + if (pEvalSet) { + delete pEvalSet; + pEvalSet=NULL; + } + } + } +} +//======================================================================= +//function : operationSets +//purpose : +//======================================================================= +SALOME_ListOfPEvalSet SALOME_EvalParser::operationSets() const +{ + return mySets; +} +//======================================================================= +//function : operationSet +//purpose : +//======================================================================= +SALOME_EvalSet* SALOME_EvalParser::operationSet(const RString& theName) const +{ + SALOME_EvalSet* pSet; + // + pSet = NULL; + SALOME_ListOfPEvalSet::const_iterator it = mySets.begin(); + for (; it != mySets.end() && !pSet; ++it ) { + if ( (*it)->name()==theName ) { + pSet = *it; + } + } + return pSet; +} +//======================================================================= +//function : insertOperationSet +//purpose : +//======================================================================= +void SALOME_EvalParser::insertOperationSet(SALOME_EvalSet* theSet, + const int idx) +{ + if (SALOME_EvalSet::contains(mySets, theSet)) { + return; + } + // + int iSize=(int)mySets.size(); + int index = idx < 0 ? iSize : idx; + if (index>iSize) { + index=iSize; + } + SALOME_EvalSet::insert(mySets, index, theSet); +} +//======================================================================= +//function : removeOperationSet +//purpose : +//======================================================================= +void SALOME_EvalParser::removeOperationSet(SALOME_EvalSet* theSet) +{ + //mySets.removeAll( theSet ); + mySets.remove(theSet); +} +//======================================================================= +//function : autoDeleteOperationSets +//purpose : +//======================================================================= +bool SALOME_EvalParser::autoDeleteOperationSets() const +{ + return myAutoDel; +} +//======================================================================= +//function : setAutoDeleteOperationSets +//purpose : +//======================================================================= +void SALOME_EvalParser::setAutoDeleteOperationSets( const bool theOn ) +{ + myAutoDel = theOn; +} +//======================================================================= +//function : search +//purpose : +//======================================================================= +int SALOME_EvalParser::search(const RStringList& aList, + const RString& aStr, + int offset, + int& matchLen, + int& listind ) +{ + int min = -1; + int ind = 0; + int pos; + //static const basic_string ::size_type npos = -1; + // + RStringList::const_iterator aIt = aList.begin(), aLast = aList.end(); + for (ind = 0; aIt != aLast; aIt++, ind++ ) { + const RString& aStrX=*aIt; + + pos=(int)aStr.find(aStrX, offset); + if ( pos >= 0 && + ( min < 0 || min > pos || + ( min == pos && matchLen < (int)aStrX.length()) + ) + ) { + min = pos; + listind = ind; + matchLen = (int)aStrX.length(); + } + + } + if ( min < 0 ){ + matchLen = 0; + } + return min; +} +//======================================================================= +//function : note +//purpose : +//======================================================================= +RString SALOME_EvalParser::note(const RString& aStr, int pos, int len ) +{ + RString aStr1; + // + aStr1=aStr.substr(pos, len); + aStr1=trimmed(aStr1); + + return aStr1; +} + +//======================================================================= +//function : prepare +//purpose : +//======================================================================= +bool SALOME_EvalParser::prepare(const RString& expr, Postfix& post) +{ + int pos = 0; + int len =(int)expr.length(); + stack aBracketStack; + RStringList anOpers, anOpenBr, aCloseBr; + if ( !checkOperations() ) + return false; + + bracketsList( anOpenBr, true ); + bracketsList( aCloseBr, false ); + operationList( anOpers ); + + while ( pos < len && error() == SALOME_EvalExpr_OK ) { + PostfixItem item; + char aC=expr[pos]; + // + while ( pos < len && isSpace(aC) ) { + pos++; + } + // + if ( pos >= len ) { + break; + } + // + int mBrLen = 0, mLen = 0, br_ind = -1, op_ind = -1; + int oPos = search( anOpenBr, expr, pos, mBrLen, br_ind ); + int cPos = oPos == pos ? -1 : search( aCloseBr, expr, pos, mBrLen, br_ind ); + int opPos = search( anOpers, expr, pos, mLen, op_ind ); + // + if ( aC=='\'') { + int vpos = pos + 1; + while ( vpos < (int)expr.length() && expr[vpos] != '\'' ) { + vpos++; + } + // + mLen = vpos - pos + 1; + // + int res = createValue( note( expr, pos, mLen ), item.myValue ); + item.myType = res ? Value : Param; + post.push_back( item ); + pos = vpos + 1; + continue; + } + // + if ( oPos == pos ) { + aBracketStack.push( br_ind ); + item.myValue = note( expr, pos, mBrLen ); + item.myType = Open; + post.push_back( item ); + } + else if ( cPos == pos ) { + if ( aBracketStack.size() == 0 ) { + setError( SALOME_EvalExpr_ExcessClose ); + break; + } + if ( br_ind != aBracketStack.top() ) { + setError( SALOME_EvalExpr_BracketsNotMatch ); + break; + } + else { + aBracketStack.pop(); + item.myValue = note( expr, pos, mBrLen ); + item.myType = Close; + post.push_back( item ); + } + } + else { + mBrLen = 0; + } + // + if ( opPos == pos ) { + mBrLen = 0; + item.myValue = note( expr, pos, mLen ); + item.myType = Binary; + + if ( oPos == pos ) { + insert(post, (int)(post.size()-1), item); + } + else + post.push_back( item ); + } + else { + mLen = 0; + if ( oPos != pos && cPos != pos ) { + int i; + for ( i = pos + 1; i < (int)expr.length(); i++ ) { + if ( isSpace(expr[i]) ) { + break; + } + } + // + int vpos = i; + if ( oPos >= 0 && oPos < vpos ) { + vpos = oPos; + } + if ( cPos >= 0 && cPos < vpos ){ + vpos = cPos; + } + if ( opPos >= 0 && opPos < vpos ){ + vpos = opPos; + } + + while( vpos < (int)expr.length() && + ( isalpha(expr[vpos]) || + isdigit(expr[vpos]) || + expr[vpos]=='_' ) + ) { + vpos++; + } + // + mLen = vpos - pos; + bool res = createValue( note( expr, pos, mLen ), item.myValue ); + item.myType = res ? Value : Param; + post.push_back( item ); + } + } + pos += mBrLen + mLen; + } + + //Bracket checking + int brValue = 0; + for ( Postfix::iterator anIt = post.begin(); anIt != post.end(); ++anIt ) { + if ( (*anIt).myType == Open ){ + brValue++; + } + else if ( (*anIt).myType == Close ) { + if ( brValue > 0 ){ + brValue--; + } + else { + setError( SALOME_EvalExpr_ExcessClose ); + break; + } + } + } + // + if ( brValue > 0 ) { + setError( SALOME_EvalExpr_CloseExpected ); + } + // + return error() == SALOME_EvalExpr_OK; +} +//======================================================================= +//function : setOperationTypes +//purpose : +//======================================================================= +bool SALOME_EvalParser::setOperationTypes( Postfix& post ) +{ + if ( !checkOperations() ) + return false; + + RStringList anOpen, aClose; + // + bracketsList( anOpen, true ); + bracketsList( aClose, false ); + + Postfix::iterator aPrev, aNext; + Postfix::iterator anIt = post.begin(); + for (; anIt != post.end(); ++anIt ) { + aPrev = anIt; + aPrev--; + aNext = anIt; + aNext++; + if ( (*anIt).myType != Binary ){ + continue; + } + // + if ( ( anIt == post.begin() || (*aPrev).myType == Open || + (*aPrev).myType == Pre || (*aPrev).myType == Binary ) && aNext != post.end() && + ( (*aNext).myType == Value || (*aNext).myType == Param || + (*aNext).myType == Open || (*aNext).myType == Binary ) ) + (*anIt).myType = Pre; + // + else if ( anIt != post.begin() && ( (*aPrev).myType == Close || (*aPrev).myType == Param || + (*aPrev).myType == Value || (*aPrev).myType == Pre || + (*aPrev).myType == Post || (*aPrev).myType == Binary ) && + ( aNext == post.end() || (*aNext).myType == Close ) ) + (*anIt).myType = Post; + // + SALOME_EvalVariant& aRV=(*anIt).myValue; + RString aRVS=aRV.toString(); + // + if ( contains(anOpen, aRVS) ) { + (*anIt).myType = Pre; + } + else if ( contains(aClose, aRVS) ) { + (*anIt).myType = Post; + } + } + + return error() == SALOME_EvalExpr_OK; +} +//======================================================================= +//function : globalBrackets +//purpose : +//======================================================================= +int SALOME_EvalParser::globalBrackets(const Postfix& post, + int f, + int l ) +{ + int i; + int start_br = 0; + int fin_br = 0; + int br = 0; + int br_num = 0; + int min_br_num = (l-f+1)*5; + + PostfixItem *pPost; + pPost=new PostfixItem[post.size()]; + // + Postfix::const_iterator aIt = post.begin(); + for (i=0; aIt != post.end(); ++aIt, ++i) { + const PostfixItem& aPostfixItem=*aIt; + pPost[i]=aPostfixItem; + } + + for( i=f; i<=l; i++ ) + if( pPost[ i ].myType==SALOME_EvalParser::Open ) + start_br++; + else + break; + for( i=l; i>=f; i-- ) + if( pPost[ i ].myType==SALOME_EvalParser::Close ) + fin_br++; + else + break; + + br = start_br= 0 ) { + res.push_back(at(post,f)); + } + if ( l <= f ) { + return true; + } + + if ( !checkOperations() ) { + return false; + } + + int min = -1; + list argmin; + list min_types; + // + for ( int i = 0, j = f; j <= l; i++, j++ ) { + const PostfixItem& item = at(post,j);//!!! + PostfixItemType tt = item.myType; + if ( tt == Binary || tt == Pre || tt == Post ) { + int cur_pr = priority( item.myValue.toString(), tt == Binary ); + if ( cur_pr > 0 ) { + if ( min < 0 || min >= cur_pr ) { + if ( min == cur_pr ) { + argmin.push_back( f + i ); + min_types.push_back( tt ); + } + else { + min = cur_pr; + argmin.clear(); + argmin.push_back( f + i ); + min_types.clear(); + min_types.push_back( tt ); + } + } + } + else { + setError( SALOME_EvalExpr_InvalidOperation ); + break; + } + } + else if ( tt == Open ){ + RString opBr = item.myValue.toString(); + int ind, brValue = 0; + ind = indexOf(anOpen, opBr ); + while ( j <= l ) { + const PostfixItem& anItem = at(post,j); + if ( anItem.myType == Open ) + brValue++; + + if ( anItem.myType == Close ) { + brValue--; + RString clBr = anItem.myValue.toString(); + if ( indexOf(aClose, clBr ) == ind && brValue == 0 ){ + break; + } + } + i++; j++; + } + // + if ( brValue > 0 ) { + setError( SALOME_EvalExpr_CloseExpected ); + break; + } + } + } + // + if ( error() == SALOME_EvalExpr_OK ) { + if ( min >= 0 ) { + Postfix one; + list parts; + list::const_iterator anIt = argmin.begin(), aLast = argmin.end(); + bool ok = sort( post, one, anOpen, aClose, f, *anIt - 1 ); + parts.push_back( one ); + one.clear(); + for ( ; anIt != aLast && ok; anIt++ ) { + list::const_iterator aNext = anIt; aNext++; + ok = sort( post, one, anOpen, aClose, *anIt + 1, aNext == aLast ? l : *aNext - 1 ); + parts.push_back( one ); + one.clear(); + } + // + if ( !ok ){ + return false; + } + // + stack aStack; + list::const_iterator aPIt = parts.begin(); + list::const_iterator aTIt = min_types.begin(); + // + append(res,*aPIt); + aPIt++; + anIt = argmin.begin(); + for ( ; anIt != aLast; anIt++, aPIt++, aTIt++ ) { + if ( *aTIt == Pre ) { + if (!contains(anOpen, at(post,*anIt).myValue.toString())) { + append(res,*aPIt); + aStack.push( at(post, *anIt) ); + } + else { + res.push_back( at(post, *anIt) ); + append(res,*aPIt); + } + } + else { + append(res,*aPIt); + while (!aStack.empty() ) { + res.push_back( aStack.top() ); + aStack.pop(); + } + res.push_back( at(post, *anIt) ); + } + } + while ( !aStack.empty() ) { + res.push_back( aStack.top() ); + aStack.pop(); + } + } + else { //there are no operations + for ( int k = f; k <= l; k++ ) { + const PostfixItem& aItem=at(post, k); + if ( aItem.myType==Value || aItem.myType == Param ){ + res.push_back( aItem ); + } + } + } + } + return error() == SALOME_EvalExpr_OK; +} +//======================================================================= +//function : parse +//purpose : +//======================================================================= +bool SALOME_EvalParser::parse( const RString& expr ) +{ + myPostfix.clear(); + + if ( !checkOperations() ) + return false; + + Postfix p; + RStringList opens, closes; + + setError( SALOME_EvalExpr_OK ); + bracketsList( opens, true ); + bracketsList( closes, false ); + + return prepare( expr, p ) && setOperationTypes( p ) && sort( p, myPostfix, opens, closes ); +} +//======================================================================= +//function : calculate +//purpose : +//======================================================================= +bool SALOME_EvalParser::calculate(const RString& op, + SALOME_EvalVariant& v1, + SALOME_EvalVariant& v2 ) +{ + SALOME_EvalExprError aErr; + SALOME_EvalVariantType aType1, aType2; + // + aType1=v1.type(); + aType2=v2.type(); + // + aErr = isValid( op, aType1, aType2 ); + if ( aErr == SALOME_EvalExpr_OK ){ + aErr=calculation( op, v1, v2 ); + setError( aErr ); + } + else{ + setError( aErr ); + } + return error() == SALOME_EvalExpr_OK; +} +//======================================================================= +//function : calculate +//purpose : +//======================================================================= +SALOME_EvalVariant SALOME_EvalParser::calculate() +{ + if ( !checkOperations() ) + return SALOME_EvalVariant(); + + setError( SALOME_EvalExpr_OK ); + // + RStringList anOpen, aClose; + PostfixItemType aType; + // + bracketsList( anOpen, true ); + bracketsList( aClose, false ); + + stack aStack; + Postfix::iterator anIt = myPostfix.begin(), aLast = myPostfix.end(); + for ( ; anIt != aLast && error() == SALOME_EvalExpr_OK; anIt++ ) { + const PostfixItem& aPostfixItem=*anIt; + aType=aPostfixItem.myType; + RString nn = aPostfixItem.myValue.toString(); + // + if ( aType == Param ) { + if ( hasParameter( nn ) ) { + SALOME_EvalVariant& v = myParams[nn]; + if ( v.isValid() ) { + aStack.push( v ); + } + else { + setError( SALOME_EvalExpr_InvalidToken ); + } + } + else { + setError( SALOME_EvalExpr_InvalidToken ); + } + } + // + else if ( aType == Value ) { + aStack.push( (*anIt).myValue ); + } + // + else if ( aType == Pre || aType == Post ) { + if ( contains(anOpen, nn ) ) { + SALOME_EvalVariant inv; + if ( calculate( nn, inv, inv ) ) { + aStack.push( SALOME_EvalVariant() ); + } + } + else if ( contains(aClose, nn ) ) { + SALOME_ListOfEvalVariant aSet; + while ( true ) { + if ( aStack.empty() ) { + setError( SALOME_EvalExpr_StackUnderflow ); + break; + } + if ( aStack.top().isValid() ) { + aSet.push_back( aStack.top() ); + aStack.pop(); + } + else { + aStack.pop(); + break; + } + } + + SALOME_EvalVariant qSet = aSet, inv; + if ( calculate( nn, qSet, inv ) ) { + aStack.push( aSet ); + } + } + else if ( aStack.size() >= 1 ) { + SALOME_EvalVariant inv; + SALOME_EvalVariant *v1, *v2; + v1=&aStack.top(); + v2 = &inv; //"post-" case + if ( aType == Pre ) { + v2 = &aStack.top(); + v1 = &inv; + } + calculate( nn, *v1, *v2 ); + } + else { + setError( SALOME_EvalExpr_StackUnderflow ); + } + }//else if ( aType== Pre || aType == Post ) { + else if ( aType == Binary ) { + if ( aStack.size() >= 2 ) { + SALOME_EvalVariant v2= aStack.top(); + aStack.pop(); + calculate( nn, aStack.top(), v2 ); + } + else { + setError( SALOME_EvalExpr_StackUnderflow ); + } + } + } + // + SALOME_EvalVariant res; + if ( error() == SALOME_EvalExpr_OK ) { + int count; + + count = (int)aStack.size(); + if ( count == 0 ) { + setError( SALOME_EvalExpr_StackUnderflow ); + } + else if( count == 1 ) { + res = aStack.top(); + } + else{ + setError( SALOME_EvalExpr_ExcessData ); + } + } + return res; +} +//======================================================================= +//function : calculate +//purpose : +//======================================================================= +SALOME_EvalVariant SALOME_EvalParser::calculate( const RString& expr ) +{ + setExpression( expr ); + return calculate(); +} +//======================================================================= +//function : setExpression +//purpose : +//======================================================================= +bool SALOME_EvalParser::setExpression( const RString& expr ) +{ + return parse( expr ); +} +//======================================================================= +//function : hasParameter +//purpose : +//======================================================================= +bool SALOME_EvalParser::hasParameter( const RString& name ) const +{ + bool bRet; + RString aStr; + ParamMap::const_iterator aIt; + aStr=trimmed(name); + aIt=myParams.find(aStr); + bRet=(aIt!=myParams.end()); + return bRet; +} +//======================================================================= +//function : setParameter +//purpose : +//======================================================================= +void SALOME_EvalParser::setParameter( const RString& name, const SALOME_EvalVariant& value ) +{ + RString aStr; + // + aStr=trimmed(name); + PairParamMap aPair(aStr, value); + myParams.insert(aPair); +} +//======================================================================= +//function : removeParameter +//purpose : +//======================================================================= +bool SALOME_EvalParser::removeParameter( const RString& name ) +{ + int iRet; + RString aStr; + // + aStr=trimmed(name); + iRet=(int)myParams.erase(aStr); + return iRet? true : false; + +} +//======================================================================= +//function : parameter +//purpose : +//======================================================================= +SALOME_EvalVariant SALOME_EvalParser::parameter( const RString& theName ) const +{ + SALOME_EvalVariant res; + ParamMap::const_iterator aIt; + RString aStr; + aStr=trimmed(theName); + aIt=myParams.find(theName); + if (aIt!=myParams.end()) { + const PairParamMap& aVT=*aIt; + const SALOME_EvalVariant& aRV=aVT.second; + res=aRV; + } + return res; +} +//======================================================================= +//function : firstInvalid +//purpose : +//======================================================================= +bool SALOME_EvalParser::firstInvalid( RString& name ) const +{ + ParamMap::const_iterator aIt = myParams.begin(); + for (; aIt != myParams.end(); aIt++ ) { + const PairParamMap& aVT=*aIt; + const SALOME_EvalVariant& aRV=aVT.second; + if ( !aRV.isValid() ) { + name = aVT.first; + return true; + } + } + return false; +} +//======================================================================= +//function : removeInvalids +//purpose : +//======================================================================= +void SALOME_EvalParser::removeInvalids() +{ + ParamMap aM; + // + ParamMap::const_iterator aIt = myParams.begin(); + for (; aIt != myParams.end(); aIt++ ) { + const PairParamMap& aVT=*aIt; + const SALOME_EvalVariant& aRV=aVT.second; + if (aRV.isValid() ) { + aM.insert(aVT); + } + } + myParams.clear(); + myParams=aM; +} +//======================================================================= +//function : error +//purpose : +//======================================================================= +SALOME_EvalExprError SALOME_EvalParser::error() const +{ + return myError; +} +//======================================================================= +//function : setError +//purpose : +//======================================================================= + +void SALOME_EvalParser::setError(SALOME_EvalExprError err) +{ + myError = err; +} +//======================================================================= +//function : dump +//purpose : +//======================================================================= +RString SALOME_EvalParser::dump() const +{ + return dump( myPostfix ); +} +//======================================================================= +//function : dump +//purpose : +//======================================================================= +RString SALOME_EvalParser::dump( const Postfix& post ) const +{ + RString res; + // + if ( !checkOperations() ) { + return res; + } + // + Postfix::const_iterator anIt = post.begin(); + for (; anIt != post.end(); anIt++ ) { + if ((*anIt).myType == Value && + (*anIt).myValue.type() == SALOME_EvalVariant_String ) { + res += "'" + (*anIt).myValue.toString() + "'"; + } + else{ + res += (*anIt).myValue.toString(); + } + if ( (*anIt).myType == Pre ){ + res += "(pre)"; + } + else if ( (*anIt).myType == Post ){ + res += "(post)"; + } + else if ( (*anIt).myType == Binary ){ + res += "(bin)"; + } + + res += "_"; + } + + return res; +} +//======================================================================= +//function : parameters +//purpose : +//======================================================================= +RStringList SALOME_EvalParser::parameters() const +{ + RStringList lst; + Postfix::const_iterator anIt = myPostfix.begin(); + for (; anIt != myPostfix.end(); anIt++ ) { + if ( (*anIt).myType == Param ) { + RString name = (*anIt).myValue.toString(); + if ( !contains(lst, name ) ) { + lst.push_back( name ); + } + } + } + return lst; +} +//======================================================================= +//function : clearParameters +//purpose : +//======================================================================= +void SALOME_EvalParser::clearParameters() +{ + myParams.clear(); +} +//======================================================================= +//function : toString +//purpose : +//======================================================================= +RString SALOME_EvalParser::toString( const SALOME_ListOfEvalVariant& theList ) +{ + RString res = "set : [ "; + SALOME_ListOfEvalVariant::const_iterator aIt = theList.begin(); + for ( ; aIt != theList.end(); aIt++ ) + res += (*aIt).toString() + " "; + res += "]"; + return res; +} +//======================================================================= +//function : operationList +//purpose : +//======================================================================= +void SALOME_EvalParser::operationList( RStringList& theList ) const +{ + SALOME_ListOfPEvalSet::const_iterator aIt = mySets.begin(); + for (; aIt != mySets.end(); ++aIt ) { + RStringList custom; + SALOME_EvalSet* aSet = *aIt; + aSet->operationList( custom ); + RStringList::const_iterator sIt = custom.begin(); + for ( ; sIt != custom.end(); ++sIt ) { + if ( !contains(theList, *sIt ) ) { + theList.push_back( *sIt ); + } + } + } +} +//======================================================================= +//function : operationList +//purpose : +//======================================================================= +void SALOME_EvalParser::bracketsList(RStringList& theList, + bool open ) const +{ + SALOME_ListOfPEvalSet::const_iterator it = mySets.begin(); + for (; it != mySets.end(); ++it ) { + RStringList custom; + SALOME_EvalSet* aSet = *it; + aSet->bracketsList( custom, open ); + RStringList::const_iterator sIt = custom.begin(); + for (; sIt != custom.end(); ++sIt ) { + if ( !contains(theList, *sIt ) ) + theList.push_back( *sIt ); + } + } +} +//======================================================================= +//function : createValue +//purpose : +//======================================================================= +bool SALOME_EvalParser::createValue(const RString& str, + SALOME_EvalVariant& val) const +{ + bool ok = false; + // + SALOME_ListOfPEvalSet::const_iterator it = mySets.begin(); + for (; it != mySets.end() && !ok; ++it ) { + ok = (*it)->createValue( str, val ); + } + return ok; +} +//======================================================================= +//function : priority +//purpose : +//======================================================================= +int SALOME_EvalParser::priority(const RString& op, + bool isBin ) const +{ + int i = 0; + int priority = 0; + SALOME_ListOfPEvalSet::const_iterator it = mySets.begin(); + for (; it != mySets.end() && priority <= 0; ++it, i++ ){ + priority = (*it)->priority( op, isBin ); + } + return priority > 0 ? priority + i * 50 : 0; +} +//======================================================================= +//function : isValid +//purpose : +//======================================================================= +SALOME_EvalExprError SALOME_EvalParser::isValid(const RString& op, + const SALOME_EvalVariantType t1, + const SALOME_EvalVariantType t2 ) const +{ + SALOME_EvalExprError err = SALOME_EvalExpr_OK; + // + SALOME_ListOfPEvalSet::const_iterator it = mySets.begin(); + for (; it != mySets.end(); ++it ) { + SALOME_EvalSet *pSet=*it; + err = pSet->isValid( op, t1, t2 ); + if ( err == SALOME_EvalExpr_OK ){ + break; + } + } + return err; +} +//======================================================================= +//function : calculation +//purpose : +//======================================================================= +SALOME_EvalExprError SALOME_EvalParser::calculation(const RString& op, + SALOME_EvalVariant& v1, + SALOME_EvalVariant& v2 ) const +{ + SALOME_EvalVariant nv1, nv2; + SALOME_EvalSet *pSet; + SALOME_EvalVariantType aType1, aType2; + SALOME_EvalExprError aErr; + // + aType1=v1.type(); + aType2=v2.type(); + // + SALOME_ListOfPEvalSet::const_iterator aIt = mySets.begin(); + for (; aIt != mySets.end(); ++aIt ) { + pSet=*aIt; + nv1 = v1; + nv2 = v2; + aErr=pSet->isValid( op, aType1, aType2); + if ( aErr == SALOME_EvalExpr_OK ) { + aErr = pSet->calculate( op, nv1, nv2 ); + if ( aErr == SALOME_EvalExpr_OK || aErr == SALOME_EvalExpr_InvalidResult ) { + v1 = nv1; + v2 = nv2; + return aErr; + } + } + } + // + return SALOME_EvalExpr_InvalidOperation; +} +//======================================================================= +//function : checkOperations +//purpose : +//======================================================================= +bool SALOME_EvalParser::checkOperations() const +{ + if ( mySets.size() ) + return true; + + SALOME_EvalParser* that = (SALOME_EvalParser*)this; + that->setError( SALOME_EvalExpr_OperationsNull ); + return false; +} +//======================================================================= +//function : append +//purpose : +//======================================================================= +void SALOME_EvalParser::append(Postfix& aL, + const Postfix& aL1) +{ + Postfix::const_iterator aIt=aL1.begin(); + for(; aIt!=aL1.end(); ++aIt) { + aL.push_back(*aIt); + } +} +//======================================================================= +//function : at +//purpose : +//======================================================================= +const SALOME_EvalParser::PostfixItem& SALOME_EvalParser::at(const Postfix& aL, + const int aIndex) +{ + int i; + Postfix::const_iterator aIt=aL.begin(); + for(i=0; aIt!=aL.end(); ++aIt, ++i) { + if (i==aIndex) { + break; + } + } + return *aIt; +} +//======================================================================= +//function : insert +//purpose : +//======================================================================= +void SALOME_EvalParser::insert(Postfix& aL, + const int aIndex, + PostfixItem& pS) +{ + int i; + // + if (!aIndex) { + aL.push_front(pS); + } + else if (aIndex==aL.size()) { + aL.push_back(pS); + } + else { + Postfix::iterator aIt=aL.begin(); + for(i=0; aIt!=aL.end(); ++aIt, ++i) { + if (i==aIndex) { + aL.insert( aIt, pS ); + break; + } + } + } +} +//======================================================================= +//function : indexOf +//purpose : +//======================================================================= +int indexOf(const RStringList& aLS, + const RString& aS) +{ + int i, iRet; + RStringList::const_iterator aIt; + // + iRet=-1; + // + aIt=aLS.begin(); + for (i=0; aIt!=aLS.end(); ++aIt, ++i) { + const RString aSx=*aIt; + if (aSx==aS) { + iRet=i; + break; + } + } + return iRet; +} +//======================================================================= +//function : contains +//purpose : +//======================================================================= +bool contains(const RStringList& aLS, + const RString& aS) +{ + bool bRet; + // + bRet=false; + RStringList::const_iterator aIt; + // + aIt=aLS.begin(); + for (; aIt!=aLS.end(); ++aIt) { + const RString aSx=*aIt; + if (aSx==aS) { + bRet=!bRet; + break; + } + } + return bRet; +} + + +//======================================================================= +//function : isSpace +//purpose : +//======================================================================= +bool isSpace(const char aC) +{ + bool bRet; + char aWhat[]={ + '\t', '\n', '\v', '\f', '\r', ' ' + }; + int i, aNb; + // + bRet=false; + aNb=sizeof(aWhat)/sizeof(aWhat[0]); + for (i=0; i +#include +// +#include +#include +#include + +class SALOME_EvalParser +{ +public: + SALOME_EvalParser(); + virtual ~SALOME_EvalParser(); + + SALOME_EvalVariant calculate(); + SALOME_EvalVariant calculate( const RString& ); + bool setExpression( const RString& ); + + SALOME_ListOfPEvalSet operationSets() const; + SALOME_PEvalSet operationSet( const RString& ) const; + void removeOperationSet(SALOME_PEvalSet ); + void insertOperationSet(SALOME_PEvalSet, + const int = -1 ); + + bool autoDeleteOperationSets() const; + void setAutoDeleteOperationSets( const bool ); + + virtual void clearParameters(); + virtual bool removeParameter( const RString& name ); + virtual SALOME_EvalVariant parameter( const RString& name ) const; + virtual bool hasParameter( const RString& name ) const; + virtual void setParameter( const RString& name, + const SALOME_EvalVariant& value ); + RStringList parameters() const; + + SALOME_EvalExprError error() const; + + bool firstInvalid( RString& ) const; + void removeInvalids(); + RString dump() const; + + static RString toString( const SALOME_ListOfEvalVariant& ); + +protected: + //! Types of postfix representation elements + typedef enum { + Value, //!< Value (number, string, etc.) + Param, //!< Parameter + Open, //!< Open bracket + Close, //!< Close bracket + Pre, //!< Unary prefix operation + Post, //!< Unary postfix operation + Binary //!< Binary operation + } PostfixItemType; + + //! Postfix representation element + typedef struct { + SALOME_EvalVariant myValue; + PostfixItemType myType; + } PostfixItem; + + typedef list Postfix; //!< postfix representation + typedef SALOME_ListOfPEvalSet SetList; //!< list of operations + typedef map ParamMap; //!< parameter-to-value map + typedef map ::value_type PairParamMap; + +protected: + RString dump( const Postfix& ) const; + + virtual bool prepare( const RString&, Postfix& ); + + virtual bool setOperationTypes( Postfix& ); + + virtual bool sort(const Postfix&, + Postfix&, + const RStringList&, + const RStringList&, + int f = -1, + int l = -1 ); + + virtual bool parse( const RString& ); + virtual void setError( const SALOME_EvalExprError ); + + bool calculate(const RString&, + SALOME_EvalVariant&, + SALOME_EvalVariant& ); + + static int search(const RStringList&, + const RString&, + int offset, + int& matchLen, + int& listind ); + + static RString note(const RString& str, + int pos, + int len ); + + static int globalBrackets(const Postfix&, + int, + int ); + +private: + void operationList(RStringList& ) const; + + void bracketsList(RStringList&, + bool ) const; + + bool createValue(const RString&, + SALOME_EvalVariant& ) const; + + int priority(const RString&, + bool isBin ) const; + + SALOME_EvalExprError isValid(const RString&, + const SALOME_EvalVariantType, + const SALOME_EvalVariantType ) const; + + SALOME_EvalExprError calculation(const RString&, + SALOME_EvalVariant&, + SALOME_EvalVariant& ) const; + + bool checkOperations() const; + //////////////////////////////////// + void insert(Postfix& aL, + const int aIndex, + PostfixItem& aItem); + + const PostfixItem& at(const Postfix& aL, + const int aIndex); + + void SALOME_EvalParser::append(Postfix& aL, + const Postfix& aL1); +private: + SALOME_ListOfPEvalSet mySets; + SALOME_EvalExprError myError; + ParamMap myParams; + Postfix myPostfix; + bool myAutoDel; +}; + +#endif diff --git a/src/Notebook/SALOME_EvalSet.cxx b/src/Notebook/SALOME_EvalSet.cxx new file mode 100755 index 000000000..882aa1c52 --- /dev/null +++ b/src/Notebook/SALOME_EvalSet.cxx @@ -0,0 +1,1281 @@ +// Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File : SALOME_EvalSet.cxx +// Author : Peter KURNEV +// Module : SALOME + + +#include +// +#include +#include +static + int toInt(const RString& str, bool *ok); +static + double toDouble(const RString& str, bool *ok); +static + RString tolower(const RString& str); +static + RString toupper(const RString& str); + +//======================================================================= +//function : SALOME_EvalSet +//purpose : +//======================================================================= +SALOME_EvalSet::SALOME_EvalSet() +{ +} +//======================================================================= +//function : SALOME_EvalSet::~SALOME_EvalSet +//purpose : +//======================================================================= +SALOME_EvalSet::~SALOME_EvalSet() +{ +} +//======================================================================= +//function : SALOME_EvalSet::createValue +//purpose : +//======================================================================= +bool SALOME_EvalSet::createValue(const RString& str, + SALOME_EvalVariant& val) const +{ + val=str; + return false; +} +//======================================================================= +//function : SALOME_EvalSet::contains +//purpose : +//======================================================================= +bool SALOME_EvalSet::contains(const SALOME_ListOfPEvalSet& aL, + const SALOME_EvalSet* pS) +{ + bool bRet; + // + bRet=false; + SALOME_ListOfPEvalSet::const_iterator aIt=aL.begin(); + for(; aIt!=aL.end(); ++aIt) { + const SALOME_EvalSet* pSx=*aIt; + bRet=pSx==pS; + if (bRet) { + break; + } + } + return bRet; +} +//======================================================================= +//function : SALOME_EvalSet::insert +//purpose : +//======================================================================= +void SALOME_EvalSet::insert(SALOME_ListOfPEvalSet& aL, + const int aIndex, + SALOME_EvalSet* pS) +{ + int i; + // + if (!aIndex) { + aL.push_front(pS); + } + else if (aIndex==aL.size()) { + aL.push_back(pS); + } + else { + SALOME_ListOfPEvalSet::iterator aIt=aL.begin(); + for(i=0; aIt!=aL.end(); ++aIt, ++i) { + //const SALOME_EvalSet* pSx=*aIt; + if (i==aIndex) { + aL.insert( aIt, pS ); + break; + } + } + } +} +/////////////////////////////////// +//======================================================================= +//function : SALOME_EvalSetBase +//purpose : +//======================================================================= +SALOME_EvalSetBase::SALOME_EvalSetBase() +{ +} +//======================================================================= +//function : ~SALOME_EvalSetBase +//purpose : +//======================================================================= +SALOME_EvalSetBase::~SALOME_EvalSetBase() +{ +} +//======================================================================= +//function : SALOME_EvalSetBase::operationList +//purpose : +//======================================================================= +void SALOME_EvalSetBase::operationList(RStringList& aList) const +{ + using namespace std; + RStringList::const_iterator aIt; + // + aIt=myOpers.begin(); + for (; aIt!=myOpers.end(); ++aIt) { + aList.push_back(*aIt); + } +} +//======================================================================= +//function : SALOME_EvalSetBase::bracketsList +//purpose : +//======================================================================= +void SALOME_EvalSetBase::bracketsList(RStringList& aList, + bool bOpen) const +{ + aList.push_back( bOpen ? "(" : ")" ); +} +//======================================================================= +//function : SALOME_EvalSetBase::addOperations +//purpose : +//======================================================================= +void SALOME_EvalSetBase::addOperations(const RStringList& aList) +{ + RStringList::const_iterator aIt; + SetOfRString aSet; + SetPair aSetPair; + // + aIt=aList.begin(); + for (; aIt != aList.end(); ++aIt) { + aSetPair=aSet.insert(*aIt); + if(aSetPair.second) { + myOpers.push_back( *aIt ); + } + } +} +//======================================================================= +//function : SALOME_EvalSetBase::addTypes +//purpose : +//======================================================================= +void SALOME_EvalSetBase::addTypes(const SALOME_ListOfEvalVariantType& aList) +{ + SALOME_ListOfEvalVariantType::const_iterator aIt; + SALOME_SetOfEvalVariantType aSet; + SALOME_SetPairOfEvalVariantType aSetPair; + // + aIt=aList.begin(); + for (; aIt != aList.end(); ++aIt ) { + aSetPair=aSet.insert(*aIt); + if(aSetPair.second) { + myTypes.push_back(*aIt); + } + } +} +//======================================================================= +//function : SALOME_EvalSetBase::isValid +//purpose : +//======================================================================= +SALOME_EvalExprError SALOME_EvalSetBase::isValid(const RString& op, + const SALOME_EvalVariantType t1, + const SALOME_EvalVariantType t2 ) const +{ + bool bContainsT1, bContainsT2; + SALOME_SetOfEvalVariantType aSet; + SALOME_SetPairOfEvalVariantType aSetPair; + SALOME_ListOfEvalVariantType::const_iterator aIt; + SALOME_EvalVariantType aType; + // + aIt=myTypes.begin(); + for (; aIt != myTypes.end(); ++aIt ) { + aType=*aIt; + aSet.insert(aType); + } + // + aSetPair=aSet.insert(t1); + bContainsT1=!aSetPair.second; + // + aSetPair=aSet.insert(t2); + bContainsT2=!aSetPair.second; + // + if ( ( t1 == SALOME_EvalVariant_Invalid || bContainsT1 ) && + ( t2 == SALOME_EvalVariant_Invalid || bContainsT2 ) && + ( t1 != SALOME_EvalVariant_Invalid || t2 != SALOME_EvalVariant_Invalid ) ) { + if ( priority( op, t1 != SALOME_EvalVariant_Invalid && t2 != SALOME_EvalVariant_Invalid ) > 0 ) { + return SALOME_EvalExpr_OK; + } + else { + return SALOME_EvalExpr_InvalidOperation; + } + } + return SALOME_EvalExpr_OperandsNotMatch; +} +//////////////////////////////////////////////////////////////////////// +//======================================================================= +//function : SALOME_EvalSetArithmetic::SALOME_EvalSetArithmetic +//purpose : +//======================================================================= +SALOME_EvalSetArithmetic::SALOME_EvalSetArithmetic() +: SALOME_EvalSetBase() +{ + RString aOp; + RStringList aStringList; + SALOME_ListOfEvalVariantType aTypes; + // + aOp="+"; aStringList.push_back(aOp); + aOp="-"; aStringList.push_back(aOp); + aOp="*"; aStringList.push_back(aOp); + aOp="/"; aStringList.push_back(aOp); + aOp="="; aStringList.push_back(aOp); + aOp="<"; aStringList.push_back(aOp); + aOp=">"; aStringList.push_back(aOp); + aOp="<="; aStringList.push_back(aOp); + aOp=">="; aStringList.push_back(aOp); + aOp="<>"; aStringList.push_back(aOp); + aOp="!="; aStringList.push_back(aOp); + addOperations( aStringList ); + // + aTypes.push_back( SALOME_EvalVariant_Int ); + aTypes.push_back( SALOME_EvalVariant_UInt ); + aTypes.push_back( SALOME_EvalVariant_Double ); + addTypes(aTypes); +} +//======================================================================= +//function : SALOME_EvalSetArithmetic::~SALOME_EvalSetArithmetic +//purpose : +//======================================================================= +SALOME_EvalSetArithmetic::~SALOME_EvalSetArithmetic() +{ +} +//======================================================================= +//function : SALOME_EvalSetArithmetic::Name +//purpose : +//======================================================================= +RString SALOME_EvalSetArithmetic::Name() +{ + return "Arithmetic"; +} +//======================================================================= +//function : SALOME_EvalSetArithmetic::name +//purpose : +//======================================================================= +RString SALOME_EvalSetArithmetic::name()const +{ + return Name(); +} +//======================================================================= +//function : SALOME_EvalSetArithmetic::createValue +//purpose : +//======================================================================= +bool SALOME_EvalSetArithmetic::createValue(const RString& str, + SALOME_EvalVariant& val ) const +{ + bool ok; + int iX; + double dX; + // + ok = false; + + iX=toInt(str, &ok ); + if (ok) { + SALOME_EvalVariant aRV(iX); + val=aRV; + return ok; + } + // + dX=toDouble(str, &ok ); + if (ok) { + SALOME_EvalVariant aRV(dX); + val=aRV; + return ok; + } + // + ok = SALOME_EvalSetBase::createValue( str, val ); + return ok; +} +//======================================================================= +//function : SALOME_EvalSetArithmetic::priority +//purpose : +//======================================================================= +int SALOME_EvalSetArithmetic::priority(const RString& op, bool isBin) const +{ + if ( isBin ) + { + if ( op == "<" || op == ">" || op == "=" || + op == "<=" || op == ">=" || op == "<>" || op == "!=" ) + return 1; + else if ( op == "+" || op == "-" ) + return 2; + else if( op == "*" || op == "/" ) + return 3; + else + return 0; + } + else if ( op == "+" || op == "-" ) + return 5; + else + return 0; +} +//======================================================================= +//function : SALOME_EvalSetArithmetic::calculate +//purpose : +//======================================================================= +SALOME_EvalExprError SALOME_EvalSetArithmetic::calculate(const RString& op, + SALOME_EvalVariant& v1, + SALOME_EvalVariant& v2 ) const +{ + bool bValid1, bValid2, bOk; + SALOME_EvalExprError err; + SALOME_EvalVariantType aType1, aType2; + // + err = SALOME_EvalExpr_OK; + // + bValid1=v1.isValid(); + bValid2=v2.isValid(); + aType1=v1.type(); + aType2=v2.type(); + + if ( bValid1 && bValid2 ) {// binary operations + if ( ( aType1 == SALOME_EvalVariant_Int || aType1 == SALOME_EvalVariant_UInt ) && + ( aType2 == SALOME_EvalVariant_Int || aType2 == SALOME_EvalVariant_UInt ) ) { + int _v1, _v2; + // + _v1 = v1.toInt(&bOk); + _v2 = v2.toInt(&bOk); + + if ( op == "+" ) { + v1 = _v1 + _v2; + } + else if ( op == "-" ){ + v1 = _v1 - _v2; + } + else if ( op == "*" ){ + v1 = _v1 * _v2; + } + else if ( op == "/" ) { + if ( _v2 != 0 ) { + if ( _v1 % _v2 == 0 ) { + v1 = _v1 / _v2; + } + else { + v1 = double( _v1 ) / double( _v2 ); + } + } + else { + err = SALOME_EvalExpr_InvalidResult; + } + } + else if ( op == "<" ) { + v1 = _v1 < _v2; + } + else if ( op == ">" ){ + v1 = _v1 > _v2; + } + else if ( op == "=" ){ + v1 = _v1 == _v2; + } + else if ( op == "<=" ){ + v1 = _v1 <= _v2; + } + else if ( op == ">=" ){ + v1 = _v1 >= _v2; + } + else if ( op == "<>" || op == "!=" ){ + v1 = _v1 != _v2; + } + } + else if ( ( aType1 == SALOME_EvalVariant_Int || aType1 == SALOME_EvalVariant_Double ) && + ( aType2 == SALOME_EvalVariant_Int || aType2 == SALOME_EvalVariant_Double ) ) { + double _v1 = v1.toDouble(&bOk); + double _v2 = v2.toDouble(&bOk); + + if ( op == "+" ) { + v1 = _v1 + _v2; + } + else if ( op == "-" ){ + v1 = _v1 - _v2; + } + else if ( op == "*" ){ + v1 = _v1 * _v2; + } + else if ( op == "/" ) { + if ( _v2 != 0 ){ + v1 = _v1 / _v2; + } + else{ + err = SALOME_EvalExpr_InvalidResult; + } + } + else if ( op == "<" ){ + v1 = _v1 < _v2; + } + else if ( op == ">" ){ + v1 = _v1 > _v2; + } + else if ( op == "=" ){ + v1 = _v1 == _v2; + } + else if ( op == "<=" ){ + v1 = _v1 <= _v2; + } + else if ( op == ">=" ){ + v1 = _v1 >= _v2; + } + else if ( op == "<>" || op == "!=" ){ + v1 = _v1 != _v2; + } + } + } + + else {// prefix operations + if ( op == "-" ) { + if ( aType2 == SALOME_EvalVariant_Int ){ + v2 = -v2.toInt(&bOk); + } + else if ( aType2 == SALOME_EvalVariant_Double ){ + v2 = -v2.toDouble(&bOk); + } + } + } + return err; +} +///////////////////////////////////////////////////////////////////////// +//======================================================================= +//function : SALOME_EvalSetLogic::SALOME_EvalSetLogic +//purpose : +//======================================================================= +SALOME_EvalSetLogic::SALOME_EvalSetLogic() +: SALOME_EvalSetBase() +{ + RString aOp; + RStringList aStringList; + SALOME_ListOfEvalVariantType aTypes; + // + aOp="and"; aStringList.push_back(aOp); + aOp="&&"; aStringList.push_back(aOp); + aOp="or"; aStringList.push_back(aOp); + aOp="||"; aStringList.push_back(aOp); + aOp="xor"; aStringList.push_back(aOp); + aOp="not"; aStringList.push_back(aOp); + aOp="!"; aStringList.push_back(aOp); + aOp="imp"; aStringList.push_back(aOp); + aOp="="; aStringList.push_back(aOp); + addOperations( aStringList ); + // + aTypes.push_back( SALOME_EvalVariant_Boolean ); + aTypes.push_back( SALOME_EvalVariant_Int ); + aTypes.push_back( SALOME_EvalVariant_UInt ); + addTypes( aTypes ); +} +//======================================================================= +//function : SALOME_EvalSetLogic::~SALOME_EvalSetLogic +//purpose : +//======================================================================= +SALOME_EvalSetLogic::~SALOME_EvalSetLogic() +{ +} +//======================================================================= +//function : SALOME_EvalSetLogic::Name +//purpose : +//======================================================================= +RString SALOME_EvalSetLogic::Name() +{ + return "Logic"; +} +//======================================================================= +//function : SALOME_EvalSetLogic::name +//purpose : +//======================================================================= +RString SALOME_EvalSetLogic::name() const +{ + return Name(); +} +//======================================================================= +//function : SALOME_EvalSetLogic::createValue +//purpose : +//======================================================================= +bool SALOME_EvalSetLogic::createValue(const RString& str, SALOME_EvalVariant& val)const +{ + bool ok = true; + RString valStr = tolower(str); + // + if ( valStr == "true" || valStr == "yes" ) + val = SALOME_EvalVariant( true ); + else if ( valStr == "false" || valStr == "no" ) + val = SALOME_EvalVariant( false ); + else + ok = SALOME_EvalSetBase::createValue( str, val ); + // + return ok; +} +//======================================================================= +//function : SALOME_EvalSetLogic::priority +//purpose : +//======================================================================= +int SALOME_EvalSetLogic::priority(const RString& op, bool isBin) const +{ + if ( isBin ) + { + if ( op == "and" || + op == "or" || + op == "xor" || + op == "&&" || + op == "||" || + op == "imp" ) + return 1; + else if ( op == "=" ) + return 2; + else + return 0; + } + else if ( op == "not" || op == "!" ) + return 5; + else + return 0; +} +//======================================================================= +//function : SALOME_EvalSetLogic::priority +//purpose : +//======================================================================= +SALOME_EvalExprError SALOME_EvalSetLogic::calculate(const RString& op, + SALOME_EvalVariant& v1, + SALOME_EvalVariant& v2 ) const +{ + SALOME_EvalExprError err = SALOME_EvalExpr_OK; + // + int val1 = intValue( v1 ); + int val2 = intValue( v2 ); + if ( v1.isValid() && v2.isValid() ) { + if ( op == "and" || op == "&&" ) { + v1 = val1 && val2; + } + else if ( op == "or" || op == "||" ){ + v1 = val1 || val2; + } + else if ( op == "xor" ){ + v1 = ( !val1 && val2 ) || ( val1 && !val2 ); + } + else if ( op == "imp" ){ + v1 = !val1 || val2; + } + else if ( op == "=" ){ + v1 = val1 == val2; + } + } + else if ( op == "not" || op == "!" ){ + v2 = !val2; + } + // + return err; +} +//======================================================================= +//function : SALOME_EvalSetLogic::intValue +//purpose : +//======================================================================= +int SALOME_EvalSetLogic::intValue(const SALOME_EvalVariant& v) const +{ + bool bOk; + int res = 0; + switch ( v.type() ) + { + case SALOME_EvalVariant_Boolean: + res = v.toBool() ? 1 : 0; + break; + case SALOME_EvalVariant_Int: + case SALOME_EvalVariant_UInt: + res = v.toInt(&bOk); + break; + default: + break; + } + return res; +} +///////////////////////////////////////////////////////////////////////// +//======================================================================= +//function : SALOME_EvalSetMath::SALOME_EvalSetMath +//purpose : +//======================================================================= +SALOME_EvalSetMath::SALOME_EvalSetMath() +: SALOME_EvalSetBase() +{ + RString aOp; + RStringList aStringList; + SALOME_ListOfEvalVariantType aTypes; + // + aOp="sqrt"; aStringList.push_back(aOp); + aOp="abs"; aStringList.push_back(aOp); + aOp="sin"; aStringList.push_back(aOp); + aOp="cos"; aStringList.push_back(aOp); + aOp="rad2grad"; aStringList.push_back(aOp); + aOp="grad2rad"; aStringList.push_back(aOp); + addOperations( aStringList ); + // + aTypes.push_back( SALOME_EvalVariant_Int ); + aTypes.push_back( SALOME_EvalVariant_Double ); + addTypes( aTypes ); +} +//======================================================================= +//function : SALOME_EvalSetMath::~SALOME_EvalSetMath +//purpose : +//======================================================================= +SALOME_EvalSetMath::~SALOME_EvalSetMath() +{ +} +//======================================================================= +//function : SALOME_EvalSetMath::Name +//purpose : +//======================================================================= +RString SALOME_EvalSetMath::Name() +{ + return "Math"; +} +//======================================================================= +//function : SALOME_EvalSetMath::name +//purpose : +//======================================================================= +RString SALOME_EvalSetMath::name() const +{ + return Name(); +} +//======================================================================= +//function : SALOME_EvalSetMath::createValue +//purpose : +//======================================================================= +bool SALOME_EvalSetMath::createValue(const RString& str, SALOME_EvalVariant& val) const +{ + bool ok = false; + // + val=toInt(str, &ok); + if (!ok) { + val=toDouble(str, &ok); + if (!ok) { + ok = SALOME_EvalSetBase::createValue(str, val); + } + } + return ok; +} +//======================================================================= +//function : SALOME_EvalSetMath::priority +//purpose : +//======================================================================= +int SALOME_EvalSetMath::priority(const RString& op, bool isBin) const +{ + if ( isBin ){ + return 0; + } + else if ( op == "sqrt" || + op == "abs" || + op == "sin" || + op == "cos" || + op == "rad2grad" + || op == "grad2rad" ){ + return 1; + } + return 0; +} +//======================================================================= +//function : SALOME_EvalSetMath::calculate +//purpose : +//======================================================================= +SALOME_EvalExprError SALOME_EvalSetMath::calculate(const RString& op, + SALOME_EvalVariant& v1, + SALOME_EvalVariant& v2 ) const +{ + bool bOk; + SALOME_EvalExprError err = SALOME_EvalExpr_OK; + + double val = v2.toDouble(&bOk); + if ( op == "sqrt" ) { + if ( val >= 0 ) { + v2 = sqrt( val ); + } + else { + err = SALOME_EvalExpr_InvalidResult; + } + } + else if ( op == "abs" ) { + if ( v2.type() == SALOME_EvalVariant_Int ) { + v2 = abs( v2.toInt(&bOk) ); + } + else { + v2 = fabs( v2.toDouble(&bOk) ); + } + } + // + else if ( op == "sin" ) { + v2 = sin( val ); + } + else if ( op == "cos" ) { + v2 = cos( val ); + } + else if ( op == "grad2rad" ){ + v2 = val * 3.14159256 / 180.0; + } + else if ( op == "rad2grad" ) { + v2 = val * 180.0 / 3.14159256; + } + + return err; +} +///////////////////////////////////////////////////////////////////////// +//======================================================================= +//function : SALOME_EvalSetString::SALOME_EvalSetString +//purpose : +//======================================================================= +SALOME_EvalSetString::SALOME_EvalSetString() +: SALOME_EvalSetBase() +{ + RString aOp; + RStringList aStringList; + SALOME_ListOfEvalVariantType aTypes; + // + aOp="+"; aStringList.push_back(aOp); + aOp="="; aStringList.push_back(aOp); + aOp="<"; aStringList.push_back(aOp); + aOp=">"; aStringList.push_back(aOp); + aOp="<="; aStringList.push_back(aOp); + aOp=">="; aStringList.push_back(aOp); + aOp="<>"; aStringList.push_back(aOp); + aOp="!="; aStringList.push_back(aOp); + aOp="length"; aStringList.push_back(aOp); + aOp="lower"; aStringList.push_back(aOp); + aOp="upper"; aStringList.push_back(aOp); + addOperations( aStringList ); + + aTypes.push_back( SALOME_EvalVariant_Int ); + aTypes.push_back( SALOME_EvalVariant_Double ); + aTypes.push_back( SALOME_EvalVariant_String ); + addTypes( aTypes ); +} +//======================================================================= +//function : SALOME_EvalSetString::~SALOME_EvalSetString +//purpose : +//======================================================================= +SALOME_EvalSetString::~SALOME_EvalSetString() +{ +} +//======================================================================= +//function : SALOME_EvalSetString::Name +//purpose : +//======================================================================= +RString SALOME_EvalSetString::Name() +{ + return "String"; +} +//======================================================================= +//function : SALOME_EvalSetString::name +//purpose : +//======================================================================= +RString SALOME_EvalSetString::name()const +{ + return Name(); +} +//======================================================================= +//function : SALOME_EvalSetString::createValue +//purpose : +//======================================================================= +bool SALOME_EvalSetString::createValue(const RString& str, + SALOME_EvalVariant& val) const +{ + bool ok = false; + const char* myString=str.c_str(); + int myLength=(int)strlen(myString); + // + if (myLength > 1 && + myString[0] == '\'' && + myString[str.length() - 1] == '\'' ) { + char *pMid; + int i, aNb; + // + aNb=myLength-2; + pMid=new char[aNb+1]; + // + for(i=0; i" || + op == "<=" || op == ">=" || op == "<>" || op == "!=" ){ + return 1; + } + else{ + return 0; + } + } + else if ( op == "length" || op == "lower" || op=="upper" ){ + return 5; + } + return 0; +} +//======================================================================= +//function : SALOME_EvalSetString::calculate +//purpose : +//======================================================================= +SALOME_EvalExprError SALOME_EvalSetString::calculate(const RString& op, + SALOME_EvalVariant& v1, + SALOME_EvalVariant& v2) const +{ + SALOME_EvalExprError err = SALOME_EvalExpr_OK; + if ( v1.isValid() && v2.isValid() ) { + RString _v1 = v1.toString(); + RString _v2 = v2.toString(); + if ( op == "+" ) + v1 = _v1 + _v2; + else if ( op == "=" ) + v1 = _v1 ==_v2; + else if ( op == "<" ) + v1 = _v1 < _v2; + else if ( op == ">" ) + v1 = _v1 > _v2; + else if ( op == "<>" || op == "!=" ) + v1 = _v1 != _v2; + else if ( op == "<=" ) + v1 = _v1 < _v2 || _v1 == _v2; + else if ( op == ">=" ) + v1 = _v1 > _v2 || _v1 == _v2; + } + else if ( !v1.isValid() && v2.isValid() ) { + RString val = v2.toString(); + if ( op == "length" ) + v2 = (int)val.length(); + else if ( op == "lower" ) { + v2=tolower(val); + } + else if ( op == "upper" ) { + v2=toupper(val); + } + } + return err; +} +///////////////////////////////////////////////////////////////////////// +//======================================================================= +//function : SALOME_EvalSetSets::SALOME_EvalSetSets +//purpose : +//======================================================================= +SALOME_EvalSetSets::SALOME_EvalSetSets() +: SALOME_EvalSetBase() +{ + RString aOp; + RStringList aStringList; + SALOME_ListOfEvalVariantType aTypes; + // + aOp="{"; aStringList.push_back(aOp); + aOp="}"; aStringList.push_back(aOp); + aOp="<>"; aStringList.push_back(aOp); + aOp="!="; aStringList.push_back(aOp); + aOp="+"; aStringList.push_back(aOp); + aOp="-"; aStringList.push_back(aOp); + aOp="*"; aStringList.push_back(aOp); + aOp="in"; aStringList.push_back(aOp); + aOp="count"; aStringList.push_back(aOp); + addOperations( aStringList ); + // + aTypes.push_back( SALOME_EvalVariant_List ); + addTypes( aTypes ); +} +//======================================================================= +//function : SALOME_EvalSetSets::~SALOME_EvalSetSets +//purpose : +//======================================================================= +SALOME_EvalSetSets::~SALOME_EvalSetSets() +{ +} +//======================================================================= +//function : SALOME_EvalSetSets::Name +//purpose : +//======================================================================= +RString SALOME_EvalSetSets::Name() +{ + return "Sets"; +} +//======================================================================= +//function : SALOME_EvalSetSets::name +//purpose : +//======================================================================= +RString SALOME_EvalSetSets::name()const +{ + return Name(); +} +//======================================================================= +//function : SALOME_EvalSetSets::bracketsList +//purpose : +//======================================================================= +void SALOME_EvalSetSets::bracketsList(RStringList& aList, + bool bOpen ) const +{ + aList.push_back( bOpen ? "{" : "}" ); + SALOME_EvalSetBase::bracketsList(aList, bOpen); +} +//======================================================================= +//function : SALOME_EvalSetSets::bracketsList +//purpose : +//======================================================================= +int SALOME_EvalSetSets::priority(const RString& op, bool isBin) const +{ + if ( isBin ) { + if ( op == "=" || op == "<>" || op == "!=" ) + return 1; + else if ( op == "+" || op == "-" || op == "*" ) + return 2; + else if ( op == "in" ) + return 3; + else + return 0; + } + else if ( op == "{" || op == "}" ) + return 5; + else if ( op == "count" ) + return 4; + else + return 0; +} +//======================================================================= +//function : SALOME_EvalSetSets::isValid +//purpose : +//======================================================================= +SALOME_EvalExprError SALOME_EvalSetSets::isValid(const RString& op, + const SALOME_EvalVariantType t1, + const SALOME_EvalVariantType t2 ) const +{ + if ( op == "{" ) { + return SALOME_EvalExpr_OK; + } + if ( op != "in" ) { + return SALOME_EvalSetBase::isValid( op, t1, t2 ); + } + if ( t1 != SALOME_EvalVariant_Invalid && t2 == SALOME_EvalVariant_List ) { + return SALOME_EvalExpr_OK; + } + //else + return SALOME_EvalExpr_OperandsNotMatch; +} +//======================================================================= +//function : SALOME_EvalSetSets::add +//purpose : +//======================================================================= +void SALOME_EvalSetSets::add(ValueSet& aSet, const SALOME_EvalVariant& v) +{ + + if ( v.isValid() && !SALOME_EvalVariant::contains( aSet, v ) ) { + aSet.push_back( v ); + } +} +//======================================================================= +//function : SALOME_EvalSetSets::add +//purpose : +//======================================================================= +void SALOME_EvalSetSets::add(ValueSet& s1, const ValueSet& s2) +{ + ValueSet::const_iterator anIt = s2.begin(); + for ( ; anIt != s2.end(); ++anIt ) + add( s1, *anIt ); +} +//======================================================================= +//function : SALOME_EvalSetSets::remove +//purpose : +//======================================================================= +void SALOME_EvalSetSets::remove(ValueSet& aSet, const SALOME_EvalVariant& v) +{ + aSet.remove(v); +} +//======================================================================= +//function : SALOME_EvalSetSets::remove +//purpose : +//======================================================================= +void SALOME_EvalSetSets::remove(ValueSet& s1, const ValueSet& s2) +{ + ValueSet::const_iterator aIt = s2.begin(); + for (; aIt != s2.end(); ++aIt ) { + s1.remove(*aIt); + } +} +//======================================================================= +//function : SALOME_EvalSetSets::calculate +//purpose : +//======================================================================= +SALOME_EvalExprError SALOME_EvalSetSets::calculate(const RString& op, + SALOME_EvalVariant& v1, + SALOME_EvalVariant& v2 ) const +{ + SALOME_EvalExprError err = SALOME_EvalExpr_OK; + // !!! + if ( op != "{" ) { + if ( op == "}" ) { + ValueSet aNewList; + add( aNewList, v1.toList() ); + v1 = aNewList; + } + else if (op == "=" || op == "<>" || op == "!=" || + op == "+" || op == "-" || op == "*" ) { + ValueSet aNewList; + add( aNewList, v1.toList() ); + if ( op == "=" || op == "<>" || op == "!=" || op == "-" ) { + remove( aNewList, v2.toList() ); + if ( op == "=" ){ + v1 = aNewList.empty() && v1.toList().size() == v2.toList().size(); + } + else if ( op == "<>" || op == "!=" ) { + v1 = !aNewList.empty() || v1.toList().size() != v2.toList().size(); + } + else{ + v1 = aNewList; + } + } + else if ( op == "+" ) { + add( aNewList, v2.toList() ); + v1 = aNewList; + } + else if ( op == "*" ) { + ValueSet toDelete; + add( toDelete, aNewList ); + remove( toDelete, v2.toList() ); + remove( aNewList, toDelete ); + v1 = aNewList; + } + } + else if ( op== "count" ) { + v2 = (int)v2.toList().size(); + } + else if ( op == "in" ) { + if ( v1.type() == SALOME_EvalVariant_List ) { + bool res = true; + ValueSet lst1 = v1.toList(); + ValueSet lst2 = v2.toList(); + ValueSet::const_iterator anIt = lst1.begin(); + for (; anIt != lst1.end() && res; ++anIt ){ + res = SALOME_EvalVariant::contains(lst2, *anIt ); + } + v1 = res; + } + else { + SALOME_ListOfEvalVariant aL=v2.toList(); + bool bContains=SALOME_EvalVariant::contains(aL, v1); + v1=SALOME_EvalVariant(bContains); + } + } + } + + return err; +} +///////////////////////////////////////////////////////////////////////// +//======================================================================= +//function : SALOME_EvalSetConst::SALOME_EvalSetConst +//purpose : +//======================================================================= +SALOME_EvalSetConst::SALOME_EvalSetConst() +: SALOME_EvalSet() +{ +} +//======================================================================= +//function : SALOME_EvalSetConst::~SALOME_EvalSetConst +//purpose : +//======================================================================= +SALOME_EvalSetConst::~SALOME_EvalSetConst() +{ +} +//======================================================================= +//function : SALOME_EvalSetConst::Name +//purpose : +//======================================================================= +RString SALOME_EvalSetConst::Name() +{ + return "Const"; +} +//======================================================================= +//function : SALOME_EvalSetConst::name +//purpose : +//======================================================================= +RString SALOME_EvalSetConst::name() const +{ + return Name(); +} +//======================================================================= +//function : SALOME_EvalSetConst::createValue +//purpose : +//======================================================================= +bool SALOME_EvalSetConst::createValue(const RString& str, SALOME_EvalVariant& val) const +{ + bool ok = true; + if ( str == "pi" ) // PI number + val = SALOME_EvalVariant( 3.141593 ); + else if ( str == "exp" ) // Exponent value (e) + val = SALOME_EvalVariant( 2.718282 ); + else if ( str == "g" ) // Free fall acceleration (g) + val = SALOME_EvalVariant( 9.80665 ); + else + ok = false; + + return ok; +} +//======================================================================= +//function : SALOME_EvalSetConst::operationList +//purpose : +//======================================================================= +void SALOME_EvalSetConst::operationList(RStringList&) const +{ +} +//======================================================================= +//function : SALOME_EvalSetConst::bracketsList +//purpose : +//======================================================================= +void SALOME_EvalSetConst::bracketsList(RStringList& , bool) const +{ +} +//======================================================================= +//function : SALOME_EvalSetConst::priority +//purpose : +//======================================================================= +int SALOME_EvalSetConst::priority(const RString& /*op*/, bool /*isBin*/ ) const +{ + return 0; +} +//======================================================================= +//function : SALOME_EvalSetConst::isValid +//purpose : +//======================================================================= +SALOME_EvalExprError SALOME_EvalSetConst::isValid(const RString& /*op*/, + const SALOME_EvalVariantType /*t1*/, + const SALOME_EvalVariantType /*t2*/ ) const +{ + return SALOME_EvalExpr_InvalidOperation; +} +//======================================================================= +//function : SALOME_EvalSetConst::calculate +//purpose : +//======================================================================= +SALOME_EvalExprError SALOME_EvalSetConst::calculate(const RString&, + SALOME_EvalVariant&, + SALOME_EvalVariant& ) const +{ + return SALOME_EvalExpr_InvalidOperation; +} + +///////////////////////////////////////////////////////////////////////// +//======================================================================= +//function : toupper +//purpose : +//======================================================================= +RString toupper(const RString& str) +{ + char aC; + const char* mystring=str.c_str(); + size_t mylength, i; + RString aRet; + // + aRet=str; + if(mystring) { + mylength=strlen(mystring); + for (i=0; i < mylength; i++) { + aC=(char)toupper(mystring[i]); + aRet.at(i)=aC; + } + } + return aRet; +} +//======================================================================= +//function : tolower +//purpose : +//======================================================================= +RString tolower(const RString& str) +{ + char aC; + const char* mystring=str.c_str(); + size_t mylength, i; + RString aRet; + // + aRet=str; + if(mystring) { + mylength=strlen(mystring); + for (i=0; i < mylength; i++) { + aC=(char)tolower(mystring[i]); + aRet.at(i)=aC; + } + } + return aRet; +} +//======================================================================= +//function : toDouble +//purpose : +//======================================================================= +double toDouble(const RString& str, bool *ok) +{ + char *ptr; + const char* mystring=str.c_str(); + double value; + // + value=0.; + *ok=false; + if(mystring) { + value=strtod(mystring,&ptr); + if (ptr != mystring) { + *ok=true; + return value; + } + else{ + value=0.; + } + } + return value; +} +//======================================================================= +//function : toInt +//purpose : +//======================================================================= +int toInt(const RString& str, bool *ok) +{ + char *ptr; + const char* mystring=str.c_str(); + size_t mylength, i; + int value; + // + value=0; + *ok=false; + if(mystring) { + mylength=strlen(mystring); + value = (int)strtol(mystring, &ptr,10); + // + if (ptr != mystring) { + for (i=0; i < mylength; i++) { + if (mystring[i] == '.') { + value=0; + return value; + } + } + *ok=true; + return value; + } + } + return value; +} diff --git a/src/Notebook/SALOME_EvalSet.hxx b/src/Notebook/SALOME_EvalSet.hxx new file mode 100755 index 000000000..ff6b0164e --- /dev/null +++ b/src/Notebook/SALOME_EvalSet.hxx @@ -0,0 +1,276 @@ +// Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File : SALOME_EvalSet.hxx +// Author : Peter KURNEV +// Module : SALOME + + +#ifndef SALOME_EvalSet_Header_File +#define SALOME_EvalSet_Header_File +// +#ifdef WNT +#pragma warning(disable : 4786) +#endif +// +#include +#include +#include +// +using namespace std; +// +class SALOME_EvalSet; +typedef SALOME_EvalSet* SALOME_PEvalSet; +typedef list SALOME_ListOfPEvalSet; + +//======================================================================= +//class : SALOME_EvalSet +//purpose : +//======================================================================= +class SALOME_EvalSet +{ +public: + SALOME_EvalSet(); + // + virtual ~SALOME_EvalSet(); + // + virtual RString name() const = 0; + // + virtual void operationList( RStringList& ) const = 0; + // + virtual void bracketsList(RStringList&, + bool open ) const = 0; + // + virtual bool createValue(const RString&, + SALOME_EvalVariant& ) const; + // + virtual int priority( const RString&, bool isBin ) const = 0; + // + virtual SALOME_EvalExprError isValid(const RString&, + const SALOME_EvalVariantType, + const SALOME_EvalVariantType) const = 0; + + virtual SALOME_EvalExprError calculate(const RString&, + SALOME_EvalVariant&, + SALOME_EvalVariant&) const = 0; + // + static bool contains(const SALOME_ListOfPEvalSet&, + const SALOME_EvalSet*); + // + static void insert (SALOME_ListOfPEvalSet& aL, + const int aIndex, + SALOME_EvalSet* pS); + // +}; +// + +// +//======================================================================= +//class : SALOME_EvalSetBase +//purpose : +//======================================================================= +class SALOME_EvalSetBase : public SALOME_EvalSet +{ +public: + SALOME_EvalSetBase(); + // + virtual ~SALOME_EvalSetBase(); + // + virtual void operationList( RStringList& ) const; + virtual void bracketsList ( RStringList&, bool open ) const; + + virtual + SALOME_EvalExprError isValid(const RString&, + const SALOME_EvalVariantType a, + const SALOME_EvalVariantType b) const; + + + +protected: + void addTypes(const SALOME_ListOfEvalVariantType&); + void addOperations(const RStringList& ); + +private: + RStringList myOpers; + SALOME_ListOfEvalVariantType myTypes; +}; +//======================================================================= +//class : SALOME_EvalSetArithmetic +//purpose : +//======================================================================= +class SALOME_EvalSetArithmetic : public SALOME_EvalSetBase +{ +public: + SALOME_EvalSetArithmetic(); + virtual ~SALOME_EvalSetArithmetic(); + + virtual bool createValue(const RString&, + SALOME_EvalVariant& ) const; + + virtual int priority(const RString&, + bool isBin ) const; + + virtual SALOME_EvalExprError calculate(const RString&, + SALOME_EvalVariant&, + SALOME_EvalVariant& ) const; + + static RString Name(); + virtual RString name() const; +}; +//======================================================================= +//class : SALOME_EvalSetLogic +//purpose : +//======================================================================= +class SALOME_EvalSetLogic : public SALOME_EvalSetBase +{ +public: + SALOME_EvalSetLogic(); + + virtual ~SALOME_EvalSetLogic(); + + virtual bool createValue(const RString&, + SALOME_EvalVariant& ) const; + + virtual int priority(const RString&, + bool isBin ) const; + + virtual SALOME_EvalExprError calculate(const RString&, + SALOME_EvalVariant&, + SALOME_EvalVariant& ) const; + + static RString Name(); + + virtual RString name() const; + +private: + int intValue(const SALOME_EvalVariant& ) const; +}; +//======================================================================= +//class : SALOME_EvalSetMath +//purpose : +//======================================================================= +class SALOME_EvalSetMath : public SALOME_EvalSetBase +{ +public: + SALOME_EvalSetMath(); + virtual ~SALOME_EvalSetMath(); + + virtual bool createValue(const RString&, + SALOME_EvalVariant&) const; + + virtual int priority (const RString&, + bool isBin) const; + + virtual SALOME_EvalExprError calculate (const RString&, + SALOME_EvalVariant&, + SALOME_EvalVariant& ) const; + + static RString Name(); + virtual RString name() const; +}; +//======================================================================= +//class : SALOME_EvalSetString +//purpose : +//======================================================================= +class SALOME_EvalSetString : public SALOME_EvalSetBase +{ +public: + SALOME_EvalSetString(); + virtual ~SALOME_EvalSetString(); + + virtual bool createValue(const RString&, + SALOME_EvalVariant& ) const; + + virtual int priority( const RString&, + bool isBin ) const; + + virtual SALOME_EvalExprError calculate( const RString&, + SALOME_EvalVariant&, + SALOME_EvalVariant& ) const; + + static RString Name(); + virtual RString name() const; +}; +//======================================================================= +//class : SALOME_EvalSetSets +//purpose : +//======================================================================= +class SALOME_EvalSetSets : public SALOME_EvalSetBase +{ +public: + typedef SALOME_ListOfEvalVariant ValueSet; + +public: + SALOME_EvalSetSets(); + virtual ~SALOME_EvalSetSets(); + // + static RString Name(); + virtual RString name() const; + // + virtual void bracketsList(RStringList&, bool open ) const; + // + virtual int priority(const RString&, bool isBin ) const; + + virtual SALOME_EvalExprError isValid(const RString&, + const SALOME_EvalVariantType, + const SALOME_EvalVariantType) const; + + virtual SALOME_EvalExprError calculate(const RString&, + SALOME_EvalVariant&, + SALOME_EvalVariant& ) const; + + static void add (ValueSet&, const SALOME_EvalVariant& ); + static void add (ValueSet&, const ValueSet& ); + static void remove(ValueSet&, const SALOME_EvalVariant& ); + static void remove(ValueSet&, const ValueSet& ); + +}; +//======================================================================= +//class : SALOME_EvalSetConst +//purpose : +//======================================================================= +class SALOME_EvalSetConst : public SALOME_EvalSet +{ +public: + SALOME_EvalSetConst(); + virtual ~SALOME_EvalSetConst(); + + static RString Name(); + virtual RString name() const; + + virtual bool createValue( const RString&, SALOME_EvalVariant& ) const; + + virtual void operationList( RStringList& ) const; + + virtual void bracketsList( RStringList&, bool open ) const; + + virtual int priority( const RString&, bool isBin ) const; + + virtual SALOME_EvalExprError isValid(const RString&, + const SALOME_EvalVariantType, + const SALOME_EvalVariantType ) const; + + virtual SALOME_EvalExprError calculate(const RString&, + SALOME_EvalVariant&, + SALOME_EvalVariant& ) const; +}; + +#endif diff --git a/src/Notebook/SALOME_EvalVariant.cxx b/src/Notebook/SALOME_EvalVariant.cxx new file mode 100755 index 000000000..1e5c25cd6 --- /dev/null +++ b/src/Notebook/SALOME_EvalVariant.cxx @@ -0,0 +1,501 @@ +// Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File : SALOME_EvalVariant.cxx +// Author : Peter KURNEV +// Module : SALOME + +#include + +#include +#include + +//======================================================================= +//function : SALOME_EvalVariantData +//purpose : +//======================================================================= +SALOME_EvalVariantData::SALOME_EvalVariantData(const SALOME_EvalVariantData& other) +{ + myType=other.myType; + myValue.myPtr=NULL; + // + switch (myType) { + case SALOME_EvalVariant_String: { + RString& aStr=*(RString *)(other.myValue.myPtr); + RString *pStr=new RString(aStr); + myValue.myPtr=(void*)pStr; + } + break; + + case SALOME_EvalVariant_List: { + SALOME_ListOfEvalVariant& aX=*(SALOME_ListOfEvalVariant*)(other.myValue.myPtr); + SALOME_ListOfEvalVariant *p=new SALOME_ListOfEvalVariant(aX); + myValue.myPtr=(void*)p; + } + break; + + default: + myValue=other.myValue; + break; + } +} +//======================================================================= +//function : operator= +//purpose : +//======================================================================= +SALOME_EvalVariantData& SALOME_EvalVariantData::operator=(const SALOME_EvalVariantData& other) +{ + clear(); + myType=other.myType; + switch (myType) { + case SALOME_EvalVariant_String: { + RString& aStr=*(string *)(other.myValue.myPtr); + RString *pStr=new RString(aStr); + myValue.myPtr=(void*)pStr; + } + break; + + case SALOME_EvalVariant_List: { + SALOME_ListOfEvalVariant& aX=*(SALOME_ListOfEvalVariant*)(other.myValue.myPtr); + SALOME_ListOfEvalVariant *p=new SALOME_ListOfEvalVariant(aX); + myValue.myPtr=(void*)p; + } + break; + + default: + myValue=other.myValue; + break; + } + return *this; +} +//======================================================================= +//function : operator== +//purpose : +//======================================================================= +bool SALOME_EvalVariantData::operator==(const SALOME_EvalVariantData& theOther) const +{ + bool bRet; + // + bRet=myType==SALOME_EvalVariant_Invalid || theOther.myType==SALOME_EvalVariant_Invalid; + if (!bRet) { + return bRet; + } + // + bRet=myType==theOther.myType; + if (!bRet) { + return bRet; + } + // + switch (myType) { + case SALOME_EvalVariant_Boolean: + bRet=myValue.myBoolean==theOther.myValue.myBoolean; + break; + // + case SALOME_EvalVariant_Int: + bRet=myValue.myInt==theOther.myValue.myInt; + break; + // + case SALOME_EvalVariant_UInt: + bRet=myValue.myUInt==theOther.myValue.myUInt; + break; + // + case SALOME_EvalVariant_Double: + bRet=myValue.myDouble==theOther.myValue.myDouble; + break; + // + case SALOME_EvalVariant_String: { + bool bIsNull, bIsNullOther; + // + bIsNull=!myValue.myPtr; + bIsNullOther=!theOther.myValue.myPtr; + if (bIsNull && bIsNullOther) { + bRet=true; + } + else if (bIsNull && !bIsNullOther) { + bRet=false; + } + else if (!bIsNull && bIsNullOther) { + bRet=false; + } + else { + RString& myStr=*(RString *)(myValue.myPtr); + RString& aOther=*(RString *)(theOther.myValue.myPtr); + bRet=myStr==aOther; + } + } + break; + // + case SALOME_EvalVariant_List: { + bool bIsNull, bIsNullOther; + // + bIsNull=!myValue.myPtr; + bIsNullOther=!theOther.myValue.myPtr; + if (bIsNull && bIsNullOther) { + bRet=true; + } + else if (bIsNull && !bIsNullOther) { + bRet=false; + } + else if (!bIsNull && bIsNullOther) { + bRet=false; + } + else { + size_t aNb1, aNb2; + // + const SALOME_ListOfEvalVariant& aL1=*(SALOME_ListOfEvalVariant*)(myValue.myPtr); + const SALOME_ListOfEvalVariant& aL2=*(SALOME_ListOfEvalVariant*)(theOther.myValue.myPtr); + aNb1=aL1.size(); + aNb2=aL2.size(); + bRet=aNb1==aNb2; + if (!bRet) { + break; + } + // + SALOME_ListOfEvalVariant::const_iterator aIt1 = aL1.begin(); + SALOME_ListOfEvalVariant::const_iterator aIt2 = aL2.begin(); + for ( ; aIt1 != aL1.end(); ++aIt1, ++aIt2 ) { + const SALOME_EvalVariant& aV1=*aIt1; + const SALOME_EvalVariant& aV2=*aIt2; + const SALOME_EvalVariantData& aD1=aV1.Data(); + const SALOME_EvalVariantData& aD2=aV2.Data(); + bRet=aD1==aD2; + if (!bRet) { + break; + } + } + } + } + break; + + default: + bRet=false; + break; + } + return bRet; +} +///////////////////////////////////////////////////////////////////////// +// +// class: SALOME_EvalVariant +// + + +//======================================================================= +//function : SALOME_EvalVariant +//purpose : +//======================================================================= +SALOME_EvalVariant::SALOME_EvalVariant(bool theValue) +{ + ChangeDataType()=SALOME_EvalVariant_Boolean; + ChangeDataValue().myBoolean=theValue; +} +//======================================================================= +//function : SALOME_EvalVariant +//purpose : +//======================================================================= +SALOME_EvalVariant::SALOME_EvalVariant(int theValue) +{ + ChangeDataType()=SALOME_EvalVariant_Int; + ChangeDataValue().myInt=theValue; +} +//======================================================================= +//function : SALOME_EvalVariant +//purpose : +//======================================================================= +SALOME_EvalVariant::SALOME_EvalVariant(uint theValue) +{ + ChangeDataType()=SALOME_EvalVariant_UInt; + ChangeDataValue().myUInt=theValue; +} +//======================================================================= +//function : SALOME_EvalVariant +//purpose : +//======================================================================= +SALOME_EvalVariant::SALOME_EvalVariant(double theValue) +{ + ChangeDataType()=SALOME_EvalVariant_Double; + ChangeDataValue().myDouble=theValue; +} +//======================================================================= +//function : SALOME_EvalVariant +//purpose : +//======================================================================= +SALOME_EvalVariant::SALOME_EvalVariant(const RString& theValue) +{ + ChangeDataType()=SALOME_EvalVariant_String; + RString *p=new RString(theValue); + ChangeDataValue().myPtr=(void*)p; +} +//======================================================================= +//function : SALOME_EvalVariant +//purpose : +//======================================================================= +SALOME_EvalVariant::SALOME_EvalVariant(const SALOME_ListOfEvalVariant& theValue) +{ + ChangeDataType()=SALOME_EvalVariant_List; + SALOME_ListOfEvalVariant *p=new SALOME_ListOfEvalVariant(theValue); + ChangeDataValue().myPtr=(void*)p; +} +//======================================================================= +//function : operator= +//purpose : +//======================================================================= +void SALOME_EvalVariant::operator=(const RString& theValue) +{ + clear(); + // + ChangeDataType()=SALOME_EvalVariant_String; + RString *p=new RString(theValue); + ChangeDataValue().myPtr=(void*)p; +} +//======================================================================= +//function : operator= +//purpose : +//======================================================================= +void SALOME_EvalVariant::operator=(const bool theValue) +{ + clear(); + // + ChangeDataType()=SALOME_EvalVariant_Boolean; + ChangeDataValueBoolean()=theValue; +} +//======================================================================= +//function : operator= +//purpose : +//======================================================================= +void SALOME_EvalVariant::operator=(const int theValue) +{ + clear(); + // + ChangeDataType()=SALOME_EvalVariant_Int; + ChangeDataValueInt()=theValue; +} +//======================================================================= +//function : operator= +//purpose : +//======================================================================= +void SALOME_EvalVariant::operator=(const uint theValue) +{ + clear(); + // + ChangeDataType()=SALOME_EvalVariant_UInt; + ChangeDataValueUInt()=theValue; +} +//======================================================================= +//function : operator= +//purpose : +//======================================================================= +void SALOME_EvalVariant::operator=(const double theValue) +{ + clear(); + // + ChangeDataType()=SALOME_EvalVariant_Double; + ChangeDataValueDouble()=theValue; +} +//======================================================================= +//function : operator= +//purpose : +//======================================================================= +void SALOME_EvalVariant::operator=(const char* theValue) +{ + clear(); + // + ChangeDataType()=SALOME_EvalVariant_String; + RString *p=new RString(theValue); + ChangeDataValue().myPtr=(void*)p; +} +//======================================================================= +//function : toBool +//purpose : +//======================================================================= +bool SALOME_EvalVariant::toBool() const +{ + SALOME_EvalVariantType aType=type(); + // + if ( aType == SALOME_EvalVariant_Boolean ) { + return DataValueBoolean(); + } + else if ( aType == SALOME_EvalVariant_Int ) { + return DataValueInt() != 0; + } + else if ( aType == SALOME_EvalVariant_UInt ) { + return DataValueUInt() != 0; + } + else if ( aType == SALOME_EvalVariant_Double ) { + return DataValueDouble() != 0.; + } + else if ( aType == SALOME_EvalVariant_String ) { + RString aZero("0"), aFalse("false"); + const RString& aStr=DataValueString(); + return !(aStr==aZero || aStr==aFalse || aStr.empty()); + } + return false; +} +//======================================================================= +//function : toInt +//purpose : +//======================================================================= +int SALOME_EvalVariant::toInt(bool *ok) const +{ + *ok=false; + // + SALOME_EvalVariantType aType=type(); + // + if (aType == SALOME_EvalVariant_Boolean ) { + return (int)DataValueBoolean(); + } + else if (aType == SALOME_EvalVariant_Int ) { + return DataValueInt(); + } + else if (aType == SALOME_EvalVariant_UInt ) { + return (int)DataValueUInt(); + } + else if (aType == SALOME_EvalVariant_Double ) { + return (int)DataValueDouble(); + } + else if (aType == SALOME_EvalVariant_String) { + int iRet; + // + const RString& aStr=DataValueString(); + const char *pStr=aStr.c_str(); + iRet=atoi(pStr); + return iRet; + } + return 0; +} +//======================================================================= +//function : toUInt +//purpose : +//======================================================================= +uint SALOME_EvalVariant::toUInt(bool *ok) const +{ + int iX; + // + iX=toInt(ok); + // + return (uint)iX; +} +//======================================================================= +//function : toDouble +//purpose : +//======================================================================= +double SALOME_EvalVariant::toDouble(bool *ok) const +{ + *ok=false; + // + SALOME_EvalVariantType aType=type(); + // + if (aType == SALOME_EvalVariant_Boolean ) { + return (double)DataValueBoolean(); + } + else if (aType == SALOME_EvalVariant_Int ) { + return (double)DataValueInt(); + } + else if (aType == SALOME_EvalVariant_UInt ) { + return (double)DataValueUInt(); + } + else if (aType == SALOME_EvalVariant_Double ) { + return (double)DataValueDouble(); + } + else if (aType == SALOME_EvalVariant_String) { + double dRet; + // + const RString& aStr=DataValueString(); + const char *pStr=aStr.c_str(); + dRet=atof(pStr); + return dRet; + } + return 0.; +} + +//======================================================================= +//function : toString +//purpose : +//======================================================================= +RString SALOME_EvalVariant::toString() const +{ + char buffer[32]; + RString aS; + // + SALOME_EvalVariantType aType=type(); + // + if (aType == SALOME_EvalVariant_Boolean) { + int iX; + // + iX=toInt(); + aS= iX? "true" : "false"; + } + else if (aType == SALOME_EvalVariant_Int) { + sprintf(buffer, "%d", DataValueInt()); + aS=buffer; + } + else if (aType == SALOME_EvalVariant_UInt ) { + sprintf(buffer, "%d", (int)DataValueUInt()); + aS=buffer; + } + else if (aType == SALOME_EvalVariant_Double ) { + + double aX; + // + aX=DataValueDouble(); + gcvt(aX, 12, buffer); + aS=buffer; + } + else if (aType == SALOME_EvalVariant_String) { + const RString& aStr=DataValueString(); + aS=aStr; + } + return aS; +} +//======================================================================= +//function : toList +//purpose : +//======================================================================= +SALOME_ListOfEvalVariant SALOME_EvalVariant::toList() const +{ + SALOME_ListOfEvalVariant aS; + // + SALOME_EvalVariantType aType=type(); + // + if (aType == SALOME_EvalVariant_List) { + aS=DataValueList(); + } + return aS; +} +//======================================================================= +//function : contains +//purpose : +//======================================================================= +bool SALOME_EvalVariant::contains(const SALOME_ListOfEvalVariant& aL, + const SALOME_EvalVariant& aV) +{ + bool bRet; + // + bRet=false; + SALOME_ListOfEvalVariant::const_iterator aIt = aL.begin(); + for ( ; aIt != aL.end(); ++aIt) { + const SALOME_EvalVariant& aVx=*aIt; + bRet=aVx==aV; + if (bRet) { + break; + } + } + return bRet; +} diff --git a/src/Notebook/SALOME_EvalVariant.hxx b/src/Notebook/SALOME_EvalVariant.hxx new file mode 100755 index 000000000..f6177b470 --- /dev/null +++ b/src/Notebook/SALOME_EvalVariant.hxx @@ -0,0 +1,263 @@ +// Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// +// File : SALOME_EvalVariant.hxx +// Author : Peter KURNEV +// Module : SALOME + +#ifndef SALOME_EvalVariant_Header_File +#define SALOME_EvalVariant_Header_File +// +#ifdef WNT +#pragma warning(disable : 4786) +#endif +// +#include +#include +#include +#include +// +using namespace std; +// +typedef unsigned int uint; +// + +class SALOME_EvalVariant; +class SALOME_EvalVariantData; +typedef list SALOME_ListOfEvalVariant; +// +// SALOME_EvalVariantType +enum SALOME_EvalVariantType { + SALOME_EvalVariant_Invalid, + SALOME_EvalVariant_Boolean, + SALOME_EvalVariant_Int, + SALOME_EvalVariant_UInt, + SALOME_EvalVariant_Double, + SALOME_EvalVariant_String, + SALOME_EvalVariant_List +}; +// +typedef list SALOME_ListOfEvalVariantType; +// +typedef set SALOME_SetOfEvalVariantType; +typedef SALOME_SetOfEvalVariantType::iterator SALOME_SetIteratorOfSetOfEvalVariantType; +typedef pair SALOME_SetPairOfEvalVariantType; +// +//======================================================================= +//union : SALOME_EvalVariantValue +//purpose : +//======================================================================= +union SALOME_EvalVariantValue { + bool myBoolean; + int myInt; + uint myUInt; + double myDouble; + void* myPtr; +}; +//======================================================================= +//class : SALOME_EvalVariantData +//purpose : +//======================================================================= +class SALOME_EvalVariantData { + public: + SALOME_EvalVariantData(){ + myType=SALOME_EvalVariant_Invalid; + myValue.myBoolean=false; + myValue.myInt=0; + myValue.myUInt=0; + myValue.myDouble=0.; + myValue.myPtr=NULL; + }; + // + ~SALOME_EvalVariantData(){ + clear(); + }; + // + SALOME_EvalVariantData(const SALOME_EvalVariantData& other); + SALOME_EvalVariantData& operator=(const SALOME_EvalVariantData& other); + bool operator==(const SALOME_EvalVariantData& other) const; + // + void clear(){ + if (myType==SALOME_EvalVariant_String) { + if (myValue.myPtr) { + RString *pStr=(RString *)myValue.myPtr; + delete pStr; + myValue.myPtr=NULL; + } + + } + else if (myType==SALOME_EvalVariant_List) { + if (myValue.myPtr) { + SALOME_ListOfEvalVariant *p=(SALOME_ListOfEvalVariant *)myValue.myPtr; + delete p; + myValue.myPtr=NULL; + } + } + else { + myValue.myBoolean=false; + myValue.myInt=0; + myValue.myUInt=0; + myValue.myDouble=0.; + myValue.myPtr=NULL; + } + }; + // + SALOME_EvalVariantType myType; + SALOME_EvalVariantValue myValue; +}; +//======================================================================= +//class : SALOME_EvalVariant +//purpose : +//======================================================================= +class SALOME_EvalVariant +{ + public: + SALOME_EvalVariant(){ + }; + // + ~SALOME_EvalVariant(){ + myData.clear(); + }; + // + SALOME_EvalVariant(const SALOME_EvalVariant& other) : + myData(other.myData) { + }; + // + SALOME_EvalVariant& operator=(const SALOME_EvalVariant& other) { + myData=other.myData; + return *this; + }; + // + bool operator==(const SALOME_EvalVariant& theOther) const { + return myData==theOther.myData;; + }; + // + //// + void operator=(const RString& aString); + void operator=(const bool theValue); + void operator=(const int theValue); + void operator=(const uint theValue); + void operator=(const double theValue); + void operator=(const char* theValue); + //// + SALOME_EvalVariant(bool b); + SALOME_EvalVariant(int i); + SALOME_EvalVariant(uint ui); + SALOME_EvalVariant(double d); + SALOME_EvalVariant(const RString& s); + SALOME_EvalVariant(const SALOME_ListOfEvalVariant& s); + // + SALOME_EvalVariantType type() const { + return DataType(); + }; + // + bool isValid() const { + return type()!=SALOME_EvalVariant_Invalid; + } + // + bool isNull() const { + return !isValid(); + }; + // + void clear() { + myData.clear(); + }; + // + bool toBool() const; + int toInt(bool *ok = 0) const; + uint toUInt(bool *ok = 0) const; + double toDouble(bool *ok = 0) const; + RString toString() const; + SALOME_ListOfEvalVariant toList() const; + // + const SALOME_EvalVariantData& Data() const{ + return myData; + }; + // + static bool contains(const SALOME_ListOfEvalVariant& aL, + const SALOME_EvalVariant& aV); + // + protected: + SALOME_EvalVariantType& ChangeDataType() { + return ChangeData().myType; + }; + // + SALOME_EvalVariantData& ChangeData() { + return myData; + }; + // + SALOME_EvalVariantValue& ChangeDataValue() { + return ChangeData().myValue; + }; + // + bool& ChangeDataValueBoolean() { + return ChangeDataValue().myBoolean; + }; + // + int& ChangeDataValueInt() { + return ChangeDataValue().myInt; + }; + // + uint& ChangeDataValueUInt() { + return ChangeDataValue().myUInt; + }; + // + double& ChangeDataValueDouble() { + return ChangeDataValue().myDouble; + }; + // + const SALOME_EvalVariantType& DataType() const{ + return Data().myType; + }; + // + const SALOME_EvalVariantValue& DataValue() const{ + return Data().myValue; + }; + // + bool DataValueBoolean() const { + return DataValue().myBoolean; + }; + // + int DataValueInt() const { + return DataValue().myInt; + }; + // + uint DataValueUInt() const { + return DataValue().myUInt; + }; + // + double DataValueDouble() const { + return DataValue().myDouble; + }; + // + const RString& DataValueString() const { + return *((string *)(DataValue().myPtr)); + }; + // + const SALOME_ListOfEvalVariant& DataValueList() const { + return *((SALOME_ListOfEvalVariant *)(DataValue().myPtr)); + }; + // + protected: + SALOME_EvalVariantData myData;// data itself +}; + +#endif -- 2.39.2