Commit 42bfe240 authored by Wouter van Oortmerssen's avatar Wouter van Oortmerssen Committed by Android (Google) Code Review

Merge "added reuse option for root objects" into ub-games-master

parents 1c8c9438 b0910e75
...@@ -322,13 +322,18 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, ...@@ -322,13 +322,18 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
if (!struct_def.fixed) { if (!struct_def.fixed) {
// Generate a special accessor for the table that when used as the root // Generate a special accessor for the table that when used as the root
// of a FlatBuffer // of a FlatBuffer
code += " public static " + struct_def.name + " "; std::string method_name = FunctionStart(lang, 'G') + "etRootAs" + struct_def.name;
code += FunctionStart(lang, 'G') + "etRootAs" + struct_def.name; std::string method_signature = " public static " + struct_def.name + " " + method_name;
code += "(ByteBuffer _bb) { ";
// 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 += lang.set_bb_byteorder;
code += "return (new " + struct_def.name; code += "return (obj.__init(_bb." + FunctionStart(lang, 'G');
code += "()).__init(_bb." + FunctionStart(lang, 'G'); code += "etInt(_bb.position()) + _bb.position(), _bb)); }\n";
code += "etInt(_bb.position()) + _bb.position(), _bb); }\n";
if (parser.root_struct_def == &struct_def) { if (parser.root_struct_def == &struct_def) {
if (parser.file_identifier_.length()) { if (parser.file_identifier_.length()) {
// Check if a buffer has the identifier. // Check if a buffer has the identifier.
......
...@@ -6,7 +6,8 @@ namespace MyGame.Example ...@@ -6,7 +6,8 @@ namespace MyGame.Example
using FlatBuffers; using FlatBuffers;
public class Monster : Table { 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 static bool MonsterBufferHasIdentifier(ByteBuffer _bb) { return __has_identifier(_bb, "MONS"); }
public Monster __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } public Monster __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; }
......
...@@ -8,7 +8,8 @@ import java.util.*; ...@@ -8,7 +8,8 @@ import java.util.*;
import com.google.flatbuffers.*; import com.google.flatbuffers.*;
public class Monster extends Table { 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 static boolean MonsterBufferHasIdentifier(ByteBuffer _bb) { return __has_identifier(_bb, "MONS"); }
public Monster __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } public Monster __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; }
......
...@@ -6,7 +6,8 @@ namespace MyGame.Example ...@@ -6,7 +6,8 @@ namespace MyGame.Example
using FlatBuffers; using FlatBuffers;
public class Stat : Table { 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 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; } public string Id() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; }
......
...@@ -8,7 +8,8 @@ import java.util.*; ...@@ -8,7 +8,8 @@ import java.util.*;
import com.google.flatbuffers.*; import com.google.flatbuffers.*;
public class Stat extends Table { 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 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; } public String id() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment