VRS
A file format for sensor data.
Loading...
Searching...
No Matches
DataPieceValue.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_VALUE_H
20#define DATA_PIECES_VALUE_H
21
22#include <memory>
23
24#ifndef DATA_PIECES_H
25#include "DataPieces.h"
26#endif
27
28namespace vrs {
29
30using std::map;
31using std::ostream;
32using std::string;
33using std::unique_ptr;
34
36template <typename T>
37class DataPieceValue : public DataPiece {
38 static_assert(
39 !std::is_same<T, bool>::value,
40 "DataPieceValue does not support bool. Use vrs::Bool instead");
41 static_assert(std::is_trivially_copyable<T>::value, "DataPieceValue only supports POD types.");
42
43 public:
45 explicit DataPieceValue(const string& label)
46 : DataPiece(label, DataPieceType::Value, sizeof(T)) {}
49 DataPieceValue(const string& label, T defaultValue)
50 : DataPiece(label, DataPieceType::Value, sizeof(T)) {
51 defaultValue_ = std::make_unique<T>(defaultValue);
52 }
55 explicit DataPieceValue(const MakerBundle& bundle);
56
59 const string& getElementTypeName() const override {
60 return vrs::getTypeName<T>();
61 }
64 size_t getVariableSize() const override {
65 return 0;
66 }
69 size_t collectVariableData(int8_t*, size_t) override {
70 return 0;
71 }
72
74 T get() const {
75 const T* const ptr = layout_.getFixedData<T>(offset_, sizeof(T));
76 return ptr != nullptr ? readUnaligned<T>(ptr) : getDefault();
77 }
81 bool get(T& outValue) const {
82 const T* const ptr = layout_.getFixedData<T>(offset_, sizeof(T));
83 if (ptr != nullptr) {
84 outValue = readUnaligned<T>(ptr);
85 return true;
86 } else {
87 getDefault(outValue);
88 return false;
89 }
90 }
91
95 bool set(const T& value) {
96 T* const ptr = layout_.getFixedData<T>(offset_, sizeof(T));
97 if (ptr != nullptr) {
98 writeUnaligned<T>(ptr, value);
99 return true;
100 }
101 return false;
102 }
103
106 T getDefault() const {
107 return defaultValue_ ? *defaultValue_.get() : T{};
108 }
112 bool getDefault(T& outDefault) const {
113 if (defaultValue_) {
114 outDefault = *defaultValue_.get();
115 return true;
116 }
117 outDefault = T{};
118 return false;
119 }
120
123 void setDefault(const T& defaultValue) {
124 if (defaultValue_) {
125 *defaultValue_.get() = defaultValue;
126 } else {
127 defaultValue_ = std::make_unique<T>(defaultValue);
128 }
129 }
130
135 bool getProperty(const string& propertyName, T& outValue) const {
136 auto iter = properties_.find(propertyName);
137 if (iter != properties_.end()) {
138 outValue = iter->second;
139 return true;
140 }
141 return false;
142 }
146 void setProperty(const string& propertyName, T value) {
147 properties_[propertyName] = value;
148 }
149
153 bool getMin(T& outMin) const {
154 return getProperty(kMinValue, outMin);
155 }
159 bool getMax(T& outMax) const {
160 return getProperty(kMaxValue, outMax);
161 }
165 bool getMinIncrement(T& outMinIncrement) const {
166 return getProperty(kMinIncrement, outMinIncrement);
167 }
171 bool getMaxIncrement(T& outMaxIncrement) const {
172 return getProperty(kMaxIncrement, outMaxIncrement);
173 }
178 void setMin(T min) {
179 properties_[kMinValue] = min;
180 }
185 void setMax(T max) {
186 properties_[kMaxValue] = max;
187 }
193 void setRange(T min, T max) {
194 properties_[kMinValue] = min;
195 properties_[kMaxValue] = max;
196 }
201 void setMinIncrement(T minIncrement) {
202 properties_[kMinIncrement] = minIncrement;
203 }
208 void setMaxIncrement(T maxIncrement) {
209 properties_[kMaxIncrement] = maxIncrement;
210 }
216 void setIncrement(T minIncrement, T maxIncrement) {
217 properties_[kMinIncrement] = minIncrement;
218 properties_[kMaxIncrement] = maxIncrement;
219 }
220
223 bool isAvailable() const override {
224 return layout_.getFixedData<T>(offset_, sizeof(T)) != nullptr;
225 }
226
231 void print(ostream& out, const string& indent) const override;
236 void printCompact(ostream& out, const string& indent) const override;
237
242 bool isSame(const DataPiece* rhs) const override;
243
247 void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile) override;
248
252 unique_ptr<DataPiece> clone() const override {
253 auto other = std::make_unique<DataPieceValue<T>>(getLabel());
254 other->tags_ = tags_;
255 other->required_ = required_;
256 other->properties_ = properties_;
257 if (defaultValue_) {
258 other->defaultValue_ = std::make_unique<T>(*defaultValue_);
259 }
260 return other;
261 }
262
263 protected:
264 bool stageFrom(const DataPiece* /*original*/) override {
265 return false; // not applicable
266 }
267
268 private:
269 map<string, T> properties_;
270 unique_ptr<T> defaultValue_;
271};
272
286template <typename EnumType, typename StorageType>
287class DataPieceEnum : public DataPieceValue<StorageType> {
288 public:
290 explicit DataPieceEnum(const string& label) : DataPieceValue<StorageType>(label) {}
291
294 DataPieceEnum(const string& label, EnumType defaultValue)
295 : DataPieceValue<StorageType>(label, static_cast<StorageType>(defaultValue)) {}
296
300 EnumType get() const {
301 return static_cast<EnumType>(DataPieceValue<StorageType>::get());
302 }
303
308 bool get(EnumType& e) const {
309 StorageType v{};
311 e = static_cast<EnumType>(v);
312 return r;
313 }
314
318 bool set(const EnumType e) {
319 return DataPieceValue<StorageType>::set(static_cast<StorageType>(e));
320 }
321
324 EnumType getDefault() const {
325 return static_cast<EnumType>(DataPieceValue<StorageType>::getDefault());
326 }
327
331 bool getDefault(EnumType& outDefault) const {
332 StorageType v;
334 outDefault = static_cast<EnumType>(v);
335 return r;
336 }
337
340 void setDefault(const EnumType defaultValue) {
341 DataPieceValue<StorageType>::setDefault(static_cast<StorageType>(defaultValue));
342 }
343};
344
345} // namespace vrs
346
347#endif // DATA_PIECES_VALUE_H
vector< int8_t > & getFixedData()
Definition DataLayout.h:237
DataPieceValue specialization class to store enums more conveniently and safely.
Definition DataPieceValue.h:287
bool get(EnumType &e) const
Definition DataPieceValue.h:308
DataPieceEnum(const string &label, EnumType defaultValue)
Definition DataPieceValue.h:294
EnumType get() const
Definition DataPieceValue.h:300
DataPieceEnum(const string &label)
Definition DataPieceValue.h:290
EnumType getDefault() const
Definition DataPieceValue.h:324
void setDefault(const EnumType defaultValue)
Definition DataPieceValue.h:340
bool set(const EnumType e)
Definition DataPieceValue.h:318
bool getDefault(EnumType &outDefault) const
Definition DataPieceValue.h:331
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
static const string kMaxValue
Special property name for the maximum value of the DataPiece.
Definition DataPieces.h:61
static const string kMaxIncrement
Special property name for the maximum increment of the DataPiece.
Definition DataPieces.h:65
static const string kMinValue
Special property name for the minimum value of the DataPiece.
Definition DataPieces.h:59
const string & getLabel() const
Definition DataPieces.h:76
DataPiece for a single value of type T. The value is stored in DataLayout's fixed size buffer.
Definition DataPieceValue.h:37
void setRange(T min, T max)
Definition DataPieceValue.h:193
void setMaxIncrement(T maxIncrement)
Definition DataPieceValue.h:208
bool get(T &outValue) const
Definition DataPieceValue.h:81
void print(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1176
DataPieceValue(const string &label, T defaultValue)
Definition DataPieceValue.h:49
bool isAvailable() const override
Definition DataPieceValue.h:223
bool getMin(T &outMin) const
Definition DataPieceValue.h:153
void serialize(JsonWrapper &jsonWrapper, const JsonFormatProfileSpec &profile) override
Definition DataLayout.cpp:1222
size_t getVariableSize() const override
Definition DataPieceValue.h:64
bool set(const T &value)
Definition DataPieceValue.h:95
bool getProperty(const string &propertyName, T &outValue) const
Definition DataPieceValue.h:135
bool isSame(const DataPiece *rhs) const override
Definition DataLayout.cpp:1212
void setMinIncrement(T minIncrement)
Definition DataPieceValue.h:201
void printCompact(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1203
bool getMaxIncrement(T &outMaxIncrement) const
Definition DataPieceValue.h:171
T get() const
Definition DataPieceValue.h:74
void setMax(T max)
Definition DataPieceValue.h:185
void setProperty(const string &propertyName, T value)
Definition DataPieceValue.h:146
void setIncrement(T minIncrement, T maxIncrement)
Definition DataPieceValue.h:216
bool stageFrom(const DataPiece *) override
Stage value from another piece known to be of the same type.
Definition DataPieceValue.h:264
const string & getElementTypeName() const override
Definition DataPieceValue.h:59
void setDefault(const T &defaultValue)
Definition DataPieceValue.h:123
T getDefault() const
Definition DataPieceValue.h:106
void setMin(T min)
Definition DataPieceValue.h:178
unique_ptr< DataPiece > clone() const override
Definition DataPieceValue.h:252
bool getMax(T &outMax) const
Definition DataPieceValue.h:159
bool getMinIncrement(T &outMinIncrement) const
Definition DataPieceValue.h:165
DataPieceValue(const string &label)
Definition DataPieceValue.h:45
size_t collectVariableData(int8_t *, size_t) override
Definition DataPieceValue.h:69
bool getDefault(T &outDefault) const
Definition DataPieceValue.h:112
Definition AsyncDiskFileChunk.hpp:49
DataPieceType
Specifier for a type of DataPiece.
Definition DataLayout.h:57
@ Value
Single value.
When printing out a DataLayout as json, this class allows to specify what should be included in the g...
Definition DataLayout.h:84