From 479b2127e00c790f18b195494c11e1959ffc0eff Mon Sep 17 00:00:00 2001 From: Renaud G Date: Wed, 19 Aug 2015 18:10:24 +0200 Subject: add new validator for support several condition in one operator --- compositevalidator.cpp | 149 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 compositevalidator.cpp (limited to 'compositevalidator.cpp') diff --git a/compositevalidator.cpp b/compositevalidator.cpp new file mode 100644 index 0000000..0715ba2 --- /dev/null +++ b/compositevalidator.cpp @@ -0,0 +1,149 @@ +/*************************************************************************** +* Copyright (C) 2014 by Renaud Guezennec * +* http://renaudguezennec.homelinux.org/accueil,3.html * +* * +* This file is part of DiceParser * +* * +* DiceParser is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program 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 General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program; if not, write to the * +* Free Software Foundation, Inc., * +* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * +***************************************************************************/ +#include "compositevalidator.h" + + +CompositeValidator::BooleanCondition() +{ +} +qint64 CompositeValidator::hasValid(Die* b,bool recursive,bool unhighlight) const +{ + + int i = 0; + qint64 sum = 0; + foreach(Validator* validator, m_validatorList) + { + qint64 val = validator->hasValid(b,recursive,unhighlight); + if(i==0) + { + sum = val; + } + else + { + switch(m_operators[i-1]) + { + case OR: + sum |= val; + break; + case EXCLUSIVE_OR: + sum ^= val;/// @todo may required to be done by hand + break; + case AND: + sum &= val; + break; + } + } + ++i; + } + + /*QList listValues; + if(recursive) + { + listValues = b->getListValue(); + } + else + { + listValues.append(b->getLastRolledValue()); + } + + qint64 sum= 0; + foreach(qint64 value, listValues) + { + switch(m_operator) + { + case Equal: + sum+=(value==m_value)?1:0; + break; + case GreaterThan: + sum+= (value>m_value)?1:0; + break; + case LesserThan: + sum+= (value=m_value)?1:0; + break; + case LesserOrEqual: + sum+= (value<=m_value)?1:0; + break; + } + } + if((unhighlight)&&(sum==0)) + { + b->setHighlighted(false); + } + else + { + b->setHighlighted(true); + } + + return sum;*/ +} + +void CompositeValidator::setOperator(LogicOperator m) +{ + m_operator = m; +} + +void CompositeValidator::setValue(qint64 v) +{ + m_value=v; +} +QString CompositeValidator::toString() +{ + QString str=""; + switch (m_operator) + { + case Equal: + str.append("="); + break; + case GreaterThan: + str.append(">"); + break; + case LesserThan: + str.append("<"); + break; + case GreaterOrEqual: + str.append(">="); + break; + case LesserOrEqual: + str.append("<="); + break; + } + return QString("[%1%2]").arg(str).arg(m_value); +} +quint8 CompositeValidator::getValidRangeSize(quint64 faces) const +{ + switch (m_operator) + { + case Equal: + return 1; + case GreaterThan: + return faces-m_value; + case LesserThan: + return m_value-1; + case GreaterOrEqual: + return faces-(m_value-1); + case LesserOrEqual: + return m_value; + } +} -- cgit v1.2.3-70-g09d2 From bb413e7eab871cbc377eea0ece5444d1c758481b Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 20 Aug 2015 00:31:09 +0200 Subject: first implementation of compositevalidator. --- compositevalidator.cpp | 109 ++++++++++++++++++------------------------------- compositevalidator.h | 13 +++--- 2 files changed, 46 insertions(+), 76 deletions(-) (limited to 'compositevalidator.cpp') diff --git a/compositevalidator.cpp b/compositevalidator.cpp index 0715ba2..cc52fdd 100644 --- a/compositevalidator.cpp +++ b/compositevalidator.cpp @@ -22,7 +22,7 @@ #include "compositevalidator.h" -CompositeValidator::BooleanCondition() +CompositeValidator::CompositeValidator() { } qint64 CompositeValidator::hasValid(Die* b,bool recursive,bool unhighlight) const @@ -30,7 +30,7 @@ qint64 CompositeValidator::hasValid(Die* b,bool recursive,bool unhighlight) cons int i = 0; qint64 sum = 0; - foreach(Validator* validator, m_validatorList) + foreach(const Validator* validator, *m_validatorList) { qint64 val = validator->hasValid(b,recursive,unhighlight); if(i==0) @@ -39,7 +39,7 @@ qint64 CompositeValidator::hasValid(Die* b,bool recursive,bool unhighlight) cons } else { - switch(m_operators[i-1]) + switch(m_operators->at(i-1)) { case OR: sum |= val; @@ -55,63 +55,13 @@ qint64 CompositeValidator::hasValid(Die* b,bool recursive,bool unhighlight) cons ++i; } - /*QList listValues; - if(recursive) - { - listValues = b->getListValue(); - } - else - { - listValues.append(b->getLastRolledValue()); - } - - qint64 sum= 0; - foreach(qint64 value, listValues) - { - switch(m_operator) - { - case Equal: - sum+=(value==m_value)?1:0; - break; - case GreaterThan: - sum+= (value>m_value)?1:0; - break; - case LesserThan: - sum+= (value=m_value)?1:0; - break; - case LesserOrEqual: - sum+= (value<=m_value)?1:0; - break; - } - } - if((unhighlight)&&(sum==0)) - { - b->setHighlighted(false); - } - else - { - b->setHighlighted(true); - } - - return sum;*/ + return sum; } -void CompositeValidator::setOperator(LogicOperator m) -{ - m_operator = m; -} - -void CompositeValidator::setValue(qint64 v) -{ - m_value=v; -} QString CompositeValidator::toString() { QString str=""; - switch (m_operator) + /*switch (m_operator) { case Equal: str.append("="); @@ -129,21 +79,40 @@ QString CompositeValidator::toString() str.append("<="); break; } - return QString("[%1%2]").arg(str).arg(m_value); + return QString("[%1%2]").arg(str).arg(m_value);*/ + return str; } -quint8 CompositeValidator::getValidRangeSize(quint64 faces) const +quint64 CompositeValidator::getValidRangeSize(quint64 faces) const { - switch (m_operator) - { - case Equal: - return 1; - case GreaterThan: - return faces-m_value; - case LesserThan: - return m_value-1; - case GreaterOrEqual: - return faces-(m_value-1); - case LesserOrEqual: - return m_value; - } + quint64 sum =0; + int i = -1; + foreach(Validator* tmp,*m_validatorList) + { + quint64 rel = tmp->getValidRangeSize(faces); + LogicOperation opt; + if(i>=0) + { + opt = m_operators->at(i); + } + if(opt == OR) + { + sum += rel; + } + else if((opt == AND)&&(opt == EXCLUSIVE_OR)) + { + sum = qMax(rel,sum); + } + ++i; + } + + return sum; +} +void CompositeValidator::setOperationList(QVector* m) +{ + m_operators = m; +} + +void CompositeValidator::setValidatorList(QList* m) +{ + m_validatorList = m; } diff --git a/compositevalidator.h b/compositevalidator.h index b207db9..789f33f 100644 --- a/compositevalidator.h +++ b/compositevalidator.h @@ -24,6 +24,7 @@ #include #include +#include #include #include "validator.h" @@ -39,16 +40,16 @@ public: virtual qint64 hasValid(Die* b,bool recursive, bool unhighlight = false) const; - void addOperation(LogicOperation m); - void setValue(qint64); + void setOperationList(QVector* m); + void setValidatorList(QList*); + QString toString(); - virtual quint8 getValidRangeSize(quint64 faces) const; + virtual quint64 getValidRangeSize(quint64 faces) const; private: - QVector m_operators; - qint64 m_value; - QList m_validatorList; + QVector* m_operators; + QList* m_validatorList; }; #endif // BOOLEANCONDITION_H -- cgit v1.2.3-70-g09d2