25class MutableDynamicStruct;
26class StructDescriptor;
27class StructDescriptorDatabase;
65 struct private_init {};
75 using EnumValues = std::vector<std::pair<std::string, int64_t>>;
95 const std::string&
GetName()
const {
return m_name; }
148 return m_bitWidth == 0 ? m_size * 8 : m_bitWidth;
171 bool IsArray()
const {
return m_arraySize > 1; }
185 bool HasEnum()
const {
return !m_enum.empty(); }
221 return static_cast<int64_t
>(-(m_bitMask >> 1)) - 1;
237 return (m_bitShift != 0 || m_bitWidth != (m_size * 8)) &&
252 unsigned int m_bitWidth;
253 unsigned int m_bitShift = 0;
260 struct private_init {};
271 const std::string&
GetName()
const {
return m_name; }
278 const std::string&
GetSchema()
const {
return m_schema; }
313 const std::vector<StructFieldDescriptor>&
GetFields()
const {
320 std::string CalculateOffsets(
324 std::string m_schema;
325 std::vector<StructDescriptor*> m_references;
326 std::vector<StructFieldDescriptor> m_fields;
329 bool m_valid =
false;
376 :
m_desc{desc}, m_data{data} {}
390 std::span<const uint8_t>
GetData()
const {
return m_data; }
410 size_t arrIndex = 0)
const {
412 return GetFieldImpl(field, arrIndex);
423 size_t arrIndex = 0)
const {
424 assert(field->
IsInt());
425 uint64_t raw = GetFieldImpl(field, arrIndex);
426 switch (field->m_size) {
428 return static_cast<int8_t
>(raw);
430 return static_cast<int16_t
>(raw);
432 return static_cast<int32_t
>(raw);
446 size_t arrIndex = 0)
const {
448 return GetFieldImpl(field, arrIndex);
459 size_t arrIndex = 0)
const {
461 return bit_cast<float>(
462 static_cast<uint32_t
>(GetFieldImpl(field, arrIndex)));
473 size_t arrIndex = 0)
const {
475 return bit_cast<double>(GetFieldImpl(field, arrIndex));
494 size_t arrIndex = 0)
const {
496 assert(field->m_parent ==
m_desc);
498 assert(arrIndex < field->m_arraySize);
500 m_data.subspan(field->m_offset +
501 arrIndex * field->m_struct->
GetSize())};
509 size_t arrIndex)
const;
511 std::span<const uint8_t> m_data;
535 std::span<uint8_t>
GetData() {
return m_data; }
554 size_t arrIndex = 0) {
556 SetFieldImpl(field, value ? 1 : 0, arrIndex);
567 size_t arrIndex = 0) {
568 assert(field->
IsInt());
569 SetFieldImpl(field, value, arrIndex);
580 size_t arrIndex = 0) {
582 SetFieldImpl(field, value, arrIndex);
593 size_t arrIndex = 0) {
595 SetFieldImpl(field, bit_cast<uint32_t>(value), arrIndex);
606 size_t arrIndex = 0) {
608 SetFieldImpl(field, bit_cast<uint64_t>(value), arrIndex);
619 std::string_view value);
639 size_t arrIndex = 0) {
641 assert(field->m_parent ==
m_desc);
643 assert(arrIndex < field->m_arraySize);
645 field->m_struct, m_data.subspan(field->m_offset +
646 arrIndex * field->m_struct->
GetSize())};
655 std::span<uint8_t> m_data;
661 explicit DSOData(std::span<const uint8_t> data)
689 std::span<const uint8_t> data)
691 assert(data.size() >= desc->
GetSize());
Dynamic (run-time) read-only access to a serialized raw struct.
Definition DynamicStruct.h:365
std::span< const uint8_t > GetData() const
Gets the serialized data.
Definition DynamicStruct.h:390
uint64_t GetUintField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of an unsigned integer field.
Definition DynamicStruct.h:445
const StructDescriptor * m_desc
Definition DynamicStruct.h:505
const StructFieldDescriptor * FindField(std::string_view name) const
Gets a struct field descriptor by name.
Definition DynamicStruct.h:398
DynamicStruct GetStructField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a struct field.
Definition DynamicStruct.h:493
DynamicStruct(const StructDescriptor *desc, std::span< const uint8_t > data)
Constructs a new dynamic struct.
Definition DynamicStruct.h:375
double GetDoubleField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a double field.
Definition DynamicStruct.h:472
const StructDescriptor * GetDescriptor() const
Gets the struct descriptor.
Definition DynamicStruct.h:383
int64_t GetIntField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a signed integer field.
Definition DynamicStruct.h:422
bool GetBoolField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a boolean field.
Definition DynamicStruct.h:409
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:458
Dynamic (run-time) mutable access to a serialized raw struct, with internal data storage.
Definition DynamicStruct.h:672
Dynamic (run-time) mutable access to a serialized raw struct.
Definition DynamicStruct.h:517
void SetFloatField(const StructFieldDescriptor *field, float value, size_t arrIndex=0)
Sets the value of a float field.
Definition DynamicStruct.h:592
void SetBoolField(const StructFieldDescriptor *field, bool value, size_t arrIndex=0)
Sets the value of a boolean field.
Definition DynamicStruct.h:553
MutableDynamicStruct GetStructField(const StructFieldDescriptor *field, size_t arrIndex=0)
Gets the value of a struct field.
Definition DynamicStruct.h:638
void SetUintField(const StructFieldDescriptor *field, uint64_t value, size_t arrIndex=0)
Sets the value of an unsigned integer field.
Definition DynamicStruct.h:579
MutableDynamicStruct(const StructDescriptor *desc, std::span< uint8_t > data)
Constructs a new dynamic struct.
Definition DynamicStruct.h:527
std::span< uint8_t > GetData()
Gets the serialized data.
Definition DynamicStruct.h:535
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:605
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:566
StringMap is a sorted associative container that contains key-value pairs with unique string keys.
Definition StringMap.h:26
Database of raw struct dynamic descriptors.
Definition DynamicStruct.h:335
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:259
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:264
const std::vector< StructFieldDescriptor > & GetFields() const
Gets all field descriptors.
Definition DynamicStruct.h:313
size_t GetSize() const
Returns the struct size, in bytes.
Definition DynamicStruct.h:293
bool IsValid() const
Returns whether the struct is valid (e.g.
Definition DynamicStruct.h:286
const std::string & GetSchema() const
Gets the struct schema.
Definition DynamicStruct.h:278
const std::string & GetName() const
Gets the struct name.
Definition DynamicStruct.h:271
Raw struct dynamic field descriptor.
Definition DynamicStruct.h:64
const StructDescriptor * GetStruct() const
Gets the struct descriptor for a struct data type.
Definition DynamicStruct.h:199
size_t GetOffset() const
Gets the storage offset of the field, in bytes.
Definition DynamicStruct.h:140
StructFieldType GetType() const
Gets the field type.
Definition DynamicStruct.h:102
const StructDescriptor * GetParent() const
Gets the dynamic struct this field is contained in.
Definition DynamicStruct.h:88
int64_t GetIntMax() const
Gets the maximum signed integer value that can be stored in this field.
Definition DynamicStruct.h:229
const std::string & GetName() const
Gets the field name.
Definition DynamicStruct.h:95
std::vector< std::pair< std::string, int64_t > > EnumValues
Set of enumerated values.
Definition DynamicStruct.h:75
const EnumValues & GetEnumValues() const
Gets the enumerated values.
Definition DynamicStruct.h:192
uint64_t GetUintMin() const
Gets the minimum unsigned integer value that can be stored in this field.
Definition DynamicStruct.h:206
unsigned int GetBitShift() const
Gets the bit shift for the field (LSB=0).
Definition DynamicStruct.h:164
size_t GetSize() const
Gets the underlying storage size of the field, in bytes.
Definition DynamicStruct.h:133
bool IsUint() const
Returns whether the field type is an unsigned integer.
Definition DynamicStruct.h:121
bool IsArray() const
Returns whether the field is an array.
Definition DynamicStruct.h:171
int64_t GetIntMin() const
Gets the minimum signed integer value that can be stored in this field.
Definition DynamicStruct.h:220
bool HasEnum() const
Returns whether the field has enumerated values.
Definition DynamicStruct.h:185
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:147
uint64_t GetBitMask() const
Gets the bit mask for the field.
Definition DynamicStruct.h:157
uint64_t GetUintMax() const
Gets the maximum unsigned integer value that can be stored in this field.
Definition DynamicStruct.h:213
size_t GetArraySize() const
Gets the array size.
Definition DynamicStruct.h:178
bool IsInt() const
Returns whether the field type is a signed integer.
Definition DynamicStruct.h:109
bool IsBitField() const
Returns whether the field is a bitfield.
Definition DynamicStruct.h:236
Definition ntcore_cpp.h:26
StructFieldType
Known data types for raw struct dynamic fields (see StructFieldDescriptor).
Definition DynamicStruct.h:32
Definition DynamicStruct.h:659
DSOData(std::span< const uint8_t > data)
Definition DynamicStruct.h:661
std::vector< uint8_t > m_dataStore
Definition DynamicStruct.h:664
DSOData(size_t size)
Definition DynamicStruct.h:660