summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qbytearray.h
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.h
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.h')
-rw-r--r--src/corelib/tools/qbytearray.h37
1 files changed, 22 insertions, 15 deletions
diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h
index 31fa462ca4..2065c8fe91 100644
--- a/src/corelib/tools/qbytearray.h
+++ b/src/corelib/tools/qbytearray.h
@@ -136,21 +136,31 @@ template<int N> struct QStaticByteArrayData
{
QByteArrayData ba;
char data[N + 1];
+
+ QByteArrayData *data_ptr() const
+ {
+ Q_ASSERT(ba.ref.isStatic());
+ return const_cast<QByteArrayData *>(&ba);
+ }
};
-template<int N> struct QStaticByteArrayDataPtr
+struct QByteArrayDataPtr
{
- const QStaticByteArrayData<N> *ptr;
+ QByteArrayData *ptr;
};
#if defined(Q_COMPILER_LAMBDA)
-# define QByteArrayLiteral(str) ([]() -> QStaticByteArrayDataPtr<sizeof(str) - 1> { \
+
+# define QByteArrayLiteral(str) \
+ ([]() -> QByteArrayDataPtr { \
enum { Size = sizeof(str) - 1 }; \
static const QStaticByteArrayData<Size> qbytearray_literal = \
{ { Q_REFCOUNT_INITIALIZE_STATIC, Size, 0, 0, sizeof(QByteArrayData) }, str }; \
- QStaticByteArrayDataPtr<Size> holder = { &qbytearray_literal }; \
- return holder; }())
+ QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; \
+ return holder; \
+ }()) \
+ /**/
#elif defined(Q_CC_GNU)
// We need to create a QByteArrayData in the .rodata section of memory
@@ -162,8 +172,11 @@ template<int N> struct QStaticByteArrayDataPtr
enum { Size = sizeof(str) - 1 }; \
static const QStaticByteArrayData<Size> qbytearray_literal = \
{ { Q_REFCOUNT_INITIALIZE_STATIC, Size, 0, 0, sizeof(QByteArrayData) }, str }; \
- QStaticByteArrayDataPtr<Size> holder = { &qbytearray_literal }; \
- holder; })
+ QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; \
+ holder; \
+ }) \
+ /**/
+
#endif
#ifndef QByteArrayLiteral
@@ -377,19 +390,13 @@ public:
int length() const { return d->size; }
bool isNull() const;
- template <int n>
- inline QByteArray(const QStaticByteArrayData<n> &dd)
- : d(const_cast<QByteArrayData *>(&dd.ba)) {}
- template <int N>
- Q_DECL_CONSTEXPR inline QByteArray(QStaticByteArrayDataPtr<N> dd)
- : d(const_cast<QByteArrayData *>(&dd.ptr->ba)) {}
+ Q_DECL_CONSTEXPR inline QByteArray(QByteArrayDataPtr dd) : d(dd.ptr) {}
private:
operator QNoImplicitBoolCast() const;
static const QStaticByteArrayData<1> shared_null;
static const QStaticByteArrayData<1> shared_empty;
Data *d;
- QByteArray(Data *dd, int /*dummy*/, int /*dummy*/) : d(dd) {}
void realloc(int alloc);
void expand(int i);
QByteArray nulTerminated() const;
@@ -402,7 +409,7 @@ public:
inline DataPtr &data_ptr() { return d; }
};
-inline QByteArray::QByteArray(): d(const_cast<Data *>(&shared_null.ba)) { }
+inline QByteArray::QByteArray(): d(shared_null.data_ptr()) { }
inline QByteArray::~QByteArray() { if (!d->ref.deref()) free(d); }
inline int QByteArray::size() const
{ return d->size; }