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; }
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);
497 assert(
m_desc->IsValid());
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);
642 assert(
m_desc->IsValid());
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());
695 DynamicStructObject(DynamicStructObject&&) =
delete;
696 DynamicStructObject& operator=(DynamicStructObject&&) =
delete;
@ name
Definition base.h:690
Dynamic (run-time) read-only access to a serialized raw struct.
Definition DynamicStruct.hpp:365
float GetFloatField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a float field.
Definition DynamicStruct.hpp:458
bool GetBoolField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a boolean field.
Definition DynamicStruct.hpp:409
double GetDoubleField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a double field.
Definition DynamicStruct.hpp:472
const StructDescriptor * GetDescriptor() const
Gets the struct descriptor.
Definition DynamicStruct.hpp:383
uint64_t GetUintField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of an unsigned integer field.
Definition DynamicStruct.hpp:445
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:505
DynamicStruct GetStructField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a struct field.
Definition DynamicStruct.hpp:493
int64_t GetIntField(const StructFieldDescriptor *field, size_t arrIndex=0) const
Gets the value of a signed integer field.
Definition DynamicStruct.hpp:422
std::span< const uint8_t > GetData() const
Gets the serialized data.
Definition DynamicStruct.hpp:390
const StructFieldDescriptor * FindField(std::string_view name) const
Gets a struct field descriptor by name.
Definition DynamicStruct.hpp:398
DynamicStruct(const StructDescriptor *desc, std::span< const uint8_t > data)
Constructs a new dynamic struct.
Definition DynamicStruct.hpp:375
Dynamic (run-time) mutable access to a serialized raw struct.
Definition DynamicStruct.hpp:517
void SetUintField(const StructFieldDescriptor *field, uint64_t value, size_t arrIndex=0)
Sets the value of an unsigned integer field.
Definition DynamicStruct.hpp:579
void SetFloatField(const StructFieldDescriptor *field, float value, size_t arrIndex=0)
Sets the value of a float field.
Definition DynamicStruct.hpp:592
void SetBoolField(const StructFieldDescriptor *field, bool value, size_t arrIndex=0)
Sets the value of a boolean field.
Definition DynamicStruct.hpp:553
void SetIntField(const StructFieldDescriptor *field, int64_t value, size_t arrIndex=0)
Sets the value of a signed integer field.
Definition DynamicStruct.hpp:566
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:527
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:638
void SetDoubleField(const StructFieldDescriptor *field, double value, size_t arrIndex=0)
Sets the value of a double field.
Definition DynamicStruct.hpp:605
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:535
Definition BooleanTopic.hpp:24
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: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.hpp:259
const std::string & GetSchema() const
Gets the struct schema.
Definition DynamicStruct.hpp:278
bool IsValid() const
Returns whether the struct is valid (e.g.
Definition DynamicStruct.hpp:286
StructDescriptor(std::string_view name, const private_init &)
Definition DynamicStruct.hpp:264
const std::vector< StructFieldDescriptor > & GetFields() const
Gets all field descriptors.
Definition DynamicStruct.hpp:313
const std::string & GetName() const
Gets the struct name.
Definition DynamicStruct.hpp:271
friend class StructDescriptorDatabase
Definition DynamicStruct.hpp:261
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:293
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:199
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:133
size_t GetOffset() const
Gets the storage offset of the field, in bytes.
Definition DynamicStruct.hpp:140
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:171
size_t GetArraySize() const
Gets the array size.
Definition DynamicStruct.hpp:178
bool IsUint() const
Returns whether the field type is an unsigned integer.
Definition DynamicStruct.hpp:121
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:213
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:206
unsigned int GetBitWidth() const
Gets the bit width of the field, in bits.
Definition DynamicStruct.hpp:147
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:220
bool HasEnum() const
Returns whether the field has enumerated values.
Definition DynamicStruct.hpp:185
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:164
const EnumValues & GetEnumValues() const
Gets the enumerated values.
Definition DynamicStruct.hpp:192
friend class StructDescriptorDatabase
Definition DynamicStruct.hpp:69
uint64_t GetBitMask() const
Gets the bit mask for the field.
Definition DynamicStruct.hpp:157
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:236
int64_t GetIntMax() const
Gets the maximum signed integer value that can be stored in this field.
Definition DynamicStruct.hpp:229
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
@ kInt32
int32.
Definition DynamicStruct.hpp:42
@ kUint8
uint8.
Definition DynamicStruct.hpp:46
@ kBool
bool.
Definition DynamicStruct.hpp:34
@ kInt64
int64.
Definition DynamicStruct.hpp:44
@ kStruct
struct.
Definition DynamicStruct.hpp:58
@ kUint64
uint64.
Definition DynamicStruct.hpp:52
@ kInt16
int16.
Definition DynamicStruct.hpp:40
@ kDouble
double.
Definition DynamicStruct.hpp:56
@ kFloat
float.
Definition DynamicStruct.hpp:54
@ kUint16
uint16.
Definition DynamicStruct.hpp:48
@ kInt8
int8.
Definition DynamicStruct.hpp:38
@ kChar
char.
Definition DynamicStruct.hpp:36
@ kUint32
uint32.
Definition DynamicStruct.hpp:50
Definition DynamicStruct.hpp:659
DSOData(size_t size)
Definition DynamicStruct.hpp:660
DSOData(std::span< const uint8_t > data)
Definition DynamicStruct.hpp:661
std::vector< uint8_t > m_dataStore
Definition DynamicStruct.hpp:664