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>
This commit is contained in:
parent
f518f01be6
commit
4d943225eb
@ -2328,6 +2328,33 @@ bool QVariant::equals(const QVariant &v) const
|
||||
return metatype.equals(d.storage(), v.d.storage());
|
||||
}
|
||||
|
||||
/*!
|
||||
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
|
||||
|
@ -492,6 +492,8 @@ class Q_CORE_EXPORT QVariant
|
||||
inline bool operator!=(const QVariant &v) const
|
||||
{ return !equals(v); }
|
||||
|
||||
static std::optional<int> compare(const QVariant &lhs, const QVariant &rhs);
|
||||
|
||||
protected:
|
||||
friend inline bool operator==(const QVariant &, const QVariantComparisonHelper &);
|
||||
#ifndef QT_NO_DEBUG_STREAM
|
||||
|
Loading…
x
Reference in New Issue
Block a user