Blender V5.3
NOD_geometry_nodes_list.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 <variant>
12
17
19
20namespace blender::nodes {
21
23 public:
24 class ArrayData {
25 public:
30 const void *data;
32 static ArrayData ForValue(const GPointer &value, int64_t size);
33 static ArrayData ForDefaultValue(const CPPType &type, int64_t size);
34 static ArrayData ForConstructed(const CPPType &type, int64_t size);
35 static ArrayData ForUninitialized(const CPPType &type, int64_t size);
36
37 void count_memory(MemoryCounter &memory, const CPPType &type, const int64_t size) const;
38
40 };
41
42 class SingleData {
43 public:
48 const void *value;
50 static SingleData ForValue(const GPointer &value);
51 static SingleData ForDefaultValue(const CPPType &type);
52
53 void count_memory(MemoryCounter &memory, const CPPType &type) const;
54
56 };
57
58 using DataVariant = std::variant<ArrayData, SingleData>;
59
60 private:
61 const CPPType &cpp_type_;
62 DataVariant data_;
63 int64_t size_ = 0;
64
65 public:
70 GList() = delete;
71 GList(const CPPType &type) : GList(type, DataVariant{}, 0) {}
72 GList(const CPPType &type, DataVariant data, const int64_t size);
73 static GListPtr create(const CPPType &type, DataVariant data, const int64_t size);
74 template<typename ContainerT> static GListPtr from_container(ContainerT &&container);
77
79 const DataVariant &data() const;
80 const CPPType &cpp_type() const;
81 int64_t size() const;
82
87 std::variant<GSpan, GPointer> values() const;
88 std::variant<GMutableSpan, GMutablePointer> values_for_write();
89
90 void delete_self() override;
91 GListPtr copy() const;
92
94 GVArray varray() const;
95
96 void count_memory(MemoryCounter &memory) const;
97
99 bool owns_direct_data() const;
100
105 template<typename T> const List<T> &typed() const;
106 template<typename T> List<T> &typed();
107};
108
109template<typename T> class List {
110 public:
111 using base_type = T;
112 using generic_type = GList;
113 using DataVariant = std::variant<GList::ArrayData, GList::SingleData>;
114
115 private:
119 GList list_;
120
121 friend GList;
122
123 public:
124 List();
125
126 template<typename ContainerT>
127 requires std::is_same_v<typename ContainerT::value_type, T>
128 static ListPtr<T> from_container(ContainerT &&container);
129
131 operator const GList &() const;
132
133 DataVariant &data();
134 const DataVariant &data() const;
135 int64_t size() const;
136
138 VArray<T> varray() const;
139
140 std::variant<Span<T>, const T *> values() const;
141 std::variant<MutableSpan<T>, T *> values_for_write();
142
143 template<typename Fn> void foreach(Fn &&fn) const;
144 template<typename Fn> void foreach_for_write(Fn &&fn);
145};
146
147class GListPtr {
148 private:
150
151 public:
152 GListPtr() = default;
153 explicit GListPtr(const GList *data) : data_(data) {}
154 explicit GListPtr(const CPPType &type) : GListPtr(MEM_new<GList>(__func__, type)) {}
155
156 operator bool() const;
157 const GList *operator->() const;
158 const GList &operator*() const;
159
160 const GList *get() const;
162
163 template<typename T> const ListPtr<T> &typed() const;
164 template<typename T> ListPtr<T> &typed();
165};
166
167template<typename T> class ListPtr {
168 public:
169 using base_type = T;
171
172 private:
173 GListPtr data_;
174
175 public:
176 operator bool() const;
177 operator const GListPtr &() const;
178 const List<T> *operator->() const;
179 const List<T> &operator*() const;
180
181 const List<T> &get() const;
183};
184
185template<typename T> constexpr bool is_ListPtr_v = false;
186template<typename T> constexpr bool is_ListPtr_v<ListPtr<T>> = true;
187
188template<typename ContainerT> inline GListPtr GList::from_container(ContainerT &&container)
189{
190 using T = typename std::decay_t<ContainerT>::value_type;
191 static_assert(std::is_convertible_v<ContainerT, MutableSpan<T>>);
192 auto *sharable_data = new ImplicitSharedValue<std::decay_t<ContainerT>>(
193 std::forward<ContainerT>(container));
194 ArrayData array_data;
195 array_data.data = sharable_data->data.data();
196 array_data.sharing_info = ImplicitSharingPtr<>(sharable_data);
197 return GList::create(CPPType::get<T>(), std::move(array_data), sharable_data->data.size());
198}
199
200template<typename T>
201template<typename ContainerT>
202 requires std::is_same_v<typename ContainerT::value_type, T>
203inline ListPtr<T> List<T>::from_container(ContainerT &&container)
204{
205 return GList::from_container(std::forward<ContainerT>(container)).template typed<T>();
206}
207
209{
210 return data_;
211}
212
213inline const GList::DataVariant &GList::data() const
214{
215 return data_;
216}
217
218inline const CPPType &GList::cpp_type() const
219{
220 return cpp_type_;
221}
222
223inline int64_t GList::size() const
224{
225 return size_;
226}
227
228template<typename T> inline const List<T> &GList::typed() const
229{
230 static_assert(sizeof(GList) == sizeof(List<T>));
231 BLI_assert(this->cpp_type().is<T>());
232 return reinterpret_cast<const List<T> &>(*this);
233}
234
235template<typename T> inline List<T> &GList::typed()
236{
237 static_assert(sizeof(GList) == sizeof(List<T>));
238 BLI_assert(this->cpp_type().is<T>());
239 return reinterpret_cast<List<T> &>(*this);
240}
241
242template<typename T> inline List<T>::List() : list_(CPPType::get<T>()) {}
243
244template<typename T> inline List<T>::operator const GList &() const
245{
246 return list_;
247}
248
249template<typename T> inline GList::DataVariant &List<T>::data()
250{
251 return list_.data();
252}
253
254template<typename T> inline const GList::DataVariant &List<T>::data() const
255{
256 return list_.data();
257}
258
259template<typename T> inline int64_t List<T>::size() const
260{
261 return list_.size();
262}
263
264template<typename T> inline VArray<T> List<T>::varray() const
265{
266 return list_.varray().template typed<T>();
267}
268
269template<typename T> inline std::variant<Span<T>, const T *> List<T>::values() const
270{
271 const std::variant<GSpan, GPointer> values = list_.values();
272 if (const auto *span_values = std::get_if<GSpan>(&values)) {
273 return span_values->typed<T>();
274 }
275 if (const auto *single_value = std::get_if<GPointer>(&values)) {
276 return single_value->get<T>();
277 }
279 return {};
280}
281
282template<typename T> inline std::variant<MutableSpan<T>, T *> List<T>::values_for_write()
283{
284 const std::variant<GMutableSpan, GMutablePointer> values = list_.values_for_write();
285 if (const auto *span_values = std::get_if<GMutableSpan>(&values)) {
286 return span_values->typed<T>();
287 }
288 if (const auto *single_value = std::get_if<GMutablePointer>(&values)) {
289 return single_value->get<T>();
290 }
292 return {};
293}
294
295template<typename T> template<typename Fn> inline void List<T>::foreach(Fn &&fn) const
296{
297 if (list_.size() == 0) {
298 return;
299 }
300 const std::variant<Span<T>, const T *> values = this->values();
301 if (const auto *span_values = std::get_if<Span<T>>(&values)) {
302 for (const T &value : *span_values) {
303 fn(value);
304 }
305 }
306 else if (const auto *single_value = std::get_if<const T *>(&values)) {
307 fn(**single_value);
308 }
309}
310
311template<typename T> template<typename Fn> inline void List<T>::foreach_for_write(Fn &&fn)
312{
313 if (list_.size() == 0) {
314 return;
315 }
316 const std::variant<MutableSpan<T>, T *> values = this->values_for_write();
317 if (auto *span_values = std::get_if<MutableSpan<T>>(&values)) {
318 for (T &value : *span_values) {
319 fn(value);
320 }
321 }
322 else if (auto *single_value = std::get_if<T *>(&values)) {
323 fn(**single_value);
324 }
325}
326
327inline GListPtr::operator bool() const
328{
329 return data_;
330}
331
332inline const GList *GListPtr::operator->() const
333{
334 return data_.get();
335}
336
337inline const GList &GListPtr::operator*() const
338{
339 return *data_;
340}
341
342inline const GList *GListPtr::get() const
343{
344 return data_.get();
345}
346
348{
349 BLI_assert(data_);
350 if (!data_->is_mutable()) {
351 *this = data_->copy();
352 }
353 BLI_assert(data_->is_mutable());
354 data_->tag_ensured_mutable();
355 return const_cast<GList &>(*data_);
356}
357
358template<typename T> inline const ListPtr<T> &GListPtr::typed() const
359{
360 static_assert(sizeof(GList) == sizeof(List<T>));
361 BLI_assert(!data_ || data_->cpp_type().is<T>());
362 return reinterpret_cast<const ListPtr<T> &>(*this);
363}
364
365template<typename T> inline ListPtr<T> &GListPtr::typed()
366{
367 static_assert(sizeof(GList) == sizeof(List<T>));
368 BLI_assert(!data_ || data_->cpp_type().is<T>());
369 return reinterpret_cast<ListPtr<T> &>(*this);
370}
371
372template<typename T> inline ListPtr<T>::operator bool() const
373{
374 return data_;
375}
376
377template<typename T> inline ListPtr<T>::operator const GListPtr &() const
378{
379 return data_;
380}
381
382template<typename T> inline const List<T> *ListPtr<T>::operator->() const
383{
384 if (!data_) {
385 return nullptr;
386 }
387 return &data_->typed<T>();
388}
389
390template<typename T> inline const List<T> &ListPtr<T>::operator*() const
391{
392 return data_->typed<T>();
393}
394
395template<typename T> inline const List<T> &ListPtr<T>::get() const
396{
397 return data_->typed<T>();
398}
399
400template<typename T> inline List<T> &ListPtr<T>::get_for_write()
401{
402 return data_.get_for_write().template typed<T>();
403}
404
405} // namespace blender::nodes
#define BLI_assert_unreachable()
static const CPPType & get()
const ListPtr< T > & typed() const
static ArrayData ForConstructed(const CPPType &type, int64_t size)
static ArrayData ForUninitialized(const CPPType &type, int64_t size)
static ArrayData ForDefaultValue(const CPPType &type, int64_t size)
GMutableSpan span_for_write(const CPPType &type, int64_t size)
void count_memory(MemoryCounter &memory, const CPPType &type, const int64_t size) const
static ArrayData ForValue(const GPointer &value, int64_t size)
static SingleData ForValue(const GPointer &value)
void count_memory(MemoryCounter &memory, const CPPType &type) const
static SingleData ForDefaultValue(const CPPType &type)
GMutablePointer value_for_write(const CPPType &type)
const CPPType & cpp_type() const
const List< T > & typed() const
static GListPtr from_garray(GArray<> array)
static GListPtr from_container(ContainerT &&container)
void count_memory(MemoryCounter &memory) const
std::variant< ArrayData, SingleData > DataVariant
static GListPtr from_single(GPointer value, int64_t size)
std::variant< GSpan, GPointer > values() const
static GListPtr create(const CPPType &type, DataVariant data, const int64_t size)
std::variant< GMutableSpan, GMutablePointer > values_for_write()
const List< T > * operator->() const
const List< T > & operator*() const
const List< T > & get() const
std::variant< Span< T >, const T * > values() const
static ListPtr< T > from_container(ContainerT &&container)
std::variant< MutableSpan< T >, T * > values_for_write()
std::variant< GList::ArrayData, GList::SingleData > DataVariant
#define T
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
BMesh const char void * data
MatBase< T, B_NumCol, A_NumRow > operator*(const MatBase< T, A_NumCol, A_NumRow > &a, const MatBase< T, B_NumCol, B_NumRow > &b)