VTK  9.7.1
MockDataArray.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
3 
4 #include "vtkBuffer.h"
5 #include "vtkGenericDataArray.h"
6 #include "vtkNew.h"
7 
12 template <typename ValueT>
13 class MockDataArray : public vtkGenericDataArray<MockDataArray<ValueT>, ValueT>
14 {
16 
17 public:
19  using ValueType = typename Superclass::ValueType;
21  void* GetVoidPointer(vtkIdType idx) override { return this->Buffer->GetBuffer() + idx; }
22  ValueType GetValue(vtkIdType valueIdx) const { return this->Buffer->GetBuffer()[valueIdx]; }
24  {
25  this->Buffer->GetBuffer()[valueIdx] = value;
26  }
27  void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
28  {
29  const vtkIdType valueIdx = tupleIdx * this->NumberOfComponents;
30  std::copy(this->Buffer->GetBuffer() + valueIdx,
31  this->Buffer->GetBuffer() + valueIdx + this->NumberOfComponents, tuple);
32  }
33  void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
34  {
35  const vtkIdType valueIdx = tupleIdx * this->NumberOfComponents;
36  std::copy(tuple, tuple + this->NumberOfComponents, this->Buffer->GetBuffer() + valueIdx);
37  }
38  ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
39  {
40  return this->Buffer->GetBuffer()[this->NumberOfComponents * tupleIdx + compIdx];
41  }
42  void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
43  {
44  const vtkIdType valueIdx = tupleIdx * this->NumberOfComponents + compIdx;
45  this->SetValue(valueIdx, value);
46  }
47 
48 protected:
50  bool ReallocateTuples(vtkIdType numTuples)
51  {
52  if (this->Buffer->Reallocate(numTuples * this->GetNumberOfComponents()))
53  {
54  this->Capacity = this->Buffer->GetSize();
55  return true;
56  }
57  return false;
58  }
59  friend class vtkGenericDataArray<MockDataArray<ValueT>, ValueT>;
60 };
This class is used in some unit tests to setup a mock data array which derives vtkGenericDataArray.
Definition: MockDataArray.h:13
vtkNew< vtkBuffer< ValueT > > Buffer
Definition: MockDataArray.h:49
void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
Definition: MockDataArray.h:42
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
Definition: MockDataArray.h:38
void * GetVoidPointer(vtkIdType idx) override
Return a void pointer.
Definition: MockDataArray.h:21
int vtkIdType
Definition: vtkType.h:363
Base interface for all typed vtkDataArray subclasses.
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Definition: MockDataArray.h:27
ScalarType * GetBuffer()
Access the buffer as a scalar pointer.
Definition: vtkBuffer.h:43
bool ReallocateTuples(vtkIdType numTuples)
Definition: MockDataArray.h:50
#define VTK_STANDARD_NEW_BODY(thisClass)
void SetValue(vtkIdType valueIdx, ValueType value)
Definition: MockDataArray.h:23
vtkTemplateTypeMacro(MockDataArray< ValueT >, GenericDataArrayType)
typename Superclass::ValueType ValueType
Definition: MockDataArray.h:19
Allocate and hold a VTK object.
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Definition: MockDataArray.h:33
static MockDataArray * New()
Definition: MockDataArray.h:20
ValueType GetValue(vtkIdType valueIdx) const
Definition: MockDataArray.h:22