VTK  9.0.1
vtkBuffer.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkBuffer.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
25 #ifndef vtkBuffer_h
26 #define vtkBuffer_h
27 
28 #include "vtkObject.h"
29 #include "vtkObjectFactory.h" // New() implementation
30 
31 #include <algorithm> // for std::min and std::copy
32 
33 template <class ScalarTypeT>
34 class vtkBuffer : public vtkObject
35 {
36 public:
38  typedef ScalarTypeT ScalarType;
39 
40  static vtkBuffer<ScalarTypeT>* New();
41 
45  inline ScalarType* GetBuffer() { return this->Pointer; }
46  inline const ScalarType* GetBuffer() const { return this->Pointer; }
47 
53  void SetBuffer(ScalarType* array, vtkIdType size);
54 
61  void SetFreeFunction(bool noFreeFunction, void (*deleteFunction)(void*) = free);
62 
66  inline vtkIdType GetSize() const { return this->Size; }
67 
71  bool Allocate(vtkIdType size);
72 
77  bool Reallocate(vtkIdType newsize);
78 
79 protected:
81  : Pointer(nullptr)
82  , Size(0)
83  , DeleteFunction(free)
84  {
85  }
86 
87  ~vtkBuffer() override { this->SetBuffer(nullptr, 0); }
88 
89  ScalarType* Pointer;
91  void (*DeleteFunction)(void*);
92 
93 private:
94  vtkBuffer(const vtkBuffer&) = delete;
95  void operator=(const vtkBuffer&) = delete;
96 };
97 
98 template <class ScalarT>
100 {
102 }
103 
104 //------------------------------------------------------------------------------
105 template <typename ScalarT>
107 {
108  if (this->Pointer != array)
109  {
110  if (this->DeleteFunction)
111  {
112  this->DeleteFunction(this->Pointer);
113  }
114  this->Pointer = array;
115  }
116  this->Size = size;
117 }
118 //------------------------------------------------------------------------------
119 template <typename ScalarT>
120 void vtkBuffer<ScalarT>::SetFreeFunction(bool noFreeFunction, void (*deleteFunction)(void*))
121 {
122  if (noFreeFunction)
123  {
124  this->DeleteFunction = nullptr;
125  }
126  else
127  {
128  this->DeleteFunction = deleteFunction;
129  }
130 }
131 
132 //------------------------------------------------------------------------------
133 template <typename ScalarT>
135 {
136  // release old memory.
137  this->SetBuffer(nullptr, 0);
138  if (size > 0)
139  {
140  ScalarType* newArray = static_cast<ScalarType*>(malloc(size * sizeof(ScalarType)));
141  if (newArray)
142  {
143  this->SetBuffer(newArray, size);
144  this->DeleteFunction = free;
145  return true;
146  }
147  return false;
148  }
149  return true; // size == 0
150 }
151 
152 //------------------------------------------------------------------------------
153 template <typename ScalarT>
155 {
156  if (newsize == 0)
157  {
158  return this->Allocate(0);
159  }
160 
161  if (this->Pointer && this->DeleteFunction != free)
162  {
163  ScalarType* newArray = static_cast<ScalarType*>(malloc(newsize * sizeof(ScalarType)));
164  if (!newArray)
165  {
166  return false;
167  }
168  std::copy(this->Pointer, this->Pointer + std::min(this->Size, newsize), newArray);
169  // now save the new array and release the old one too.
170  this->SetBuffer(newArray, newsize);
171  this->DeleteFunction = free;
172  }
173  else
174  {
175  // Try to reallocate with minimal memory usage and possibly avoid
176  // copying.
177  ScalarType* newArray =
178  static_cast<ScalarType*>(realloc(this->Pointer, newsize * sizeof(ScalarType)));
179  if (!newArray)
180  {
181  return false;
182  }
183  this->Pointer = newArray;
184  this->Size = newsize;
185  }
186  return true;
187 }
188 
189 #endif
190 // VTK-HeaderTest-Exclude: vtkBuffer.h
vtkIdType
int vtkIdType
Definition: vtkType.h:338
vtkBuffer::SetBuffer
void SetBuffer(ScalarType *array, vtkIdType size)
Set the memory buffer that this vtkBuffer object will manage.
Definition: vtkBuffer.h:106
vtkBuffer::DeleteFunction
void(* DeleteFunction)(void *)
Definition: vtkBuffer.h:91
vtkObject
abstract base class for most VTK objects
Definition: vtkObject.h:62
vtkBuffer::Size
vtkIdType Size
Definition: vtkBuffer.h:90
vtkObjectFactory.h
vtkBuffer::Pointer
ScalarType * Pointer
Definition: vtkBuffer.h:89
vtkBuffer::ScalarType
ScalarTypeT ScalarType
Definition: vtkBuffer.h:38
vtkBuffer
internal storage class used by vtkSOADataArrayTemplate, vtkAOSDataArrayTemplate, and others.
Definition: vtkBuffer.h:34
vtkBuffer::~vtkBuffer
~vtkBuffer() override
Definition: vtkBuffer.h:87
vtkBuffer::Allocate
bool Allocate(vtkIdType size)
Allocate a new buffer that holds size elements.
Definition: vtkBuffer.h:134
vtkBuffer::New
static vtkBuffer< ScalarTypeT > * New()
Definition: vtkBuffer.h:99
vtkX3D::size
@ size
Definition: vtkX3D.h:259
vtkBuffer::Reallocate
bool Reallocate(vtkIdType newsize)
Allocate a new buffer that holds newsize elements.
Definition: vtkBuffer.h:154
vtkBuffer::vtkBuffer
vtkBuffer()
Definition: vtkBuffer.h:80
vtkObject.h
vtkBuffer::GetSize
vtkIdType GetSize() const
Return the number of elements the current buffer can hold.
Definition: vtkBuffer.h:66
vtkBuffer::SetFreeFunction
void SetFreeFunction(bool noFreeFunction, void(*deleteFunction)(void *)=free)
Set the free function to be used when releasing this object.
Definition: vtkBuffer.h:120
VTK_STANDARD_NEW_BODY
#define VTK_STANDARD_NEW_BODY(thisClass)
Definition: vtkObjectFactory.h:341
vtkBuffer::GetBuffer
ScalarType * GetBuffer()
Access the buffer as a scalar pointer.
Definition: vtkBuffer.h:45
vtkBuffer::vtkTemplateTypeMacro
vtkTemplateTypeMacro(vtkBuffer< ScalarTypeT >, vtkObject)
vtkBuffer::GetBuffer
const ScalarType * GetBuffer() const
Definition: vtkBuffer.h:46