Skip to content

Commit

Permalink
Add C++ support for length prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethnym committed Feb 16, 2024
1 parent d4e21b6 commit b48cde3
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions internal/cxxgen/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ struct {{.MessageName}} : {{if .HasParentMessage}}{{.ParentMessageName}}{{else}}
[[nodiscard]] int32_t type_id() const override;
[[nodiscard]] std::vector<uint8_t> data() const override;
[[nodiscard]] std::vector<uint8_t> data_with_length_prefix() const override;
};
{{if .Namespace}}} // namespace {{.Namespace}}{{end}}
Expand Down Expand Up @@ -184,6 +186,26 @@ std::vector<uint8_t> {{if .Namespace}}{{.Namespace}}::{{end}}{{.MessageName}}::d
return buf;
}
std::vector<uint8_t> {{if .Namespace}}{{.Namespace}}::{{end}}{{.MessageName}}::data_with_length_prefix() const {
std::vector<uint8_t> buf({{.InitialWriteBufferSize}} + 4);
NanoPack::Writer writer(&buf, 4);
writer.write_type_id(TYPE_ID);
{{range .FieldWriteCodeFragments}}
{{.}}
{{end}}
const size_t byte_size = buf.size() - 4;
buf[0] = byte_size & 0xFF;
buf[1] = byte_size & 0xFF00;
buf[2] = byte_size & 0xFF0000;
buf[3] = byte_size & 0xFF000000;
return buf;
}
`

const childMessageFactoryHeaderFile = `// AUTOMATICALLY GENERATED BY NANOPACK. DO NOT MODIFY BY HAND.
Expand Down

0 comments on commit b48cde3

Please sign in to comment.