VTK  9.5.20251109
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
11template <typename ValueT>
12class MockDataArray : public vtkGenericDataArray<MockDataArray<ValueT>, ValueT>
13{
15
16public:
18 using ValueType = typename Superclass::ValueType;
20 void* GetVoidPointer(vtkIdType idx) override { return this->Buffer->GetBuffer() + idx; }
21 ValueType GetValue(vtkIdType valueIdx) const { return this->Buffer->GetBuffer()[valueIdx]; }
22 void SetValue(vtkIdType valueIdx, ValueType value)
23 {
24 this->Buffer->GetBuffer()[valueIdx] = value;
25 }
26 void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
27 {
28 const vtkIdType valueIdx = tupleIdx * this->NumberOfComponents;
29 std::copy(this->Buffer->GetBuffer() + valueIdx,
30 this->Buffer->GetBuffer() + valueIdx + this->NumberOfComponents, tuple);
31 }
32 void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
33 {
34 const vtkIdType valueIdx = tupleIdx * this->NumberOfComponents;
35 std::copy(tuple, tuple + this->NumberOfComponents, this->Buffer->GetBuffer() + valueIdx);
36 }
37 ValueType GetTypedComponent(vtkIdType tupleIdx, int compIdx) const
38 {
39 return this->Buffer->GetBuffer()[this->NumberOfComponents * tupleIdx + compIdx];
40 }
41 void SetTypedComponent(vtkIdType tupleIdx, int compIdx, ValueType value)
42 {
43 const vtkIdType valueIdx = tupleIdx * this->NumberOfComponents + compIdx;
44 this->SetValue(valueIdx, value);
45 }
46
47protected:
49 bool AllocateTuples(vtkIdType numTuples)
50 {
51 vtkIdType numValues = numTuples * this->GetNumberOfComponents();
52 if (this->Buffer->Allocate(numValues))
53 {
54 this->Size = this->Buffer->GetSize();
55 return true;
56 }
57 return false;
58 }
60 {
61 if (this->Buffer->Reallocate(numTuples * this->GetNumberOfComponents()))
62 {
63 this->Size = this->Buffer->GetSize();
64 return true;
65 }
66 return false;
67 }
68 friend class vtkGenericDataArray<MockDataArray<ValueT>, ValueT>;
69};
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.
ScalarType * GetBuffer()
Access the buffer as a scalar pointer.
Definition vtkBuffer.h:41
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:367