mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-06 01:45:38 +08:00
refs #84 removed the CValueObject::compare method and added a friend function BlackMisc::compare to replace it.
The new compare is implemented using "multimethods" described in the book Advanced C++ Programming Styles and Idioms by James Coplien. First, the isA method is used to determine which of the values being compared is the most general. (For example, CLength is more general than CAltitude.) Then the compareImpl method is called on the most general value, with the other value as an argument. If there is not a direct inheritance relation between the two values (or they are the same class) then the comparison is invalid and a assert is triggered.
This commit is contained in:
@@ -71,17 +71,34 @@ namespace BlackMisc
|
||||
return LATorLON(a);
|
||||
}
|
||||
|
||||
/*
|
||||
* metaTypeId
|
||||
*/
|
||||
template <class LATorLON> int CEarthAngle<LATorLON>::getMetaTypeId() const
|
||||
{
|
||||
return qMetaTypeId<LATorLON>();
|
||||
}
|
||||
|
||||
/*
|
||||
* is a
|
||||
*/
|
||||
template <class LATorLON> bool CEarthAngle<LATorLON>::isA(int metaTypeId) const
|
||||
{
|
||||
if (metaTypeId == qMetaTypeId<LATorLON>()) { return true; }
|
||||
|
||||
return this->CAngle::isA(metaTypeId);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compare
|
||||
*/
|
||||
template <class LATorLON> int CEarthAngle<LATorLON>::compare(const QVariant &qv) const
|
||||
template <class LATorLON> int CEarthAngle<LATorLON>::compareImpl(const CValueObject &otherBase) const
|
||||
{
|
||||
Q_ASSERT(qv.canConvert<LATorLON>() || qv.canConvert<CAngle>());
|
||||
Q_ASSERT(qv.isValid() && !qv.isNull());
|
||||
if (qv.canConvert<LATorLON>())
|
||||
return this->compare(qv.value<LATorLON>());
|
||||
else
|
||||
return this->compare(qv.value<CAngle>());
|
||||
const auto &other = static_cast<const LATorLON &>(otherBase);
|
||||
|
||||
if (*this < other) { return -1; }
|
||||
else if (*this > other) { return 1; }
|
||||
else { return 0; }
|
||||
}
|
||||
|
||||
// see here for the reason of thess forward instantiations
|
||||
|
||||
Reference in New Issue
Block a user