VTK  9.3.20240528
vtkAOSDataArrayTemplate.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
22#ifndef vtkAOSDataArrayTemplate_h
23#define vtkAOSDataArrayTemplate_h
24
25#include "vtkBuffer.h" // For storage buffer.
26#include "vtkBuild.h" // For VTK_BUILD_SHARED_LIBS
27#include "vtkCommonCoreModule.h" // For export macro
28#include "vtkCompiler.h" // for VTK_USE_EXTERN_TEMPLATE
29#include "vtkDataArrayMeta.h" // For vtkDataArrayMeta::ComponentType
30#include "vtkGenericDataArray.h"
31
32namespace vtk
33{
34namespace detail
35{
36VTK_ABI_NAMESPACE_BEGIN
37template <typename ArrayType, ComponentIdType TupleSize>
38struct TupleRange;
39template <typename ArrayType, ComponentIdType TupleSize,
40 typename ForceValueTypeForVtkDataArray = double>
41struct ValueRange;
42VTK_ABI_NAMESPACE_END
43} // namespace detail
44} // namespace vtk
45
46// The export macro below makes no sense, but is necessary for older compilers
47// when we export instantiations of this class from vtkCommonCore.
48VTK_ABI_NAMESPACE_BEGIN
49template <class ValueTypeT>
50class VTKCOMMONCORE_EXPORT vtkAOSDataArrayTemplate
51 : public vtkGenericDataArray<vtkAOSDataArrayTemplate<ValueTypeT>, ValueTypeT>
52{
54
55 // Friendship required by vtkDataArray(Value/Tuple)Range so that it can access the memory buffer
56 // which is required to avoid accessing raw pointers that might no longer be valid.
57 template <typename ArrayType, vtk::ComponentIdType TupleSize>
59 template <typename ArrayType, vtk::ComponentIdType TupleSize,
60 typename ForceValueTypeForVtkDataArray>
62
63public:
66 typedef typename Superclass::ValueType ValueType;
67
69 {
72 VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
73 VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
74 };
75
77
82 VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
83 {
84 return this->Buffer->GetBuffer()[valueIdx];
85 }
86
90 void SetValue(vtkIdType valueIdx, ValueType value)
91 VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
92 {
93 this->Buffer->GetBuffer()[valueIdx] = value;
94 }
95
97
100 void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
101 VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
102 {
103 const vtkIdType valueIdx = tupleIdx * this->NumberOfComponents;
104 std::copy(this->Buffer->GetBuffer() + valueIdx,
105 this->Buffer->GetBuffer() + valueIdx + this->NumberOfComponents, tuple);
106 }
108
110
113 void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
114 VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
115 {
116 const vtkIdType valueIdx = tupleIdx * this->NumberOfComponents;
117 std::copy(tuple, tuple + this->NumberOfComponents, this->Buffer->GetBuffer() + valueIdx);
118 }
120
124 ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const VTK_EXPECTS(0 <= tupleIdx &&
125 tupleIdx < GetNumberOfTuples()) VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents())
126 {
127 return this->Buffer->GetBuffer()[this->NumberOfComponents * tupleIdx + comp];
128 }
129
131
134 void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value) VTK_EXPECTS(0 <= tupleIdx &&
135 tupleIdx < GetNumberOfTuples()) VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents())
136 {
137 const vtkIdType valueIdx = tupleIdx * this->NumberOfComponents + comp;
138 this->SetValue(valueIdx, value);
139 }
141
143
146 void FillTypedComponent(int compIdx, ValueType value) override;
148
150
153 void FillValue(ValueType value) override;
154 void Fill(double value) override;
156
158
164 void* WriteVoidPointer(vtkIdType valueIdx, vtkIdType numValues) override;
166
168
176 void* GetVoidPointer(vtkIdType valueIdx) override;
178
180
194 void SetArray(VTK_ZEROCOPY ValueType* array, vtkIdType size, int save, int deleteMethod);
196 void SetVoidArray(void* array, vtkIdType size, int save) override;
197 void SetVoidArray(void* array, vtkIdType size, int save, int deleteMethod) override;
199
206 void SetArrayFreeFunction(void (*callback)(void*)) override;
207
208 // Overridden for optimized implementations:
209 void SetTuple(vtkIdType tupleIdx, const float* tuple) override;
210 void SetTuple(vtkIdType tupleIdx, const double* tuple) override;
211 // MSVC doesn't like 'using' here (error C2487). Just forward instead:
212 // using Superclass::SetTuple;
213 void SetTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx, vtkAbstractArray* source) override
214 {
215 this->Superclass::SetTuple(dstTupleIdx, srcTupleIdx, source);
216 }
217 void InsertTuple(vtkIdType tupleIdx, const float* source) override;
218 void InsertTuple(vtkIdType tupleIdx, const double* source) override;
219 // MSVC doesn't like 'using' here (error C2487). Just forward instead:
220 // using Superclass::InsertTuple;
221 void InsertTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx, vtkAbstractArray* source) override
222 {
223 this->Superclass::InsertTuple(dstTupleIdx, srcTupleIdx, source);
224 }
225 void InsertComponent(vtkIdType tupleIdx, int compIdx, double value) override;
226 vtkIdType InsertNextTuple(const float* tuple) override;
227 vtkIdType InsertNextTuple(const double* tuple) override;
228 // MSVC doesn't like 'using' here (error C2487). Just forward instead:
229 // using Superclass::InsertNextTuple;
231 {
232 return this->Superclass::InsertNextTuple(srcTupleIdx, source);
233 }
234 void GetTuple(vtkIdType tupleIdx, double* tuple) override;
235 double* GetTuple(vtkIdType tupleIdx) override;
236
248
253 typedef ValueType* Iterator;
254 Iterator Begin() { return Iterator(this->GetVoidPointer(0)); }
255 Iterator End() { return Iterator(this->GetVoidPointer(this->MaxId + 1)); }
256
258
267
270 bool HasStandardMemoryLayout() const override { return true; }
271 void ShallowCopy(vtkDataArray* other) override;
272
273 // Reimplemented for efficiency:
275 vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
276 // MSVC doesn't like 'using' here (error C2487). Just forward instead:
277 // using Superclass::InsertTuples;
278 void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
279 {
280 this->Superclass::InsertTuples(dstIds, srcIds, source);
281 }
283 vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override
284 {
285 this->Superclass::InsertTuplesStartingAt(dstStart, srcIds, source);
286 }
287
288protected:
291
296 bool AllocateTuples(vtkIdType numTuples);
297
303
305
306private:
308 void operator=(const vtkAOSDataArrayTemplate&) = delete;
309
310 friend class vtkGenericDataArray<vtkAOSDataArrayTemplate<ValueTypeT>, ValueTypeT>;
311};
312
313// Declare vtkArrayDownCast implementations for AoS containers:
315
316VTK_ABI_NAMESPACE_END
317
318// This macro is used by the subclasses to create dummy
319// declarations for these functions such that the wrapper
320// can see them. The wrappers ignore vtkAOSDataArrayTemplate.
321#define vtkCreateWrappedArrayInterface(T) \
322 vtkCreateReadOnlyWrappedArrayInterface(T) void SetTypedTuple(vtkIdType i, const T* tuple) \
323 VTK_EXPECTS(0 <= i && i < GetNumberOfTuples()); \
324 void InsertTypedTuple(vtkIdType i, const T* tuple) VTK_EXPECTS(0 <= i); \
325 vtkIdType InsertNextTypedTuple(const T* tuple); \
326 void SetValue(vtkIdType id, T value) VTK_EXPECTS(0 <= id && id < GetNumberOfValues()); \
327 bool SetNumberOfValues(vtkIdType number) override; \
328 void InsertValue(vtkIdType id, T f) VTK_EXPECTS(0 <= id); \
329 vtkIdType InsertNextValue(T f); \
330 T* WritePointer(vtkIdType id, vtkIdType number); \
331 T* GetPointer(vtkIdType id); \
332 void SetArray(VTK_ZEROCOPY T* array, vtkIdType size, int save); \
333 void SetArray(VTK_ZEROCOPY T* array, vtkIdType size, int save, int deleteMethod)
334
335#define vtkCreateReadOnlyWrappedArrayInterface(T) \
336 int GetDataType() const override; \
337 void GetTypedTuple(vtkIdType i, T* tuple) VTK_EXPECTS(0 <= i && i < GetNumberOfTuples()); \
338 T GetValue(vtkIdType id) const VTK_EXPECTS(0 <= id && id < GetNumberOfValues()); \
339 T* GetValueRange(int comp) VTK_SIZEHINT(2); \
340 T* GetValueRange() VTK_SIZEHINT(2);
341
342#endif // header guard
343
344// This portion must be OUTSIDE the include blockers. This is used to tell
345// libraries other than vtkCommonCore that instantiations of
346// vtkAOSDataArrayTemplate can be found externally. This prevents each library
347// from instantiating these on their own.
348#ifdef VTK_AOS_DATA_ARRAY_TEMPLATE_INSTANTIATING
349#define VTK_AOS_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
350 namespace vtkDataArrayPrivate \
351 { \
352 VTK_ABI_NAMESPACE_BEGIN \
353 VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkAOSDataArrayTemplate<T>, double); \
354 VTK_ABI_NAMESPACE_END \
355 } \
356 VTK_ABI_NAMESPACE_BEGIN \
357 template class VTKCOMMONCORE_EXPORT vtkAOSDataArrayTemplate<T>; \
358 VTK_ABI_NAMESPACE_END
359
360#elif defined(VTK_USE_EXTERN_TEMPLATE)
361#ifndef VTK_AOS_DATA_ARRAY_TEMPLATE_EXTERN
362#define VTK_AOS_DATA_ARRAY_TEMPLATE_EXTERN
363#ifdef _MSC_VER
364#pragma warning(push)
365// The following is needed when the vtkAOSDataArrayTemplate is declared
366// dllexport and is used from another class in vtkCommonCore
367#pragma warning(disable : 4910) // extern and dllexport incompatible
368#endif
369VTK_ABI_NAMESPACE_BEGIN
370vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkAOSDataArrayTemplate);
371VTK_ABI_NAMESPACE_END
372#ifdef _MSC_VER
373#pragma warning(pop)
374#endif
375#endif // VTK_AOS_DATA_ARRAY_TEMPLATE_EXTERN
376
377// The following clause is only for MSVC
378#elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
379#pragma warning(push)
380
381// C4091: 'extern ' : ignored on left of 'int' when no variable is declared
382#pragma warning(disable : 4091)
383
384// Compiler-specific extension warning.
385#pragma warning(disable : 4231)
386
387// We need to disable warning 4910 and do an extern dllexport
388// anyway. When deriving vtkCharArray and other types from an
389// instantiation of this template the compiler does an explicit
390// instantiation of the base class. From outside the vtkCommon
391// library we block this using an extern dllimport instantiation.
392// For classes inside vtkCommon we should be able to just do an
393// extern instantiation, but VS complains about missing
394// definitions. We cannot do an extern dllimport inside vtkCommon
395// since the symbols are local to the dll. An extern dllexport
396// seems to be the only way to convince VS to do the right
397// thing, so we just disable the warning.
398#pragma warning(disable : 4910) // extern and dllexport incompatible
399
400// Use an "extern explicit instantiation" to give the class a DLL
401// interface. This is a compiler-specific extension.
402VTK_ABI_NAMESPACE_BEGIN
403vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkAOSDataArrayTemplate);
404VTK_ABI_NAMESPACE_END
405
406#pragma warning(pop)
407
408#endif
409
410// VTK-HeaderTest-Exclude: vtkAOSDataArrayTemplate.h
Array-Of-Structs implementation of vtkGenericDataArray.
static vtkAOSDataArrayTemplate< ValueType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkAOSDataArrayTemplate.
void SetVoidArray(void *array, vtkIdType size, int save) override
This method lets the user specify data to be held by the array.
void SetArray(ValueType *array, vtkIdType size, int save)
This method lets the user specify data to be held by the array.
void SetTuple(vtkIdType tupleIdx, const float *tuple) override
Set the data tuple at tupleIdx.
void InsertTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx, vtkAbstractArray *source) override
Insert the tuple at srcTupleIdx in the source array into this array at dstTupleIdx.
void SetTuple(vtkIdType tupleIdx, const double *tuple) override
Set the data tuple at tupleIdx.
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array's tuple at tupleIdx to the values in tuple.
void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray *source) override
Copy n consecutive tuples starting at srcStart from the source array to this array,...
void InsertTuplesStartingAt(vtkIdType dstStart, vtkIdList *srcIds, vtkAbstractArray *source) override
Copy the tuples indexed in srcIds from the source array to the tuple locations starting at index dstS...
void SetTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx, vtkAbstractArray *source) override
Set the tuple at dstTupleIdx in this array to the tuple at srcTupleIdx in the source array.
void DataElementChanged(vtkIdType)
Tell the array explicitly that a single data element has changed.
void SetVoidArray(void *array, vtkIdType size, int save, int deleteMethod) override
This method lets the user specify data to be held by the array.
ValueType * WritePointer(vtkIdType valueIdx, vtkIdType numValues)
Get the address of a particular data index.
void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source) override
Copy the tuples indexed in srcIds from the source array to the tuple locations indexed by dstIds in t...
void FillValue(ValueType value) override
Set all the values in array to value.
vtkIdType InsertNextTuple(vtkIdType srcTupleIdx, vtkAbstractArray *source) override
Insert the tuple from srcTupleIdx in the source array at the end of this array.
void InsertTuple(vtkIdType tupleIdx, const float *source) override
Insert the data tuple at tupleIdx.
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
void * WriteVoidPointer(vtkIdType valueIdx, vtkIdType numValues) override
Get the address of a particular data index.
void SetArray(ValueType *array, vtkIdType size, int save, int deleteMethod)
This method lets the user specify data to be held by the array.
void SetArrayFreeFunction(void(*callback)(void *)) override
This method allows the user to specify a custom free function to be called when the array is dealloca...
vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
void GetTuple(vtkIdType tupleIdx, double *tuple) override
Get the data tuple at tupleIdx by filling in a user-provided array, Make sure that your array is larg...
void InsertTuple(vtkIdType tupleIdx, const double *source) override
Insert the data tuple at tupleIdx.
bool HasStandardMemoryLayout() const override
Returns true if this array uses the standard memory layout defined in the VTK user guide,...
void * GetVoidPointer(vtkIdType valueIdx) override
Get the address of a particular data index.
vtkIdType InsertNextTuple(const float *tuple) override
Insert the data tuple at the end of the array and return the tuple index at which the data was insert...
void FillTypedComponent(int compIdx, ValueType value) override
Set component comp of all tuples to value.
ValueType * GetPointer(vtkIdType valueIdx)
Get the address of a particular data index.
static vtkAOSDataArrayTemplate * New()
double * GetTuple(vtkIdType tupleIdx) override
Get the data tuple at tupleIdx.
void Fill(double value) override
Set all the values in array to value.
void InsertComponent(vtkIdType tupleIdx, int compIdx, double value) override
Insert value at the location specified by tupleIdx and compIdx.
vtkTemplateTypeMacro(SelfType, GenericDataArrayType)
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
~vtkAOSDataArrayTemplate() override
void ShallowCopy(vtkDataArray *other) override
Create a shallow copy of other into this, if possible.
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
int GetArrayType() const override
Method for type-checking in FastDownCast implementations.
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
Get component comp of the tuple at tupleIdx.
ValueType GetValue(vtkIdType valueIdx) const
Get the value at valueIdx.
ValueType * Iterator
Legacy support for array-of-structs value iteration.
vtkAOSDataArrayTemplate< ValueTypeT > SelfType
vtkBuffer< ValueType > * Buffer
vtkIdType InsertNextTuple(const double *tuple) override
Insert the data tuple at the end of the array and return the tuple index at which the data was insert...
Superclass::ValueType ValueType
Abstract superclass for all arrays.
vtkIdType GetNumberOfTuples() const
Get the number of complete tuples (a component group) in the array.
virtual void SetTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx, vtkAbstractArray *source)=0
Set the tuple at dstTupleIdx in this array to the tuple at srcTupleIdx in the source array.
virtual void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source)=0
Copy the tuples indexed in srcIds from the source array to the tuple locations indexed by dstIds in t...
virtual void InsertTuplesStartingAt(vtkIdType dstStart, vtkIdList *srcIds, vtkAbstractArray *source)=0
Copy the tuples indexed in srcIds from the source array to the tuple locations starting at index dstS...
virtual void InsertTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx, vtkAbstractArray *source)=0
Insert the tuple at srcTupleIdx in the source array into this array at dstTupleIdx.
vtkIdType GetNumberOfValues() const
Get the total number of values in the array.
virtual vtkIdType InsertNextTuple(vtkIdType srcTupleIdx, vtkAbstractArray *source)=0
Insert the tuple from srcTupleIdx in the source array at the end of this array.
Abstract superclass to iterate over elements in an vtkAbstractArray.
internal storage class used by vtkSOADataArrayTemplate, vtkAOSDataArrayTemplate, and others.
Definition vtkBuffer.h:27
abstract superclass for arrays of numeric data
Base interface for all typed vtkDataArray subclasses.
void DataChanged() override
Tell the array explicitly that the data has changed.
void * GetVoidPointer(vtkIdType valueIdx) override
Default implementation raises a runtime error.
list of point or cell ids
Definition vtkIdList.h:133
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
int ComponentIdType
#define vtkArrayDownCast_TemplateFastCastMacro(ArrayT)
Same as vtkArrayDownCast_FastCastMacro, but treats ArrayT as a single-parameter template (the paramet...
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
This file contains a variety of metaprogramming constructs for working with vtkDataArrays.
#define vtkExternTemplateMacro(decl)
A macro to declare extern templates for all numerical types.
Definition vtkType.h:410
int vtkIdType
Definition vtkType.h:315
#define vtkInstantiateTemplateMacro(decl)
A macro to instantiate a template over all numerical types.
Definition vtkType.h:363
void save(Archiver &ar, const std::string &str, const unsigned int vtkNotUsed(version))
#define VTK_EXPECTS(x)
#define VTK_ZEROCOPY
#define VTK_NEWINSTANCE