Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qintrusivelist.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
6
7/*!
8\class QIntrusiveList
9\brief The QIntrusiveList class is a template class that provides a list of objects using static storage.
10\internal
11
12QIntrusiveList creates a linked list of objects. Adding and removing objects from the
13QIntrusiveList is a constant time operation and is very quick. The list performs no memory
14allocations, but does require the objects being added to the list to contain a QIntrusiveListNode
15instance for the list's use. Even so, for small lists QIntrusiveList uses less memory than Qt's
16other list classes.
17
18As QIntrusiveList uses storage inside the objects in the list, each object can only be in one
19list at a time. Objects are inserted by the insert() method. If the object is already
20in a list (including the one it is being inserted into) it is first removed, and then inserted
21at the head of the list. QIntrusiveList is a last-in-first-out list. That is, following an
22insert() the inserted object becomes the list's first() object.
23
24\code
25struct MyObject {
26 MyObject(int value) : value(value) {}
27
28 int value;
29 QIntrusiveListNode node;
30};
31typedef QIntrusiveList<MyObject, &MyObject::node> MyObjectList;
32
33void foo() {
34 MyObjectList list;
35
36 MyObject m0(0);
37 MyObject m1(1);
38 MyObject m2(2);
39
40 list.insert(&m0);
41 list.insert(&m1);
42 list.insert(&m2);
43
44 // QIntrusiveList is LIFO, so will print: 2... 1... 0...
45 for (MyObjectList::iterator iter = list.begin(); iter != list.end(); ++iter) {
46 qWarning() << iter->value;
47 }
48}
49\endcode
50*/
51
52
53/*!
54\fn QIntrusiveList::QIntrusiveList();
55
56Construct an empty list.
57*/
58
59/*!
60\fn QIntrusiveList::~QIntrusiveList();
61
62Destroy the list. All entries are removed.
63*/
64
65/*!
66\fn void QIntrusiveList::insert(N *object);
67
68Insert \a object into the list. If \a object is a member of this, or another list, it will be
69removed and inserted at the head of this list.
70*/
71
72/*!
73\fn void QIntrusiveList::remove(N *object);
74
75Remove \a object from the list. \a object must not be null.
76*/
77
78/*!
79\fn bool QIntrusiveList::contains(N *object) const
80
81Returns true if the list contains \a object; otherwise returns false.
82*/
83
84/*!
85\fn N *QIntrusiveList::first() const
86
87Returns the first entry in this list, or null if the list is empty.
88*/
89
90/*!
91\fn N *QIntrusiveList::next(N *current)
92
93Returns the next object after \a current, or null if \a current is the last object. \a current cannot be null.
94*/
95
96/*!
97\fn iterator QIntrusiveList::begin()
98
99Returns an STL-style interator pointing to the first item in the list.
100
101\sa end()
102*/
103
104/*!
105\fn iterator QIntrusiveList::end()
106
107Returns an STL-style iterator pointing to the imaginary item after the last item in the list.
108
109\sa begin()
110*/
111
112/*!
113\fn iterator &QIntrusiveList::iterator::erase()
114
115Remove the current object from the list, and return an iterator to the next element.
116*/
117
118/*!
119 \class QIntrusiveListNode
120 \internal
121*/
122
123/*!
124\fn QIntrusiveListNode::QIntrusiveListNode()
125
126Create a QIntrusiveListNode.
127*/
128
129/*!
130\fn QIntrusiveListNode::~QIntrusiveListNode()
131
132Destroy the QIntrusiveListNode. If the node is in a list, it is removed.
133*/
134
135/*!
136\fn void QIntrusiveListNode::remove()
137
138If in a list, remove this node otherwise do nothing.
139*/
140
141/*!
142\fn bool QIntrusiveListNode::isInList() const
143
144Returns true if this node is in a list, false otherwise.
145*/