Skip to content

Commit a08654a

Browse files
committed
Add TypeBinaryStruct class
This patch introduces the `TypeBinaryStruct` class to handle attributes that are of binary type and structured as a `struct` in the `rt-addr`. Unlike the standard `TypeBinary`, `TypeBinaryStruct` has a defined structure, so the `struct_member` should be `optional<T>` and the `presence_type` is `flag`. Signed-off-by: Zibo Gong <[email protected]>
1 parent 23e8df2 commit a08654a

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

ynl-gen-cpp.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,36 @@ def _setter_lines(self, ri, member, presence):
513513
]
514514

515515

516+
class TypeBinaryStruct(TypeBinary):
517+
def struct_member(self, ri):
518+
ri.cw.p(f'std::optional<struct {c_lower(self.get("struct"))}> {self.c_name};')
519+
520+
def presence_type(self):
521+
return "flag"
522+
523+
def attr_put(self, ri, var):
524+
struct_sz = "sizeof(struct " + c_lower(self.get("struct")) + ")"
525+
self._attr_put_line(
526+
ri,
527+
var,
528+
f"ynl_attr_put(nlh, {self.enum_name}, "
529+
+ f"&*{var}.{self.c_name}, {struct_sz})",
530+
)
531+
532+
def _attr_get(self, ri, var):
533+
struct_sz = "sizeof(struct " + c_lower(self.get("struct")) + ")"
534+
return (
535+
[
536+
"unsigned int len = ynl_attr_data_len(attr);",
537+
f"unsigned int struct_sz = {struct_sz};",
538+
f"{var}->{self.c_name}.emplace();",
539+
f"memcpy(&*{var}->{self.c_name}, ynl_attr_data(attr), std::min(struct_sz, len));",
540+
],
541+
None,
542+
None,
543+
)
544+
545+
516546
class TypeBitfield32(Type):
517547
def _complex_member_type(self, ri):
518548
return "struct nla_bitfield32"
@@ -867,7 +897,10 @@ def new_attr(self, elem, value):
867897
elif elem["type"] == "string":
868898
t = TypeString(self.family, self, elem, value)
869899
elif elem["type"] == "binary":
870-
t = TypeBinary(self.family, self, elem, value)
900+
if "struct" in elem:
901+
t = TypeBinaryStruct(self.family, self, elem, value)
902+
else:
903+
t = TypeBinary(self.family, self, elem, value)
871904
elif elem["type"] == "bitfield32":
872905
t = TypeBitfield32(self.family, self, elem, value)
873906
elif elem["type"] == "nest":

0 commit comments

Comments
 (0)