cesium-native 0.43.0
Loading...
Searching...
No Matches
PropertyArrayView.h
1#pragma once
2
3#include "CesiumGltf/PropertyType.h"
4#include "getOffsetFromOffsetsBuffer.h"
5
6#include <CesiumUtility/SpanHelper.h>
7
8#include <cstddef>
9#include <cstring>
10#include <span>
11#include <variant>
12#include <vector>
13
14namespace CesiumGltf {
15
23template <typename ElementType> class PropertyArrayView {
24public:
28 PropertyArrayView() : _values{} {}
29
35 PropertyArrayView(const std::span<const std::byte>& buffer) noexcept
37
41 const ElementType& operator[](int64_t index) const noexcept {
42 return this->_values[index];
43 }
44
48 int64_t size() const noexcept { return this->_values.size(); }
49
53 auto begin() { return this->_values.begin(); }
57 auto end() { return this->_values.end(); }
59 auto begin() const { return this->_values.begin(); }
61 auto end() const { return this->_values.end(); }
62
63private:
64 std::span<const ElementType> _values;
65};
66
77template <typename ElementType> class PropertyArrayCopy {
78public:
82 PropertyArrayCopy() : _storage{}, _view() {}
83
89 PropertyArrayCopy(const std::vector<ElementType>& values) noexcept
90 : _storage(), _view() {
91 size_t numberOfElements = values.size();
92 size_t sizeInBytes = numberOfElements * sizeof(ElementType);
93 this->_storage.resize(sizeInBytes);
94 std::memcpy(
95 this->_storage.data(),
96 reinterpret_cast<const std::byte*>(values.data()),
97 sizeInBytes);
98 this->_view = PropertyArrayView<ElementType>(this->_storage);
99 }
100
105
108 PropertyArrayCopy(std::vector<std::byte>&& buffer) noexcept
109 : _storage(std::move(buffer)), _view(this->_storage) {}
110
113 : _storage(rhs._storage), _view(this->_storage) {}
114
117 this->_storage = rhs._storage;
118 this->_view = PropertyArrayView<ElementType>(this->_storage);
119 return *this;
120 }
121
128 const ElementType& operator[](int64_t index) const noexcept {
129 return this->_view[index];
130 }
131
133 int64_t size() const noexcept { return this->_view.size(); }
134
136 auto begin() { return this->_view.begin(); }
138 auto end() { return this->_view.end(); }
140 auto begin() const { return this->_view.begin(); }
142 auto end() const { return this->_view.end(); }
143
147 const PropertyArrayView<ElementType>& view() const { return this->_view; }
148
157 toViewAndExternalBuffer(std::vector<std::byte>& outBuffer) && {
158 outBuffer = std::move(this->_storage);
159 PropertyArrayView<ElementType> result = std::move(this->_view);
160 this->_view = PropertyArrayView<ElementType>();
161 return result;
162 }
163
164private:
165 std::vector<std::byte> _storage;
167};
168
176template <> class PropertyArrayView<bool> {
177public:
181 PropertyArrayView() : _values{}, _bitOffset{0}, _size{0} {}
182
192 const std::span<const std::byte>& buffer,
193 int64_t bitOffset,
194 int64_t size) noexcept
195 : _values{buffer}, _bitOffset{bitOffset}, _size{size} {}
196
200 bool operator[](int64_t index) const noexcept {
201 index += _bitOffset;
202 const int64_t byteIndex = index / 8;
203 const int64_t bitIndex = index % 8;
204 const int bitValue = static_cast<int>(_values[byteIndex] >> bitIndex) & 1;
205 return bitValue == 1;
206 }
207
211 int64_t size() const noexcept { return _size; }
212
213private:
214 std::span<const std::byte> _values;
215 int64_t _bitOffset;
216 int64_t _size;
217};
218
226template <> class PropertyArrayView<std::string_view> {
227public:
232 : _values{},
233 _stringOffsets{},
234 _stringOffsetType{PropertyComponentType::None},
235 _size{0} {}
236
246 const std::span<const std::byte>& values,
247 const std::span<const std::byte>& stringOffsets,
248 PropertyComponentType stringOffsetType,
249 int64_t size) noexcept
250 : _values{values},
251 _stringOffsets{stringOffsets},
252 _stringOffsetType{stringOffsetType},
253 _size{size} {}
254
258 std::string_view operator[](int64_t index) const noexcept {
259 const size_t currentOffset =
260 getOffsetFromOffsetsBuffer(index, _stringOffsets, _stringOffsetType);
261 const size_t nextOffset = getOffsetFromOffsetsBuffer(
262 index + 1,
263 _stringOffsets,
264 _stringOffsetType);
265 return std::string_view(
266 reinterpret_cast<const char*>(_values.data() + currentOffset),
267 (nextOffset - currentOffset));
268 }
269
273 int64_t size() const noexcept { return _size; }
274
275private:
276 std::span<const std::byte> _values;
277 std::span<const std::byte> _stringOffsets;
278 PropertyComponentType _stringOffsetType;
279 int64_t _size;
280};
281
285template <typename T>
287 const PropertyArrayView<T>& lhs,
288 const PropertyArrayView<T>& rhs) {
289 int64_t size = lhs.size();
290 if (size != rhs.size()) {
291 return false;
292 }
293
294 for (int64_t i = 0; i < size; ++i) {
295 if (lhs[i] != rhs[i]) {
296 return false;
297 }
298 }
299
300 return true;
301}
302
305template <typename T>
307 const PropertyArrayView<T>& lhs,
308 const PropertyArrayCopy<T>& rhs) {
309 return lhs == PropertyArrayView(rhs);
310}
311
314template <typename T>
316 const PropertyArrayCopy<T>& lhs,
317 const PropertyArrayView<T>& rhs) {
318 return lhs.view() == rhs;
319}
320
323template <typename T>
325 const PropertyArrayCopy<T>& lhs,
326 const PropertyArrayCopy<T>& rhs) {
327 return lhs.view() == rhs.view();
328}
329
333template <typename T>
335 const PropertyArrayView<T>& lhs,
336 const PropertyArrayView<T>& rhs) {
337 return !(lhs == rhs);
338}
339
343template <typename T>
345 const PropertyArrayView<T>& lhs,
346 const PropertyArrayCopy<T>& rhs) {
347 return !(lhs == rhs);
348}
349
353template <typename T>
355 const PropertyArrayCopy<T>& lhs,
356 const PropertyArrayView<T>& rhs) {
357 return !(lhs == rhs);
358}
359
363template <typename T>
365 const PropertyArrayCopy<T>& lhs,
366 const PropertyArrayCopy<T>& rhs) {
367 return !(lhs == rhs);
368}
369
370} // namespace CesiumGltf
A copy of an array element of a PropertyTableProperty or PropertyTextureProperty.
PropertyArrayCopy & operator=(const PropertyArrayCopy &rhs)
Copy assignment operator.
PropertyArrayCopy(const std::vector< ElementType > &values) noexcept
Constructs an array view from a buffer.
auto begin() const
The begin iterator.
auto end() const
The end iterator.
auto begin()
The begin iterator.
PropertyArrayCopy & operator=(PropertyArrayCopy &&)=default
Default move assignment operator.
PropertyArrayCopy(std::vector< std::byte > &&buffer) noexcept
Creates a new PropertyArrayCopy directly from a buffer of bytes, which will be moved into this copy.
PropertyArrayCopy(const PropertyArrayCopy &rhs)
Copy constructor.
auto end()
The end iterator.
int64_t size() const noexcept
The number of elements in this array.
const ElementType & operator[](int64_t index) const noexcept
Returns the ElementType at the given index from this copy.
const PropertyArrayView< ElementType > & view() const
Obtains a PropertyArrayView over the contents of this copy.
PropertyArrayCopy(PropertyArrayCopy &&)=default
Default move constructor.
PropertyArrayCopy()
Constructs an empty array view.
PropertyArrayView< ElementType > toViewAndExternalBuffer(std::vector< std::byte > &outBuffer) &&
Obtains a buffer and view from the copied data, leaving this PropertyArrayCopy empty.
PropertyArrayView()
Constructs an empty array view.
PropertyArrayView(const std::span< const std::byte > &buffer, int64_t bitOffset, int64_t size) noexcept
Constructs an array view from a buffer.
bool operator[](int64_t index) const noexcept
Obtains the element in the array at the given index.
int64_t size() const noexcept
The number of entries in the array.
std::string_view operator[](int64_t index) const noexcept
Obtains an std::string_view for the element at the given index.
PropertyArrayView(const std::span< const std::byte > &values, const std::span< const std::byte > &stringOffsets, PropertyComponentType stringOffsetType, int64_t size) noexcept
Constructs an array view from buffers and their information.
int64_t size() const noexcept
The number of elements in this array.
PropertyArrayView()
Constructs an empty array view.
A view on an array element of a PropertyTableProperty or PropertyTextureProperty.
auto end()
The end iterator.
const ElementType & operator[](int64_t index) const noexcept
Accesses the element of this array at the given index.
auto end() const
The end iterator.
PropertyArrayView(const std::span< const std::byte > &buffer) noexcept
Constructs an array view from a buffer.
PropertyArrayView()
Constructs an empty array view.
auto begin() const
The begin iterator.
auto begin()
The begin iterator.
int64_t size() const noexcept
The number of elements in this array.
Classes for working with glTF models.
PropertyComponentType
The possible types of a property component.
bool operator==(const PropertyArrayView< T > &lhs, const PropertyArrayView< T > &rhs)
Compares two PropertyArrayView instances by comparing their values. If the two arrays aren't the same...
bool operator!=(const PropertyArrayView< T > &lhs, const PropertyArrayView< T > &rhs)
Compares two PropertyArrayView instances and returns the inverse.
std::span< To > reintepretCastSpan(const std::span< From > &from) noexcept
This function converts between span types. This function has the same rules with C++ reintepret_cast ...
Definition SpanHelper.h:13
STL namespace.