Using friend function comparison and arithmetic operators, for parameter symmetry.

This commit is contained in:
Mat Sutcliffe
2019-02-27 22:27:34 +00:00
parent 1089adf18d
commit 798a0b8f06
5 changed files with 27 additions and 67 deletions

View File

@@ -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<T> m_impl;

View File

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

View File

@@ -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)

View File

@@ -76,7 +76,7 @@ namespace BlackMisc
}
template <class MU, class PQ>
bool CPhysicalQuantity<MU, PQ>::operator ==(const CPhysicalQuantity<MU, PQ> &other) const
bool CPhysicalQuantity<MU, PQ>::equals(const CPhysicalQuantity<MU, PQ> &other) const
{
if (this == &other) return true;
@@ -87,12 +87,6 @@ namespace BlackMisc
return diff <= m_unit.getEpsilon();
}
template <class MU, class PQ>
bool CPhysicalQuantity<MU, PQ>::operator !=(const CPhysicalQuantity<MU, PQ> &other) const
{
return !((*this) == other);
}
template <class MU, class PQ>
CPhysicalQuantity<MU, PQ> &CPhysicalQuantity<MU, PQ>::operator +=(const CPhysicalQuantity<MU, PQ> &other)
{
@@ -100,14 +94,6 @@ namespace BlackMisc
return *this;
}
template <class MU, class PQ>
PQ CPhysicalQuantity<MU, PQ>::operator +(const PQ &other) const
{
PQ copy(other);
copy += *this;
return copy;
}
template <class MU, class PQ>
void CPhysicalQuantity<MU, PQ>::addValueSameUnit(double value)
{
@@ -127,14 +113,6 @@ namespace BlackMisc
return *this;
}
template <class MU, class PQ>
PQ CPhysicalQuantity<MU, PQ>::operator -(const PQ &other) const
{
PQ copy = *derived();
copy -= other;
return copy;
}
template <class MU, class PQ>
bool CPhysicalQuantity<MU, PQ>::isZeroEpsilonConsidered() const
{
@@ -241,7 +219,7 @@ namespace BlackMisc
}
template <class MU, class PQ>
bool CPhysicalQuantity<MU, PQ>::operator <(const CPhysicalQuantity<MU, PQ> &other) const
bool CPhysicalQuantity<MU, PQ>::lessThan(const CPhysicalQuantity<MU, PQ> &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 <class MU, class PQ>
bool CPhysicalQuantity<MU, PQ>::operator >(const CPhysicalQuantity<MU, PQ> &other) const
{
return other < *this;
}
template <class MU, class PQ>
bool CPhysicalQuantity<MU, PQ>::operator >=(const CPhysicalQuantity<MU, PQ> &other) const
{
if (*this == other) return true;
return *this > other;
}
template <class MU, class PQ>
bool CPhysicalQuantity<MU, PQ>::operator <=(const CPhysicalQuantity<MU, PQ> &other) const
{
if (*this == other) return true;
return *this < other;
}
template <class MU, class PQ>
PQ &CPhysicalQuantity<MU, PQ>::switchUnit(const MU &newUnit)
{

View File

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