summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qbytearray.cpp
diff options
context:
space:
mode:
authorJoão Abecasis <joao.abecasis@nokia.com>2012-04-01 01:05:47 +0200
committerQt by Nokia <qt-info@nokia.com>2012-04-04 23:29:37 +0200
commita959f34d716f42925b22d42838e7a4b97e415c69 (patch)
tree7491904bb192c19c04538665b590724dece8a36a /src/corelib/tools/qbytearray.cpp
parentff6e8460e601a2fb1eb41a1907417524a67505bb (diff)
Clean up constructors for "statics" in QString and QByteArray
There were two constuctors offering essentially the same functionality. One taking the QStatic*Data<N> struct, the other what essentially amounts to a pointer wrapper of that struct. The former was dropped and the latter untemplatized and kept, as that is the most generic and widely applicable. The template parameter in the wrapper was not very useful as it essentially duplicated information that already maintained in the struct, and there were no consistency checks to ensure they were in sync. In this case, using a wrapper is preferred over the use of naked pointers both as a way to make explicit the transfer of ownership as well as to avoid unintended conversions. By using the reference count (even if only by calling deref() in the destructor), QByteArray and QString must own their Data pointers. Const qualification was dropped from the member variable in these wrappers as it causes some compilers to emit warnings on the lack of constructors, and because it isn't needed there. To otherwise reduce noise, QStatic*Data<N> gained a member function to directly access the const_cast'ed naked pointer. This plays nicely with the above constructor. Its use also allows us to do further changes in the QStatic*Data structs with fewer changes in remaining code. The function has an assert on isStatic(), to ensure it is not inadvertently used with data that requires ref-count operations. With this change, the need for the private constructor taking a naked Q*Data pointer is obviated and that was dropped too. In updating QStringBuilder's QConcatenable specializations I noticed they were broken (using data, instead of data()), so a test was added to avoid this happening again in the future. An unnecessary ref-count increment in QByteArray::clear was also dropped. Change-Id: I9b92fbaae726ab9807837e83d0d19812bf7db5ab Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qbytearray.cpp')
-rw-r--r--src/corelib/tools/qbytearray.cpp30
1 files changed, 17 insertions, 13 deletions
diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp
index 574153fdaf..71244e0eab 100644
--- a/src/corelib/tools/qbytearray.cpp
+++ b/src/corelib/tools/qbytearray.cpp
@@ -584,7 +584,10 @@ QByteArray qUncompress(const uchar* data, int nbytes)
d->offset = sizeof(QByteArrayData);
d->data()[len] = 0;
- return QByteArray(d.take(), 0, 0);
+ {
+ QByteArrayDataPtr dataPtr = { d.take() };
+ return QByteArray(dataPtr);
+ }
case Z_MEM_ERROR:
qWarning("qUncompress: Z_MEM_ERROR: Not enough memory");
@@ -912,9 +915,9 @@ QByteArray &QByteArray::operator=(const char *str)
{
Data *x;
if (!str) {
- x = const_cast<Data *>(&shared_null.ba);
+ x = shared_null.data_ptr();
} else if (!*str) {
- x = const_cast<Data *>(&shared_empty.ba);
+ x = shared_empty.data_ptr();
} else {
int len = strlen(str);
if (d->ref.isShared() || len > int(d->alloc) || (len < d->size && len < int(d->alloc) >> 1))
@@ -1320,12 +1323,12 @@ void QByteArray::chop(int n)
QByteArray::QByteArray(const char *data, int size)
{
if (!data) {
- d = const_cast<Data *>(&shared_null.ba);
+ d = shared_null.data_ptr();
} else {
if (size < 0)
size = strlen(data);
if (!size) {
- d = const_cast<Data *>(&shared_empty.ba);
+ d = shared_empty.data_ptr();
} else {
d = static_cast<Data *>(malloc(sizeof(Data) + size + 1));
Q_CHECK_PTR(d);
@@ -1350,7 +1353,7 @@ QByteArray::QByteArray(const char *data, int size)
QByteArray::QByteArray(int size, char ch)
{
if (size <= 0) {
- d = const_cast<Data *>(&shared_empty.ba);
+ d = shared_empty.data_ptr();
} else {
d = static_cast<Data *>(malloc(sizeof(Data) + size + 1));
Q_CHECK_PTR(d);
@@ -1406,7 +1409,7 @@ void QByteArray::resize(int size)
}
if (size == 0 && !d->capacityReserved) {
- Data *x = const_cast<Data *>(&shared_empty.ba);
+ Data *x = shared_empty.data_ptr();
if (!d->ref.deref())
free(d);
d = x;
@@ -2728,8 +2731,7 @@ void QByteArray::clear()
{
if (!d->ref.deref())
free(d);
- d = const_cast<Data *>(&shared_null.ba);
- d->ref.ref();
+ d = shared_null.data_ptr();
}
#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
@@ -3171,7 +3173,8 @@ QByteArray QByteArray::trimmed() const
}
int l = end - start + 1;
if (l <= 0) {
- return QByteArray(const_cast<Data *>(&shared_empty.ba), 0, 0);
+ QByteArrayDataPtr empty = { shared_empty.data_ptr() };
+ return QByteArray(empty);
}
return QByteArray(s+start, l);
}
@@ -3878,9 +3881,9 @@ QByteArray QByteArray::fromRawData(const char *data, int size)
{
Data *x;
if (!data) {
- x = const_cast<Data *>(&shared_null.ba);
+ x = shared_null.data_ptr();
} else if (!size) {
- x = const_cast<Data *>(&shared_empty.ba);
+ x = shared_empty.data_ptr();
} else {
x = static_cast<Data *>(malloc(sizeof(Data) + 1));
Q_CHECK_PTR(x);
@@ -3890,7 +3893,8 @@ QByteArray QByteArray::fromRawData(const char *data, int size)
x->capacityReserved = false;
x->offset = data - reinterpret_cast<char *>(x);
}
- return QByteArray(x, 0, 0);
+ QByteArrayDataPtr dataPtr = { x };
+ return QByteArray(dataPtr);
}
/*!