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...
This commit is contained in:
Andreas Lauser
2014-09-13 11:02:23 +02:00
parent 435e6aa6c5
commit eccf3d4551
6 changed files with 38 additions and 46 deletions

View File

@@ -77,9 +77,9 @@ namespace Opm
setDefault( jsonConfig.get_double("default") );
}
bool ParserDoubleItem::equal(const ParserItem& other) const
{
return (ParserItemEqual<ParserDoubleItem>(this , other) && equalDimensions(other)); }
bool ParserDoubleItem::equal(const ParserItem& other) const {
return parserRawItemEqual<ParserDoubleItem>(other) && equalDimensions(other);
}
bool ParserDoubleItem::equalDimensions(const ParserItem& other) const {

View File

@@ -79,10 +79,9 @@ namespace Opm
setDefault( jsonConfig.get_double("default"));
}
bool ParserFloatItem::equal(const ParserItem& other) const
{
return (ParserItemEqual<ParserFloatItem>(this , other) && equalDimensions(other));
return parserRawItemEqual<ParserFloatItem>(other) && equalDimensions(other);
}
bool ParserFloatItem::equalDimensions(const ParserItem& other) const {

View File

@@ -78,7 +78,7 @@ namespace Opm {
bool ParserIntItem::equal(const ParserItem& other) const
{
return ParserItemEqual<ParserIntItem>(this , other);
return parserRawItemEqual<ParserIntItem>(other);
}

View File

@@ -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;
}
}

View File

@@ -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 <class T>
bool parserRawItemEqual(const ParserItem &other) const {
const T * lhs = dynamic_cast<const T*>(this);
const T * rhs = dynamic_cast<const T*>(&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<const ParserItem> ParserItemConstPtr;
typedef std::shared_ptr<ParserItem> ParserItemPtr;
template<typename T>
bool ParserItemEqual(const T * self , const ParserItem& other) {
const T * rhs = dynamic_cast<const T*>(&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!

View File

@@ -75,7 +75,7 @@ namespace Opm {
bool ParserStringItem::equal(const ParserItem& other) const
{
return ParserItemEqual<ParserStringItem>(this , other);
return parserRawItemEqual<ParserStringItem>(other);
}
void ParserStringItem::inlineNew(std::ostream& os) const {