From b0910e75e058f73165526002d4afcec317d7955e Mon Sep 17 00:00:00 2001 From: Florian Enner Date: Fri, 30 Jan 2015 17:09:59 -0500 Subject: [PATCH] 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 --- src/idl_gen_general.cpp | 17 +++++++++++------ tests/MyGame/Example/Monster.cs | 3 ++- tests/MyGame/Example/Monster.java | 3 ++- tests/MyGame/Example/Stat.cs | 3 ++- tests/MyGame/Example/Stat.java | 3 ++- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index 4baf85752..562fd5f52 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -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. diff --git a/tests/MyGame/Example/Monster.cs b/tests/MyGame/Example/Monster.cs index da56bea8f..453e596fa 100644 --- a/tests/MyGame/Example/Monster.cs +++ b/tests/MyGame/Example/Monster.cs @@ -6,7 +6,8 @@ namespace MyGame.Example using FlatBuffers; public class Monster : Table { - public static Monster GetRootAsMonster(ByteBuffer _bb) { return (new Monster()).__init(_bb.GetInt(_bb.position()) + _bb.position(), _bb); } + public static Monster GetRootAsMonster(ByteBuffer _bb) { return GetRootAsMonster(_bb, new Monster()); } + public static Monster GetRootAsMonster(ByteBuffer _bb, Monster obj) { return (obj.__init(_bb.GetInt(_bb.position()) + _bb.position(), _bb)); } public static bool MonsterBufferHasIdentifier(ByteBuffer _bb) { return __has_identifier(_bb, "MONS"); } public Monster __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } diff --git a/tests/MyGame/Example/Monster.java b/tests/MyGame/Example/Monster.java index 25f3515d3..b129908b9 100644 --- a/tests/MyGame/Example/Monster.java +++ b/tests/MyGame/Example/Monster.java @@ -8,7 +8,8 @@ import java.util.*; import com.google.flatbuffers.*; public class Monster extends Table { - public static Monster getRootAsMonster(ByteBuffer _bb) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (new Monster()).__init(_bb.getInt(_bb.position()) + _bb.position(), _bb); } + public static Monster getRootAsMonster(ByteBuffer _bb) { return getRootAsMonster(_bb, new Monster()); } + public static Monster getRootAsMonster(ByteBuffer _bb, Monster obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__init(_bb.getInt(_bb.position()) + _bb.position(), _bb)); } public static boolean MonsterBufferHasIdentifier(ByteBuffer _bb) { return __has_identifier(_bb, "MONS"); } public Monster __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } diff --git a/tests/MyGame/Example/Stat.cs b/tests/MyGame/Example/Stat.cs index 1a554f292..8ad3a9e1b 100644 --- a/tests/MyGame/Example/Stat.cs +++ b/tests/MyGame/Example/Stat.cs @@ -6,7 +6,8 @@ namespace MyGame.Example using FlatBuffers; public class Stat : Table { - public static Stat GetRootAsStat(ByteBuffer _bb) { return (new Stat()).__init(_bb.GetInt(_bb.position()) + _bb.position(), _bb); } + public static Stat GetRootAsStat(ByteBuffer _bb) { return GetRootAsStat(_bb, new Stat()); } + public static Stat GetRootAsStat(ByteBuffer _bb, Stat obj) { return (obj.__init(_bb.GetInt(_bb.position()) + _bb.position(), _bb)); } public Stat __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } public string Id() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; } diff --git a/tests/MyGame/Example/Stat.java b/tests/MyGame/Example/Stat.java index 66be12f15..50939d881 100644 --- a/tests/MyGame/Example/Stat.java +++ b/tests/MyGame/Example/Stat.java @@ -8,7 +8,8 @@ import java.util.*; import com.google.flatbuffers.*; public class Stat extends Table { - public static Stat getRootAsStat(ByteBuffer _bb) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (new Stat()).__init(_bb.getInt(_bb.position()) + _bb.position(), _bb); } + public static Stat getRootAsStat(ByteBuffer _bb) { return getRootAsStat(_bb, new Stat()); } + public static Stat getRootAsStat(ByteBuffer _bb, Stat obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__init(_bb.getInt(_bb.position()) + _bb.position(), _bb)); } public Stat __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } public String id() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; }