aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorhjk <hjk@qt.io>2017-03-02 09:44:53 +0100
committerhjk <hjk@qt.io>2017-03-03 14:59:44 +0000
commitf6715fe52b168ac94f27d7bd1f6163fd318315e4 (patch)
tree6b1dc1be46fcc1662efa7b7e06527aa86c896578 /tests
parente9cdc807e764fe5f00b1fa7e19e217fb2826fdad (diff)
Utils: Add a ObjectPool convenience class template
The ObjectPool class template provides parts of the functionality of the global PluginManager object pool but is intented to be used with smaller set objects, typically with same base type (e.g. factories) only. The ObjectPool takes ownership of add items if and only if the item does not have a QObject parent. Items owned by the Object pool are destructed when the pool is destructed, the other items are taken care of by their QObject parent according to the usual parent/child behavior. Change-Id: I60886095c8b04eae017e1fb56774b1bf66dbefa1 Reviewed-by: Tobias Hunger <tobias.hunger@qt.io> Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/utils/objectpool/objectpool.pro4
-rw-r--r--tests/auto/utils/objectpool/objectpool.qbs7
-rw-r--r--tests/auto/utils/objectpool/tst_objectpool.cpp96
-rw-r--r--tests/auto/utils/utils.pro1
-rw-r--r--tests/auto/utils/utils.qbs1
5 files changed, 109 insertions, 0 deletions
diff --git a/tests/auto/utils/objectpool/objectpool.pro b/tests/auto/utils/objectpool/objectpool.pro
new file mode 100644
index 0000000000..4da02c45c5
--- /dev/null
+++ b/tests/auto/utils/objectpool/objectpool.pro
@@ -0,0 +1,4 @@
+QTC_LIB_DEPENDS += utils
+include(../../qttest.pri)
+
+SOURCES += tst_objectpool.cpp
diff --git a/tests/auto/utils/objectpool/objectpool.qbs b/tests/auto/utils/objectpool/objectpool.qbs
new file mode 100644
index 0000000000..1fdd37c19a
--- /dev/null
+++ b/tests/auto/utils/objectpool/objectpool.qbs
@@ -0,0 +1,7 @@
+import qbs
+
+QtcAutotest {
+ name: "ObjectPool autotest"
+ Depends { name: "Utils" }
+ files: "tst_objectpool.cpp"
+}
diff --git a/tests/auto/utils/objectpool/tst_objectpool.cpp b/tests/auto/utils/objectpool/tst_objectpool.cpp
new file mode 100644
index 0000000000..40bf1eb47a
--- /dev/null
+++ b/tests/auto/utils/objectpool/tst_objectpool.cpp
@@ -0,0 +1,96 @@
+/****************************************************************************
+**
+** Copyright (C) 2017 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of Qt Creator.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+****************************************************************************/
+
+#include <utils/objectpool.h>
+
+#include <QtTest>
+
+//TESTED_COMPONENT=src/libs/utils
+
+using namespace Utils;
+
+class tst_ObjectPool : public QObject
+{
+ Q_OBJECT
+
+private slots:
+ void testSize();
+};
+
+void tst_ObjectPool::testSize()
+{
+ QObject parent;
+
+ QPointer<QObject> object1 = new QObject;
+ object1->setObjectName("object1");
+
+ QPointer<QObject> object2 = new QObject(&parent);
+ object2->setObjectName("object2");
+
+ QPointer<QObject> object3 = new QObject;
+ object3->setObjectName("object3");
+
+ QPointer<QObject> object4 = new QObject(&parent);
+ object4->setObjectName("object4");
+
+ {
+ ObjectPool<QObject> pool;
+ QCOMPARE(pool.size(), 0);
+
+ pool.addObject(object1.data());
+ QCOMPARE(pool.size(), 1);
+
+ pool.addObject(object2.data());
+ QCOMPARE(pool.size(), 2);
+
+ pool.addObject(object3.data());
+ QCOMPARE(pool.size(), 3);
+
+ pool.addObject(object4.data());
+ QCOMPARE(pool.size(), 4);
+
+ delete object1;
+ QCOMPARE(pool.size(), 3);
+ QCOMPARE(parent.children().size(), 2);
+
+ delete object2;
+ QCOMPARE(pool.size(), 2);
+ QCOMPARE(parent.children().size(), 1);
+ }
+
+ QCOMPARE(parent.children().size(), 1);
+ QCOMPARE(object3.isNull(), true);
+ QCOMPARE(object4.isNull(), false);
+
+ delete object4;
+ QCOMPARE(parent.children().size(), 0);
+ QCOMPARE(object3.isNull(), true);
+ QCOMPARE(object4.isNull(), true);
+}
+
+
+QTEST_MAIN(tst_ObjectPool)
+
+#include "tst_objectpool.moc"
diff --git a/tests/auto/utils/utils.pro b/tests/auto/utils/utils.pro
index 884634641e..f5d9e94b00 100644
--- a/tests/auto/utils/utils.pro
+++ b/tests/auto/utils/utils.pro
@@ -3,6 +3,7 @@ TEMPLATE = subdirs
SUBDIRS = \
fileutils \
ansiescapecodehandler \
+ objectpool \
stringutils \
templateengine \
treemodel
diff --git a/tests/auto/utils/utils.qbs b/tests/auto/utils/utils.qbs
index 1917c51df2..4389b5b477 100644
--- a/tests/auto/utils/utils.qbs
+++ b/tests/auto/utils/utils.qbs
@@ -6,6 +6,7 @@ Project {
"fileutils/fileutils.qbs",
"ansiescapecodehandler/ansiescapecodehandler.qbs",
"stringutils/stringutils.qbs",
+ "objectpool/objectpool.qbs",
"templateengine/templateengine.qbs",
"treemodel/treemodel.qbs",
]