From a959f34d716f42925b22d42838e7a4b97e415c69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= Date: Sun, 1 Apr 2012 01:05:47 +0200 Subject: Clean up constructors for "statics" in QString and QByteArray There were two constuctors offering essentially the same functionality. One taking the QStatic*Data 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 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 --- src/corelib/tools/qstringbuilder.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/corelib/tools/qstringbuilder.h') diff --git a/src/corelib/tools/qstringbuilder.h b/src/corelib/tools/qstringbuilder.h index 0b85e590cd..9a1fd6949b 100644 --- a/src/corelib/tools/qstringbuilder.h +++ b/src/corelib/tools/qstringbuilder.h @@ -253,16 +253,16 @@ template <> struct QConcatenable : private QAbstractConcatenable } }; -template struct QConcatenable > : private QAbstractConcatenable +template <> struct QConcatenable : private QAbstractConcatenable { - typedef QStaticStringDataPtr type; + typedef QStringDataPtr type; typedef QString ConvertTo; enum { ExactSize = true }; - static int size(const type &) { return N; } + static int size(const type &a) { return a.ptr->size; } static inline void appendTo(const type &a, QChar *&out) { - memcpy(out, reinterpret_cast(a.ptr->data), sizeof(QChar) * N); - out += N; + memcpy(out, reinterpret_cast(a.ptr->data()), sizeof(QChar) * a.ptr->size); + out += a.ptr->size; } }; @@ -358,22 +358,22 @@ template <> struct QConcatenable : private QAbstractConcatenable } }; -template struct QConcatenable > : private QAbstractConcatenable +template <> struct QConcatenable : private QAbstractConcatenable { - typedef QStaticByteArrayDataPtr type; + typedef QByteArrayDataPtr type; typedef QByteArray ConvertTo; enum { ExactSize = false }; - static int size(const type &) { return N; } + static int size(const type &ba) { return ba.ptr->size; } #ifndef QT_NO_CAST_FROM_ASCII static inline QT_ASCII_CAST_WARN void appendTo(const type &a, QChar *&out) { - QAbstractConcatenable::convertFromAscii(a.ptr->data, N, out); + QAbstractConcatenable::convertFromAscii(a.ptr->data(), a.ptr->size, out); } #endif static inline void appendTo(const type &ba, char *&out) { - ::memcpy(out, ba.ptr->data, N); - out += N; + ::memcpy(out, ba.ptr->data(), ba.ptr->size); + out += ba.ptr->size; } }; -- cgit v1.2.3