summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qarraydata.h
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2013-10-11 13:42:01 -0700
committerLars Knoll <lars.knoll@qt.io>2019-12-07 14:17:45 +0100
commit41287d355b9571db0fbdf5841b31595705af0102 (patch)
tree150fcfba72714a6e9ff94cad0d909016de973902 /src/corelib/tools/qarraydata.h
parent329ec3a268d636cc2cd4e403b5323c4d65723d33 (diff)
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>
Diffstat (limited to 'src/corelib/tools/qarraydata.h')
-rw-r--r--src/corelib/tools/qarraydata.h12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h
index a2d9901677..a91dd54262 100644
--- a/src/corelib/tools/qarraydata.h
+++ b/src/corelib/tools/qarraydata.h
@@ -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>