StructΒΆ
example_struct_def = CStructDef(
"Examplestruct",
doc=Doc("Example Struct", "This struct has many awesome members"),
members=[
CStructDef.Member(CVariable("i8Title", Cint8), doc=Doc("Title")),
CStructDef.Member(CVariable("i8Asdf", Cint8), bitfield=3, doc=Doc("Random variable")),
CStructDef.Member(CArray("i8Name", c_type=Cint8, length=3)),
CStructDef.Member(
CVariable("tNestedstruct", c_type=CStructDef(
# This struct def is anonymous
is_packed=True, # And packed
members=[
CStructDef.Member(CVariable("i64Qwer", Cint64)),
]),
)
),
CStructDef.Member(
CVariable("tNestedstruct2", c_type=CStructDef(
"TNestedstruct2",
members=[
CStructDef.Member(CVariable("i64Qwer", Cint64)),
]).struct,
# Reference the struct type, not the def, and its not declared inplace.
# Assumes this struct will be defined somewhere else
)
)
])
# Declaration of struct definition
print(example_struct_def.declare().render())
/**
* @brief Example Struct
*
* This struct has many awesome members
*/
struct Examplestruct
{
/** @brief Title */
int8_t i8Title;
/** @brief Random variable */
int8_t i8Asdf: 3;
int8_t i8Name[3];
struct __attribute__((__packed__))
{
int8_t i64Qwer;
} tNestedstruct;
struct TNestedstruct2 tNestedstruct2;
};
# Can declare a struct with a variable in the same sentence
print(CVariable("tInst", c_type=example_struct_def).declare().render())
/**
* @brief Example Struct
*
* This struct has many awesome members
*/
struct Examplestruct
{
/** @brief Title */
int8_t i8Title;
/** @brief Random variable */
int8_t i8Asdf: 3;
int8_t i8Name[3];
struct __attribute__((__packed__))
{
int8_t i64Qwer;
} tNestedstruct;
struct TNestedstruct2 tNestedstruct2;
} tInst;
# Or assume the struct is already declared and use it as type
print(CVariable("tInst", c_type=example_struct_def.struct).declare().render())
struct Examplestruct tInst;
# Can do a typedef of the struct with the declaration of the struct inplace
print(example_struct_def.type("TMyStruct").typedef().render())
typedef struct Examplestruct
{
/** @brief Title */
int8_t i8Title;
/** @brief Random variable */
int8_t i8Asdf: 3;
int8_t i8Name[3];
struct __attribute__((__packed__))
{
int8_t i64Qwer;
} tNestedstruct;
struct TNestedstruct2 tNestedstruct2;
} TMyStruct;
# Or the struct is already declared and can be typedefed afterwards
print(example_struct_def.struct.type("TMyStruct").typedef().render())
typedef struct Examplestruct TMyStruct;
# Third member is the packed anonymous struct, can be reobtained
packed_struct = example_struct_def.members[3].variable.c_type
if not isinstance(packed_struct, CStructDef):
raise TypeError
# Only packed structs have bit_sizes, calculated. CCG Cant know the size of structs with possible padding
print(packed_struct.bit_size)
64