refs #356 Minor refactoring to Compare policy, preparation for removing CValueObject.

This commit is contained in:
Mathew Sutcliffe
2015-03-16 17:19:15 +00:00
parent 1340cec049
commit d878f879aa
12 changed files with 45 additions and 51 deletions

View File

@@ -25,7 +25,7 @@ namespace BlackMisc
using MetaType = Policy::MetaType::Default; using MetaType = Policy::MetaType::Default;
using Equals = Policy::Equals::None; using Equals = Policy::Equals::None;
using LessThan = Policy::LessThan::None; using LessThan = Policy::LessThan::None;
using Compare = Policy::Compare::Own; using Compare = Policy::Compare::None;
using Hash = Policy::Hash::Own; using Hash = Policy::Hash::Own;
using DBus = Policy::DBus::Own; using DBus = Policy::DBus::Own;
using Json = Policy::Json::Own; using Json = Policy::Json::Own;

View File

@@ -22,7 +22,7 @@ namespace BlackMisc
template <> struct CValueObjectStdTuplePolicy<Aviation::CComSystem> : public CValueObjectStdTuplePolicy<> template <> struct CValueObjectStdTuplePolicy<Aviation::CComSystem> : public CValueObjectStdTuplePolicy<>
{ {
using LessThan = Policy::LessThan::None; using LessThan = Policy::LessThan::None;
using Compare = Policy::Compare::Own; using Compare = Policy::Compare::None;
using Hash = Policy::Hash::Own; using Hash = Policy::Hash::Own;
using DBus = Policy::DBus::Own; using DBus = Policy::DBus::Own;
using Json = Policy::Json::Own; using Json = Policy::Json::Own;

View File

@@ -24,7 +24,7 @@ namespace BlackMisc
using MetaType = Policy::MetaType::Default; using MetaType = Policy::MetaType::Default;
using Equals = Policy::Equals::None; using Equals = Policy::Equals::None;
using LessThan = Policy::LessThan::None; using LessThan = Policy::LessThan::None;
using Compare = Policy::Compare::Own; using Compare = Policy::Compare::None;
using Hash = Policy::Hash::Own; using Hash = Policy::Hash::Own;
using DBus = Policy::DBus::Own; using DBus = Policy::DBus::Own;
using Json = Policy::Json::Own; using Json = Policy::Json::Own;

View File

@@ -17,7 +17,7 @@ namespace BlackMisc
//! \private //! \private
template <> struct CValueObjectStdTuplePolicy<Geo::CCoordinateEcef> : public CValueObjectStdTuplePolicy<> template <> struct CValueObjectStdTuplePolicy<Geo::CCoordinateEcef> : public CValueObjectStdTuplePolicy<>
{ {
using Compare = Policy::Compare::Own; using Compare = Policy::Compare::None;
using Hash = Policy::Hash::Own; using Hash = Policy::Hash::Own;
using DBus = Policy::DBus::Own; using DBus = Policy::DBus::Own;
using Json = Policy::Json::Own; using Json = Policy::Json::Own;

View File

@@ -142,16 +142,14 @@ namespace BlackMisc
/* /*
* Compare * Compare
*/ */
template <class ImplMatrix, int Rows, int Columns> int CMatrixBase<ImplMatrix, Rows, Columns>::compareImpl(const CValueObject &otherBase) const template <class ImplMatrix, int Rows, int Columns> int CMatrixBase<ImplMatrix, Rows, Columns>::compareImpl(const ImplMatrix &a, const ImplMatrix &b)
{ {
const auto &other = static_cast<const CMatrixBase &>(otherBase);
for (int r = 0; r < Rows; ++r) for (int r = 0; r < Rows; ++r)
{ {
for (int c = 0; c < Columns; ++c) for (int c = 0; c < Columns; ++c)
{ {
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 (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; }
} }
} }
return 0; return 0;

View File

@@ -22,7 +22,7 @@ namespace BlackMisc
{ {
using Equals = Policy::Equals::None; using Equals = Policy::Equals::None;
using LessThan = Policy::LessThan::None; using LessThan = Policy::LessThan::None;
using Compare = Policy::Compare::Own; using Compare = Policy::Compare::None;
using Hash = Policy::Hash::Own; using Hash = Policy::Hash::Own;
using DBus = Policy::DBus::Own; using DBus = Policy::DBus::Own;
using Json = Policy::Json::Own; using Json = Policy::Json::Own;
@@ -36,6 +36,9 @@ namespace BlackMisc
*/ */
template<class ImplMatrix, int Rows, int Columns> class CMatrixBase : public CValueObjectStdTuple<CMatrixBase<ImplMatrix, Rows, Columns>> template<class ImplMatrix, int Rows, int Columns> class CMatrixBase : public CValueObjectStdTuple<CMatrixBase<ImplMatrix, Rows, Columns>>
{ {
//! \copydoc CValueObject::compare
friend int compare(const ImplMatrix &a, const ImplMatrix &b) { return compareImpl(a, b); }
public: public:
//! \brief Default constructor //! \brief Default constructor
CMatrixBase() = default; CMatrixBase() = default;
@@ -214,9 +217,6 @@ namespace BlackMisc
// no bug, Qt expects columns rows // no bug, Qt expects columns rows
QGenericMatrix<Columns, Rows, double> m_matrix; //!< backing data QGenericMatrix<Columns, Rows, double> m_matrix; //!< backing data
//! \copydoc CValueObject::compareImpl
virtual int compareImpl(const CValueObject &other) const override;
//! \copydoc CValueObject::marshallToDbus //! \copydoc CValueObject::marshallToDbus
virtual void marshallToDbus(QDBusArgument &argument) const override; virtual void marshallToDbus(QDBusArgument &argument) const override;
@@ -241,6 +241,9 @@ namespace BlackMisc
//! \brief Check range of row / column //! \brief Check range of row / column
void checkRange(int row, int column) const; void checkRange(int row, int column) const;
//! Implementation of compare
static int compareImpl(const ImplMatrix &, const ImplMatrix &);
}; };
} // namespace } // namespace

View File

@@ -21,7 +21,7 @@ namespace BlackMisc
//! \private //! \private
template <> struct CValueObjectStdTuplePolicy<Math::CVector3D> : public CValueObjectStdTuplePolicy<> template <> struct CValueObjectStdTuplePolicy<Math::CVector3D> : public CValueObjectStdTuplePolicy<>
{ {
using Compare = Policy::Compare::Own; using Compare = Policy::Compare::None;
using Hash = Policy::Hash::Own; using Hash = Policy::Hash::Own;
using DBus = Policy::DBus::Own; using DBus = Policy::DBus::Own;
using Json = Policy::Json::Own; using Json = Policy::Json::Own;

View File

@@ -350,15 +350,13 @@ namespace BlackMisc
/* /*
* Compare * Compare
*/ */
template <class MU, class PQ> int CPhysicalQuantity<MU, PQ>::compareImpl(const CValueObject &otherBase) const template <class MU, class PQ> int CPhysicalQuantity<MU, PQ>::compareImpl(const PQ &a, const PQ &b)
{ {
const auto &other = static_cast<const CPhysicalQuantity &>(otherBase); if (a.isNull() > b.isNull()) { return -1; }
if (a.isNull() < b.isNull()) { return 1; }
if (this->isNull() > other.isNull()) { return -1; } if (a < b) { return -1; }
if (this->isNull() < other.isNull()) { return 1; } else if (a > b) { return 1; }
if (*this < other) { return -1; }
else if (*this > other) { return 1; }
else { return 0; } else { return 0; }
} }

View File

@@ -34,7 +34,7 @@ namespace BlackMisc
using MetaType = Policy::MetaType::DefaultAndQList; using MetaType = Policy::MetaType::DefaultAndQList;
using Equals = Policy::Equals::None; using Equals = Policy::Equals::None;
using LessThan = Policy::LessThan::None; using LessThan = Policy::LessThan::None;
using Compare = Policy::Compare::Own; using Compare = Policy::Compare::None;
using Hash = Policy::Hash::Own; using Hash = Policy::Hash::Own;
using DBus = Policy::DBus::Own; using DBus = Policy::DBus::Own;
using Json = Policy::Json::Own; using Json = Policy::Json::Own;
@@ -47,6 +47,9 @@ namespace BlackMisc
*/ */
template <class MU, class PQ> class CPhysicalQuantity : public CValueObjectStdTuple<CPhysicalQuantity<MU, PQ>> template <class MU, class PQ> class CPhysicalQuantity : public CValueObjectStdTuple<CPhysicalQuantity<MU, PQ>>
{ {
//! \copydoc CValueObject::compare
friend int compare(const PQ &a, const PQ &b) { return compareImpl(a, b); }
public: public:
//! Index //! Index
enum ColumnIndex enum ColumnIndex
@@ -254,9 +257,6 @@ namespace BlackMisc
//! \copydoc CValueObject::convertToQString //! \copydoc CValueObject::convertToQString
virtual QString convertToQString(bool i18n = false) const override; virtual QString convertToQString(bool i18n = false) const override;
//! \copydoc CValueObject::compareImpl
virtual int compareImpl(const CValueObject &other) const override;
private: private:
double m_value; //!< numeric part double m_value; //!< numeric part
MU m_unit; //!< unit part MU m_unit; //!< unit part
@@ -264,6 +264,9 @@ namespace BlackMisc
//! Which subclass of CMeasurementUnit is used? //! Which subclass of CMeasurementUnit is used?
typedef MU UnitClass; typedef MU UnitClass;
//! Implementation of compare
static int compareImpl(const PQ &, const PQ &);
//! Easy access to derived class (CRTP template parameter) //! Easy access to derived class (CRTP template parameter)
PQ const *derived() const { return static_cast<PQ const *>(this); } PQ const *derived() const { return static_cast<PQ const *>(this); }

View File

@@ -197,7 +197,7 @@ namespace BlackMisc
{ {
//! Policy implementation of CValueObject::compareImpl //! Policy implementation of CValueObject::compareImpl
template <class T, class...> template <class T, class...>
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 //! CValueObjectStdTuple polymorphic comparison policy which inherits the policy of the base class
@@ -215,14 +215,6 @@ namespace BlackMisc
template <class T, class...> template <class T, class...>
static bool compareImpl(const T &a, const T &b) { return compare(Private::EncapsulationBreaker::toMetaTuple(a), Private::EncapsulationBreaker::toMetaTuple(b)); } 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 <class T, class...>
static bool compareImpl(const T &, const T &) { return 0; }
};
} }
namespace Hash namespace Hash

View File

@@ -35,24 +35,22 @@ namespace BlackMisc
return m_v.toString(); 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<const CVariant &>(otherBase); if (a.userType() < b.userType()) { return -1; }
if (a.userType() > b.userType()) { return 1; }
if (userType() < other.userType()) { return -1; } auto *aMeta = a.getValueObjectMetaInfo();
if (userType() > other.userType()) { return 1; } auto *bMeta = b.getValueObjectMetaInfo();
auto *meta = getValueObjectMetaInfo(); if (aMeta && bMeta)
auto *otherMeta = other.getValueObjectMetaInfo();
if (meta && otherMeta)
{ {
const void *casted = nullptr; 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 else
{ {
@@ -60,8 +58,8 @@ namespace BlackMisc
return 0; return 0;
} }
} }
if (m_v < other.m_v) { return -1; } if (a.m_v < b.m_v) { return -1; }
if (m_v > other.m_v) { return 1; } if (a.m_v > b.m_v) { return 1; }
return 0; return 0;
} }

View File

@@ -32,7 +32,7 @@ namespace BlackMisc
using MetaType = Policy::MetaType::QMetaTypeAndDBusOnly; using MetaType = Policy::MetaType::QMetaTypeAndDBusOnly;
using Equals = Policy::Equals::OwnEquals; using Equals = Policy::Equals::OwnEquals;
using LessThan = Policy::LessThan::OwnLessThan; using LessThan = Policy::LessThan::OwnLessThan;
using Compare = Policy::Compare::Own; using Compare = Policy::Compare::None;
using Hash = Policy::Hash::Own; using Hash = Policy::Hash::Own;
using DBus = Policy::DBus::Own; using DBus = Policy::DBus::Own;
using Json = Policy::Json::Own; using Json = Policy::Json::Own;
@@ -194,8 +194,8 @@ namespace BlackMisc
//! Less than operator. //! Less than operator.
friend bool operator <(const CVariant &a, const CVariant &b) { return compare(a, b) < 0; } friend bool operator <(const CVariant &a, const CVariant &b) { return compare(a, b) < 0; }
//! \copydoc CValueObject::compareImpl //! \copydoc CValueObject::compare
virtual int compareImpl(const CValueObject &other) const override; friend int compare(const CVariant &a, const CVariant &b) { return compareImpl(a, b); }
//! \copydoc CValueObject::setPropertyByIndex //! \copydoc CValueObject::setPropertyByIndex
virtual void setPropertyByIndex(const CVariant &variant, const CPropertyIndex &index) override; 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); } Private::IValueObjectMetaInfo *getValueObjectMetaInfo() const { return Private::getValueObjectMetaInfo(m_v); }
void *data() { return m_v.data(); } void *data() { return m_v.data(); }
const void *data() const { 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. //! Compare stored value of CVariant with any CValueObject derived class.