path: root/tests/auto/corelib/tools/qstringbuilder
diff options
authorJoão Abecasis <>2012-04-01 01:05:47 +0200
committerQt by Nokia <>2012-04-04 23:29:37 +0200
commita959f34d716f42925b22d42838e7a4b97e415c69 (patch)
tree7491904bb192c19c04538665b590724dece8a36a /tests/auto/corelib/tools/qstringbuilder
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 <>
Diffstat (limited to 'tests/auto/corelib/tools/qstringbuilder')
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qstringbuilder/qstringbuilder1/stringbuilder.cpp b/tests/auto/corelib/tools/qstringbuilder/qstringbuilder1/stringbuilder.cpp
index 556b9ac16a..429652d92d 100644
--- a/tests/auto/corelib/tools/qstringbuilder/qstringbuilder1/stringbuilder.cpp
+++ b/tests/auto/corelib/tools/qstringbuilder/qstringbuilder1/stringbuilder.cpp
@@ -107,6 +107,21 @@ void runScenario()
QCOMPARE(r, r3);
+ {
+ static const QStaticStringData<12> literalData = {
+ { Q_REFCOUNT_INITIALIZE_STATIC, 12, 0, 0, sizeof(QStringData) },
+ { 's', 'o', 'm', 'e', ' ', 'l', 'i', 't', 'e', 'r', 'a', 'l' }
+ };
+ static QStringDataPtr literal = { literalData.data_ptr() };
+ r = literal;
+ QCOMPARE(r, string);
+ r = r Q literal;
+ QCOMPARE(r, r2);
+ r = literal Q literal;
+ QCOMPARE(r, r2);
+ }
r = string P LITERAL;
QCOMPARE(r, r2);
@@ -211,6 +226,21 @@ void runScenario()
QCOMPARE(r, ba);
+ {
+ static const QStaticByteArrayData<12> literalData = {
+ { Q_REFCOUNT_INITIALIZE_STATIC, 12, 0, 0, sizeof(QByteArrayData) },
+ { 's', 'o', 'm', 'e', ' ', 'l', 'i', 't', 'e', 'r', 'a', 'l' }
+ };
+ static QByteArrayDataPtr literal = { literalData.data_ptr() };
+ QByteArray ba = literal;
+ QCOMPARE(ba, QByteArray(LITERAL));
+ ba = ba Q literal;
+ ba = literal Q literal;
+ }
//operator QString +=
QString str = QString::fromUtf8(UTF8_LITERAL);