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 
53  void SetBuffer(ScalarType* array, vtkIdType size, bool save=false,
54  void (*deleteFunction)(void*)=free);
55 
59  inline vtkIdType GetSize() const { return this->Size; }
60 
64  bool Allocate(vtkIdType size);
65 
70  bool Reallocate(vtkIdType newsize);
71 
72 protected:
74  : Pointer(nullptr),
75  Size(0),
76  Save(false),
77  DeleteFunction(free)
78  {
79  }
80 
81  ~vtkBuffer() override
82  {
83  this->SetBuffer(nullptr, 0);
84  }
85 
86  ScalarType *Pointer;
88  bool Save;
89  void (*DeleteFunction)(void*);
90 
91 private:
92  vtkBuffer(const vtkBuffer&) = delete;
93  void operator=(const vtkBuffer&) = delete;
94 };
95 
96 template <class ScalarT>
98 {
100 }
101 
102 //------------------------------------------------------------------------------
103 template <typename ScalarT>
105  typename vtkBuffer<ScalarT>::ScalarType *array,
106  vtkIdType size, bool save, void (*deleteFunction)(void*))
107 {
108  if (this->Pointer != array)
109  {
110  if (!this->Save)
111  {
112  this->DeleteFunction(this->Pointer);
113  }
114  this->Pointer = array;
115  }
116  this->Size = size;
117  this->Save = save;
118  this->DeleteFunction = deleteFunction;
119 }
120 
121 //------------------------------------------------------------------------------
122 template <typename ScalarT>
124 {
125  // release old memory.
126  this->SetBuffer(nullptr, 0);
127  if (size > 0)
128  {
129  ScalarType* newArray =
130  static_cast<ScalarType*>(malloc(size * sizeof(ScalarType)));
131  if (newArray)
132  {
133  this->SetBuffer(newArray, size, false, free);
134  return true;
135  }
136  return false;
137  }
138  return true; // size == 0
139 }
140 
141 //------------------------------------------------------------------------------
142 template <typename ScalarT>
144 {
145  if (newsize == 0) { return this->Allocate(0); }
146 
147  if (this->Pointer &&
148  (this->Save || this->DeleteFunction != free))
149  {
150  ScalarType* newArray =
151  static_cast<ScalarType*>(malloc(newsize * sizeof(ScalarType)));
152  if (!newArray)
153  {
154  return false;
155  }
156  std::copy(this->Pointer, this->Pointer + std::min(this->Size, newsize),
157  newArray);
158  // now save the new array and release the old one too.
159  this->SetBuffer(newArray, newsize, false, free);
160  }
161  else
162  {
163  // Try to reallocate with minimal memory usage and possibly avoid
164  // copying.
165  ScalarType* newArray = static_cast<ScalarType*>(
166  realloc(this->Pointer, newsize * sizeof(ScalarType)));
167  if (!newArray)
168  {
169  return false;
170  }
171  this->Pointer = newArray;
172  this->Size = newsize;
173  }
174  return true;
175 }
176 
177 #endif
178 // VTK-HeaderTest-Exclude: vtkBuffer.h
static vtkBuffer< ScalarTypeT > * New()
Definition: vtkBuffer.h:97
vtkIdType GetSize() const
Return the number of elements the current buffer can hold.
Definition: vtkBuffer.h:59
const ScalarType * GetBuffer() const
Definition: vtkBuffer.h:44
abstract base class for most VTK objects
Definition: vtkObject.h:59
vtkTemplateTypeMacro(vtkBuffer< ScalarTypeT >, vtkObject) typedef ScalarTypeT ScalarType
void SetBuffer(ScalarType *array, vtkIdType size, bool save=false, void(*deleteFunction)(void *)=free)
Set the memory buffer that this vtkBuffer object will manage.
Definition: vtkBuffer.h:104
bool Save
Definition: vtkBuffer.h:88
int vtkIdType
Definition: vtkType.h:345
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:86
bool Reallocate(vtkIdType newsize)
Allocate a new buffer that holds newsize elements.
Definition: vtkBuffer.h:143
vtkBuffer()
Definition: vtkBuffer.h:73
void(* DeleteFunction)(void *)
Definition: vtkBuffer.h:89
bool Allocate(vtkIdType size)
Allocate a new buffer that holds size elements.
Definition: vtkBuffer.h:123
#define VTK_STANDARD_NEW_BODY(thisClass)
void save(Archiver &ar, const vtkUnicodeString &str, const unsigned int vtkNotUsed(version))
~vtkBuffer() override
Definition: vtkBuffer.h:81
vtkIdType Size
Definition: vtkBuffer.h:87