Blender V5.3
DNA_listBase.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#pragma once
14
15#include "MEM_guardedalloc.h"
16
17namespace blender {
18
20struct Link {
21 struct Link *next, *prev;
22};
23
25struct LinkData {
26 struct LinkData *next, *prev;
27 void *data;
28};
29
36struct ListBase {
37 void *first_, *last_;
38};
39
40#ifdef __cplusplus
41
42template<typename T> struct ListBaseTIterator;
43template<typename T> struct ListBaseEnumerateWrapper;
44template<typename T> struct ListBaseMutableWrapper;
45template<typename T> struct ListBaseBackwardWrapper;
46template<typename T> struct ListBaseMutableBackwardWrapper;
47
52template<typename T> struct ListBaseT : public ListBase {
54 bool is_empty() const
55 {
56 return this->first_ == nullptr;
57 }
58
60 bool is_single() const
61 {
62 return this->first_ != nullptr && this->last_ == this->first_;
63 }
64
68 void clear_no_delete()
69 {
70 this->first_ = nullptr;
71 this->last_ = nullptr;
72 }
73
78 void free_no_destruct()
79 {
80 static_assert(std::is_trivially_destructible_v<T>,
81 "Items with a non-trivial destructor have to be freed separately");
82 for (T &item : this->items_mutable()) {
83 MEM_delete(&item);
84 }
85 this->first_ = nullptr;
86 this->last_ = nullptr;
87 }
88
92 int count() const
93 {
94 int count = 0;
95 for ([[maybe_unused]] const T &item : *this) {
96 count++;
97 }
98 return count;
99 }
100
101 /* Some places require getting a mutable first item from a const list. */
102 T *first() const
103 {
104 return static_cast<T *>(this->first_);
105 }
106 T *first()
107 {
108 return static_cast<T *>(this->first_);
109 }
110 T *last() const
111 {
112 return static_cast<T *>(this->last_);
113 }
114 T *last()
115 {
116 return static_cast<T *>(this->last_);
117 }
118 template<typename U> U *first_as() const
119 {
120 return reinterpret_cast<U *>(this->first());
121 }
122 template<typename U> U *first_as()
123 {
124 return reinterpret_cast<U *>(this->first());
125 }
126 template<typename U> U *last_as() const
127 {
128 return reinterpret_cast<U *>(this->last_);
129 }
130 template<typename U> U *last_as()
131 {
132 return reinterpret_cast<U *>(this->last_);
133 }
134
135 /* TODO: Add const and non-const iterators. However this will require some refactoring
136 * as some places rely on being able to get a mutable list element from a const list. */
137 ListBaseTIterator<T> begin() const
138 {
139 return ListBaseTIterator<T>{this->first()};
140 }
141
142 ListBaseTIterator<T> end() const
143 {
144 /* Don't use `this->last` because this iterator has to point to one-past-the-end. */
145 return ListBaseTIterator<T>{nullptr};
146 }
147
152 ListBaseEnumerateWrapper<T> enumerate()
153 {
154 return {this->first_};
155 }
156
157 ListBaseEnumerateWrapper<const T> enumerate() const
158 {
159 return {this->first_};
160 }
161
163 ListBaseMutableWrapper<T> items_mutable()
164 {
165 return {this->first_};
166 }
167
169 ListBaseBackwardWrapper<T> items_reversed()
170 {
171 return {this->last_};
172 }
173
174 ListBaseBackwardWrapper<const T> items_reversed() const
175 {
176 return {this->last_};
177 }
178
180 ListBaseMutableBackwardWrapper<T> items_reversed_mutable()
181 {
182 return {this->last_};
183 }
184
185 /* Cast for opaque types and C style subclasses. */
186 template<typename OtherT> const ListBaseT<OtherT> &cast() const
187 {
188 return *reinterpret_cast<const ListBaseT<OtherT> *>(this);
189 }
190
191 template<typename OtherT> ListBaseT<OtherT> &cast()
192 {
193 return *reinterpret_cast<ListBaseT<OtherT> *>(this);
194 }
195};
196
197#endif
198
199/* 8 byte alignment! */
200
201} // namespace blender
Read Blender memory allocation and freeing.
#define U
ccl_device_inline int4 cast(const float4 a)
Definition math_float4.h:32
#define T
BMesh Iterators.
iter begin(iter)
struct LinkData * prev
struct LinkData * next