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 QFIELDLIST_P_H |
5 | #define QFIELDLIST_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 | #include <QtCore/qtaggedpointer.h> |
20 | |
21 | |
22 | // QForwardFieldList is a super simple linked list that can only prepend |
23 | template<class N, N *N::*nextMember, typename Tag = QtPrivate::TagInfo<N>> |
24 | class QForwardFieldList |
25 | { |
26 | public: |
27 | inline QForwardFieldList(); |
28 | inline N *first() const; |
29 | inline N *takeFirst(); |
30 | |
31 | inline void prepend(N *); |
32 | template <typename OtherTag> |
33 | inline void copyAndClearPrepend(QForwardFieldList<N, nextMember, OtherTag> &); |
34 | |
35 | inline bool isEmpty() const; |
36 | inline bool isOne() const; |
37 | inline bool isMany() const; |
38 | |
39 | static inline N *next(N *v); |
40 | |
41 | inline Tag tag() const; |
42 | inline void setTag(Tag t); |
43 | private: |
44 | QTaggedPointer<N, Tag> _first; |
45 | }; |
46 | |
47 | // QFieldList is a simple linked list, that can append and prepend and also |
48 | // maintains a count |
49 | template<class N, N *N::*nextMember> |
50 | class QFieldList |
51 | { |
52 | public: |
53 | inline QFieldList(); |
54 | inline N *first() const; |
55 | inline N *takeFirst(); |
56 | |
57 | inline void append(N *); |
58 | inline void prepend(N *); |
59 | |
60 | inline bool isEmpty() const; |
61 | inline bool isOne() const; |
62 | inline bool isMany() const; |
63 | inline int count() const; |
64 | |
65 | inline void append(QFieldList<N, nextMember> &); |
66 | inline void prepend(QFieldList<N, nextMember> &); |
67 | inline void insertAfter(N *, QFieldList<N, nextMember> &); |
68 | |
69 | inline void copyAndClear(QFieldList<N, nextMember> &); |
70 | template <typename Tag> |
71 | inline void copyAndClearAppend(QForwardFieldList<N, nextMember, Tag> &); |
72 | template <typename Tag> |
73 | inline void copyAndClearPrepend(QForwardFieldList<N, nextMember, Tag> &); |
74 | |
75 | static inline N *next(N *v); |
76 | |
77 | inline bool flag() const; |
78 | inline void setFlag(); |
79 | inline void clearFlag(); |
80 | inline void setFlagValue(bool); |
81 | private: |
82 | N *_first; |
83 | N *_last; |
84 | quint32 _flag:1; |
85 | quint32 _count:31; |
86 | }; |
87 | |
88 | template<class N, N *N::*nextMember, typename Tag> |
89 | QForwardFieldList<N, nextMember, Tag>::QForwardFieldList() |
90 | { |
91 | } |
92 | |
93 | template<class N, N *N::*nextMember, typename Tag> |
94 | N *QForwardFieldList<N, nextMember, Tag>::first() const |
95 | { |
96 | return _first.data(); |
97 | } |
98 | |
99 | template<class N, N *N::*nextMember, typename Tag> |
100 | N *QForwardFieldList<N, nextMember, Tag>::takeFirst() |
101 | { |
102 | N *value = _first.data(); |
103 | if (value) { |
104 | _first = next(v: value); |
105 | value->*nextMember = nullptr; |
106 | } |
107 | return value; |
108 | } |
109 | |
110 | template<class N, N *N::*nextMember, typename Tag> |
111 | void QForwardFieldList<N, nextMember, Tag>::prepend(N *v) |
112 | { |
113 | Q_ASSERT(v->*nextMember == nullptr); |
114 | v->*nextMember = _first.data(); |
115 | _first = v; |
116 | } |
117 | |
118 | template<class N, N *N::*nextMember, typename Tag> |
119 | template <typename OtherTag> |
120 | void QForwardFieldList<N, nextMember, Tag>::copyAndClearPrepend(QForwardFieldList<N, nextMember, OtherTag> &o) |
121 | { |
122 | _first = nullptr; |
123 | while (N *n = o.takeFirst()) prepend(v: n); |
124 | } |
125 | |
126 | template<class N, N *N::*nextMember, typename Tag> |
127 | bool QForwardFieldList<N, nextMember, Tag>::isEmpty() const |
128 | { |
129 | return _first.isNull(); |
130 | } |
131 | |
132 | template<class N, N *N::*nextMember, typename Tag> |
133 | bool QForwardFieldList<N, nextMember, Tag>::isOne() const |
134 | { |
135 | return _first.data() && _first->*nextMember == 0; |
136 | } |
137 | |
138 | template<class N, N *N::*nextMember, typename Tag> |
139 | bool QForwardFieldList<N, nextMember, Tag>::isMany() const |
140 | { |
141 | return _first.data() && _first->*nextMember != 0; |
142 | } |
143 | |
144 | template<class N, N *N::*nextMember, typename Tag> |
145 | N *QForwardFieldList<N, nextMember, Tag>::next(N *v) |
146 | { |
147 | Q_ASSERT(v); |
148 | return v->*nextMember; |
149 | } |
150 | |
151 | template<class N, N *N::*nextMember, typename Tag> |
152 | Tag QForwardFieldList<N, nextMember, Tag>::tag() const |
153 | { |
154 | return _first.tag(); |
155 | } |
156 | |
157 | template<class N, N *N::*nextMember, typename Tag> |
158 | void QForwardFieldList<N, nextMember, Tag>::setTag(Tag t) |
159 | { |
160 | _first.setTag(t); |
161 | } |
162 | |
163 | template<class N, N *N::*nextMember> |
164 | QFieldList<N, nextMember>::QFieldList() |
165 | : _first(nullptr), _last(nullptr), _flag(0), _count(0) |
166 | { |
167 | } |
168 | |
169 | template<class N, N *N::*nextMember> |
170 | N *QFieldList<N, nextMember>::first() const |
171 | { |
172 | return _first; |
173 | } |
174 | |
175 | template<class N, N *N::*nextMember> |
176 | N *QFieldList<N, nextMember>::takeFirst() |
177 | { |
178 | N *value = _first; |
179 | if (value) { |
180 | _first = next(v: value); |
181 | if (_last == value) { |
182 | Q_ASSERT(_first == nullptr); |
183 | _last = nullptr; |
184 | } |
185 | value->*nextMember = nullptr; |
186 | --_count; |
187 | } |
188 | return value; |
189 | } |
190 | |
191 | template<class N, N *N::*nextMember> |
192 | void QFieldList<N, nextMember>::append(N *v) |
193 | { |
194 | Q_ASSERT(v->*nextMember == nullptr); |
195 | if (isEmpty()) { |
196 | _first = v; |
197 | _last = v; |
198 | } else { |
199 | _last->*nextMember = v; |
200 | _last = v; |
201 | } |
202 | ++_count; |
203 | } |
204 | |
205 | template<class N, N *N::*nextMember> |
206 | void QFieldList<N, nextMember>::prepend(N *v) |
207 | { |
208 | Q_ASSERT(v->*nextMember == nullptr); |
209 | if (isEmpty()) { |
210 | _first = v; |
211 | _last = v; |
212 | } else { |
213 | v->*nextMember = _first; |
214 | _first = v; |
215 | } |
216 | ++_count; |
217 | } |
218 | |
219 | template<class N, N *N::*nextMember> |
220 | bool QFieldList<N, nextMember>::isEmpty() const |
221 | { |
222 | return _count == 0; |
223 | } |
224 | |
225 | template<class N, N *N::*nextMember> |
226 | bool QFieldList<N, nextMember>::isOne() const |
227 | { |
228 | return _count == 1; |
229 | } |
230 | |
231 | template<class N, N *N::*nextMember> |
232 | bool QFieldList<N, nextMember>::isMany() const |
233 | { |
234 | return _count > 1; |
235 | } |
236 | |
237 | template<class N, N *N::*nextMember> |
238 | int QFieldList<N, nextMember>::count() const |
239 | { |
240 | return _count; |
241 | } |
242 | |
243 | template<class N, N *N::*nextMember> |
244 | N *QFieldList<N, nextMember>::next(N *v) |
245 | { |
246 | Q_ASSERT(v); |
247 | return v->*nextMember; |
248 | } |
249 | |
250 | template<class N, N *N::*nextMember> |
251 | void QFieldList<N, nextMember>::append(QFieldList<N, nextMember> &o) |
252 | { |
253 | if (!o.isEmpty()) { |
254 | if (isEmpty()) { |
255 | _first = o._first; |
256 | _last = o._last; |
257 | _count = o._count; |
258 | } else { |
259 | _last->*nextMember = o._first; |
260 | _last = o._last; |
261 | _count += o._count; |
262 | } |
263 | o._first = o._last = 0; o._count = 0; |
264 | } |
265 | } |
266 | |
267 | template<class N, N *N::*nextMember> |
268 | void QFieldList<N, nextMember>::prepend(QFieldList<N, nextMember> &o) |
269 | { |
270 | if (!o.isEmpty()) { |
271 | if (isEmpty()) { |
272 | _first = o._first; |
273 | _last = o._last; |
274 | _count = o._count; |
275 | } else { |
276 | o._last->*nextMember = _first; |
277 | _first = o._first; |
278 | _count += o._count; |
279 | } |
280 | o._first = o._last = 0; o._count = 0; |
281 | } |
282 | } |
283 | |
284 | template<class N, N *N::*nextMember> |
285 | void QFieldList<N, nextMember>::insertAfter(N *after, QFieldList<N, nextMember> &o) |
286 | { |
287 | if (after == 0) { |
288 | prepend(o); |
289 | } else if (after == _last) { |
290 | append(o); |
291 | } else if (!o.isEmpty()) { |
292 | if (isEmpty()) { |
293 | _first = o._first; |
294 | _last = o._last; |
295 | _count = o._count; |
296 | } else { |
297 | o._last->*nextMember = after->*nextMember; |
298 | after->*nextMember = o._first; |
299 | _count += o._count; |
300 | } |
301 | o._first = o._last = 0; o._count = 0; |
302 | } |
303 | } |
304 | |
305 | template<class N, N *N::*nextMember> |
306 | void QFieldList<N, nextMember>::copyAndClear(QFieldList<N, nextMember> &o) |
307 | { |
308 | _first = o._first; |
309 | _last = o._last; |
310 | _count = o._count; |
311 | o._first = o._last = nullptr; |
312 | o._count = 0; |
313 | } |
314 | |
315 | template<class N, N *N::*nextMember> |
316 | template <typename Tag> |
317 | void QFieldList<N, nextMember>::copyAndClearAppend(QForwardFieldList<N, nextMember, Tag> &o) |
318 | { |
319 | _first = 0; |
320 | _last = 0; |
321 | _count = 0; |
322 | while (N *n = o.takeFirst()) append(n); |
323 | } |
324 | |
325 | template<class N, N *N::*nextMember> |
326 | template <typename Tag> |
327 | void QFieldList<N, nextMember>::copyAndClearPrepend(QForwardFieldList<N, nextMember, Tag> &o) |
328 | { |
329 | _first = nullptr; |
330 | _last = nullptr; |
331 | _count = 0; |
332 | while (N *n = o.takeFirst()) prepend(n); |
333 | } |
334 | |
335 | template<class N, N *N::*nextMember> |
336 | bool QFieldList<N, nextMember>::flag() const |
337 | { |
338 | return _flag; |
339 | } |
340 | |
341 | template<class N, N *N::*nextMember> |
342 | void QFieldList<N, nextMember>::setFlag() |
343 | { |
344 | _flag = true; |
345 | } |
346 | |
347 | template<class N, N *N::*nextMember> |
348 | void QFieldList<N, nextMember>::clearFlag() |
349 | { |
350 | _flag = false; |
351 | } |
352 | |
353 | template<class N, N *N::*nextMember> |
354 | void QFieldList<N, nextMember>::setFlagValue(bool v) |
355 | { |
356 | _flag = v; |
357 | } |
358 | |
359 | #endif // QFIELDLIST_P_H |
360 | |