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
89 const map<string, T>& getDefault() const {
90 return defaultValues_;
91 }
94 void setDefault(const map<string, T>& values) {
95 defaultValues_ = values;
96 }
99 void setDefault(map<string, T>&& values) {
100 defaultValues_ = std::move(values);
101 }
102
104 void initToDefault() override {
105 stagedValues_ = defaultValues_;
106 }
107
112 bool patchValue(const map<string, T>& values) const {
113 auto* patchedPiece = layout_.getMappedPiece<DataPieceStringMap<T>>(pieceIndex_);
114 return patchedPiece != nullptr && (patchedPiece->stage(values), true);
115 }
116
119 bool isAvailable() const override {
120 size_t count = 0;
121 return layout_.getVarData<int8_t>(offset_, count) != nullptr;
122 }
123
128 void print(ostream& out, const string& indent) const override;
133 void printCompact(ostream& out, const string& indent) const override;
134
139 bool isSame(const DataPiece* rhs) const override;
140
144 void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile) override;
145
148 bool stageCurrentValue() override {
149 return get(stagedValues());
150 }
151
155 unique_ptr<DataPiece> clone() const override {
156 auto other = std::make_unique<DataPieceStringMap<T>>(getLabel());
157 other->tags_ = tags_;
158 other->required_ = required_;
159 other->defaultValues_ = defaultValues_;
160 return other;
161 }
162
163 protected:
164 bool copyFrom(const DataPiece* original) override {
165 const DataPieceStringMap<T>* origMap = reinterpret_cast<const DataPieceStringMap<T>*>(original);
166 return origMap->get(stagedValues_);
167 }
168
169 private:
170 map<string, T> stagedValues_; // values to write to disk
171 map<string, T> defaultValues_;
172};
173
174} // namespace vrs
175
176#endif // DATA_PIECES_STRING_MAP_H
The DataLayout class describes the data stored inside a DataLayoutContentBlock.
Definition DataLayout.h:191
vector< int8_t > & getVarData()
Definition DataLayout.h:242
T * getMappedPiece(size_t pieceIndex) const
Get a typed piece by index in the mapped datalayout, exclusively.
Definition DataLayout.h:505
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:99
void printCompact(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1688
void initToDefault() override
Initialize to default value.
Definition DataPieceStringMap.h:104
unique_ptr< DataPiece > clone() const override
Definition DataPieceStringMap.h:155
void stage(map< string, T > &&values)
Definition DataPieceStringMap.h:78
void print(ostream &out, const string &indent) const override
Definition DataLayout.cpp:1654
bool stageCurrentValue() override
Definition DataPieceStringMap.h:148
bool patchValue(const map< string, T > &values) const
Definition DataPieceStringMap.h:112
bool isAvailable() const override
Definition DataPieceStringMap.h:119
size_t getVariableSize() const override
Definition DataLayout.cpp:1609
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:1716
size_t collectVariableData(int8_t *data, size_t bufferSize) override
Definition DataLayout.cpp:1618
const map< string, T > & stagedValues() const
Definition DataPieceStringMap.h:63
void serialize(JsonWrapper &jsonWrapper, const JsonFormatProfileSpec &profile) override
Definition DataLayout.cpp:1725
bool get(map< string, T > &outValues) const
Definition DataLayout.cpp:1630
bool copyFrom(const DataPiece *original) override
Set or stage value from another piece known to be of the same type.
Definition DataPieceStringMap.h:164
DataPieceStringMap(string label, map< string, T > defaultValues={})
Definition DataPieceStringMap.h:40
const map< string, T > & getDefault() const
Definition DataPieceStringMap.h:89
void setDefault(const map< string, T > &values)
Definition DataPieceStringMap.h:94
Definition AsyncDiskFileChunk.hpp:49
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