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(const string& label, size_t count)
46 : DataPiece(label, DataPieceType::Array, sizeof(T) * count), count_(count) {}
50 template <size_t n>
51 DataPieceArray(const string& label, size_t count, const T (&defaultValues)[n])
52 : DataPiece(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 memcpy(ptr, values, sizeof(T) * min<size_t>(count, count_));
151 while (count < count_) {
152 writeUnaligned<T>(ptr + count++, T{});
153 }
154 return true;
155 } else {
156 return false;
157 }
158 }
164 bool set(const T& value, size_t index) {
165 T* const ptr = layout_.getFixedData<T>(offset_, getFixedSize());
166 if (ptr != nullptr && index < count_) {
167 writeUnaligned<T>(ptr + index, value);
168 return true;
169 }
170 return false;
171 }
179 template <size_t n>
180 bool set(const T (&arr)[n]) {
181 return set(arr, n);
182 }
190 bool set(const vector<T>& values) {
191 return set(values.data(), values.size());
192 }
193
196 const vector<T>& getDefault() const {
197 return defaultValues_;
198 }
199
205 void setDefault(const T* defaultValues, size_t count) {
206 defaultValues_.resize(count_);
207 size_t copySize = min(count, count_) * sizeof(T);
208 if (copySize > 0) {
209 memcpy(defaultValues_.data(), defaultValues, copySize);
210 }
211 for (; count < count_; count++) {
212 defaultValues_[count] = T{};
213 }
214 }
219 template <size_t n>
220 void setDefault(const T (&arr)[n]) {
221 setDefault(arr, n);
222 }
227 void setDefault(const vector<T>& values) {
228 setDefault(values.data(), values.size());
229 }
230
234 void setProperty(const string& propertyName, T value) {
235 properties_[propertyName] = value;
236 }
241 bool getProperty(const string& propertyName, T& outValue) const {
242 auto iter = properties_.find(propertyName);
243 if (iter != properties_.end()) {
244 outValue = iter->second;
245 return true;
246 }
247 return false;
248 }
249
254 void setMin(T min) {
255 properties_[kMinValue] = min;
256 }
261 void setMax(T max) {
262 properties_[kMaxValue] = max;
263 }
269 void setRange(T min, T max) {
270 properties_[kMinValue] = min;
271 properties_[kMaxValue] = max;
272 }
276 bool getMin(T& outMin) const {
277 return getProperty(kMinValue, outMin);
278 }
282 bool getMax(T& outMax) const {
283 return getProperty(kMinValue, outMax);
284 }
285
288 bool isAvailable() const override {
289 return layout_.getFixedData<T>(offset_, getFixedSize()) != nullptr;
290 }
291
296 void print(ostream& out, const string& indent) const override;
301 void printCompact(ostream& out, const string& indent) const override;
302
307 bool isSame(const DataPiece* rhs) const override;
308
312 void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile) override;
313
317 unique_ptr<DataPiece> clone() const override {
318 auto other = std::make_unique<DataPieceArray<T>>(getLabel(), getArraySize());
319 other->tags_ = tags_;
320 other->required_ = required_;
321 other->properties_ = properties_;
322 other->defaultValues_ = defaultValues_;
323 return other;
324 }
325
326 protected:
327 bool stageFrom(const DataPiece* /*original*/) override {
328 return false; // not applicable
329 }
330
331 private:
332 const size_t count_{};
333 map<string, T> properties_;
334 vector<T> defaultValues_;
335};
336
337} // namespace vrs
338
339#endif // DATA_PIECES_ARRAY_H
vector< int8_t > & getFixedData()
Definition DataLayout.h:237
Fixed size array of POD values.
Definition DataPieceArray.h:39
const vector< T > & getDefault() const
Definition DataPieceArray.h:196
bool get(T *outValues, size_t count) const
Definition DataPieceArray.h:88
bool set(const T &value, size_t index)
Definition DataPieceArray.h:164
DataPieceArray(const string &label, size_t count, const T(&defaultValues)[n])
Definition DataPieceArray.h:51
void printCompact(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1285
bool stageFrom(const DataPiece *) override
Stage value from another piece known to be of the same type.
Definition DataPieceArray.h:327
void setProperty(const string &propertyName, T value)
Definition DataPieceArray.h:234
void serialize(JsonWrapper &jsonWrapper, const JsonFormatProfileSpec &profile) override
Definition DataLayout.cpp:1313
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:276
bool isAvailable() const override
Definition DataPieceArray.h:288
bool set(const T(&arr)[n])
Definition DataPieceArray.h:180
DataPieceArray(const string &label, size_t count)
Definition DataPieceArray.h:45
bool set(const T *values, size_t count)
Definition DataPieceArray.h:147
void print(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1253
size_t getArraySize() const
Get the size of the array.
Definition DataPieceArray.h:60
bool getMax(T &outMax) const
Definition DataPieceArray.h:282
bool set(const vector< T > &values)
Definition DataPieceArray.h:190
void setRange(T min, T max)
Definition DataPieceArray.h:269
void setDefault(const T(&arr)[n])
Definition DataPieceArray.h:220
void setMax(T max)
Definition DataPieceArray.h:261
unique_ptr< DataPiece > clone() const override
Definition DataPieceArray.h:317
bool get(vector< T > &outValues) const
Definition DataPieceArray.h:129
void setDefault(const vector< T > &values)
Definition DataPieceArray.h:227
size_t getVariableSize() const override
Definition DataPieceArray.h:71
void setDefault(const T *defaultValues, size_t count)
Definition DataPieceArray.h:205
void setMin(T min)
Definition DataPieceArray.h:254
bool isSame(const DataPiece *rhs) const override
Definition DataLayout.cpp:1303
bool getProperty(const string &propertyName, T &outValue) const
Definition DataPieceArray.h:241
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:106
Definition AsyncDiskFileChunk.hpp:49
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