1 #include "QtxStdOperations.h"
6 //================================================================
9 //================================================================
10 QtxStdOperations::QtxStdOperations()
14 //================================================================
17 //================================================================
18 QtxStdOperations::~QtxStdOperations()
22 //================================================================
25 //================================================================
26 void QtxStdOperations::bracketsList( QStringList& list, bool open ) const
34 //================================================================
37 //================================================================
38 void QtxStdOperations::opersList( QStringList& list ) const
43 //================================================================
46 //================================================================
47 void QtxStdOperations::addOperations( const QStringList& list )
49 QStringList::const_iterator anIt = list.begin(),
51 for( ; anIt!=aLast; anIt++ )
52 if( myOpers.contains( *anIt )==0 )
53 myOpers.append( *anIt );
56 //================================================================
59 //================================================================
60 void QtxStdOperations::addTypes( const ListOfTypes& list )
62 ListOfTypes::const_iterator anIt = list.begin(),
64 for( ; anIt!=aLast; anIt++ )
65 if( myTypes.contains( *anIt )==0 )
66 myTypes.append( *anIt );
69 //================================================================
72 //================================================================
73 QtxParser::Error QtxStdOperations::isValid( const QString& op,
74 const QVariant::Type t1,
75 const QVariant::Type t2 ) const
77 if( ( t1==QVariant::Invalid || myTypes.contains( t1 ) ) &&
78 ( t2==QVariant::Invalid || myTypes.contains( t2 ) ) &&
79 ( t1!=QVariant::Invalid || t2!=QVariant::Invalid ) )
80 if( prior( op, t1!=QVariant::Invalid && t2!=QVariant::Invalid ) > 0 )
83 return QtxParser::InvalidOperation;
85 return QtxParser::OperandsNotMatch;
95 //================================================================
98 //================================================================
99 QtxArithmetics::QtxArithmetics()
110 aList.append( "<=" );
111 aList.append( ">=" );
112 aList.append( "<>" );
113 aList.append( "!=" ); // same as "<>" - for C++ addicts
114 addOperations( aList );
117 aTypes.append( QVariant::Int );
118 aTypes.append( QVariant::UInt );
119 aTypes.append( QVariant::Double );
123 //================================================================
126 //================================================================
127 QtxArithmetics::~QtxArithmetics()
131 //================================================================
134 //================================================================
135 bool QtxArithmetics::createValue( const QString& str, QtxValue& v ) const
138 v = str.toInt( &ok );
142 v = str.toDouble( &ok );
144 ok = QtxStdOperations::createValue( str, v );
149 //================================================================
152 //================================================================
153 int QtxArithmetics::prior( const QString& op, bool isBin ) const
156 if( op=="<" || op==">" || op=="=" ||
157 op=="<=" || op==">=" || op=="<>" || op=="!=" )
159 else if( op=="+" || op=="-" )
161 else if( op=="*" || op=="/" )
165 else if( op=="+" || op=="-" )
171 void set( QVariant& v1, bool v2 )
173 v1 = QVariant( v2, 0 );
176 //================================================================
179 //================================================================
180 QtxParser::Error QtxArithmetics::calculate( const QString& op,
181 QtxValue& v1, QtxValue& v2 ) const
183 QtxParser::Error err = QtxParser::OK;
185 if( v1.isValid() && v2.isValid() )
187 if( ( v1.type()==QVariant::Int || v1.type()==QVariant::UInt ) &&
188 ( v2.type()==QVariant::Int || v2.type()==QVariant::UInt ) )
190 int _v1 = v1.toInt(),
204 v1 = double( _v1 ) / double( _v2 );
206 err = QtxParser::InvalidResult;
217 else if( op=="<>" || op=="!=" )
220 else if( ( v1.type()==QVariant::Int || v1.type()==QVariant::Double ) &&
221 ( v2.type()==QVariant::Int || v2.type()==QVariant::Double ) )
223 double _v1 = v1.toDouble(),
236 err = QtxParser::InvalidResult;
247 else if( op=="<>" || op=="!=" )
253 if( v2.type()==QVariant::Int )
255 else if( v2.type()==QVariant::Double )
271 //================================================================
274 //================================================================
279 aList.append( "and" );
280 aList.append( "&&" );
281 aList.append( "or" );
282 aList.append( "||" );
283 aList.append( "xor" );
284 aList.append( "not" );
286 aList.append( "imp" );
288 addOperations( aList );
291 aTypes.append( QVariant::Bool );
292 aTypes.append( QVariant::Int );
293 aTypes.append( QVariant::UInt );
297 //================================================================
300 //================================================================
301 QtxLogic::~QtxLogic()
305 //================================================================
308 //================================================================
309 bool QtxLogic::createValue( const QString& str, QtxValue& v ) const
312 if( str.lower()=="true" )
313 v = QtxValue( true, 0 );
314 else if( str.lower()=="false" )
315 v = QtxValue( false, 0 );
317 ok = QtxStdOperations::createValue( str, v );
322 //================================================================
325 //================================================================
326 int QtxLogic::prior( const QString& op, bool isBin ) const
329 if( op=="and" || op=="or" || op=="xor" ||
330 op=="&&" || op=="||" ||
337 else if( op=="not" || op=="!" )
343 bool boolean_value( const QtxValue& v )
345 if( v.type()==QVariant::Bool )
347 else if( v.type()==QVariant::Int )
349 else if( v.type()==QVariant::UInt )
350 return v.toUInt()!=0;
355 //================================================================
358 //================================================================
359 QtxParser::Error QtxLogic::calculate( const QString& op,
360 QtxValue& v1, QtxValue& v2 ) const
362 QtxParser::Error err = QtxParser::OK;
363 bool val1 = boolean_value( v1 ),
364 val2 = boolean_value( v2 );
365 if( v1.isValid() && v2.isValid() )
367 if( op=="and" || op=="&&" )
368 set( v1, val1 && val2 );
369 else if( op=="or" || op=="||" )
370 set( v1, val1 || val2 );
372 set( v1, ( !val1 && val2 ) || ( val1 && !val2 ) );
374 set( v1, !val1 || val2 );
376 set( v1, val1==val2 );
379 if( op=="not" || op=="!" )
390 //================================================================
393 //================================================================
394 QtxFunctions::QtxFunctions()
398 aList.append( "sqrt" );
399 aList.append( "abs" );
400 aList.append( "sin" );
401 aList.append( "cos" );
402 aList.append( "rad2grad" );
403 aList.append( "grad2rad" );
404 addOperations( aList );
407 aTypes.append( QVariant::Int );
408 aTypes.append( QVariant::Double );
412 //================================================================
415 //================================================================
416 QtxFunctions::~QtxFunctions()
420 //================================================================
423 //================================================================
424 bool QtxFunctions::createValue( const QString& str, QtxValue& v ) const
427 v = str.toInt( &ok );
431 v = str.toDouble( &ok );
433 ok = QtxStdOperations::createValue( str, v );
438 //================================================================
441 //================================================================
442 int QtxFunctions::prior( const QString& op, bool isBin ) const
446 else if( op=="sqrt" || op=="abs" || op=="sin" || op=="cos" ||
447 op=="rad2grad" || op=="grad2rad" )
453 //================================================================
456 //================================================================
457 QtxParser::Error QtxFunctions::calculate( const QString& op,
458 QtxValue& v1, QtxValue& v2 ) const
460 QtxParser::Error err = QtxParser::OK;
461 double val = v2.toDouble();
466 err = QtxParser::InvalidResult;
468 if( v2.type()==QVariant::Int )
469 v2 = abs( v2.toInt() );
471 v2 = fabs( v2.toDouble() );
476 else if( op=="grad2rad" )
477 v2 = val * 3.14159256 / 180.0;
478 else if( op=="rad2grad" )
479 v2 = val * 180.0 / 3.14159256;
491 //================================================================
494 //================================================================
495 QtxStrings::QtxStrings()
503 aList.append( "<=" );
504 aList.append( ">=" );
505 aList.append( "<>" );
506 aList.append( "!=" ); // same as "<>" - for C++ addicts
507 aList.append( "length" );
508 aList.append( "lower" );
509 aList.append( "upper" );
510 addOperations( aList );
513 aTypes.append( QVariant::Int );
514 aTypes.append( QVariant::Double );
515 aTypes.append( QVariant::String );
516 aTypes.append( QVariant::CString );
521 //================================================================
524 //================================================================
525 QtxStrings::~QtxStrings()
529 //================================================================
532 //================================================================
533 bool QtxStrings::createValue( const QString& str, QtxValue& v ) const
536 fin = str[ ( int )( str.length()-1 ) ];
537 if( st=="'" && fin=="'" )
539 v = str.mid( 1, str.length()-2 );
543 return QtxStdOperations::createValue( str, v );
546 //================================================================
549 //================================================================
550 int QtxStrings::prior( const QString& op, bool isBin ) const
555 else if( op=="=" || op=="<" || op==">" ||
556 op=="<=" || op==">=" || op=="<>" || op=="!=" )
561 if( op=="length" || op=="lower" || op=="upper" )
567 //================================================================
570 //================================================================
571 QtxParser::Error QtxStrings::calculate( const QString& op,
572 QtxValue& v1, QtxValue& v2 ) const
574 QtxParser::Error err = QtxParser::OK;
575 if( v1.isValid() && v2.isValid() )
577 QString _v1 = v1.toString(),
587 else if( op=="<>" || op=="!=" )
590 set( v1, _v1<_v2 || _v1==_v2 );
592 set( v1, _v1>_v2 || _v1==_v2 );
594 else if( !v1.isValid() && v2.isValid() )
596 QString val = v2.toString();
598 v2 = ( int )val.length();
599 else if( op=="lower" )
601 else if( op=="upper" )
613 //================================================================
616 //================================================================
623 aList.append( "<>" );
624 aList.append( "!=" ); // same as "<>" - for C++ addicts
628 aList.append( "in" );
629 aList.append( "count" );
630 addOperations( aList );
633 aTypes.append( QVariant::List );
637 //================================================================
640 //================================================================
645 //================================================================
648 //================================================================
649 void QtxSets::bracketsList( QStringList& list, bool open ) const
655 QtxStdOperations::bracketsList( list, open );
658 //================================================================
661 //================================================================
662 bool QtxSets::createValue( const QString& str, QtxValue& val ) const
664 return QtxStdOperations::createValue( str, val );
667 //================================================================
670 //================================================================
671 int QtxSets::prior( const QString& op, bool isBin ) const
674 if( op=="=" || op=="<>" || op=="!=" )
676 else if( op=="+" || op=="-" || op=="*" )
684 if( op=="{" || op=="}" )
686 else if( op=="count" )
692 //================================================================
695 //================================================================
696 QtxParser::Error QtxSets::isValid( const QString& op,
697 const QVariant::Type t1,
698 const QVariant::Type t2 ) const
701 return QtxParser::OK;
703 if( t1!=QVariant::Invalid && t2==QVariant::List )
704 return QtxParser::OK;
706 return QtxParser::OperandsNotMatch;
708 return QtxStdOperations::isValid( op, t1, t2 );
711 //================================================================
714 //================================================================
715 void QtxSets::add( ValueSet& set, const QtxValue& v )
717 if( v.isValid() && set.contains( v )==0 )
721 //================================================================
724 //================================================================
725 void QtxSets::add( ValueSet& s1, const ValueSet& s2 )
727 ValueSet::const_iterator anIt = s2.begin(),
729 for( ; anIt!=aLast; anIt++ )
733 //================================================================
736 //================================================================
737 void QtxSets::remove( ValueSet& set, const QtxValue& v )
742 //================================================================
745 //================================================================
746 void QtxSets::remove( ValueSet& s1, const ValueSet& s2 )
748 ValueSet::const_iterator anIt = s2.begin(),
750 for( ; anIt!=aLast; anIt++ )
754 //================================================================
757 //================================================================
758 QtxParser::Error QtxSets::calculate( const QString& op, QtxValue& v1, QtxValue& v2 ) const
760 QtxParser::Error err = QtxParser::OK;
766 add( aNewList, v1.toList() );
770 else if( op=="=" || op=="<>" || op=="!=" || op=="+" || op=="-" || op=="*" )
773 add( aNewList, v1.toList() );
775 if( op=="=" || op=="<>" || op=="!=" || op=="-" )
777 remove( aNewList, v2.toList() );
780 set( v1, aNewList.isEmpty() );
781 else if( op=="<>" || op=="!=" )
782 set( v1, !aNewList.isEmpty() );
788 add( aNewList, v2.toList() );
794 add( toDelete, aNewList );
795 remove( toDelete, v2.toList() );
796 remove( aNewList, toDelete );
800 else if( op=="count" )
801 v2 = ( int )v2.toList().count();
804 if( v1.type()==QVariant::List )
807 ValueSet::const_iterator anIt = v1.toList().begin(),
808 aLast = v1.toList().end();
809 for( ; anIt!=aLast && res; anIt++ )
810 res = v2.toList().contains( *anIt )>0;
816 const QValueList< QVariant >& list = v2.toList();
817 v1 = ( bool )( list.find( v1 )!=list.end() );