cesium-native  0.41.0
PropertyArrayView.h
1 #pragma once
2 
3 #include "CesiumGltf/PropertyType.h"
4 #include "getOffsetFromOffsetsBuffer.h"
5 
6 #include <CesiumUtility/SpanHelper.h>
7 
8 #include <gsl/span>
9 
10 #include <cstddef>
11 #include <cstring>
12 #include <variant>
13 #include <vector>
14 
15 namespace CesiumGltf {
16 
24 template <typename ElementType> class PropertyArrayView {
25 public:
29  PropertyArrayView() : _values{} {}
30 
36  PropertyArrayView(const gsl::span<const std::byte>& buffer) noexcept
37  : _values{CesiumUtility::reintepretCastSpan<const ElementType>(buffer)} {}
38 
39  const ElementType& operator[](int64_t index) const noexcept {
40  return this->_values[index];
41  }
42 
43  int64_t size() const noexcept { return this->_values.size(); }
44 
45  auto begin() { return this->_values.begin(); }
46  auto end() { return this->_values.end(); }
47  auto begin() const { return this->_values.begin(); }
48  auto end() const { return this->_values.end(); }
49 
50 private:
51  gsl::span<const ElementType> _values;
52 };
53 
64 template <typename ElementType> class PropertyArrayCopy {
65 public:
69  PropertyArrayCopy() : _storage{}, _view() {}
70 
76  PropertyArrayCopy(const std::vector<ElementType>& values) noexcept
77  : _storage(), _view() {
78  size_t numberOfElements = values.size();
79  size_t sizeInBytes = numberOfElements * sizeof(ElementType);
80  this->_storage.resize(sizeInBytes);
81  std::memcpy(
82  this->_storage.data(),
83  reinterpret_cast<const std::byte*>(values.data()),
84  sizeInBytes);
85  this->_view = PropertyArrayView<ElementType>(this->_storage);
86  }
87 
89  PropertyArrayCopy& operator=(PropertyArrayCopy&&) = default;
90 
91  PropertyArrayCopy(std::vector<std::byte>&& buffer) noexcept
92  : _storage(std::move(buffer)), _view(this->_storage) {}
93 
95  : _storage(rhs._storage), _view(this->_storage) {}
96 
97  PropertyArrayCopy& operator=(const PropertyArrayCopy& rhs) {
98  this->_storage = rhs._storage;
99  this->_view = PropertyArrayView<ElementType>(this->_storage);
100  return *this;
101  }
102 
103  const ElementType& operator[](int64_t index) const noexcept {
104  return this->_view[index];
105  }
106 
107  int64_t size() const noexcept { return this->_view.size(); }
108 
109  auto begin() { return this->_view.begin(); }
110  auto end() { return this->_view.end(); }
111  auto begin() const { return this->_view.begin(); }
112  auto end() const { return this->_view.end(); }
113 
114  const PropertyArrayView<ElementType>& view() const { return this->_view; }
115 
116  PropertyArrayView<ElementType>
117  toViewAndExternalBuffer(std::vector<std::byte>& outBuffer) && {
118  outBuffer = std::move(this->_storage);
119  PropertyArrayView<ElementType> result = std::move(this->_view);
120  this->_view = PropertyArrayView<ElementType>();
121  return result;
122  }
123 
124 private:
125  std::vector<std::byte> _storage;
126  PropertyArrayView<ElementType> _view;
127 };
128 
129 template <> class PropertyArrayView<bool> {
130 public:
134  PropertyArrayView() : _values{}, _bitOffset{0}, _size{0} {}
135 
145  const gsl::span<const std::byte>& buffer,
146  int64_t bitOffset,
147  int64_t size) noexcept
148  : _values{buffer}, _bitOffset{bitOffset}, _size{size} {}
149 
150  bool operator[](int64_t index) const noexcept {
151  index += _bitOffset;
152  const int64_t byteIndex = index / 8;
153  const int64_t bitIndex = index % 8;
154  const int bitValue = static_cast<int>(_values[byteIndex] >> bitIndex) & 1;
155  return bitValue == 1;
156  }
157 
158  int64_t size() const noexcept { return _size; }
159 
160 private:
161  gsl::span<const std::byte> _values;
162  int64_t _bitOffset;
163  int64_t _size;
164 };
165 
166 template <> class PropertyArrayView<std::string_view> {
167 public:
172  : _values{},
173  _stringOffsets{},
174  _stringOffsetType{PropertyComponentType::None},
175  _size{0} {}
176 
186  const gsl::span<const std::byte>& values,
187  const gsl::span<const std::byte>& stringOffsets,
188  PropertyComponentType stringOffsetType,
189  int64_t size) noexcept
190  : _values{values},
191  _stringOffsets{stringOffsets},
192  _stringOffsetType{stringOffsetType},
193  _size{size} {}
194 
195  std::string_view operator[](int64_t index) const noexcept {
196  const size_t currentOffset =
197  getOffsetFromOffsetsBuffer(index, _stringOffsets, _stringOffsetType);
198  const size_t nextOffset = getOffsetFromOffsetsBuffer(
199  index + 1,
200  _stringOffsets,
201  _stringOffsetType);
202  return std::string_view(
203  reinterpret_cast<const char*>(_values.data() + currentOffset),
204  (nextOffset - currentOffset));
205  }
206 
207  int64_t size() const noexcept { return _size; }
208 
209 private:
210  gsl::span<const std::byte> _values;
211  gsl::span<const std::byte> _stringOffsets;
212  PropertyComponentType _stringOffsetType;
213  int64_t _size;
214 };
215 
216 template <typename T>
217 bool operator==(
218  const PropertyArrayView<T>& lhs,
219  const PropertyArrayView<T>& rhs) {
220  int64_t size = lhs.size();
221  if (size != rhs.size()) {
222  return false;
223  }
224 
225  for (int64_t i = 0; i < size; ++i) {
226  if (lhs[i] != rhs[i]) {
227  return false;
228  }
229  }
230 
231  return true;
232 }
233 
234 template <typename T>
235 bool operator==(
236  const PropertyArrayView<T>& lhs,
237  const PropertyArrayCopy<T>& rhs) {
238  return lhs == PropertyArrayView(rhs);
239 }
240 
241 template <typename T>
242 bool operator==(
243  const PropertyArrayCopy<T>& lhs,
244  const PropertyArrayView<T>& rhs) {
245  return lhs.view() == rhs;
246 }
247 
248 template <typename T>
249 bool operator==(
250  const PropertyArrayCopy<T>& lhs,
251  const PropertyArrayCopy<T>& rhs) {
252  return lhs.view() == rhs.view();
253 }
254 
255 template <typename T>
256 bool operator!=(
257  const PropertyArrayView<T>& lhs,
258  const PropertyArrayView<T>& rhs) {
259  return !(lhs == rhs);
260 }
261 
262 template <typename T>
263 bool operator!=(
264  const PropertyArrayView<T>& lhs,
265  const PropertyArrayCopy<T>& rhs) {
266  return !(lhs == rhs);
267 }
268 
269 template <typename T>
270 bool operator!=(
271  const PropertyArrayCopy<T>& lhs,
272  const PropertyArrayView<T>& rhs) {
273  return !(lhs == rhs);
274 }
275 
276 template <typename T>
277 bool operator!=(
278  const PropertyArrayCopy<T>& lhs,
279  const PropertyArrayCopy<T>& rhs) {
280  return !(lhs == rhs);
281 }
282 
283 } // namespace CesiumGltf
A copy of an array element of a PropertyTableProperty or PropertyTextureProperty.
PropertyArrayCopy(const std::vector< ElementType > &values) noexcept
Constructs an array view from a buffer.
PropertyArrayCopy()
Constructs an empty array view.
PropertyArrayView()
Constructs an empty array view.
PropertyArrayView(const gsl::span< const std::byte > &buffer, int64_t bitOffset, int64_t size) noexcept
Constructs an array view from a buffer.
PropertyArrayView(const gsl::span< const std::byte > &values, const gsl::span< const std::byte > &stringOffsets, PropertyComponentType stringOffsetType, int64_t size) noexcept
Constructs an array view from buffers and their information.
PropertyArrayView()
Constructs an empty array view.
A view on an array element of a PropertyTableProperty or PropertyTextureProperty.
PropertyArrayView(const gsl::span< const std::byte > &buffer) noexcept
Constructs an array view from a buffer.
PropertyArrayView()
Constructs an empty array view.
Classes for working with glTF models.