Commit 7196c368 authored by Maor Itzkovitch's avatar Maor Itzkovitch

begin to correct C# bytebuffer behavior

parent 6be146d6
......@@ -330,6 +330,25 @@ static std::string DestinationValue(const LanguageParameters &lang,
}
}
static std::string SourceCast(const LanguageParameters &lang,
const Type &type) {
switch (lang.language) {
case GeneratorOptions::kJava:
if (type.base_type == BASE_TYPE_UINT) return "(int)";
else if (type.base_type == BASE_TYPE_USHORT) return "(short)";
else if (type.base_type == BASE_TYPE_UCHAR) return "(byte)";
break;
case GeneratorOptions::kCSharp:
if (type.enum_def != nullptr &&
type.base_type != BASE_TYPE_UNION)
return "(" + GenTypeGet(lang, type) + ")";
break;
default:
break;
}
return "";
}
static std::string GenDefaultValue(const Value &value) {
return value.type.base_type == BASE_TYPE_BOOL
? (value.constant == "0" ? "false" : "true")
......@@ -570,6 +589,7 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
std::string type_name_dest = GenTypeNameDest(lang, field.value.type);
std::string dest_mask = DestinationMask(lang, field.value.type, true);
std::string dest_cast = DestinationCast(lang, field.value.type);
std::string src_cast = SourceCast(lang, field.value.type);
std::string method_start = " public " + type_name_dest + " " +
MakeCamel(field.name, lang.first_camel_upper);
// Most field accessors need to retrieve and test the field offset first,
......@@ -725,15 +745,15 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
code += " public ";
code += struct_def.fixed ? "void " : lang.bool_type;
code += mutator_prefix + MakeCamel(field.name, true) + "(";
code += GenTypeBasic(lang, field.value.type);
code += GenTypeNameDest(lang, field.value.type);
code += " " + field.name + ") { ";
if (struct_def.fixed) {
code += GenSetter(lang, field.value.type) + "(bb_pos + ";
code += NumToString(field.value.offset) + ", " + setter_parameter + "); }\n";
code += NumToString(field.value.offset) + ", " + src_cast + setter_parameter + "); }\n";
} else {
code += "int o = __offset(" + NumToString(field.value.offset) + ");";
code += " if (o != 0) { " + GenSetter(lang, field.value.type);
code += "(o + bb_pos, " + setter_parameter + "); return true; } else { return false; } }\n";
code += "(o + bb_pos, " + src_cast + setter_parameter + "); return true; } else { return false; } }\n";
}
}
}
......
......@@ -81,6 +81,7 @@ class JavaTest {
Monster.addTest4(fbb, test4);
Monster.addTestarrayofstring(fbb, testArrayOfString);
Monster.addTestbool(fbb, false);
Monster.addTesthashu32Fnv1(fbb, Integer.MAX_VALUE + 1L);
int mon = Monster.endMonster(fbb);
Monster.finishMonsterBuffer(fbb, mon);
......@@ -101,12 +102,12 @@ class JavaTest {
}
// Test it:
TestBuffer(fbb.dataBuffer());
TestExtendedBuffer(fbb.dataBuffer());
// Make sure it also works with read only ByteBuffers. This is slower,
// since creating strings incurs an additional copy
// (see Table.__string).
TestBuffer(fbb.dataBuffer().asReadOnlyBuffer());
TestExtendedBuffer(fbb.dataBuffer().asReadOnlyBuffer());
TestEnums();
......@@ -115,7 +116,7 @@ class JavaTest {
Monster monster = Monster.getRootAsMonster(fbb.dataBuffer());
// mana is optional and does not exist in the buffer so the mutation should fail
// it should retain its default value
// the mana field should retain its default value
TestEq(monster.mutateMana((short)10), false);
TestEq(monster.mana(), (short)150);
......@@ -127,7 +128,6 @@ class JavaTest {
TestEq(monster.testType(), (byte)Any.Monster);
// get a struct field and edit one of its fields
//
Vec3 pos = monster.pos();
TestEq(pos.x(), 1.0f);
pos.mutateX(55.0f);
......@@ -135,7 +135,7 @@ class JavaTest {
pos.mutateX(1.0f);
TestEq(pos.x(), 1.0f);
TestBuffer(fbb.dataBuffer().asReadOnlyBuffer());
TestExtendedBuffer(fbb.dataBuffer().asReadOnlyBuffer());
System.out.println("FlatBuffers test: completed successfully");
}
......@@ -149,7 +149,7 @@ class JavaTest {
static void TestBuffer(ByteBuffer bb) {
TestEq(Monster.MonsterBufferHasIdentifier(bb), true);
Monster monster = Monster.getRootAsMonster(bb);
TestEq(monster.hp(), (short)80);
......@@ -198,6 +198,16 @@ class JavaTest {
TestEq(monster.testbool(), false);
}
// this method checks additional fields not present in the binary buffer read from file
// these new tests are performed on top of the regular tests
static void TestExtendedBuffer(ByteBuffer bb) {
TestBuffer(bb);
Monster monster = Monster.getRootAsMonster(bb);
TestEq(monster.testhashu32Fnv1(), Integer.MAX_VALUE + 1L);
}
static <T> void TestEq(T a, T b) {
if (!a.equals(b)) {
System.out.println("" + a.getClass().getName() + " " + b.getClass().getName());
......
......@@ -21,9 +21,9 @@ public sealed class Monster : Table {
public byte GetInventory(int j) { int o = __offset(14); return o != 0 ? bb.Get(__vector(o) + j * 1) : (byte)0; }
public int InventoryLength { get { int o = __offset(14); return o != 0 ? __vector_len(o) : 0; } }
public Color Color { get { int o = __offset(16); return o != 0 ? (Color)bb.GetSbyte(o + bb_pos) : (Color)8; } }
public bool MutateColor(sbyte color) { int o = __offset(16); if (o != 0) { bb.PutSbyte(o + bb_pos, color); return true; } else { return false; } }
public bool MutateColor(Color color) { int o = __offset(16); if (o != 0) { bb.PutSbyte(o + bb_pos, (sbyte)color); return true; } else { return false; } }
public Any TestType { get { int o = __offset(18); return o != 0 ? (Any)bb.Get(o + bb_pos) : (Any)0; } }
public bool MutateTestType(byte test_type) { int o = __offset(18); if (o != 0) { bb.Put(o + bb_pos, test_type); return true; } else { return false; } }
public bool MutateTestType(Any test_type) { int o = __offset(18); if (o != 0) { bb.Put(o + bb_pos, (byte)test_type); return true; } else { return false; } }
public TTable GetTest<TTable>(TTable obj) where TTable : Table { int o = __offset(20); return o != 0 ? __union(obj, o) : null; }
public Test GetTest4(int j) { return GetTest4(new Test(), j); }
public Test GetTest4(Test obj, int j) { int o = __offset(22); return o != 0 ? obj.__init(__vector(o) + j * 4, bb) : null; }
......
......@@ -53,7 +53,7 @@ public final class Monster extends Table {
public int testhashs32Fnv1() { int o = __offset(36); return o != 0 ? bb.getInt(o + bb_pos) : 0; }
public boolean mutateTesthashs32Fnv1(int testhashs32_fnv1) { int o = __offset(36); if (o != 0) { bb.putInt(o + bb_pos, testhashs32_fnv1); return true; } else { return false; } }
public long testhashu32Fnv1() { int o = __offset(38); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0; }
public boolean mutateTesthashu32Fnv1(int testhashu32_fnv1) { int o = __offset(38); if (o != 0) { bb.putInt(o + bb_pos, testhashu32_fnv1); return true; } else { return false; } }
public boolean mutateTesthashu32Fnv1(long testhashu32_fnv1) { int o = __offset(38); if (o != 0) { bb.putInt(o + bb_pos, (int)testhashu32_fnv1); return true; } else { return false; } }
public long testhashs64Fnv1() { int o = __offset(40); return o != 0 ? bb.getLong(o + bb_pos) : 0; }
public boolean mutateTesthashs64Fnv1(long testhashs64_fnv1) { int o = __offset(40); if (o != 0) { bb.putLong(o + bb_pos, testhashs64_fnv1); return true; } else { return false; } }
public long testhashu64Fnv1() { int o = __offset(42); return o != 0 ? bb.getLong(o + bb_pos) : 0; }
......@@ -61,7 +61,7 @@ public final class Monster extends Table {
public int testhashs32Fnv1a() { int o = __offset(44); return o != 0 ? bb.getInt(o + bb_pos) : 0; }
public boolean mutateTesthashs32Fnv1a(int testhashs32_fnv1a) { int o = __offset(44); if (o != 0) { bb.putInt(o + bb_pos, testhashs32_fnv1a); return true; } else { return false; } }
public long testhashu32Fnv1a() { int o = __offset(46); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0; }
public boolean mutateTesthashu32Fnv1a(int testhashu32_fnv1a) { int o = __offset(46); if (o != 0) { bb.putInt(o + bb_pos, testhashu32_fnv1a); return true; } else { return false; } }
public boolean mutateTesthashu32Fnv1a(long testhashu32_fnv1a) { int o = __offset(46); if (o != 0) { bb.putInt(o + bb_pos, (int)testhashu32_fnv1a); return true; } else { return false; } }
public long testhashs64Fnv1a() { int o = __offset(48); return o != 0 ? bb.getLong(o + bb_pos) : 0; }
public boolean mutateTesthashs64Fnv1a(long testhashs64_fnv1a) { int o = __offset(48); if (o != 0) { bb.putLong(o + bb_pos, testhashs64_fnv1a); return true; } else { return false; } }
public long testhashu64Fnv1a() { int o = __offset(50); return o != 0 ? bb.getLong(o + bb_pos) : 0; }
......
......@@ -17,7 +17,7 @@ public final class Stat extends Table {
public long val() { int o = __offset(6); return o != 0 ? bb.getLong(o + bb_pos) : 0; }
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; }
public boolean mutateCount(short count) { int o = __offset(8); if (o != 0) { bb.putShort(o + bb_pos, count); return true; } else { return false; } }
public boolean mutateCount(int count) { int o = __offset(8); if (o != 0) { bb.putShort(o + bb_pos, (short)count); return true; } else { return false; } }
public static int createStat(FlatBufferBuilder builder,
int id,
......
......@@ -17,7 +17,7 @@ public sealed class Vec3 : Struct {
public double Test1 { get { return bb.GetDouble(bb_pos + 16); } }
public void MutateTest1(double test1) { bb.PutDouble(bb_pos + 16, test1); }
public Color Test2 { get { return (Color)bb.GetSbyte(bb_pos + 24); } }
public void MutateTest2(sbyte test2) { bb.PutSbyte(bb_pos + 24, test2); }
public void MutateTest2(Color test2) { bb.PutSbyte(bb_pos + 24, (sbyte)test2); }
public Test Test3 { get { return GetTest3(new Test()); } }
public Test GetTest3(Test obj) { return obj.__init(bb_pos + 26, bb); }
......
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