mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-02 15:15:50 +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:
@@ -87,11 +87,21 @@ namespace BlackMisc
|
||||
/*
|
||||
* Compare
|
||||
*/
|
||||
int CValueObject::compare(const QVariant & /** qv **/) const
|
||||
int compare(const CValueObject &v1, const CValueObject &v2)
|
||||
{
|
||||
// not all classes have to implement this
|
||||
qFatal("Property by index as string not implemented");
|
||||
return -1; // avoid compiler warning
|
||||
if (v1.isA(v2.getMetaTypeId()))
|
||||
{
|
||||
return v2.compareImpl(v1) * -1;
|
||||
}
|
||||
else if (v2.isA(v1.getMetaTypeId()))
|
||||
{
|
||||
return v1.compareImpl(v2);
|
||||
}
|
||||
else
|
||||
{
|
||||
Q_ASSERT_X(false, Q_FUNC_INFO, "Attempt to compare between instances of unrelated classes");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
Reference in New Issue
Block a user