[Swift] Rebuild the way swift handles structs from scratch (#6326)

* Rebuild the way swift handles structs from scratch

* Updates docs, and sample binary

* Replaces InMemory to Mutable

* Migrates docs from inmemory

* use inline for some functions

* Renamed Mutable objects

* Updates documentation
This commit is contained in:
mustiikhalil
2020-12-18 01:55:32 +03:00
committed by GitHub
parent 05192553f4
commit 4e79d129cb
21 changed files with 1051 additions and 720 deletions

View File

@@ -544,10 +544,10 @@ The first step is to import/include the library, generated files, etc.
import Flatbuffers
// typealiases for convenience
typealias Monster = MyGame1.Sample.Monster
typealias Weapon = MyGame1.Sample.Weapon
typealias Color = MyGame1.Sample.Color
typealias Vec3 = MyGame1.Sample.Vec3
typealias Monster = MyGame1_Sample_Monster
typealias Weapon = MyGame1_Sample_Weapon
typealias Color = MyGame1_Sample_Color
typealias Vec3 = MyGame1_Sample_Vec3
~~~
</div>
@@ -1420,9 +1420,20 @@ for the `path` field above:
<div class="language-swift">
~~~{.swift}
//
Monster.startVectorOfvec3(2, in: &fbb)
MyGame_Example_Vec3.createVec3(builder: &fbb, x: 1, y: 2, z: 3)
MyGame_Example_Vec3.createVec3(builder: &fbb, x: 4, y: 5, z: 6)
let points = fbb.createVector(ofStructs: [
Vec3(x: 1, y: 2, z: 3),
Vec3(x: 4, y: 5, z: 6)
])
// OR
var vec3 = [
Vec3(x: 1, y: 2, z: 3),
Vec3(x: 4, y: 5, z: 6)
]
Monster.startVectorOfVec3(2, in: &fbb)
for i in obj {
_ = create(struct: i)
}
let points = fbb.endVectorOfStructs(count: size)
~~~
</div>
@@ -1702,17 +1713,16 @@ can serialize the monster itself:
</div>
<div class="language-swift">
~~~{.swift}
let start = Monster.startMonster(&builder)
let posStruct = MyGame_Example_Vec3.createVec3(builder: &builder, x: 1, y: 2, z: 3)
Monster.add(pos: pos, &builder)
Monster.add(hp: 300, &builder)
Monster.add(name: name, &builder)
Monster.addVectorOf(inventory: inventoryOffset, &builder)
Monster.add(color: .red, &builder)
Monster.addVectorOf(weapons: weaponsOffset, &builder)
Monster.add(equippedType: .weapon, &builder)
Monster.add(equipped: axe, &builder)
var orc = Monster.endMonster(&builder, start: start)
let orc = Monster.createMonster(
fbb: &builder,
pos: Vec3(x: 1, y: 2, z: 3),
hp: 300,
name: name,
inventory: inventoryOffset,
color: .red,
weapons: weaponsOffset,
equippedType: .weapon,
equipped: axe)
~~~
</div>
@@ -1780,6 +1790,21 @@ a bit more flexibility.
~~~
</div>
<div class="language-swift">
~~~{.swift}
let start = Monster.startMonster(&builder)
Monster.add(pos: Vec3(x: 1, y: 2, z: 3), &builder)
Monster.add(hp: 300, &builder)
Monster.add(name: name, &builder)
Monster.addVectorOf(inventory: inventoryOffset, &builder)
Monster.add(color: .red, &builder)
Monster.addVectorOf(weapons: weaponsOffset, &builder)
Monster.add(equippedType: .weapon, &builder)
Monster.add(equipped: axe, &builder)
var orc = Monster.endMonster(&builder, start: start)
~~~
</div>
Before finishing the serialization, let's take a quick look at FlatBuffer
`union Equipped`. There are two parts to each FlatBuffer `union`. The first is
a hidden field `_type` that is generated to hold the type of `table` referred
@@ -3239,7 +3264,8 @@ mutators like so:
~~~{.swift}
let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf))
monster.mutate(hp: 10) // mutates a value in a table
monster.pos.mutate(z: 4) // mutates a value in a struct
/// to mutate structs in swift you have to use the mutable accessors
monster.mutablePos.mutate(z: 4) // mutates a value in a struct
monster.mutate(inventory: 6, at index: 0) // mutates a value in an Scalar array
~~~
</div>