Blender V5.3
BLI_listbase_iterator.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "DNA_listBase.h"
12
13#include <iterator>
14
15namespace blender {
16
17struct Link;
18
20template<typename T> struct ListBaseTIterator {
21 public:
22 using iterator_category = std::bidirectional_iterator_tag;
23 using value_type = T;
24 using difference_type = std::ptrdiff_t;
25 using pointer = T *;
26 using reference = T &;
27
28 private:
29 T *data_ = nullptr;
30
31 public:
33
35 {
36 data_ = reinterpret_cast<T *>((reinterpret_cast<const Link *>(data_))->next);
37 return *this;
38 }
39
41 {
42 ListBaseTIterator tmp = *this;
43 ++(*this);
44 return tmp;
45 }
46
48 {
49 data_ = reinterpret_cast<T *>((reinterpret_cast<const Link *>(data_))->prev);
50 return *this;
51 }
52
54 {
55 ListBaseTIterator tmp = *this;
56 --(*this);
57 return tmp;
58 }
59
60 friend bool operator==(const ListBaseTIterator &a, const ListBaseTIterator &b)
61 {
62 return a.data_ == b.data_;
63 }
64
65 friend bool operator!=(const ListBaseTIterator &a, const ListBaseTIterator &b)
66 {
67 return a.data_ != b.data_;
68 }
69
70 T &operator*() const
71 {
72 return *data_;
73 }
74};
75
77template<typename T> struct ListBaseEnumerateIterator {
78 public:
79 using iterator_category = std::forward_iterator_tag;
80 using value_type = std::pair<int, T &>;
81 using difference_type = std::ptrdiff_t;
82 using pointer = void;
84
85 private:
86 T *data_ = nullptr;
87 int index_ = 0;
88
89 public:
90 ListBaseEnumerateIterator(T *data, int index) : data_(data), index_(index) {}
91
93 {
94 data_ = reinterpret_cast<T *>(reinterpret_cast<const Link *>(data_)->next);
95 index_++;
96 return *this;
97 }
98
100 {
101 ListBaseEnumerateIterator tmp = *this;
102 ++(*this);
103 return tmp;
104 }
105
107 {
108 return a.data_ == b.data_;
109 }
110
112 {
113 return a.data_ != b.data_;
114 }
115
117 {
118 return {index_, *data_};
119 }
120};
121
122template<typename T> struct ListBaseEnumerateWrapper {
123 void *first;
124
126 {
127 return ListBaseEnumerateIterator<T>(static_cast<T *>(first), 0);
128 }
129
131 {
132 return ListBaseEnumerateIterator<T>(nullptr, 0);
133 }
134};
135
137template<typename T> struct ListBaseMutableIterator {
138 public:
139 using iterator_category = std::forward_iterator_tag;
140 using value_type = T;
141 using difference_type = std::ptrdiff_t;
142 using pointer = T *;
143 using reference = T &;
144
145 private:
146 T *current_ = nullptr;
147 T *next_ = nullptr;
148
149 public:
151 {
152 if (current_) {
153 next_ = reinterpret_cast<T *>((reinterpret_cast<const Link *>(current_))->next);
154 }
155 }
156
158 {
159 current_ = next_;
160 if (current_) {
161 next_ = reinterpret_cast<T *>((reinterpret_cast<const Link *>(current_))->next);
162 }
163 return *this;
164 }
165
167 {
168 ListBaseMutableIterator tmp = *this;
169 ++(*this);
170 return tmp;
171 }
172
174 {
175 return a.current_ == b.current_;
176 }
177
179 {
180 return a.current_ != b.current_;
181 }
182
183 T &operator*() const
184 {
185 return *current_;
186 }
187};
188
189template<typename T> struct ListBaseMutableWrapper {
190 void *first;
191
193 {
194 return ListBaseMutableIterator<T>(static_cast<T *>(first));
195 }
196
198 {
199 return ListBaseMutableIterator<T>(nullptr);
200 }
201};
202
204template<typename T> struct ListBaseBackwardIterator {
205 public:
206 using iterator_category = std::forward_iterator_tag;
207 using value_type = T;
208 using difference_type = std::ptrdiff_t;
209 using pointer = T *;
210 using reference = T &;
211
212 private:
213 T *data_ = nullptr;
214
215 public:
217
219 {
220 data_ = reinterpret_cast<T *>((reinterpret_cast<const Link *>(data_))->prev);
221 return *this;
222 }
223
225 {
226 ListBaseBackwardIterator tmp = *this;
227 ++(*this);
228 return tmp;
229 }
230
232 {
233 return a.data_ == b.data_;
234 }
235
237 {
238 return a.data_ != b.data_;
239 }
240
241 T &operator*() const
242 {
243 return *data_;
244 }
245};
246
247template<typename T> struct ListBaseBackwardWrapper {
248 void *last;
249
251 {
252 return ListBaseBackwardIterator<T>(static_cast<T *>(last));
253 }
254
256 {
257 return ListBaseBackwardIterator<T>(nullptr);
258 }
259};
260
262template<typename T> struct ListBaseMutableBackwardIterator {
263 public:
264 using iterator_category = std::forward_iterator_tag;
265 using value_type = T;
266 using difference_type = std::ptrdiff_t;
267 using pointer = T *;
268 using reference = T &;
269
270 private:
271 T *current_ = nullptr;
272 T *prev_ = nullptr;
273
274 public:
276 {
277 if (current_) {
278 prev_ = reinterpret_cast<T *>((reinterpret_cast<const Link *>(current_))->prev);
279 }
280 }
281
283 {
284 current_ = prev_;
285 if (current_) {
286 prev_ = reinterpret_cast<T *>((reinterpret_cast<const Link *>(current_))->prev);
287 }
288 return *this;
289 }
290
292 {
294 ++(*this);
295 return tmp;
296 }
297
300 {
301 return a.current_ == b.current_;
302 }
303
306 {
307 return a.current_ != b.current_;
308 }
309
310 T &operator*() const
311 {
312 return *current_;
313 }
314};
315
316template<typename T> struct ListBaseMutableBackwardWrapper {
317 void *last;
318
320 {
321 return ListBaseMutableBackwardIterator<T>(static_cast<T *>(last));
322 }
323
328};
329
330} // namespace blender
These structs are the foundation for all linked lists in the library system.
#define T
BMesh Iterators.
BMesh const char void * data
ListBaseBackwardIterator & operator++()
friend bool operator==(const ListBaseBackwardIterator &a, const ListBaseBackwardIterator &b)
std::forward_iterator_tag iterator_category
ListBaseBackwardIterator operator++(int)
friend bool operator!=(const ListBaseBackwardIterator &a, const ListBaseBackwardIterator &b)
ListBaseBackwardIterator< T > begin() const
ListBaseBackwardIterator< T > end() const
friend bool operator==(const ListBaseEnumerateIterator &a, const ListBaseEnumerateIterator &b)
ListBaseEnumerateIterator & operator++()
std::forward_iterator_tag iterator_category
ListBaseEnumerateIterator operator++(int)
friend bool operator!=(const ListBaseEnumerateIterator &a, const ListBaseEnumerateIterator &b)
ListBaseEnumerateIterator< T > begin() const
ListBaseEnumerateIterator< T > end() const
friend bool operator!=(const ListBaseMutableBackwardIterator &a, const ListBaseMutableBackwardIterator &b)
ListBaseMutableBackwardIterator & operator++()
friend bool operator==(const ListBaseMutableBackwardIterator &a, const ListBaseMutableBackwardIterator &b)
ListBaseMutableBackwardIterator operator++(int)
ListBaseMutableBackwardIterator< T > end() const
ListBaseMutableBackwardIterator< T > begin() const
friend bool operator==(const ListBaseMutableIterator &a, const ListBaseMutableIterator &b)
friend bool operator!=(const ListBaseMutableIterator &a, const ListBaseMutableIterator &b)
ListBaseMutableIterator & operator++()
ListBaseMutableIterator operator++(int)
std::forward_iterator_tag iterator_category
ListBaseMutableIterator< T > begin() const
ListBaseMutableIterator< T > end() const
std::bidirectional_iterator_tag iterator_category
friend bool operator!=(const ListBaseTIterator &a, const ListBaseTIterator &b)
friend bool operator==(const ListBaseTIterator &a, const ListBaseTIterator &b)
ListBaseTIterator operator--(int)
ListBaseTIterator operator++(int)