1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#ifndef vtkXMLDataHeaderPrivate_DoNotInclude
#error "do not include unless you know what you are doing"
#endif
#ifndef vtkXMLDataHeaderPrivate_h
#define vtkXMLDataHeaderPrivate_h
#include "vtkType.h"
#include <vector>
// Abstract interface using type vtkTypeUInt64 to access an array
// of either vtkTypeUInt32 or vtkTypeUInt64. Shared by vtkXMLWriter
// and vtkXMLDataParser to write/read binary data headers.
VTK_ABI_NAMESPACE_BEGIN
class vtkXMLDataHeader
{
public:
virtual void Resize(size_t count) = 0;
virtual vtkTypeUInt64 Get(size_t index) const = 0;
virtual bool Set(size_t index, vtkTypeUInt64 value) = 0;
virtual size_t WordSize() const = 0;
virtual size_t WordCount() const = 0;
virtual unsigned char* Data() = 0;
size_t DataSize() const { return this->WordCount() * this->WordSize(); }
virtual ~vtkXMLDataHeader() = default;
static inline vtkXMLDataHeader* New(int width, size_t count);
};
template <typename T>
class vtkXMLDataHeaderImpl : public vtkXMLDataHeader
{
std::vector<T> Header;
public:
vtkXMLDataHeaderImpl(size_t n)
: Header(n, 0)
{
}
void Resize(size_t count) override { this->Header.resize(count, 0); }
vtkTypeUInt64 Get(size_t index) const override { return this->Header[index]; }
bool Set(size_t index, vtkTypeUInt64 value) override
{
this->Header[index] = T(value);
return vtkTypeUInt64(this->Header[index]) == value;
}
size_t WordSize() const override { return sizeof(T); }
size_t WordCount() const override { return this->Header.size(); }
unsigned char* Data() override { return reinterpret_cast<unsigned char*>(&this->Header[0]); }
};
vtkXMLDataHeader* vtkXMLDataHeader::New(int width, size_t count)
{
switch (width)
{
case 32:
return new vtkXMLDataHeaderImpl<vtkTypeUInt32>(count);
case 64:
return new vtkXMLDataHeaderImpl<vtkTypeUInt64>(count);
}
return nullptr;
}
VTK_ABI_NAMESPACE_END
#endif
// VTK-HeaderTest-Exclude: vtkXMLDataHeaderPrivate.h
|