VRS
A file format for sensor data.
Loading...
Searching...
No Matches
DataPieceString.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_H
20#define DATA_PIECES_STRING_H
21
22#ifndef DATA_PIECES_H
23#include <vrs/DataPieces.h>
24#endif
25
26#include <vrs/VrsExport.h>
27
28namespace vrs {
29
30using std::min;
31using std::ostream;
32using std::string;
33
38class VRS_API DataPieceString : public DataPiece {
39 public:
41 explicit DataPieceString(string label, string defaultValue = {})
42 : DataPiece(std::move(label), DataPieceType::String, DataLayout::kVariableSize),
43 defaultString_{std::move(defaultValue)} {}
46 explicit DataPieceString(const MakerBundle& bundle);
47
50 const string& getElementTypeName() const override;
53 size_t getVariableSize() const override {
54 return stagedString_.size();
55 }
60 size_t collectVariableData(int8_t* data, size_t bufferSize) override;
61
65 void stage(const string& string) {
66 stagedString_ = string;
67 }
71 void stage(string&& string) {
72 stagedString_ = std::move(string);
73 }
77 const string& stagedValue() const {
78 return stagedString_;
79 }
83 string& stagedValue() {
84 return stagedString_;
85 }
86
89 string get() const;
92 bool get(string& outString) const;
93
97 void setDefault(const string& defaultString) {
98 defaultString_ = defaultString;
99 }
103 void setDefault(string&& defaultString) {
104 defaultString_ = std::move(defaultString);
105 }
108 const string& getDefault() const {
109 return defaultString_;
110 }
111
113 void initToDefault() override {
114 stagedString_ = defaultString_;
115 }
116
121 bool patchValue(const string& str) const {
122 auto* patchedPiece = layout_.getMappedPiece<DataPieceString>(pieceIndex_);
123 return patchedPiece != nullptr && (patchedPiece->stage(str), true);
124 }
125
127 bool isAvailable() const override;
128
133 void print(ostream& out, const string& indent) const override;
138 void printCompact(ostream& out, const string& indent) const override;
143 bool isSame(const DataPiece* rhs) const override;
144
148 void serialize(JsonWrapper& jsonWrapper, const JsonFormatProfileSpec& profile) override;
149
152 bool stageCurrentValue() override {
153 return get(stagedValue());
154 }
155
159 unique_ptr<DataPiece> clone() const override {
160 auto other = std::make_unique<DataPieceString>(getLabel());
161 other->tags_ = tags_;
162 other->required_ = required_;
163 other->defaultString_ = defaultString_;
164 return other;
165 }
166
167 protected:
168 bool copyFrom(const DataPiece* original) override {
169 const DataPieceString* cloneString = reinterpret_cast<const DataPieceString*>(original);
170 return cloneString->get(stagedString_);
171 }
172
173 private:
174 string stagedString_;
175 string defaultString_;
176};
177
178} // namespace vrs
179
180#endif // DATA_PIECES_STRING_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
DataPiece for variable length string.
Definition DataPieceString.h:38
bool copyFrom(const DataPiece *original) override
Set or stage value from another piece known to be of the same type.
Definition DataPieceString.h:168
void setDefault(const string &defaultString)
Definition DataPieceString.h:97
DataPieceString(string label, string defaultValue={})
Definition DataPieceString.h:41
string & stagedValue()
Definition DataPieceString.h:83
bool stageCurrentValue() override
Definition DataPieceString.h:152
size_t getVariableSize() const override
Definition DataPieceString.h:53
void setDefault(string &&defaultString)
Definition DataPieceString.h:103
const string & getDefault() const
Definition DataPieceString.h:108
bool patchValue(const string &str) const
Definition DataPieceString.h:121
void stage(const string &string)
Definition DataPieceString.h:65
string get() const
Definition DataLayout.cpp:1776
void stage(string &&string)
Definition DataPieceString.h:71
void initToDefault() override
Stage default value.
Definition DataPieceString.h:113
unique_ptr< DataPiece > clone() const override
Definition DataPieceString.h:159
const string & stagedValue() const
Definition DataPieceString.h:77
Definition Compressor.cpp:113
DataPieceType
Specifier for a type of DataPiece.
Definition DataLayout.h:59
@ String
Variable size array of char, null terminated.
When printing out a DataLayout as json, this class allows to specify what should be included in the g...
Definition DataLayout.h:86