Commit 4ea1be53 authored by Mitchel's avatar Mitchel Committed by Wouter van Oortmerssen

Java + C#, reuse object in lookup_by_key (#4648)

* Java + C#, reuse object in lookup_by_key

* Java + C#, reuse object in lookup_by_key
parent 0068b251
......@@ -1041,7 +1041,17 @@ class GeneralGenerator : public BaseGenerator {
code += MakeCamel(field.name, lang_.first_camel_upper) + "ByKey(";
code += GenTypeNameDest(key_field.value.type) + " key)";
code += offset_prefix;
code += qualified_name + ".__lookup_by_key(";
code += qualified_name + ".__lookup_by_key(null, ";
code += lang_.accessor_prefix + "__vector(o), key, ";
code += lang_.accessor_prefix + "bb) : null; ";
code += "}\n";
code += " public " + qualified_name + lang_.optional_suffix + " ";
code += MakeCamel(field.name, lang_.first_camel_upper) + "ByKey(";
code += qualified_name + lang_.optional_suffix + " obj, ";
code += GenTypeNameDest(key_field.value.type) + " key)";
code += offset_prefix;
code += qualified_name + ".__lookup_by_key(obj, ";
code += lang_.accessor_prefix + "__vector(o), key, ";
code += lang_.accessor_prefix + "bb) : null; ";
code += "}\n";
......@@ -1368,7 +1378,8 @@ class GeneralGenerator : public BaseGenerator {
}
code += "\n public static " + struct_def.name + lang_.optional_suffix;
code += " __lookup_by_key(int vectorLocation, ";
code += " __lookup_by_key(" + struct_def.name;
code += " obj, int vectorLocation, ";
code += GenTypeNameDest(key_field->value.type);
code += " key, ByteBuffer bb) {\n";
if (key_field->value.type.base_type == BASE_TYPE_STRING) {
......@@ -1391,8 +1402,9 @@ class GeneralGenerator : public BaseGenerator {
code += " start += middle;\n";
code += " span -= middle;\n";
code += " } else {\n";
code += " return new " + struct_def.name;
code += "().__assign(tableOffset, bb);\n";
code += " return (obj == null ? ";
code += "new " + struct_def.name;
code += "(), obj).__assign(tableOffset, bb);\n";
code += " }\n }\n";
code += " return null;\n";
code += " }\n";
......
......@@ -43,7 +43,8 @@ public struct Monster : IFlatbufferObject
/// multiline too
public Monster? Testarrayoftables(int j) { int o = __p.__offset(26); return o != 0 ? (Monster?)(new Monster()).__assign(__p.__indirect(__p.__vector(o) + j * 4), __p.bb) : null; }
public int TestarrayoftablesLength { get { int o = __p.__offset(26); return o != 0 ? __p.__vector_len(o) : 0; } }
public Monster? TestarrayoftablesByKey(string key) { int o = __p.__offset(26); return o != 0 ? Monster.__lookup_by_key(__p.__vector(o), key, __p.bb) : null; }
public Monster? TestarrayoftablesByKey(string key) { int o = __p.__offset(26); return o != 0 ? Monster.__lookup_by_key(null, __p.__vector(o), key, __p.bb) : null; }
public Monster? TestarrayoftablesByKey(Monster? obj, string key) { int o = __p.__offset(26); return o != 0 ? Monster.__lookup_by_key(obj, __p.__vector(o), key, __p.bb) : null; }
public Monster? Enemy { get { int o = __p.__offset(28); return o != 0 ? (Monster?)(new Monster()).__assign(__p.__indirect(o + __p.bb_pos), __p.bb) : null; } }
public byte Testnestedflatbuffer(int j) { int o = __p.__offset(30); return o != 0 ? __p.bb.Get(__p.__vector(o) + j * 1) : (byte)0; }
public int TestnestedflatbufferLength { get { int o = __p.__offset(30); return o != 0 ? __p.__vector_len(o) : 0; } }
......@@ -100,7 +101,8 @@ public struct Monster : IFlatbufferObject
public MyGame.InParentNamespace? ParentNamespaceTest { get { int o = __p.__offset(72); return o != 0 ? (MyGame.InParentNamespace?)(new MyGame.InParentNamespace()).__assign(__p.__indirect(o + __p.bb_pos), __p.bb) : null; } }
public Referrable? VectorOfReferrables(int j) { int o = __p.__offset(74); return o != 0 ? (Referrable?)(new Referrable()).__assign(__p.__indirect(__p.__vector(o) + j * 4), __p.bb) : null; }
public int VectorOfReferrablesLength { get { int o = __p.__offset(74); return o != 0 ? __p.__vector_len(o) : 0; } }
public Referrable? VectorOfReferrablesByKey(ulong key) { int o = __p.__offset(74); return o != 0 ? Referrable.__lookup_by_key(__p.__vector(o), key, __p.bb) : null; }
public Referrable? VectorOfReferrablesByKey(ulong key) { int o = __p.__offset(74); return o != 0 ? Referrable.__lookup_by_key(null, __p.__vector(o), key, __p.bb) : null; }
public Referrable? VectorOfReferrablesByKey(Referrable? obj, ulong key) { int o = __p.__offset(74); return o != 0 ? Referrable.__lookup_by_key(obj, __p.__vector(o), key, __p.bb) : null; }
public ulong SingleWeakReference { get { int o = __p.__offset(76); return o != 0 ? __p.bb.GetUlong(o + __p.bb_pos) : (ulong)0; } }
public bool MutateSingleWeakReference(ulong single_weak_reference) { int o = __p.__offset(76); if (o != 0) { __p.bb.PutUlong(o + __p.bb_pos, single_weak_reference); return true; } else { return false; } }
public ulong VectorOfWeakReferences(int j) { int o = __p.__offset(78); return o != 0 ? __p.bb.GetUlong(__p.__vector(o) + j * 8) : (ulong)0; }
......@@ -183,7 +185,7 @@ public struct Monster : IFlatbufferObject
return builder.CreateVectorOfTables(offsets);
}
public static Monster? __lookup_by_key(int vectorLocation, string key, ByteBuffer bb) {
public static Monster? __lookup_by_key(Monster obj, int vectorLocation, string key, ByteBuffer bb) {
byte[] byteKey = System.Text.Encoding.UTF8.GetBytes(key);
int span = bb.GetInt(vectorLocation - 4);
int start = 0;
......@@ -198,7 +200,7 @@ public struct Monster : IFlatbufferObject
start += middle;
span -= middle;
} else {
return new Monster().__assign(tableOffset, bb);
return (obj == null ? new Monster(), obj).__assign(tableOffset, bb);
}
}
return null;
......
......@@ -49,7 +49,8 @@ public final class Monster extends Table {
public Monster testarrayoftables(int j) { return testarrayoftables(new Monster(), j); }
public Monster testarrayoftables(Monster obj, int j) { int o = __offset(26); return o != 0 ? obj.__assign(__indirect(__vector(o) + j * 4), bb) : null; }
public int testarrayoftablesLength() { int o = __offset(26); return o != 0 ? __vector_len(o) : 0; }
public Monster testarrayoftablesByKey(String key) { int o = __offset(26); return o != 0 ? Monster.__lookup_by_key(__vector(o), key, bb) : null; }
public Monster testarrayoftablesByKey(String key) { int o = __offset(26); return o != 0 ? Monster.__lookup_by_key(null, __vector(o), key, bb) : null; }
public Monster testarrayoftablesByKey(Monster obj, String key) { int o = __offset(26); return o != 0 ? Monster.__lookup_by_key(obj, __vector(o), key, bb) : null; }
public Monster enemy() { return enemy(new Monster()); }
public Monster enemy(Monster obj) { int o = __offset(28); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; }
public int testnestedflatbuffer(int j) { int o = __offset(30); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; }
......@@ -118,7 +119,8 @@ public final class Monster extends Table {
public Referrable vectorOfReferrables(int j) { return vectorOfReferrables(new Referrable(), j); }
public Referrable vectorOfReferrables(Referrable obj, int j) { int o = __offset(74); return o != 0 ? obj.__assign(__indirect(__vector(o) + j * 4), bb) : null; }
public int vectorOfReferrablesLength() { int o = __offset(74); return o != 0 ? __vector_len(o) : 0; }
public Referrable vectorOfReferrablesByKey(long key) { int o = __offset(74); return o != 0 ? Referrable.__lookup_by_key(__vector(o), key, bb) : null; }
public Referrable vectorOfReferrablesByKey(long key) { int o = __offset(74); return o != 0 ? Referrable.__lookup_by_key(null, __vector(o), key, bb) : null; }
public Referrable vectorOfReferrablesByKey(Referrable obj, long key) { int o = __offset(74); return o != 0 ? Referrable.__lookup_by_key(obj, __vector(o), key, bb) : null; }
public long singleWeakReference() { int o = __offset(76); return o != 0 ? bb.getLong(o + bb_pos) : 0L; }
public boolean mutateSingleWeakReference(long single_weak_reference) { int o = __offset(76); if (o != 0) { bb.putLong(o + bb_pos, single_weak_reference); return true; } else { return false; } }
public long vectorOfWeakReferences(int j) { int o = __offset(78); return o != 0 ? bb.getLong(__vector(o) + j * 8) : 0; }
......@@ -200,7 +202,7 @@ public final class Monster extends Table {
@Override
protected int keysCompare(Integer o1, Integer o2, ByteBuffer _bb) { return compareStrings(__offset(10, o1, _bb), __offset(10, o2, _bb), _bb); }
public static Monster __lookup_by_key(int vectorLocation, String key, ByteBuffer bb) {
public static Monster __lookup_by_key(Monster obj, int vectorLocation, String key, ByteBuffer bb) {
byte[] byteKey = key.getBytes(Table.UTF8_CHARSET.get());
int span = bb.getInt(vectorLocation - 4);
int start = 0;
......@@ -215,7 +217,7 @@ public final class Monster extends Table {
start += middle;
span -= middle;
} else {
return new Monster().__assign(tableOffset, bb);
return (obj == null ? new Monster(), obj).__assign(tableOffset, bb);
}
}
return null;
......
......@@ -16,6 +16,7 @@ public final class Stat extends Table {
public String id() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; }
public ByteBuffer idAsByteBuffer() { return __vector_as_bytebuffer(4, 1); }
public ByteBuffer idInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 4, 1); }
public long val() { int o = __offset(6); return o != 0 ? bb.getLong(o + bb_pos) : 0L; }
public boolean mutateVal(long val) { int o = __offset(6); if (o != 0) { bb.putLong(o + bb_pos, val); return true; } else { return false; } }
public int count() { int o = __offset(8); return o != 0 ? bb.getShort(o + bb_pos) & 0xFFFF : 0; }
......
......@@ -37,10 +37,12 @@ public final class TypeAliases extends Table {
public byte v8(int j) { int o = __offset(24); return o != 0 ? bb.get(__vector(o) + j * 1) : 0; }
public int v8Length() { int o = __offset(24); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer v8AsByteBuffer() { return __vector_as_bytebuffer(24, 1); }
public ByteBuffer v8InByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 24, 1); }
public boolean mutateV8(int j, byte v8) { int o = __offset(24); if (o != 0) { bb.put(__vector(o) + j * 1, v8); return true; } else { return false; } }
public double vf64(int j) { int o = __offset(26); return o != 0 ? bb.getDouble(__vector(o) + j * 8) : 0; }
public int vf64Length() { int o = __offset(26); return o != 0 ? __vector_len(o) : 0; }
public ByteBuffer vf64AsByteBuffer() { return __vector_as_bytebuffer(26, 8); }
public ByteBuffer vf64InByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 26, 8); }
public boolean mutateVf64(int j, double vf64) { int o = __offset(26); if (o != 0) { bb.putDouble(__vector(o) + j * 8, vf64); return true; } else { return false; } }
public static int createTypeAliases(FlatBufferBuilder builder,
......
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