From 5744c260c503cba4b03ce022cafdbc63c09d7589 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 7 Mar 2018 01:28:08 +0100 Subject: [PATCH] Ref T259, Ref T243 improvements of PQ * null() * allow chaining for makePositive * abs() --- src/blackmisc/pq/physicalquantity.cpp | 21 +++++++++++++++++++-- src/blackmisc/pq/physicalquantity.h | 10 ++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/blackmisc/pq/physicalquantity.cpp b/src/blackmisc/pq/physicalquantity.cpp index ecba26182..0aa488c41 100644 --- a/src/blackmisc/pq/physicalquantity.cpp +++ b/src/blackmisc/pq/physicalquantity.cpp @@ -147,15 +147,25 @@ namespace BlackMisc } template - void CPhysicalQuantity::makePositive() + const PQ &CPhysicalQuantity::makePositive() { if (m_value < 0) { m_value *= -1.0; } + return *this->derived(); } template - void CPhysicalQuantity::makeNegative() + const PQ &CPhysicalQuantity::makeNegative() { if (m_value > 0) { m_value *= -1.0; } + return *this->derived(); + } + + template + PQ CPhysicalQuantity::abs() const + { + if (m_value >= 0) { return *this->derived(); } + PQ copy(*this->derived()); + return copy.makePositive(); } template @@ -373,6 +383,13 @@ namespace BlackMisc return pq1 < pq2 ? pq1 : pq2; } + template + const PQ &CPhysicalQuantity::null() + { + static const PQ n(0, MU::nullUnit()); + return n; + } + template uint CPhysicalQuantity::getValueHash() const { diff --git a/src/blackmisc/pq/physicalquantity.h b/src/blackmisc/pq/physicalquantity.h index 34e456d28..caa4dbda9 100644 --- a/src/blackmisc/pq/physicalquantity.h +++ b/src/blackmisc/pq/physicalquantity.h @@ -193,10 +193,13 @@ namespace BlackMisc bool isNegativeWithEpsilonConsidered() const; //! Make value always positive - void makePositive(); + const PQ &makePositive(); //! Make value always negative - void makeNegative(); + const PQ &makeNegative(); + + //! Absolute value (always >=0) + PQ abs() const; //! \copydoc BlackMisc::Mixin::DBusByMetaClass::marshallToDbus void marshallToDbus(QDBusArgument &argument) const; @@ -240,6 +243,9 @@ namespace BlackMisc //! Minimum of 2 quantities static const PQ &minValue(const PQ &pq1, const PQ &pq2); + //! NULL PQ + static const PQ &null(); + protected: //! Constructor with double CPhysicalQuantity(double value, MU unit);