1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qintrusivelist_p.h"
46 \brief The QIntrusiveList class is a template class that provides a list of objects using static storage.
49 QIntrusiveList creates a linked list of objects. Adding and removing objects from the
50 QIntrusiveList is a constant time operation and is very quick. The list performs no memory
51 allocations, but does require the objects being added to the list to contain a QIntrusiveListNode
52 instance for the list's use. Even so, for small lists QIntrusiveList uses less memory than Qt's
55 As QIntrusiveList uses storage inside the objects in the list, each object can only be in one
56 list at a time. Objects are inserted by the insert() method. If the object is already
57 in a list (including the one it is being inserted into) it is first removed, and then inserted
58 at the head of the list. QIntrusiveList is a last-in-first-out list. That is, following an
59 insert() the inserted object becomes the list's first() object.
63 MyObject(int value) : value(value) {}
66 QIntrusiveListNode node;
68 typedef QIntrusiveList<MyObject, &MyObject::node> MyObjectList;
81 // QIntrusiveList is LIFO, so will print: 2... 1... 0...
82 for (MyObjectList::iterator iter = list.begin(); iter != list.end(); ++iter) {
83 qWarning() << iter->value;
91 \fn QIntrusiveList::QIntrusiveList();
93 Construct an empty list.
97 \fn QIntrusiveList::~QIntrusiveList();
99 Destroy the list. All entries are removed.
103 \fn void QIntrusiveList::insert(N *object);
105 Insert \a object into the list. If \a object is a member of this, or another list, it will be
106 removed and inserted at the head of this list.
110 \fn void QIntrusiveList::remove(N *object);
112 Remove \a object from the list. \a object must not be null.
116 \fn bool QIntrusiveList::contains(N *object) const
118 Returns true if the list contains \a object; otherwise returns false.
122 \fn N *QIntrusiveList::first() const
124 Returns the first entry in this list, or null if the list is empty.
128 \fn N *QIntrusiveList::next(N *current)
130 Returns the next object after \a current, or null if \a current is the last object. \a current cannot be null.
134 \fn iterator QIntrusiveList::begin()
136 Returns an STL-style interator pointing to the first item in the list.
142 \fn iterator QIntrusiveList::end()
144 Returns an STL-style iterator pointing to the imaginary item after the last item in the list.
150 iterator &QInplacelist::iterator::erase()
152 Remove the current object from the list, and return an iterator to the next element.
157 \fn QIntrusiveListNode::QIntrusiveListNode()
159 Create a QIntrusiveListNode.
163 \fn QIntrusiveListNode::~QIntrusiveListNode()
165 Destroy the QIntrusiveListNode. If the node is in a list, it is removed.
169 \fn void QIntrusiveListNode::remove()
171 If in a list, remove this node otherwise do nothing.
175 \fn bool QIntrusiveListNode::isInList() const
177 Returns true if this node is in a list, false otherwise.