VTK  9.6.20260331
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"
6#include "vtkNew.h"
7
12template <typename ValueT>
13class MockDataArray : public vtkGenericDataArray<MockDataArray<ValueT>, ValueT>
14{
15 using GenericDataArrayType = vtkGenericDataArray<MockDataArray<ValueT>, ValueT>;
16
17public:
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]; }
23 void SetValue(vtkIdType valueIdx, ValueType value)
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
48protected:
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.
vtkTemplateTypeMacro(MockDataArray< ValueT >, GenericDataArrayType)
void * GetVoidPointer(vtkIdType idx) override
Return a void pointer.
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
vtkNew< vtkBuffer< ValueT > > Buffer
void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
ValueType GetValue(vtkIdType valueIdx) const
void SetValue(vtkIdType valueIdx, ValueType value)
bool ReallocateTuples(vtkIdType numTuples)
typename Superclass::ValueType ValueType
static MockDataArray * New()
Base interface for all typed vtkDataArray subclasses.
Allocate and hold a VTK object.
Definition vtkNew.h:168
#define VTK_STANDARD_NEW_BODY(thisClass)
int vtkIdType
Definition vtkType.h:363