Introducing QArrayData
authorJoão Abecasis <joao.abecasis@nokia.com>
Thu, 1 Dec 2011 14:17:10 +0000 (15:17 +0100)
committerQt by Nokia <qt-info@nokia.com>
Tue, 6 Dec 2011 15:57:17 +0000 (16:57 +0100)
Modeled on QByteArrayData/QStringData/QVectorData, the intent is to
unify book-keeping structs for array-like data and enable sharing of
code among them.

As in those structures, size (and alloc) data member(s) specify the
number of *typed* elements the array does (and can) hold.  The size or
alignment requirements of those objects is not tracked in this data
structure and needs to be maintained by its users.

Contrary to QByteArrayData and QStringData, QArrayData's offset member
keeps a *byte* offset to the actual data array and is computed from the
beginning of the struct.

Shared-null and -empty functionality is provided by QArrayData and
shared among all users.

Planned features include setSharable (force deep copies), fromRawData
(detached header and data allocations) and literals a la QStringLiteral
(static immutable instances), thus covering the functionality needed for
QByteArray, QString and QVector.

Change-Id: I9aa709dbb675442e6d06965efb8138ab84602bbd
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
src/corelib/tools/qarraydata.cpp [new file with mode: 0644]
src/corelib/tools/qarraydata.h [new file with mode: 0644]
src/corelib/tools/tools.pri
tests/auto/corelib/tools/qarraydata/qarraydata.pro [new file with mode: 0644]
tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp [new file with mode: 0644]
tests/auto/corelib/tools/tools.pro

diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp
new file mode 100644 (file)
index 0000000..3b15c0e
--- /dev/null
@@ -0,0 +1,49 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtCore/qarraydata.h>
+
+QT_BEGIN_NAMESPACE
+
+const QArrayData QArrayData::shared_null = { Q_REFCOUNT_INITIALIZER(-1), 0, 0, 0, 0 };
+const QArrayData QArrayData::shared_empty = { Q_REFCOUNT_INITIALIZER(-1), 0, 0, 0, 0 };
+
+QT_END_NAMESPACE
diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h
new file mode 100644 (file)
index 0000000..0b6949f
--- /dev/null
@@ -0,0 +1,97 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QARRAYDATA_H
+#define QARRAYDATA_H
+
+#include <QtCore/qrefcount.h>
+
+QT_BEGIN_HEADER
+
+QT_BEGIN_NAMESPACE
+
+QT_MODULE(Core)
+
+struct Q_CORE_EXPORT QArrayData
+{
+    QtPrivate::RefCount ref;
+    int size;
+    uint alloc : 31;
+    uint capacityReserved : 1;
+
+    qptrdiff offset; // in bytes from beginning of header
+
+    void *data()
+    {
+        Q_ASSERT(size == 0
+                || offset < 0 || size_t(offset) >= sizeof(QArrayData));
+        return reinterpret_cast<char *>(this) + offset;
+    }
+
+    const void *data() const
+    {
+        Q_ASSERT(size == 0
+                || offset < 0 || size_t(offset) >= sizeof(QArrayData));
+        return reinterpret_cast<const char *>(this) + offset;
+    }
+
+    static const QArrayData shared_null;
+    static const QArrayData shared_empty;
+};
+
+template <class T, size_t N>
+struct QStaticArrayData
+{
+    QArrayData header;
+    T data[N];
+};
+
+#define Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(type, size) { \
+    Q_REFCOUNT_INITIALIZER(-1), size, 0, 0, \
+    (sizeof(QArrayData) + (Q_ALIGNOF(type) - 1)) \
+        & ~(Q_ALIGNOF(type) - 1) } \
+    /**/
+
+QT_END_NAMESPACE
+
+QT_END_HEADER
+
+#endif // include guard
index 13b597d..199dd85 100644 (file)
@@ -2,6 +2,7 @@
 
 HEADERS +=  \
         tools/qalgorithms.h \
+        tools/qarraydata.h \
         tools/qbitarray.h \
         tools/qbytearray.h \
         tools/qbytearraymatcher.h \
@@ -55,6 +56,7 @@ HEADERS +=  \
 
 
 SOURCES += \
+        tools/qarraydata.cpp \
         tools/qbitarray.cpp \
         tools/qbytearray.cpp \
         tools/qbytearraymatcher.cpp \
diff --git a/tests/auto/corelib/tools/qarraydata/qarraydata.pro b/tests/auto/corelib/tools/qarraydata/qarraydata.pro
new file mode 100644 (file)
index 0000000..d384408
--- /dev/null
@@ -0,0 +1,4 @@
+TARGET = tst_qarraydata
+SOURCES  += tst_qarraydata.cpp
+QT = core testlib
+CONFIG += testcase parallel_test
diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp
new file mode 100644 (file)
index 0000000..31006c6
--- /dev/null
@@ -0,0 +1,152 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+
+#include <QtTest/QtTest>
+#include <QtCore/qarraydata.h>
+
+class tst_QArrayData : public QObject
+{
+    Q_OBJECT
+
+private slots:
+    void referenceCounting();
+    void sharedNullEmpty();
+    void staticData();
+};
+
+void tst_QArrayData::referenceCounting()
+{
+    {
+        // Reference counting initialized to 1 (owned)
+        QArrayData array = { Q_REFCOUNT_INITIALIZER(1), 0, 0, 0, 0 };
+
+        QCOMPARE(int(array.ref), 1);
+
+        array.ref.ref();
+        QCOMPARE(int(array.ref), 2);
+
+        QVERIFY(array.ref.deref());
+        QCOMPARE(int(array.ref), 1);
+
+        array.ref.ref();
+        QCOMPARE(int(array.ref), 2);
+
+        QVERIFY(array.ref.deref());
+        QCOMPARE(int(array.ref), 1);
+
+        QVERIFY(!array.ref.deref());
+        QCOMPARE(int(array.ref), 0);
+
+        // Now would be a good time to free/release allocated data
+    }
+
+    {
+        // Reference counting initialized to -1 (static read-only data)
+        QArrayData array = { Q_REFCOUNT_INITIALIZER(-1), 0, 0, 0, 0 };
+
+        QCOMPARE(int(array.ref), -1);
+
+        array.ref.ref();
+        QCOMPARE(int(array.ref), -1);
+
+        QVERIFY(array.ref.deref());
+        QCOMPARE(int(array.ref), -1);
+    }
+}
+
+void tst_QArrayData::sharedNullEmpty()
+{
+    QArrayData *null = const_cast<QArrayData *>(&QArrayData::shared_null);
+    QArrayData *empty = const_cast<QArrayData *>(&QArrayData::shared_empty);
+
+    QCOMPARE(int(null->ref), -1);
+    QCOMPARE(int(empty->ref), -1);
+
+    null->ref.ref();
+    empty->ref.ref();
+
+    QCOMPARE(int(null->ref), -1);
+    QCOMPARE(int(empty->ref), -1);
+
+    QVERIFY(null->ref.deref());
+    QVERIFY(empty->ref.deref());
+
+    QCOMPARE(int(null->ref), -1);
+    QCOMPARE(int(empty->ref), -1);
+
+    QVERIFY(null != empty);
+
+    QCOMPARE(null->size, 0);
+    QCOMPARE(null->alloc, 0u);
+    QCOMPARE(null->capacityReserved, 0u);
+
+    QCOMPARE(empty->size, 0);
+    QCOMPARE(empty->alloc, 0u);
+    QCOMPARE(empty->capacityReserved, 0u);
+}
+
+void tst_QArrayData::staticData()
+{
+    QStaticArrayData<char, 10> charArray = {
+        Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(char, 10),
+        { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }
+    };
+    QStaticArrayData<int, 10> intArray = {
+        Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(int, 10),
+        { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
+    };
+    QStaticArrayData<double, 10> doubleArray = {
+        Q_STATIC_ARRAY_DATA_HEADER_INITIALIZER(double, 10),
+        { 0.f, 1.f, 2.f, 3.f, 4.f, 5.f, 6.f, 7.f, 8.f, 9.f }
+    };
+
+    QCOMPARE(charArray.header.size, 10);
+    QCOMPARE(intArray.header.size, 10);
+    QCOMPARE(doubleArray.header.size, 10);
+
+    QCOMPARE(charArray.header.data(), reinterpret_cast<void *>(&charArray.data));
+    QCOMPARE(intArray.header.data(), reinterpret_cast<void *>(&intArray.data));
+    QCOMPARE(doubleArray.header.data(), reinterpret_cast<void *>(&doubleArray.data));
+}
+
+QTEST_APPLESS_MAIN(tst_QArrayData)
+#include "tst_qarraydata.moc"
index 930799e..e2002a9 100644 (file)
@@ -1,6 +1,7 @@
 TEMPLATE=subdirs
 SUBDIRS=\
     qalgorithms \
+    qarraydata \
     qbitarray \
     qbytearray \
     qbytearraymatcher \