27class MutableDynamicStruct;
28class StructDescriptor;
29class StructDescriptorDatabase;
67 struct private_init {};
77 using EnumValues = std::vector<std::pair<std::string, int64_t>>;
97 const std::string&
GetName()
const {
return m_name; }
112 return m_type == StructFieldType::kInt8 ||
113 m_type == StructFieldType::kInt16 ||
114 m_type == StructFieldType::kInt32 ||
115 m_type == StructFieldType::kInt64;
124 return m_type == StructFieldType::kUint8 ||
125 m_type == StructFieldType::kUint16 ||
126 m_type == StructFieldType::kUint32 ||
127 m_type == StructFieldType::kUint64;
150 return m_bitWidth == 0 ? m_size * 8 : m_bitWidth;
173 bool IsArray()
const {
return m_arraySize > 1; }
187 bool HasEnum()
const {
return !m_enum.empty(); }
223 return static_cast<int64_t
>(-(m_bitMask >> 1)) - 1;
239 return m_bitShift != 0 || m_bitWidth != (m_size * 8);
253 unsigned int m_bitWidth;
254 unsigned int m_bitShift = 0;
261 struct private_init {};
272 const std::string&
GetName()
const {
return m_name; }
279 const std::string&
GetSchema()
const {
return m_schema; }
314 const std::vector<StructFieldDescriptor>&
GetFields()
const {
321 std::string CalculateOffsets(
325 std::string m_schema;
326 std::vector<StructDescriptor*> m_references;
327 std::vector<StructFieldDescriptor> m_fields;
330 bool m_valid =
false;
377 :
m_desc{desc}, m_data{data} {}
391 std::span<const uint8_t>
GetData()
const {
return m_data; }
411 size_t arrIndex = 0)
const {
412 assert(field->m_type == StructFieldType::kBool);
413 return GetFieldImpl(field, arrIndex);
424 size_t arrIndex = 0)
const {
425 assert(field->
IsInt());
426 return GetFieldImpl(field, arrIndex);
437 size_t arrIndex = 0)
const {
439 return GetFieldImpl(field, arrIndex);
450 size_t arrIndex = 0)
const {
451 assert(field->m_type == StructFieldType::kFloat);
452 return bit_cast<float>(
453 static_cast<uint32_t
>(GetFieldImpl(field, arrIndex)));
464 size_t arrIndex = 0)
const {
465 assert(field->m_type == StructFieldType::kDouble);
466 return bit_cast<double>(GetFieldImpl(field, arrIndex));
485 size_t arrIndex = 0)
const {
486 assert(field->m_type == StructFieldType::kStruct);
487 assert(field->m_parent ==
m_desc);
489 assert(arrIndex < field->m_arraySize);
491 m_data.subspan(field->m_offset +
492 arrIndex * field->m_struct->
GetSize())};
500 size_t arrIndex)
const;
502 std::span<const uint8_t> m_data;
526 std::span<uint8_t>
GetData() {
return m_data; }
545 size_t arrIndex = 0) {
546 assert(field->m_type == StructFieldType::kBool);
547 SetFieldImpl(field, value ? 1 : 0, arrIndex);
558 size_t arrIndex = 0) {
559 assert(field->
IsInt());
560 SetFieldImpl(field, value, arrIndex);
571 size_t arrIndex = 0) {
573 SetFieldImpl(field, value, arrIndex);
584 size_t arrIndex = 0) {
585 assert(field->m_type == StructFieldType::kFloat);
586 SetFieldImpl(field, bit_cast<uint32_t>(value), arrIndex);
597 size_t arrIndex = 0) {
598 assert(field->m_type == StructFieldType::kDouble);
599 SetFieldImpl(field, bit_cast<uint64_t>(value), arrIndex);
630 size_t arrIndex = 0) {
631 assert(field->m_type == StructFieldType::kStruct);
632 assert(field->m_parent ==
m_desc);
634 assert(arrIndex < field->m_arraySize);
636 field->m_struct, m_data.subspan(field->m_offset +
637 arrIndex * field->m_struct->
GetSize())};
646 std::span<uint8_t> m_data;
652 explicit DSOData(std::span<const uint8_t> data)
680 std::span<const uint8_t> data)
682 assert(data.size() >= desc->
GetSize());
This file defines the StringMap class.
This file implements the C++20 <bit> header.
Dynamic (run-time) read-only access to a serialized raw struct.
Definition: DynamicStruct.h:366
std::span< const uint8_t > GetData() const
Gets the serialized data.
Definition: DynamicStruct.h:391
uint64_t GetUintField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of an unsigned integer field.
Definition: DynamicStruct.h:436
const StructDescriptor * m_desc
Definition: DynamicStruct.h:496
const StructFieldDescriptor * FindField(std::string_view name) const
Gets a struct field descriptor by name.
Definition: DynamicStruct.h:399
DynamicStruct GetStructField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a struct field.
Definition: DynamicStruct.h:484
DynamicStruct(const StructDescriptor *desc, std::span< const uint8_t > data)
Constructs a new dynamic struct.
Definition: DynamicStruct.h:376
double GetDoubleField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a double field.
Definition: DynamicStruct.h:463
const StructDescriptor * GetDescriptor() const
Gets the struct descriptor.
Definition: DynamicStruct.h:384
int64_t GetIntField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a signed integer field.
Definition: DynamicStruct.h:423
bool GetBoolField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a boolean field.
Definition: DynamicStruct.h:410
std::string_view GetStringField(const StructFieldDescriptor *field) const
Gets the value of a char or char array field.
float GetFloatField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a float field.
Definition: DynamicStruct.h:449
Dynamic (run-time) mutable access to a serialized raw struct, with internal data storage.
Definition: DynamicStruct.h:663
Dynamic (run-time) mutable access to a serialized raw struct.
Definition: DynamicStruct.h:508
void SetFloatField(const StructFieldDescriptor *field, float value, size_t arrIndex=0)
Sets the value of a float field.
Definition: DynamicStruct.h:583
void SetBoolField(const StructFieldDescriptor *field, bool value, size_t arrIndex=0)
Sets the value of a boolean field.
Definition: DynamicStruct.h:544
MutableDynamicStruct GetStructField(const StructFieldDescriptor *field, size_t arrIndex=0)
Gets the value of a struct field.
Definition: DynamicStruct.h:629
void SetUintField(const StructFieldDescriptor *field, uint64_t value, size_t arrIndex=0)
Sets the value of an unsigned integer field.
Definition: DynamicStruct.h:570
MutableDynamicStruct(const StructDescriptor *desc, std::span< uint8_t > data)
Constructs a new dynamic struct.
Definition: DynamicStruct.h:518
std::span< uint8_t > GetData()
Gets the serialized data.
Definition: DynamicStruct.h:526
bool SetStringField(const StructFieldDescriptor *field, std::string_view value)
Sets the value of a char or char array field.
void SetDoubleField(const StructFieldDescriptor *field, double value, size_t arrIndex=0)
Sets the value of a double field.
Definition: DynamicStruct.h:596
void SetData(std::span< const uint8_t > data)
Overwrites the entire serialized struct by copying data from a span.
void SetStructField(const StructFieldDescriptor *field, const DynamicStruct &value, size_t arrIndex=0)
Sets the value of a struct field.
void SetIntField(const StructFieldDescriptor *field, int64_t value, size_t arrIndex=0)
Sets the value of a signed integer field.
Definition: DynamicStruct.h:557
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:579
Database of raw struct dynamic descriptors.
Definition: DynamicStruct.h:336
const StructDescriptor * Find(std::string_view name) const
Finds a structure in the database by name.
const StructDescriptor * Add(std::string_view name, std::string_view schema, std::string *err)
Adds a structure schema to the database.
Raw struct dynamic struct descriptor.
Definition: DynamicStruct.h:260
const StructFieldDescriptor * FindFieldByName(std::string_view name) const
Gets a field descriptor by name.
StructDescriptor(std::string_view name, const private_init &)
Definition: DynamicStruct.h:265
const std::vector< StructFieldDescriptor > & GetFields() const
Gets all field descriptors.
Definition: DynamicStruct.h:314
size_t GetSize() const
Returns the struct size, in bytes.
Definition: DynamicStruct.h:294
bool IsValid() const
Returns whether the struct is valid (e.g.
Definition: DynamicStruct.h:287
const std::string & GetSchema() const
Gets the struct schema.
Definition: DynamicStruct.h:279
const std::string & GetName() const
Gets the struct name.
Definition: DynamicStruct.h:272
Raw struct dynamic field descriptor.
Definition: DynamicStruct.h:66
const StructDescriptor * GetStruct() const
Gets the struct descriptor for a struct data type.
Definition: DynamicStruct.h:201
size_t GetOffset() const
Gets the storage offset of the field, in bytes.
Definition: DynamicStruct.h:142
StructFieldType GetType() const
Gets the field type.
Definition: DynamicStruct.h:104
const StructDescriptor * GetParent() const
Gets the dynamic struct this field is contained in.
Definition: DynamicStruct.h:90
int64_t GetIntMax() const
Gets the maximum signed integer value that can be stored in this field.
Definition: DynamicStruct.h:231
const std::string & GetName() const
Gets the field name.
Definition: DynamicStruct.h:97
const EnumValues & GetEnumValues()
Gets the enumerated values.
Definition: DynamicStruct.h:194
std::vector< std::pair< std::string, int64_t > > EnumValues
Set of enumerated values.
Definition: DynamicStruct.h:77
uint64_t GetUintMin() const
Gets the minimum unsigned integer value that can be stored in this field.
Definition: DynamicStruct.h:208
unsigned int GetBitShift() const
Gets the bit shift for the field (LSB=0).
Definition: DynamicStruct.h:166
size_t GetSize() const
Gets the underlying storage size of the field, in bytes.
Definition: DynamicStruct.h:135
bool IsUint() const
Returns whether the field type is an unsigned integer.
Definition: DynamicStruct.h:123
bool IsArray() const
Returns whether the field is an array.
Definition: DynamicStruct.h:173
int64_t GetIntMin() const
Gets the minimum signed integer value that can be stored in this field.
Definition: DynamicStruct.h:222
bool HasEnum() const
Returns whether the field has enumerated values.
Definition: DynamicStruct.h:187
StructFieldDescriptor(const StructDescriptor *parent, std::string_view name, StructFieldType type, size_t size, size_t arraySize, unsigned int bitWidth, EnumValues enumValues, const StructDescriptor *structDesc, const private_init &)
unsigned int GetBitWidth() const
Gets the bit width of the field, in bits.
Definition: DynamicStruct.h:149
uint64_t GetBitMask() const
Gets the bit mask for the field.
Definition: DynamicStruct.h:159
uint64_t GetUintMax() const
Gets the maximum unsigned integer value that can be stored in this field.
Definition: DynamicStruct.h:215
size_t GetArraySize() const
Gets the array size.
Definition: DynamicStruct.h:180
bool IsInt() const
Returns whether the field type is a signed integer.
Definition: DynamicStruct.h:111
bool IsBitField() const
Returns whether the field is a bitfield.
Definition: DynamicStruct.h:238
basic_string_view< char > string_view
Definition: core.h:501
type
Definition: core.h:556
constexpr const char * name(const T &)
Definition: ntcore_cpp.h:26
Definition: DynamicStruct.h:650
DSOData(std::span< const uint8_t > data)
Definition: DynamicStruct.h:652
std::vector< uint8_t > m_dataStore
Definition: DynamicStruct.h:655
DSOData(size_t size)
Definition: DynamicStruct.h:651
StructFieldType
Known data types for raw struct dynamic fields (see StructFieldDescriptor).
Definition: DynamicStruct.h:34