diff --git a/src/blackmisc/avioadfsystem.h b/src/blackmisc/avioadfsystem.h index b2660b298..848378428 100644 --- a/src/blackmisc/avioadfsystem.h +++ b/src/blackmisc/avioadfsystem.h @@ -25,7 +25,7 @@ namespace BlackMisc using MetaType = Policy::MetaType::Default; using Equals = Policy::Equals::None; using LessThan = Policy::LessThan::None; - using Compare = Policy::Compare::Own; + using Compare = Policy::Compare::None; using Hash = Policy::Hash::Own; using DBus = Policy::DBus::Own; using Json = Policy::Json::Own; diff --git a/src/blackmisc/aviocomsystem.h b/src/blackmisc/aviocomsystem.h index 8953bf8c0..44451b840 100644 --- a/src/blackmisc/aviocomsystem.h +++ b/src/blackmisc/aviocomsystem.h @@ -22,7 +22,7 @@ namespace BlackMisc template <> struct CValueObjectStdTuplePolicy : public CValueObjectStdTuplePolicy<> { using LessThan = Policy::LessThan::None; - using Compare = Policy::Compare::Own; + using Compare = Policy::Compare::None; using Hash = Policy::Hash::Own; using DBus = Policy::DBus::Own; using Json = Policy::Json::Own; diff --git a/src/blackmisc/avionavsystem.h b/src/blackmisc/avionavsystem.h index c61788075..71c1df4f6 100644 --- a/src/blackmisc/avionavsystem.h +++ b/src/blackmisc/avionavsystem.h @@ -24,7 +24,7 @@ namespace BlackMisc using MetaType = Policy::MetaType::Default; using Equals = Policy::Equals::None; using LessThan = Policy::LessThan::None; - using Compare = Policy::Compare::Own; + using Compare = Policy::Compare::None; using Hash = Policy::Hash::Own; using DBus = Policy::DBus::Own; using Json = Policy::Json::Own; diff --git a/src/blackmisc/coordinateecef.h b/src/blackmisc/coordinateecef.h index ec1615ebe..a75700ca0 100644 --- a/src/blackmisc/coordinateecef.h +++ b/src/blackmisc/coordinateecef.h @@ -17,7 +17,7 @@ namespace BlackMisc //! \private template <> struct CValueObjectStdTuplePolicy : public CValueObjectStdTuplePolicy<> { - using Compare = Policy::Compare::Own; + using Compare = Policy::Compare::None; using Hash = Policy::Hash::Own; using DBus = Policy::DBus::Own; using Json = Policy::Json::Own; diff --git a/src/blackmisc/mathmatrixbase.cpp b/src/blackmisc/mathmatrixbase.cpp index e93db27a0..e7d739a95 100644 --- a/src/blackmisc/mathmatrixbase.cpp +++ b/src/blackmisc/mathmatrixbase.cpp @@ -142,16 +142,14 @@ namespace BlackMisc /* * Compare */ - template int CMatrixBase::compareImpl(const CValueObject &otherBase) const + template int CMatrixBase::compareImpl(const ImplMatrix &a, const ImplMatrix &b) { - const auto &other = static_cast(otherBase); - for (int r = 0; r < Rows; ++r) { for (int c = 0; c < Columns; ++c) { - if (this->m_matrix(r, c) < other.m_matrix(r, c)) { return -1; } - if (this->m_matrix(r, c) > other.m_matrix(r, c)) { return 1; } + if (a.m_matrix(r, c) < b.m_matrix(r, c)) { return -1; } + if (a.m_matrix(r, c) > b.m_matrix(r, c)) { return 1; } } } return 0; diff --git a/src/blackmisc/mathmatrixbase.h b/src/blackmisc/mathmatrixbase.h index ab0ec8a1d..b9ff5a431 100644 --- a/src/blackmisc/mathmatrixbase.h +++ b/src/blackmisc/mathmatrixbase.h @@ -22,7 +22,7 @@ namespace BlackMisc { using Equals = Policy::Equals::None; using LessThan = Policy::LessThan::None; - using Compare = Policy::Compare::Own; + using Compare = Policy::Compare::None; using Hash = Policy::Hash::Own; using DBus = Policy::DBus::Own; using Json = Policy::Json::Own; @@ -36,6 +36,9 @@ namespace BlackMisc */ template class CMatrixBase : public CValueObjectStdTuple> { + //! \copydoc CValueObject::compare + friend int compare(const ImplMatrix &a, const ImplMatrix &b) { return compareImpl(a, b); } + public: //! \brief Default constructor CMatrixBase() = default; @@ -214,9 +217,6 @@ namespace BlackMisc // no bug, Qt expects columns rows QGenericMatrix m_matrix; //!< backing data - //! \copydoc CValueObject::compareImpl - virtual int compareImpl(const CValueObject &other) const override; - //! \copydoc CValueObject::marshallToDbus virtual void marshallToDbus(QDBusArgument &argument) const override; @@ -241,6 +241,9 @@ namespace BlackMisc //! \brief Check range of row / column void checkRange(int row, int column) const; + + //! Implementation of compare + static int compareImpl(const ImplMatrix &, const ImplMatrix &); }; } // namespace diff --git a/src/blackmisc/mathvector3d.h b/src/blackmisc/mathvector3d.h index 3234036a2..2caa7f357 100644 --- a/src/blackmisc/mathvector3d.h +++ b/src/blackmisc/mathvector3d.h @@ -21,7 +21,7 @@ namespace BlackMisc //! \private template <> struct CValueObjectStdTuplePolicy : public CValueObjectStdTuplePolicy<> { - using Compare = Policy::Compare::Own; + using Compare = Policy::Compare::None; using Hash = Policy::Hash::Own; using DBus = Policy::DBus::Own; using Json = Policy::Json::Own; diff --git a/src/blackmisc/pqphysicalquantity.cpp b/src/blackmisc/pqphysicalquantity.cpp index fd514523c..b1ae81b95 100644 --- a/src/blackmisc/pqphysicalquantity.cpp +++ b/src/blackmisc/pqphysicalquantity.cpp @@ -350,15 +350,13 @@ namespace BlackMisc /* * Compare */ - template int CPhysicalQuantity::compareImpl(const CValueObject &otherBase) const + template int CPhysicalQuantity::compareImpl(const PQ &a, const PQ &b) { - const auto &other = static_cast(otherBase); + if (a.isNull() > b.isNull()) { return -1; } + if (a.isNull() < b.isNull()) { return 1; } - if (this->isNull() > other.isNull()) { return -1; } - if (this->isNull() < other.isNull()) { return 1; } - - if (*this < other) { return -1; } - else if (*this > other) { return 1; } + if (a < b) { return -1; } + else if (a > b) { return 1; } else { return 0; } } diff --git a/src/blackmisc/pqphysicalquantity.h b/src/blackmisc/pqphysicalquantity.h index a8f58abf1..90d40ccb0 100644 --- a/src/blackmisc/pqphysicalquantity.h +++ b/src/blackmisc/pqphysicalquantity.h @@ -34,7 +34,7 @@ namespace BlackMisc using MetaType = Policy::MetaType::DefaultAndQList; using Equals = Policy::Equals::None; using LessThan = Policy::LessThan::None; - using Compare = Policy::Compare::Own; + using Compare = Policy::Compare::None; using Hash = Policy::Hash::Own; using DBus = Policy::DBus::Own; using Json = Policy::Json::Own; @@ -47,6 +47,9 @@ namespace BlackMisc */ template class CPhysicalQuantity : public CValueObjectStdTuple> { + //! \copydoc CValueObject::compare + friend int compare(const PQ &a, const PQ &b) { return compareImpl(a, b); } + public: //! Index enum ColumnIndex @@ -254,9 +257,6 @@ namespace BlackMisc //! \copydoc CValueObject::convertToQString virtual QString convertToQString(bool i18n = false) const override; - //! \copydoc CValueObject::compareImpl - virtual int compareImpl(const CValueObject &other) const override; - private: double m_value; //!< numeric part MU m_unit; //!< unit part @@ -264,6 +264,9 @@ namespace BlackMisc //! Which subclass of CMeasurementUnit is used? typedef MU UnitClass; + //! Implementation of compare + static int compareImpl(const PQ &, const PQ &); + //! Easy access to derived class (CRTP template parameter) PQ const *derived() const { return static_cast(this); } diff --git a/src/blackmisc/valueobject_policy.h b/src/blackmisc/valueobject_policy.h index 2edfc4185..07ae3d9e1 100644 --- a/src/blackmisc/valueobject_policy.h +++ b/src/blackmisc/valueobject_policy.h @@ -197,7 +197,7 @@ namespace BlackMisc { //! Policy implementation of CValueObject::compareImpl template - static bool compareImpl(const T &, const T &) { qFatal("Not implemented"); return 0; } + static bool compareImpl(const T &, const T &) { return 0; } }; //! CValueObjectStdTuple polymorphic comparison policy which inherits the policy of the base class @@ -215,14 +215,6 @@ namespace BlackMisc template static bool compareImpl(const T &a, const T &b) { return compare(Private::EncapsulationBreaker::toMetaTuple(a), Private::EncapsulationBreaker::toMetaTuple(b)); } }; - - //! CValueObjectStdTuple policy for a class which implements its own custom poylymorphic comparison support - struct Own - { - //! Policy implementation of CValueObject::compareImpl - template - static bool compareImpl(const T &, const T &) { return 0; } - }; } namespace Hash diff --git a/src/blackmisc/variant.cpp b/src/blackmisc/variant.cpp index 7dc53842a..7b8289033 100644 --- a/src/blackmisc/variant.cpp +++ b/src/blackmisc/variant.cpp @@ -35,24 +35,22 @@ namespace BlackMisc return m_v.toString(); } - int CVariant::compareImpl(const CValueObject &otherBase) const + int CVariant::compareImpl(const CVariant &a, const CVariant &b) { - const auto &other = static_cast(otherBase); - - if (userType() < other.userType()) { return -1; } - if (userType() > other.userType()) { return 1; } - auto *meta = getValueObjectMetaInfo(); - auto *otherMeta = other.getValueObjectMetaInfo(); - if (meta && otherMeta) + if (a.userType() < b.userType()) { return -1; } + if (a.userType() > b.userType()) { return 1; } + auto *aMeta = a.getValueObjectMetaInfo(); + auto *bMeta = b.getValueObjectMetaInfo(); + if (aMeta && bMeta) { const void *casted = nullptr; - if ((casted = meta->upCastTo(data(), otherMeta->getMetaTypeId()))) + if ((casted = aMeta->upCastTo(a.data(), bMeta->getMetaTypeId()))) { - return otherMeta->compare(casted, other.data()); + return bMeta->compareImpl(casted, b.data()); } - else if ((casted = otherMeta->upCastTo(other.data(), meta->getMetaTypeId()))) + else if ((casted = bMeta->upCastTo(b.data(), aMeta->getMetaTypeId()))) { - return meta->compare(data(), casted); + return aMeta->compareImpl(a.data(), casted); } else { @@ -60,8 +58,8 @@ namespace BlackMisc return 0; } } - if (m_v < other.m_v) { return -1; } - if (m_v > other.m_v) { return 1; } + if (a.m_v < b.m_v) { return -1; } + if (a.m_v > b.m_v) { return 1; } return 0; } diff --git a/src/blackmisc/variant.h b/src/blackmisc/variant.h index dc158a5ce..790bfb107 100644 --- a/src/blackmisc/variant.h +++ b/src/blackmisc/variant.h @@ -32,7 +32,7 @@ namespace BlackMisc using MetaType = Policy::MetaType::QMetaTypeAndDBusOnly; using Equals = Policy::Equals::OwnEquals; using LessThan = Policy::LessThan::OwnLessThan; - using Compare = Policy::Compare::Own; + using Compare = Policy::Compare::None; using Hash = Policy::Hash::Own; using DBus = Policy::DBus::Own; using Json = Policy::Json::Own; @@ -194,8 +194,8 @@ namespace BlackMisc //! Less than operator. friend bool operator <(const CVariant &a, const CVariant &b) { return compare(a, b) < 0; } - //! \copydoc CValueObject::compareImpl - virtual int compareImpl(const CValueObject &other) const override; + //! \copydoc CValueObject::compare + friend int compare(const CVariant &a, const CVariant &b) { return compareImpl(a, b); } //! \copydoc CValueObject::setPropertyByIndex virtual void setPropertyByIndex(const CVariant &variant, const CPropertyIndex &index) override; @@ -215,6 +215,8 @@ namespace BlackMisc Private::IValueObjectMetaInfo *getValueObjectMetaInfo() const { return Private::getValueObjectMetaInfo(m_v); } void *data() { return m_v.data(); } const void *data() const { return m_v.data(); } + + static int compareImpl(const CVariant &, const CVariant &); }; //! Compare stored value of CVariant with any CValueObject derived class.