added reuse option for root objects

getRootAs..() function now has a second implementation that
accepts an existing object to allow object reuse, much like
all other methods that refer to objects.

Change-Id: Iffef567c903a130761ef7de98867e5465d29a04d
This commit is contained in:
Florian Enner
2015-01-30 17:09:59 -05:00
committed by Wouter van Oortmerssen
parent 4d3db99283
commit b0910e75e0
5 changed files with 19 additions and 10 deletions

View File

@@ -322,13 +322,18 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
if (!struct_def.fixed) {
// Generate a special accessor for the table that when used as the root
// of a FlatBuffer
code += " public static " + struct_def.name + " ";
code += FunctionStart(lang, 'G') + "etRootAs" + struct_def.name;
code += "(ByteBuffer _bb) { ";
std::string method_name = FunctionStart(lang, 'G') + "etRootAs" + struct_def.name;
std::string method_signature = " public static " + struct_def.name + " " + method_name;
// create convenience method that doesn't require an existing object
code += method_signature + "(ByteBuffer _bb) ";
code += "{ return " + method_name + "(_bb, new " + struct_def.name+ "()); }\n";
// create method that allows object reuse
code += method_signature + "(ByteBuffer _bb, " + struct_def.name + " obj) { ";
code += lang.set_bb_byteorder;
code += "return (new " + struct_def.name;
code += "()).__init(_bb." + FunctionStart(lang, 'G');
code += "etInt(_bb.position()) + _bb.position(), _bb); }\n";
code += "return (obj.__init(_bb." + FunctionStart(lang, 'G');
code += "etInt(_bb.position()) + _bb.position(), _bb)); }\n";
if (parser.root_struct_def == &struct_def) {
if (parser.file_identifier_.length()) {
// Check if a buffer has the identifier.