Blender V4.5
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;
19namespace blender {
20class ResourceScope;
21}
22
23namespace blender::bke {
24
25enum class AttrDomain : int8_t;
26enum class AttrType : int16_t;
27enum class AttrStorageType : int8_t;
28
30class Attribute {
31 public:
36 struct ArrayData {
37 /* NOTE: Since the shared data pointed to by `sharing_info` knows how to free itself, it often
38 * stores the size and type itself. It may be possible to make use of that fact to avoid
39 * storing it here, or even vice versa. */
40 void *data;
41 /* The number of elements in the array. */
44 };
45
46 struct SingleData {
47 /* NOTE: For simplicity and to avoid a bit of redundancy, the domain size isn't stored here.
48 * It's not necessary to manage a single value. */
49 void *value;
51 };
52 using DataVariant = std::variant<ArrayData, SingleData>;
54
55 private:
60 std::string name_;
61 AttrDomain domain_;
62 AttrType type_;
63
64 DataVariant data_;
65
66 public:
73 StringRefNull name() const;
74
76 AttrDomain domain() const;
77
82 AttrType data_type() const;
83
89
94 const DataVariant &data() const;
95
103};
104
106 friend AttributeStorage;
107 struct AttributeNameGetter {
108 StringRef operator()(const std::unique_ptr<Attribute> &value) const
109 {
110 return value->name();
111 }
112 };
118 CustomIDVectorSet<std::unique_ptr<Attribute>, AttributeNameGetter> attributes;
119};
120
122 public:
129
134 void foreach(FunctionRef<void(Attribute &)> fn);
135 void foreach(FunctionRef<void(const Attribute &)> fn) const;
136
142 const Attribute *lookup(StringRef name) const;
143
148 bool remove(StringRef name);
149
154 Attribute &add(std::string name,
155 bke::AttrDomain domain,
156 bke::AttrType data_type,
158
160 std::string unique_name_calc(StringRef name);
161
166 void blend_read(BlendDataReader &reader);
176
180 void blend_write(BlendWriter &writer, const BlendWriteData &write_data);
181
182 void count_memory(MemoryCounter &memory) const;
183};
184
186static_assert(sizeof(AttributeStorage) == sizeof(::AttributeStorage));
187
189{
190 return name_;
191}
192
194{
195 return domain_;
196}
197
199{
200 return type_;
201}
202
204{
205 return data_;
206}
207
208} // namespace blender::bke
209
210inline blender::bke::AttributeStorage &AttributeStorage::wrap()
211{
212 return *reinterpret_cast<blender::bke::AttributeStorage *>(this);
213}
214inline const blender::bke::AttributeStorage &AttributeStorage::wrap() const
215{
216 return *reinterpret_cast<const blender::bke::AttributeStorage *>(this);
217}
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
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)
std::string unique_name_calc(StringRef name)
void blend_write(BlendWriter &writer, const BlendWriteData &write_data)
AttributeStorage & operator=(const AttributeStorage &other)
AttrStorageType storage_type() const
const DataVariant & data() const
std::variant< ArrayData, SingleData > DataVariant
VectorSet< T, InlineBufferCapacity, DefaultProbingStrategy, CustomIDHash< T, GetIDFn >, CustomIDEqual< T, GetIDFn > > CustomIDVectorSet