From 20942e66588ecefed85a78d9e9f69b15b23e24ba Mon Sep 17 00:00:00 2001 From: Aesylwinn Date: Fri, 19 Feb 2016 14:10:47 -0500 Subject: [PATCH] Fixes to testing if condition is always or never true --- apps/opencs/model/world/infoselectwrapper.cpp | 31 ++++++++++++------- apps/opencs/model/world/infoselectwrapper.hpp | 5 ++- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/apps/opencs/model/world/infoselectwrapper.cpp b/apps/opencs/model/world/infoselectwrapper.cpp index 496f8e17f..d036b1965 100644 --- a/apps/opencs/model/world/infoselectwrapper.cpp +++ b/apps/opencs/model/world/infoselectwrapper.cpp @@ -706,6 +706,13 @@ bool CSMWorld::ConstInfoSelectWrapper::rangeContains(T1 value, std::pair return (value >= range.first && value <= range.second); } +template +bool CSMWorld::ConstInfoSelectWrapper::rangeFullyContains(std::pair containingRange, + std::pair testRange) const +{ + return (containingRange.first <= testRange.first) && (testRange.second <= containingRange.second); +} + template bool CSMWorld::ConstInfoSelectWrapper::rangesOverlap(std::pair range1, std::pair range2) const { @@ -727,21 +734,22 @@ template bool CSMWorld::ConstInfoSelectWrapper::conditionIsAlwaysTrue(std::pair conditionRange, std::pair validRange) const { - switch (mRelationType) { case Relation_Equal: - case Relation_Greater: - case Relation_GreaterOrEqual: - case Relation_Less: - case Relation_LessOrEqual: - // If ranges are same, it will always be true - return rangesMatch(conditionRange, validRange); + return false; case Relation_NotEqual: // If value is not within range, it will always be true return !rangeContains(conditionRange.first, validRange); + case Relation_Greater: + case Relation_GreaterOrEqual: + case Relation_Less: + case Relation_LessOrEqual: + // If the valid range is completely within the condition range, it will always be true + return rangeFullyContains(conditionRange, validRange); + default: throw std::logic_error("InfoCondition: operator can not be used to compare"); } @@ -756,6 +764,11 @@ bool CSMWorld::ConstInfoSelectWrapper::conditionIsNeverTrue(std::pair con switch (mRelationType) { case Relation_Equal: + return !rangeContains(conditionRange.first, validRange); + + case Relation_NotEqual: + return false; + case Relation_Greater: case Relation_GreaterOrEqual: case Relation_Less: @@ -763,10 +776,6 @@ bool CSMWorld::ConstInfoSelectWrapper::conditionIsNeverTrue(std::pair con // If ranges do not overlap, it will never be true return !rangesOverlap(conditionRange, validRange); - case Relation_NotEqual: - // If the value is the only value withing the range, it will never be true - return rangesMatch(conditionRange, validRange); - default: throw std::logic_error("InfoCondition: operator can not be used to compare"); } diff --git a/apps/opencs/model/world/infoselectwrapper.hpp b/apps/opencs/model/world/infoselectwrapper.hpp index 1aa86aeca..ce26a46dc 100644 --- a/apps/opencs/model/world/infoselectwrapper.hpp +++ b/apps/opencs/model/world/infoselectwrapper.hpp @@ -164,7 +164,7 @@ namespace CSMWorld bool variantTypeIsValid() const; const ESM::Variant& getVariant() const; - + std::string toString() const; protected: @@ -188,6 +188,9 @@ namespace CSMWorld template bool rangesOverlap(std::pair range1, std::pair range2) const; + template + bool rangeFullyContains(std::pair containing, std::pair test) const; + template bool rangesMatch(std::pair range1, std::pair range2) const;