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:
Mathew Sutcliffe
2014-01-14 00:45:19 +00:00
parent e40c93fe1b
commit db4c05dd9f
48 changed files with 1015 additions and 119 deletions

View File

@@ -141,6 +141,15 @@ namespace BlackMisc
*/
friend bool operator!=(const CValueObject &uc, const CValueMap &valueMap);
/*!
* Compares two instances of related classes
* and returns an integer less than, equal to, or greater than zero
* if v1 is less than, equal to, or greater than v2.
* \return
* \pre The runtime types of the two objects must be the same or related by inheritance.
*/
friend int compare(const CValueObject &v1, const CValueObject &v2);
public:
/*!
* \brief Virtual destructor
@@ -179,15 +188,6 @@ namespace BlackMisc
*/
virtual uint getValueHash() const = 0;
/*!
* Compares with QVariant with this object
* and returns an integer less than, equal to, or greater than zero
* if this is less than, equal to, or greater than QVariant.
* \remarks allows sorting among QVariants, not all classes implement this
* \return
*/
virtual int compare(const QVariant &qv) const;
/*!
* \brief Virtual method to return QVariant, used with DBUS QVariant lists
* \return
@@ -257,6 +257,30 @@ namespace BlackMisc
*/
virtual QString convertToQString(bool i18n = false) const = 0;
/*!
* \brief Returns the Qt meta type ID of this object.
* \return
*/
virtual int getMetaTypeId() const = 0;
/*!
* \brief Returns true if this object is an instance of the class with the given meta type ID,
* or one of its subclasses.
* \param metaTypeId
* \return
*/
virtual bool isA(int metaTypeId) const { Q_UNUSED(metaTypeId); return false; }
/*!
* \brief Compare this value with another value of the same type
* \param other
* \return Less than, equal to, or greater than zero if this is
* less than, equal to, or greather than other.
* \pre Other must have the same runtime type as the this object.
* \remark It is usually safer to use the friend function compare() instead.
*/
virtual int compareImpl(const CValueObject &other) const = 0;
/*!
* \brief Marshall to DBus
* \param argument