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_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 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
5#ifndef QINTRUSIVELIST_P_H
6#define QINTRUSIVELIST_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/private/qglobal_p.h>
20
21QT_BEGIN_NAMESPACE
22
23class QIntrusiveListNode
24{
25public:
26 ~QIntrusiveListNode() { remove(); }
27
28 void remove()
29 {
30 if (_prev) *_prev = _next;
31 if (_next) _next->_prev = _prev;
32 _prev = nullptr;
33 _next = nullptr;
34 }
35
36 bool isInList() const { return _prev != nullptr; }
37
38private:
39 template<class N, QIntrusiveListNode N::*member>
40 friend class QIntrusiveList;
41
42 QIntrusiveListNode *_next = nullptr;
43 QIntrusiveListNode**_prev = nullptr;
44};
45
46template<class N, QIntrusiveListNode N::*member>
48{
49private:
50 template<typename O>
51 class iterator_impl {
52 public:
53 iterator_impl() = default;
54 iterator_impl(O value) : _value(value) {}
55
56 O operator*() const { return _value; }
57 O operator->() const { return _value; }
58 bool operator==(const iterator_impl &other) const { return other._value == _value; }
59 bool operator!=(const iterator_impl &other) const { return other._value != _value; }
60 iterator_impl &operator++()
61 {
62 _value = QIntrusiveList<N, member>::next(_value);
63 return *this;
64 }
65
66 protected:
67 O _value = nullptr;
68 };
69
70public:
71 class iterator : public iterator_impl<N *>
72 {
73 public:
74 iterator() = default;
75 iterator(N *value) : iterator_impl<N *>(value) {}
76
78 {
79 N *old = this->_value;
80 this->_value = QIntrusiveList<N, member>::next(this->_value);
81 (old->*member).remove();
82 return *this;
83 }
84 };
85
86 using const_iterator = iterator_impl<const N *>;
87
90
91 ~QIntrusiveList() { while (__first) __first->remove(); }
92
93 bool isEmpty() const { return __first == nullptr; }
94
95 void insert(N *n)
96 {
97 QIntrusiveListNode *nnode = &(n->*member);
98 nnode->remove();
99
100 nnode->_next = __first;
101 if (nnode->_next) nnode->_next->_prev = &nnode->_next;
102 __first = nnode;
103 nnode->_prev = &__first;
104 }
105
106 void remove(N *n)
107 {
108 QIntrusiveListNode *nnode = &(n->*member);
109 nnode->remove();
110 }
111
112 bool contains(const N *n) const
113 {
114 QIntrusiveListNode *nnode = __first;
115 while (nnode) {
116 if (nodeToN(nnode) == n)
117 return true;
118 nnode = nnode->_next;
119 }
120 return false;
121 }
122
123 const N *first() const { return __first ? nodeToN(__first) : nullptr; }
124 N *first() { return __first ? nodeToN(__first) : nullptr; }
125
126 template<typename O>
127 static O next(O current)
128 {
129 QIntrusiveListNode *nextnode = (current->*member)._next;
130 return nextnode ? nodeToN(nextnode) : nullptr;
131 }
132
133 iterator begin() { return __first ? iterator(nodeToN(__first)) : iterator(); }
134 iterator end() { return iterator(); }
135
137 {
138 return __first ? const_iterator(nodeToN(__first)) : const_iterator();
139 }
140
141 const_iterator end() const { return const_iterator(); }
142
143private:
144
145 static N *nodeToN(QIntrusiveListNode *node)
146 {
147 QT_WARNING_PUSH
148#if defined(Q_CC_CLANG) && Q_CC_CLANG >= 1300
149 QT_WARNING_DISABLE_CLANG("-Wnull-pointer-subtraction")
150#endif
151 return (N *)((char *)node - ((char *)&(((N *)nullptr)->*member) - (char *)nullptr));
152 QT_WARNING_POP
153 }
154
155 QIntrusiveListNode *__first = nullptr;
156};
157
158QT_END_NAMESPACE
159
160#endif // QINTRUSIVELIST_P_H
iterator & erase()
Remove the current object from the list, and return an iterator to the next element.
The QIntrusiveList class is a template class that provides a list of objects using static storage.
bool isEmpty() const
void insert(N *n)
Insert object into the list.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list.
void remove(N *n)
Remove object from the list.
static O next(O current)
Returns the next object after current, or null if current is the last object.
~QIntrusiveList()
Destroy the list.
iterator_impl< const N * > const_iterator
iterator begin()
Returns an STL-style interator pointing to the first item in the list.
const N * first() const
Returns the first entry in this list, or null if the list is empty.
const_iterator ConstIterator
bool contains(const N *n) const
Returns true if the list contains object; otherwise returns false.
const_iterator begin() const
const_iterator end() const