VRS
A file format for sensor data.
Loading...
Searching...
No Matches
DataLayout.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#include <array>
20
21#include <functional>
22#include <memory>
23#include <ostream>
24#include <string>
25#include <vector>
26
27#include <vrs/ForwardDefinitions.h>
28#include <vrs/VrsExport.h>
29
30namespace vrs {
31
32using std::ostream;
33using std::string;
34using std::unique_ptr;
35using std::vector;
36
37class DataPiece;
39
40template <typename T>
41class DataPieceValue;
42template <typename T>
43class DataPieceArray;
44template <typename T>
45class DataPieceVector;
46template <typename T>
48class DataPieceString;
49
50namespace internal {
51class DataLayouter;
52struct DataPieceFactory;
53} // namespace internal
54
56struct JsonWrapper;
57
59enum class DataPieceType : uint8_t {
61 Value = 1,
62 Array = 2,
63 Vector = 3,
64 String = 4,
66
67 COUNT
68};
69
77
86struct VRS_API JsonFormatProfileSpec {
87 bool publicNames = false;
88 bool prettyJson = false;
89 bool value = false;
90 bool name = true;
91 bool type = true;
92 bool shortType = false;
93 bool index = true;
94 bool defaults = true;
95 bool tags = true;
96 bool properties = true;
97 bool required = true;
98
99 // Default format
100 JsonFormatProfileSpec() = default;
101 explicit JsonFormatProfileSpec(JsonFormatProfile profile);
102};
103
210class VRS_API DataLayout {
211 protected:
212 DataLayout() = default;
213
214 public:
215 DataLayout(const DataLayout&) = delete;
216 DataLayout(const DataLayout&&) = delete;
217
218 virtual ~DataLayout(); // for derived classes
219
220 DataLayout& operator=(const DataLayout&) = delete;
221 DataLayout& operator=(const DataLayout&&) = delete;
222
224 static const size_t kNotFound;
226 static const size_t kVariableSize;
227
228// Pack and use unit32_t because we're storing on disk, and size_t might be 32 or 64 bits
229#pragma pack(push, 1)
232 public:
233 void setOffset(size_t offset) {
234 offset_ = static_cast<uint32_t>(offset);
235 }
236 size_t getOffset() const {
237 return static_cast<uint32_t>(offset_);
238 }
239 void setLength(size_t length) {
240 length_ = static_cast<uint32_t>(length);
241 }
242 size_t getLength() const {
243 return static_cast<uint32_t>(length_);
244 }
245
246 private:
247 uint32_t offset_{};
248 uint32_t length_{};
249 };
250#pragma pack(pop)
251
254
257 vector<int8_t>& getFixedData() {
258 return fixedData_;
259 }
260
262 vector<int8_t>& getVarData() {
263 return varData_;
264 }
265
267 size_t getFixedDataSizeNeeded() const {
269 }
270
276 void initDataPiecesToDefaultValue();
277
286 size_t getVarDataSizeFromIndex() const;
287
291 size_t getVarDataSizeNeeded() const;
292
295 void collectVariableDataAndUpdateIndex();
300 void collectVariableDataAndUpdateIndex(void* destination);
304 void getRawData(vector<int8_t>& outRawData) const;
310 void stageCurrentValues();
316 bool copyClonedDataPieceValues(const DataLayout& originalLayout);
329 size_t copyDataPieceValuesFromMappedLayout(const DataLayout& mappedLayout);
330
348 bool mapLayout(DataLayout& targetLayout);
351 bool isMapped() const {
352 return mappedDataLayout_ != nullptr;
353 }
354
359 bool hasAllRequiredPieces() const {
360 return mappedDataLayout_ == nullptr || hasAllRequiredPieces_;
361 }
362
363 void requireAllPieces();
364
370 void printLayout(ostream& out, const string& indent = "") const;
374 void printLayoutCompact(ostream& out, const string& indent = "") const;
375
380 string asJson(JsonFormatProfile profile) const;
385 string asJson(const JsonFormatProfileSpec& profile = JsonFormatProfileSpec()) const;
386
388 string getListOfPiecesSpec() const;
389
395 bool isSame(const DataLayout& otherLayout) const;
396
401 static unique_ptr<DataLayout> makeFromJson(const string& json);
402
407 template <class T>
408 const DataPieceValue<T>* findDataPieceValue(const string& label) const;
409 template <class T>
410 DataPieceValue<T>* findDataPieceValue(const string& label);
415 template <class T>
416 const DataPieceArray<T>* findDataPieceArray(const string& label, size_t arraySize) const;
417 template <class T>
418 DataPieceArray<T>* findDataPieceArray(const string& label, size_t arraySize);
423 template <class T>
424 const DataPieceVector<T>* findDataPieceVector(const string& label) const;
425 template <class T>
426 DataPieceVector<T>* findDataPieceVector(const string& label);
431 template <class T>
432 const DataPieceStringMap<T>* findDataPieceStringMap(const string& label) const;
433 template <class T>
434 DataPieceStringMap<T>* findDataPieceStringMap(const string& label);
439 const DataPieceString* findDataPieceString(const string& label) const;
440 DataPieceString* findDataPieceString(const string& label);
445 const std::function<void(const DataPiece*)>&,
449 const std::function<void(DataPiece*)>&,
451
455 bool isVarDataIndexValid() const;
459 return fixedSizePieces_.size();
460 }
461
464 return varSizePieces_.size();
465 }
466
470 size_t getAvailableFixedDataPiecesCount() const;
475 size_t getAvailableVarDataPiecesCount() const;
476
477 protected:
483 template <class T>
484 T* getFixedData(size_t offset, size_t size) {
485 if (mappedDataLayout_ != nullptr) {
486 return mappedDataLayout_->getFixedData<T>(offset, size);
487 }
488 if (offset != kNotFound && offset + size <= fixedData_.size()) {
489 return reinterpret_cast<T*>(fixedData_.data() + offset);
490 }
491 return nullptr;
492 }
493
496 const IndexEntry* getVarSizeIndex() const;
500 IndexEntry* getVarSizeIndex();
506 template <class T>
507 T* getVarData(size_t varPieceIndex, size_t& outCount) {
508 if (mappedDataLayout_ != nullptr) {
509 return mappedDataLayout_->getVarData<T>(varPieceIndex, outCount);
510 }
511 if (varPieceIndex < varSizePieces_.size()) {
512 const IndexEntry& indexEntry = getVarSizeIndex()[varPieceIndex];
513 if (indexEntry.getOffset() + indexEntry.getLength() <= varData_.size()) {
514 outCount = indexEntry.getLength() / sizeof(T);
515 return reinterpret_cast<T*>(varData_.data() + indexEntry.getOffset());
516 }
517 }
518 outCount = 0;
519 return nullptr;
520 }
521
522 DataPiece* getPieceByIndex(size_t pieceIndex);
524 template <class T>
525 T* getMappedPiece(size_t pieceIndex) const {
526 return mappedDataLayout_ != nullptr
527 ? static_cast<T*>(mappedDataLayout_->getPieceByIndex(pieceIndex))
528 : nullptr;
529 }
530
535 void initLayout();
536
540 void serialize(JsonWrapper& rj, const JsonFormatProfileSpec& profile) const;
541
542 static DataPiece* findMatch(DataPiece* piece, const vector<DataPiece*>& pieces, size_t& start);
543 static bool mapPieces(
544 const vector<DataPiece*>& searchPieces,
545 const vector<DataPiece*>& givenPieces);
546 static size_t copyMappedValues(
547 const vector<DataPiece*>& pieces,
548 const vector<DataPiece*>& mappedPieces);
549
550 friend class internal::DataLayouter;
551 friend class DataPiece;
552 template <class T>
553 friend class DataPieceValue;
554 template <class T>
555 friend class DataPieceArray;
556 template <class T>
557 friend class DataPieceVector;
558 template <class T>
559 friend class DataPieceStringMap;
560 friend class DataPieceString;
561
563 vector<DataPiece*> fixedSizePieces_;
565 vector<DataPiece*> varSizePieces_;
567 vector<int8_t> fixedData_;
571 vector<int8_t> varData_;
575 DataLayout* mappedDataLayout_{};
576};
577
579class VRS_API EmptyDataLayout : public DataLayout {
580 public:
581 EmptyDataLayout() {
582 initLayout();
583 }
584};
585
604class VRS_API AutoDataLayout : public DataLayout {
605 public:
606 AutoDataLayout();
607};
608
610class VRS_API AutoDataLayoutEnd {
611 public:
612 AutoDataLayoutEnd();
613};
614
619class VRS_API ManualDataLayout : public DataLayout {
620 public:
623 explicit ManualDataLayout(const string& json);
624
629 explicit ManualDataLayout(const DataLayout& layout);
630
631 ~ManualDataLayout() override;
632
638 DataPiece* add(unique_ptr<DataPiece> dataPiece);
639
641 void endLayout();
642
643 private:
644 vector<unique_ptr<DataPiece>> manualPieces;
645 bool layoutInProgress_;
646};
647
680struct VRS_API DataLayoutStruct {
681 explicit DataLayoutStruct(const string& structName);
682 static void dataLayoutStructEnd(const string& structName);
683};
684
685#define DATA_LAYOUT_STRUCT(DATA_LAYOUT_STRUCT_TYPE) \
686 explicit DATA_LAYOUT_STRUCT_TYPE(const std::string& _structName_) \
687 : DataLayoutStruct(_structName_) { \
688 dataLayoutStructEnd(_structName_); \
689 }
690
691#define DATA_LAYOUT_STRUCT_WITH_INIT(DATA_LAYOUT_STRUCT_TYPE) \
692 explicit DATA_LAYOUT_STRUCT_TYPE(const std::string& _structName_) \
693 : DataLayoutStruct(_structName_) { \
694 dataLayoutStructEnd(_structName_); \
695 init(); \
696 }
697
726template <typename T, size_t Size>
728 DATA_LAYOUT_STRUCT(DataLayoutStructArray)
729 std::array<T, Size> array{createArrayHelper<T>(std::make_index_sequence<Size>())};
730
731 T& operator[](const size_t index) {
732 return array[index];
733 }
734
735 constexpr const T& operator[](const size_t index) const {
736 return array[index];
737 }
738
739 constexpr std::size_t size() const noexcept {
740 return array.size();
741 }
742
743 template <typename S, size_t... Indices>
744 static constexpr auto createArrayHelper(std::index_sequence<Indices...>) {
745 return std::array<S, sizeof...(Indices)>{S{std::to_string(Indices)}...};
746 }
747};
748
752template <class OptionalFields>
753class OptionalDataPieces : public std::unique_ptr<OptionalFields> {
754 public:
755 explicit OptionalDataPieces(bool allocateFields)
756 : std::unique_ptr<OptionalFields>(
757 allocateFields ? std::make_unique<OptionalFields>() : nullptr) {}
758};
759
760} // namespace vrs
For use within an AutoDataLayout class, to end the AutoDataLayout's construction.
Definition DataLayout.h:610
Specification of a VRS record content block.
Definition RecordFormat.h:522
Describes where the data of a variable size DataPiece is in the varData_ buffer.
Definition DataLayout.h:231
The DataLayout class describes the data stored inside a DataLayoutContentBlock.
Definition DataLayout.h:210
vector< DataPiece * > fixedSizePieces_
Ordered fixed-size DataPieces.
Definition DataLayout.h:563
T * getFixedData(size_t offset, size_t size)
Definition DataLayout.h:484
void initLayout()
Definition DataLayout.cpp:288
bool hasAllRequiredPieces() const
Definition DataLayout.h:359
size_t fixedDataSizeNeeded_
Byte count for all the fixed size pieces + var size index.
Definition DataLayout.h:569
static const size_t kVariableSize
Special value used for a DataPiece size, telling that that DataPiece has a variable size.
Definition DataLayout.h:226
void forEachDataPiece(const std::function< void(const DataPiece *)> &, DataPieceType type=DataPieceType::Undefined) const
T * getVarData(size_t varPieceIndex, size_t &outCount)
Definition DataLayout.h:507
vector< int8_t > & getVarData()
Definition DataLayout.h:262
const IndexEntry * getVarSizeIndex() const
Definition DataLayout.cpp:718
ContentBlock getContentBlock() const
Definition DataLayout.cpp:282
vector< DataPiece * > varSizePieces_
Ordered variable-size DataPieces.
Definition DataLayout.h:565
bool hasAllRequiredPieces_
Tells all the required pieces have been mapped successfully.
Definition DataLayout.h:573
bool isMapped() const
Definition DataLayout.h:351
size_t getFixedDataSizeNeeded() const
Definition DataLayout.h:267
bool isVarDataIndexValid() const
Definition DataLayout.cpp:734
static const size_t kNotFound
Special OffsetAndLength offset value marking that a piece of data isn't available.
Definition DataLayout.h:224
vector< int8_t > & getFixedData()
Definition DataLayout.h:257
T * getMappedPiece(size_t pieceIndex) const
Get a typed piece by index in the mapped datalayout, exclusively.
Definition DataLayout.h:525
size_t getDeclaredVarDataPiecesCount() const
Definition DataLayout.h:463
size_t getDeclaredFixedDataPiecesCount() const
Definition DataLayout.h:458
void forEachDataPiece(const std::function< void(DataPiece *)> &, DataPieceType type=DataPieceType::Undefined)
Same as above, but as a non-const version.
vector< int8_t > fixedData_
Buffer to hold fixed-size pieces, and the index of var size pieces (if any).
Definition DataLayout.h:567
DataLayout * mappedDataLayout_
DataLayout this layout has been mapped to, if any.
Definition DataLayout.h:575
vector< int8_t > varData_
Buffer holding variable-size pieces, after they've been collected, or read from disk.
Definition DataLayout.h:571
Fixed size array of POD values.
Definition DataPieceArray.h:39
Abstract class representing a piece of information part of a DataLayout.
Definition DataPieces.h:42
DataPiece for variable length string.
Definition DataPieceString.h:38
DataPiece map container, with string keys and values of type T.
Definition DataPieceStringMap.h:36
DataPiece for a single value of type T. The value is stored in DataLayout's fixed size buffer.
Definition DataPieceValue.h:37
Vector of type T and variable size.
Definition DataPieceVector.h:39
void endLayout()
End the construction of the DataLayout. Do not call add() after calling this method.
Definition DataLayout.cpp:1932
ManualDataLayout(const string &json)
Definition DataLayout.cpp:1877
DataPiece * add(unique_ptr< DataPiece > dataPiece)
Definition DataLayout.cpp:1924
Helper class to manage the registration of DataPiece objects within a single DataLayout.
Definition DataLayout.cpp:409
DataLayout private namespace for internal helper classes & functions.
Definition DataLayout.cpp:396
Definition Compressor.cpp:130
@ Undefined
when not set explicitly
Definition Compressor.h:40
JsonFormatProfile
Enum for a DataLayout printout json formatting profile.
Definition DataLayout.h:71
@ ExternalCompact
for external tools (VRStools in particular), but compact.
Definition DataLayout.h:73
@ VrsFormat
for internal VRS usage. (default)
Definition DataLayout.h:72
@ Public
for public use cases, avoiding VRS internal names
Definition DataLayout.h:75
@ ExternalPretty
for external tools (VRStools in particular), formatted for readability.
Definition DataLayout.h:74
DataPieceType
Specifier for a type of DataPiece.
Definition DataLayout.h:59
@ String
Variable size array of char, null terminated.
Definition DataLayout.h:64
@ Array
Fixed size array.
Definition DataLayout.h:62
@ Vector
Variable size array of T.
Definition DataLayout.h:63
@ Value
Single value.
Definition DataLayout.h:61
@ StringMap
Map with string keys, and T values.
Definition DataLayout.h:65
@ Undefined
Undefined type.
Definition DataLayout.h:60
Helper class to include DataLayout structs containing a sliced array of DataPieceXXX and DataLayoutSt...
Definition DataLayout.h:727
Helper class to include DataLayout structs containing a set of DataPieceXXX and DataLayoutStruct whil...
Definition DataLayout.h:680
When printing out a DataLayout as json, this class allows to specify what should be included in the g...
Definition DataLayout.h:86
bool tags
Include tags.
Definition DataLayout.h:95
bool shortType
Use the short version of the type names.
Definition DataLayout.h:92
bool required
Include the required flag.
Definition DataLayout.h:97
bool defaults
Include default values.
Definition DataLayout.h:94
bool value
Include the value of the data piece elements.
Definition DataLayout.h:89
bool publicNames
Use internal names, or public names. "data_layout" vs. "metadata".
Definition DataLayout.h:87
bool name
Include the label name.
Definition DataLayout.h:90
bool prettyJson
Format the text so that it is easier to read.
Definition DataLayout.h:88
bool type
Include the type name.
Definition DataLayout.h:91
bool properties
Includes properties.
Definition DataLayout.h:96
bool index
Include the index of the data pieces.
Definition DataLayout.h:93
Helper factory class to create DataPiece objects.
Definition DataLayout.cpp:536