diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-09-18 15:45:36 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-10-01 12:15:22 +0200 |
commit | 4d943225eb7b8de22e33771b9389c919d59a4bd3 (patch) | |
tree | 4e6c1de71408d13f54bad71162c5338c0ccfb0a0 /src/corelib/kernel/qvariant.cpp | |
parent | f518f01be602a2dad00d81457ce480c4db50aa0f (diff) |
Add a QVariant::compare() method
Add a method that allows comparing two variants. The method returns
a std::optional, as comparing two variants of different type is not
meaningful, or the types could not be comparable.
Change-Id: If4ae838d671e051dda1b474f25a2f9dcf85dc265
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/kernel/qvariant.cpp')
-rw-r--r-- | src/corelib/kernel/qvariant.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 31c2173072..ad52921cb7 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -2329,6 +2329,33 @@ bool QVariant::equals(const QVariant &v) const } /*! + Compares the objects at \a lhs and \a rhs for ordering. + + Returns an std::nullopt if comparison is not supported or the values are unordered. + Otherwise, returns -1, 0 or +1 according as \a lhs is less than, equal to or greater + than \a rhs. + + If the variants contain data with a different metatype, the values are considered + unordered unless they are both of numeric or pointer types, where regular numeric or + pointer comparison rules will be used. + + If both variants contain data of the same metatype, the method will use the + QMetaType::compare method to determine the ordering of the two variants, which can + also indicate that it can't establish an ordering between the two values. + + \since 6.0 + \sa QMetaType::compare(), QMetaType::isOrdered() +*/ +std::optional<int> QVariant::compare(const QVariant &lhs, const QVariant &rhs) +{ + QMetaType t = lhs.d.type(); + if (t != rhs.d.type()) + return std::nullopt; + return t.compare(lhs.constData(), rhs.constData()); +} + + +/*! \fn const void *QVariant::constData() const \fn const void* QVariant::data() const \internal |