Blender V5.3
BKE_attribute_storage.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
14#include "BLI_map.hh"
17#include "BLI_set.hh"
18#include "BLI_string_ref.hh"
19#include "BLI_vector_set.hh"
20
22
23#include "DNA_attribute_types.h"
24
25namespace blender {
26
27struct Attribute;
28struct BlendDataReader;
29struct BlendWriter;
31class GPointer;
32class CPPType;
33class ResourceScope;
34
35namespace bke {
36
37enum class AttrDomain : int8_t;
38enum class AttrType : int16_t;
39enum class AttrStorageType : int8_t;
40
42class Attribute {
43 public:
48 struct ArrayData {
49 /* NOTE: Since the shared data pointed to by `sharing_info` knows how to free itself, it often
50 * stores the size and type itself. It may be possible to make use of that fact to avoid
51 * storing it here, or even vice versa. */
52 void *data;
56 static ArrayData from_value(const GPointer &value, int64_t domain_size);
57 static ArrayData from_default_value(const CPPType &type, int64_t domain_size);
58 static ArrayData from_uninitialized(const CPPType &type, int64_t domain_size);
59 static ArrayData from_constructed(const CPPType &type, int64_t domain_size);
60 };
61
62 struct SingleData {
63 /* NOTE: For simplicity and to avoid a bit of redundancy, the domain size isn't stored here.
64 * It's not necessary to manage a single value. */
65 void *value;
67 static SingleData from_value(const GPointer &value);
68 static SingleData from_default_value(const CPPType &type);
69 };
70 using DataVariant = std::variant<ArrayData, SingleData>;
72
73 private:
78 std::string name_;
79 AttrDomain domain_;
80 AttrType type_;
81
82 DataVariant data_;
83
84 public:
91 StringRefNull name() const;
92
94 AttrDomain domain() const;
95
100 AttrType data_type() const;
101
107
112 const DataVariant &data() const;
113
119
122};
123
125 friend AttributeStorage;
126 struct AttributeNameGetter {
127 StringRef operator()(const std::unique_ptr<Attribute> &value) const
128 {
129 return value->name();
130 }
131 };
137 CustomIDVectorSet<std::unique_ptr<Attribute>, AttributeNameGetter> attributes;
138};
139
141 public:
148
150 int count() const;
151
154 const Attribute &at_index(int index) const;
155
157 int index_of(StringRef name) const;
158
164 const Attribute *lookup(StringRef name) const;
165
170 bool remove(StringRef name);
171 bool remove(const Set<StringRef> &names);
172 bool remove(const Set<const Attribute *> &attributes);
173
178 Attribute &add(std::string name,
179 bke::AttrDomain domain,
180 bke::AttrType data_type,
182
184 std::string unique_name_calc(StringRef name) const;
185
187 void rename(StringRef old_name, std::string new_name);
188 void rename(Attribute &attr, std::string new_name);
189 void rename(const Map<Attribute *, StringRef> &renames);
190
195 void resize(AttrDomain domain, int64_t new_size);
196
201 void blend_read(BlendDataReader &reader);
212
216 void blend_write(BlendWriter &writer, const BlendWriteData &write_data);
217
222
223 void count_memory(MemoryCounter &memory) const;
224
226 private:
227 using It = const std::unique_ptr<Attribute> *;
228 It it_;
229
230 public:
232 using pointer = const Attribute *;
233 using reference = const Attribute &;
234
235 explicit Iterator(It it) : it_(it) {}
236
237 const Attribute &operator*() const
238 {
239 return **it_;
240 }
241
242 const It &iter_prop() const
243 {
244 return it_;
245 }
246 };
247
248 class MutableIterator : public iterator::RandomAccessIteratorMixin<MutableIterator> {
249 private:
250 using It = std::unique_ptr<Attribute> *;
251 It it_;
252
253 public:
257
258 explicit MutableIterator(It it) : it_(it) {}
259
261 {
262 return **it_;
263 }
264
265 const It &iter_prop() const
266 {
267 return it_;
268 }
269 };
270
271 Iterator begin() const;
272 Iterator end() const;
273
274 MutableIterator begin();
275 MutableIterator end();
276};
277
279static_assert(sizeof(AttributeStorage) == sizeof(AttributeStorage));
280
282{
283 return name_;
284}
285
287{
288 return domain_;
289}
290
292{
293 return type_;
294}
295
297{
298 return data_;
299}
300
302{
303 static_assert(std::is_same_v<
304 std::variant_alternative_t<int(AttrStorageType::Array), Attribute::DataVariant>,
306 static_assert(std::is_same_v<
307 std::variant_alternative_t<int(AttrStorageType::Single), Attribute::DataVariant>,
309 return AttrStorageType(data_.index());
310}
311
313{
314 data_ = std::move(data);
315}
316
318{
319 return Iterator(this->runtime->attributes.begin());
320}
321
323{
324 return Iterator(this->runtime->attributes.end());
325}
326
328{
329 /* Removing const is fine as long as the name of the attribute is not changed while iterating
330 * over the attributes. Renaming goes through #AttributeStorage::rename anyway. */
331 return MutableIterator(
332 const_cast<std::unique_ptr<Attribute> *>(this->runtime->attributes.begin()));
333}
334
336{
337 return MutableIterator(
338 const_cast<std::unique_ptr<Attribute> *>(this->runtime->attributes.end()));
339}
340
341} // namespace bke
342
343inline bke::AttributeStorage &AttributeStorage::wrap()
344{
345 return *reinterpret_cast<bke::AttributeStorage *>(this);
346}
347inline const bke::AttributeStorage &AttributeStorage::wrap() const
348{
349 return *reinterpret_cast<const bke::AttributeStorage *>(this);
350}
351
352} // namespace blender
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition btTransform.h:90
void blend_read(BlendDataReader &reader)
void rename(StringRef old_name, std::string new_name)
AttributeStorage & operator=(const AttributeStorage &other)
void resize(AttrDomain domain, int64_t new_size)
Attribute * lookup(StringRef name)
void blend_write(BlendWriter &writer, const BlendWriteData &write_data)
std::string unique_name_calc(StringRef name) const
void foreach_working_space_color(const IDTypeForeachColorFunctionCallback &fn)
void count_memory(MemoryCounter &memory) const
Attribute & add(std::string name, bke::AttrDomain domain, bke::AttrType data_type, Attribute::DataVariant data)
int index_of(StringRef name) const
AttrStorageType storage_type() const
const DataVariant & data() const
std::variant< ArrayData, SingleData > DataVariant
void assign_data(DataVariant &&data)
BMesh Iterators.
__int64 int64_t
Definition BKE_text.h:90
BMesh const char void * data
VectorSet< T, InlineBufferCapacity, DefaultProbingStrategy, CustomIDHash< T, GetIDFn >, CustomIDEqual< T, GetIDFn > > CustomIDVectorSet
iter begin(iter)
signed char int8_t
Definition BKE_text.h:76
signed short int16_t
Definition BKE_text.h:77
bke::AttributeStorageRuntime * runtime
BlendWriteData(BlendWriter *writer, ResourceScope &scope)
static ArrayData from_uninitialized(const CPPType &type, int64_t domain_size)
static ArrayData from_default_value(const CPPType &type, int64_t domain_size)
static ArrayData from_value(const GPointer &value, int64_t domain_size)
static ArrayData from_constructed(const CPPType &type, int64_t domain_size)
static SingleData from_value(const GPointer &value)
static SingleData from_default_value(const CPPType &type)