UnionΒΆ
example_union_def = CUnionDef(
"ExampleUnion",
doc=Doc("Example Union", "This union has many schrodinger's cat members"),
members=[
CVariable("i8Title", Cint8, doc=Doc("Title")),
CVariable("i8Asdf", Cint8, doc=Doc("Random variable")),
CArray("i8Name", c_type=Cint8, length=3),
CVariable("tNestedUnion", c_type=CUnionDef(
# This union def is anonymous
members=[
CVariable("i64Qwer", Cint64),
]),
),
CVariable("tNestedUnion2", c_type=CUnionDef(
"TNestedUnion2",
members=[
CVariable("i64Qwer", Cint64),
]).union, # Reference the union type, not the def, and its not declared inplace
)
])
# Declaration of struct definition
print(example_union_def.declare().render())
/**
* @brief Example Union
*
* This union has many schrodinger's cat members
*/
union ExampleUnion
{
/** @brief Title */
int8_t i8Title;
/** @brief Random variable */
int8_t i8Asdf;
int8_t i8Name[3];
union
{
int8_t i64Qwer;
} tNestedUnion;
union TNestedUnion2 tNestedUnion2;
};
# Can declare a struct with a variable in the same sentence
print(CVariable("tInst", c_type=example_union_def).declare().render())
/**
* @brief Example Union
*
* This union has many schrodinger's cat members
*/
union ExampleUnion
{
/** @brief Title */
int8_t i8Title;
/** @brief Random variable */
int8_t i8Asdf;
int8_t i8Name[3];
union
{
int8_t i64Qwer;
} tNestedUnion;
union TNestedUnion2 tNestedUnion2;
} tInst;
# Or assume the struct is already declared and use it as type
print(CVariable("tInst", c_type=example_union_def.union).declare().render())
union ExampleUnion tInst;
# Can do a typedef of the struct with the declaration of the struct inplace
print(example_union_def.type("TMyStruct").typedef().render())
typedef union ExampleUnion
{
/** @brief Title */
int8_t i8Title;
/** @brief Random variable */
int8_t i8Asdf;
int8_t i8Name[3];
union
{
int8_t i64Qwer;
} tNestedUnion;
union TNestedUnion2 tNestedUnion2;
} TMyStruct;
# Or the struct is already declared and can be typedefed afterwards
print(example_union_def.union.type("TMyStruct").typedef().render())
typedef union ExampleUnion TMyStruct;