mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +00:00
[Python/JS/TS] Codegen SizeOf method for structs (#6136)
* [Python] Codegen SizeOf classmethod for structs This codegens a `SizeOf()` classmethod for all structs since we can't determine the size of a FlatBuffer generated struct from Python otherwise. * [JS/TS] Codegen sizeOf static method for structs This codegens a `sizeOf()` static method for all structs since we can't determine the size of a FlatBuffer generated struct from JavaScript or TypeScript otherwise.
This commit is contained in:
@@ -621,6 +621,16 @@ class PythonGenerator : public BaseGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
// Generate struct sizeof.
|
||||
void GenStructSizeOf(const StructDef &struct_def, std::string *code_ptr) {
|
||||
auto &code = *code_ptr;
|
||||
code += Indent + "@classmethod\n";
|
||||
code += Indent + "def SizeOf(cls):\n";
|
||||
code +=
|
||||
Indent + Indent + "return " + NumToString(struct_def.bytesize) + "\n";
|
||||
code += "\n";
|
||||
}
|
||||
|
||||
// Generate table constructors, conditioned on its members' types.
|
||||
void GenTableBuilders(const StructDef &struct_def, std::string *code_ptr) {
|
||||
GetStartOfTable(struct_def, code_ptr);
|
||||
@@ -678,6 +688,9 @@ class PythonGenerator : public BaseGenerator {
|
||||
// Generate a special function to test file_identifier
|
||||
GenHasFileIdentifier(struct_def, code_ptr);
|
||||
}
|
||||
} else {
|
||||
// Generates the SizeOf method for all structs.
|
||||
GenStructSizeOf(struct_def, code_ptr);
|
||||
}
|
||||
// Generates the Init method that sets the field in a pre-existing
|
||||
// accessor object. This is to allow object reuse.
|
||||
|
||||
Reference in New Issue
Block a user