diff --git a/src/blackmisc/collection.h b/src/blackmisc/collection.h index a3ea6b783..10d4949ab 100644 --- a/src/blackmisc/collection.h +++ b/src/blackmisc/collection.h @@ -265,10 +265,10 @@ namespace BlackMisc void detach() { m_impl.detach(); } //! Test for equality. - bool operator ==(const CCollection &other) const { return m_impl == other.m_impl; } + friend bool operator ==(const CCollection &a, const CCollection &b) { return a.m_impl == b.m_impl; } //! Test for inequality. - bool operator !=(const CCollection &other) const { return m_impl != other.m_impl; } + friend bool operator !=(const CCollection &a, const CCollection &b) { return a.m_impl != b.m_impl; } private: QOrderedSet m_impl; diff --git a/src/blackmisc/pq/measurementunit.cpp b/src/blackmisc/pq/measurementunit.cpp index c6dbcd893..3f710bb4b 100644 --- a/src/blackmisc/pq/measurementunit.cpp +++ b/src/blackmisc/pq/measurementunit.cpp @@ -16,17 +16,6 @@ namespace BlackMisc { namespace PhysicalQuantities { - bool CMeasurementUnit::operator ==(const CMeasurementUnit &other) const - { - if (this == &other) return true; - return m_data->m_name == other.m_data->m_name; - } - - bool CMeasurementUnit::operator !=(const CMeasurementUnit &other) const - { - return !(other == *this); - } - double CMeasurementUnit::convertFrom(double value, const CMeasurementUnit &unit) const { if (this->isNull() || unit.isNull()) return 0; diff --git a/src/blackmisc/pq/measurementunit.h b/src/blackmisc/pq/measurementunit.h index 2b6bfbd9f..78ff39464 100644 --- a/src/blackmisc/pq/measurementunit.h +++ b/src/blackmisc/pq/measurementunit.h @@ -272,10 +272,17 @@ namespace BlackMisc } //! Equal operator == - bool operator == (const CMeasurementUnit &other) const; + friend bool operator == (const CMeasurementUnit &a, const CMeasurementUnit &b) + { + if (&a == &b) return true; + return a.m_data->m_name == b.m_data->m_name; + } //! Unequal operator != - bool operator != (const CMeasurementUnit &other) const; + friend bool operator != (const CMeasurementUnit &a, const CMeasurementUnit &b) + { + return !(a == b); + } //! \copydoc CValueObject::qHash friend uint qHash(const CMeasurementUnit &unit) diff --git a/src/blackmisc/pq/physicalquantity.cpp b/src/blackmisc/pq/physicalquantity.cpp index 29eeefd72..77bf11f64 100644 --- a/src/blackmisc/pq/physicalquantity.cpp +++ b/src/blackmisc/pq/physicalquantity.cpp @@ -76,7 +76,7 @@ namespace BlackMisc } template - bool CPhysicalQuantity::operator ==(const CPhysicalQuantity &other) const + bool CPhysicalQuantity::equals(const CPhysicalQuantity &other) const { if (this == &other) return true; @@ -87,12 +87,6 @@ namespace BlackMisc return diff <= m_unit.getEpsilon(); } - template - bool CPhysicalQuantity::operator !=(const CPhysicalQuantity &other) const - { - return !((*this) == other); - } - template CPhysicalQuantity &CPhysicalQuantity::operator +=(const CPhysicalQuantity &other) { @@ -100,14 +94,6 @@ namespace BlackMisc return *this; } - template - PQ CPhysicalQuantity::operator +(const PQ &other) const - { - PQ copy(other); - copy += *this; - return copy; - } - template void CPhysicalQuantity::addValueSameUnit(double value) { @@ -127,14 +113,6 @@ namespace BlackMisc return *this; } - template - PQ CPhysicalQuantity::operator -(const PQ &other) const - { - PQ copy = *derived(); - copy -= other; - return copy; - } - template bool CPhysicalQuantity::isZeroEpsilonConsidered() const { @@ -241,7 +219,7 @@ namespace BlackMisc } template - bool CPhysicalQuantity::operator <(const CPhysicalQuantity &other) const + bool CPhysicalQuantity::lessThan(const CPhysicalQuantity &other) const { if (*this == other) return false; if (this->isNull() || other.isNull()) return false; @@ -249,26 +227,6 @@ namespace BlackMisc return (m_value < other.value(m_unit)); } - template - bool CPhysicalQuantity::operator >(const CPhysicalQuantity &other) const - { - return other < *this; - } - - template - bool CPhysicalQuantity::operator >=(const CPhysicalQuantity &other) const - { - if (*this == other) return true; - return *this > other; - } - - template - bool CPhysicalQuantity::operator <=(const CPhysicalQuantity &other) const - { - if (*this == other) return true; - return *this < other; - } - template PQ &CPhysicalQuantity::switchUnit(const MU &newUnit) { diff --git a/src/blackmisc/pq/physicalquantity.h b/src/blackmisc/pq/physicalquantity.h index 3e709022d..c3138cf06 100644 --- a/src/blackmisc/pq/physicalquantity.h +++ b/src/blackmisc/pq/physicalquantity.h @@ -160,10 +160,10 @@ namespace BlackMisc PQ operator /(double divide) const; //! Equal operator == - bool operator==(const CPhysicalQuantity &other) const; + friend bool operator==(const CPhysicalQuantity &a, const CPhysicalQuantity &b) { return a.equals(b); } //! Not equal operator != - bool operator!=(const CPhysicalQuantity &other) const; + friend bool operator!=(const CPhysicalQuantity &a, const CPhysicalQuantity &b) { return !a.equals(b); } //! Plus operator += CPhysicalQuantity &operator +=(const CPhysicalQuantity &other); @@ -172,22 +172,22 @@ namespace BlackMisc CPhysicalQuantity &operator -=(const CPhysicalQuantity &other); //! Greater operator > - bool operator >(const CPhysicalQuantity &other) const; + friend bool operator>(const CPhysicalQuantity &a, const CPhysicalQuantity &b) { return b.lessThan(a); } //! Less operator < - bool operator <(const CPhysicalQuantity &other) const; + friend bool operator<(const CPhysicalQuantity &a, const CPhysicalQuantity &b) { return a.lessThan(b); } //! Less equal operator <= - bool operator <=(const CPhysicalQuantity &other) const; + friend bool operator<=(const CPhysicalQuantity &a, const CPhysicalQuantity &b) { return !b.lessThan(a); } //! Greater equal operator >= - bool operator >=(const CPhysicalQuantity &other) const; + friend bool operator>=(const CPhysicalQuantity &a, const CPhysicalQuantity &b) { return !a.lessThan(b); } //! Plus operator + - PQ operator +(const PQ &other) const; + friend PQ operator +(const PQ &a, const PQ &b) { PQ copy(a); copy += b; return copy; } //! Minus operator - - PQ operator -(const PQ &other) const; + friend PQ operator -(const PQ &a, const PQ &b) { PQ copy(a); copy -= b; return copy; } //! Quantity value <= epsilon bool isZeroEpsilonConsidered() const; @@ -281,6 +281,12 @@ namespace BlackMisc //! Implementation of compare static int compareImpl(const PQ &, const PQ &); + //! Private implementation of equality operators + bool equals(const CPhysicalQuantity &other) const; + + //! Private implementation of comparison operators + bool lessThan(const CPhysicalQuantity &other) const; + //! Easy access to derived class (CRTP template parameter) PQ const *derived() const;