Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#ifndef QINTRUSIVELIST_P_H
5#define QINTRUSIVELIST_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qglobal_p.h>
19
21
23{
24public:
26
27 void remove()
28 {
29 if (_prev) *_prev = _next;
30 if (_next) _next->_prev = _prev;
31 _prev = nullptr;
32 _next = nullptr;
33 }
34
35 bool isInList() const { return _prev != nullptr; }
36
37private:
38 template<class N, QIntrusiveListNode N::*member>
39 friend class QIntrusiveList;
40
41 QIntrusiveListNode *_next = nullptr;
42 QIntrusiveListNode**_prev = nullptr;
43};
44
45template<class N, QIntrusiveListNode N::*member>
47{
48private:
49 template<typename O>
50 class iterator_impl {
51 public:
52 iterator_impl() = default;
53 iterator_impl(O value) : _value(value) {}
54
55 O operator*() const { return _value; }
56 O operator->() const { return _value; }
57 bool operator==(const iterator_impl &other) const { return other._value == _value; }
58 bool operator!=(const iterator_impl &other) const { return other._value != _value; }
59 iterator_impl &operator++()
60 {
61 _value = QIntrusiveList<N, member>::next(_value);
62 return *this;
63 }
64
65 protected:
66 O _value = nullptr;
67 };
68
69public:
70 class iterator : public iterator_impl<N *>
71 {
72 public:
73 iterator() = default;
74 iterator(N *value) : iterator_impl<N *>(value) {}
75
77 {
78 N *old = this->_value;
79 this->_value = QIntrusiveList<N, member>::next(this->_value);
80 (old->*member).remove();
81 return *this;
82 }
83 };
84
85 using const_iterator = iterator_impl<const N *>;
86
89
90 ~QIntrusiveList() { while (__first) __first->remove(); }
91
92 bool isEmpty() const { return __first == nullptr; }
93
94 void insert(N *n)
95 {
96 QIntrusiveListNode *nnode = &(n->*member);
97 nnode->remove();
98
99 nnode->_next = __first;
100 if (nnode->_next) nnode->_next->_prev = &nnode->_next;
101 __first = nnode;
102 nnode->_prev = &__first;
103 }
104
105 void remove(N *n)
106 {
107 QIntrusiveListNode *nnode = &(n->*member);
108 nnode->remove();
109 }
110
111 bool contains(const N *n) const
112 {
113 QIntrusiveListNode *nnode = __first;
114 while (nnode) {
115 if (nodeToN(nnode) == n)
116 return true;
117 nnode = nnode->_next;
118 }
119 return false;
120 }
121
122 const N *first() const { return __first ? nodeToN(__first) : nullptr; }
123 N *first() { return __first ? nodeToN(__first) : nullptr; }
124
125 template<typename O>
126 static O next(O current)
127 {
128 QIntrusiveListNode *nextnode = (current->*member)._next;
129 return nextnode ? nodeToN(nextnode) : nullptr;
130 }
131
132 iterator begin() { return __first ? iterator(nodeToN(__first)) : iterator(); }
133 iterator end() { return iterator(); }
134
136 {
137 return __first ? const_iterator(nodeToN(__first)) : const_iterator();
138 }
139
140 const_iterator end() const { return const_iterator(); }
141
142private:
143
144 static N *nodeToN(QIntrusiveListNode *node)
145 {
147#if defined(Q_CC_CLANG) && Q_CC_CLANG >= 1300
148 QT_WARNING_DISABLE_CLANG("-Wnull-pointer-subtraction")
149#endif
150 return (N *)((char *)node - ((char *)&(((N *)nullptr)->*member) - (char *)nullptr));
152 }
153
154 QIntrusiveListNode *__first = nullptr;
155};
156
158
159#endif // QINTRUSIVELIST_P_H
void remove()
If in a list, remove this node otherwise do nothing.
bool isInList() const
Returns true if this node is in a list, false otherwise.
~QIntrusiveListNode()
Destroy the QIntrusiveListNode.
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.
static O next(O current)
Returns the next object after current, or null if current is the last object.
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.
~QIntrusiveList()
Destroy the list.
iterator_impl< const N * > const_iterator
const N * first() const
Returns the first entry in this list, or null if the list is empty.
iterator begin()
Returns an STL-style interator pointing to the first item in the list.
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
Combined button and popup list for selecting options.
#define QT_WARNING_POP
#define QT_WARNING_PUSH
#define QT_WARNING_DISABLE_CLANG(text)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLfloat n
QSharedPointer< T > other(t)
[5]