From eccf3d4551baec7e1690e86820b80c7588db5579 Mon Sep 17 00:00:00 2001 From: Andreas Lauser Date: Sat, 13 Sep 2014 11:02:23 +0200 Subject: [PATCH] ParserItem: improve the equallity testing code and remove the defaultSet() method but we still need the m_defaultSet member because NaN == NaN as well as NaN != NaN... --- .../eclipse/Parser/ParserDoubleItem.cpp | 6 +-- opm/parser/eclipse/Parser/ParserFloatItem.cpp | 3 +- opm/parser/eclipse/Parser/ParserIntItem.cpp | 2 +- opm/parser/eclipse/Parser/ParserItem.cpp | 20 -------- opm/parser/eclipse/Parser/ParserItem.hpp | 51 ++++++++++++------- .../eclipse/Parser/ParserStringItem.cpp | 2 +- 6 files changed, 38 insertions(+), 46 deletions(-) diff --git a/opm/parser/eclipse/Parser/ParserDoubleItem.cpp b/opm/parser/eclipse/Parser/ParserDoubleItem.cpp index 2719b4f2d..ec13f1a91 100644 --- a/opm/parser/eclipse/Parser/ParserDoubleItem.cpp +++ b/opm/parser/eclipse/Parser/ParserDoubleItem.cpp @@ -77,9 +77,9 @@ namespace Opm setDefault( jsonConfig.get_double("default") ); } - bool ParserDoubleItem::equal(const ParserItem& other) const - { - return (ParserItemEqual(this , other) && equalDimensions(other)); } + bool ParserDoubleItem::equal(const ParserItem& other) const { + return parserRawItemEqual(other) && equalDimensions(other); + } bool ParserDoubleItem::equalDimensions(const ParserItem& other) const { diff --git a/opm/parser/eclipse/Parser/ParserFloatItem.cpp b/opm/parser/eclipse/Parser/ParserFloatItem.cpp index b03d34585..e87cde405 100644 --- a/opm/parser/eclipse/Parser/ParserFloatItem.cpp +++ b/opm/parser/eclipse/Parser/ParserFloatItem.cpp @@ -79,10 +79,9 @@ namespace Opm setDefault( jsonConfig.get_double("default")); } - bool ParserFloatItem::equal(const ParserItem& other) const { - return (ParserItemEqual(this , other) && equalDimensions(other)); + return parserRawItemEqual(other) && equalDimensions(other); } bool ParserFloatItem::equalDimensions(const ParserItem& other) const { diff --git a/opm/parser/eclipse/Parser/ParserIntItem.cpp b/opm/parser/eclipse/Parser/ParserIntItem.cpp index dfebf1347..d9536124c 100644 --- a/opm/parser/eclipse/Parser/ParserIntItem.cpp +++ b/opm/parser/eclipse/Parser/ParserIntItem.cpp @@ -78,7 +78,7 @@ namespace Opm { bool ParserIntItem::equal(const ParserItem& other) const { - return ParserItemEqual(this , other); + return parserRawItemEqual(other); } diff --git a/opm/parser/eclipse/Parser/ParserItem.cpp b/opm/parser/eclipse/Parser/ParserItem.cpp index da983a148..d15ea55b8 100644 --- a/opm/parser/eclipse/Parser/ParserItem.cpp +++ b/opm/parser/eclipse/Parser/ParserItem.cpp @@ -97,24 +97,4 @@ namespace Opm { void ParserItem::setDescription(std::string description) { m_description = description; } - - - bool ParserItem::defaultSet() const { - return m_defaultSet; - } - - - bool ParserItem::equal(const ParserItem& other) const { - if (typeid(this) == typeid(&other)) { - if ((name() == other.name()) && - (sizeType() == other.sizeType()) && - (m_defaultSet == other.m_defaultSet)) - return true; - else - return false; - } - else - return false; - } - } diff --git a/opm/parser/eclipse/Parser/ParserItem.hpp b/opm/parser/eclipse/Parser/ParserItem.hpp index d65ef1b07..61e426516 100644 --- a/opm/parser/eclipse/Parser/ParserItem.hpp +++ b/opm/parser/eclipse/Parser/ParserItem.hpp @@ -50,17 +50,44 @@ namespace Opm { const std::string& name() const; ParserItemSizeEnum sizeType() const; std::string getDescription() const; - bool defaultSet() const; bool scalar() const; void setDescription(std::string helpText); - virtual bool equal(const ParserItem& other) const; - virtual void inlineNew(std::ostream& /* os */) const {} - + virtual void inlineNew(std::ostream& /* os */) const = 0; + virtual ~ParserItem() { } - + + virtual bool equal(const ParserItem& other) const = 0; + protected: + template + bool parserRawItemEqual(const ParserItem &other) const { + const T * lhs = dynamic_cast(this); + const T * rhs = dynamic_cast(&other); + if (!lhs || !rhs) + return false; + + if (lhs->name() != rhs->name()) + return false; + + if (lhs->getDescription() != rhs->getDescription()) + return false; + + if (lhs->sizeType() != rhs->sizeType()) + return false; + + if (lhs->m_defaultSet != rhs->m_defaultSet) + return false; + + // we only care that the default value is equal if it was + // specified... + if (lhs->m_defaultSet && lhs->getDefault() != rhs->getDefault()) + return false; + + return true; + } + bool m_defaultSet; private: @@ -72,20 +99,6 @@ namespace Opm { typedef std::shared_ptr ParserItemConstPtr; typedef std::shared_ptr ParserItemPtr; - - - template - bool ParserItemEqual(const T * self , const ParserItem& other) { - const T * rhs = dynamic_cast(&other); - if (rhs && self->ParserItem::equal(other)) { - if (self->defaultSet()) - return self->getDefault() == rhs->getDefault(); - return true; - } else - return false; - } - - /// Scans the rawRecords data according to the ParserItems definition. /// returns a DeckItem object. /// NOTE: data are popped from the rawRecords deque! diff --git a/opm/parser/eclipse/Parser/ParserStringItem.cpp b/opm/parser/eclipse/Parser/ParserStringItem.cpp index ac278965d..0c6d3936d 100644 --- a/opm/parser/eclipse/Parser/ParserStringItem.cpp +++ b/opm/parser/eclipse/Parser/ParserStringItem.cpp @@ -75,7 +75,7 @@ namespace Opm { bool ParserStringItem::equal(const ParserItem& other) const { - return ParserItemEqual(this , other); + return parserRawItemEqual(other); } void ParserStringItem::inlineNew(std::ostream& os) const {