Blender V5.0
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
5#pragma once
6
7#include <variant>
8
9#include "BLI_function_ref.hh"
12#include "BLI_string_ref.hh"
13#include "BLI_vector_set.hh"
14
15#include "DNA_attribute_types.h"
16
17struct BlendDataReader;
18struct BlendWriter;
20namespace blender {
21class GPointer;
22class CPPType;
23class ResourceScope;
24} // namespace blender
25
26namespace blender::bke {
27
28enum class AttrDomain : int8_t;
29enum class AttrType : int16_t;
30enum class AttrStorageType : int8_t;
31
33class Attribute {
34 public:
39 struct ArrayData {
40 /* NOTE: Since the shared data pointed to by `sharing_info` knows how to free itself, it often
41 * stores the size and type itself. It may be possible to make use of that fact to avoid
42 * storing it here, or even vice versa. */
43 void *data;
47 static ArrayData from_value(const GPointer &value, int64_t domain_size);
48 static ArrayData from_default_value(const CPPType &type, int64_t domain_size);
49 static ArrayData from_uninitialized(const CPPType &type, int64_t domain_size);
50 static ArrayData from_constructed(const CPPType &type, int64_t domain_size);
51 };
52
53 struct SingleData {
54 /* NOTE: For simplicity and to avoid a bit of redundancy, the domain size isn't stored here.
55 * It's not necessary to manage a single value. */
56 void *value;
58 static SingleData from_value(const GPointer &value);
59 static SingleData from_default_value(const CPPType &type);
60 };
61 using DataVariant = std::variant<ArrayData, SingleData>;
63
64 private:
69 std::string name_;
70 AttrDomain domain_;
71 AttrType type_;
72
73 DataVariant data_;
74
75 public:
82 StringRefNull name() const;
83
85 AttrDomain domain() const;
86
91 AttrType data_type() const;
92
98
103 const DataVariant &data() const;
104
110
113};
114
116 friend AttributeStorage;
117 struct AttributeNameGetter {
118 StringRef operator()(const std::unique_ptr<Attribute> &value) const
119 {
120 return value->name();
121 }
122 };
128 CustomIDVectorSet<std::unique_ptr<Attribute>, AttributeNameGetter> attributes;
129};
130
132 public:
139
144 void foreach(FunctionRef<void(Attribute &)> fn);
145 void foreach(FunctionRef<void(const Attribute &)> fn) const;
147 void foreach_with_stop(FunctionRef<bool(const Attribute &)> fn) const;
148
150 int count() const;
151
153 Attribute &at_index(int index);
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
176 Attribute &add(std::string name,
177 bke::AttrDomain domain,
178 bke::AttrType data_type,
180
182 std::string unique_name_calc(StringRef name) const;
183
185 void rename(StringRef old_name, std::string new_name);
186
191 void resize(AttrDomain domain, int64_t new_size);
192
197 void blend_read(BlendDataReader &reader);
207
211 void blend_write(BlendWriter &writer, const BlendWriteData &write_data);
212
217
218 void count_memory(MemoryCounter &memory) const;
219};
220
222static_assert(sizeof(AttributeStorage) == sizeof(::AttributeStorage));
223
225{
226 return name_;
227}
228
230{
231 return domain_;
232}
233
235{
236 return type_;
237}
238
240{
241 return data_;
242}
243
245{
246 data_ = std::move(data);
247}
248
249} // namespace blender::bke
250
251inline blender::bke::AttributeStorage &AttributeStorage::wrap()
252{
253 return *reinterpret_cast<blender::bke::AttributeStorage *>(this);
254}
255inline const blender::bke::AttributeStorage &AttributeStorage::wrap() const
256{
257 return *reinterpret_cast<const blender::bke::AttributeStorage *>(this);
258}
BMesh const char void * data
long long int int64_t
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition btTransform.h:90
void rename(StringRef old_name, std::string new_name)
Attribute & add(std::string name, bke::AttrDomain domain, bke::AttrType data_type, Attribute::DataVariant data)
void count_memory(MemoryCounter &memory) const
Attribute * lookup(StringRef name)
void blend_read(BlendDataReader &reader)
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 resize(AttrDomain domain, int64_t new_size)
int index_of(StringRef name) const
void foreach_with_stop(FunctionRef< bool(Attribute &)> fn)
AttributeStorage & operator=(const AttributeStorage &other)
AttrStorageType storage_type() const
const DataVariant & data() const
std::variant< ArrayData, SingleData > DataVariant
void assign_data(DataVariant &&data)
VectorSet< T, InlineBufferCapacity, DefaultProbingStrategy, CustomIDHash< T, GetIDFn >, CustomIDEqual< T, GetIDFn > > CustomIDVectorSet
const char * name
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)