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 "DataPieces.h"
24#endif
25
26namespace vrs {
27
28using std::ostream;
29using std::string;
30using std::vector;
31
36template <typename T>
38 public:
40 explicit DataPieceStringMap(string label, map<string, T> defaultValues = {})
41 : DataPiece(std::move(label), DataPieceType::StringMap, DataLayout::kVariableSize),
42 defaultValues_{std::move(defaultValues)} {}
45 explicit DataPieceStringMap(const MakerBundle& bundle);
46
49 const string& getElementTypeName() const override {
50 return vrs::getTypeName<T>();
51 }
54 size_t getVariableSize() const override;
59 size_t collectVariableData(int8_t* data, size_t bufferSize) override;
60
63 const map<string, T>& stagedValues() const {
64 return stagedValues_;
65 }
68 map<string, T>& stagedValues() {
69 return stagedValues_;
70 }
73 void stage(const map<string, T>& values) {
74 stagedValues_ = values;
75 }
78 void stage(map<string, T>&& values) {
79 stagedValues_ = std::move(values);
80 }
81
85 bool get(map<string, T>& outValues) const;
86
91 void setDefault(const map<string, T>& values) {
92 defaultValues_ = values;
93 }
98 void setDefault(map<string, T>&& values) {
99 defaultValues_ = std::move(values);
100 }
103 const map<string, T>& getDefault() const {
104 return defaultValues_;
105 }
107 void initToDefault() override {
108 stagedValues_ = defaultValues_;
109 }
110
115 bool patchValue(const map<string, T>& values) const {
116 auto* patchedPiece = layout_.getMappedPiece<DataPieceStringMap<T>>(pieceIndex_);
117 return patchedPiece != nullptr && (patchedPiece->stage(values), true);
118 }
119
122 bool isAvailable() const override {
123 size_t count = 0;
124 return layout_.getVarData<int8_t>(offset_, count) != nullptr;
125 }
126
131 void print(ostream& out, const string& indent) const override;
136 void printCompact(ostream& out, const string& indent) const override;
137
142 bool isSame(const DataPiece* rhs) const override;
143
147 void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile) override;
148
151 bool stageCurrentValue() override {
152 return get(stagedValues());
153 }
154
158 unique_ptr<DataPiece> clone() const override {
159 auto other = std::make_unique<DataPieceStringMap<T>>(getLabel());
160 other->tags_ = tags_;
161 other->required_ = required_;
162 other->defaultValues_ = defaultValues_;
163 return other;
164 }
165
166 protected:
167 bool copyFrom(const DataPiece* original) override {
168 const DataPieceStringMap<T>* origMap = reinterpret_cast<const DataPieceStringMap<T>*>(original);
169 return origMap->get(stagedValues_);
170 }
171
172 private:
173 map<string, T> stagedValues_; // values to write to disk
174 map<string, T> defaultValues_;
175};
176
177} // namespace vrs
178
179#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:37
void setDefault(map< string, T > &&values)
Definition DataPieceStringMap.h:98
void printCompact(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1684
void initToDefault() override
Stage default value.
Definition DataPieceStringMap.h:107
unique_ptr< DataPiece > clone() const override
Definition DataPieceStringMap.h:158
void stage(map< string, T > &&values)
Definition DataPieceStringMap.h:78
void print(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1650
bool stageCurrentValue() override
Definition DataPieceStringMap.h:151
bool patchValue(const map< string, T > &values) const
Definition DataPieceStringMap.h:115
bool isAvailable() const override
Definition DataPieceStringMap.h:122
size_t getVariableSize() const override
Definition DataLayout.cpp:1598
const string & getElementTypeName() const override
Definition DataPieceStringMap.h:49
void stage(const map< string, T > &values)
Definition DataPieceStringMap.h:73
map< string, T > & stagedValues()
Definition DataPieceStringMap.h:68
bool isSame(const DataPiece *rhs) const override
Definition DataLayout.cpp:1712
size_t collectVariableData(int8_t *data, size_t bufferSize) override
Definition DataLayout.cpp:1607
const map< string, T > & stagedValues() const
Definition DataPieceStringMap.h:63
void serialize(JsonWrapper &jsonWrapper, const JsonFormatProfileSpec &profile) override
Definition DataLayout.cpp:1721
bool get(map< string, T > &outValues) const
Definition DataLayout.cpp:1619
bool copyFrom(const DataPiece *original) override
Set or stage value from another piece known to be of the same type.
Definition DataPieceStringMap.h:167
DataPieceStringMap(string label, map< string, T > defaultValues={})
Definition DataPieceStringMap.h:40
const map< string, T > & getDefault() const
Definition DataPieceStringMap.h:103
void setDefault(const map< string, T > &values)
Definition DataPieceStringMap.h:91
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