VTK
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 template <class ScalarTypeT>
32 class vtkBuffer : public vtkObject
33 {
34 public:
36  typedef ScalarTypeT ScalarType;
37 
38  static vtkBuffer<ScalarTypeT>* New();
39 
43  inline ScalarType* GetBuffer() { return this->Pointer; }
44  inline const ScalarType* GetBuffer() const { return this->Pointer; }
45 
51  void SetBuffer(ScalarType* array, vtkIdType size);
52 
59  void SetFreeFunction(bool noFreeFunction, void(*deleteFunction)(void*)=free);
60 
64  inline vtkIdType GetSize() const { return this->Size; }
65 
69  bool Allocate(vtkIdType size);
70 
75  bool Reallocate(vtkIdType newsize);
76 
77 protected:
79  : Pointer(nullptr),
80  Size(0),
81  DeleteFunction(free)
82  {
83  }
84 
85  ~vtkBuffer() override
86  {
87  this->SetBuffer(nullptr, 0);
88  }
89 
90  ScalarType *Pointer;
92  void (*DeleteFunction)(void*);
93 
94 private:
95  vtkBuffer(const vtkBuffer&) = delete;
96  void operator=(const vtkBuffer&) = delete;
97 };
98 
99 template <class ScalarT>
101 {
103 }
104 
105 //------------------------------------------------------------------------------
106 template <typename ScalarT>
108  typename vtkBuffer<ScalarT>::ScalarType *array, vtkIdType size) {
109  if (this->Pointer != array)
110  {
111  if(this->DeleteFunction)
112  {
113  this->DeleteFunction(this->Pointer);
114  }
115  this->Pointer = array;
116  }
117  this->Size = size;
118 }
119 //------------------------------------------------------------------------------
120 template <typename ScalarT>
121 void vtkBuffer<ScalarT>::SetFreeFunction(bool noFreeFunction, void(*deleteFunction)(void*))
122 {
123  if(noFreeFunction)
124  {
125  this->DeleteFunction = nullptr;
126  }
127  else
128  {
129  this->DeleteFunction = deleteFunction;
130  }
131 }
132 
133 //------------------------------------------------------------------------------
134 template <typename ScalarT>
136 {
137  // release old memory.
138  this->SetBuffer(nullptr, 0);
139  if (size > 0)
140  {
141  ScalarType* newArray =
142  static_cast<ScalarType*>(malloc(size * sizeof(ScalarType)));
143  if (newArray)
144  {
145  this->SetBuffer(newArray, size);
146  this->DeleteFunction = free;
147  return true;
148  }
149  return false;
150  }
151  return true; // size == 0
152 }
153 
154 //------------------------------------------------------------------------------
155 template <typename ScalarT>
157 {
158  if (newsize == 0) { return this->Allocate(0); }
159 
160  if (this->Pointer && this->DeleteFunction != free)
161  {
162  ScalarType* newArray =
163  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),
169  newArray);
170  // now save the new array and release the old one too.
171  this->SetBuffer(newArray, newsize);
172  this->DeleteFunction = free;
173  }
174  else
175  {
176  // Try to reallocate with minimal memory usage and possibly avoid
177  // copying.
178  ScalarType* newArray = static_cast<ScalarType*>(
179  realloc(this->Pointer, newsize * sizeof(ScalarType)));
180  if (!newArray)
181  {
182  return false;
183  }
184  this->Pointer = newArray;
185  this->Size = newsize;
186  }
187  return true;
188 }
189 
190 #endif
191 // VTK-HeaderTest-Exclude: vtkBuffer.h
static vtkBuffer< ScalarTypeT > * New()
Definition: vtkBuffer.h:100
abstract base class for most VTK objects
Definition: vtkObject.h:59
vtkTemplateTypeMacro(vtkBuffer< ScalarTypeT >, vtkObject) typedef ScalarTypeT ScalarType
vtkIdType GetSize() const
Return the number of elements the current buffer can hold.
Definition: vtkBuffer.h:64
int vtkIdType
Definition: vtkType.h:347
void SetFreeFunction(bool noFreeFunction, void(*deleteFunction)(void *)=free)
Set the free function to be used when releasing this object.
Definition: vtkBuffer.h:121
internal storage class used by vtkSOADataArrayTemplate, vtkAOSDataArrayTemplate, and others...
Definition: vtkBuffer.h:32
ScalarType * GetBuffer()
Access the buffer as a scalar pointer.
Definition: vtkBuffer.h:43
ScalarType * Pointer
Definition: vtkBuffer.h:90
void SetBuffer(ScalarType *array, vtkIdType size)
Set the memory buffer that this vtkBuffer object will manage.
Definition: vtkBuffer.h:107
bool Reallocate(vtkIdType newsize)
Allocate a new buffer that holds newsize elements.
Definition: vtkBuffer.h:156
vtkBuffer()
Definition: vtkBuffer.h:78
void(* DeleteFunction)(void *)
Definition: vtkBuffer.h:92
bool Allocate(vtkIdType size)
Allocate a new buffer that holds size elements.
Definition: vtkBuffer.h:135
#define VTK_STANDARD_NEW_BODY(thisClass)
const ScalarType * GetBuffer() const
Definition: vtkBuffer.h:44
~vtkBuffer() override
Definition: vtkBuffer.h:85
vtkIdType Size
Definition: vtkBuffer.h:91