* Add vectorNumElements attribute to Builder for simpler vector creation.
This adds a default to EndVector which should simplify its use.
* Update tutorial to reflect new default arg in Python EndVector.
* Remove optional argument to Python EndVector.
* Add generated files.
* Unset Builder.vectorNumElems when not in use.
MyGame.Sample.Monster.MonsterStartInventoryVector(builder, 10)
for i in reversed(range(0, 10)):
builder.PrependByte(i)
- inv = builder.EndVector(10)
+ inv = builder.EndVector()
~~~
</div>
<div class="language-javascript">
MyGame.Sample.Monster.MonsterStartWeaponsVector(builder, 2)
builder.PrependUOffsetTRelative(axe)
builder.PrependUOffsetTRelative(sword)
- weapons = builder.EndVector(2)
+ weapons = builder.EndVector()
~~~
</div>
<div class="language-javascript">
MyGame.Sample.Monster.MonsterStartPathVector(builder, 2)
MyGame.Sample.Vec3.CreateVec3(builder, 1.0, 2.0, 3.0)
MyGame.Sample.Vec3.CreateVec3(builder, 4.0, 5.0, 6.0)
- path = builder.EndVector(2)
+ path = builder.EndVector()
~~~
</div>
<div class="language-javascript">
## @cond FLATBUFFERS_INTENRAL
__slots__ = ("Bytes", "current_vtable", "head", "minalign", "objectEnd",
- "vtables", "nested", "forceDefaults", "finished")
+ "vtables", "nested", "forceDefaults", "finished", "vectorNumElems")
"""Maximum buffer size constant, in bytes.
self.assertNotNested()
self.nested = True
+ self.vectorNumElems = numElems
self.Prep(N.Uint32Flags.bytewidth, elemSize*numElems)
self.Prep(alignment, elemSize*numElems) # In case alignment > int.
return self.Offset()
## @endcond
- def EndVector(self, vectorNumElems):
+ def EndVector(self):
"""EndVector writes data necessary to finish vector construction."""
self.assertNested()
self.nested = False
## @endcond
# we already made space for this, so write without PrependUint32
- self.PlaceUOffsetT(vectorNumElems)
+ self.PlaceUOffsetT(self.vectorNumElems)
+ self.vectorNumElems = None
return self.Offset()
def CreateString(self, s, encoding='utf-8', errors='strict'):
## @endcond
self.Bytes[self.Head():self.Head()+l] = x
- return self.EndVector(len(x))
+ self.vectorNumElems = len(x)
+ return self.EndVector()
def CreateByteVector(self, x):
"""CreateString writes a byte vector."""
## @endcond
self.Bytes[self.Head():self.Head()+l] = x
- return self.EndVector(len(x))
+ self.vectorNumElems = len(x)
+ return self.EndVector()
def CreateNumpyVector(self, x):
"""CreateNumpyVector writes a numpy array into the buffer."""
# tobytes ensures c_contiguous ordering
self.Bytes[self.Head():self.Head()+l] = x_lend.tobytes(order='C')
- return self.EndVector(x.size)
+ self.vectorNumElems = x.size
+ return self.EndVector()
## @cond FLATBUFFERS_INTERNAL
def assertNested(self):
field_instance_name + "))):";
code_prefix +=
GenIndents(4) + "self." + field_instance_name + "[i].Pack(builder)";
- code_prefix += GenIndents(3) + field_instance_name +
- " = builder.EndVector(len(self." + field_instance_name +
- "))";
+ code_prefix += GenIndents(3) + field_instance_name + " = builder.EndVector()";
} else {
// If the vector is a struct vector, we need to first build accessor for
// each struct element.
field_instance_name + "))):";
code_prefix += GenIndents(4) + "builder.PrependUOffsetTRelative" + "(" +
field_instance_name + "list[i])";
- code_prefix += GenIndents(3) + field_instance_name +
- " = builder.EndVector(len(self." + field_instance_name +
- "))";
+ code_prefix += GenIndents(3) + field_instance_name + " = builder.EndVector()";
}
// Adds the field into the struct.
field_instance_name + "[i]))";
GenPackForScalarVectorFieldHelper(struct_def, field, code_prefix_ptr, 3);
code_prefix += "(" + MakeLowerCamel(field) + "list[i])";
- code_prefix += GenIndents(3) + field_instance_name +
- " = builder.EndVector(len(self." + field_instance_name +
- "))";
+ code_prefix += GenIndents(3) + field_instance_name + " = builder.EndVector()";
return;
}
code_prefix += GenIndents(3) + "else:";
GenPackForScalarVectorFieldHelper(struct_def, field, code_prefix_ptr, 4);
code_prefix += "(self." + field_instance_name + "[i])";
- code_prefix += GenIndents(4) + field_instance_name +
- " = builder.EndVector(len(self." + field_instance_name +
- "))";
+ code_prefix += GenIndents(4) + field_instance_name + " = builder.EndVector()";
}
void GenPackForStructField(const StructDef &struct_def, const FieldDef &field,
MonsterStartInventoryVector(builder, len(self.inventory))
for i in reversed(range(len(self.inventory))):
builder.PrependUint8(self.inventory[i])
- inventory = builder.EndVector(len(self.inventory))
+ inventory = builder.EndVector()
if self.test is not None:
test = self.test.Pack(builder)
if self.test4 is not None:
MonsterStartTest4Vector(builder, len(self.test4))
for i in reversed(range(len(self.test4))):
self.test4[i].Pack(builder)
- test4 = builder.EndVector(len(self.test4))
+ test4 = builder.EndVector()
if self.testarrayofstring is not None:
testarrayofstringlist = []
for i in range(len(self.testarrayofstring)):
MonsterStartTestarrayofstringVector(builder, len(self.testarrayofstring))
for i in reversed(range(len(self.testarrayofstring))):
builder.PrependUOffsetTRelative(testarrayofstringlist[i])
- testarrayofstring = builder.EndVector(len(self.testarrayofstring))
+ testarrayofstring = builder.EndVector()
if self.testarrayoftables is not None:
testarrayoftableslist = []
for i in range(len(self.testarrayoftables)):
MonsterStartTestarrayoftablesVector(builder, len(self.testarrayoftables))
for i in reversed(range(len(self.testarrayoftables))):
builder.PrependUOffsetTRelative(testarrayoftableslist[i])
- testarrayoftables = builder.EndVector(len(self.testarrayoftables))
+ testarrayoftables = builder.EndVector()
if self.enemy is not None:
enemy = self.enemy.Pack(builder)
if self.testnestedflatbuffer is not None:
MonsterStartTestnestedflatbufferVector(builder, len(self.testnestedflatbuffer))
for i in reversed(range(len(self.testnestedflatbuffer))):
builder.PrependUint8(self.testnestedflatbuffer[i])
- testnestedflatbuffer = builder.EndVector(len(self.testnestedflatbuffer))
+ testnestedflatbuffer = builder.EndVector()
if self.testempty is not None:
testempty = self.testempty.Pack(builder)
if self.testarrayofbools is not None:
MonsterStartTestarrayofboolsVector(builder, len(self.testarrayofbools))
for i in reversed(range(len(self.testarrayofbools))):
builder.PrependBool(self.testarrayofbools[i])
- testarrayofbools = builder.EndVector(len(self.testarrayofbools))
+ testarrayofbools = builder.EndVector()
if self.testarrayofstring2 is not None:
testarrayofstring2list = []
for i in range(len(self.testarrayofstring2)):
MonsterStartTestarrayofstring2Vector(builder, len(self.testarrayofstring2))
for i in reversed(range(len(self.testarrayofstring2))):
builder.PrependUOffsetTRelative(testarrayofstring2list[i])
- testarrayofstring2 = builder.EndVector(len(self.testarrayofstring2))
+ testarrayofstring2 = builder.EndVector()
if self.testarrayofsortedstruct is not None:
MonsterStartTestarrayofsortedstructVector(builder, len(self.testarrayofsortedstruct))
for i in reversed(range(len(self.testarrayofsortedstruct))):
self.testarrayofsortedstruct[i].Pack(builder)
- testarrayofsortedstruct = builder.EndVector(len(self.testarrayofsortedstruct))
+ testarrayofsortedstruct = builder.EndVector()
if self.flex is not None:
if np is not None and type(self.flex) is np.ndarray:
flex = builder.CreateNumpyVector(self.flex)
MonsterStartFlexVector(builder, len(self.flex))
for i in reversed(range(len(self.flex))):
builder.PrependUint8(self.flex[i])
- flex = builder.EndVector(len(self.flex))
+ flex = builder.EndVector()
if self.test5 is not None:
MonsterStartTest5Vector(builder, len(self.test5))
for i in reversed(range(len(self.test5))):
self.test5[i].Pack(builder)
- test5 = builder.EndVector(len(self.test5))
+ test5 = builder.EndVector()
if self.vectorOfLongs is not None:
if np is not None and type(self.vectorOfLongs) is np.ndarray:
vectorOfLongs = builder.CreateNumpyVector(self.vectorOfLongs)
MonsterStartVectorOfLongsVector(builder, len(self.vectorOfLongs))
for i in reversed(range(len(self.vectorOfLongs))):
builder.PrependInt64(self.vectorOfLongs[i])
- vectorOfLongs = builder.EndVector(len(self.vectorOfLongs))
+ vectorOfLongs = builder.EndVector()
if self.vectorOfDoubles is not None:
if np is not None and type(self.vectorOfDoubles) is np.ndarray:
vectorOfDoubles = builder.CreateNumpyVector(self.vectorOfDoubles)
MonsterStartVectorOfDoublesVector(builder, len(self.vectorOfDoubles))
for i in reversed(range(len(self.vectorOfDoubles))):
builder.PrependFloat64(self.vectorOfDoubles[i])
- vectorOfDoubles = builder.EndVector(len(self.vectorOfDoubles))
+ vectorOfDoubles = builder.EndVector()
if self.parentNamespaceTest is not None:
parentNamespaceTest = self.parentNamespaceTest.Pack(builder)
if self.vectorOfReferrables is not None:
MonsterStartVectorOfReferrablesVector(builder, len(self.vectorOfReferrables))
for i in reversed(range(len(self.vectorOfReferrables))):
builder.PrependUOffsetTRelative(vectorOfReferrableslist[i])
- vectorOfReferrables = builder.EndVector(len(self.vectorOfReferrables))
+ vectorOfReferrables = builder.EndVector()
if self.vectorOfWeakReferences is not None:
if np is not None and type(self.vectorOfWeakReferences) is np.ndarray:
vectorOfWeakReferences = builder.CreateNumpyVector(self.vectorOfWeakReferences)
MonsterStartVectorOfWeakReferencesVector(builder, len(self.vectorOfWeakReferences))
for i in reversed(range(len(self.vectorOfWeakReferences))):
builder.PrependUint64(self.vectorOfWeakReferences[i])
- vectorOfWeakReferences = builder.EndVector(len(self.vectorOfWeakReferences))
+ vectorOfWeakReferences = builder.EndVector()
if self.vectorOfStrongReferrables is not None:
vectorOfStrongReferrableslist = []
for i in range(len(self.vectorOfStrongReferrables)):
MonsterStartVectorOfStrongReferrablesVector(builder, len(self.vectorOfStrongReferrables))
for i in reversed(range(len(self.vectorOfStrongReferrables))):
builder.PrependUOffsetTRelative(vectorOfStrongReferrableslist[i])
- vectorOfStrongReferrables = builder.EndVector(len(self.vectorOfStrongReferrables))
+ vectorOfStrongReferrables = builder.EndVector()
if self.vectorOfCoOwningReferences is not None:
if np is not None and type(self.vectorOfCoOwningReferences) is np.ndarray:
vectorOfCoOwningReferences = builder.CreateNumpyVector(self.vectorOfCoOwningReferences)
MonsterStartVectorOfCoOwningReferencesVector(builder, len(self.vectorOfCoOwningReferences))
for i in reversed(range(len(self.vectorOfCoOwningReferences))):
builder.PrependUint64(self.vectorOfCoOwningReferences[i])
- vectorOfCoOwningReferences = builder.EndVector(len(self.vectorOfCoOwningReferences))
+ vectorOfCoOwningReferences = builder.EndVector()
if self.vectorOfNonOwningReferences is not None:
if np is not None and type(self.vectorOfNonOwningReferences) is np.ndarray:
vectorOfNonOwningReferences = builder.CreateNumpyVector(self.vectorOfNonOwningReferences)
MonsterStartVectorOfNonOwningReferencesVector(builder, len(self.vectorOfNonOwningReferences))
for i in reversed(range(len(self.vectorOfNonOwningReferences))):
builder.PrependUint64(self.vectorOfNonOwningReferences[i])
- vectorOfNonOwningReferences = builder.EndVector(len(self.vectorOfNonOwningReferences))
+ vectorOfNonOwningReferences = builder.EndVector()
if self.anyUnique is not None:
anyUnique = self.anyUnique.Pack(builder)
if self.anyAmbiguous is not None:
MonsterStartVectorOfEnumsVector(builder, len(self.vectorOfEnums))
for i in reversed(range(len(self.vectorOfEnums))):
builder.PrependUint8(self.vectorOfEnums[i])
- vectorOfEnums = builder.EndVector(len(self.vectorOfEnums))
+ vectorOfEnums = builder.EndVector()
if self.testrequirednestedflatbuffer is not None:
if np is not None and type(self.testrequirednestedflatbuffer) is np.ndarray:
testrequirednestedflatbuffer = builder.CreateNumpyVector(self.testrequirednestedflatbuffer)
MonsterStartTestrequirednestedflatbufferVector(builder, len(self.testrequirednestedflatbuffer))
for i in reversed(range(len(self.testrequirednestedflatbuffer))):
builder.PrependUint8(self.testrequirednestedflatbuffer[i])
- testrequirednestedflatbuffer = builder.EndVector(len(self.testrequirednestedflatbuffer))
+ testrequirednestedflatbuffer = builder.EndVector()
MonsterStart(builder)
if self.pos is not None:
pos = self.pos.Pack(builder)
TypeAliasesStartV8Vector(builder, len(self.v8))
for i in reversed(range(len(self.v8))):
builder.PrependByte(self.v8[i])
- v8 = builder.EndVector(len(self.v8))
+ v8 = builder.EndVector()
if self.vf64 is not None:
if np is not None and type(self.vf64) is np.ndarray:
vf64 = builder.CreateNumpyVector(self.vf64)
TypeAliasesStartVf64Vector(builder, len(self.vf64))
for i in reversed(range(len(self.vf64))):
builder.PrependFloat64(self.vf64[i])
- vf64 = builder.EndVector(len(self.vf64))
+ vf64 = builder.EndVector()
TypeAliasesStart(builder)
TypeAliasesAddI8(builder, self.i8)
TypeAliasesAddU8(builder, self.u8)
MonsterExtraStartDvecVector(builder, len(self.dvec))
for i in reversed(range(len(self.dvec))):
builder.PrependFloat64(self.dvec[i])
- dvec = builder.EndVector(len(self.dvec))
+ dvec = builder.EndVector()
if self.fvec is not None:
if np is not None and type(self.fvec) is np.ndarray:
fvec = builder.CreateNumpyVector(self.fvec)
MonsterExtraStartFvecVector(builder, len(self.fvec))
for i in reversed(range(len(self.fvec))):
builder.PrependFloat32(self.fvec[i])
- fvec = builder.EndVector(len(self.fvec))
+ fvec = builder.EndVector()
MonsterExtraStart(builder)
MonsterExtraAddD0(builder, self.d0)
MonsterExtraAddD1(builder, self.d1)
self.assertBuilderEquals(b, [0, 0, 0]) # align to 4bytes
b.PrependByte(1)
self.assertBuilderEquals(b, [1, 0, 0, 0])
- b.EndVector(1)
+ b.EndVector()
self.assertBuilderEquals(b, [1, 0, 0, 0, 1, 0, 0, 0]) # padding
def test_2xbyte_vector(self):
self.assertBuilderEquals(b, [1, 0, 0])
b.PrependByte(2)
self.assertBuilderEquals(b, [2, 1, 0, 0])
- b.EndVector(2)
+ b.EndVector()
self.assertBuilderEquals(b, [2, 0, 0, 0, 2, 1, 0, 0]) # padding
def test_1xuint16_vector(self):
self.assertBuilderEquals(b, [0, 0]) # align to 4bytes
b.PrependUint16(1)
self.assertBuilderEquals(b, [1, 0, 0, 0])
- b.EndVector(1)
+ b.EndVector()
self.assertBuilderEquals(b, [1, 0, 0, 0, 1, 0, 0, 0]) # padding
def test_2xuint16_vector(self):
self.assertBuilderEquals(b, [0xCD, 0xAB])
b.PrependUint16(0xDCBA)
self.assertBuilderEquals(b, [0xBA, 0xDC, 0xCD, 0xAB])
- b.EndVector(2)
+ b.EndVector()
self.assertBuilderEquals(b, [2, 0, 0, 0, 0xBA, 0xDC, 0xCD, 0xAB])
def test_create_ascii_string(self):
def test_vtable_with_empty_vector(self):
b = flatbuffers.Builder(0)
b.StartVector(flatbuffers.number_types.Uint8Flags.bytewidth, 0, 1)
- vecend = b.EndVector(0)
+ vecend = b.EndVector()
b.StartObject(1)
b.PrependUOffsetTRelativeSlot(0, vecend, 0)
b.EndObject()
def test_vtable_with_empty_vector_of_byte_and_some_scalars(self):
b = flatbuffers.Builder(0)
b.StartVector(flatbuffers.number_types.Uint8Flags.bytewidth, 0, 1)
- vecend = b.EndVector(0)
+ vecend = b.EndVector()
b.StartObject(2)
b.PrependInt16Slot(0, 55, 0)
b.PrependUOffsetTRelativeSlot(1, vecend, 0)
b.StartVector(flatbuffers.number_types.Int16Flags.bytewidth, 2, 1)
b.PrependInt16(0x1234)
b.PrependInt16(0x5678)
- vecend = b.EndVector(2)
+ vecend = b.EndVector()
b.StartObject(2)
b.PrependUOffsetTRelativeSlot(1, vecend, 0)
b.PrependInt16Slot(0, 55, 0)
b.PrependInt8(44)
b.PrependInt8(55)
b.PrependInt8(66)
- vecend = b.EndVector(2)
+ vecend = b.EndVector()
b.StartObject(1)
b.PrependUOffsetTRelativeSlot(0, vecend, 0)
b.EndObject()
b.PrependByte(2)
b.PrependByte(1)
b.PrependByte(0)
- inv = b.EndVector(5)
+ inv = b.EndVector()
MyGame.Example.Monster.MonsterStart(b)
MyGame.Example.Monster.MonsterAddName(b, fred)
MyGame.Example.Monster.MonsterStartTest4Vector(b, 2)
MyGame.Example.Test.CreateTest(b, 10, 20)
MyGame.Example.Test.CreateTest(b, 30, 40)
- test4 = b.EndVector(2)
+ test4 = b.EndVector()
MyGame.Example.Monster.MonsterStartTestarrayofstringVector(b, 2)
b.PrependUOffsetTRelative(test2)
b.PrependUOffsetTRelative(test1)
- testArrayOfString = b.EndVector(2)
+ testArrayOfString = b.EndVector()
MyGame.Example.Monster.MonsterStartVectorOfLongsVector(b, 5)
b.PrependInt64(100000000)
b.PrependInt64(10000)
b.PrependInt64(100)
b.PrependInt64(1)
- VectorOfLongs = b.EndVector(5)
+ VectorOfLongs = b.EndVector()
MyGame.Example.Monster.MonsterStartVectorOfDoublesVector(b, 3)
b.PrependFloat64(1.7976931348623157e+308)
b.PrependFloat64(0)
b.PrependFloat64(-1.7976931348623157e+308)
- VectorOfDoubles = b.EndVector(3)
+ VectorOfDoubles = b.EndVector()
MyGame.Example.Monster.MonsterStart(b)
def test_empty_monster_inventory_vector(self):
b = flatbuffers.Builder(0)
MyGame.Example.Monster.MonsterStartInventoryVector(b, 0)
- inv = b.EndVector(0)
+ inv = b.EndVector()
MyGame.Example.Monster.MonsterStart(b)
MyGame.Example.Monster.MonsterAddInventory(b, inv)
mon = MyGame.Example.Monster.MonsterEnd(b)
def test_empty_monster_test4_vector(self):
b = flatbuffers.Builder(0)
MyGame.Example.Monster.MonsterStartTest4Vector(b, 0)
- test4 = b.EndVector(0)
+ test4 = b.EndVector()
MyGame.Example.Monster.MonsterStart(b)
MyGame.Example.Monster.MonsterAddTest4(b, test4)
mon = MyGame.Example.Monster.MonsterEnd(b)
def test_empty_monster_testarrayofstring_vector(self):
b = flatbuffers.Builder(0)
MyGame.Example.Monster.MonsterStartTestarrayofstringVector(b, 0)
- testarrayofstring = b.EndVector(0)
+ testarrayofstring = b.EndVector()
MyGame.Example.Monster.MonsterStart(b)
MyGame.Example.Monster.MonsterAddTestarrayofstring(b, testarrayofstring)
mon = MyGame.Example.Monster.MonsterEnd(b)
# build the vector:
MyGame.Example.Monster.MonsterStartTestarrayoftablesVector(b, 1)
b.PrependUOffsetTRelative(sub_monster)
- vec = b.EndVector(1)
+ vec = b.EndVector()
# make the parent monster and include the vector of Monster:
MyGame.Example.Monster.MonsterStart(b)
def test_empty_monster_testarrayoftables_vector(self):
b = flatbuffers.Builder(0)
MyGame.Example.Monster.MonsterStartTestarrayoftablesVector(b, 0)
- testarrayoftables = b.EndVector(0)
+ testarrayoftables = b.EndVector()
MyGame.Example.Monster.MonsterStart(b)
MyGame.Example.Monster.MonsterAddTestarrayoftables(b, testarrayoftables)
mon = MyGame.Example.Monster.MonsterEnd(b)
def test_empty_monster_testnestedflatbuffer_vector(self):
b = flatbuffers.Builder(0)
MyGame.Example.Monster.MonsterStartTestnestedflatbufferVector(b, 0)
- testnestedflatbuffer = b.EndVector(0)
+ testnestedflatbuffer = b.EndVector()
MyGame.Example.Monster.MonsterStart(b)
MyGame.Example.Monster.MonsterAddTestnestedflatbuffer(b, testnestedflatbuffer)
mon = MyGame.Example.Monster.MonsterEnd(b)
b.PrependByte(4)
b.PrependByte(2)
b.PrependByte(0)
- sub_buf = b.EndVector(3)
+ sub_buf = b.EndVector()
# make the parent monster and include the vector of Monster:
MyGame.Example.Monster.MonsterStart(b)