17#include "wpi/util/bit.hpp"
65 struct private_init {};
75 using EnumValues = std::vector<std::pair<std::string, int64_t>>;
95 const std::string&
GetName()
const {
return m_name; }
147 return m_bitWidth == 0 ? m_size * 8 : m_bitWidth;
170 bool IsArray()
const {
return m_arraySize > 1; }
184 bool HasEnum()
const {
return !m_enum.empty(); }
220 return static_cast<int64_t
>(-(m_bitMask >> 1)) - 1;
236 return (m_bitShift != 0 || m_bitWidth != (m_size * 8)) &&
251 unsigned int m_bitWidth;
252 unsigned int m_bitShift = 0;
259 struct private_init {};
270 const std::string&
GetName()
const {
return m_name; }
277 const std::string&
GetSchema()
const {
return m_schema; }
312 const std::vector<StructFieldDescriptor>&
GetFields()
const {
319 std::string CalculateOffsets(
323 std::string m_schema;
324 std::vector<StructDescriptor*> m_references;
325 std::vector<StructFieldDescriptor> m_fields;
328 bool m_valid =
false;
375 :
m_desc{desc}, m_data{data} {}
389 std::span<const uint8_t>
GetData()
const {
return m_data; }
409 size_t arrIndex = 0)
const {
411 return GetFieldImpl(field, arrIndex);
422 size_t arrIndex = 0)
const {
423 assert(field->
IsInt());
424 uint64_t raw = GetFieldImpl(field, arrIndex);
425 switch (field->m_size) {
427 return static_cast<int8_t
>(raw);
429 return static_cast<int16_t
>(raw);
431 return static_cast<int32_t
>(raw);
445 size_t arrIndex = 0)
const {
447 return GetFieldImpl(field, arrIndex);
458 size_t arrIndex = 0)
const {
460 return bit_cast<float>(
461 static_cast<uint32_t
>(GetFieldImpl(field, arrIndex)));
472 size_t arrIndex = 0)
const {
474 return bit_cast<double>(GetFieldImpl(field, arrIndex));
493 size_t arrIndex = 0)
const {
495 assert(field->m_parent ==
m_desc);
496 assert(
m_desc->IsValid());
497 assert(arrIndex < field->m_arraySize);
499 m_data.subspan(field->m_offset +
500 arrIndex * field->m_struct->
GetSize())};
508 size_t arrIndex)
const;
510 std::span<const uint8_t> m_data;
534 std::span<uint8_t>
GetData() {
return m_data; }
553 size_t arrIndex = 0) {
555 SetFieldImpl(field, value ? 1 : 0, arrIndex);
566 size_t arrIndex = 0) {
567 assert(field->
IsInt());
568 SetFieldImpl(field, value, arrIndex);
579 size_t arrIndex = 0) {
581 SetFieldImpl(field, value, arrIndex);
592 size_t arrIndex = 0) {
594 SetFieldImpl(field, bit_cast<uint32_t>(value), arrIndex);
605 size_t arrIndex = 0) {
607 SetFieldImpl(field, bit_cast<uint64_t>(value), arrIndex);
618 std::string_view value);
638 size_t arrIndex = 0) {
640 assert(field->m_parent ==
m_desc);
641 assert(
m_desc->IsValid());
642 assert(arrIndex < field->m_arraySize);
644 field->m_struct, m_data.subspan(field->m_offset +
645 arrIndex * field->m_struct->
GetSize())};
654 std::span<uint8_t> m_data;
660 explicit DSOData(std::span<const uint8_t> data)
688 std::span<const uint8_t> data)
690 assert(data.size() >= desc->
GetSize());
694 DynamicStructObject(DynamicStructObject&&) =
delete;
695 DynamicStructObject& operator=(DynamicStructObject&&) =
delete;
@ name
Definition base.h:690
Dynamic (run-time) read-only access to a serialized raw struct.
Definition DynamicStruct.hpp:364
float GetFloatField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a float field.
Definition DynamicStruct.hpp:457
bool GetBoolField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a boolean field.
Definition DynamicStruct.hpp:408
double GetDoubleField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a double field.
Definition DynamicStruct.hpp:471
const StructDescriptor * GetDescriptor() const
Gets the struct descriptor.
Definition DynamicStruct.hpp:382
uint64_t GetUintField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of an unsigned integer field.
Definition DynamicStruct.hpp:444
std::string_view GetStringField(const StructFieldDescriptor *field) const
Gets the value of a char or char array field.
const StructDescriptor * m_desc
Definition DynamicStruct.hpp:504
DynamicStruct GetStructField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a struct field.
Definition DynamicStruct.hpp:492
int64_t GetIntField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a signed integer field.
Definition DynamicStruct.hpp:421
std::span< const uint8_t > GetData() const
Gets the serialized data.
Definition DynamicStruct.hpp:389
const StructFieldDescriptor * FindField(std::string_view name) const
Gets a struct field descriptor by name.
Definition DynamicStruct.hpp:397
DynamicStruct(const StructDescriptor *desc, std::span< const uint8_t > data)
Constructs a new dynamic struct.
Definition DynamicStruct.hpp:374
Dynamic (run-time) mutable access to a serialized raw struct.
Definition DynamicStruct.hpp:516
void SetUintField(const StructFieldDescriptor *field, uint64_t value, size_t arrIndex=0)
Sets the value of an unsigned integer field.
Definition DynamicStruct.hpp:578
void SetFloatField(const StructFieldDescriptor *field, float value, size_t arrIndex=0)
Sets the value of a float field.
Definition DynamicStruct.hpp:591
void SetBoolField(const StructFieldDescriptor *field, bool value, size_t arrIndex=0)
Sets the value of a boolean field.
Definition DynamicStruct.hpp:552
void SetIntField(const StructFieldDescriptor *field, int64_t value, size_t arrIndex=0)
Sets the value of a signed integer field.
Definition DynamicStruct.hpp:565
void SetStructField(const StructFieldDescriptor *field, const DynamicStruct &value, size_t arrIndex=0)
Sets the value of a struct field.
MutableDynamicStruct(const StructDescriptor *desc, std::span< uint8_t > data)
Constructs a new dynamic struct.
Definition DynamicStruct.hpp:526
void SetData(std::span< const uint8_t > data)
Overwrites the entire serialized struct by copying data from a span.
MutableDynamicStruct GetStructField(const StructFieldDescriptor *field, size_t arrIndex=0)
Gets the value of a struct field.
Definition DynamicStruct.hpp:637
void SetDoubleField(const StructFieldDescriptor *field, double value, size_t arrIndex=0)
Sets the value of a double field.
Definition DynamicStruct.hpp:604
bool SetStringField(const StructFieldDescriptor *field, std::string_view value)
Sets the value of a char or char array field.
std::span< uint8_t > GetData()
Gets the serialized data.
Definition DynamicStruct.hpp:534
Definition BooleanTopic.hpp:22
StringMap is a sorted associative container that contains key-value pairs with unique string keys.
Definition StringMap.hpp:26
Database of raw struct dynamic descriptors.
Definition DynamicStruct.hpp:334
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.hpp:258
const std::string & GetSchema() const
Gets the struct schema.
Definition DynamicStruct.hpp:277
bool IsValid() const
Returns whether the struct is valid (e.g.
Definition DynamicStruct.hpp:285
StructDescriptor(std::string_view name, const private_init &)
Definition DynamicStruct.hpp:263
const std::vector< StructFieldDescriptor > & GetFields() const
Gets all field descriptors.
Definition DynamicStruct.hpp:312
const std::string & GetName() const
Gets the struct name.
Definition DynamicStruct.hpp:270
friend class StructDescriptorDatabase
Definition DynamicStruct.hpp:260
const StructFieldDescriptor * FindFieldByName(std::string_view name) const
Gets a field descriptor by name.
size_t GetSize() const
Returns the struct size, in bytes.
Definition DynamicStruct.hpp:292
Raw struct dynamic field descriptor.
Definition DynamicStruct.hpp:64
friend class MutableDynamicStruct
Definition DynamicStruct.hpp:67
const StructDescriptor * GetStruct() const
Gets the struct descriptor for a struct data type.
Definition DynamicStruct.hpp:198
StructFieldType GetType() const
Gets the field type.
Definition DynamicStruct.hpp:102
friend class StructDescriptor
Definition DynamicStruct.hpp:68
size_t GetSize() const
Gets the underlying storage size of the field, in bytes.
Definition DynamicStruct.hpp:132
size_t GetOffset() const
Gets the storage offset of the field, in bytes.
Definition DynamicStruct.hpp:139
const StructDescriptor * GetParent() const
Gets the dynamic struct this field is contained in.
Definition DynamicStruct.hpp:88
bool IsArray() const
Returns whether the field is an array.
Definition DynamicStruct.hpp:170
size_t GetArraySize() const
Gets the array size.
Definition DynamicStruct.hpp:177
bool IsUint() const
Returns whether the field type is an unsigned integer.
Definition DynamicStruct.hpp:120
std::vector< std::pair< std::string, int64_t > > EnumValues
Set of enumerated values.
Definition DynamicStruct.hpp:75
uint64_t GetUintMax() const
Gets the maximum unsigned integer value that can be stored in this field.
Definition DynamicStruct.hpp:212
friend class DynamicStruct
Definition DynamicStruct.hpp:66
uint64_t GetUintMin() const
Gets the minimum unsigned integer value that can be stored in this field.
Definition DynamicStruct.hpp:205
unsigned int GetBitWidth() const
Gets the bit width of the field, in bits.
Definition DynamicStruct.hpp:146
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 &)
int64_t GetIntMin() const
Gets the minimum signed integer value that can be stored in this field.
Definition DynamicStruct.hpp:219
bool HasEnum() const
Returns whether the field has enumerated values.
Definition DynamicStruct.hpp:184
bool IsInt() const
Returns whether the field type is a signed integer.
Definition DynamicStruct.hpp:109
unsigned int GetBitShift() const
Gets the bit shift for the field (LSB=0).
Definition DynamicStruct.hpp:163
const EnumValues & GetEnumValues() const
Gets the enumerated values.
Definition DynamicStruct.hpp:191
friend class StructDescriptorDatabase
Definition DynamicStruct.hpp:69
uint64_t GetBitMask() const
Gets the bit mask for the field.
Definition DynamicStruct.hpp:156
const std::string & GetName() const
Gets the field name.
Definition DynamicStruct.hpp:95
bool IsBitField() const
Returns whether the field is a bitfield.
Definition DynamicStruct.hpp:235
int64_t GetIntMax() const
Gets the maximum signed integer value that can be stored in this field.
Definition DynamicStruct.hpp:228
Definition Errors.hpp:112
Definition RobotBase.hpp:26
Definition raw_os_ostream.hpp:19
StructFieldType
Known data types for raw struct dynamic fields (see StructFieldDescriptor).
Definition DynamicStruct.hpp:32
@ CHAR
char.
Definition DynamicStruct.hpp:36
@ UINT32
uint32.
Definition DynamicStruct.hpp:50
@ UINT16
uint16.
Definition DynamicStruct.hpp:48
@ INT64
int64.
Definition DynamicStruct.hpp:44
@ INT16
int16.
Definition DynamicStruct.hpp:40
@ INT32
int32.
Definition DynamicStruct.hpp:42
@ UINT64
uint64.
Definition DynamicStruct.hpp:52
@ BOOL
bool.
Definition DynamicStruct.hpp:34
@ STRUCT
struct.
Definition DynamicStruct.hpp:58
@ FLOAT
float.
Definition DynamicStruct.hpp:54
@ UINT8
uint8.
Definition DynamicStruct.hpp:46
@ INT8
int8.
Definition DynamicStruct.hpp:38
@ DOUBLE
double.
Definition DynamicStruct.hpp:56
Definition DynamicStruct.hpp:658
DSOData(size_t size)
Definition DynamicStruct.hpp:659
DSOData(std::span< const uint8_t > data)
Definition DynamicStruct.hpp:660
std::vector< uint8_t > m_dataStore
Definition DynamicStruct.hpp:663