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#include <type_traits>
23
24#ifndef DATA_PIECES_H
25#include <vrs/DataPieces.h>
26#endif
27
28namespace vrs {
29
30using std::ostream;
31using std::string;
32using std::vector;
33
38template <typename T>
39class DataPieceVector : public DataPiece {
40 public:
42 explicit DataPieceVector(string label, vector<T> defaultValues = {})
43 : DataPiece(std::move(label), DataPieceType::Vector, DataLayout::kVariableSize),
44 defaultValues_{std::move(defaultValues)} {}
47 explicit DataPieceVector(const MakerBundle& bundle);
48
51 const string& getElementTypeName() const override {
52 return vrs::getTypeName<T>();
53 }
56 size_t getVariableSize() const override {
57 return sizeof(T) * stagedValues_.size();
58 }
63 size_t collectVariableData(int8_t* data, size_t bufferSize) override {
64 const size_t writtenSize = min(bufferSize, getVariableSize());
65 if (writtenSize > 0) {
66 unalignedCopy(data, stagedValues_.data(), writtenSize);
67 }
68 return writtenSize;
69 }
70
73 const vector<T>& stagedValues() const {
74 return stagedValues_;
75 }
78 vector<T>& stagedValues() {
79 return stagedValues_;
80 }
83 void stage(const vector<T>& values) {
84 stagedValues_ = values;
85 }
88 void stage(vector<T>&& values) {
89 stagedValues_ = std::move(values);
90 }
91
95 void stage(const T* values, size_t count) {
96 stagedValues_.resize(count);
97 if (count > 0) {
98 unalignedCopy(stagedValues_.data(), values, count * sizeof(T));
99 }
100 }
103 template <size_t n>
104 void stage(const T (&arr)[n]) {
105 stage(arr, n);
106 }
107
111 bool get(vector<T>& outValues) const {
112 size_t count = 0;
113 const T* ptr = layout_.getVarData<T>(offset_, count);
114 if (ptr != nullptr && count > 0) {
115 outValues.resize(count);
116 unalignedCopy(outValues.data(), ptr, count * sizeof(T));
117 return true;
118 }
119 if (pieceIndex_ != DataLayout::kNotFound) {
120 outValues.clear();
121 return true;
122 }
123 outValues = defaultValues_;
124 return false;
125 }
132 template <
133 typename U = T,
134 std::enable_if_t<std::is_same<U, T>::value && std::is_trivially_copyable<U>::value, int> = 0>
135 bool getRawData(const void*& outData, size_t& outCount) const {
136 outData = layout_.getVarData<T>(offset_, outCount);
137 if (outData != nullptr && outCount > 0) {
138 return true;
139 }
140 outData = nullptr;
141 outCount = 0;
142 return pieceIndex_ != DataLayout::kNotFound;
143 }
144
150 void setDefault(const T* defaultValues, size_t count) {
151 defaultValues_.resize(count);
152 if (count > 0) {
153 unalignedCopy(defaultValues_.data(), defaultValues, count * sizeof(T));
154 }
155 }
160 template <size_t n>
161 void setDefault(const T (&arr)[n]) {
162 setDefault(arr, n);
163 }
168 void setDefault(const vector<T>& values) {
169 defaultValues_ = values;
170 }
175 void setDefault(vector<T>&& values) {
176 defaultValues_ = std::move(values);
177 }
180 const vector<T>& getDefault() const {
181 return defaultValues_;
182 }
184 void initToDefault() override {
185 stagedValues_ = defaultValues_;
186 }
187
192 bool patchValue(const vector<T>& values) const {
193 auto* patchedPiece = layout_.getMappedPiece<DataPieceVector<T>>(pieceIndex_);
194 return patchedPiece != nullptr && (patchedPiece->stage(values), true);
195 }
196
199 bool isAvailable() const override {
200 size_t count = 0;
201 return layout_.getVarData<T>(offset_, count) != nullptr;
202 }
203
208 void print(ostream& out, const string& indent) const override;
213 void printCompact(ostream& out, const string& indent) const override;
214
219 bool isSame(const DataPiece* rhs) const override;
220
224 void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile) override;
225
228 bool stageCurrentValue() override {
229 return get(stagedValues());
230 }
231
235 unique_ptr<DataPiece> clone() const override {
236 auto other = std::make_unique<DataPieceVector<T>>(getLabel());
237 other->tags_ = tags_;
238 other->required_ = required_;
239 other->defaultValues_ = defaultValues_;
240 return other;
241 }
242
243 protected:
244 bool copyFrom(const DataPiece* original) override {
245 const DataPieceVector<T>* originalVect = reinterpret_cast<const DataPieceVector<T>*>(original);
246 return originalVect->get(stagedValues_);
247 }
248
249 private:
250 vector<T> stagedValues_; // values to write to disk
251 vector<T> defaultValues_;
252};
253
254// Specializations declaration, required here for come compilers.
255template <>
256VRS_API void DataPieceVector<string>::stage(const string* values, size_t count);
257template <>
259template <>
260VRS_API size_t DataPieceVector<string>::collectVariableData(int8_t* data, size_t bufferSize);
261template <>
262void DataPieceVector<string>::setDefault(const string* defaultValues, size_t count) = delete;
263template <>
264VRS_API bool DataPieceVector<string>::get(vector<string>& outValues) const;
265template <>
266VRS_API void DataPieceVector<string>::print(ostream& out, const string& indent) const;
267template <>
268VRS_API void DataPieceVector<string>::printCompact(ostream& out, const string& indent) const;
269
270} // namespace vrs
271
272#endif // DATA_PIECES_VECTOR_H
The DataLayout class describes the data stored inside a DataLayoutContentBlock.
Definition DataLayout.h:211
vector< int8_t > & getVarData()
Definition DataLayout.h:263
static const size_t kNotFound
Special OffsetAndLength offset value marking that a piece of data isn't available.
Definition DataLayout.h:225
T * getMappedPiece(size_t pieceIndex) const
Get a typed piece by index in the mapped datalayout, exclusively.
Definition DataLayout.h:526
Abstract class representing a piece of information part of a DataLayout.
Definition DataPieces.h:42
const string & getLabel() const
Definition DataPieces.h:78
Vector of type T and variable size.
Definition DataPieceVector.h:39
void initToDefault() override
Stage default value.
Definition DataPieceVector.h:184
bool isAvailable() const override
Definition DataPieceVector.h:199
void serialize(JsonWrapper &jsonWrapper, const JsonFormatProfileSpec &profile) override
Definition DataLayout.cpp:1593
const vector< T > & stagedValues() const
Definition DataPieceVector.h:73
void setDefault(vector< T > &&values)
Definition DataPieceVector.h:175
void stage(vector< T > &&values)
Definition DataPieceVector.h:88
void stage(const T(&arr)[n])
Definition DataPieceVector.h:104
size_t collectVariableData(int8_t *data, size_t bufferSize) override
Definition DataPieceVector.h:63
void setDefault(const vector< T > &values)
Definition DataPieceVector.h:168
unique_ptr< DataPiece > clone() const override
Definition DataPieceVector.h:235
const vector< T > & getDefault() const
Definition DataPieceVector.h:180
bool isSame(const DataPiece *rhs) const override
Definition DataLayout.cpp:1584
bool get(vector< T > &outValues) const
Definition DataPieceVector.h:111
void stage(const T *values, size_t count)
Definition DataPieceVector.h:95
void print(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1470
void setDefault(const T(&arr)[n])
Definition DataPieceVector.h:161
bool patchValue(const vector< T > &values) const
Definition DataPieceVector.h:192
void setDefault(const T *defaultValues, size_t count)
Definition DataPieceVector.h:150
size_t getVariableSize() const override
Definition DataPieceVector.h:56
vector< T > & stagedValues()
Definition DataPieceVector.h:78
bool stageCurrentValue() override
Definition DataPieceVector.h:228
bool copyFrom(const DataPiece *original) override
Set or stage value from another piece known to be of the same type.
Definition DataPieceVector.h:244
const string & getElementTypeName() const override
Definition DataPieceVector.h:51
bool getRawData(const void *&outData, size_t &outCount) const
Definition DataPieceVector.h:135
DataPieceVector(string label, vector< T > defaultValues={})
Definition DataPieceVector.h:42
void stage(const vector< T > &values)
Definition DataPieceVector.h:83
void printCompact(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1529
Definition Compressor.cpp:130
void unalignedCopy(void *dst, const void *src, size_t size)
ASAN can trip when pointers are not aligned, but pointing to larger types. Make asan happy.
Definition DataPieces.h:315
DataPieceType
Specifier for a type of DataPiece.
Definition DataLayout.h:59
@ 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:86