From d489bed3705b78e99201e9e336952c2bca923d81 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Mon, 9 Jul 2018 22:06:03 +0200 Subject: [PATCH] Ref T268, compare for PQs --- src/blackmisc/pq/physicalquantity.cpp | 9 +++++++-- src/blackmisc/pq/physicalquantity.h | 10 ++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/blackmisc/pq/physicalquantity.cpp b/src/blackmisc/pq/physicalquantity.cpp index ea993ef2f..8737a92e0 100644 --- a/src/blackmisc/pq/physicalquantity.cpp +++ b/src/blackmisc/pq/physicalquantity.cpp @@ -500,8 +500,13 @@ namespace BlackMisc template int CPhysicalQuantity::compareImpl(const PQ &a, const PQ &b) { - if (a.isNull() > b.isNull()) { return -1; } - if (a.isNull() < b.isNull()) { return 1; } + // fetch "null" as we do not know how expensive it is + const bool aIsNull = a.isNull(); + const bool bIsNull = b.isNull(); + + if (aIsNull && bIsNull) { return 0; } + if (aIsNull > bIsNull) { return -1; } + if (aIsNull < bIsNull) { return 1; } if (a < b) { return -1; } else if (a > b) { return 1; } diff --git a/src/blackmisc/pq/physicalquantity.h b/src/blackmisc/pq/physicalquantity.h index 7d914002a..e501d30fc 100644 --- a/src/blackmisc/pq/physicalquantity.h +++ b/src/blackmisc/pq/physicalquantity.h @@ -228,6 +228,9 @@ namespace BlackMisc //! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex void setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant); + //! \copydoc BlackMisc::Mixin::Index::comparePropertyByIndex + int comparePropertyByIndex(const CPropertyIndex &index, const PQ &pq) const; + //! \copydoc BlackMisc::Mixin::String::toQString QString convertToQString(bool i18n = false) const; @@ -237,8 +240,8 @@ namespace BlackMisc //! Parse value from string void parseFromString(const QString &value); - //! Compare - int comparePropertyByIndex(const CPropertyIndex &index, const PQ &pq) const; + //! Compare with other PQ + int compare(const PQ &other) const { return compareImpl(*this->derived(), other); } //! Maximum of 2 quantities static const PQ &maxValue(const PQ &pq1, const PQ &pq2); @@ -246,6 +249,9 @@ namespace BlackMisc //! Minimum of 2 quantities static const PQ &minValue(const PQ &pq1, const PQ &pq2); + //! Implementation of compare + static int compare(const PQ &a, const PQ &b) { return compareImpl(a, b); } + //! NULL PQ static const PQ &null();