forked from BigfootDev/flatbuffers
Support for booleans in the Java/C# API
Change-Id: I72e92183a7b5f4145ea51fcec29257dc9553a461
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -34,6 +34,7 @@ flatsamplebinary
|
||||
flatsampletext
|
||||
snapshot.sh
|
||||
tests/go_gen
|
||||
tests/monsterdata_java_wire.mon
|
||||
CMakeLists.txt.user
|
||||
CMakeScripts/**
|
||||
build/Xcode/FlatBuffers.xcodeproj/project.xcworkspace/**
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace flatbuffers {
|
||||
#define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
|
||||
TD(NONE, "", uint8_t, byte, byte, byte) \
|
||||
TD(UTYPE, "", uint8_t, byte, byte, byte) /* begin scalar/int */ \
|
||||
TD(BOOL, "bool", uint8_t, byte, byte, byte) \
|
||||
TD(BOOL, "bool", uint8_t, boolean,byte, bool) \
|
||||
TD(CHAR, "byte", int8_t, byte, int8, sbyte) \
|
||||
TD(UCHAR, "ubyte", uint8_t, byte, byte, byte) \
|
||||
TD(SHORT, "short", int16_t, short, int16, short) \
|
||||
|
||||
@@ -102,21 +102,23 @@ public class FlatBufferBuilder {
|
||||
|
||||
// Add a scalar to the buffer, backwards from the current location.
|
||||
// Doesn't align nor check for space.
|
||||
public void putByte (byte x) { bb.put (space -= 1, x); }
|
||||
public void putShort (short x) { bb.putShort (space -= 2, x); }
|
||||
public void putInt (int x) { bb.putInt (space -= 4, x); }
|
||||
public void putLong (long x) { bb.putLong (space -= 8, x); }
|
||||
public void putFloat (float x) { bb.putFloat (space -= 4, x); }
|
||||
public void putDouble(double x) { bb.putDouble(space -= 8, x); }
|
||||
public void putBoolean(boolean x) { bb.put (space -= 1, (byte)(x ? 1 : 0)); }
|
||||
public void putByte (byte x) { bb.put (space -= 1, x); }
|
||||
public void putShort (short x) { bb.putShort (space -= 2, x); }
|
||||
public void putInt (int x) { bb.putInt (space -= 4, x); }
|
||||
public void putLong (long x) { bb.putLong (space -= 8, x); }
|
||||
public void putFloat (float x) { bb.putFloat (space -= 4, x); }
|
||||
public void putDouble (double x) { bb.putDouble(space -= 8, x); }
|
||||
|
||||
// Adds a scalar to the buffer, properly aligned, and the buffer grown
|
||||
// if needed.
|
||||
public void addByte (byte x) { prep(1, 0); putByte (x); }
|
||||
public void addShort (short x) { prep(2, 0); putShort (x); }
|
||||
public void addInt (int x) { prep(4, 0); putInt (x); }
|
||||
public void addLong (long x) { prep(8, 0); putLong (x); }
|
||||
public void addFloat (float x) { prep(4, 0); putFloat (x); }
|
||||
public void addDouble(double x) { prep(8, 0); putDouble(x); }
|
||||
public void addBoolean(boolean x) { prep(1, 0); putBoolean(x); }
|
||||
public void addByte (byte x) { prep(1, 0); putByte (x); }
|
||||
public void addShort (short x) { prep(2, 0); putShort (x); }
|
||||
public void addInt (int x) { prep(4, 0); putInt (x); }
|
||||
public void addLong (long x) { prep(8, 0); putLong (x); }
|
||||
public void addFloat (float x) { prep(4, 0); putFloat (x); }
|
||||
public void addDouble (double x) { prep(8, 0); putDouble (x); }
|
||||
|
||||
// Adds on offset, relative to where it will be written.
|
||||
public void addOffset(int off) {
|
||||
@@ -169,13 +171,14 @@ public class FlatBufferBuilder {
|
||||
}
|
||||
|
||||
// Add a scalar to a table at `o` into its vtable, with value `x` and default `d`
|
||||
public void addByte (int o, byte x, int d) { if(x != d) { addByte (x); slot(o); } }
|
||||
public void addShort (int o, short x, int d) { if(x != d) { addShort (x); slot(o); } }
|
||||
public void addInt (int o, int x, int d) { if(x != d) { addInt (x); slot(o); } }
|
||||
public void addLong (int o, long x, long d) { if(x != d) { addLong (x); slot(o); } }
|
||||
public void addFloat (int o, float x, double d) { if(x != d) { addFloat (x); slot(o); } }
|
||||
public void addDouble(int o, double x, double d) { if(x != d) { addDouble(x); slot(o); } }
|
||||
public void addOffset(int o, int x, int d) { if(x != d) { addOffset(x); slot(o); } }
|
||||
public void addBoolean(int o, boolean x, boolean d) { if(x != d) { addBoolean(x); slot(o); } }
|
||||
public void addByte (int o, byte x, int d) { if(x != d) { addByte (x); slot(o); } }
|
||||
public void addShort (int o, short x, int d) { if(x != d) { addShort (x); slot(o); } }
|
||||
public void addInt (int o, int x, int d) { if(x != d) { addInt (x); slot(o); } }
|
||||
public void addLong (int o, long x, long d) { if(x != d) { addLong (x); slot(o); } }
|
||||
public void addFloat (int o, float x, double d) { if(x != d) { addFloat (x); slot(o); } }
|
||||
public void addDouble (int o, double x, double d) { if(x != d) { addDouble (x); slot(o); } }
|
||||
public void addOffset (int o, int x, int d) { if(x != d) { addOffset (x); slot(o); } }
|
||||
|
||||
// Structs are stored inline, so nothing additional is being added. `d` is always 0.
|
||||
public void addStruct(int voffset, int x, int d) {
|
||||
|
||||
@@ -105,6 +105,11 @@ namespace FlatBuffers
|
||||
Pad(alignSize);
|
||||
}
|
||||
|
||||
public void PutBool(bool x)
|
||||
{
|
||||
_bb.PutByte(_space -= sizeof(byte), (byte)(x ? 1 : 0));
|
||||
}
|
||||
|
||||
public void PutSbyte(sbyte x)
|
||||
{
|
||||
_bb.PutSbyte(_space -= sizeof(sbyte), x);
|
||||
@@ -157,6 +162,7 @@ namespace FlatBuffers
|
||||
|
||||
// Adds a scalar to the buffer, properly aligned, and the buffer grown
|
||||
// if needed.
|
||||
public void AddBool(bool x) { Prep(sizeof(byte), 0); PutBool(x); }
|
||||
public void AddSbyte(sbyte x) { Prep(sizeof(sbyte), 0); PutSbyte(x); }
|
||||
public void AddByte(byte x) { Prep(sizeof(byte), 0); PutByte(x); }
|
||||
public void AddShort(short x) { Prep(sizeof(short), 0); PutShort(x); }
|
||||
@@ -231,6 +237,7 @@ namespace FlatBuffers
|
||||
}
|
||||
|
||||
// Add a scalar to a table at `o` into its vtable, with value `x` and default `d`
|
||||
public void AddBool(int o, bool x, bool d) { if (x != d) { AddBool(x); Slot(o); } }
|
||||
public void AddSbyte(int o, sbyte x, sbyte d) { if (x != d) { AddSbyte(x); Slot(o); } }
|
||||
public void AddByte(int o, byte x, byte d) { if (x != d) { AddByte(x); Slot(o); } }
|
||||
public void AddShort(int o, short x, int d) { if (x != d) { AddShort(x); Slot(o); } }
|
||||
|
||||
@@ -214,13 +214,17 @@ static std::string GenGetter(const LanguageParameters &lang,
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_STRING: return "__string";
|
||||
case BASE_TYPE_STRUCT: return "__struct";
|
||||
case BASE_TYPE_UNION: return "__union";
|
||||
case BASE_TYPE_UNION: return "__union";
|
||||
case BASE_TYPE_VECTOR: return GenGetter(lang, type.VectorType());
|
||||
default:
|
||||
return "bb." + FunctionStart(lang, 'G') + "et" +
|
||||
(GenTypeBasic(lang, type) != "byte"
|
||||
? MakeCamel(GenTypeGet(lang, type))
|
||||
: "");
|
||||
default: {
|
||||
std::string getter = "bb." + FunctionStart(lang, 'G') + "et";
|
||||
if (type.base_type == BASE_TYPE_BOOL) {
|
||||
getter = "0!=" + getter;
|
||||
} else if (GenTypeBasic(lang, type) != "byte") {
|
||||
getter += MakeCamel(GenTypeGet(lang, type));
|
||||
}
|
||||
return getter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,7 +368,10 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
|
||||
code += "(bb_pos + " + NumToString(field.value.offset) + ")";
|
||||
} else {
|
||||
code += offset_prefix + getter;
|
||||
code += "(o + bb_pos) : " + default_cast + field.value.constant;
|
||||
code += "(o + bb_pos) : " + default_cast;
|
||||
code += field.value.type.base_type == BASE_TYPE_BOOL
|
||||
? (field.value.constant == "0" ? "false" : "true")
|
||||
: field.value.constant;
|
||||
}
|
||||
} else {
|
||||
switch (field.value.type.base_type) {
|
||||
@@ -524,7 +531,12 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser,
|
||||
code += " " + argname + ") { builder." + FunctionStart(lang, 'A') + "dd";
|
||||
code += GenMethod(lang, field.value.type) + "(";
|
||||
code += NumToString(it - struct_def.fields.vec.begin()) + ", ";
|
||||
code += argname + ", " + field.value.constant;
|
||||
code += argname + ", ";
|
||||
if (field.value.type.base_type == BASE_TYPE_BOOL) {
|
||||
code += field.value.constant == "0" ? "false" : "true";
|
||||
} else {
|
||||
code += field.value.constant;
|
||||
}
|
||||
code += "); }\n";
|
||||
if (field.value.type.base_type == BASE_TYPE_VECTOR) {
|
||||
auto vector_type = field.value.type.VectorType();
|
||||
|
||||
@@ -76,6 +76,7 @@ namespace FlatBuffers.Test
|
||||
Monster.AddTest(fbb, mon2);
|
||||
Monster.AddTest4(fbb, test4);
|
||||
Monster.AddTestarrayofstring(fbb, testArrayOfString);
|
||||
Monster.AddTestbool(fbb, false);
|
||||
var mon = Monster.EndMonster(fbb);
|
||||
|
||||
fbb.Finish(mon);
|
||||
@@ -134,6 +135,8 @@ namespace FlatBuffers.Test
|
||||
Assert.AreEqual(2, monster.TestarrayofstringLength());
|
||||
Assert.AreEqual("test1", monster.Testarrayofstring(0));
|
||||
Assert.AreEqual("test2", monster.Testarrayofstring(1));
|
||||
|
||||
Assert.AreEqual(false, monster.Testbool());
|
||||
}
|
||||
|
||||
public void CanReadCppGeneratedWireFile()
|
||||
|
||||
@@ -80,6 +80,7 @@ class JavaTest {
|
||||
Monster.addTest(fbb, mon2);
|
||||
Monster.addTest4(fbb, test4);
|
||||
Monster.addTestarrayofstring(fbb, testArrayOfString);
|
||||
Monster.addTestbool(fbb, false);
|
||||
int mon = Monster.endMonster(fbb);
|
||||
|
||||
Monster.finishMonsterBuffer(fbb, mon);
|
||||
@@ -166,6 +167,8 @@ class JavaTest {
|
||||
TestEq(monster.testarrayofstringLength(), 2);
|
||||
TestEq(monster.testarrayofstring(0),"test1");
|
||||
TestEq(monster.testarrayofstring(1),"test2");
|
||||
|
||||
TestEq(monster.testbool(), false);
|
||||
}
|
||||
|
||||
static <T> void TestEq(T a, T b) {
|
||||
|
||||
@@ -36,8 +36,9 @@ public class Monster : Table {
|
||||
public int TestnestedflatbufferLength() { int o = __offset(30); return o != 0 ? __vector_len(o) : 0; }
|
||||
public Stat Testempty() { return Testempty(new Stat()); }
|
||||
public Stat Testempty(Stat obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; }
|
||||
public bool Testbool() { int o = __offset(34); return o != 0 ? 0!=bb.Get(o + bb_pos) : (bool)false; }
|
||||
|
||||
public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(15); }
|
||||
public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(16); }
|
||||
public static void AddPos(FlatBufferBuilder builder, int posOffset) { builder.AddStruct(0, posOffset, 0); }
|
||||
public static void AddMana(FlatBufferBuilder builder, short mana) { builder.AddShort(1, mana, 150); }
|
||||
public static void AddHp(FlatBufferBuilder builder, short hp) { builder.AddShort(2, hp, 100); }
|
||||
@@ -61,6 +62,7 @@ public class Monster : Table {
|
||||
public static int CreateTestnestedflatbufferVector(FlatBufferBuilder builder, byte[] data) { builder.StartVector(1, data.Length, 1); for (int i = data.Length - 1; i >= 0; i--) builder.AddByte(data[i]); return builder.EndVector(); }
|
||||
public static void StartTestnestedflatbufferVector(FlatBufferBuilder builder, int numElems) { builder.StartVector(1, numElems, 1); }
|
||||
public static void AddTestempty(FlatBufferBuilder builder, int testemptyOffset) { builder.AddOffset(14, testemptyOffset, 0); }
|
||||
public static void AddTestbool(FlatBufferBuilder builder, bool testbool) { builder.AddBool(15, testbool, false); }
|
||||
public static int EndMonster(FlatBufferBuilder builder) {
|
||||
int o = builder.EndObject();
|
||||
builder.Required(o, 10); // name
|
||||
|
||||
@@ -207,7 +207,15 @@ func (rcv *Monster) Testempty(obj *Stat) *Stat {
|
||||
return nil
|
||||
}
|
||||
|
||||
func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(15) }
|
||||
func (rcv *Monster) Testbool() byte {
|
||||
o := flatbuffers.UOffsetT(rcv._tab.Offset(34))
|
||||
if o != 0 {
|
||||
return rcv._tab.GetByte(o + rcv._tab.Pos)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(16) }
|
||||
func MonsterAddPos(builder *flatbuffers.Builder, pos flatbuffers.UOffsetT) { builder.PrependStructSlot(0, flatbuffers.UOffsetT(pos), 0) }
|
||||
func MonsterAddMana(builder *flatbuffers.Builder, mana int16) { builder.PrependInt16Slot(1, mana, 150) }
|
||||
func MonsterAddHp(builder *flatbuffers.Builder, hp int16) { builder.PrependInt16Slot(2, hp, 100) }
|
||||
@@ -232,4 +240,5 @@ func MonsterAddTestnestedflatbuffer(builder *flatbuffers.Builder, testnestedflat
|
||||
func MonsterStartTestnestedflatbufferVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(1, numElems, 1)
|
||||
}
|
||||
func MonsterAddTestempty(builder *flatbuffers.Builder, testempty flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(14, flatbuffers.UOffsetT(testempty), 0) }
|
||||
func MonsterAddTestbool(builder *flatbuffers.Builder, testbool byte) { builder.PrependByteSlot(15, testbool, 0) }
|
||||
func MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() }
|
||||
|
||||
@@ -41,8 +41,9 @@ public class Monster extends Table {
|
||||
public ByteBuffer testnestedflatbufferAsByteBuffer() { return __vector_as_bytebuffer(30, 1); }
|
||||
public Stat testempty() { return testempty(new Stat()); }
|
||||
public Stat testempty(Stat obj) { int o = __offset(32); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; }
|
||||
public boolean testbool() { int o = __offset(34); return o != 0 ? 0!=bb.get(o + bb_pos) : false; }
|
||||
|
||||
public static void startMonster(FlatBufferBuilder builder) { builder.startObject(15); }
|
||||
public static void startMonster(FlatBufferBuilder builder) { builder.startObject(16); }
|
||||
public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); }
|
||||
public static void addMana(FlatBufferBuilder builder, short mana) { builder.addShort(1, mana, 150); }
|
||||
public static void addHp(FlatBufferBuilder builder, short hp) { builder.addShort(2, hp, 100); }
|
||||
@@ -66,6 +67,7 @@ public class Monster extends Table {
|
||||
public static int createTestnestedflatbufferVector(FlatBufferBuilder builder, byte[] data) { builder.startVector(1, data.length, 1); for (int i = data.length - 1; i >= 0; i--) builder.addByte(data[i]); return builder.endVector(); }
|
||||
public static void startTestnestedflatbufferVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); }
|
||||
public static void addTestempty(FlatBufferBuilder builder, int testemptyOffset) { builder.addOffset(14, testemptyOffset, 0); }
|
||||
public static void addTestbool(FlatBufferBuilder builder, boolean testbool) { builder.addBoolean(15, testbool, false); }
|
||||
public static int endMonster(FlatBufferBuilder builder) {
|
||||
int o = builder.endObject();
|
||||
builder.required(o, 10); // name
|
||||
|
||||
@@ -43,6 +43,7 @@ table Monster {
|
||||
test4:[Test] (id: 9);
|
||||
testnestedflatbuffer:[ubyte] (id:13, nested_flatbuffer: "Monster");
|
||||
testempty:Stat (id:14);
|
||||
testbool:bool (id:15);
|
||||
}
|
||||
|
||||
root_type Monster;
|
||||
|
||||
@@ -140,6 +140,7 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
const flatbuffers::Vector<uint8_t> *testnestedflatbuffer() const { return GetPointer<const flatbuffers::Vector<uint8_t> *>(30); }
|
||||
const Monster *testnestedflatbuffer_nested_root() { return flatbuffers::GetRoot<Monster>(testnestedflatbuffer()->Data()); }
|
||||
const Stat *testempty() const { return GetPointer<const Stat *>(32); }
|
||||
uint8_t testbool() const { return GetField<uint8_t>(34, 0); }
|
||||
bool Verify(flatbuffers::Verifier &verifier) const {
|
||||
return VerifyTableStart(verifier) &&
|
||||
VerifyField<Vec3>(verifier, 4 /* pos */) &&
|
||||
@@ -167,6 +168,7 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
|
||||
verifier.Verify(testnestedflatbuffer()) &&
|
||||
VerifyField<flatbuffers::uoffset_t>(verifier, 32 /* testempty */) &&
|
||||
verifier.VerifyTable(testempty()) &&
|
||||
VerifyField<uint8_t>(verifier, 34 /* testbool */) &&
|
||||
verifier.EndTable();
|
||||
}
|
||||
};
|
||||
@@ -188,10 +190,11 @@ struct MonsterBuilder {
|
||||
void add_enemy(flatbuffers::Offset<Monster> enemy) { fbb_.AddOffset(28, enemy); }
|
||||
void add_testnestedflatbuffer(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> testnestedflatbuffer) { fbb_.AddOffset(30, testnestedflatbuffer); }
|
||||
void add_testempty(flatbuffers::Offset<Stat> testempty) { fbb_.AddOffset(32, testempty); }
|
||||
void add_testbool(uint8_t testbool) { fbb_.AddElement<uint8_t>(34, testbool, 0); }
|
||||
MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
|
||||
MonsterBuilder &operator=(const MonsterBuilder &);
|
||||
flatbuffers::Offset<Monster> Finish() {
|
||||
auto o = flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 15));
|
||||
auto o = flatbuffers::Offset<Monster>(fbb_.EndTable(start_, 16));
|
||||
fbb_.Required(o, 10); // name
|
||||
return o;
|
||||
}
|
||||
@@ -211,7 +214,8 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
|
||||
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<Monster>>> testarrayoftables = 0,
|
||||
flatbuffers::Offset<Monster> enemy = 0,
|
||||
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> testnestedflatbuffer = 0,
|
||||
flatbuffers::Offset<Stat> testempty = 0) {
|
||||
flatbuffers::Offset<Stat> testempty = 0,
|
||||
uint8_t testbool = 0) {
|
||||
MonsterBuilder builder_(_fbb);
|
||||
builder_.add_testempty(testempty);
|
||||
builder_.add_testnestedflatbuffer(testnestedflatbuffer);
|
||||
@@ -225,6 +229,7 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
|
||||
builder_.add_pos(pos);
|
||||
builder_.add_hp(hp);
|
||||
builder_.add_mana(mana);
|
||||
builder_.add_testbool(testbool);
|
||||
builder_.add_test_type(test_type);
|
||||
builder_.add_color(color);
|
||||
return builder_.Finish();
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user