Blender V5.3
BLI_generic_array.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
17
18#include "BLI_allocator.hh"
19#include "BLI_cpp_type.hh"
20#include "BLI_generic_span.hh"
21
22namespace blender {
23
24template<
29 typename Allocator = GuardedAllocator>
30class GArray {
31 protected:
36 const CPPType *type_ = nullptr;
37 void *data_ = nullptr;
39
41
42 public:
48 GArray(Allocator allocator = {}) noexcept : allocator_(allocator) {}
49
50 GArray(NoExceptConstructor, Allocator allocator = {}) noexcept : GArray(allocator) {}
51
61
63 const int64_t size,
64 NoInitialization /*not_init_tag*/,
65 Allocator allocator = {})
67 {
68 BLI_assert(size >= 0);
69 size_ = size;
70 data_ = this->allocate(size_);
71 }
72
76 GArray(const CPPType &type, Allocator allocator = {}) : GArray(allocator)
77 {
78 type_ = &type;
79 }
80
85 GArray(const CPPType &type, void *buffer, int64_t size, Allocator allocator = {})
87 {
88 BLI_assert(size >= 0);
89 BLI_assert(buffer != nullptr || size == 0);
91
92 data_ = buffer;
93 size_ = size;
94 }
95
99 GArray(const GSpan span, Allocator allocator = {}) : GArray(span.type(), span.size(), allocator)
100 {
101 /* Use copy assign rather than construct since the memory is already initialized. */
103 }
104
108 GArray(const GArray &other) : GArray(other.as_span(), other.allocator()) {}
109
113 GArray(GArray &&other)
114 : type_(other.type_), data_(other.data_), size_(other.size_), allocator_(other.allocator_)
115 {
116 other.data_ = nullptr;
117 other.size_ = 0;
118 }
119
121 {
122 if (data_ != nullptr) {
124 this->deallocate(data_);
125 }
126 }
127
128 GArray &operator=(const GArray &other)
129 {
130 return copy_assign_container(*this, other);
131 }
132
134 {
135 return move_assign_container(*this, std::move(other));
136 }
137
138 const CPPType &type() const
139 {
140 BLI_assert(type_ != nullptr);
141 return *type_;
142 }
143
144 bool is_empty() const
145 {
146 return size_ == 0;
147 }
148
152 int64_t size() const
153 {
154 return size_;
155 }
156
160 const void *data() const
161 {
162 return data_;
163 }
164 void *data()
165 {
166 return data_;
167 }
168
169 const void *operator[](int64_t index) const
170 {
172 return POINTER_OFFSET(data_, type_->size * index);
173 }
174
176 {
178 return POINTER_OFFSET(data_, type_->size * index);
179 }
180
181 operator GSpan() const
182 {
183 BLI_assert(size_ == 0 || type_ != nullptr);
184 return GSpan(type_, data_, size_);
185 }
186
187 operator GMutableSpan()
188 {
189 BLI_assert(size_ == 0 || type_ != nullptr);
190 return GMutableSpan(type_, data_, size_);
191 }
192
194 {
195 return *this;
196 }
197
199 {
200 return *this;
201 }
202
206 Allocator &allocator()
207 {
208 return allocator_;
209 }
210 const Allocator &allocator() const
211 {
212 return allocator_;
213 }
214
219 void reinitialize(const int64_t new_size)
220 {
221 BLI_assert(new_size >= 0);
222 int64_t old_size = size_;
223
225 size_ = 0;
226
227 if (new_size <= old_size) {
228 type_->default_construct_n(data_, new_size);
229 }
230 else {
231 void *new_data = this->allocate(new_size);
232 try {
233 type_->default_construct_n(new_data, new_size);
234 }
235 catch (...) {
236 this->deallocate(new_data);
237 throw;
238 }
239 if (this->data_) {
240 this->deallocate(data_);
241 }
242 data_ = new_data;
243 }
244
245 size_ = new_size;
246 }
247
248 private:
249 void *allocate(int64_t size)
250 {
251 const int64_t item_size = type_->size;
252 const int64_t alignment = type_->alignment;
253 return allocator_.allocate(size_t(size) * item_size, alignment, AT);
254 }
255
256 void deallocate(void *ptr)
257 {
258 allocator_.deallocate(ptr);
259 }
260};
261
262} // namespace blender
#define POINTER_OFFSET(v, ofs)
#define AT
#define BLI_NO_UNIQUE_ADDRESS
void default_construct_n(void *ptr, int64_t n) const
void destruct_n(void *ptr, int64_t n) const
void copy_assign_n(const void *src, void *dst, int64_t n) const
bool pointer_has_valid_alignment(const void *ptr) const
Allocator & allocator()
GMutableSpan as_mutable_span()
void * operator[](int64_t index)
GArray & operator=(const GArray &other)
const CPPType & type() const
BLI_NO_UNIQUE_ADDRESS Allocator allocator_
const void * operator[](int64_t index) const
int64_t size() const
GArray(NoExceptConstructor, Allocator allocator={}) noexcept
GArray & operator=(GArray &&other)
void reinitialize(const int64_t new_size)
GArray(const GArray &other)
GArray(Allocator allocator={}) noexcept
GSpan as_span() const
GArray(GArray &&other)
GArray(const CPPType &type, int64_t size, Allocator allocator={})
GArray(const GSpan span, Allocator allocator={})
GArray(const CPPType &type, void *buffer, int64_t size, Allocator allocator={})
const void * data() const
const CPPType * type_
const Allocator & allocator() const
GArray(const CPPType &type, const int64_t size, NoInitialization, Allocator allocator={})
GArray(const CPPType &type, Allocator allocator={})
GSpan()=default
const void * data() const
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
Container & copy_assign_container(Container &dst, const Container &src)
Container & move_assign_container(Container &dst, Container &&src) noexcept(std::is_nothrow_move_constructible_v< Container >)