VTK  9.3.20231210
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 
32 namespace vtk
33 {
34 namespace detail
35 {
36 VTK_ABI_NAMESPACE_BEGIN
37 template <typename ArrayType, ComponentIdType TupleSize>
38 struct TupleRange;
39 template <typename ArrayType, ComponentIdType TupleSize>
40 struct ValueRange;
41 VTK_ABI_NAMESPACE_END
42 } // namespace detail
43 } // namespace vtk
44 
45 // The export macro below makes no sense, but is necessary for older compilers
46 // when we export instantiations of this class from vtkCommonCore.
47 VTK_ABI_NAMESPACE_BEGIN
48 template <class ValueTypeT>
49 class VTKCOMMONCORE_EXPORT vtkAOSDataArrayTemplate
50  : public vtkGenericDataArray<vtkAOSDataArrayTemplate<ValueTypeT>, ValueTypeT>
51 {
53 
54  // Friendship required by vtkDataArray(Value/Tuple)Range so that it can access the memory buffer
55  // which is required to avoid accessing raw pointers that might no longer be valid.
56  template <typename ArrayType, vtk::ComponentIdType TupleSize>
57  friend struct vtk::detail::TupleRange;
58  template <typename ArrayType, vtk::ComponentIdType TupleSize>
59  friend struct vtk::detail::ValueRange;
60 
61 public:
64  typedef typename Superclass::ValueType ValueType;
65 
67  {
69  VTK_DATA_ARRAY_DELETE = vtkAbstractArray::VTK_DATA_ARRAY_DELETE,
70  VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
71  VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
72  };
73 
75 
79  ValueType GetValue(vtkIdType valueIdx) const
80  VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
81  {
82  return this->Buffer->GetBuffer()[valueIdx];
83  }
84 
89  VTK_EXPECTS(0 <= valueIdx && valueIdx < GetNumberOfValues())
90  {
91  this->Buffer->GetBuffer()[valueIdx] = value;
92  }
93 
95 
98  void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
99  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
100  {
101  const vtkIdType valueIdx = tupleIdx * this->NumberOfComponents;
102  std::copy(this->Buffer->GetBuffer() + valueIdx,
103  this->Buffer->GetBuffer() + valueIdx + this->NumberOfComponents, tuple);
104  }
106 
108 
111  void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
112  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples())
113  {
114  const vtkIdType valueIdx = tupleIdx * this->NumberOfComponents;
115  std::copy(tuple, tuple + this->NumberOfComponents, this->Buffer->GetBuffer() + valueIdx);
116  }
118 
122  ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const VTK_EXPECTS(0 <= tupleIdx &&
123  tupleIdx < GetNumberOfTuples()) VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents())
124  {
125  return this->Buffer->GetBuffer()[this->NumberOfComponents * tupleIdx + comp];
126  }
127 
129 
132  void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value) VTK_EXPECTS(0 <= tupleIdx &&
133  tupleIdx < GetNumberOfTuples()) VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents())
134  {
135  const vtkIdType valueIdx = tupleIdx * this->NumberOfComponents + comp;
136  this->SetValue(valueIdx, value);
137  }
139 
141 
144  void FillTypedComponent(int compIdx, ValueType value) override;
146 
148 
151  void FillValue(ValueType value) override;
152  void Fill(double value) override;
154 
156 
161  ValueType* WritePointer(vtkIdType valueIdx, vtkIdType numValues);
162  void* WriteVoidPointer(vtkIdType valueIdx, vtkIdType numValues) override;
164 
166 
174  void* GetVoidPointer(vtkIdType valueIdx) override;
176 
178 
192  void SetArray(VTK_ZEROCOPY ValueType* array, vtkIdType size, int save, int deleteMethod);
194  void SetVoidArray(void* array, vtkIdType size, int save) override;
195  void SetVoidArray(void* array, vtkIdType size, int save, int deleteMethod) override;
197 
204  void SetArrayFreeFunction(void (*callback)(void*)) override;
205 
206  // Overridden for optimized implementations:
207  void SetTuple(vtkIdType tupleIdx, const float* tuple) override;
208  void SetTuple(vtkIdType tupleIdx, const double* tuple) override;
209  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
210  // using Superclass::SetTuple;
211  void SetTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx, vtkAbstractArray* source) override
212  {
213  this->Superclass::SetTuple(dstTupleIdx, srcTupleIdx, source);
214  }
215  void InsertTuple(vtkIdType tupleIdx, const float* source) override;
216  void InsertTuple(vtkIdType tupleIdx, const double* source) override;
217  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
218  // using Superclass::InsertTuple;
219  void InsertTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx, vtkAbstractArray* source) override
220  {
221  this->Superclass::InsertTuple(dstTupleIdx, srcTupleIdx, source);
222  }
223  void InsertComponent(vtkIdType tupleIdx, int compIdx, double value) override;
224  vtkIdType InsertNextTuple(const float* tuple) override;
225  vtkIdType InsertNextTuple(const double* tuple) override;
226  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
227  // using Superclass::InsertNextTuple;
229  {
230  return this->Superclass::InsertNextTuple(srcTupleIdx, source);
231  }
232  void GetTuple(vtkIdType tupleIdx, double* tuple) override;
233  double* GetTuple(vtkIdType tupleIdx) override;
234 
246 
251  typedef ValueType* Iterator;
252  Iterator Begin() { return Iterator(this->GetVoidPointer(0)); }
253  Iterator End() { return Iterator(this->GetVoidPointer(this->MaxId + 1)); }
254 
256 
265 
266  int GetArrayType() const override { return vtkAbstractArray::AoSDataArrayTemplate; }
268  bool HasStandardMemoryLayout() const override { return true; }
269  void ShallowCopy(vtkDataArray* other) override;
270 
271  // Reimplemented for efficiency:
273  vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
274  // MSVC doesn't like 'using' here (error C2487). Just forward instead:
275  // using Superclass::InsertTuples;
276  void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override
277  {
278  this->Superclass::InsertTuples(dstIds, srcIds, source);
279  }
281  vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override
282  {
283  this->Superclass::InsertTuplesStartingAt(dstStart, srcIds, source);
284  }
285 
286 protected:
289 
294  bool AllocateTuples(vtkIdType numTuples);
295 
300  bool ReallocateTuples(vtkIdType numTuples);
301 
303 
304 private:
306  void operator=(const vtkAOSDataArrayTemplate&) = delete;
307 
308  friend class vtkGenericDataArray<vtkAOSDataArrayTemplate<ValueTypeT>, ValueTypeT>;
309 };
310 
311 // Declare vtkArrayDownCast implementations for AoS containers:
313 
314 VTK_ABI_NAMESPACE_END
315 
316 // This macro is used by the subclasses to create dummy
317 // declarations for these functions such that the wrapper
318 // can see them. The wrappers ignore vtkAOSDataArrayTemplate.
319 #define vtkCreateWrappedArrayInterface(T) \
320  int GetDataType() const override; \
321  void GetTypedTuple(vtkIdType i, T* tuple) VTK_EXPECTS(0 <= i && i < GetNumberOfTuples()); \
322  void SetTypedTuple(vtkIdType i, const T* tuple) VTK_EXPECTS(0 <= i && i < GetNumberOfTuples()); \
323  void InsertTypedTuple(vtkIdType i, const T* tuple) VTK_EXPECTS(0 <= i); \
324  vtkIdType InsertNextTypedTuple(const T* tuple); \
325  T GetValue(vtkIdType id) const VTK_EXPECTS(0 <= id && id < GetNumberOfValues()); \
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* GetValueRange(int comp) VTK_SIZEHINT(2); \
331  T* GetValueRange() VTK_SIZEHINT(2); \
332  T* WritePointer(vtkIdType id, vtkIdType number); \
333  T* GetPointer(vtkIdType id); \
334  void SetArray(VTK_ZEROCOPY T* array, vtkIdType size, int save); \
335  void SetArray(VTK_ZEROCOPY T* array, vtkIdType size, int save, int deleteMethod)
336 
337 #endif // header guard
338 
339 // This portion must be OUTSIDE the include blockers. This is used to tell
340 // libraries other than vtkCommonCore that instantiations of
341 // vtkAOSDataArrayTemplate can be found externally. This prevents each library
342 // from instantiating these on their own.
343 #ifdef VTK_AOS_DATA_ARRAY_TEMPLATE_INSTANTIATING
344 #define VTK_AOS_DATA_ARRAY_TEMPLATE_INSTANTIATE(T) \
345  namespace vtkDataArrayPrivate \
346  { \
347  VTK_ABI_NAMESPACE_BEGIN \
348  VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkAOSDataArrayTemplate<T>, double); \
349  VTK_ABI_NAMESPACE_END \
350  } \
351  VTK_ABI_NAMESPACE_BEGIN \
352  template class VTKCOMMONCORE_EXPORT vtkAOSDataArrayTemplate<T>; \
353  VTK_ABI_NAMESPACE_END
354 
355 #elif defined(VTK_USE_EXTERN_TEMPLATE)
356 #ifndef VTK_AOS_DATA_ARRAY_TEMPLATE_EXTERN
357 #define VTK_AOS_DATA_ARRAY_TEMPLATE_EXTERN
358 #ifdef _MSC_VER
359 #pragma warning(push)
360 // The following is needed when the vtkAOSDataArrayTemplate is declared
361 // dllexport and is used from another class in vtkCommonCore
362 #pragma warning(disable : 4910) // extern and dllexport incompatible
363 #endif
364 VTK_ABI_NAMESPACE_BEGIN
365 vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkAOSDataArrayTemplate);
366 VTK_ABI_NAMESPACE_END
367 #ifdef _MSC_VER
368 #pragma warning(pop)
369 #endif
370 #endif // VTK_AOS_DATA_ARRAY_TEMPLATE_EXTERN
371 
372 // The following clause is only for MSVC
373 #elif defined(_MSC_VER) && !defined(VTK_BUILD_SHARED_LIBS)
374 #pragma warning(push)
375 
376 // C4091: 'extern ' : ignored on left of 'int' when no variable is declared
377 #pragma warning(disable : 4091)
378 
379 // Compiler-specific extension warning.
380 #pragma warning(disable : 4231)
381 
382 // We need to disable warning 4910 and do an extern dllexport
383 // anyway. When deriving vtkCharArray and other types from an
384 // instantiation of this template the compiler does an explicit
385 // instantiation of the base class. From outside the vtkCommon
386 // library we block this using an extern dllimport instantiation.
387 // For classes inside vtkCommon we should be able to just do an
388 // extern instantiation, but VS complains about missing
389 // definitions. We cannot do an extern dllimport inside vtkCommon
390 // since the symbols are local to the dll. An extern dllexport
391 // seems to be the only way to convince VS to do the right
392 // thing, so we just disable the warning.
393 #pragma warning(disable : 4910) // extern and dllexport incompatible
394 
395 // Use an "extern explicit instantiation" to give the class a DLL
396 // interface. This is a compiler-specific extension.
397 VTK_ABI_NAMESPACE_BEGIN
398 vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkAOSDataArrayTemplate);
399 VTK_ABI_NAMESPACE_END
400 
401 #pragma warning(pop)
402 
403 #endif
404 
405 // VTK-HeaderTest-Exclude: vtkAOSDataArrayTemplate.h
Array-Of-Structs implementation of vtkGenericDataArray.
vtkArrayIterator * NewIterator() override
Subclasses must override this method and provide the right kind of templated vtkArrayIteratorTemplate...
static vtkAOSDataArrayTemplate * New()
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.
ValueType * WritePointer(vtkIdType valueIdx, vtkIdType numValues)
Get the address of a particular data index.
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 * WriteVoidPointer(vtkIdType valueIdx, vtkIdType numValues) override
Get the address of a particular data index.
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.
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 * GetVoidPointer(vtkIdType valueIdx) override
Get the address of a particular data index.
void FillValue(ValueType value) override
Set all the values in array to value.
ValueType * GetPointer(vtkIdType valueIdx)
Get the address of a particular data index.
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.
static vtkAOSDataArrayTemplate< ValueType > * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkAOSDataArrayTemplate.
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...
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...
double * GetTuple(vtkIdType tupleIdx) override
Get the data tuple at tupleIdx.
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,...
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.
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.
vtkIdType GetNumberOfValues() const
Get the total number of values in the array.
Abstract superclass to iterate over elements in an vtkAbstractArray.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:154
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:132
void SetValue(vtkIdType valueIdx, ValueType value)
Set the value at valueIdx to value.
@ value
Definition: vtkX3D.h:220
@ size
Definition: vtkX3D.h:253
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
vtkExternTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkAOSDataArrayTemplate)
vtkArrayDownCast_TemplateFastCastMacro(vtkAOSDataArrayTemplate)
vtkInstantiateTemplateMacro(extern template class VTKCOMMONCORE_EXPORT vtkArrayIteratorTemplate)
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.
int vtkIdType
Definition: vtkType.h:315
void save(Archiver &ar, const std::string &str, const unsigned int vtkNotUsed(version))
#define VTK_EXPECTS(x)
#define VTK_ZEROCOPY
#define VTK_NEWINSTANCE