QVariant of nullptr should always be null

Implements isNull for QVariants of a nullptr so they always return
true to isNull(), instead of depending on how they were constructed.

Task-number: QTBUG-58296
Change-Id: Ibddec795cdadedef7e17d22c265c29e752d8f99f
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Allan Sandfeld Jensen 2016-11-10 14:57:04 +01:00
parent 374a173d14
commit f823af43f2
2 changed files with 32 additions and 1 deletions

View File

@ -177,6 +177,26 @@ inline void v_clear(QVariant::Private *d, T* = 0)
} }
template <typename T>
struct PrimitiveIsNull
{
public:
static bool isNull(const QVariant::Private *d)
{
return d->is_null;
}
};
template <>
struct PrimitiveIsNull<std::nullptr_t>
{
public:
static bool isNull(const QVariant::Private *)
{
return true;
}
};
template<class Filter> template<class Filter>
class QVariantComparator { class QVariantComparator {
template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted> template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
@ -268,7 +288,7 @@ class QVariantIsNull
{ {
static bool isNull(const QVariant::Private *d) static bool isNull(const QVariant::Private *d)
{ {
return d->is_null; return PrimitiveIsNull<T>::isNull(d);
} }
}; };

View File

@ -400,6 +400,17 @@ void tst_QVariant::isNull()
QVERIFY( !varLL.isNull() ); QVERIFY( !varLL.isNull() );
QVariant var7(QString::null); QVariant var7(QString::null);
QVERIFY(var7.isNull()); QVERIFY(var7.isNull());
var7 = QVariant::fromValue<QString>(QString::null);
QVERIFY(var7.isNull());
QVariant var8(QMetaType::Nullptr, nullptr);
QVERIFY(var8.isNull());
var8 = QVariant::fromValue<std::nullptr_t>(nullptr);
QVERIFY(var8.isNull());
QVariant var9 = QVariant(QJsonValue(QJsonValue::Null));
QVERIFY(var9.isNull());
var9 = QVariant::fromValue<QJsonValue>(QJsonValue(QJsonValue::Null));
QVERIFY(var9.isNull());
} }
void tst_QVariant::swap() void tst_QVariant::swap()