VTK  9.6.20260310
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:
50 bool AllocateTuples(vtkIdType numTuples)
51 {
52 vtkIdType numValues = numTuples * this->GetNumberOfComponents();
53 if (this->Buffer->Allocate(numValues))
54 {
55 this->Size = this->Buffer->GetSize();
56 return true;
57 }
58 return false;
59 }
61 {
62 if (this->Buffer->Reallocate(numTuples * this->GetNumberOfComponents()))
63 {
64 this->Size = this->Buffer->GetSize();
65 return true;
66 }
67 return false;
68 }
69 friend class vtkGenericDataArray<MockDataArray<ValueT>, ValueT>;
70};
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
bool AllocateTuples(vtkIdType numTuples)
static MockDataArray * New()
int GetNumberOfComponents() const
Set/Get the dimension (n) of the components.
Base interface for all typed vtkDataArray subclasses.
Allocate and hold a VTK object.
Definition vtkNew.h:167
#define VTK_STANDARD_NEW_BODY(thisClass)
int vtkIdType
Definition vtkType.h:363