VRS
A file format for sensor data.
Loading...
Searching...
No Matches
DataPieceArray.h
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#ifndef DATA_PIECES_ARRAY_H
20#define DATA_PIECES_ARRAY_H
21
22#ifndef DATA_PIECES_H
23#include "DataPieces.h"
24#endif
25
26namespace vrs {
27
28using std::map;
29using std::min;
30using std::ostream;
31using std::string;
32using std::vector;
33
38template <typename T>
39class DataPieceArray : public DataPiece {
40 static_assert(std::is_trivially_copyable<T>::value, "DataPieceArray only supports POD types.");
41
42 public:
45 DataPieceArray(string label, size_t count)
46 : DataPiece(std::move(label), DataPieceType::Array, sizeof(T) * count), count_(count) {}
50 template <size_t n>
51 DataPieceArray(string label, size_t count, const T (&defaultValues)[n])
52 : DataPiece(std::move(label), DataPieceType::Array, sizeof(T) * count), count_(count) {
53 setDefault(defaultValues, n);
54 }
57 explicit DataPieceArray(const MakerBundle& bundle);
58
60 size_t getArraySize() const {
61 return count_;
62 }
63
66 const string& getElementTypeName() const override {
67 return vrs::getTypeName<T>();
68 }
71 size_t getVariableSize() const override {
72 return 0;
73 }
76 size_t collectVariableData(int8_t*, size_t) override {
77 return 0;
78 }
79
88 bool get(T* outValues, size_t count) const {
89 const size_t bytesRequested = sizeof(T) * count;
90 const T* const ptr =
91 (count <= count_) ? layout_.getFixedData<T>(offset_, bytesRequested) : nullptr;
92 if (ptr != nullptr && bytesRequested > 0) {
93 memcpy(outValues, ptr, bytesRequested);
94 return true;
95 }
96 const size_t defaultReadCount = min<size_t>(count, defaultValues_.size());
97 if (defaultReadCount > 0) {
98 memcpy(outValues, defaultValues_.data(), defaultReadCount * sizeof(T));
99 }
100 // blank values set set by default values
101 for (size_t k = defaultValues_.size(); k < count; k++) {
102 outValues[k] = T{};
103 }
104 return false;
105 }
111 bool get(T& outValue, size_t index) const {
112 const size_t bytesRequested = sizeof(T) * (index + 1);
113 const T* const ptr =
114 (index < count_) ? layout_.getFixedData<T>(offset_, bytesRequested) : nullptr;
115 if (ptr != nullptr) {
116 outValue = readUnaligned<T>(ptr + index);
117 return true;
118 }
119 if (index < defaultValues_.size()) {
120 outValue = defaultValues_[index];
121 } else {
122 outValue = T{};
123 }
124 return false;
125 }
129 bool get(vector<T>& outValues) const {
130 const T* const ptr = layout_.getFixedData<T>(offset_, getFixedSize());
131 if (ptr != nullptr) {
132 outValues.resize(count_);
133 memcpy(outValues.data(), ptr, getFixedSize());
134 return true;
135 }
136 outValues = defaultValues_;
137 return false;
138 }
147 bool set(const T* values, size_t count) {
148 T* const ptr = layout_.getFixedData<T>(offset_, getFixedSize());
149 if (ptr != nullptr) {
150 if (count > 0) {
151 memcpy(ptr, values, sizeof(T) * min<size_t>(count, count_));
152 }
153 T v{};
154 while (count < count_) {
155 writeUnaligned<T>(ptr + count++, v);
156 }
157 return true;
158 } else {
159 return false;
160 }
161 }
167 bool set(const T& value, size_t index) {
168 T* const ptr = layout_.getFixedData<T>(offset_, getFixedSize());
169 if (ptr != nullptr && index < count_) {
170 writeUnaligned<T>(ptr + index, value);
171 return true;
172 }
173 return false;
174 }
182 template <size_t n>
183 bool set(const T (&arr)[n]) {
184 return set(arr, n);
185 }
193 bool set(const vector<T>& values) {
194 return values.empty() ? set(nullptr, 0) : set(values.data(), values.size());
195 }
196
204 void setDefault(const T* defaultValues, size_t count) {
205 defaultValues_.resize(count_);
206 size_t copySize = min(count, count_) * sizeof(T);
207 if (copySize > 0) {
208 memcpy(defaultValues_.data(), defaultValues, copySize);
209 }
210 for (; count < count_; count++) {
211 defaultValues_[count] = T{};
212 }
213 }
220 template <size_t n>
221 void setDefault(const T (&arr)[n]) {
222 setDefault(arr, n);
223 }
230 void setDefault(const vector<T>& values) {
231 setDefault(values.data(), values.size());
232 }
235 const vector<T>& getDefault() const {
236 return defaultValues_;
237 }
239 void initToDefault() override {
240 set(defaultValues_);
241 }
242
246 void setProperty(const string& propertyName, T value) {
247 properties_[propertyName] = value;
248 }
253 bool getProperty(const string& propertyName, T& outValue) const {
254 auto iter = properties_.find(propertyName);
255 if (iter != properties_.end()) {
256 outValue = iter->second;
257 return true;
258 }
259 return false;
260 }
261
266 void setMin(T min) {
267 properties_[kMinValue] = min;
268 }
273 void setMax(T max) {
274 properties_[kMaxValue] = max;
275 }
281 void setRange(T min, T max) {
282 properties_[kMinValue] = min;
283 properties_[kMaxValue] = max;
284 }
288 bool getMin(T& outMin) const {
289 return getProperty(kMinValue, outMin);
290 }
294 bool getMax(T& outMax) const {
295 return getProperty(kMinValue, outMax);
296 }
297
302 bool patchValue(const T* values, size_t count) const {
303 auto* patchedPiece = layout_.getMappedPiece<DataPieceArray<T>>(pieceIndex_);
304 return patchedPiece != nullptr && patchedPiece->set(values, count);
305 }
306
309 bool isAvailable() const override {
310 return layout_.getFixedData<T>(offset_, getFixedSize()) != nullptr;
311 }
312
317 void print(ostream& out, const string& indent) const override;
322 void printCompact(ostream& out, const string& indent) const override;
323
328 bool isSame(const DataPiece* rhs) const override;
329
333 void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile) override;
334
338 unique_ptr<DataPiece> clone() const override {
339 auto other = std::make_unique<DataPieceArray<T>>(getLabel(), getArraySize());
340 other->tags_ = tags_;
341 other->required_ = required_;
342 other->properties_ = properties_;
343 other->defaultValues_ = defaultValues_;
344 return other;
345 }
346
347 protected:
348 bool copyFrom(const DataPiece* original) override {
349 const auto* source = reinterpret_cast<const DataPieceArray<T>*>(original);
350 vector<T> values;
351 bool retrieved = source->get(values);
352 set(values);
353 return retrieved;
354 }
355
356 private:
357 const size_t count_{};
358 map<string, T> properties_;
359 vector<T> defaultValues_;
360};
361
362} // namespace vrs
363
364#endif // DATA_PIECES_ARRAY_H
vector< int8_t > & getFixedData()
Definition DataLayout.h:256
T * getMappedPiece(size_t pieceIndex) const
Get a typed piece by index in the mapped datalayout, exclusively.
Definition DataLayout.h:524
Fixed size array of POD values.
Definition DataPieceArray.h:39
const vector< T > & getDefault() const
Definition DataPieceArray.h:235
bool get(T *outValues, size_t count) const
Definition DataPieceArray.h:88
bool set(const T &value, size_t index)
Definition DataPieceArray.h:167
void printCompact(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1334
void setProperty(const string &propertyName, T value)
Definition DataPieceArray.h:246
void serialize(JsonWrapper &jsonWrapper, const JsonFormatProfileSpec &profile) override
Definition DataLayout.cpp:1362
size_t collectVariableData(int8_t *, size_t) override
Definition DataPieceArray.h:76
const string & getElementTypeName() const override
Definition DataPieceArray.h:66
bool getMin(T &outMin) const
Definition DataPieceArray.h:288
void initToDefault() override
Initialize to default value.
Definition DataPieceArray.h:239
bool copyFrom(const DataPiece *original) override
Set or stage value from another piece known to be of the same type.
Definition DataPieceArray.h:348
bool isAvailable() const override
Definition DataPieceArray.h:309
bool set(const T(&arr)[n])
Definition DataPieceArray.h:183
DataPieceArray(string label, size_t count)
Definition DataPieceArray.h:45
bool set(const T *values, size_t count)
Definition DataPieceArray.h:147
bool patchValue(const T *values, size_t count) const
Definition DataPieceArray.h:302
void print(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1302
size_t getArraySize() const
Get the size of the array.
Definition DataPieceArray.h:60
bool getMax(T &outMax) const
Definition DataPieceArray.h:294
bool set(const vector< T > &values)
Definition DataPieceArray.h:193
void setRange(T min, T max)
Definition DataPieceArray.h:281
void setDefault(const T(&arr)[n])
Definition DataPieceArray.h:221
void setMax(T max)
Definition DataPieceArray.h:273
unique_ptr< DataPiece > clone() const override
Definition DataPieceArray.h:338
bool get(vector< T > &outValues) const
Definition DataPieceArray.h:129
void setDefault(const vector< T > &values)
Definition DataPieceArray.h:230
size_t getVariableSize() const override
Definition DataPieceArray.h:71
void setDefault(const T *defaultValues, size_t count)
Definition DataPieceArray.h:204
void setMin(T min)
Definition DataPieceArray.h:266
bool isSame(const DataPiece *rhs) const override
Definition DataLayout.cpp:1352
DataPieceArray(string label, size_t count, const T(&defaultValues)[n])
Definition DataPieceArray.h:51
bool getProperty(const string &propertyName, T &outValue) const
Definition DataPieceArray.h:253
bool get(T &outValue, size_t index) const
Definition DataPieceArray.h:111
Abstract class representing a piece of information part of a DataLayout.
Definition DataPieces.h:40
static const string kMaxValue
Special property name for the maximum value of the DataPiece.
Definition DataPieces.h:61
static const string kMinValue
Special property name for the minimum value of the DataPiece.
Definition DataPieces.h:59
const string & getLabel() const
Definition DataPieces.h:76
size_t getFixedSize() const
Definition DataPieces.h:114
Definition Compressor.cpp:113
DataPieceType
Specifier for a type of DataPiece.
Definition DataLayout.h:57
@ Array
Fixed size array.
When printing out a DataLayout as json, this class allows to specify what should be included in the g...
Definition DataLayout.h:84