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
31template <class ScalarTypeT>
32class vtkBuffer : public vtkObject
33{
34public:
36 typedef ScalarTypeT ScalarType;
37 enum
38 {
41 };
42
44
48 inline ScalarType* GetBuffer() { return this->Pointer; }
49 inline const ScalarType* GetBuffer() const { return this->Pointer; }
50
58 void SetBuffer(ScalarType* array, vtkIdType size, bool save=false,
59 int deleteMethod=VTK_DATA_ARRAY_FREE);
60
64 inline vtkIdType GetSize() const { return this->Size; }
65
70
75 bool Reallocate(vtkIdType newsize);
76
77protected:
79 : Pointer(NULL),
80 Size(0),
81 Save(false),
83 {
84 }
85
86 ~vtkBuffer() VTK_OVERRIDE
87 {
88 this->SetBuffer(NULL, 0);
89 }
90
91 ScalarType *Pointer;
93 bool Save;
95
96private:
97 vtkBuffer(const vtkBuffer&) VTK_DELETE_FUNCTION;
98 void operator=(const vtkBuffer&) VTK_DELETE_FUNCTION;
99};
100
101template <class ScalarT>
103{
105}
106
107//------------------------------------------------------------------------------
108template <typename ScalarT>
110 typename vtkBuffer<ScalarT>::ScalarType *array,
111 vtkIdType size, bool save, int deleteMethod)
112{
113 if (this->Pointer != array)
114 {
115 if (!this->Save)
116 {
117 if (this->DeleteMethod == VTK_DATA_ARRAY_FREE)
118 {
119 free(this->Pointer);
120 }
121 else
122 {
123 delete [] this->Pointer;
124 }
125 }
126 this->Pointer = array;
127 }
128 this->Size = size;
129 this->Save = save;
130 this->DeleteMethod = deleteMethod;
131}
132
133//------------------------------------------------------------------------------
134template <typename ScalarT>
136{
137 // release old memory.
138 this->SetBuffer(NULL, 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, false, VTK_DATA_ARRAY_FREE);
146 return true;
147 }
148 return false;
149 }
150 return true; // size == 0
151}
152
153//------------------------------------------------------------------------------
154template <typename ScalarT>
156{
157 if (newsize == 0) { return this->Allocate(0); }
158
159 if (this->Pointer &&
160 (this->Save || this->DeleteMethod == VTK_DATA_ARRAY_DELETE))
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, false, VTK_DATA_ARRAY_FREE);
172 }
173 else
174 {
175 // Try to reallocate with minimal memory usage and possibly avoid
176 // copying.
177 ScalarType* newArray = static_cast<ScalarType*>(
178 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
internal storage class used by vtkSOADataArrayTemplate, vtkAOSDataArrayTemplate, and others.
Definition: vtkBuffer.h:33
bool Reallocate(vtkIdType newsize)
Allocate a new buffer that holds newsize elements.
Definition: vtkBuffer.h:155
ScalarType * Pointer
Definition: vtkBuffer.h:91
ScalarType * GetBuffer()
Access the buffer as a scalar pointer.
Definition: vtkBuffer.h:48
@ VTK_DATA_ARRAY_FREE
Definition: vtkBuffer.h:39
@ VTK_DATA_ARRAY_DELETE
Definition: vtkBuffer.h:40
const ScalarType * GetBuffer() const
Definition: vtkBuffer.h:49
vtkTemplateTypeMacro(vtkBuffer< ScalarTypeT >, vtkObject) typedef ScalarTypeT ScalarType
vtkIdType Size
Definition: vtkBuffer.h:92
vtkIdType GetSize() const
Return the number of elements the current buffer can hold.
Definition: vtkBuffer.h:64
~vtkBuffer() override
Definition: vtkBuffer.h:86
bool Save
Definition: vtkBuffer.h:93
vtkBuffer()
Definition: vtkBuffer.h:78
bool Allocate(vtkIdType size)
Allocate a new buffer that holds size elements.
Definition: vtkBuffer.h:135
void SetBuffer(ScalarType *array, vtkIdType size, bool save=false, int deleteMethod=VTK_DATA_ARRAY_FREE)
Set the memory buffer that this vtkBuffer object will manage.
Definition: vtkBuffer.h:109
static vtkBuffer< ScalarTypeT > * New()
Definition: vtkBuffer.h:102
int DeleteMethod
Definition: vtkBuffer.h:94
abstract base class for most VTK objects
Definition: vtkObject.h:60
@ size
Definition: vtkX3D.h:253
#define VTK_STANDARD_NEW_BODY(thisClass)
int vtkIdType
Definition: vtkType.h:287
void save(Archiver &ar, const vtkUnicodeString &str, const unsigned int vtkNotUsed(version))