27 #include <type_traits>
29 VTK_ABI_NAMESPACE_BEGIN
30 template <
class ScalarTypeT>
124 void operator=(
const vtkBuffer&) =
delete;
127 template <
class ScalarT>
133 template <
class ScalarT>
141 template <
typename ScalarT>
144 if (this->Pointer != array)
146 if (this->DeleteFunction)
148 if (this->DeleteFunction != free)
150 this->DeleteFunction(this->Pointer);
152 else if constexpr (std::is_trivially_destructible_v<ScalarT>)
158 vtkErrorMacro(
"SetBuffer is not supported for non-trivially destructible types with free.");
162 this->Pointer = array;
167 template <
typename ScalarT>
170 this->MallocFunction = mallocFunction;
173 template <
typename ScalarT>
176 this->ReallocFunction = reallocFunction;
180 template <
typename ScalarT>
185 this->DeleteFunction =
nullptr;
189 this->DeleteFunction = deleteFunction;
194 template <
typename ScalarT>
198 this->SetBuffer(
nullptr, 0);
202 if (this->MallocFunction && this->MallocFunction != malloc)
206 else if constexpr (std::is_trivially_constructible_v<ScalarType>)
212 vtkErrorMacro(
"Allocate is not supported for non-trivially constructible types with malloc.");
217 this->SetBuffer(newArray, size);
218 if (!this->MallocFunction)
220 this->DeleteFunction = free;
230 template <
typename ScalarT>
233 if (!this->Pointer || newsize == 0)
235 return this->Allocate(newsize);
237 if (this->DeleteFunction != free)
240 if (this->MallocFunction && this->MallocFunction != malloc)
244 else if constexpr (std::is_trivially_constructible_v<ScalarType>)
251 "Reallocate is not supported for non-trivially constructible types with malloc.");
260 std::copy(this->Pointer, this->Pointer + (std::min)(this->Size, newsize), newArray);
262 this->SetBuffer(newArray, newsize);
268 if (!this->MallocFunction || this->MallocFunction == malloc)
270 this->DeleteFunction = free;
277 if (this->ReallocFunction && this->ReallocFunction != realloc)
280 this->ReallocFunction(this->Pointer, newsize *
sizeof(
ScalarType)));
282 else if constexpr (std::is_trivially_copyable_v<ScalarType>)
288 vtkErrorMacro(
"Reallocate is not supported for non-trivially copyable types with realloc.");
295 this->Pointer = newArray;
296 this->Size = newsize;
301 VTK_ABI_NAMESPACE_END
static vtkBuffer< ScalarTypeT > * New()
static vtkBuffer< ScalarTypeT > * ExtendedNew()
void SetReallocFunction(vtkReallocingFunction reallocFunction=realloc)
Set the realloc function to be used when allocating space inside this object.
vtkFreeingFunction DeleteFunction
void SetFreeFunction(bool noFreeFunction, vtkFreeingFunction deleteFunction)
Set the free function to be used when releasing this object.
void * GetVoidBuffer() override
vtkAbstractBuffer interface implementation for Python buffer protocol support.
void(* vtkFreeingFunction)(void *)
vtkIdType GetSize() const
Return the number of elements the current buffer can hold.
A class to help modify and restore the global UsingMemkind state, like SetUsingMemkind(newValue), but safer.
void SetMallocFunction(vtkMallocingFunction mallocFunction=malloc)
Set the malloc function to be used when allocating space inside this object.
vtkTemplateTypeMacro(vtkBuffer< ScalarTypeT >, vtkAbstractBuffer)
static vtkReallocingFunction GetCurrentReallocFunction()
vtkIdType GetNumberOfElements() const override
vtkAbstractBuffer interface implementation for Python buffer protocol support.
vtkMallocingFunction MallocFunction
internal storage class used by vtkSOADataArrayTemplate, vtkAOSDataArrayTemplate, and others...
ScalarType * GetBuffer()
Access the buffer as a scalar pointer.
void SetBuffer(ScalarType *array, vtkIdType size)
Set the memory buffer that this vtkBuffer object will manage.
void *(* vtkMallocingFunction)(size_t)
#define VTK_STANDARD_NEW_BODY(thisClass)
bool Reallocate(vtkIdType newsize)
Allocate a new buffer that holds newsize elements.
bool Allocate(vtkIdType size)
Allocate a new buffer that holds size elements.
int GetDataTypeSize() const override
vtkAbstractBuffer interface implementation for Python buffer protocol support.
static vtkFreeingFunction GetCurrentFreeFunction()
Abstract base class for vtkBuffer providing buffer protocol support.
const ScalarType * GetBuffer() const
void SetFreeFunction(bool noFreeFunction)
Set whether the buffer should be freed when this object is deleted or resized.
void *(* vtkReallocingFunction)(void *, size_t)
int GetDataType() const override
vtkAbstractBuffer interface implementation for Python buffer protocol support.
vtkReallocingFunction ReallocFunction
static vtkMallocingFunction GetCurrentMallocFunction()
Template defining traits of native types used by VTK.