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 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
232 void initToDefault() override {
233 set(defaultValues_);
234 }
235
239 void setProperty(const string& propertyName, T value) {
240 properties_[propertyName] = value;
241 }
246 bool getProperty(const string& propertyName, T& outValue) const {
247 auto iter = properties_.find(propertyName);
248 if (iter != properties_.end()) {
249 outValue = iter->second;
250 return true;
251 }
252 return false;
253 }
254
259 void setMin(T min) {
260 properties_[kMinValue] = min;
261 }
266 void setMax(T max) {
267 properties_[kMaxValue] = max;
268 }
274 void setRange(T min, T max) {
275 properties_[kMinValue] = min;
276 properties_[kMaxValue] = max;
277 }
281 bool getMin(T& outMin) const {
282 return getProperty(kMinValue, outMin);
283 }
287 bool getMax(T& outMax) const {
288 return getProperty(kMinValue, outMax);
289 }
290
295 bool patchValue(const T* values, size_t count) const {
296 auto* patchedPiece = layout_.getMappedPiece<DataPieceArray<T>>(pieceIndex_);
297 return patchedPiece != nullptr && patchedPiece->set(values, count);
298 }
299
302 bool isAvailable() const override {
303 return layout_.getFixedData<T>(offset_, getFixedSize()) != nullptr;
304 }
305
310 void print(ostream& out, const string& indent) const override;
315 void printCompact(ostream& out, const string& indent) const override;
316
321 bool isSame(const DataPiece* rhs) const override;
322
326 void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile) override;
327
331 unique_ptr<DataPiece> clone() const override {
332 auto other = std::make_unique<DataPieceArray<T>>(getLabel(), getArraySize());
333 other->tags_ = tags_;
334 other->required_ = required_;
335 other->properties_ = properties_;
336 other->defaultValues_ = defaultValues_;
337 return other;
338 }
339
340 protected:
341 bool copyFrom(const DataPiece* original) override {
342 const auto* source = reinterpret_cast<const DataPieceArray<T>*>(original);
343 vector<T> values;
344 bool retrieved = source->get(values);
345 set(values);
346 return retrieved;
347 }
348
349 private:
350 const size_t count_{};
351 map<string, T> properties_;
352 vector<T> defaultValues_;
353};
354
355} // namespace vrs
356
357#endif // DATA_PIECES_ARRAY_H
vector< int8_t > & getFixedData()
Definition DataLayout.h:237
T * getMappedPiece(size_t pieceIndex) const
Get a typed piece by index in the mapped datalayout, exclusively.
Definition DataLayout.h:505
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
void printCompact(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1345
void setProperty(const string &propertyName, T value)
Definition DataPieceArray.h:239
void serialize(JsonWrapper &jsonWrapper, const JsonFormatProfileSpec &profile) override
Definition DataLayout.cpp:1373
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:281
void initToDefault() override
Initialize to default value.
Definition DataPieceArray.h:232
bool copyFrom(const DataPiece *original) override
Set or stage value from another piece known to be of the same type.
Definition DataPieceArray.h:341
bool isAvailable() const override
Definition DataPieceArray.h:302
bool set(const T(&arr)[n])
Definition DataPieceArray.h:180
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:295
void print(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1313
size_t getArraySize() const
Get the size of the array.
Definition DataPieceArray.h:60
bool getMax(T &outMax) const
Definition DataPieceArray.h:287
bool set(const vector< T > &values)
Definition DataPieceArray.h:190
void setRange(T min, T max)
Definition DataPieceArray.h:274
void setDefault(const T(&arr)[n])
Definition DataPieceArray.h:220
void setMax(T max)
Definition DataPieceArray.h:266
unique_ptr< DataPiece > clone() const override
Definition DataPieceArray.h:331
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:259
bool isSame(const DataPiece *rhs) const override
Definition DataLayout.cpp:1363
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:246
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 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