diff options
| author | 2016-11-28 00:36:15 +0100 | |
|---|---|---|
| committer | 2016-11-28 00:36:15 +0100 | |
| commit | 13e297604b00615d47352538e3c82ea1ac700d4c (patch) | |
| tree | 567efe03594d261831897f03a7821c4c6eeac59b | |
| parent | cdd1716bd1aa8d9bbd0137aa98ff24867b6b3a0e (diff) | |
| download | OneRoll-13e297604b00615d47352538e3c82ea1ac700d4c.tar.gz OneRoll-13e297604b00615d47352538e3c82ea1ac700d4c.zip | |
-Add API for copying a branch of execution node.
-Allow to use if node several times.
51 files changed, 608 insertions, 40 deletions
diff --git a/booleancondition.cpp b/booleancondition.cpp index 585620a..7be9836 100644 --- a/booleancondition.cpp +++ b/booleancondition.cpp @@ -128,3 +128,13 @@ quint64 BooleanCondition::getValidRangeSize(quint64 faces) const return faces-1; } } +Validator* BooleanCondition::getCopy() const +{ + BooleanCondition* val = new BooleanCondition(); + val->setOperator(m_operator); + val->setValue(m_value); + + + return val; + +} diff --git a/booleancondition.h b/booleancondition.h index fa178b2..86dad72 100644 --- a/booleancondition.h +++ b/booleancondition.h @@ -41,6 +41,11 @@ public: QString toString(); virtual quint64 getValidRangeSize(quint64 faces) const; + /** + * @brief getCopy + * @return + */ + virtual Validator* getCopy() const; private: LogicOperator m_operator; diff --git a/compositevalidator.cpp b/compositevalidator.cpp index 2de9dba..fce4260 100644 --- a/compositevalidator.cpp +++ b/compositevalidator.cpp @@ -26,6 +26,15 @@ CompositeValidator::CompositeValidator() : m_operators(NULL),m_validatorList(NULL) { } + +CompositeValidator::~CompositeValidator() +{ + qDeleteAll(*m_validatorList); + if(NULL!=m_operators) + { + delete m_operators; + } +} qint64 CompositeValidator::hasValid(Die* b,bool recursive,bool unhighlight) const { @@ -87,7 +96,7 @@ quint64 CompositeValidator::getValidRangeSize(quint64 faces) const { quint64 sum =0; int i = -1; - foreach(Validator* tmp,*m_validatorList) + for(Validator* tmp :*m_validatorList) { quint64 rel = tmp->getValidRangeSize(faces); LogicOperation opt; @@ -117,3 +126,19 @@ void CompositeValidator::setValidatorList(QList<Validator*>* m) { m_validatorList = m; } +Validator* CompositeValidator::getCopy() const +{ + QVector<LogicOperation>* vector = new QVector<LogicOperation>(); + *vector = *m_operators; + + QList<Validator*>* list= new QList<Validator*>(); + for(auto val : *m_validatorList) + { + list->append(val->getCopy()); + } + + CompositeValidator* val = new CompositeValidator(); + val->setOperationList(vector); + val->setValidatorList(list); + return val; +} diff --git a/compositevalidator.h b/compositevalidator.h index a5afcbf..788cd10 100644 --- a/compositevalidator.h +++ b/compositevalidator.h @@ -37,6 +37,8 @@ class CompositeValidator : public Validator public: enum LogicOperation { OR, EXCLUSIVE_OR , AND}; CompositeValidator(); + virtual ~CompositeValidator(); + virtual qint64 hasValid(Die* b,bool recursive, bool unhighlight = false) const; @@ -47,6 +49,7 @@ public: virtual quint64 getValidRangeSize(quint64 faces) const; + virtual Validator* getCopy() const; private: QVector<LogicOperation>* m_operators; QList<Validator*>* m_validatorList; diff --git a/diceparser.cpp b/diceparser.cpp index 5a13fbd..e71a24c 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -992,6 +992,7 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous)//, case ifOperator: { IfNode* nodeif = new IfNode(); + nodeif->setConditionType(m_parsingToolbox->readConditionType(str)); Validator* validator = m_parsingToolbox->readCompositeValidator(str); if(NULL!=validator) { diff --git a/node/countexecutenode.cpp b/node/countexecutenode.cpp index 89b21e5..1f78fd2 100644 --- a/node/countexecutenode.cpp +++ b/node/countexecutenode.cpp @@ -35,7 +35,10 @@ void CountExecuteNode::run(ExecutionNode *previous) qint64 sum = 0; foreach(Die* dice,diceList) { - sum+=m_validator->hasValid(dice,true,true); + if(NULL!=m_validator) + { + sum+=m_validator->hasValid(dice,true,true); + } } m_scalarResult->setValue(sum); @@ -66,5 +69,20 @@ qint64 CountExecuteNode::getPriority() const } - return priority; + return priority; +} + +ExecutionNode* CountExecuteNode::getCopy() const +{ + CountExecuteNode* node = new CountExecuteNode(); + if(NULL!=m_validator) + { + node->setValidator(m_validator->getCopy()); + } + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; + } diff --git a/node/countexecutenode.h b/node/countexecutenode.h index 15b2509..8a6132b 100644 --- a/node/countexecutenode.h +++ b/node/countexecutenode.h @@ -36,7 +36,11 @@ public: * @return */ virtual qint64 getPriority() const; - + /** + * @brief getCopy + * @return + */ + virtual ExecutionNode* getCopy() const; private: ScalarResult* m_scalarResult; Validator* m_validator; diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp index e6f2950..2641ab7 100644 --- a/node/dicerollernode.cpp +++ b/node/dicerollernode.cpp @@ -36,6 +36,7 @@ void DiceRollerNode::run(ExecutionNode* previous) die->setFaces(m_faces); die->setBase(m_offset); die->roll(); + //qDebug() << die->getValue() << "value"; m_diceResult->insertResult(die); } if(NULL!=m_nextNode) @@ -72,3 +73,12 @@ qint64 DiceRollerNode::getPriority() const return priority; } +ExecutionNode* DiceRollerNode::getCopy() const +{ + DiceRollerNode* node = new DiceRollerNode(m_faces,m_offset); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; +} diff --git a/node/dicerollernode.h b/node/dicerollernode.h index 2de1cfe..4e2f9d7 100644 --- a/node/dicerollernode.h +++ b/node/dicerollernode.h @@ -40,6 +40,9 @@ public: * @return priority for dice roll: 4 (higher) */ virtual qint64 getPriority() const; + + virtual ExecutionNode* getCopy() const; + //private members private: quint64 m_diceCount; diff --git a/node/executionnode.h b/node/executionnode.h index c015595..7a00db8 100644 --- a/node/executionnode.h +++ b/node/executionnode.h @@ -69,6 +69,13 @@ public: * @return */ virtual QString getHelp(); + + /** + * @brief getCopy + * @return should return a copy of that node. + */ + virtual ExecutionNode* getCopy() const = 0; + protected: /** * @brief m_nextNode diff --git a/node/explosedicenode.cpp b/node/explosedicenode.cpp index cdbeb6c..33c15d2 100644 --- a/node/explosedicenode.cpp +++ b/node/explosedicenode.cpp @@ -66,3 +66,17 @@ qint64 ExploseDiceNode::getPriority() const return priority; } + +ExecutionNode* ExploseDiceNode::getCopy() const +{ + ExploseDiceNode* node = new ExploseDiceNode(); + if(NULL!=m_validator) + { + node->setValidator(m_validator->getCopy()); + } + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; +} diff --git a/node/explosedicenode.h b/node/explosedicenode.h index 65c0abd..ceb35b8 100644 --- a/node/explosedicenode.h +++ b/node/explosedicenode.h @@ -19,6 +19,7 @@ public: virtual QString toString(bool )const; virtual qint64 getPriority() const; + virtual ExecutionNode *getCopy() const; protected: DiceResult* m_diceResult; Validator* m_validator; diff --git a/node/filternode.cpp b/node/filternode.cpp index 8fe99c3..8f0baa9 100644 --- a/node/filternode.cpp +++ b/node/filternode.cpp @@ -74,3 +74,16 @@ qint64 FilterNode::getPriority() const return priority; } +ExecutionNode* FilterNode::getCopy() const +{ + FilterNode* node = new FilterNode(); + if(NULL!=m_validator) + { + node->setValidator(m_validator->getCopy()); + } + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; +} diff --git a/node/filternode.h b/node/filternode.h index ca08c3b..02e37af 100644 --- a/node/filternode.h +++ b/node/filternode.h @@ -28,6 +28,7 @@ public: */ virtual qint64 getPriority() const; + virtual ExecutionNode* getCopy() const; private: DiceResult* m_diceResult; Validator* m_validator; diff --git a/node/helpnode.cpp b/node/helpnode.cpp index 30fa904..4425969 100644 --- a/node/helpnode.cpp +++ b/node/helpnode.cpp @@ -68,3 +68,13 @@ void HelpNode::setHelpPath(QString path) { m_path = path; } + +ExecutionNode* HelpNode::getCopy() const +{ + HelpNode* node = new HelpNode(); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; +} diff --git a/node/helpnode.h b/node/helpnode.h index 14edde4..2b82af0 100644 --- a/node/helpnode.h +++ b/node/helpnode.h @@ -56,6 +56,11 @@ public: * @param path */ void setHelpPath(QString path); + /** + * @brief getCopy + * @return + */ + virtual ExecutionNode* getCopy() const; private: QString m_path; diff --git a/node/ifnode.cpp b/node/ifnode.cpp index 8cf667c..61049cd 100644 --- a/node/ifnode.cpp +++ b/node/ifnode.cpp @@ -21,7 +21,7 @@ #include "result/diceresult.h" IfNode::IfNode() - : m_validator(NULL),m_true(NULL),m_false(NULL) + : m_validator(NULL),m_true(NULL),m_false(NULL),m_conditionType(AllOfThem) { //m_result = new DiceResult(); } @@ -40,31 +40,89 @@ void IfNode::run(ExecutionNode *previous) } ExecutionNode* previousLoop = previous; ExecutionNode* nextNode = NULL; + bool runNext = (NULL==m_nextNode) ? false : true; Result* previousResult = previous->getResult(); m_result = previousResult; - DiceResult* previousDiceResult = dynamic_cast<DiceResult*>(previousResult); - if(NULL!=previousDiceResult) + if(NULL!=m_result) { qreal value = previousResult->getResult(Result::SCALAR).toReal(); - QList<Die*> diceList=previousDiceResult->getResultList(); - - if(NULL!=m_validator) { - if(!diceList.isEmpty()) + DiceResult* previousDiceResult = dynamic_cast<DiceResult*>(previousResult); + if(NULL!=previousDiceResult) { - foreach(Die* dice,diceList) + QList<Die*> diceList=previousDiceResult->getResultList(); + if(m_conditionType == OnEach) { - if(m_validator->hasValid(dice,true,true)) + for(Die* dice : diceList) { - nextNode=m_true; + if(m_validator->hasValid(dice,true,true)) + { + nextNode = (NULL==m_true) ? NULL: m_true->getCopy(); + } + else + { + nextNode = (NULL==m_false) ? NULL: m_false->getCopy(); + } + if(NULL!=nextNode) + { + if(NULL==previousLoop->getNextNode()) + { + previousLoop->setNextNode(nextNode); + } + if(NULL==m_nextNode) + { + m_nextNode = nextNode; + } + nextNode->run(previousLoop); + previousLoop = getLeafNode(nextNode); + } } - else + } + else + { + bool trueForAll=true; + bool falseForAll=true; + + bool oneIsTrue=false; + bool oneIsFalse=false; + + for(Die* dice : diceList) { - nextNode=m_false; + bool result = m_validator->hasValid(dice,true,true); + qDebug() << result << m_conditionType; + trueForAll = trueForAll ? result : false; + falseForAll = falseForAll ? result : false; + + oneIsTrue = (oneIsTrue==false) ? result : true; + oneIsFalse = (oneIsFalse==false) ? result : true; } + if(m_conditionType==OneOfThem) + { + if(oneIsTrue) + { + nextNode = (NULL==m_true) ? NULL: m_true->getCopy(); + } + else if(oneIsFalse) + { + nextNode = (NULL==m_false) ? NULL: m_false->getCopy(); + } + } + else if(m_conditionType==AllOfThem) + { + if(trueForAll) + { + nextNode = (NULL==m_true) ? NULL: m_true->getCopy(); + } + else if(falseForAll) + { + nextNode = (NULL==m_false) ? NULL: m_false->getCopy(); + } + } + + if(NULL!=nextNode) { if(NULL==m_nextNode) @@ -81,12 +139,28 @@ void IfNode::run(ExecutionNode *previous) Die* dice = new Die(); dice->setValue(value); dice->setFaces(value); - m_validator->hasValid(dice,true,true); + if(m_validator->hasValid(dice,true,true)) + { + nextNode=m_true; + } + else + { + nextNode=m_false; + } + if(NULL!=nextNode) + { + if(NULL==m_nextNode) + { + m_nextNode = nextNode; + } + nextNode->run(previousLoop); + previousLoop = getLeafNode(nextNode); + } } } } - if(NULL!=m_nextNode) + if((NULL!=m_nextNode)&&(runNext)) { m_nextNode->run(previousLoop); } @@ -180,3 +254,36 @@ ExecutionNode* IfNode::getLeafNode(ExecutionNode* node) } return next; } + +IfNode::ConditionType IfNode::getConditionType() const +{ + return m_conditionType; +} + +void IfNode::setConditionType(const IfNode::ConditionType &conditionType) +{ + qDebug() << "set condisition type" << conditionType; + m_conditionType = conditionType; +} +ExecutionNode* IfNode::getCopy() const +{ + IfNode* node = new IfNode(); + if(NULL!=m_validator) + { + node->setValidator(m_validator->getCopy()); + } + if(NULL!=m_false) + { + node->setInstructionFalse(m_false->getCopy()); + } + if(NULL!=m_true) + { + node->setInstructionTrue(m_true->getCopy()); + } + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; + +} diff --git a/node/ifnode.h b/node/ifnode.h index ed9e8de..da8bb0b 100644 --- a/node/ifnode.h +++ b/node/ifnode.h @@ -31,13 +31,44 @@ class IfNode : public ExecutionNode { public: + /** + * @brief The ConditionType enum + */ + enum ConditionType {OnEach,OneOfThem,AllOfThem}; + /** + * @brief IfNode + */ IfNode(); + /** + * @brief ~IfNode + */ virtual ~IfNode(); + /** + * @brief run + * @param previous + */ virtual void run(ExecutionNode* previous = NULL); + /** + * @brief setValidator + */ virtual void setValidator(Validator* ); + /** + * @brief setInstructionTrue + */ virtual void setInstructionTrue(ExecutionNode*); + /** + * @brief setInstructionFalse + */ virtual void setInstructionFalse(ExecutionNode*); + /** + * @brief toString + * @return + */ virtual QString toString(bool )const; + /** + * @brief getPriority + * @return + */ virtual qint64 getPriority() const; @@ -46,12 +77,29 @@ public: */ virtual void generateDotTree(QString&); + /** + * @brief getCopy + * @return + */ + virtual ExecutionNode *getCopy() const; + /** + * @brief getConditionType + * @return + */ + ConditionType getConditionType() const; + + /** + * @brief setConditionType + * @param conditionType + */ + void setConditionType(const IfNode::ConditionType &conditionType); protected: ExecutionNode *getLeafNode(ExecutionNode *node); protected: Validator* m_validator; + ConditionType m_conditionType; ExecutionNode* m_true; ExecutionNode* m_false; diff --git a/node/jumpbackwardnode.cpp b/node/jumpbackwardnode.cpp index 0b2cbf6..b7af106 100644 --- a/node/jumpbackwardnode.cpp +++ b/node/jumpbackwardnode.cpp @@ -158,6 +158,15 @@ void JumpBackwardNode::run(ExecutionNode* previous) } } } +} +ExecutionNode* JumpBackwardNode::getCopy() const +{ + JumpBackwardNode* node = new JumpBackwardNode(); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; } diff --git a/node/jumpbackwardnode.h b/node/jumpbackwardnode.h index dd70b11..cb87181 100644 --- a/node/jumpbackwardnode.h +++ b/node/jumpbackwardnode.h @@ -49,7 +49,16 @@ public: * @return */ virtual qint64 getPriority() const; + /** + * @brief generateDotTree + * @param s + */ virtual void generateDotTree(QString &s); + /** + * @brief getCopy + * @return + */ + virtual ExecutionNode *getCopy() const; private: DiceResult* m_diceResult; ExecutionNode* m_backwardNode; diff --git a/node/keepdiceexecnode.cpp b/node/keepdiceexecnode.cpp index 40a3fce..ab961af 100644 --- a/node/keepdiceexecnode.cpp +++ b/node/keepdiceexecnode.cpp @@ -48,7 +48,7 @@ void KeepDiceExecNode::run(ExecutionNode* previous) m_errors.insert(TOO_MANY_DICE,QObject::tr(" You ask to keep %1 dice but the result only has %2").arg(m_numberOfDice).arg(diceList.size())); } - foreach(Die* tmp,diceList.mid(m_numberOfDice,-1)) + for(Die* tmp : diceList.mid(m_numberOfDice,-1)) { tmp->setHighlighted(false); } @@ -86,3 +86,15 @@ qint64 KeepDiceExecNode::getPriority() const return priority; } + +ExecutionNode* KeepDiceExecNode::getCopy() const +{ + KeepDiceExecNode* node = new KeepDiceExecNode(); + node->setDiceKeepNumber(m_numberOfDice); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; + +} diff --git a/node/keepdiceexecnode.h b/node/keepdiceexecnode.h index f86daa5..4b41c95 100644 --- a/node/keepdiceexecnode.h +++ b/node/keepdiceexecnode.h @@ -36,6 +36,7 @@ public: virtual void setDiceKeepNumber(quint64 ); virtual QString toString(bool)const; virtual qint64 getPriority() const; + virtual ExecutionNode *getCopy() const; private: quint64 m_numberOfDice; DiceResult* m_diceResult; diff --git a/node/listaliasnode.cpp b/node/listaliasnode.cpp index 9ced186..91644d4 100644 --- a/node/listaliasnode.cpp +++ b/node/listaliasnode.cpp @@ -75,10 +75,19 @@ QString ListAliasNode::toString(bool wl) const { return m_id; } - - } qint64 ListAliasNode::getPriority() const { return 0; } + +ExecutionNode* ListAliasNode::getCopy() const +{ + ListAliasNode* node = new ListAliasNode(m_aliasList); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; + +} diff --git a/node/listaliasnode.h b/node/listaliasnode.h index 2f83a1b..19212cd 100644 --- a/node/listaliasnode.h +++ b/node/listaliasnode.h @@ -54,6 +54,8 @@ public: */ virtual qint64 getPriority() const; + virtual ExecutionNode *getCopy() const; + private: QList<DiceAlias*>* m_aliasList; }; diff --git a/node/listsetrollnode.cpp b/node/listsetrollnode.cpp index 7674720..fa66710 100644 --- a/node/listsetrollnode.cpp +++ b/node/listsetrollnode.cpp @@ -143,3 +143,17 @@ void ListSetRollNode::getValueFromDie(Die* die,QStringList& rollResult) } } } +ExecutionNode* ListSetRollNode::getCopy() const +{ + ListSetRollNode* node = new ListSetRollNode(); + QList<Range> dataList = m_rangeList; + node->setRangeList(dataList); + node->setUnique(m_unique); + node->setListValue(m_values); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; + +} diff --git a/node/listsetrollnode.h b/node/listsetrollnode.h index 05e7ea4..c140d8d 100644 --- a/node/listsetrollnode.h +++ b/node/listsetrollnode.h @@ -44,7 +44,7 @@ public: void setListValue(QStringList); void setUnique(bool ); void setRangeList(QList<Range>&); - + virtual ExecutionNode *getCopy() const; private: void getValueFromDie(Die* die,QStringList& rollResult); diff --git a/node/mergenode.cpp b/node/mergenode.cpp index 16a6649..5e65df3 100644 --- a/node/mergenode.cpp +++ b/node/mergenode.cpp @@ -41,7 +41,7 @@ void MergeNode::run(ExecutionNode* previous) { ///@todo improve here to set homogeneous while is really m_diceResult->setHomogeneous(false); - foreach(Die* die, dice->getResultList()) + for(Die* die : dice->getResultList()) { if(!m_diceResult->getResultList().contains(die)) { @@ -62,7 +62,7 @@ QString MergeNode::toString(bool withLabel) const { if(withLabel) { - return QString("%1 [label=\"Merge Node %2\"]").arg(m_id).arg(m_number); + return QString("%1 [label=\"Merge Node\"]").arg(m_id); } else { @@ -78,3 +78,13 @@ qint64 MergeNode::getPriority() const } return priority; } +ExecutionNode* MergeNode::getCopy() const +{ + MergeNode* node = new MergeNode(); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; + +} diff --git a/node/mergenode.h b/node/mergenode.h index e32faab..aebf9e2 100644 --- a/node/mergenode.h +++ b/node/mergenode.h @@ -35,8 +35,8 @@ public: void run(ExecutionNode* previous); virtual QString toString(bool withLabel)const; virtual qint64 getPriority() const; + virtual ExecutionNode *getCopy() const; private: - qint64 m_number; DiceResult* m_diceResult; }; diff --git a/node/numbernode.cpp b/node/numbernode.cpp index 01a170e..7a2dcff 100644 --- a/node/numbernode.cpp +++ b/node/numbernode.cpp @@ -66,3 +66,14 @@ qint64 NumberNode::getPriority() const return priority; } +ExecutionNode* NumberNode::getCopy() const +{ + NumberNode* node = new NumberNode(); + node->setNumber(m_number); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; + +} diff --git a/node/numbernode.h b/node/numbernode.h index c96a47a..9f1b751 100644 --- a/node/numbernode.h +++ b/node/numbernode.h @@ -36,6 +36,7 @@ public: void setNumber(qint64); virtual QString toString(bool withLabel)const; virtual qint64 getPriority() const; + virtual ExecutionNode *getCopy() const; private: qint64 m_number; ScalarResult* m_scalarResult; diff --git a/node/paintnode.cpp b/node/paintnode.cpp index 02a9358..5a85590 100644 --- a/node/paintnode.cpp +++ b/node/paintnode.cpp @@ -124,3 +124,12 @@ void PainterNode::insertColorItem(QString color, int value) ColorItem item(color,value); m_colors.append(item); } +ExecutionNode* PainterNode::getCopy() const +{ + PainterNode* node = new PainterNode(); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; +} diff --git a/node/paintnode.h b/node/paintnode.h index 52a9ddb..ffd8ae1 100644 --- a/node/paintnode.h +++ b/node/paintnode.h @@ -40,7 +40,9 @@ private: int m_colorNumber; QString m_color; }; - +/** + * @brief The PainterNode class means to manage color attribute of dice. + */ class PainterNode : public ExecutionNode { public: @@ -51,6 +53,7 @@ public: virtual QString toString(bool )const; virtual qint64 getPriority() const; void insertColorItem(QString color, int value); + virtual ExecutionNode *getCopy() const; protected: QList<ColorItem> m_colors; }; diff --git a/node/parenthesesnode.cpp b/node/parenthesesnode.cpp index 7eefaba..2383226 100644 --- a/node/parenthesesnode.cpp +++ b/node/parenthesesnode.cpp @@ -66,3 +66,17 @@ qint64 ParenthesesNode::getPriority() const qint64 priority=3; return priority; } +ExecutionNode* ParenthesesNode::getCopy() const +{ + ParenthesesNode* node = new ParenthesesNode(); + if(NULL!=m_internalNode) + { + node->setInternelNode(m_internalNode->getCopy()); + } + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; + +} diff --git a/node/parenthesesnode.h b/node/parenthesesnode.h index fc0799d..df3c32a 100644 --- a/node/parenthesesnode.h +++ b/node/parenthesesnode.h @@ -36,6 +36,7 @@ public: void setInternelNode(ExecutionNode* node); virtual QString toString(bool)const; virtual qint64 getPriority() const; + virtual ExecutionNode *getCopy() const; private: ExecutionNode* m_internalNode; }; diff --git a/node/rerolldicenode.cpp b/node/rerolldicenode.cpp index ce8f109..341b6d2 100644 --- a/node/rerolldicenode.cpp +++ b/node/rerolldicenode.cpp @@ -73,3 +73,14 @@ qint64 RerollDiceNode::getPriority() const return priority; } +ExecutionNode* RerollDiceNode::getCopy() const +{ + RerollDiceNode* node = new RerollDiceNode(); + node->setValidator(m_validator); + node->setAddingMode(m_adding); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; +} diff --git a/node/rerolldicenode.h b/node/rerolldicenode.h index 2a98b4a..add7a48 100644 --- a/node/rerolldicenode.h +++ b/node/rerolldicenode.h @@ -52,6 +52,12 @@ public: */ virtual qint64 getPriority() const; + /** + * @brief getCopy + * @return + */ + virtual ExecutionNode* getCopy() const; + private: DiceResult* m_myDiceResult; bool m_adding; diff --git a/node/scalaroperatornode.cpp b/node/scalaroperatornode.cpp index 692b09f..1d004cb 100644 --- a/node/scalaroperatornode.cpp +++ b/node/scalaroperatornode.cpp @@ -221,17 +221,28 @@ QMap<ExecutionNode::DICE_ERROR_CODE,QString> ScalarOperatorNode::getExecutionErr { if(NULL!=m_internalNode) { - foreach (ExecutionNode::DICE_ERROR_CODE key, m_internalNode->getExecutionErrorMap().keys()) + for (ExecutionNode::DICE_ERROR_CODE key: m_internalNode->getExecutionErrorMap().keys()) { m_errors.insert(key,m_internalNode->getExecutionErrorMap().value(key)); } } if(NULL!=m_nextNode) { - foreach (ExecutionNode::DICE_ERROR_CODE key, m_nextNode->getExecutionErrorMap().keys()) + for (ExecutionNode::DICE_ERROR_CODE key: m_nextNode->getExecutionErrorMap().keys()) { m_errors.insert(key,m_nextNode->getExecutionErrorMap().value(key)); } } return m_errors; } +ExecutionNode* ScalarOperatorNode::getCopy() const +{ + ScalarOperatorNode* node = new ScalarOperatorNode(); + node->setInternalNode(m_internalNode->getCopy()); + node->setArithmeticOperator(m_arithmeticOperator); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; +} diff --git a/node/scalaroperatornode.h b/node/scalaroperatornode.h index 7fa9b6b..4b21b8c 100644 --- a/node/scalaroperatornode.h +++ b/node/scalaroperatornode.h @@ -35,30 +35,84 @@ class ScalarOperatorNode : public ExecutionNode { public: + /** + * @brief The ArithmeticOperator enum + */ enum ArithmeticOperator {PLUS,MINUS,DIVIDE,MULTIPLICATION}; + /** + * @brief ScalarOperatorNode + */ ScalarOperatorNode(); - virtual ~ScalarOperatorNode(); + /** + * @brief ~ScalarOperatorNode + */ + virtual ~ScalarOperatorNode(); + /** + * @brief run + */ virtual void run(ExecutionNode*); - + /** + * @brief setInternalNode + * @param node + */ void setInternalNode(ExecutionNode* node); - + /** + * @brief toString + * @param wl + * @return + */ virtual QString toString(bool wl)const; + /** + * @brief getPriority + * @return + */ virtual qint64 getPriority() const; - + /** + * @brief generateDotTree + * @param s + */ void generateDotTree(QString& s); /** * @brief getErrorList * @return */ virtual QMap<ExecutionNode::DICE_ERROR_CODE,QString> getExecutionErrorMap(); - + /** + * @brief getArithmeticOperator + * @return + */ ScalarOperatorNode::ArithmeticOperator getArithmeticOperator() const; + /** + * @brief setArithmeticOperator + * @param arithmeticOperator + */ void setArithmeticOperator(const ScalarOperatorNode::ArithmeticOperator &arithmeticOperator); + /** + * @brief getCopy + * @return + */ + virtual ExecutionNode *getCopy() const; private: + /** + * @brief add + * @return + */ static qint64 add(qint64,qint64); + /** + * @brief substract + * @return + */ static qint64 substract(qint64,qint64); + /** + * @brief divide not static because of error management + * @return + */ qreal divide(qint64,qint64); + /** + * @brief multiple + * @return + */ static qint64 multiple(qint64,qint64); private: diff --git a/node/sortresult.cpp b/node/sortresult.cpp index f72d884..8dee7d5 100644 --- a/node/sortresult.cpp +++ b/node/sortresult.cpp @@ -45,7 +45,6 @@ void SortResultNode::run(ExecutionNode* node) QList<Die*> diceList=previousDiceResult->getResultList(); QList<Die*> diceList2=m_diceResult->getResultList(); - // half-interval search sorting for(int i = 0; i<diceList.size();++i) { @@ -128,3 +127,14 @@ qint64 SortResultNode::getPriority() const return priority; } +ExecutionNode* SortResultNode::getCopy() const +{ + SortResultNode* node = new SortResultNode(); + node->setSortAscending(m_ascending); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; + +} diff --git a/node/sortresult.h b/node/sortresult.h index f0c9269..804841d 100644 --- a/node/sortresult.h +++ b/node/sortresult.h @@ -55,7 +55,11 @@ public: * @return */ virtual qint64 getPriority() const; - + /** + * @brief getCopy + * @return + */ + virtual ExecutionNode *getCopy() const; private: bool m_ascending; DiceResult* m_diceResult; diff --git a/node/startingnode.cpp b/node/startingnode.cpp index 3a300b4..3cd3352 100644 --- a/node/startingnode.cpp +++ b/node/startingnode.cpp @@ -54,3 +54,13 @@ qint64 StartingNode::getPriority() const } return priority; } +ExecutionNode* StartingNode::getCopy() const +{ + StartingNode* node = new StartingNode(); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; + +} diff --git a/node/startingnode.h b/node/startingnode.h index 834a9dc..ff171e9 100644 --- a/node/startingnode.h +++ b/node/startingnode.h @@ -47,6 +47,11 @@ public: * @return */ virtual qint64 getPriority() const; + /** + * @brief getCopy + * @return + */ + virtual ExecutionNode *getCopy() const; }; #endif // STARTINGNODE_H diff --git a/node/stringnode.cpp b/node/stringnode.cpp index 157f595..d427cd6 100644 --- a/node/stringnode.cpp +++ b/node/stringnode.cpp @@ -115,7 +115,16 @@ qint64 StringNode::getPriority() const { priority = m_nextNode->getPriority(); } - - return priority; } +ExecutionNode* StringNode::getCopy() const +{ + StringNode* node = new StringNode(); + node->setString(m_data); + if(NULL!=m_nextNode) + { + node->setNextNode(m_nextNode->getCopy()); + } + return node; + +} diff --git a/node/stringnode.h b/node/stringnode.h index cf70d02..f693a19 100644 --- a/node/stringnode.h +++ b/node/stringnode.h @@ -15,7 +15,11 @@ public: void setString(QString str); virtual QString toString(bool withLabel)const; virtual qint64 getPriority() const; - + /** + * @brief getCopy + * @return + */ + virtual ExecutionNode *getCopy() const; private: QString m_data; StringResult* m_stringResult; diff --git a/operationcondition.cpp b/operationcondition.cpp index 943cac9..a4ac2bd 100644 --- a/operationcondition.cpp +++ b/operationcondition.cpp @@ -101,3 +101,13 @@ quint64 OperationCondition::getValidRangeSize(quint64 faces) const { return faces/m_value; } +Validator* OperationCondition::getCopy() const +{ + OperationCondition* val = new OperationCondition(); + val->setOperator(m_operator); + val->setValue(m_value); + BooleanCondition* boolean = dynamic_cast<BooleanCondition*>(m_boolean->getCopy()); + val->setBoolean(boolean); + return val; + +} diff --git a/operationcondition.h b/operationcondition.h index a1a1b55..c3da827 100644 --- a/operationcondition.h +++ b/operationcondition.h @@ -42,7 +42,7 @@ public: BooleanCondition* getBoolean() const; void setBoolean(BooleanCondition *boolean); - + virtual Validator* getCopy() const; private: ConditionOperator m_operator; BooleanCondition* m_boolean; diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index f792d7a..8bd52b7 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -24,6 +24,7 @@ #include "parsingtoolbox.h" #include "node/sortresult.h" + QHash<QString,QString>* ParsingToolBox::m_variableHash = NULL; ParsingToolBox::ParsingToolBox() @@ -199,6 +200,22 @@ Validator* ParsingToolBox::readValidator(QString& str) } return returnVal; } +IfNode::ConditionType ParsingToolBox::readConditionType(QString& str) +{ + IfNode::ConditionType type = IfNode::OnEach; + if(str.startsWith('.')) + { + str=str.remove(0,1); + type = IfNode::OneOfThem; + } + else if(str.startsWith('*')) + { + str=str.remove(0,1); + type = IfNode::AllOfThem; + } + return type; +} + Validator* ParsingToolBox::readCompositeValidator(QString& str) { bool expectSquareBrasket=false; diff --git a/parsingtoolbox.h b/parsingtoolbox.h index 7c5bc53..5d198be 100644 --- a/parsingtoolbox.h +++ b/parsingtoolbox.h @@ -32,6 +32,8 @@ #include "range.h" #include "node/scalaroperatornode.h" #include "node/paintnode.h" +#include "node/ifnode.h" + /** * @brief The ParsingToolBox is gathering many useful methods for dice parsing. * Its goal is to make the diceparser a bit lighter. @@ -170,7 +172,12 @@ public: static QHash<QString, QString> *getVariableHash(); static void setVariableHash(QHash<QString, QString> *variableHash); - + /** + * @brief readConditionType + * @param str + * @return + */ + static IfNode::ConditionType readConditionType(QString &str); private: QMap<QString,BooleanCondition::LogicOperator>* m_logicOp; @@ -100,3 +100,17 @@ bool Range::isEmptyRange() { return m_emptyRange; } +Validator* Range::getCopy() const +{ + Range* val = new Range(); + val->setEmptyRange(m_emptyRange); + if(m_hasEnd) + { + val->setEnd(m_end); + } + if(m_hasStart) + { + val->setStart(m_start); + } + return val; +} @@ -47,6 +47,7 @@ public: void setEmptyRange(bool); bool isEmptyRange(); + virtual Validator* getCopy() const; private: qint64 m_start; qint64 m_end; diff --git a/validator.h b/validator.h index 6a404da..edb72a3 100644 --- a/validator.h +++ b/validator.h @@ -59,6 +59,11 @@ public: * @return */ virtual quint64 getValidRangeSize(quint64 faces) const = 0 ; + /** + * @brief getCopy + * @return return a copy of this validator + */ + virtual Validator* getCopy() const = 0; }; #endif // VALIDATOR_H |