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 <vrs/DataLayout.h>
28#include <vrs/DataPieceTypes.h>
29#include <vrs/VrsExport.h>
30
31namespace vrs {
32
33using std::map;
34using std::ostream;
35using std::string;
36
42class VRS_API DataPiece {
43 public:
46 struct MakerBundle;
47
48 protected:
53 DataPiece(string label, DataPieceType type, size_t size);
54
56 static const string kUnit;
58 static const string kDescription;
59
61 static const string kMinValue;
63 static const string kMaxValue;
65 static const string kMinIncrement;
67 static const string kMaxIncrement;
68
69 public:
70 virtual ~DataPiece();
73 const DataLayout& getDataLayout() const {
74 return layout_;
75 }
78 const string& getLabel() const {
79 return label_;
80 }
84 return pieceType_;
85 }
86
89 string getTypeName() const;
90
95 size_t getPieceIndex() const {
96 return pieceIndex_;
97 }
98
106 size_t getOffset() const {
107 return offset_;
108 }
111 bool hasFixedSize() const {
112 return fixedSize_ != DataLayout::kVariableSize;
113 }
116 size_t getFixedSize() const {
117 return fixedSize_;
118 }
123 bool getTag(const string& tagName, string& outTag) const;
127 void setTag(const string& tagName, const string& tag) {
128 tags_[tagName] = tag;
129 }
133 bool getUnit(string& outUnit) const {
134 return getTag(kUnit, outUnit);
135 }
138 void setUnit(const string& unit) {
139 tags_[kUnit] = unit;
140 }
141
145 bool getDescription(string& outDescription) const {
146 return getTag(kDescription, outDescription);
147 }
150 void setDescription(const string& description) {
151 tags_[kDescription] = description;
152 }
153
156 void setRequired(bool required = true) {
157 required_ = required;
158 }
162 bool isRequired() const {
163 return required_;
164 }
165
169 virtual size_t getVariableSize() const = 0;
174 virtual size_t collectVariableData(int8_t* data, size_t bufferSize) = 0;
178 virtual const string& getElementTypeName() const = 0;
182 virtual bool isAvailable() const = 0;
184 bool isMapped() const {
185 return layout_.isMapped() && pieceIndex_ != DataLayout::kNotFound;
186 }
191 virtual void print(ostream& out, const string& indent = "") const = 0;
196 virtual void printCompact(ostream& out, const string& indent = "") const = 0;
200 virtual void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile);
206 virtual bool stageCurrentValue() {
207 return isAvailable();
208 }
209
214 virtual void initToDefault() = 0;
216 virtual unique_ptr<DataPiece> clone() const = 0;
217
218 protected:
220 bool isMatch(const DataPiece& rhs) const;
222 virtual bool isSame(const DataPiece* rhs) const;
223 void setIndexOffset(size_t pieceIndex, size_t offset) {
224 pieceIndex_ = pieceIndex;
225 offset_ = offset;
226 }
228 virtual bool copyFrom(const DataPiece* original) = 0;
229
230 const string label_;
231 const DataPieceType pieceType_;
232 const size_t fixedSize_;
233 size_t pieceIndex_;
234 size_t offset_;
235 DataLayout& layout_;
236 map<string, string> tags_;
237 bool required_;
238
239 friend class DataLayout;
240};
241
243template <class T>
244const string& getTypeName();
245
246template <>
247inline const string& getTypeName<string>() {
248 static const string sName("string");
249 return sName;
250}
251
252// The getTypeName<> POD specializations are defined out-of-line in DataLayout.cpp. Declare them
253// here with VRS_API so consumers of the shared library see the import-side attribute at every call
254// site (e.g. the inline DataPiece<T>::getElementTypeName()).
255#define POD_MACRO(x) \
256 template <> \
257 VRS_API const string& getTypeName<x>();
258#include <vrs/helpers/PODMacro.inc>
259
265#pragma pack(push, 1) // tells the compiler that the data might not be aligned
266template <class T>
268 T value;
269};
270#pragma pack(pop)
271
273template <class T>
274T readUnaligned(const void* ptr) {
275 return reinterpret_cast<const UnalignedValue<T>*>(ptr)->value;
276}
277
279template <class T>
280void writeUnaligned(void* ptr, const T& value) {
281 reinterpret_cast<UnalignedValue<T>*>(ptr)->value = value;
282}
283
285inline void unalignedCopy(void* dst, const void* src, size_t size) {
286 memcpy(reinterpret_cast<char*>(dst), reinterpret_cast<const char*>(src), size);
287}
288
289} // namespace vrs
290
291// These are required pretty much every single time we use DataLayout. Just include them all.
292#ifndef DATA_PIECES_ARRAY_H
293#include <vrs/DataPieceArray.h>
294#endif
295
296#ifndef DATA_PIECES_STRING_H
297#include <vrs/DataPieceString.h>
298#endif
299
300#ifndef DATA_PIECES_STRING_MAP_H
301#include <vrs/DataPieceStringMap.h>
302#endif
303
304#ifndef DATA_PIECES_VALUE_H
305#include <vrs/DataPieceValue.h>
306#endif
307
308#ifndef DATA_PIECES_VECTOR_H
309#include <vrs/DataPieceVector.h>
310#endif
311
312#endif // DATA_PIECES_H
The DataLayout class describes the data stored inside a DataLayoutContentBlock.
Definition DataLayout.h:211
Abstract class representing a piece of information part of a DataLayout.
Definition DataPieces.h:42
static const string kMinIncrement
Special property name for the minimum increment of the DataPiece.
Definition DataPieces.h:65
void setRequired(bool required=true)
Definition DataPieces.h:156
size_t getPieceIndex() const
Definition DataPieces.h:95
bool getDescription(string &outDescription) const
Definition DataPieces.h:145
static const string kMaxValue
Special property name for the maximum value of the DataPiece.
Definition DataPieces.h:63
bool isRequired() const
Definition DataPieces.h:162
void setDescription(const string &description)
Definition DataPieces.h:150
bool getUnit(string &outUnit) const
Definition DataPieces.h:133
static const string kMaxIncrement
Special property name for the maximum increment of the DataPiece.
Definition DataPieces.h:67
virtual void printCompact(ostream &out, const string &indent="") const =0
virtual void initToDefault()=0
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:206
bool hasFixedSize() const
Definition DataPieces.h:111
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:61
size_t getOffset() const
Definition DataPieces.h:106
DataPieceType getPieceType() const
Definition DataPieces.h:83
const string & getLabel() const
Definition DataPieces.h:78
const DataLayout & getDataLayout() const
Definition DataPieces.h:73
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:58
bool isMapped() const
Tells if the DataPiece is mapped to another DataPiece in a mapped DataLayout.
Definition DataPieces.h:184
void setUnit(const string &unit)
Definition DataPieces.h:138
static const string kUnit
Special tag name to specify a unit of the DataPiece.
Definition DataPieces.h:56
void setTag(const string &tagName, const string &tag)
Definition DataPieces.h:127
size_t getFixedSize() const
Definition DataPieces.h:116
virtual size_t getVariableSize() const =0
virtual const string & getElementTypeName() const =0
Definition Compressor.cpp:113
void writeUnaligned(void *ptr, const T &value)
Helper to make dereferencing a pointer to write an unaligned POD object safe.
Definition DataPieces.h:280
const string & getTypeName()
Get the name of the type <T>.
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:285
T readUnaligned(const void *ptr)
Helper to make dereferencing a pointer to read an unaligned POD object safe.
Definition DataPieces.h:274
DataPieceType
Specifier for a type of DataPiece.
Definition DataLayout.h:59
Definition DataLayout.cpp:1167
When printing out a DataLayout as json, this class allows to specify what should be included in the g...
Definition DataLayout.h:86
Template to represent some POD object without memory alignment.
Definition DataPieces.h:267