VRS
A file format for sensor data.
Loading...
Searching...
No Matches
DataPieces.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_H
20#define DATA_PIECES_H
21
22#include <cstring>
23
24#include <map>
25#include <ostream>
26
27#include "DataLayout.h"
28
29namespace vrs {
30
31using std::map;
32using std::ostream;
33using std::string;
34
40class DataPiece {
41 public:
44 struct MakerBundle;
45
46 protected:
51 DataPiece(string label, DataPieceType type, size_t size);
52
54 static const string kUnit;
56 static const string kDescription;
57
59 static const string kMinValue;
61 static const string kMaxValue;
63 static const string kMinIncrement;
65 static const string kMaxIncrement;
66
67 public:
68 virtual ~DataPiece();
71 const DataLayout& getDataLayout() const {
72 return layout_;
73 }
76 const string& getLabel() const {
77 return label_;
78 }
82 return pieceType_;
83 }
84
87 string getTypeName() const;
88
93 size_t getPieceIndex() const {
94 return pieceIndex_;
95 }
96
104 size_t getOffset() const {
105 return offset_;
106 }
109 bool hasFixedSize() const {
110 return fixedSize_ != DataLayout::kVariableSize;
111 }
114 size_t getFixedSize() const {
115 return fixedSize_;
116 }
121 bool getTag(const string& tagName, string& outTag) const;
125 void setTag(const string& tagName, const string& tag) {
126 tags_[tagName] = tag;
127 }
131 bool getUnit(string& outUnit) const {
132 return getTag(kUnit, outUnit);
133 }
136 void setUnit(const string& unit) {
137 tags_[kUnit] = unit;
138 }
139
143 bool getDescription(string& outDescription) const {
144 return getTag(kDescription, outDescription);
145 }
148 void setDescription(const string& description) {
149 tags_[kDescription] = description;
150 }
151
154 void setRequired(bool required = true) {
155 required_ = required;
156 }
160 bool isRequired() const {
161 return required_;
162 }
163
167 virtual size_t getVariableSize() const = 0;
172 virtual size_t collectVariableData(int8_t* data, size_t bufferSize) = 0;
176 virtual const string& getElementTypeName() const = 0;
180 virtual bool isAvailable() const = 0;
182 bool isMapped() const {
183 return layout_.isMapped() && pieceIndex_ != DataLayout::kNotFound;
184 }
189 virtual void print(ostream& out, const string& indent = "") const = 0;
194 virtual void printCompact(ostream& out, const string& indent = "") const = 0;
198 virtual void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile);
204 virtual bool stageCurrentValue() {
205 return isAvailable();
206 }
208 virtual void initToDefault() = 0;
210 virtual unique_ptr<DataPiece> clone() const = 0;
211
212 protected:
214 bool isMatch(const DataPiece& rhs) const;
216 virtual bool isSame(const DataPiece* rhs) const;
217 void setIndexOffset(size_t pieceIndex, size_t offset) {
218 pieceIndex_ = pieceIndex;
219 offset_ = offset;
220 }
222 virtual bool copyFrom(const DataPiece* original) = 0;
223
224 const string label_;
225 const DataPieceType pieceType_;
226 const size_t fixedSize_;
227 size_t pieceIndex_;
228 size_t offset_;
229 DataLayout& layout_;
230 map<string, string> tags_;
231 bool required_;
232
233 friend class DataLayout;
234};
235
237template <class T>
238const string& getTypeName();
239
240template <>
241inline const string& getTypeName<string>() {
242 static const string sName("string");
243 return sName;
244}
245
251#pragma pack(push, 1) // tells the compiler that the data might not be aligned
252template <class T>
254 T value;
255};
256#pragma pack(pop)
257
259template <class T>
260T readUnaligned(const void* ptr) {
261 return reinterpret_cast<const UnalignedValue<T>*>(ptr)->value;
262}
263
265template <class T>
266void writeUnaligned(void* ptr, const T& value) {
267 reinterpret_cast<UnalignedValue<T>*>(ptr)->value = value;
268}
269
270} // namespace vrs
271
272// These are required pretty much every single time we use DataLayout. Just include them all.
273#include "DataPieceTypes.h"
274
275#ifndef DATA_PIECES_ARRAY_H
276#include "DataPieceArray.h"
277#endif
278
279#ifndef DATA_PIECES_STRING_H
280#include "DataPieceString.h"
281#endif
282
283#ifndef DATA_PIECES_STRING_MAP_H
284#include "DataPieceStringMap.h"
285#endif
286
287#ifndef DATA_PIECES_VALUE_H
288#include "DataPieceValue.h"
289#endif
290
291#ifndef DATA_PIECES_VECTOR_H
292#include "DataPieceVector.h"
293#endif
294
295#endif // DATA_PIECES_H
The DataLayout class describes the data stored inside a DataLayoutContentBlock.
Definition DataLayout.h:191
static const size_t kVariableSize
Special value used for a DataPiece size, telling that that DataPiece has a variable size.
Definition DataLayout.h:206
bool isMapped() const
Definition DataLayout.h:331
static const size_t kNotFound
Special OffsetAndLength offset value marking that a piece of data isn't available.
Definition DataLayout.h:204
Abstract class representing a piece of information part of a DataLayout.
Definition DataPieces.h:40
static const string kMinIncrement
Special property name for the minimum increment of the DataPiece.
Definition DataPieces.h:63
void setRequired(bool required=true)
Definition DataPieces.h:154
size_t getPieceIndex() const
Definition DataPieces.h:93
bool getDescription(string &outDescription) const
Definition DataPieces.h:143
static const string kMaxValue
Special property name for the maximum value of the DataPiece.
Definition DataPieces.h:61
bool getTag(const string &tagName, string &outTag) const
Definition DataLayout.cpp:1157
bool isRequired() const
Definition DataPieces.h:160
void setDescription(const string &description)
Definition DataPieces.h:148
virtual bool isSame(const DataPiece *rhs) const
Match signature & properties (default value, min/max, etc).
Definition DataLayout.cpp:810
bool getUnit(string &outUnit) const
Definition DataPieces.h:131
static const string kMaxIncrement
Special property name for the maximum increment of the DataPiece.
Definition DataPieces.h:65
bool isMatch(const DataPiece &rhs) const
Match signature only.
Definition DataLayout.cpp:1166
virtual void printCompact(ostream &out, const string &indent="") const =0
virtual void initToDefault()=0
Initialize (set or stage) the DataPiece to its default value.
virtual size_t collectVariableData(int8_t *data, size_t bufferSize)=0
virtual bool copyFrom(const DataPiece *original)=0
Set or stage value from another piece known to be of the same type.
virtual bool stageCurrentValue()
Definition DataPieces.h:204
virtual void serialize(JsonWrapper &jsonWrapper, const JsonFormatProfileSpec &profile)
Definition DataLayout.cpp:1190
bool hasFixedSize() const
Definition DataPieces.h:109
virtual bool isAvailable() const =0
virtual void print(ostream &out, const string &indent="") const =0
static const string kMinValue
Special property name for the minimum value of the DataPiece.
Definition DataPieces.h:59
string getTypeName() const
Definition DataLayout.cpp:1186
size_t getOffset() const
Definition DataPieces.h:104
DataPieceType getPieceType() const
Definition DataPieces.h:81
const string & getLabel() const
Definition DataPieces.h:76
const DataLayout & getDataLayout() const
Definition DataPieces.h:71
virtual unique_ptr< DataPiece > clone() const =0
Create a new DataPiece of the same type, with the same label.
static const string kDescription
Special tag name to specify a human readable description the DataPiece.
Definition DataPieces.h:56
bool isMapped() const
Tells if the DataPiece is mapped to another DataPiece in a mapped DataLayout.
Definition DataPieces.h:182
void setUnit(const string &unit)
Definition DataPieces.h:136
static const string kUnit
Special tag name to specify a unit of the DataPiece.
Definition DataPieces.h:54
void setTag(const string &tagName, const string &tag)
Definition DataPieces.h:125
size_t getFixedSize() const
Definition DataPieces.h:114
virtual size_t getVariableSize() const =0
virtual const string & getElementTypeName() const =0
Definition AsyncDiskFileChunk.hpp:49
void writeUnaligned(void *ptr, const T &value)
Helper to make dereferencing a pointer to write an unaligned POD object safe.
Definition DataPieces.h:266
const string & getTypeName()
Get the name of the type <T>.
T readUnaligned(const void *ptr)
Helper to make dereferencing a pointer to read an unaligned POD object safe.
Definition DataPieces.h:260
DataPieceType
Specifier for a type of DataPiece.
Definition DataLayout.h:57
Definition DataLayout.cpp:1178
When printing out a DataLayout as json, this class allows to specify what should be included in the g...
Definition DataLayout.h:84
Template to represent some POD object without memory alignment.
Definition DataPieces.h:253