Add QArrayData::sharedNullData()

Just to simplify a few operations, like detecting when a QChar* or char*
coming from a QString or QByteArray, respectively, were null data.

While you're not supposed to dereference the pointer returned by
QVector::data() unless you know that the array is non-empty, that is
permitted for QString and QByteArray. That is, QString().constData()
must return a valid pointer to a null QChar.

Change-Id: I80b4b62f203dc841e5c99c20c51d92ca576e4bfe
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
bb10
Thiago Macieira 2013-10-11 13:42:01 -07:00 committed by Lars Knoll
parent 329ec3a268
commit 41287d355b
1 changed files with 12 additions and 0 deletions

View File

@ -122,6 +122,12 @@ struct Q_CORE_EXPORT QArrayData
static const QArrayData shared_null[2];
static QArrayData *sharedNull() noexcept { return const_cast<QArrayData*>(shared_null); }
static void *sharedNullData()
{
QArrayData *const null = const_cast<QArrayData *>(&shared_null[1]);
Q_ASSERT(sharedNull()->data() == null);
return null;
}
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QArrayData::AllocationOptions)
@ -273,6 +279,12 @@ struct QTypedArrayData
Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData));
return allocate(/* capacity */ 0);
}
static T *sharedNullData()
{
Q_STATIC_ASSERT(sizeof(QTypedArrayData) == sizeof(QArrayData));
return static_cast<T *>(QArrayData::sharedNullData());
}
};
template <class T, size_t N>