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
199 const vector<T>& getDefault() const {
200 return defaultValues_;
201 }
202
208 void setDefault(const T* defaultValues, size_t count) {
209 defaultValues_.resize(count_);
210 size_t copySize = min(count, count_) * sizeof(T);
211 if (copySize > 0) {
212 memcpy(defaultValues_.data(), defaultValues, copySize);
213 }
214 for (; count < count_; count++) {
215 defaultValues_[count] = T{};
216 }
217 }
222 template <size_t n>
223 void setDefault(const T (&arr)[n]) {
224 setDefault(arr, n);
225 }
230 void setDefault(const vector<T>& values) {
231 setDefault(values.data(), values.size());
232 }
233
235 void initToDefault() override {
236 set(defaultValues_);
237 }
238
242 void setProperty(const string& propertyName, T value) {
243 properties_[propertyName] = value;
244 }
249 bool getProperty(const string& propertyName, T& outValue) const {
250 auto iter = properties_.find(propertyName);
251 if (iter != properties_.end()) {
252 outValue = iter->second;
253 return true;
254 }
255 return false;
256 }
257
262 void setMin(T min) {
263 properties_[kMinValue] = min;
264 }
269 void setMax(T max) {
270 properties_[kMaxValue] = max;
271 }
277 void setRange(T min, T max) {
278 properties_[kMinValue] = min;
279 properties_[kMaxValue] = max;
280 }
284 bool getMin(T& outMin) const {
285 return getProperty(kMinValue, outMin);
286 }
290 bool getMax(T& outMax) const {
291 return getProperty(kMinValue, outMax);
292 }
293
298 bool patchValue(const T* values, size_t count) const {
299 auto* patchedPiece = layout_.getMappedPiece<DataPieceArray<T>>(pieceIndex_);
300 return patchedPiece != nullptr && patchedPiece->set(values, count);
301 }
302
305 bool isAvailable() const override {
306 return layout_.getFixedData<T>(offset_, getFixedSize()) != nullptr;
307 }
308
313 void print(ostream& out, const string& indent) const override;
318 void printCompact(ostream& out, const string& indent) const override;
319
324 bool isSame(const DataPiece* rhs) const override;
325
329 void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile) override;
330
334 unique_ptr<DataPiece> clone() const override {
335 auto other = std::make_unique<DataPieceArray<T>>(getLabel(), getArraySize());
336 other->tags_ = tags_;
337 other->required_ = required_;
338 other->properties_ = properties_;
339 other->defaultValues_ = defaultValues_;
340 return other;
341 }
342
343 protected:
344 bool copyFrom(const DataPiece* original) override {
345 const auto* source = reinterpret_cast<const DataPieceArray<T>*>(original);
346 vector<T> values;
347 bool retrieved = source->get(values);
348 set(values);
349 return retrieved;
350 }
351
352 private:
353 const size_t count_{};
354 map<string, T> properties_;
355 vector<T> defaultValues_;
356};
357
358} // namespace vrs
359
360#endif // DATA_PIECES_ARRAY_H
vector< int8_t > & getFixedData()
Definition DataLayout.h:255
T * getMappedPiece(size_t pieceIndex) const
Get a typed piece by index in the mapped datalayout, exclusively.
Definition DataLayout.h:523
Fixed size array of POD values.
Definition DataPieceArray.h:39
const vector< T > & getDefault() const
Definition DataPieceArray.h:199
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:242
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:284
void initToDefault() override
Initialize to default value.
Definition DataPieceArray.h:235
bool copyFrom(const DataPiece *original) override
Set or stage value from another piece known to be of the same type.
Definition DataPieceArray.h:344
bool isAvailable() const override
Definition DataPieceArray.h:305
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:298
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:290
bool set(const vector< T > &values)
Definition DataPieceArray.h:193
void setRange(T min, T max)
Definition DataPieceArray.h:277
void setDefault(const T(&arr)[n])
Definition DataPieceArray.h:223
void setMax(T max)
Definition DataPieceArray.h:269
unique_ptr< DataPiece > clone() const override
Definition DataPieceArray.h:334
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:208
void setMin(T min)
Definition DataPieceArray.h:262
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:249
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