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 outValues = defaultValues_;
118 return false;
119 }
120
123 const vector<T>& getDefault() const {
124 return defaultValues_;
125 }
129 void setDefault(const T* defaultValues, size_t count) {
130 defaultValues_.resize(count);
131 if (count > 0) {
132 memcpy(defaultValues_.data(), defaultValues, count * sizeof(T));
133 }
134 }
137 template <size_t n>
138 void setDefault(const T (&arr)[n]) {
139 setDefault(arr, n);
140 }
143 void setDefault(const vector<T>& values) {
144 defaultValues_ = values;
145 }
148 void setDefault(vector<T>&& values) {
149 defaultValues_ = std::move(values);
150 }
151
153 void initToDefault() override {
154 stagedValues_ = defaultValues_;
155 }
156
161 bool patchValue(const vector<T>& values) const {
162 auto* patchedPiece = layout_.getMappedPiece<DataPieceVector<T>>(pieceIndex_);
163 return patchedPiece != nullptr && (patchedPiece->stage(values), true);
164 }
165
168 bool isAvailable() const override {
169 size_t count = 0;
170 return layout_.getVarData<T>(offset_, count) != nullptr;
171 }
172
177 void print(ostream& out, const string& indent) const override;
182 void printCompact(ostream& out, const string& indent) const override;
183
188 bool isSame(const DataPiece* rhs) const override;
189
193 void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile) override;
194
197 bool stageCurrentValue() override {
198 return get(stagedValues());
199 }
200
204 unique_ptr<DataPiece> clone() const override {
205 auto other = std::make_unique<DataPieceVector<T>>(getLabel());
206 other->tags_ = tags_;
207 other->required_ = required_;
208 other->defaultValues_ = defaultValues_;
209 return other;
210 }
211
212 protected:
213 bool copyFrom(const DataPiece* original) override {
214 const DataPieceVector<T>* originalVect = reinterpret_cast<const DataPieceVector<T>*>(original);
215 return originalVect->get(stagedValues_);
216 }
217
218 private:
219 vector<T> stagedValues_; // values to write to disk
220 vector<T> defaultValues_;
221};
222
223// Specializations declaration, required here for come compilers.
224template <>
225void DataPieceVector<string>::stage(const string* values, size_t count);
226template <>
228template <>
229size_t DataPieceVector<string>::collectVariableData(int8_t* data, size_t bufferSize);
230template <>
231void DataPieceVector<string>::setDefault(const string* defaultValues, size_t count) = delete;
232template <>
233bool DataPieceVector<string>::get(vector<string>& outValues) const;
234template <>
235void DataPieceVector<string>::print(ostream& out, const string& indent) const;
236template <>
237void DataPieceVector<string>::printCompact(ostream& out, const string& indent) const;
238
239} // namespace vrs
240
241#endif // DATA_PIECES_VECTOR_H
The DataLayout class describes the data stored inside a DataLayoutContentBlock.
Definition DataLayout.h:191
vector< int8_t > & getVarData()
Definition DataLayout.h:242
T * getMappedPiece(size_t pieceIndex) const
Get a typed piece by index in the mapped datalayout, exclusively.
Definition DataLayout.h:505
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
Initialize to default value.
Definition DataPieceVector.h:153
bool isAvailable() const override
Definition DataPieceVector.h:168
void serialize(JsonWrapper &jsonWrapper, const JsonFormatProfileSpec &profile) override
Definition DataLayout.cpp:1585
const vector< T > & stagedValues() const
Definition DataPieceVector.h:71
void setDefault(vector< T > &&values)
Definition DataPieceVector.h:148
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:143
unique_ptr< DataPiece > clone() const override
Definition DataPieceVector.h:204
const vector< T > & getDefault() const
Definition DataPieceVector.h:123
bool isSame(const DataPiece *rhs) const override
Definition DataLayout.cpp:1576
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:1468
void setDefault(const T(&arr)[n])
Definition DataPieceVector.h:138
bool patchValue(const vector< T > &values) const
Definition DataPieceVector.h:161
void setDefault(const T *defaultValues, size_t count)
Definition DataPieceVector.h:129
size_t getVariableSize() const override
Definition DataPieceVector.h:54
vector< T > & stagedValues()
Definition DataPieceVector.h:76
bool stageCurrentValue() override
Definition DataPieceVector.h:197
bool copyFrom(const DataPiece *original) override
Set or stage value from another piece known to be of the same type.
Definition DataPieceVector.h:213
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:1521
Definition AsyncDiskFileChunk.hpp:49
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