VRS
A file format for sensor data.
Loading...
Searching...
No Matches
DataPieceStringMap.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_STRING_MAP_H
20#define DATA_PIECES_STRING_MAP_H
21
22#ifndef DATA_PIECES_H
23#include <vrs/DataPieces.h>
24#endif
25
26namespace vrs {
27
28using std::ostream;
29using std::string;
30
35template <typename T>
37 public:
39 explicit DataPieceStringMap(string label, map<string, T> defaultValues = {})
40 : DataPiece(std::move(label), DataPieceType::StringMap, DataLayout::kVariableSize),
41 defaultValues_{std::move(defaultValues)} {}
44 explicit DataPieceStringMap(const MakerBundle& bundle);
45
48 const string& getElementTypeName() const override {
49 return vrs::getTypeName<T>();
50 }
53 size_t getVariableSize() const override;
58 size_t collectVariableData(int8_t* data, size_t bufferSize) override;
59
62 const map<string, T>& stagedValues() const {
63 return stagedValues_;
64 }
67 map<string, T>& stagedValues() {
68 return stagedValues_;
69 }
72 void stage(const map<string, T>& values) {
73 stagedValues_ = values;
74 }
77 void stage(map<string, T>&& values) {
78 stagedValues_ = std::move(values);
79 }
80
84 bool get(map<string, T>& outValues) const;
85
90 void setDefault(const map<string, T>& values) {
91 defaultValues_ = values;
92 }
97 void setDefault(map<string, T>&& values) {
98 defaultValues_ = std::move(values);
99 }
102 const map<string, T>& getDefault() const {
103 return defaultValues_;
104 }
106 void initToDefault() override {
107 stagedValues_ = defaultValues_;
108 }
109
114 bool patchValue(const map<string, T>& values) const {
115 auto* patchedPiece = layout_.getMappedPiece<DataPieceStringMap<T>>(pieceIndex_);
116 return patchedPiece != nullptr && (patchedPiece->stage(values), true);
117 }
118
121 bool isAvailable() const override {
122 size_t count = 0;
123 return layout_.getVarData<int8_t>(offset_, count) != nullptr;
124 }
125
130 void print(ostream& out, const string& indent) const override;
135 void printCompact(ostream& out, const string& indent) const override;
136
141 bool isSame(const DataPiece* rhs) const override;
142
146 void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile) override;
147
150 bool stageCurrentValue() override {
151 return get(stagedValues());
152 }
153
157 unique_ptr<DataPiece> clone() const override {
158 auto other = std::make_unique<DataPieceStringMap<T>>(getLabel());
159 other->tags_ = tags_;
160 other->required_ = required_;
161 other->defaultValues_ = defaultValues_;
162 return other;
163 }
164
165 protected:
166 bool copyFrom(const DataPiece* original) override {
167 const DataPieceStringMap<T>* origMap = reinterpret_cast<const DataPieceStringMap<T>*>(original);
168 return origMap->get(stagedValues_);
169 }
170
171 private:
172 map<string, T> stagedValues_; // values to write to disk
173 map<string, T> defaultValues_;
174};
175
176} // namespace vrs
177
178#endif // DATA_PIECES_STRING_MAP_H
The DataLayout class describes the data stored inside a DataLayoutContentBlock.
Definition DataLayout.h:209
vector< int8_t > & getVarData()
Definition DataLayout.h:261
T * getMappedPiece(size_t pieceIndex) const
Get a typed piece by index in the mapped datalayout, exclusively.
Definition DataLayout.h:524
Abstract class representing a piece of information part of a DataLayout.
Definition DataPieces.h:40
const string & getLabel() const
Definition DataPieces.h:76
DataPiece map container, with string keys and values of type T.
Definition DataPieceStringMap.h:36
void setDefault(map< string, T > &&values)
Definition DataPieceStringMap.h:97
void printCompact(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1700
void initToDefault() override
Stage default value.
Definition DataPieceStringMap.h:106
unique_ptr< DataPiece > clone() const override
Definition DataPieceStringMap.h:157
void stage(map< string, T > &&values)
Definition DataPieceStringMap.h:77
void print(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1666
bool stageCurrentValue() override
Definition DataPieceStringMap.h:150
bool patchValue(const map< string, T > &values) const
Definition DataPieceStringMap.h:114
bool isAvailable() const override
Definition DataPieceStringMap.h:121
size_t getVariableSize() const override
Definition DataLayout.cpp:1614
const string & getElementTypeName() const override
Definition DataPieceStringMap.h:48
void stage(const map< string, T > &values)
Definition DataPieceStringMap.h:72
map< string, T > & stagedValues()
Definition DataPieceStringMap.h:67
bool isSame(const DataPiece *rhs) const override
Definition DataLayout.cpp:1728
size_t collectVariableData(int8_t *data, size_t bufferSize) override
Definition DataLayout.cpp:1623
const map< string, T > & stagedValues() const
Definition DataPieceStringMap.h:62
void serialize(JsonWrapper &jsonWrapper, const JsonFormatProfileSpec &profile) override
Definition DataLayout.cpp:1737
bool get(map< string, T > &outValues) const
Definition DataLayout.cpp:1635
bool copyFrom(const DataPiece *original) override
Set or stage value from another piece known to be of the same type.
Definition DataPieceStringMap.h:166
DataPieceStringMap(string label, map< string, T > defaultValues={})
Definition DataPieceStringMap.h:39
const map< string, T > & getDefault() const
Definition DataPieceStringMap.h:102
void setDefault(const map< string, T > &values)
Definition DataPieceStringMap.h:90
Definition Compressor.cpp:113
DataPieceType
Specifier for a type of DataPiece.
Definition DataLayout.h:57
@ StringMap
Map with string keys, and T values.
When printing out a DataLayout as json, this class allows to specify what should be included in the g...
Definition DataLayout.h:84