From 844436e744a688bc27fddabcb289e56d21612d86 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 26 Jan 2018 22:22:03 +0100 Subject: [PATCH] Ref T231, Ref T238 maxValue for PQs Remark: max() as function name caused compile errors --- src/blackmisc/pq/length.h | 2 -- src/blackmisc/pq/physicalquantity.cpp | 16 ++++++++++++++++ src/blackmisc/pq/physicalquantity.h | 16 +++++++++++----- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/blackmisc/pq/length.h b/src/blackmisc/pq/length.h index e331ea79e..faf8449ed 100644 --- a/src/blackmisc/pq/length.h +++ b/src/blackmisc/pq/length.h @@ -19,7 +19,6 @@ namespace BlackMisc { namespace PhysicalQuantities { - //! Physical unit length (length) class BLACKMISC_EXPORT CLength : public CPhysicalQuantity { @@ -33,7 +32,6 @@ namespace BlackMisc //! \copydoc CPhysicalQuantity(const QString &unitString) CLength(const QString &unitString) : CPhysicalQuantity(unitString) {} }; - } // ns } // ns diff --git a/src/blackmisc/pq/physicalquantity.cpp b/src/blackmisc/pq/physicalquantity.cpp index 78df8401b..ecba26182 100644 --- a/src/blackmisc/pq/physicalquantity.cpp +++ b/src/blackmisc/pq/physicalquantity.cpp @@ -357,6 +357,22 @@ namespace BlackMisc return this->valueRoundedWithUnit(this->getUnit(), -1, i18n); } + template + const PQ &CPhysicalQuantity::maxValue(const PQ &pq1, const PQ &pq2) + { + if (pq1.isNull()) { return pq2; } + if (pq2.isNull()) { return pq1; } + return pq1 > pq2 ? pq1 : pq2; + } + + template + const PQ &CPhysicalQuantity::minValue(const PQ &pq1, const PQ &pq2) + { + if (pq1.isNull()) { return pq2; } + if (pq2.isNull()) { return pq1; } + return pq1 < pq2 ? pq1 : pq2; + } + template uint CPhysicalQuantity::getValueHash() const { diff --git a/src/blackmisc/pq/physicalquantity.h b/src/blackmisc/pq/physicalquantity.h index be4b1a5bf..34e456d28 100644 --- a/src/blackmisc/pq/physicalquantity.h +++ b/src/blackmisc/pq/physicalquantity.h @@ -47,11 +47,11 @@ namespace BlackMisc */ template class CPhysicalQuantity : public Mixin::DBusOperators>, - public Mixin::JsonOperators>, - public Mixin::Index, - public Mixin::MetaType, - public Mixin::String, - public Mixin::Icon> + public Mixin::JsonOperators>, + public Mixin::Index, + public Mixin::MetaType, + public Mixin::String, + public Mixin::Icon> { //! \copydoc CValueObject::compare friend int compare(const PQ &a, const PQ &b) { return compareImpl(a, b); } @@ -234,6 +234,12 @@ namespace BlackMisc //! \copydoc BlackMisc::Mixin::String::toQString QString convertToQString(bool i18n = false) const; + //! Maximum of 2 quantities + static const PQ &maxValue(const PQ &pq1, const PQ &pq2); + + //! Minimum of 2 quantities + static const PQ &minValue(const PQ &pq1, const PQ &pq2); + protected: //! Constructor with double CPhysicalQuantity(double value, MU unit);