VRS
A file format for sensor data.
Loading...
Searching...
No Matches
DataPieceVector.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_VECTOR_H
20#define DATA_PIECES_VECTOR_H
21
22#ifndef DATA_PIECES_H
23#include "DataPieces.h"
24#endif
25
26namespace vrs {
27
28using std::ostream;
29using std::string;
30using std::vector;
31
36template <typename T>
37class DataPieceVector : public DataPiece {
38 public:
40 explicit DataPieceVector(string label, vector<T> defaultValues = {})
41 : DataPiece(std::move(label), DataPieceType::Vector, DataLayout::kVariableSize),
42 defaultValues_{std::move(defaultValues)} {}
45 explicit DataPieceVector(const MakerBundle& bundle);
46
49 const string& getElementTypeName() const override {
50 return vrs::getTypeName<T>();
51 }
54 size_t getVariableSize() const override {
55 return sizeof(T) * stagedValues_.size();
56 }
61 size_t collectVariableData(int8_t* data, size_t bufferSize) override {
62 const size_t writtenSize = min(bufferSize, getVariableSize());
63 if (writtenSize > 0) {
64 memcpy(data, stagedValues_.data(), writtenSize);
65 }
66 return writtenSize;
67 }
68
71 const vector<T>& stagedValues() const {
72 return stagedValues_;
73 }
76 vector<T>& stagedValues() {
77 return stagedValues_;
78 }
81 void stage(const vector<T>& values) {
82 stagedValues_ = values;
83 }
86 void stage(vector<T>&& values) {
87 stagedValues_ = std::move(values);
88 }
89
93 void stage(const T* values, size_t count) {
94 stagedValues_.resize(count);
95 if (count > 0) {
96 memcpy(stagedValues_.data(), values, count * sizeof(T));
97 }
98 }
101 template <size_t n>
102 void stage(const T (&arr)[n]) {
103 stage(arr, n);
104 }
105
109 bool get(vector<T>& outValues) const {
110 size_t count = 0;
111 const T* ptr = layout_.getVarData<T>(offset_, count);
112 if (ptr != nullptr && count > 0) {
113 outValues.resize(count);
114 memcpy(outValues.data(), ptr, count * sizeof(T));
115 return true;
116 }
117 if (pieceIndex_ != DataLayout::kNotFound) {
118 outValues.clear();
119 return true;
120 }
121 outValues = defaultValues_;
122 return false;
123 }
124
130 void setDefault(const T* defaultValues, size_t count) {
131 defaultValues_.resize(count);
132 if (count > 0) {
133 memcpy(defaultValues_.data(), defaultValues, count * sizeof(T));
134 }
135 }
140 template <size_t n>
141 void setDefault(const T (&arr)[n]) {
142 setDefault(arr, n);
143 }
148 void setDefault(const vector<T>& values) {
149 defaultValues_ = values;
150 }
155 void setDefault(vector<T>&& values) {
156 defaultValues_ = std::move(values);
157 }
160 const vector<T>& getDefault() const {
161 return defaultValues_;
162 }
164 void initToDefault() override {
165 stagedValues_ = defaultValues_;
166 }
167
172 bool patchValue(const vector<T>& values) const {
173 auto* patchedPiece = layout_.getMappedPiece<DataPieceVector<T>>(pieceIndex_);
174 return patchedPiece != nullptr && (patchedPiece->stage(values), true);
175 }
176
179 bool isAvailable() const override {
180 size_t count = 0;
181 return layout_.getVarData<T>(offset_, count) != nullptr;
182 }
183
188 void print(ostream& out, const string& indent) const override;
193 void printCompact(ostream& out, const string& indent) const override;
194
199 bool isSame(const DataPiece* rhs) const override;
200
204 void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile) override;
205
208 bool stageCurrentValue() override {
209 return get(stagedValues());
210 }
211
215 unique_ptr<DataPiece> clone() const override {
216 auto other = std::make_unique<DataPieceVector<T>>(getLabel());
217 other->tags_ = tags_;
218 other->required_ = required_;
219 other->defaultValues_ = defaultValues_;
220 return other;
221 }
222
223 protected:
224 bool copyFrom(const DataPiece* original) override {
225 const DataPieceVector<T>* originalVect = reinterpret_cast<const DataPieceVector<T>*>(original);
226 return originalVect->get(stagedValues_);
227 }
228
229 private:
230 vector<T> stagedValues_; // values to write to disk
231 vector<T> defaultValues_;
232};
233
234// Specializations declaration, required here for come compilers.
235template <>
236void DataPieceVector<string>::stage(const string* values, size_t count);
237template <>
239template <>
240size_t DataPieceVector<string>::collectVariableData(int8_t* data, size_t bufferSize);
241template <>
242void DataPieceVector<string>::setDefault(const string* defaultValues, size_t count) = delete;
243template <>
244bool DataPieceVector<string>::get(vector<string>& outValues) const;
245template <>
246void DataPieceVector<string>::print(ostream& out, const string& indent) const;
247template <>
248void DataPieceVector<string>::printCompact(ostream& out, const string& indent) const;
249
250} // namespace vrs
251
252#endif // DATA_PIECES_VECTOR_H
The DataLayout class describes the data stored inside a DataLayoutContentBlock.
Definition DataLayout.h:209
vector< int8_t > & getVarData()
Definition DataLayout.h:260
static const size_t kNotFound
Special OffsetAndLength offset value marking that a piece of data isn't available.
Definition DataLayout.h:222
T * getMappedPiece(size_t pieceIndex) const
Get a typed piece by index in the mapped datalayout, exclusively.
Definition DataLayout.h:523
Abstract class representing a piece of information part of a DataLayout.
Definition DataPieces.h:40
const string & getLabel() const
Definition DataPieces.h:76
Vector of type T and variable size.
Definition DataPieceVector.h:37
void initToDefault() override
Stage default value.
Definition DataPieceVector.h:164
bool isAvailable() const override
Definition DataPieceVector.h:179
void serialize(JsonWrapper &jsonWrapper, const JsonFormatProfileSpec &profile) override
Definition DataLayout.cpp:1574
const vector< T > & stagedValues() const
Definition DataPieceVector.h:71
void setDefault(vector< T > &&values)
Definition DataPieceVector.h:155
void stage(vector< T > &&values)
Definition DataPieceVector.h:86
void stage(const T(&arr)[n])
Definition DataPieceVector.h:102
size_t collectVariableData(int8_t *data, size_t bufferSize) override
Definition DataPieceVector.h:61
void setDefault(const vector< T > &values)
Definition DataPieceVector.h:148
unique_ptr< DataPiece > clone() const override
Definition DataPieceVector.h:215
const vector< T > & getDefault() const
Definition DataPieceVector.h:160
bool isSame(const DataPiece *rhs) const override
Definition DataLayout.cpp:1565
bool get(vector< T > &outValues) const
Definition DataPieceVector.h:109
void stage(const T *values, size_t count)
Definition DataPieceVector.h:93
void print(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1457
void setDefault(const T(&arr)[n])
Definition DataPieceVector.h:141
bool patchValue(const vector< T > &values) const
Definition DataPieceVector.h:172
void setDefault(const T *defaultValues, size_t count)
Definition DataPieceVector.h:130
size_t getVariableSize() const override
Definition DataPieceVector.h:54
vector< T > & stagedValues()
Definition DataPieceVector.h:76
bool stageCurrentValue() override
Definition DataPieceVector.h:208
bool copyFrom(const DataPiece *original) override
Set or stage value from another piece known to be of the same type.
Definition DataPieceVector.h:224
const string & getElementTypeName() const override
Definition DataPieceVector.h:49
DataPieceVector(string label, vector< T > defaultValues={})
Definition DataPieceVector.h:40
void stage(const vector< T > &values)
Definition DataPieceVector.h:81
void printCompact(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1510
Definition Compressor.cpp:113
DataPieceType
Specifier for a type of DataPiece.
Definition DataLayout.h:57
@ Vector
Variable size array of T.
When printing out a DataLayout as json, this class allows to specify what should be included in the g...
Definition DataLayout.h:84