summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-09-18 15:45:36 +0200
committerLars Knoll <lars.knoll@qt.io>2020-10-01 12:15:22 +0200
commit4d943225eb7b8de22e33771b9389c919d59a4bd3 (patch)
tree4e6c1de71408d13f54bad71162c5338c0ccfb0a0 /src
parentf518f01be602a2dad00d81457ce480c4db50aa0f (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')
-rw-r--r--src/corelib/kernel/qvariant.cpp27
-rw-r--r--src/corelib/kernel/qvariant.h2
2 files changed, 29 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
diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h
index 83cb40a7c7..c28f841356 100644
--- a/src/corelib/kernel/qvariant.h
+++ b/src/corelib/kernel/qvariant.h
@@ -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