[Swift] Adds new API to reduce memory copying within swift (#8484)

* Adds new API to reduce memory copying within swift

Adds new storage container _InternalByteBuffer which
will be holding the data that will be created within the swift
lib, however reading data will be redirected to ByteBuffer, which
should be able to handle all types of data that swift provide without
the need to copy the data itself. This is due to holding a reference to
the data.

Replaces assumingMemoryBinding with bindMemory which is safer

Adds function that provides access to a UnsafeBufferPointer for
scalars and NativeStructs within swift

Updates docs

Suppress compilation warnings by replacing var with let

Using overflow operators within swift to improve performance

Adds tests for GRPC message creation from a retained _InternalByteBuffer
This commit is contained in:
mustiikhalil
2025-03-18 07:48:39 +01:00
committed by GitHub
parent 1c514626e8
commit bd1b2d0baf
29 changed files with 1470 additions and 463 deletions

View File

@@ -15,8 +15,8 @@
*/ */
import Benchmark import Benchmark
import CoreFoundation
import FlatBuffers import FlatBuffers
import Foundation
@usableFromInline @usableFromInline
struct AA: NativeStruct { struct AA: NativeStruct {
@@ -29,6 +29,15 @@ struct AA: NativeStruct {
} }
let benchmarks = { let benchmarks = {
let oneGB: Int32 = 1_024_000_000
let data = {
var array = [8888.88, 8888.88]
var data = Data()
array.withUnsafeBytes { ptr in
data.append(contentsOf: ptr)
}
return data
}()
let ints: [Int] = Array(repeating: 42, count: 100) let ints: [Int] = Array(repeating: 42, count: 100)
let bytes: [UInt8] = Array(repeating: 42, count: 100) let bytes: [UInt8] = Array(repeating: 42, count: 100)
let str10 = (0...9).map { _ -> String in "x" }.joined() let str10 = (0...9).map { _ -> String in "x" }.joined()
@@ -73,12 +82,25 @@ let benchmarks = {
Benchmark("Allocating 1GB", configuration: singleConfiguration) { benchmark in Benchmark("Allocating 1GB", configuration: singleConfiguration) { benchmark in
for _ in benchmark.scaledIterations { for _ in benchmark.scaledIterations {
blackHole(FlatBufferBuilder(initialSize: 1_024_000_000)) blackHole(FlatBufferBuilder(initialSize: oneGB))
}
}
Benchmark(
"Allocating ByteBuffer 1GB",
configuration: singleConfiguration)
{ benchmark in
let memory = UnsafeMutableRawPointer.allocate(
byteCount: 1_024_000_000,
alignment: 1)
benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
blackHole(ByteBuffer(assumingMemoryBound: memory, capacity: Int(oneGB)))
} }
} }
Benchmark("Clearing 1GB", configuration: singleConfiguration) { benchmark in Benchmark("Clearing 1GB", configuration: singleConfiguration) { benchmark in
var fb = FlatBufferBuilder(initialSize: 1_024_000_000) var fb = FlatBufferBuilder(initialSize: oneGB)
benchmark.startMeasurement() benchmark.startMeasurement()
for _ in benchmark.scaledIterations { for _ in benchmark.scaledIterations {
blackHole(fb.clear()) blackHole(fb.clear())
@@ -158,6 +180,26 @@ let benchmarks = {
} }
} }
Benchmark(
"FlatBufferBuilder Start table",
configuration: kiloConfiguration)
{ benchmark in
var fb = FlatBufferBuilder(initialSize: 1024 * 1024 * 32)
benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
let s = fb.startTable(with: 4)
blackHole(fb.endTable(at: s))
}
}
Benchmark("Struct") { benchmark in
var fb = FlatBufferBuilder(initialSize: 1024 * 1024 * 32)
benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
blackHole(fb.create(struct: array.first!))
}
}
Benchmark("Structs") { benchmark in Benchmark("Structs") { benchmark in
let rawSize = ((16 * 5) * benchmark.scaledIterations.count) / 1024 let rawSize = ((16 * 5) * benchmark.scaledIterations.count) / 1024
var fb = FlatBufferBuilder(initialSize: Int32(rawSize * 1600)) var fb = FlatBufferBuilder(initialSize: Int32(rawSize * 1600))
@@ -176,7 +218,7 @@ let benchmarks = {
let start = fb.startTable(with: 1) let start = fb.startTable(with: 1)
fb.add(offset: vector, at: 4) fb.add(offset: vector, at: 4)
let root = Offset(offset: fb.endTable(at: start)) let root = Offset(offset: fb.endTable(at: start))
fb.finish(offset: root) blackHole(fb.finish(offset: root))
} }
Benchmark("Vector of Offsets") { benchmark in Benchmark("Vector of Offsets") { benchmark in
@@ -198,4 +240,11 @@ let benchmarks = {
blackHole(fb.endTable(at: s)) blackHole(fb.endTable(at: s))
} }
} }
Benchmark("Reading Doubles") { benchmark in
let byteBuffer = ByteBuffer(data: data)
for _ in benchmark.scaledIterations {
blackHole(byteBuffer.read(def: Double.self, position: 0))
}
}
} }

View File

@@ -57,7 +57,7 @@ a data object from the server, which you can pass into the `GetRootAsMonster` fu
let url = URL(fileURLWithPath: path, isDirectory: true).appendingPathComponent("monsterdata_test").appendingPathExtension("mon") let url = URL(fileURLWithPath: path, isDirectory: true).appendingPathComponent("monsterdata_test").appendingPathExtension("mon")
guard let data = try? Data(contentsOf: url) else { return } guard let data = try? Data(contentsOf: url) else { return }
let monster = Monster.getRootAsMonster(bb: ByteBuffer(data: data)) let monster: Monster = try! getCheckedRoot(byteBuffer: &byteBuffer)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Now you can access values like this: Now you can access values like this:

View File

@@ -2011,6 +2011,8 @@ like so:
let buf = builder.sizedByteArray let buf = builder.sizedByteArray
// or you can use to get an object of type Data // or you can use to get an object of type Data
let bufData = ByteBuffer(data: builder.data) let bufData = ByteBuffer(data: builder.data)
// or
let buf = builder.sizedBuffer
``` ```
=== "TypeScript" === "TypeScript"

View File

@@ -17,8 +17,7 @@ public extension GRPCFlatBufPayload {
self.init(byteBuffer: FlatBuffers.ByteBuffer(contiguousBytes: serializedByteBuffer.readableBytesView, count: serializedByteBuffer.readableBytes)) self.init(byteBuffer: FlatBuffers.ByteBuffer(contiguousBytes: serializedByteBuffer.readableBytesView, count: serializedByteBuffer.readableBytes))
} }
func serialize(into buffer: inout NIO.ByteBuffer) throws { func serialize(into buffer: inout NIO.ByteBuffer) throws {
let buf = UnsafeRawBufferPointer(start: self.rawPointer, count: Int(self.size)) withUnsafeReadableBytes { buffer.writeBytes($0) }
buffer.writeBytes(buf)
} }
} }
extension Message: GRPCFlatBufPayload {} extension Message: GRPCFlatBufPayload {}

View File

@@ -428,10 +428,7 @@ grpc::string GenerateHeader() {
code += " }\n"; code += " }\n";
code += " func serialize(into buffer: inout NIO.ByteBuffer) throws {\n"; code += " func serialize(into buffer: inout NIO.ByteBuffer) throws {\n";
code += code += " withUnsafeReadableBytes { buffer.writeBytes($0) }\n";
" let buf = UnsafeRawBufferPointer(start: self.rawPointer, count: "
"Int(self.size))\n";
code += " buffer.writeBytes(buf)\n";
code += " }\n"; code += " }\n";
code += "}\n"; code += "}\n";
code += "extension Message: GRPCFlatBufPayload {}\n"; code += "extension Message: GRPCFlatBufPayload {}\n";

View File

@@ -16,6 +16,16 @@ public enum MyGame_Sample_Color: Int8, Enum, Verifiable {
public static var min: MyGame_Sample_Color { return .red } public static var min: MyGame_Sample_Color { return .red }
} }
extension MyGame_Sample_Color: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .red: try container.encode("Red")
case .green: try container.encode("Green")
case .blue: try container.encode("Blue")
}
}
}
public enum MyGame_Sample_Equipment: UInt8, UnionEnum { public enum MyGame_Sample_Equipment: UInt8, UnionEnum {
public typealias T = UInt8 public typealias T = UInt8
@@ -33,8 +43,33 @@ public enum MyGame_Sample_Equipment: UInt8, UnionEnum {
public static var min: MyGame_Sample_Equipment { return .none_ } public static var min: MyGame_Sample_Equipment { return .none_ }
} }
extension MyGame_Sample_Equipment: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .none_: try container.encode("NONE")
case .weapon: try container.encode("Weapon")
}
}
}
public struct MyGame_Sample_Vec3: NativeStruct, Verifiable, FlatbuffersInitializable { public struct MyGame_Sample_EquipmentUnion {
public var type: MyGame_Sample_Equipment
public var value: NativeObject?
public init(_ v: NativeObject?, type: MyGame_Sample_Equipment) {
self.type = type
self.value = v
}
public func pack(builder: inout FlatBufferBuilder) -> Offset {
switch type {
case .weapon:
var __obj = value as? MyGame_Sample_WeaponT
return MyGame_Sample_Weapon.pack(&builder, obj: &__obj)
default: return Offset()
}
}
}
public struct MyGame_Sample_Vec3: NativeStruct, Verifiable, FlatbuffersInitializable, NativeObject {
static func validateVersion() { FlatBuffersVersion_25_2_10() } static func validateVersion() { FlatBuffersVersion_25_2_10() }
@@ -61,6 +96,12 @@ public struct MyGame_Sample_Vec3: NativeStruct, Verifiable, FlatbuffersInitializ
_z = 0.0 _z = 0.0
} }
public init(_ _t: inout MyGame_Sample_Vec3_Mutable) {
_x = _t.x
_y = _t.y
_z = _t.z
}
public var x: Float32 { _x } public var x: Float32 { _x }
public var y: Float32 { _y } public var y: Float32 { _y }
public var z: Float32 { _z } public var z: Float32 { _z }
@@ -70,6 +111,27 @@ public struct MyGame_Sample_Vec3: NativeStruct, Verifiable, FlatbuffersInitializ
} }
} }
extension MyGame_Sample_Vec3: Encodable {
enum CodingKeys: String, CodingKey {
case x = "x"
case y = "y"
case z = "z"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if x != 0.0 {
try container.encodeIfPresent(x, forKey: .x)
}
if y != 0.0 {
try container.encodeIfPresent(y, forKey: .y)
}
if z != 0.0 {
try container.encodeIfPresent(z, forKey: .z)
}
}
}
public struct MyGame_Sample_Vec3_Mutable: FlatBufferObject { public struct MyGame_Sample_Vec3_Mutable: FlatBufferObject {
static func validateVersion() { FlatBuffersVersion_25_2_10() } static func validateVersion() { FlatBuffersVersion_25_2_10() }
@@ -84,9 +146,22 @@ public struct MyGame_Sample_Vec3_Mutable: FlatBufferObject {
@discardableResult public func mutate(y: Float32) -> Bool { return _accessor.mutate(y, index: 4) } @discardableResult public func mutate(y: Float32) -> Bool { return _accessor.mutate(y, index: 4) }
public var z: Float32 { return _accessor.readBuffer(of: Float32.self, at: 8) } public var z: Float32 { return _accessor.readBuffer(of: Float32.self, at: 8) }
@discardableResult public func mutate(z: Float32) -> Bool { return _accessor.mutate(z, index: 8) } @discardableResult public func mutate(z: Float32) -> Bool { return _accessor.mutate(z, index: 8) }
public mutating func unpack() -> MyGame_Sample_Vec3 {
return MyGame_Sample_Vec3(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Sample_Vec3?) -> Offset {
guard var obj = obj else { return Offset() }
return pack(&builder, obj: &obj)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Sample_Vec3) -> Offset {
return builder.create(struct: obj)
}
} }
public struct MyGame_Sample_Monster: FlatBufferObject, Verifiable { public struct MyGame_Sample_Monster: FlatBufferObject, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_25_2_10() } static func validateVersion() { FlatBuffersVersion_25_2_10() }
public var __buffer: ByteBuffer! { return _accessor.bb } public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -123,6 +198,7 @@ public struct MyGame_Sample_Monster: FlatBufferObject, Verifiable {
public func inventory(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) } public func inventory(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) }
public var inventory: [UInt8] { return _accessor.getVector(at: VTOFFSET.inventory.v) ?? [] } public var inventory: [UInt8] { return _accessor.getVector(at: VTOFFSET.inventory.v) ?? [] }
public func mutate(inventory: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.inventory.v); return _accessor.directMutate(inventory, index: _accessor.vector(at: o) + index * 1) } public func mutate(inventory: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.inventory.v); return _accessor.directMutate(inventory, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToInventory<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.inventory.v, body: body) }
public var color: MyGame_Sample_Color { let o = _accessor.offset(VTOFFSET.color.v); return o == 0 ? .blue : MyGame_Sample_Color(rawValue: _accessor.readBuffer(of: Int8.self, at: o)) ?? .blue } public var color: MyGame_Sample_Color { let o = _accessor.offset(VTOFFSET.color.v); return o == 0 ? .blue : MyGame_Sample_Color(rawValue: _accessor.readBuffer(of: Int8.self, at: o)) ?? .blue }
@discardableResult public func mutate(color: MyGame_Sample_Color) -> Bool {let o = _accessor.offset(VTOFFSET.color.v); return _accessor.mutate(color.rawValue, index: o) } @discardableResult public func mutate(color: MyGame_Sample_Color) -> Bool {let o = _accessor.offset(VTOFFSET.color.v); return _accessor.mutate(color.rawValue, index: o) }
public var hasWeapons: Bool { let o = _accessor.offset(VTOFFSET.weapons.v); return o == 0 ? false : true } public var hasWeapons: Bool { let o = _accessor.offset(VTOFFSET.weapons.v); return o == 0 ? false : true }
@@ -134,6 +210,7 @@ public struct MyGame_Sample_Monster: FlatBufferObject, Verifiable {
public var pathCount: Int32 { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var pathCount: Int32 { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func path(at index: Int32) -> MyGame_Sample_Vec3? { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Sample_Vec3.self, offset: _accessor.vector(at: o) + index * 12) } public func path(at index: Int32) -> MyGame_Sample_Vec3? { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Sample_Vec3.self, offset: _accessor.vector(at: o) + index * 12) }
public func mutablePath(at index: Int32) -> MyGame_Sample_Vec3_Mutable? { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? nil : MyGame_Sample_Vec3_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 12) } public func mutablePath(at index: Int32) -> MyGame_Sample_Vec3_Mutable? { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? nil : MyGame_Sample_Vec3_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 12) }
public func withUnsafePointerToPath<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.path.v, body: body) }
public static func startMonster(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 11) } public static func startMonster(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 11) }
public static func add(pos: MyGame_Sample_Vec3?, _ fbb: inout FlatBufferBuilder) { guard let pos = pos else { return }; fbb.create(struct: pos, position: VTOFFSET.pos.p) } public static func add(pos: MyGame_Sample_Vec3?, _ fbb: inout FlatBufferBuilder) { guard let pos = pos else { return }; fbb.create(struct: pos, position: VTOFFSET.pos.p) }
public static func add(mana: Int16, _ fbb: inout FlatBufferBuilder) { fbb.add(element: mana, def: 150, at: VTOFFSET.mana.p) } public static func add(mana: Int16, _ fbb: inout FlatBufferBuilder) { fbb.add(element: mana, def: 150, at: VTOFFSET.mana.p) }
@@ -176,6 +253,53 @@ public struct MyGame_Sample_Monster: FlatBufferObject, Verifiable {
return MyGame_Sample_Monster.endMonster(&fbb, start: __start) return MyGame_Sample_Monster.endMonster(&fbb, start: __start)
} }
public mutating func unpack() -> MyGame_Sample_MonsterT {
return MyGame_Sample_MonsterT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Sample_MonsterT?) -> Offset {
guard var obj = obj else { return Offset() }
return pack(&builder, obj: &obj)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Sample_MonsterT) -> Offset {
let __name: Offset
if let s = obj.name {
__name = builder.create(string: s)
} else {
__name = Offset()
}
let __inventory = builder.createVector(obj.inventory)
var __weapons__: [Offset] = []
for var i in obj.weapons {
__weapons__.append(MyGame_Sample_Weapon.pack(&builder, obj: &i))
}
let __weapons = builder.createVector(ofOffsets: __weapons__)
let __equipped = obj.equipped?.pack(builder: &builder) ?? Offset()
MyGame_Sample_Monster.startVectorOfPath(obj.path.count, in: &builder)
for i in obj.path {
guard let _o = i else { continue }
builder.create(struct: _o)
}
let __path = builder.endVector(len: obj.path.count)
let __root = MyGame_Sample_Monster.startMonster(&builder)
MyGame_Sample_Monster.add(pos: obj.pos, &builder)
MyGame_Sample_Monster.add(mana: obj.mana, &builder)
MyGame_Sample_Monster.add(hp: obj.hp, &builder)
MyGame_Sample_Monster.add(name: __name, &builder)
MyGame_Sample_Monster.addVectorOf(inventory: __inventory, &builder)
MyGame_Sample_Monster.add(color: obj.color, &builder)
MyGame_Sample_Monster.addVectorOf(weapons: __weapons, &builder)
if let o = obj.equipped?.type {
MyGame_Sample_Monster.add(equippedType: o, &builder)
MyGame_Sample_Monster.add(equipped: __equipped, &builder)
}
MyGame_Sample_Monster.addVectorOf(path: __path, &builder)
return MyGame_Sample_Monster.endMonster(&builder, start: __root)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable { public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position) var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.pos.p, fieldName: "pos", required: false, type: MyGame_Sample_Vec3.self) try _v.visit(field: VTOFFSET.pos.p, fieldName: "pos", required: false, type: MyGame_Sample_Vec3.self)
@@ -198,7 +322,115 @@ public struct MyGame_Sample_Monster: FlatBufferObject, Verifiable {
} }
} }
public struct MyGame_Sample_Weapon: FlatBufferObject, Verifiable { extension MyGame_Sample_Monster: Encodable {
enum CodingKeys: String, CodingKey {
case pos = "pos"
case mana = "mana"
case hp = "hp"
case name = "name"
case inventory = "inventory"
case color = "color"
case weapons = "weapons"
case equippedType = "equipped_type"
case equipped = "equipped"
case path = "path"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(pos, forKey: .pos)
if mana != 150 {
try container.encodeIfPresent(mana, forKey: .mana)
}
if hp != 100 {
try container.encodeIfPresent(hp, forKey: .hp)
}
try container.encodeIfPresent(name, forKey: .name)
if inventoryCount > 0 {
try container.encodeIfPresent(inventory, forKey: .inventory)
}
if color != .blue {
try container.encodeIfPresent(color, forKey: .color)
}
if weaponsCount > 0 {
var contentEncoder = container.nestedUnkeyedContainer(forKey: .weapons)
for index in 0..<weaponsCount {
guard let type = weapons(at: index) else { continue }
try contentEncoder.encode(type)
}
}
if equippedType != .none_ {
try container.encodeIfPresent(equippedType, forKey: .equippedType)
}
switch equippedType {
case .weapon:
let _v = equipped(type: MyGame_Sample_Weapon.self)
try container.encodeIfPresent(_v, forKey: .equipped)
default: break;
}
if pathCount > 0 {
var contentEncoder = container.nestedUnkeyedContainer(forKey: .path)
for index in 0..<pathCount {
guard let type = path(at: index) else { continue }
try contentEncoder.encode(type)
}
}
}
}
public class MyGame_Sample_MonsterT: NativeObject {
public var pos: MyGame_Sample_Vec3?
public var mana: Int16
public var hp: Int16
public var name: String?
public var inventory: [UInt8]
public var color: MyGame_Sample_Color
public var weapons: [MyGame_Sample_WeaponT?]
public var equipped: MyGame_Sample_EquipmentUnion?
public var path: [MyGame_Sample_Vec3?]
public init(_ _t: inout MyGame_Sample_Monster) {
pos = _t.pos
mana = _t.mana
hp = _t.hp
name = _t.name
inventory = []
for index in 0..<_t.inventoryCount {
inventory.append(_t.inventory(at: index))
}
color = _t.color
weapons = []
for index in 0..<_t.weaponsCount {
var __v_ = _t.weapons(at: index)
weapons.append(__v_?.unpack())
}
switch _t.equippedType {
case .weapon:
var _v = _t.equipped(type: MyGame_Sample_Weapon.self)
equipped = MyGame_Sample_EquipmentUnion(_v?.unpack(), type: .weapon)
default: break
}
path = []
for index in 0..<_t.pathCount {
path.append(_t.path(at: index))
}
}
public init() {
pos = MyGame_Sample_Vec3()
mana = 150
hp = 100
inventory = []
color = .blue
weapons = []
path = []
}
public func serialize() -> ByteBuffer { return serialize(type: MyGame_Sample_Monster.self) }
}
public struct MyGame_Sample_Weapon: FlatBufferObject, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_25_2_10() } static func validateVersion() { FlatBuffersVersion_25_2_10() }
public var __buffer: ByteBuffer! { return _accessor.bb } public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -233,6 +465,29 @@ public struct MyGame_Sample_Weapon: FlatBufferObject, Verifiable {
return MyGame_Sample_Weapon.endWeapon(&fbb, start: __start) return MyGame_Sample_Weapon.endWeapon(&fbb, start: __start)
} }
public mutating func unpack() -> MyGame_Sample_WeaponT {
return MyGame_Sample_WeaponT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Sample_WeaponT?) -> Offset {
guard var obj = obj else { return Offset() }
return pack(&builder, obj: &obj)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MyGame_Sample_WeaponT) -> Offset {
let __name: Offset
if let s = obj.name {
__name = builder.create(string: s)
} else {
__name = Offset()
}
let __root = MyGame_Sample_Weapon.startWeapon(&builder)
MyGame_Sample_Weapon.add(name: __name, &builder)
MyGame_Sample_Weapon.add(damage: obj.damage, &builder)
return MyGame_Sample_Weapon.endWeapon(&builder, start: __root)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable { public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position) var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.name.p, fieldName: "name", required: false, type: ForwardOffset<String>.self) try _v.visit(field: VTOFFSET.name.p, fieldName: "name", required: false, type: ForwardOffset<String>.self)
@@ -241,3 +496,35 @@ public struct MyGame_Sample_Weapon: FlatBufferObject, Verifiable {
} }
} }
extension MyGame_Sample_Weapon: Encodable {
enum CodingKeys: String, CodingKey {
case name = "name"
case damage = "damage"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(name, forKey: .name)
if damage != 0 {
try container.encodeIfPresent(damage, forKey: .damage)
}
}
}
public class MyGame_Sample_WeaponT: NativeObject {
public var name: String?
public var damage: Int16
public init(_ _t: inout MyGame_Sample_Weapon) {
name = _t.name
damage = _t.damage
}
public init() {
damage = 0
}
public func serialize() -> ByteBuffer { return serialize(type: MyGame_Sample_Weapon.self) }
}

View File

@@ -511,7 +511,7 @@ flatc(
# Sample files # Sample files
samples_schema = "monster.fbs" samples_schema = "monster.fbs"
flatc(BASE_OPTS + CPP_OPTS + LOBSTER_OPTS, schema=samples_schema, cwd=samples_path) flatc(BASE_OPTS + CPP_OPTS + LOBSTER_OPTS + SWIFT_OPTS, schema=samples_schema, cwd=samples_path)
flatc(RUST_OPTS, prefix="rust_generated", schema=samples_schema, cwd=samples_path) flatc(RUST_OPTS, prefix="rust_generated", schema=samples_schema, cwd=samples_path)
flatc( flatc(
BINARY_OPTS + ["--bfbs-filenames", str(samples_path)], BINARY_OPTS + ["--bfbs-filenames", str(samples_path)],

View File

@@ -569,8 +569,8 @@ class SwiftGenerator : public BaseGenerator {
code_ += code_ +=
spacing + spacing +
"off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: " "off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: "
"{{VOFFSET}}, fbb: fbb.buffer), Table.offset(Int32($0.o), vOffset: " "{{VOFFSET}}, fbb: &fbb), Table.offset(Int32($0.o), vOffset: "
"{{VOFFSET}}, fbb: fbb.buffer), fbb: fbb.buffer) < 0 } "; "{{VOFFSET}}, fbb: &fbb), fbb: &fbb) < 0 } ";
code_ += spacing + "return fbb.createVector(ofOffsets: off)"; code_ += spacing + "return fbb.createVector(ofOffsets: off)";
Outdent(); Outdent();
code_ += "}"; code_ += "}";
@@ -832,6 +832,7 @@ class SwiftGenerator : public BaseGenerator {
"{{ACCESS_TYPE}} var {{FIELDVAR}}: [{{VALUETYPE}}] { return " "{{ACCESS_TYPE}} var {{FIELDVAR}}: [{{VALUETYPE}}] { return "
"{{ACCESS}}.getVector(at: {{TABLEOFFSET}}.{{OFFSET}}.v) ?? [] }"; "{{ACCESS}}.getVector(at: {{TABLEOFFSET}}.{{OFFSET}}.v) ?? [] }";
if (parser_.opts.mutable_buffer) code_ += GenMutateArray(); if (parser_.opts.mutable_buffer) code_ += GenMutateArray();
GenUnsafeBufferPointer(field);
return; return;
} }
@@ -845,6 +846,7 @@ class SwiftGenerator : public BaseGenerator {
code_ += GenArrayMainBody(nullable) + GenOffset() + const_string + code_ += GenArrayMainBody(nullable) + GenOffset() + const_string +
GenConstructor("{{ACCESS}}.vector(at: o) + index * {{SIZE}}"); GenConstructor("{{ACCESS}}.vector(at: o) + index * {{SIZE}}");
GenUnsafeBufferPointer(field);
return; return;
} }
@@ -887,6 +889,16 @@ class SwiftGenerator : public BaseGenerator {
} }
} }
void GenUnsafeBufferPointer(const FieldDef &field) {
code_.SetValue("functionName",
namer_.Variable("withUnsafePointerTo", field));
code_ +=
"{{ACCESS_TYPE}} func {{functionName}}<T>(_ body: "
"(UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try "
"{{ACCESS}}.withUnsafePointerToSlice(at: {{TABLEOFFSET}}.{{OFFSET}}.v, "
"body: body) }";
}
void GenerateCodingKeys(const StructDef &struct_def) { void GenerateCodingKeys(const StructDef &struct_def) {
code_ += "enum CodingKeys: String, CodingKey {"; code_ += "enum CodingKeys: String, CodingKey {";
Indent(); Indent();

View File

@@ -17,11 +17,11 @@
--typeattributes prev-line # wrapAttributes --typeattributes prev-line # wrapAttributes
# rules # rules
--rules wrap,todos,anyObjectProtocol,redundantParens,redundantReturn,redundantSelf,sortImports,strongifiedSelf,trailingCommas,trailingSpace,wrapArguments,wrapMultilineStatementBraces,indent,wrapAttributes,void,fileHeader --rules wrap,todos,anyObjectProtocol,redundantParens,redundantSelf,sortImports,strongifiedSelf,trailingCommas,trailingSpace,wrapArguments,wrapMultilineStatementBraces,indent,wrapAttributes,void,fileHeader
--disable trailingclosures --disable trailingclosures
--exclude **/*_generated.swift --exclude **/*_generated.swift
--exclude **/swift_code_*.swift --exclude **/swift_code_*.swift
--exclude **/*.grpc.swift --exclude **/*.grpc.swift
--header "/*\n * Copyright {year} Google Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */" --header "/*\n * Copyright 2024 Google Inc. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */"

View File

@@ -26,192 +26,240 @@ public struct ByteBuffer {
/// deallocating the memory that was held by (memory: UnsafeMutableRawPointer) /// deallocating the memory that was held by (memory: UnsafeMutableRawPointer)
@usableFromInline @usableFromInline
final class Storage { final class Storage {
// This storage doesn't own the memory, therefore, we won't deallocate on deinit. @usableFromInline
private let unowned: Bool enum Blob {
/// pointer to the start of the buffer object in memory #if !os(WASI)
var memory: UnsafeMutableRawPointer case data(Data)
case bytes(ContiguousBytes)
#endif
case byteBuffer(_InternalByteBuffer)
case array([UInt8])
case pointer(UnsafeMutableRawPointer)
}
/// This storage doesn't own the memory, therefore, we won't deallocate on deinit.
private let isOwned: Bool
/// Retained blob of data that requires the storage to retain a pointer to.
@usableFromInline
var retainedBlob: Blob
/// Capacity of UInt8 the buffer can hold /// Capacity of UInt8 the buffer can hold
var capacity: Int var capacity: Int
@usableFromInline @usableFromInline
init(count: Int, alignment: Int) { init(count: Int) {
memory = UnsafeMutableRawPointer.allocate( let memory = UnsafeMutableRawPointer.allocate(
byteCount: count, byteCount: count,
alignment: alignment) alignment: MemoryLayout<UInt8>.alignment)
capacity = count capacity = count
unowned = false retainedBlob = .pointer(memory)
isOwned = true
} }
@usableFromInline @usableFromInline
init(memory: UnsafeMutableRawPointer, capacity: Int, unowned: Bool) { init(blob: Blob, capacity count: Int) {
self.memory = memory capacity = count
self.capacity = capacity retainedBlob = blob
self.unowned = unowned isOwned = false
} }
deinit { deinit {
if !unowned { guard isOwned else { return }
memory.deallocate() switch retainedBlob {
case .pointer(let unsafeMutableRawPointer):
unsafeMutableRawPointer.deallocate()
default: break
} }
} }
@usableFromInline @usableFromInline
func copy(from ptr: UnsafeRawPointer, count: Int) { func copy(from ptr: UnsafeRawPointer, count: Int) {
assert( assert(
!unowned, isOwned,
"copy should NOT be called on a buffer that is built by assumingMemoryBound") "copy should NOT be called on a buffer that is built by assumingMemoryBound")
memory.copyMemory(from: ptr, byteCount: count) withUnsafeRawPointer {
$0.copyMemory(from: ptr, byteCount: count)
}
} }
@usableFromInline @usableFromInline
func initialize(for size: Int) { func initialize(for size: Int) {
assert( assert(
!unowned, isOwned,
"initalize should NOT be called on a buffer that is built by assumingMemoryBound") "initalize should NOT be called on a buffer that is built by assumingMemoryBound")
memset(memory, 0, size) withUnsafeRawPointer {
memset($0, 0, size)
}
} }
/// Reallocates the buffer incase the object to be written doesnt fit in the current buffer @discardableResult
/// - Parameter size: Size of the current object @inline(__always)
@usableFromInline func withUnsafeBytes<T>(
func reallocate(_ size: Int, writerSize: Int, alignment: Int) { _ body: (UnsafeRawBufferPointer) throws
let currentWritingIndex = capacity &- writerSize -> T) rethrows -> T
while capacity <= writerSize &+ size { {
capacity = capacity << 1 switch retainedBlob {
case .byteBuffer(let byteBuffer):
return try byteBuffer.withUnsafeBytes(body)
#if !os(WASI)
case .data(let data):
return try data.withUnsafeBytes(body)
case .bytes(let contiguousBytes):
return try contiguousBytes.withUnsafeBytes(body)
#endif
case .array(let array):
return try array.withUnsafeBytes(body)
case .pointer(let ptr):
return try body(UnsafeRawBufferPointer(start: ptr, count: capacity))
} }
}
/// solution take from Apple-NIO @discardableResult
capacity = capacity.convertToPowerofTwo @inline(__always)
func withUnsafeRawPointer<T>(
_ body: (UnsafeMutableRawPointer) throws
-> T) rethrows -> T
{
switch retainedBlob {
case .byteBuffer(let byteBuffer):
return try byteBuffer.withUnsafeRawPointer(body)
#if !os(WASI)
case .data(let data):
return try data
.withUnsafeBytes {
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
}
case .bytes(let contiguousBytes):
return try contiguousBytes
.withUnsafeBytes {
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
}
#endif
case .array(let array):
return try array
.withUnsafeBytes {
try body(UnsafeMutableRawPointer(mutating: $0.baseAddress!))
}
case .pointer(let ptr):
return try body(ptr)
}
}
let newData = UnsafeMutableRawPointer.allocate( @discardableResult
byteCount: capacity, @inline(__always)
alignment: alignment) func readWithUnsafeRawPointer<T>(
memset(newData, 0, capacity &- writerSize) position: Int,
memcpy( _ body: (UnsafeRawPointer) throws -> T) rethrows -> T
newData.advanced(by: capacity &- writerSize), {
memory.advanced(by: currentWritingIndex), switch retainedBlob {
writerSize) case .byteBuffer(let byteBuffer):
memory.deallocate() return try byteBuffer.readWithUnsafeRawPointer(position: position, body)
memory = newData #if !os(WASI)
case .data(let data):
return try data.withUnsafeBytes {
try body($0.baseAddress!.advanced(by: position))
}
case .bytes(let contiguousBytes):
return try contiguousBytes.withUnsafeBytes {
try body($0.baseAddress!.advanced(by: position))
}
#endif
case .array(let array):
return try array.withUnsafeBytes {
try body($0.baseAddress!.advanced(by: position))
}
case .pointer(let ptr):
return try body(ptr.advanced(by: position))
}
} }
} }
@usableFromInline var _storage: Storage @usableFromInline var _storage: Storage
/// The size of the elements written to the buffer + their paddings /// The size of the elements written to the buffer + their paddings
private var _writerSize: Int = 0 private var _readerIndex: Int = 0
/// Alignment of the current memory being written to the buffer
var alignment = 1
/// Current Index which is being used to write to the buffer, it is written from the end to the start of the buffer
var writerIndex: Int { _storage.capacity &- _writerSize }
/// Reader is the position of the current Writer Index (capacity - size) /// Reader is the position of the current Writer Index (capacity - size)
public var reader: Int { writerIndex } public var reader: Int { _storage.capacity &- _readerIndex }
/// Current size of the buffer /// Current size of the buffer
public var size: UOffset { UOffset(_writerSize) } public var size: UOffset { UOffset(_readerIndex) }
/// Public Pointer to the buffer object in memory. This should NOT be modified for any reason
public var memory: UnsafeMutableRawPointer { _storage.memory }
/// Current capacity for the buffer /// Current capacity for the buffer
public var capacity: Int { _storage.capacity } public var capacity: Int { _storage.capacity }
/// Crash if the trying to read an unaligned buffer instead of allowing users to read them.
public let allowReadingUnalignedBuffers: Bool /// Constructor that creates a Flatbuffer object from an InternalByteBuffer
/// - Parameter
/// - bytes: Array of UInt8
@inline(__always)
init(byteBuffer: _InternalByteBuffer) {
_storage = Storage(
blob: .byteBuffer(byteBuffer),
capacity: byteBuffer.capacity)
_readerIndex = Int(byteBuffer.size)
}
/// Constructor that creates a Flatbuffer from unsafe memory region by copying
/// the underlying data to a new pointer
///
/// - Parameters:
/// - copyingMemoryBound: The unsafe memory region
/// - capacity: The size of the given memory region
@inline(__always)
public init(
copyingMemoryBound memory: UnsafeRawPointer,
capacity: Int)
{
_storage = Storage(count: capacity)
_storage.copy(from: memory, count: capacity)
_readerIndex = _storage.capacity
}
/// Constructor that creates a Flatbuffer object from a UInt8 /// Constructor that creates a Flatbuffer object from a UInt8
/// - Parameter /// - Parameter
/// - bytes: Array of UInt8 /// - bytes: Array of UInt8
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer @inline(__always)
public init( public init(bytes: [UInt8]) {
bytes: [UInt8], _storage = Storage(blob: .array(bytes), capacity: bytes.count)
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false) _readerIndex = _storage.capacity
{
var b = bytes
_storage = Storage(count: bytes.count, alignment: alignment)
_writerSize = _storage.capacity
allowReadingUnalignedBuffers = allowUnalignedBuffers
b.withUnsafeMutableBytes { bufferPointer in
_storage.copy(from: bufferPointer.baseAddress!, count: bytes.count)
}
} }
#if !os(WASI) #if !os(WASI)
/// Constructor that creates a Flatbuffer from the Swift Data type object /// Constructor that creates a Flatbuffer from the Swift Data type object
/// - Parameter /// - Parameter
/// - data: Swift data Object /// - data: Swift data Object
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer @inline(__always)
public init( public init(data: Data) {
data: Data, _storage = Storage(blob: .data(data), capacity: data.count)
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false) _readerIndex = _storage.capacity
{
var b = data
_storage = Storage(count: data.count, alignment: alignment)
_writerSize = _storage.capacity
allowReadingUnalignedBuffers = allowUnalignedBuffers
b.withUnsafeMutableBytes { bufferPointer in
_storage.copy(from: bufferPointer.baseAddress!, count: data.count)
}
}
#endif
/// Constructor that creates a Flatbuffer instance with a size
/// - Parameter:
/// - size: Length of the buffer
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
init(initialSize size: Int) {
let size = size.convertToPowerofTwo
_storage = Storage(count: size, alignment: alignment)
_storage.initialize(for: size)
allowReadingUnalignedBuffers = false
} }
#if swift(>=5.0) && !os(WASI)
/// Constructor that creates a Flatbuffer object from a ContiguousBytes /// Constructor that creates a Flatbuffer object from a ContiguousBytes
/// - Parameters: /// - Parameters:
/// - contiguousBytes: Binary stripe to use as the buffer /// - contiguousBytes: Binary stripe to use as the buffer
/// - count: amount of readable bytes /// - count: amount of readable bytes
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer @inline(__always)
public init<Bytes: ContiguousBytes>( public init<Bytes: ContiguousBytes>(
contiguousBytes: Bytes, contiguousBytes: Bytes,
count: Int, count: Int)
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
{ {
_storage = Storage(count: count, alignment: alignment) _storage = Storage(blob: .bytes(contiguousBytes), capacity: count)
_writerSize = _storage.capacity _readerIndex = _storage.capacity
allowReadingUnalignedBuffers = allowUnalignedBuffers
contiguousBytes.withUnsafeBytes { buf in
_storage.copy(from: buf.baseAddress!, count: buf.count)
}
} }
#endif #endif
/// Constructor that creates a Flatbuffer from unsafe memory region without copying /// Constructor that creates a Flatbuffer from unsafe memory region without copying
/// - Parameter: /// **NOTE** Needs a call to `memory.deallocate()` later on to free the memory
///
/// - Parameters:
/// - assumingMemoryBound: The unsafe memory region /// - assumingMemoryBound: The unsafe memory region
/// - capacity: The size of the given memory region /// - capacity: The size of the given memory region
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer @inline(__always)
public init( public init(
assumingMemoryBound memory: UnsafeMutableRawPointer, assumingMemoryBound memory: UnsafeMutableRawPointer,
capacity: Int, capacity: Int)
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
{ {
_storage = Storage(memory: memory, capacity: capacity, unowned: true) _storage = Storage(
_writerSize = capacity blob: .pointer(memory),
allowReadingUnalignedBuffers = allowUnalignedBuffers capacity: capacity)
} _readerIndex = _storage.capacity
/// Creates a copy of the buffer that's being built by calling sizedBuffer
/// - Parameters:
/// - memory: Current memory of the buffer
/// - count: count of bytes
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
init(
memory: UnsafeMutableRawPointer,
count: Int,
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
{
_storage = Storage(count: count, alignment: alignment)
_storage.copy(from: memory, count: count)
_writerSize = _storage.capacity
allowReadingUnalignedBuffers = allowUnalignedBuffers
} }
/// Creates a copy of the existing flatbuffer, by copying it to a different memory. /// Creates a copy of the existing flatbuffer, by copying it to a different memory.
@@ -219,145 +267,14 @@ public struct ByteBuffer {
/// - memory: Current memory of the buffer /// - memory: Current memory of the buffer
/// - count: count of bytes /// - count: count of bytes
/// - removeBytes: Removes a number of bytes from the current size /// - removeBytes: Removes a number of bytes from the current size
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer @inline(__always)
init( init(
memory: UnsafeMutableRawPointer, blob: Storage.Blob,
count: Int, count: Int,
removing removeBytes: Int, removing removeBytes: Int)
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
{ {
_storage = Storage(count: count, alignment: alignment) _storage = Storage(blob: blob, capacity: count)
_storage.copy(from: memory, count: count) _readerIndex = removeBytes
_writerSize = removeBytes
allowReadingUnalignedBuffers = allowUnalignedBuffers
}
/// Fills the buffer with padding by adding to the writersize
/// - Parameter padding: Amount of padding between two to be serialized objects
@inline(__always)
@usableFromInline
mutating func fill(padding: Int) {
assert(padding >= 0, "Fill should be larger than or equal to zero")
ensureSpace(size: padding)
_writerSize = _writerSize &+ (MemoryLayout<UInt8>.size &* padding)
}
/// Adds an array of type Scalar to the buffer memory
/// - Parameter elements: An array of Scalars
@inline(__always)
@usableFromInline
mutating func push<T: Scalar>(elements: [T]) {
elements.withUnsafeBytes { ptr in
ensureSpace(size: ptr.count)
memcpy(
_storage.memory.advanced(by: writerIndex &- ptr.count),
ptr.baseAddress!,
ptr.count)
_writerSize = _writerSize &+ ptr.count
}
}
/// Adds an array of type Scalar to the buffer memory
/// - Parameter elements: An array of Scalars
@inline(__always)
@usableFromInline
mutating func push<T: NativeStruct>(elements: [T]) {
elements.withUnsafeBytes { ptr in
ensureSpace(size: ptr.count)
memcpy(
_storage.memory.advanced(by: writerIndex &- ptr.count),
ptr.baseAddress!,
ptr.count)
_writerSize = _writerSize &+ ptr.count
}
}
/// Adds a `ContiguousBytes` to buffer memory
/// - Parameter value: bytes to copy
#if swift(>=5.0) && !os(WASI)
@inline(__always)
@usableFromInline
mutating func push(bytes: ContiguousBytes) {
bytes.withUnsafeBytes { ptr in
ensureSpace(size: ptr.count)
memcpy(
_storage.memory.advanced(by: writerIndex &- ptr.count),
ptr.baseAddress!,
ptr.count)
_writerSize = _writerSize &+ ptr.count
}
}
#endif
/// Adds an object of type NativeStruct into the buffer
/// - Parameters:
/// - value: Object that will be written to the buffer
/// - size: size to subtract from the WriterIndex
@usableFromInline
@inline(__always)
mutating func push<T: NativeStruct>(struct value: T, size: Int) {
ensureSpace(size: size)
withUnsafePointer(to: value) {
memcpy(
_storage.memory.advanced(by: writerIndex &- size),
$0,
size)
_writerSize = _writerSize &+ size
}
}
/// Adds an object of type Scalar into the buffer
/// - Parameters:
/// - value: Object that will be written to the buffer
/// - len: Offset to subtract from the WriterIndex
@inline(__always)
@usableFromInline
mutating func push<T: Scalar>(value: T, len: Int) {
ensureSpace(size: len)
withUnsafePointer(to: value) {
memcpy(
_storage.memory.advanced(by: writerIndex &- len),
$0,
len)
_writerSize = _writerSize &+ len
}
}
/// Adds a string to the buffer using swift.utf8 object
/// - Parameter str: String that will be added to the buffer
/// - Parameter len: length of the string
@inline(__always)
@usableFromInline
mutating func push(string str: String, len: Int) {
ensureSpace(size: len)
if str.utf8
.withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) !=
nil
{
} else {
let utf8View = str.utf8
for c in utf8View.reversed() {
push(value: c, len: 1)
}
}
}
/// Writes a string to Bytebuffer using UTF8View
/// - Parameters:
/// - bytes: Pointer to the view
/// - len: Size of string
@usableFromInline
@inline(__always)
mutating func push(
bytes: UnsafeBufferPointer<String.UTF8View.Element>,
len: Int) -> Bool
{
memcpy(
_storage.memory.advanced(by: writerIndex &- len),
bytes.baseAddress!,
len)
_writerSize = _writerSize &+ len
return true
} }
/// Write stores an object into the buffer directly or indirectly. /// Write stores an object into the buffer directly or indirectly.
@@ -376,63 +293,26 @@ public struct ByteBuffer {
} }
assert(index < _storage.capacity, "Write index is out of writing bound") assert(index < _storage.capacity, "Write index is out of writing bound")
assert(index >= 0, "Writer index should be above zero") assert(index >= 0, "Writer index should be above zero")
withUnsafePointer(to: value) { _ = withUnsafePointer(to: value) { ptr in
memcpy( _storage.withUnsafeRawPointer {
_storage.memory.advanced(by: index), memcpy(
$0, $0.advanced(by: index),
MemoryLayout<T>.size) ptr,
MemoryLayout<T>.size)
}
} }
} }
/// Makes sure that buffer has enouch space for each of the objects that will be written into it
/// - Parameter size: size of object
@discardableResult
@usableFromInline
@inline(__always)
mutating func ensureSpace(size: Int) -> Int {
if size &+ _writerSize > _storage.capacity {
_storage.reallocate(size, writerSize: _writerSize, alignment: alignment)
}
assert(size < FlatBufferMaxSize, "Buffer can't grow beyond 2 Gigabytes")
return size
}
/// pops the written VTable if it's already written into the buffer
/// - Parameter size: size of the `VTable`
@usableFromInline
@inline(__always)
mutating func pop(_ size: Int) {
assert(
(_writerSize &- size) > 0,
"New size should NOT be a negative number")
memset(_storage.memory.advanced(by: writerIndex), 0, _writerSize &- size)
_writerSize = size
}
/// Clears the current size of the buffer
@inline(__always)
mutating public func clearSize() {
_writerSize = 0
}
/// Clears the current instance of the buffer, replacing it with new memory
@inline(__always)
mutating public func clear() {
_writerSize = 0
alignment = 1
_storage.initialize(for: _storage.capacity)
}
/// Reads an object from the buffer /// Reads an object from the buffer
/// - Parameters: /// - Parameters:
/// - def: Type of the object /// - def: Type of the object
/// - position: the index of the object in the buffer /// - position: the index of the object in the buffer
@inline(__always) @inline(__always)
public func read<T>(def: T.Type, position: Int) -> T { public func read<T>(def: T.Type, position: Int) -> T {
if allowReadingUnalignedBuffers { _storage.readWithUnsafeRawPointer(position: position) {
return _storage.memory.advanced(by: position).loadUnaligned(as: T.self) $0.bindMemory(to: T.self, capacity: 1)
.pointee
} }
return _storage.memory.advanced(by: position).load(as: T.self)
} }
/// Reads a slice from the memory assuming a type of T /// Reads a slice from the memory assuming a type of T
@@ -447,10 +327,32 @@ public struct ByteBuffer {
assert( assert(
index + count <= _storage.capacity, index + count <= _storage.capacity,
"Reading out of bounds is illegal") "Reading out of bounds is illegal")
let start = _storage.memory.advanced(by: index)
.assumingMemoryBound(to: T.self) return _storage.readWithUnsafeRawPointer(position: index) {
let array = UnsafeBufferPointer(start: start, count: count) let buf = UnsafeBufferPointer(
return Array(array) start: $0.bindMemory(to: T.self, capacity: count),
count: count)
return Array(buf)
}
}
/// Provides a pointer towards the underlying primitive types
/// - Parameters:
/// - index: index of the object to be read from the buffer
/// - count: count of bytes in memory
@discardableResult
@inline(__always)
public func withUnsafePointerToSlice<T>(
index: Int,
count: Int,
body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T
{
assert(
index + count <= _storage.capacity,
"Reading out of bounds is illegal")
return try _storage.readWithUnsafeRawPointer(position: index) {
try body(UnsafeRawBufferPointer(start: $0, count: count))
}
} }
#if !os(WASI) #if !os(WASI)
@@ -468,10 +370,14 @@ public struct ByteBuffer {
assert( assert(
index + count <= _storage.capacity, index + count <= _storage.capacity,
"Reading out of bounds is illegal") "Reading out of bounds is illegal")
let start = _storage.memory.advanced(by: index) return _storage.readWithUnsafeRawPointer(position: index) {
.assumingMemoryBound(to: UInt8.self) let buf = UnsafeBufferPointer(
let bufprt = UnsafeBufferPointer(start: start, count: count) start: $0.bindMemory(to: UInt8.self, capacity: count),
return String(bytes: Array(bufprt), encoding: type) count: count)
return String(
bytes: buf,
encoding: type)
}
} }
#else #else
/// Reads a string from the buffer and encodes it to a swift string /// Reads a string from the buffer and encodes it to a swift string
@@ -486,10 +392,9 @@ public struct ByteBuffer {
assert( assert(
index + count <= _storage.capacity, index + count <= _storage.capacity,
"Reading out of bounds is illegal") "Reading out of bounds is illegal")
let start = _storage.memory.advanced(by: index) return _storage.readWithUnsafeRawPointer(position: index) {
.assumingMemoryBound(to: UInt8.self) String(cString: $0.bindMemory(to: UInt8.self, capacity: count))
let bufprt = UnsafeBufferPointer(start: start, count: count) }
return String(cString: bufprt.baseAddress!)
} }
#endif #endif
@@ -502,19 +407,9 @@ public struct ByteBuffer {
removeBytes < _storage.capacity, removeBytes < _storage.capacity,
"Can NOT remove more bytes than the ones allocated") "Can NOT remove more bytes than the ones allocated")
return ByteBuffer( return ByteBuffer(
memory: _storage.memory, blob: _storage.retainedBlob,
count: _storage.capacity, count: _storage.capacity,
removing: _writerSize &- removeBytes) removing: _readerIndex &- removeBytes)
}
/// Returns the written bytes into the ``ByteBuffer``
public var underlyingBytes: [UInt8] {
let cp = capacity &- writerIndex
let start = memory.advanced(by: writerIndex)
.bindMemory(to: UInt8.self, capacity: cp)
let ptr = UnsafeBufferPointer<UInt8>(start: start, count: cp)
return Array(ptr)
} }
/// SkipPrefix Skips the first 4 bytes in case one of the following /// SkipPrefix Skips the first 4 bytes in case one of the following
@@ -524,19 +419,46 @@ public struct ByteBuffer {
@usableFromInline @usableFromInline
@inline(__always) @inline(__always)
mutating func skipPrefix() -> Int32 { mutating func skipPrefix() -> Int32 {
_writerSize = _writerSize &- MemoryLayout<Int32>.size _readerIndex = _readerIndex &- MemoryLayout<Int32>.size
return read(def: Int32.self, position: 0) return read(def: Int32.self, position: 0)
} }
@discardableResult
@inline(__always)
public func withUnsafeBytes<T>(
body: (UnsafeRawBufferPointer) throws
-> T) rethrows -> T
{
try _storage.withUnsafeBytes(body)
}
@discardableResult
@inline(__always)
func withUnsafeMutableRawPointer<T>(
body: (UnsafeMutableRawPointer) throws
-> T) rethrows -> T
{
try _storage.withUnsafeRawPointer(body)
}
@discardableResult
@inline(__always)
func readWithUnsafeRawPointer<T>(
position: Int,
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
{
try _storage.readWithUnsafeRawPointer(position: position, body)
}
} }
extension ByteBuffer: CustomDebugStringConvertible { extension ByteBuffer: CustomDebugStringConvertible {
public var debugDescription: String { public var debugDescription: String {
""" """
buffer located at: \(_storage.memory), with capacity of \(_storage.capacity) buffer located at: \(_storage.retainedBlob),
{ writerSize: \(_writerSize), readerSize: \(reader), writerIndex: \( with capacity of \(_storage.capacity),
writerIndex) } { writtenSize: \(_readerIndex), readerSize: \(reader),
size: \(size) }
""" """
} }
} }

View File

@@ -32,7 +32,7 @@ public struct FlatBufferBuilder {
/// Storage for the Vtables used in the buffer are stored in here, so they would be written later in EndTable /// Storage for the Vtables used in the buffer are stored in here, so they would be written later in EndTable
@usableFromInline internal var _vtableStorage = VTableStorage() @usableFromInline internal var _vtableStorage = VTableStorage()
/// Flatbuffer data will be written into /// Flatbuffer data will be written into
@usableFromInline internal var _bb: ByteBuffer @usableFromInline internal var _bb: _InternalByteBuffer
/// Reference Vtables that were already written to the buffer /// Reference Vtables that were already written to the buffer
private var _vtables: [UOffset] = [] private var _vtables: [UOffset] = []
@@ -54,6 +54,9 @@ public struct FlatBufferBuilder {
/// Gives a read access to the buffer's size /// Gives a read access to the buffer's size
public var size: UOffset { _bb.size } public var size: UOffset { _bb.size }
/// Current allocated capacity within the storage
@inline(__always)
public var capacity: Int { _bb.capacity }
#if !os(WASI) #if !os(WASI)
/// Data representation of the buffer /// Data representation of the buffer
@@ -61,9 +64,15 @@ public struct FlatBufferBuilder {
/// Should only be used after ``finish(offset:addPrefix:)`` is called /// Should only be used after ``finish(offset:addPrefix:)`` is called
public var data: Data { public var data: Data {
assert(finished, "Data shouldn't be called before finish()") assert(finished, "Data shouldn't be called before finish()")
return Data( return _bb.withUnsafeSlicedBytes { ptr in
bytes: _bb.memory.advanced(by: _bb.writerIndex), var data = Data()
count: _bb.capacity &- _bb.writerIndex) data.append(
ptr.baseAddress!.bindMemory(
to: UInt8.self,
capacity: _bb.capacity),
count: _bb.capacity)
return data
}
} }
#endif #endif
@@ -71,10 +80,9 @@ public struct FlatBufferBuilder {
/// ///
/// Note: This should be used with caution. /// Note: This should be used with caution.
public var fullSizedByteArray: [UInt8] { public var fullSizedByteArray: [UInt8] {
let ptr = UnsafeBufferPointer( _bb.withUnsafeBytes { ptr in
start: _bb.memory.assumingMemoryBound(to: UInt8.self), Array(ptr)
count: _bb.capacity) }
return Array(ptr)
} }
/// Returns the written bytes into the ``ByteBuffer`` /// Returns the written bytes into the ``ByteBuffer``
@@ -82,14 +90,18 @@ public struct FlatBufferBuilder {
/// Should only be used after ``finish(offset:addPrefix:)`` is called /// Should only be used after ``finish(offset:addPrefix:)`` is called
public var sizedByteArray: [UInt8] { public var sizedByteArray: [UInt8] {
assert(finished, "Data shouldn't be called before finish()") assert(finished, "Data shouldn't be called before finish()")
return _bb.underlyingBytes return _bb.withUnsafeSlicedBytes { ptr in
Array(ptr)
}
} }
/// Returns the original ``ByteBuffer`` /// Returns the original ``ByteBuffer``
/// ///
/// Returns the current buffer that was just created /// Returns the current buffer that was just created
/// with the offsets, and data written to it. /// with the offsets, and data written to it.
public var buffer: ByteBuffer { _bb } public var buffer: ByteBuffer {
ByteBuffer(byteBuffer: _bb)
}
/// Returns a newly created sized ``ByteBuffer`` /// Returns a newly created sized ``ByteBuffer``
/// ///
@@ -97,9 +109,11 @@ public struct FlatBufferBuilder {
/// to the main buffer /// to the main buffer
public var sizedBuffer: ByteBuffer { public var sizedBuffer: ByteBuffer {
assert(finished, "Data shouldn't be called before finish()") assert(finished, "Data shouldn't be called before finish()")
return ByteBuffer( return _bb.withUnsafeSlicedBytes { ptr in
memory: _bb.memory.advanced(by: _bb.reader), ByteBuffer(
count: Int(_bb.size)) copyingMemoryBound: ptr.baseAddress!,
capacity: ptr.count)
}
} }
// MARK: - Init // MARK: - Init
@@ -122,7 +136,7 @@ public struct FlatBufferBuilder {
"Reading/Writing a buffer in big endian machine is not supported on swift") "Reading/Writing a buffer in big endian machine is not supported on swift")
} }
serializeDefaults = force serializeDefaults = force
_bb = ByteBuffer(initialSize: Int(initialSize)) _bb = _InternalByteBuffer(initialSize: Int(initialSize))
} }
/// Clears the builder and the buffer from the written data. /// Clears the builder and the buffer from the written data.
@@ -149,9 +163,10 @@ public struct FlatBufferBuilder {
for index in stride(from: 0, to: fields.count, by: 1) { for index in stride(from: 0, to: fields.count, by: 1) {
let start = _bb.capacity &- Int(table.o) let start = _bb.capacity &- Int(table.o)
let startTable = start &- Int(_bb.read(def: Int32.self, position: start)) let startTable = start &- Int(_bb.read(def: Int32.self, position: start))
let isOkay = _bb.read( let isOkay =
def: VOffset.self, _bb.read(
position: startTable &+ Int(fields[index])) != 0 def: VOffset.self,
position: startTable &+ Int(fields[index])) != 0
assert(isOkay, "Flatbuffers requires the following field") assert(isOkay, "Flatbuffers requires the following field")
} }
} }
@@ -249,7 +264,8 @@ public struct FlatBufferBuilder {
/// ///
/// - Parameter startOffset:Start point of the object written /// - Parameter startOffset:Start point of the object written
/// - returns: The root of the table /// - returns: The root of the table
mutating public func endTable(at startOffset: UOffset) -> UOffset { @inline(__always)
mutating public func endTable(at startOffset: UOffset) -> UOffset {
assert(isNested, "Calling endtable without calling starttable") assert(isNested, "Calling endtable without calling starttable")
let sizeofVoffset = MemoryLayout<VOffset>.size let sizeofVoffset = MemoryLayout<VOffset>.size
let vTableOffset = push(element: SOffset(0)) let vTableOffset = push(element: SOffset(0))
@@ -313,7 +329,7 @@ public struct FlatBufferBuilder {
/// Asserts to see if the object is not nested /// Asserts to see if the object is not nested
@inline(__always) @inline(__always)
@usableFromInline @usableFromInline
mutating internal func notNested() { mutating internal func notNested() {
assert(!isNested, "Object serialization must not be nested") assert(!isNested, "Object serialization must not be nested")
} }
@@ -337,7 +353,7 @@ public struct FlatBufferBuilder {
bufSize: UInt32, bufSize: UInt32,
elementSize: UInt32) -> UInt32 elementSize: UInt32) -> UInt32
{ {
((~bufSize) &+ 1) & (elementSize - 1) ((~bufSize) &+ 1) & (elementSize &- 1)
} }
/// Prealigns the buffer before writting a new object into the buffer /// Prealigns the buffer before writting a new object into the buffer
@@ -348,9 +364,11 @@ public struct FlatBufferBuilder {
@usableFromInline @usableFromInline
mutating internal func preAlign(len: Int, alignment: Int) { mutating internal func preAlign(len: Int, alignment: Int) {
minAlignment(size: alignment) minAlignment(size: alignment)
_bb.fill(padding: Int(padding( _bb.fill(
bufSize: _bb.size &+ UOffset(len), padding: Int(
elementSize: UOffset(alignment)))) padding(
bufSize: _bb.size &+ UOffset(len),
elementSize: UOffset(alignment))))
} }
/// Prealigns the buffer before writting a new object into the buffer /// Prealigns the buffer before writting a new object into the buffer
@@ -478,10 +496,11 @@ public struct FlatBufferBuilder {
/// - Parameter bytes: bytes to be written into the buffer /// - Parameter bytes: bytes to be written into the buffer
/// - Returns: ``Offset`` of the vector /// - Returns: ``Offset`` of the vector
mutating public func createVector(bytes: ContiguousBytes) -> Offset { mutating public func createVector(bytes: ContiguousBytes) -> Offset {
let size = bytes.withUnsafeBytes { ptr in ptr.count } bytes.withUnsafeBytes {
startVector(size, elementSize: MemoryLayout<UInt8>.size) startVector($0.count, elementSize: MemoryLayout<UInt8>.size)
_bb.push(bytes: bytes) _bb.push(bytes: $0)
return endVector(len: size) return endVector(len: $0.count)
}
} }
#endif #endif
@@ -822,6 +841,10 @@ public struct FlatBufferBuilder {
return _bb.size return _bb.size
} }
@inline(__always)
public func read<T>(def: T.Type, position: Int) -> T {
_bb.read(def: def, position: position)
}
} }
extension FlatBufferBuilder: CustomDebugStringConvertible { extension FlatBufferBuilder: CustomDebugStringConvertible {

View File

@@ -19,14 +19,16 @@ import Foundation
/// FlatBufferGRPCMessage protocol that should allow us to invoke /// FlatBufferGRPCMessage protocol that should allow us to invoke
/// initializers directly from the GRPC generated code /// initializers directly from the GRPC generated code
public protocol FlatBufferGRPCMessage { public protocol FlatBufferGRPCMessage {
/// Raw pointer which would be pointing to the beginning of the readable bytes
var rawPointer: UnsafeMutableRawPointer { get }
/// Size of readable bytes in the buffer /// Size of readable bytes in the buffer
var size: Int { get } var size: Int { get }
init(byteBuffer: ByteBuffer) init(byteBuffer: ByteBuffer)
@discardableResult
@inline(__always)
func withUnsafeReadableBytes<T>(
_ body: (UnsafeRawBufferPointer) throws
-> T) rethrows -> T
} }
/// Message is a wrapper around Buffers to to able to send Flatbuffers `Buffers` through the /// Message is a wrapper around Buffers to to able to send Flatbuffers `Buffers` through the
@@ -38,13 +40,10 @@ public struct Message<T: FlatBufferObject>: FlatBufferGRPCMessage {
public var object: T { public var object: T {
T.init( T.init(
buffer, buffer,
o: Int32(buffer.read(def: UOffset.self, position: buffer.reader)) + o: Int32(buffer.read(def: UOffset.self, position: buffer.reader)) &+
Int32(buffer.reader)) Int32(buffer.reader))
} }
public var rawPointer: UnsafeMutableRawPointer {
buffer.memory.advanced(by: buffer.reader) }
public var size: Int { Int(buffer.size) } public var size: Int { Int(buffer.size) }
/// Initializes the message with the type Flatbuffer.Bytebuffer that is transmitted over /// Initializes the message with the type Flatbuffer.Bytebuffer that is transmitted over
@@ -62,4 +61,15 @@ public struct Message<T: FlatBufferObject>: FlatBufferGRPCMessage {
buffer = builder.sizedBuffer buffer = builder.sizedBuffer
builder.clear() builder.clear()
} }
@discardableResult
@inline(__always)
public func withUnsafeReadableBytes<Data>(
_ body: (UnsafeRawBufferPointer) throws
-> Data) rethrows -> Data
{
return try buffer.readWithUnsafeRawPointer(position: buffer.reader) {
try body(UnsafeRawBufferPointer(start: $0, count: size))
}
}
} }

View File

@@ -101,7 +101,7 @@ public func getCheckedRoot<T: FlatBufferObject & Verifiable>(
try ForwardOffset<T>.verify(&verifier, at: 0, of: T.self) try ForwardOffset<T>.verify(&verifier, at: 0, of: T.self)
return T.init( return T.init(
byteBuffer, byteBuffer,
o: Int32(byteBuffer.read(def: UOffset.self, position: byteBuffer.reader)) + o: Int32(byteBuffer.read(def: UOffset.self, position: byteBuffer.reader)) &+
Int32(byteBuffer.reader)) Int32(byteBuffer.reader))
} }
@@ -111,6 +111,6 @@ public func getCheckedRoot<T: FlatBufferObject & Verifiable>(
public func getRoot<T: FlatBufferObject>(byteBuffer: inout ByteBuffer) -> T { public func getRoot<T: FlatBufferObject>(byteBuffer: inout ByteBuffer) -> T {
T.init( T.init(
byteBuffer, byteBuffer,
o: Int32(byteBuffer.read(def: UOffset.self, position: byteBuffer.reader)) + o: Int32(byteBuffer.read(def: UOffset.self, position: byteBuffer.reader)) &+
Int32(byteBuffer.reader)) Int32(byteBuffer.reader))
} }

View File

@@ -64,7 +64,7 @@ extension String: FlatbuffersInitializable {
let v = Int(o) let v = Int(o)
let count = bb.read(def: Int32.self, position: v) let count = bb.read(def: Int32.self, position: v)
self = bb.readString( self = bb.readString(
at: MemoryLayout<Int32>.size + v, at: MemoryLayout<Int32>.size &+ v,
count: Int(count)) ?? "" count: Int(count)) ?? ""
} }
} }

View File

@@ -46,11 +46,11 @@ public struct Table {
/// - Parameter o: current offset /// - Parameter o: current offset
/// - Returns: offset of field within buffer /// - Returns: offset of field within buffer
public func offset(_ o: Int32) -> Int32 { public func offset(_ o: Int32) -> Int32 {
let vtable = position - bb.read(def: Int32.self, position: Int(position)) let vtable = position &- bb.read(def: Int32.self, position: Int(position))
return o < bb return o < bb
.read(def: VOffset.self, position: Int(vtable)) ? Int32(bb.read( .read(def: VOffset.self, position: Int(vtable)) ? Int32(bb.read(
def: Int16.self, def: Int16.self,
position: Int(vtable + o))) : 0 position: Int(vtable &+ o))) : 0
} }
/// Gets the indirect offset of the current stored object /// Gets the indirect offset of the current stored object
@@ -58,13 +58,13 @@ public struct Table {
/// - Parameter o: current offset /// - Parameter o: current offset
/// - Returns: offset of field within buffer /// - Returns: offset of field within buffer
public func indirect(_ o: Int32) -> Int32 { public func indirect(_ o: Int32) -> Int32 {
o + bb.read(def: Int32.self, position: Int(o)) o &+ bb.read(def: Int32.self, position: Int(o))
} }
/// String reads from the buffer with respect to position of the current table. /// String reads from the buffer with respect to position of the current table.
/// - Parameter offset: Offset of the string /// - Parameter offset: Offset of the string
public func string(at offset: Int32) -> String? { public func string(at offset: Int32) -> String? {
directString(at: offset + position) directString(at: offset &+ position)
} }
/// Direct string reads from the buffer disregarding the position of the table. /// Direct string reads from the buffer disregarding the position of the table.
@@ -73,9 +73,9 @@ public struct Table {
/// - Parameter offset: Offset of the string /// - Parameter offset: Offset of the string
public func directString(at offset: Int32) -> String? { public func directString(at offset: Int32) -> String? {
var offset = offset var offset = offset
offset += bb.read(def: Int32.self, position: Int(offset)) offset &+= bb.read(def: Int32.self, position: Int(offset))
let count = bb.read(def: Int32.self, position: Int(offset)) let count = bb.read(def: Int32.self, position: Int(offset))
let position = Int(offset) + MemoryLayout<Int32>.size let position = Int(offset) &+ MemoryLayout<Int32>.size
return bb.readString(at: position, count: Int(count)) return bb.readString(at: position, count: Int(count))
} }
@@ -84,7 +84,7 @@ public struct Table {
/// - type: Type of Element that needs to be read from the buffer /// - type: Type of Element that needs to be read from the buffer
/// - o: Offset of the Element /// - o: Offset of the Element
public func readBuffer<T>(of type: T.Type, at o: Int32) -> T { public func readBuffer<T>(of type: T.Type, at o: Int32) -> T {
directRead(of: T.self, offset: o + position) directRead(of: T.self, offset: o &+ position)
} }
/// Reads from the buffer disregarding the position of the table. /// Reads from the buffer disregarding the position of the table.
@@ -101,8 +101,7 @@ public struct Table {
/// - type: Type of Element that needs to be read from the buffer /// - type: Type of Element that needs to be read from the buffer
/// - o: Offset of the Element /// - o: Offset of the Element
public func directRead<T>(of type: T.Type, offset o: Int32) -> T { public func directRead<T>(of type: T.Type, offset o: Int32) -> T {
let r = bb.read(def: T.self, position: Int(o)) bb.read(def: T.self, position: Int(o))
return r
} }
/// Returns that current `Union` object at a specific offset /// Returns that current `Union` object at a specific offset
@@ -110,7 +109,7 @@ public struct Table {
/// - Parameter o: offset /// - Parameter o: offset
/// - Returns: A flatbuffers object /// - Returns: A flatbuffers object
public func union<T: FlatbuffersInitializable>(_ o: Int32) -> T { public func union<T: FlatbuffersInitializable>(_ o: Int32) -> T {
let o = o + position let o = o &+ position
return directUnion(o) return directUnion(o)
} }
@@ -118,7 +117,7 @@ public struct Table {
/// - Parameter o: offset /// - Parameter o: offset
/// - Returns: A flatbuffers object /// - Returns: A flatbuffers object
public func directUnion<T: FlatbuffersInitializable>(_ o: Int32) -> T { public func directUnion<T: FlatbuffersInitializable>(_ o: Int32) -> T {
T.init(bb, o: o + bb.read(def: Int32.self, position: Int(o))) T.init(bb, o: o &+ bb.read(def: Int32.self, position: Int(o)))
} }
/// Returns a vector of type T at a specific offset /// Returns a vector of type T at a specific offset
@@ -131,13 +130,30 @@ public struct Table {
return bb.readSlice(index: Int(vector(at: o)), count: Int(vector(count: o))) return bb.readSlice(index: Int(vector(at: o)), count: Int(vector(count: o)))
} }
/// Returns the underlying pointer to a vector within the buffer
/// This should only be used by `Scalars`
/// - Parameter off: Readable offset
/// - Returns: Returns a pointer to the underlying data
@inline(__always)
public func withUnsafePointerToSlice<T>(
at off: Int32,
body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T?
{
let o = offset(off)
guard o != 0 else { return nil }
return try bb.withUnsafePointerToSlice(
index: Int(vector(at: o)),
count: Int(vector(count: o)),
body: body)
}
/// Vector count gets the count of Elements within the array /// Vector count gets the count of Elements within the array
/// - Parameter o: start offset of the vector /// - Parameter o: start offset of the vector
/// - returns: Count of elements /// - returns: Count of elements
public func vector(count o: Int32) -> Int32 { public func vector(count o: Int32) -> Int32 {
var o = o var o = o
o += position o &+= position
o += bb.read(def: Int32.self, position: Int(o)) o &+= bb.read(def: Int32.self, position: Int(o))
return bb.read(def: Int32.self, position: Int(o)) return bb.read(def: Int32.self, position: Int(o))
} }
@@ -146,8 +162,8 @@ public struct Table {
/// - returns: the start index of the vector /// - returns: the start index of the vector
public func vector(at o: Int32) -> Int32 { public func vector(at o: Int32) -> Int32 {
var o = o var o = o
o += position o &+= position
return o + bb.read(def: Int32.self, position: Int(o)) + 4 return o &+ bb.read(def: Int32.self, position: Int(o)) + 4
} }
/// Reading an indirect offset of a table. /// Reading an indirect offset of a table.
@@ -165,15 +181,16 @@ public struct Table {
/// - vOffset: Field offset within a vtable /// - vOffset: Field offset within a vtable
/// - fbb: ByteBuffer /// - fbb: ByteBuffer
/// - Returns: an position of a field /// - Returns: an position of a field
@inline(__always)
static public func offset( static public func offset(
_ o: Int32, _ o: Int32,
vOffset: Int32, vOffset: Int32,
fbb: ByteBuffer) -> Int32 fbb: inout FlatBufferBuilder) -> Int32
{ {
let vTable = Int32(fbb.capacity) - o let vTable = Int32(fbb.capacity) &- o
return vTable + Int32(fbb.read( return vTable &+ Int32(fbb.read(
def: Int16.self, def: Int16.self,
position: Int(vTable + vOffset - fbb.read( position: Int(vTable &+ vOffset &- fbb.read(
def: Int32.self, def: Int32.self,
position: Int(vTable))))) position: Int(vTable)))))
} }
@@ -184,27 +201,28 @@ public struct Table {
/// - off2: second offset to compare /// - off2: second offset to compare
/// - fbb: Bytebuffer /// - fbb: Bytebuffer
/// - Returns: returns the difference between /// - Returns: returns the difference between
@inline(__always)
static public func compare( static public func compare(
_ off1: Int32, _ off1: Int32,
_ off2: Int32, _ off2: Int32,
fbb: ByteBuffer) -> Int32 fbb: inout FlatBufferBuilder) -> Int32
{ {
let memorySize = Int32(MemoryLayout<Int32>.size) let memorySize = Int32(MemoryLayout<Int32>.size)
let _off1 = off1 + fbb.read(def: Int32.self, position: Int(off1)) let _off1 = off1 &+ fbb.read(def: Int32.self, position: Int(off1))
let _off2 = off2 + fbb.read(def: Int32.self, position: Int(off2)) let _off2 = off2 &+ fbb.read(def: Int32.self, position: Int(off2))
let len1 = fbb.read(def: Int32.self, position: Int(_off1)) let len1 = fbb.read(def: Int32.self, position: Int(_off1))
let len2 = fbb.read(def: Int32.self, position: Int(_off2)) let len2 = fbb.read(def: Int32.self, position: Int(_off2))
let startPos1 = _off1 + memorySize let startPos1 = _off1 &+ memorySize
let startPos2 = _off2 + memorySize let startPos2 = _off2 &+ memorySize
let minValue = min(len1, len2) let minValue = min(len1, len2)
for i in 0...minValue { for i in 0...minValue {
let b1 = fbb.read(def: Int8.self, position: Int(i + startPos1)) let b1 = fbb.read(def: Int8.self, position: Int(i &+ startPos1))
let b2 = fbb.read(def: Int8.self, position: Int(i + startPos2)) let b2 = fbb.read(def: Int8.self, position: Int(i &+ startPos2))
if b1 != b2 { if b1 != b2 {
return Int32(b2 - b1) return Int32(b2 &- b1)
} }
} }
return len1 - len2 return len1 &- len2
} }
/// Compares two objects at offset A and array of `Bytes` within a ByteBuffer /// Compares two objects at offset A and array of `Bytes` within a ByteBuffer
@@ -213,24 +231,103 @@ public struct Table {
/// - key: bytes array to compare to /// - key: bytes array to compare to
/// - fbb: Bytebuffer /// - fbb: Bytebuffer
/// - Returns: returns the difference between /// - Returns: returns the difference between
@inline(__always)
static public func compare(
_ off1: Int32,
_ key: [Byte],
fbb: inout FlatBufferBuilder) -> Int32
{
let memorySize = Int32(MemoryLayout<Int32>.size)
let _off1 = off1 &+ fbb.read(def: Int32.self, position: Int(off1))
let len1 = fbb.read(def: Int32.self, position: Int(_off1))
let len2 = Int32(key.count)
let startPos1 = _off1 &+ memorySize
let minValue = min(len1, len2)
for i in 0..<minValue {
let b = fbb.read(def: Int8.self, position: Int(i &+ startPos1))
let byte = key[Int(i)]
if b != byte {
return Int32(b &- Int8(byte))
}
}
return len1 &- len2
}
/// Gets a vtable value according to an table Offset and a field offset
/// - Parameters:
/// - o: offset relative to entire buffer
/// - vOffset: Field offset within a vtable
/// - fbb: ByteBuffer
/// - Returns: an position of a field
@inline(__always)
static public func offset(
_ o: Int32,
vOffset: Int32,
fbb: ByteBuffer) -> Int32
{
let vTable = Int32(fbb.capacity) &- o
return vTable &+ Int32(fbb.read(
def: Int16.self,
position: Int(vTable &+ vOffset &- fbb.read(
def: Int32.self,
position: Int(vTable)))))
}
/// Compares two objects at offset A and offset B within a ByteBuffer
/// - Parameters:
/// - off1: first offset to compare
/// - off2: second offset to compare
/// - fbb: Bytebuffer
/// - Returns: returns the difference between
@inline(__always)
static public func compare(
_ off1: Int32,
_ off2: Int32,
fbb: ByteBuffer) -> Int32
{
let memorySize = Int32(MemoryLayout<Int32>.size)
let _off1 = off1 &+ fbb.read(def: Int32.self, position: Int(off1))
let _off2 = off2 &+ fbb.read(def: Int32.self, position: Int(off2))
let len1 = fbb.read(def: Int32.self, position: Int(_off1))
let len2 = fbb.read(def: Int32.self, position: Int(_off2))
let startPos1 = _off1 &+ memorySize
let startPos2 = _off2 &+ memorySize
let minValue = min(len1, len2)
for i in 0...minValue {
let b1 = fbb.read(def: Int8.self, position: Int(i &+ startPos1))
let b2 = fbb.read(def: Int8.self, position: Int(i &+ startPos2))
if b1 != b2 {
return Int32(b2 &- b1)
}
}
return len1 &- len2
}
/// Compares two objects at offset A and array of `Bytes` within a ByteBuffer
/// - Parameters:
/// - off1: Offset to compare to
/// - key: bytes array to compare to
/// - fbb: Bytebuffer
/// - Returns: returns the difference between
@inline(__always)
static public func compare( static public func compare(
_ off1: Int32, _ off1: Int32,
_ key: [Byte], _ key: [Byte],
fbb: ByteBuffer) -> Int32 fbb: ByteBuffer) -> Int32
{ {
let memorySize = Int32(MemoryLayout<Int32>.size) let memorySize = Int32(MemoryLayout<Int32>.size)
let _off1 = off1 + fbb.read(def: Int32.self, position: Int(off1)) let _off1 = off1 &+ fbb.read(def: Int32.self, position: Int(off1))
let len1 = fbb.read(def: Int32.self, position: Int(_off1)) let len1 = fbb.read(def: Int32.self, position: Int(_off1))
let len2 = Int32(key.count) let len2 = Int32(key.count)
let startPos1 = _off1 + memorySize let startPos1 = _off1 &+ memorySize
let minValue = min(len1, len2) let minValue = min(len1, len2)
for i in 0..<minValue { for i in 0..<minValue {
let b = fbb.read(def: Int8.self, position: Int(i + startPos1)) let b = fbb.read(def: Int8.self, position: Int(i &+ startPos1))
let byte = key[Int(i)] let byte = key[Int(i)]
if b != byte { if b != byte {
return Int32(b - Int8(byte)) return Int32(b &- Int8(byte))
} }
} }
return len1 - len2 return len1 &- len2
} }
} }

View File

@@ -197,17 +197,17 @@ public enum UnionVector<S> where S: UnionEnum {
while count < keysRange.count { while count < keysRange.count {
/// index of readable enum value in array /// index of readable enum value in array
let keysIndex = MemoryLayout<S.T>.size * count let keysIndex = MemoryLayout<S.T>.size &* count
guard let _enum = try S.init(value: verifier._buffer.read( guard let _enum = try S.init(value: verifier._buffer.read(
def: S.T.self, def: S.T.self,
position: keysRange.start + keysIndex)) else position: keysRange.start &+ keysIndex)) else
{ {
throw FlatbuffersErrors.unknownUnionCase throw FlatbuffersErrors.unknownUnionCase
} }
/// index of readable offset value in array /// index of readable offset value in array
let fieldIndex = MemoryLayout<UOffset>.size * count let fieldIndex = MemoryLayout<UOffset>.size &* count
try completion(&verifier, _enum, offsetsRange.start + fieldIndex) try completion(&verifier, _enum, offsetsRange.start &+ fieldIndex)
count += 1 count &+= 1
} }
} }
} }

View File

@@ -83,15 +83,18 @@ public struct Verifier {
if !_checkAlignment { return } if !_checkAlignment { return }
/// advance pointer to position X /// advance pointer to position X
let ptr = _buffer._storage.memory.advanced(by: position) try _buffer.withUnsafeBytes { pointer in
/// Check if the pointer is aligned let ptr = pointer.baseAddress!.advanced(by: position)
if Int(bitPattern: ptr) & (MemoryLayout<T>.alignment &- 1) == 0 {
return
}
throw FlatbuffersErrors.missAlignedPointer( /// Check if the pointer is aligned
position: position, if Int(bitPattern: ptr) & (MemoryLayout<T>.alignment &- 1) == 0 {
type: String(describing: T.self)) return
}
throw FlatbuffersErrors.missAlignedPointer(
position: position,
type: String(describing: T.self))
}
} }
/// Checks if the value of Size "X" is within the range of the buffer /// Checks if the value of Size "X" is within the range of the buffer
@@ -134,17 +137,17 @@ public struct Verifier {
let length = Int(vtableLength) let length = Int(vtableLength)
try isAligned( try isAligned(
position: Int(clamping: (vtablePosition + length).magnitude), position: Int(clamping: (vtablePosition &+ length).magnitude),
type: VOffset.self) type: VOffset.self)
try rangeInBuffer(position: vtablePosition, size: length) try rangeInBuffer(position: vtablePosition, size: length)
storage.tableCount += 1 storage.tableCount &+= 1
if storage.tableCount > _options._maxTableCount { if storage.tableCount > _options._maxTableCount {
throw FlatbuffersErrors.maximumTables throw FlatbuffersErrors.maximumTables
} }
storage.depth += 1 storage.depth &+= 1
if storage.depth > _options._maxDepth { if storage.depth > _options._maxDepth {
throw FlatbuffersErrors.maximumDepth throw FlatbuffersErrors.maximumDepth
@@ -211,7 +214,7 @@ public struct Verifier {
@inline(__always) @inline(__always)
func verify(id: String) throws { func verify(id: String) throws {
let size = MemoryLayout<Int32>.size let size = MemoryLayout<Int32>.size
guard storage.capacity >= (size * 2) else { guard storage.capacity >= (size &* 2) else {
throw FlatbuffersErrors.bufferDoesntContainID throw FlatbuffersErrors.bufferDoesntContainID
} }
let str = _buffer.readString(at: size, count: size) let str = _buffer.readString(at: size, count: size)

View File

@@ -0,0 +1,365 @@
/*
* Copyright 2024 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Foundation
/// `ByteBuffer` is the interface that stores the data for a `Flatbuffers` object
/// it allows users to Construct their buffers internally without much cost to performance
@usableFromInline
struct _InternalByteBuffer {
/// Storage is a container that would hold the memory pointer to solve the issue of
/// deallocating the memory that was held by (memory: UnsafeMutableRawPointer)
@usableFromInline
final class Storage {
// This storage doesn't own the memory, therefore, we won't deallocate on deinit.
private let unowned: Bool
/// pointer to the start of the buffer object in memory
var memory: UnsafeMutableRawPointer
/// Capacity of UInt8 the buffer can hold
var capacity: Int
@usableFromInline
init(count: Int, alignment: Int) {
memory = UnsafeMutableRawPointer.allocate(
byteCount: count,
alignment: alignment)
capacity = count
unowned = false
}
deinit {
if !unowned {
memory.deallocate()
}
}
@usableFromInline
func initialize(for size: Int) {
assert(
!unowned,
"initalize should NOT be called on a buffer that is built by assumingMemoryBound")
memset(memory, 0, size)
}
/// Reallocates the buffer incase the object to be written doesnt fit in the current buffer
/// - Parameter size: Size of the current object
@usableFromInline
func reallocate(_ size: Int, writerSize: Int, alignment: Int) {
let currentWritingIndex = capacity &- writerSize
while capacity <= writerSize &+ size {
capacity = capacity << 1
}
/// solution take from Apple-NIO
capacity = capacity.convertToPowerofTwo
let newData = UnsafeMutableRawPointer.allocate(
byteCount: capacity,
alignment: alignment)
memset(newData, 0, capacity &- writerSize)
memcpy(
newData.advanced(by: capacity &- writerSize),
memory.advanced(by: currentWritingIndex),
writerSize)
memory.deallocate()
memory = newData
}
}
@usableFromInline var _storage: Storage
/// The size of the elements written to the buffer + their paddings
private var _writerSize: Int = 0
/// Alignment of the current memory being written to the buffer
var alignment = 1
/// Current Index which is being used to write to the buffer, it is written from the end to the start of the buffer
var writerIndex: Int { _storage.capacity &- _writerSize }
/// Reader is the position of the current Writer Index (capacity - size)
public var reader: Int { writerIndex }
/// Current size of the buffer
public var size: UOffset { UOffset(_writerSize) }
/// Public Pointer to the buffer object in memory. This should NOT be modified for any reason
@usableFromInline
var memory: UnsafeMutableRawPointer { _storage.memory }
/// Current capacity for the buffer
public var capacity: Int { _storage.capacity }
/// Constructor that creates a Flatbuffer instance with a size
/// - Parameter:
/// - size: Length of the buffer
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
init(initialSize size: Int) {
let size = size.convertToPowerofTwo
_storage = Storage(count: size, alignment: alignment)
_storage.initialize(for: size)
}
/// Fills the buffer with padding by adding to the writersize
/// - Parameter padding: Amount of padding between two to be serialized objects
@inline(__always)
@usableFromInline
mutating func fill(padding: Int) {
assert(padding >= 0, "Fill should be larger than or equal to zero")
ensureSpace(size: padding)
_writerSize = _writerSize &+ (MemoryLayout<UInt8>.size &* padding)
}
/// Adds an array of type Scalar to the buffer memory
/// - Parameter elements: An array of Scalars
@inline(__always)
@usableFromInline
mutating func push<T: Scalar>(elements: [T]) {
elements.withUnsafeBytes { ptr in
ensureSpace(size: ptr.count)
memcpy(
_storage.memory.advanced(by: writerIndex &- ptr.count),
ptr.baseAddress!,
ptr.count)
_writerSize = _writerSize &+ ptr.count
}
}
/// Adds an array of type Scalar to the buffer memory
/// - Parameter elements: An array of Scalars
@inline(__always)
@usableFromInline
mutating func push<T: NativeStruct>(elements: [T]) {
elements.withUnsafeBytes { ptr in
ensureSpace(size: ptr.count)
memcpy(
_storage.memory.advanced(by: writerIndex &- ptr.count),
ptr.baseAddress!,
ptr.count)
_writerSize = _writerSize &+ ptr.count
}
}
/// Adds a `ContiguousBytes` to buffer memory
/// - Parameter value: bytes to copy
#if swift(>=5.0) && !os(WASI)
@inline(__always)
@usableFromInline
mutating func push(bytes: ContiguousBytes) {
bytes.withUnsafeBytes { ptr in
ensureSpace(size: ptr.count)
memcpy(
_storage.memory.advanced(by: writerIndex &- ptr.count),
ptr.baseAddress!,
ptr.count)
_writerSize = _writerSize &+ ptr.count
}
}
#endif
/// Adds an object of type NativeStruct into the buffer
/// - Parameters:
/// - value: Object that will be written to the buffer
/// - size: size to subtract from the WriterIndex
@usableFromInline
@inline(__always)
mutating func push<T: NativeStruct>(struct value: T, size: Int) {
ensureSpace(size: size)
withUnsafePointer(to: value) {
memcpy(
_storage.memory.advanced(by: writerIndex &- size),
$0,
size)
_writerSize = _writerSize &+ size
}
}
/// Adds an object of type Scalar into the buffer
/// - Parameters:
/// - value: Object that will be written to the buffer
/// - len: Offset to subtract from the WriterIndex
@inline(__always)
@usableFromInline
mutating func push<T: Scalar>(value: T, len: Int) {
ensureSpace(size: len)
withUnsafePointer(to: value) {
memcpy(
_storage.memory.advanced(by: writerIndex &- len),
$0,
len)
_writerSize = _writerSize &+ len
}
}
/// Adds a string to the buffer using swift.utf8 object
/// - Parameter str: String that will be added to the buffer
/// - Parameter len: length of the string
@inline(__always)
@usableFromInline
mutating func push(string str: String, len: Int) {
ensureSpace(size: len)
if str.utf8
.withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) !=
nil
{
} else {
let utf8View = str.utf8
for c in utf8View.reversed() {
push(value: c, len: 1)
}
}
}
/// Writes a string to Bytebuffer using UTF8View
/// - Parameters:
/// - bytes: Pointer to the view
/// - len: Size of string
@usableFromInline
@inline(__always)
mutating func push(
bytes: UnsafeBufferPointer<String.UTF8View.Element>,
len: Int) -> Bool
{
memcpy(
_storage.memory.advanced(by: writerIndex &- len),
bytes.baseAddress!,
len)
_writerSize = _writerSize &+ len
return true
}
/// Write stores an object into the buffer directly or indirectly.
///
/// Direct: ignores the capacity of buffer which would mean we are referring to the direct point in memory
/// indirect: takes into respect the current capacity of the buffer (capacity - index), writing to the buffer from the end
/// - Parameters:
/// - value: Value that needs to be written to the buffer
/// - index: index to write to
/// - direct: Should take into consideration the capacity of the buffer
@inline(__always)
func write<T>(value: T, index: Int, direct: Bool = false) {
var index = index
if !direct {
index = _storage.capacity &- index
}
assert(index < _storage.capacity, "Write index is out of writing bound")
assert(index >= 0, "Writer index should be above zero")
_ = withUnsafePointer(to: value) {
memcpy(
_storage.memory.advanced(by: index),
$0,
MemoryLayout<T>.size)
}
}
/// Makes sure that buffer has enouch space for each of the objects that will be written into it
/// - Parameter size: size of object
@discardableResult
@usableFromInline
@inline(__always)
mutating func ensureSpace(size: Int) -> Int {
if size &+ _writerSize > _storage.capacity {
_storage.reallocate(size, writerSize: _writerSize, alignment: alignment)
}
assert(size < FlatBufferMaxSize, "Buffer can't grow beyond 2 Gigabytes")
return size
}
/// pops the written VTable if it's already written into the buffer
/// - Parameter size: size of the `VTable`
@usableFromInline
@inline(__always)
mutating func pop(_ size: Int) {
assert(
(_writerSize &- size) > 0,
"New size should NOT be a negative number")
memset(_storage.memory.advanced(by: writerIndex), 0, _writerSize &- size)
_writerSize = size
}
/// Clears the current size of the buffer
@inline(__always)
mutating public func clearSize() {
_writerSize = 0
}
/// Clears the current instance of the buffer, replacing it with new memory
@inline(__always)
mutating public func clear() {
_writerSize = 0
alignment = 1
_storage.initialize(for: _storage.capacity)
}
/// Reads an object from the buffer
/// - Parameters:
/// - def: Type of the object
/// - position: the index of the object in the buffer
@inline(__always)
public func read<T>(def: T.Type, position: Int) -> T {
_storage.memory
.advanced(by: position)
.bindMemory(to: T.self, capacity: 1)
.pointee
}
@discardableResult
@inline(__always)
func withUnsafeBytes<T>(
_ body: (UnsafeRawBufferPointer) throws
-> T) rethrows -> T
{
try body(UnsafeRawBufferPointer(
start: _storage.memory,
count: capacity))
}
@discardableResult
@inline(__always)
func withUnsafeSlicedBytes<T>(
_ body: (UnsafeRawBufferPointer) throws
-> T) rethrows -> T
{
try body(UnsafeRawBufferPointer(
start: _storage.memory.advanced(by: writerIndex),
count: capacity &- writerIndex))
}
@discardableResult
@inline(__always)
func withUnsafeRawPointer<T>(
_ body: (UnsafeMutableRawPointer) throws
-> T) rethrows -> T
{
try body(_storage.memory)
}
@discardableResult
@inline(__always)
func readWithUnsafeRawPointer<T>(
position: Int,
_ body: (UnsafeRawPointer) throws -> T) rethrows -> T
{
try body(_storage.memory.advanced(by: position))
}
}
extension _InternalByteBuffer: CustomDebugStringConvertible {
public var debugDescription: String {
"""
buffer located at: \(_storage.memory), with capacity of \(_storage.capacity)
{ writerSize: \(_writerSize), readerSize: \(reader), writerIndex: \(
writerIndex) }
"""
}
}

View File

@@ -906,7 +906,7 @@ public struct MyGame_Example_Stat: FlatBufferObject, Verifiable, ObjectAPIPacker
} }
public static func sortVectorOfStat(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset { public static func sortVectorOfStat(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset {
var off = offsets var off = offsets
off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 8, fbb: fbb.buffer), Table.offset(Int32($0.o), vOffset: 8, fbb: fbb.buffer), fbb: fbb.buffer) < 0 } off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 8, fbb: &fbb), Table.offset(Int32($0.o), vOffset: 8, fbb: &fbb), fbb: &fbb) < 0 }
return fbb.createVector(ofOffsets: off) return fbb.createVector(ofOffsets: off)
} }
fileprivate static func lookupByKey(vector: Int32, key: UInt16, fbb: ByteBuffer) -> MyGame_Example_Stat? { fileprivate static func lookupByKey(vector: Int32, key: UInt16, fbb: ByteBuffer) -> MyGame_Example_Stat? {
@@ -1033,7 +1033,7 @@ public struct MyGame_Example_Referrable: FlatBufferObject, Verifiable, ObjectAPI
} }
public static func sortVectorOfReferrable(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset { public static func sortVectorOfReferrable(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset {
var off = offsets var off = offsets
off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 4, fbb: fbb.buffer), Table.offset(Int32($0.o), vOffset: 4, fbb: fbb.buffer), fbb: fbb.buffer) < 0 } off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 4, fbb: &fbb), Table.offset(Int32($0.o), vOffset: 4, fbb: &fbb), fbb: &fbb) < 0 }
return fbb.createVector(ofOffsets: off) return fbb.createVector(ofOffsets: off)
} }
fileprivate static func lookupByKey(vector: Int32, key: UInt64, fbb: ByteBuffer) -> MyGame_Example_Referrable? { fileprivate static func lookupByKey(vector: Int32, key: UInt64, fbb: ByteBuffer) -> MyGame_Example_Referrable? {
@@ -1197,6 +1197,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func inventory(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) } public func inventory(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) }
public var inventory: [UInt8] { return _accessor.getVector(at: VTOFFSET.inventory.v) ?? [] } public var inventory: [UInt8] { return _accessor.getVector(at: VTOFFSET.inventory.v) ?? [] }
public func mutate(inventory: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.inventory.v); return _accessor.directMutate(inventory, index: _accessor.vector(at: o) + index * 1) } public func mutate(inventory: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.inventory.v); return _accessor.directMutate(inventory, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToInventory<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.inventory.v, body: body) }
public var color: MyGame_Example_Color { let o = _accessor.offset(VTOFFSET.color.v); return o == 0 ? .blue : MyGame_Example_Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .blue } public var color: MyGame_Example_Color { let o = _accessor.offset(VTOFFSET.color.v); return o == 0 ? .blue : MyGame_Example_Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .blue }
@discardableResult public func mutate(color: MyGame_Example_Color) -> Bool {let o = _accessor.offset(VTOFFSET.color.v); return _accessor.mutate(color.rawValue, index: o) } @discardableResult public func mutate(color: MyGame_Example_Color) -> Bool {let o = _accessor.offset(VTOFFSET.color.v); return _accessor.mutate(color.rawValue, index: o) }
public var testType: MyGame_Example_Any_ { let o = _accessor.offset(VTOFFSET.testType.v); return o == 0 ? .none_ : MyGame_Example_Any_(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ } public var testType: MyGame_Example_Any_ { let o = _accessor.offset(VTOFFSET.testType.v); return o == 0 ? .none_ : MyGame_Example_Any_(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ }
@@ -1205,6 +1206,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public var test4Count: Int32 { let o = _accessor.offset(VTOFFSET.test4.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var test4Count: Int32 { let o = _accessor.offset(VTOFFSET.test4.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func test4(at index: Int32) -> MyGame_Example_Test? { let o = _accessor.offset(VTOFFSET.test4.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Example_Test.self, offset: _accessor.vector(at: o) + index * 4) } public func test4(at index: Int32) -> MyGame_Example_Test? { let o = _accessor.offset(VTOFFSET.test4.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Example_Test.self, offset: _accessor.vector(at: o) + index * 4) }
public func mutableTest4(at index: Int32) -> MyGame_Example_Test_Mutable? { let o = _accessor.offset(VTOFFSET.test4.v); return o == 0 ? nil : MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 4) } public func mutableTest4(at index: Int32) -> MyGame_Example_Test_Mutable? { let o = _accessor.offset(VTOFFSET.test4.v); return o == 0 ? nil : MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 4) }
public func withUnsafePointerToTest4<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.test4.v, body: body) }
public var hasTestarrayofstring: Bool { let o = _accessor.offset(VTOFFSET.testarrayofstring.v); return o == 0 ? false : true } public var hasTestarrayofstring: Bool { let o = _accessor.offset(VTOFFSET.testarrayofstring.v); return o == 0 ? false : true }
public var testarrayofstringCount: Int32 { let o = _accessor.offset(VTOFFSET.testarrayofstring.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var testarrayofstringCount: Int32 { let o = _accessor.offset(VTOFFSET.testarrayofstring.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func testarrayofstring(at index: Int32) -> String? { let o = _accessor.offset(VTOFFSET.testarrayofstring.v); return o == 0 ? nil : _accessor.directString(at: _accessor.vector(at: o) + index * 4) } public func testarrayofstring(at index: Int32) -> String? { let o = _accessor.offset(VTOFFSET.testarrayofstring.v); return o == 0 ? nil : _accessor.directString(at: _accessor.vector(at: o) + index * 4) }
@@ -1220,6 +1222,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func testnestedflatbuffer(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) } public func testnestedflatbuffer(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) }
public var testnestedflatbuffer: [UInt8] { return _accessor.getVector(at: VTOFFSET.testnestedflatbuffer.v) ?? [] } public var testnestedflatbuffer: [UInt8] { return _accessor.getVector(at: VTOFFSET.testnestedflatbuffer.v) ?? [] }
public func mutate(testnestedflatbuffer: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return _accessor.directMutate(testnestedflatbuffer, index: _accessor.vector(at: o) + index * 1) } public func mutate(testnestedflatbuffer: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return _accessor.directMutate(testnestedflatbuffer, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToTestnestedflatbuffer<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.testnestedflatbuffer.v, body: body) }
public var testempty: MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.testempty.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) } public var testempty: MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.testempty.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) }
public var testbool: Bool { let o = _accessor.offset(VTOFFSET.testbool.v); return o == 0 ? false : _accessor.readBuffer(of: Bool.self, at: o) } public var testbool: Bool { let o = _accessor.offset(VTOFFSET.testbool.v); return o == 0 ? false : _accessor.readBuffer(of: Bool.self, at: o) }
@discardableResult public func mutate(testbool: Bool) -> Bool {let o = _accessor.offset(VTOFFSET.testbool.v); return _accessor.mutate(testbool, index: o) } @discardableResult public func mutate(testbool: Bool) -> Bool {let o = _accessor.offset(VTOFFSET.testbool.v); return _accessor.mutate(testbool, index: o) }
@@ -1244,6 +1247,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func testarrayofbools(at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testarrayofbools.v); return o == 0 ? true : _accessor.directRead(of: Bool.self, offset: _accessor.vector(at: o) + index * 1) } public func testarrayofbools(at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testarrayofbools.v); return o == 0 ? true : _accessor.directRead(of: Bool.self, offset: _accessor.vector(at: o) + index * 1) }
public var testarrayofbools: [Bool] { return _accessor.getVector(at: VTOFFSET.testarrayofbools.v) ?? [] } public var testarrayofbools: [Bool] { return _accessor.getVector(at: VTOFFSET.testarrayofbools.v) ?? [] }
public func mutate(testarrayofbools: Bool, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testarrayofbools.v); return _accessor.directMutate(testarrayofbools, index: _accessor.vector(at: o) + index * 1) } public func mutate(testarrayofbools: Bool, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testarrayofbools.v); return _accessor.directMutate(testarrayofbools, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToTestarrayofbools<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.testarrayofbools.v, body: body) }
public var testf: Float32 { let o = _accessor.offset(VTOFFSET.testf.v); return o == 0 ? 3.14159 : _accessor.readBuffer(of: Float32.self, at: o) } public var testf: Float32 { let o = _accessor.offset(VTOFFSET.testf.v); return o == 0 ? 3.14159 : _accessor.readBuffer(of: Float32.self, at: o) }
@discardableResult public func mutate(testf: Float32) -> Bool {let o = _accessor.offset(VTOFFSET.testf.v); return _accessor.mutate(testf, index: o) } @discardableResult public func mutate(testf: Float32) -> Bool {let o = _accessor.offset(VTOFFSET.testf.v); return _accessor.mutate(testf, index: o) }
public var testf2: Float32 { let o = _accessor.offset(VTOFFSET.testf2.v); return o == 0 ? 3.0 : _accessor.readBuffer(of: Float32.self, at: o) } public var testf2: Float32 { let o = _accessor.offset(VTOFFSET.testf2.v); return o == 0 ? 3.0 : _accessor.readBuffer(of: Float32.self, at: o) }
@@ -1257,25 +1261,30 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public var testarrayofsortedstructCount: Int32 { let o = _accessor.offset(VTOFFSET.testarrayofsortedstruct.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var testarrayofsortedstructCount: Int32 { let o = _accessor.offset(VTOFFSET.testarrayofsortedstruct.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func testarrayofsortedstruct(at index: Int32) -> MyGame_Example_Ability? { let o = _accessor.offset(VTOFFSET.testarrayofsortedstruct.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Example_Ability.self, offset: _accessor.vector(at: o) + index * 8) } public func testarrayofsortedstruct(at index: Int32) -> MyGame_Example_Ability? { let o = _accessor.offset(VTOFFSET.testarrayofsortedstruct.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Example_Ability.self, offset: _accessor.vector(at: o) + index * 8) }
public func mutableTestarrayofsortedstruct(at index: Int32) -> MyGame_Example_Ability_Mutable? { let o = _accessor.offset(VTOFFSET.testarrayofsortedstruct.v); return o == 0 ? nil : MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 8) } public func mutableTestarrayofsortedstruct(at index: Int32) -> MyGame_Example_Ability_Mutable? { let o = _accessor.offset(VTOFFSET.testarrayofsortedstruct.v); return o == 0 ? nil : MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToTestarrayofsortedstruct<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.testarrayofsortedstruct.v, body: body) }
public var hasFlex: Bool { let o = _accessor.offset(VTOFFSET.flex.v); return o == 0 ? false : true } public var hasFlex: Bool { let o = _accessor.offset(VTOFFSET.flex.v); return o == 0 ? false : true }
public var flexCount: Int32 { let o = _accessor.offset(VTOFFSET.flex.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var flexCount: Int32 { let o = _accessor.offset(VTOFFSET.flex.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func flex(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.flex.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) } public func flex(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.flex.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) }
public var flex: [UInt8] { return _accessor.getVector(at: VTOFFSET.flex.v) ?? [] } public var flex: [UInt8] { return _accessor.getVector(at: VTOFFSET.flex.v) ?? [] }
public func mutate(flex: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.flex.v); return _accessor.directMutate(flex, index: _accessor.vector(at: o) + index * 1) } public func mutate(flex: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.flex.v); return _accessor.directMutate(flex, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToFlex<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.flex.v, body: body) }
public var hasTest5: Bool { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? false : true } public var hasTest5: Bool { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? false : true }
public var test5Count: Int32 { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var test5Count: Int32 { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func test5(at index: Int32) -> MyGame_Example_Test? { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Example_Test.self, offset: _accessor.vector(at: o) + index * 4) } public func test5(at index: Int32) -> MyGame_Example_Test? { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Example_Test.self, offset: _accessor.vector(at: o) + index * 4) }
public func mutableTest5(at index: Int32) -> MyGame_Example_Test_Mutable? { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? nil : MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 4) } public func mutableTest5(at index: Int32) -> MyGame_Example_Test_Mutable? { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? nil : MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 4) }
public func withUnsafePointerToTest5<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.test5.v, body: body) }
public var hasVectorOfLongs: Bool { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return o == 0 ? false : true } public var hasVectorOfLongs: Bool { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return o == 0 ? false : true }
public var vectorOfLongsCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var vectorOfLongsCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func vectorOfLongs(at index: Int32) -> Int64 { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return o == 0 ? 0 : _accessor.directRead(of: Int64.self, offset: _accessor.vector(at: o) + index * 8) } public func vectorOfLongs(at index: Int32) -> Int64 { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return o == 0 ? 0 : _accessor.directRead(of: Int64.self, offset: _accessor.vector(at: o) + index * 8) }
public var vectorOfLongs: [Int64] { return _accessor.getVector(at: VTOFFSET.vectorOfLongs.v) ?? [] } public var vectorOfLongs: [Int64] { return _accessor.getVector(at: VTOFFSET.vectorOfLongs.v) ?? [] }
public func mutate(vectorOfLongs: Int64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return _accessor.directMutate(vectorOfLongs, index: _accessor.vector(at: o) + index * 8) } public func mutate(vectorOfLongs: Int64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return _accessor.directMutate(vectorOfLongs, index: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToVectorOfLongs<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.vectorOfLongs.v, body: body) }
public var hasVectorOfDoubles: Bool { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? false : true } public var hasVectorOfDoubles: Bool { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? false : true }
public var vectorOfDoublesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var vectorOfDoublesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func vectorOfDoubles(at index: Int32) -> Double { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? 0 : _accessor.directRead(of: Double.self, offset: _accessor.vector(at: o) + index * 8) } public func vectorOfDoubles(at index: Int32) -> Double { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? 0 : _accessor.directRead(of: Double.self, offset: _accessor.vector(at: o) + index * 8) }
public var vectorOfDoubles: [Double] { return _accessor.getVector(at: VTOFFSET.vectorOfDoubles.v) ?? [] } public var vectorOfDoubles: [Double] { return _accessor.getVector(at: VTOFFSET.vectorOfDoubles.v) ?? [] }
public func mutate(vectorOfDoubles: Double, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return _accessor.directMutate(vectorOfDoubles, index: _accessor.vector(at: o) + index * 8) } public func mutate(vectorOfDoubles: Double, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return _accessor.directMutate(vectorOfDoubles, index: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToVectorOfDoubles<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.vectorOfDoubles.v, body: body) }
public var parentNamespaceTest: MyGame_InParentNamespace? { let o = _accessor.offset(VTOFFSET.parentNamespaceTest.v); return o == 0 ? nil : MyGame_InParentNamespace(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) } public var parentNamespaceTest: MyGame_InParentNamespace? { let o = _accessor.offset(VTOFFSET.parentNamespaceTest.v); return o == 0 ? nil : MyGame_InParentNamespace(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) }
public var hasVectorOfReferrables: Bool { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? false : true } public var hasVectorOfReferrables: Bool { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? false : true }
public var vectorOfReferrablesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var vectorOfReferrablesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? 0 : _accessor.vector(count: o) }
@@ -1288,6 +1297,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func vectorOfWeakReferences(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.vectorOfWeakReferences.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) } public func vectorOfWeakReferences(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.vectorOfWeakReferences.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) }
public var vectorOfWeakReferences: [UInt64] { return _accessor.getVector(at: VTOFFSET.vectorOfWeakReferences.v) ?? [] } public var vectorOfWeakReferences: [UInt64] { return _accessor.getVector(at: VTOFFSET.vectorOfWeakReferences.v) ?? [] }
public func mutate(vectorOfWeakReferences: UInt64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfWeakReferences.v); return _accessor.directMutate(vectorOfWeakReferences, index: _accessor.vector(at: o) + index * 8) } public func mutate(vectorOfWeakReferences: UInt64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfWeakReferences.v); return _accessor.directMutate(vectorOfWeakReferences, index: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToVectorOfWeakReferences<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.vectorOfWeakReferences.v, body: body) }
public var hasVectorOfStrongReferrables: Bool { let o = _accessor.offset(VTOFFSET.vectorOfStrongReferrables.v); return o == 0 ? false : true } public var hasVectorOfStrongReferrables: Bool { let o = _accessor.offset(VTOFFSET.vectorOfStrongReferrables.v); return o == 0 ? false : true }
public var vectorOfStrongReferrablesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfStrongReferrables.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var vectorOfStrongReferrablesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfStrongReferrables.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func vectorOfStrongReferrables(at index: Int32) -> MyGame_Example_Referrable? { let o = _accessor.offset(VTOFFSET.vectorOfStrongReferrables.v); return o == 0 ? nil : MyGame_Example_Referrable(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) } public func vectorOfStrongReferrables(at index: Int32) -> MyGame_Example_Referrable? { let o = _accessor.offset(VTOFFSET.vectorOfStrongReferrables.v); return o == 0 ? nil : MyGame_Example_Referrable(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) }
@@ -1299,6 +1309,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func vectorOfCoOwningReferences(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.vectorOfCoOwningReferences.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) } public func vectorOfCoOwningReferences(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.vectorOfCoOwningReferences.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) }
public var vectorOfCoOwningReferences: [UInt64] { return _accessor.getVector(at: VTOFFSET.vectorOfCoOwningReferences.v) ?? [] } public var vectorOfCoOwningReferences: [UInt64] { return _accessor.getVector(at: VTOFFSET.vectorOfCoOwningReferences.v) ?? [] }
public func mutate(vectorOfCoOwningReferences: UInt64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfCoOwningReferences.v); return _accessor.directMutate(vectorOfCoOwningReferences, index: _accessor.vector(at: o) + index * 8) } public func mutate(vectorOfCoOwningReferences: UInt64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfCoOwningReferences.v); return _accessor.directMutate(vectorOfCoOwningReferences, index: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToVectorOfCoOwningReferences<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.vectorOfCoOwningReferences.v, body: body) }
public var nonOwningReference: UInt64 { let o = _accessor.offset(VTOFFSET.nonOwningReference.v); return o == 0 ? 0 : _accessor.readBuffer(of: UInt64.self, at: o) } public var nonOwningReference: UInt64 { let o = _accessor.offset(VTOFFSET.nonOwningReference.v); return o == 0 ? 0 : _accessor.readBuffer(of: UInt64.self, at: o) }
@discardableResult public func mutate(nonOwningReference: UInt64) -> Bool {let o = _accessor.offset(VTOFFSET.nonOwningReference.v); return _accessor.mutate(nonOwningReference, index: o) } @discardableResult public func mutate(nonOwningReference: UInt64) -> Bool {let o = _accessor.offset(VTOFFSET.nonOwningReference.v); return _accessor.mutate(nonOwningReference, index: o) }
public var hasVectorOfNonOwningReferences: Bool { let o = _accessor.offset(VTOFFSET.vectorOfNonOwningReferences.v); return o == 0 ? false : true } public var hasVectorOfNonOwningReferences: Bool { let o = _accessor.offset(VTOFFSET.vectorOfNonOwningReferences.v); return o == 0 ? false : true }
@@ -1306,6 +1317,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func vectorOfNonOwningReferences(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.vectorOfNonOwningReferences.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) } public func vectorOfNonOwningReferences(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.vectorOfNonOwningReferences.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) }
public var vectorOfNonOwningReferences: [UInt64] { return _accessor.getVector(at: VTOFFSET.vectorOfNonOwningReferences.v) ?? [] } public var vectorOfNonOwningReferences: [UInt64] { return _accessor.getVector(at: VTOFFSET.vectorOfNonOwningReferences.v) ?? [] }
public func mutate(vectorOfNonOwningReferences: UInt64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfNonOwningReferences.v); return _accessor.directMutate(vectorOfNonOwningReferences, index: _accessor.vector(at: o) + index * 8) } public func mutate(vectorOfNonOwningReferences: UInt64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfNonOwningReferences.v); return _accessor.directMutate(vectorOfNonOwningReferences, index: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToVectorOfNonOwningReferences<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.vectorOfNonOwningReferences.v, body: body) }
public var anyUniqueType: MyGame_Example_AnyUniqueAliases { let o = _accessor.offset(VTOFFSET.anyUniqueType.v); return o == 0 ? .none_ : MyGame_Example_AnyUniqueAliases(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ } public var anyUniqueType: MyGame_Example_AnyUniqueAliases { let o = _accessor.offset(VTOFFSET.anyUniqueType.v); return o == 0 ? .none_ : MyGame_Example_AnyUniqueAliases(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ }
public func anyUnique<T: FlatbuffersInitializable>(type: T.Type) -> T? { let o = _accessor.offset(VTOFFSET.anyUnique.v); return o == 0 ? nil : _accessor.union(o) } public func anyUnique<T: FlatbuffersInitializable>(type: T.Type) -> T? { let o = _accessor.offset(VTOFFSET.anyUnique.v); return o == 0 ? nil : _accessor.union(o) }
public var anyAmbiguousType: MyGame_Example_AnyAmbiguousAliases { let o = _accessor.offset(VTOFFSET.anyAmbiguousType.v); return o == 0 ? .none_ : MyGame_Example_AnyAmbiguousAliases(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ } public var anyAmbiguousType: MyGame_Example_AnyAmbiguousAliases { let o = _accessor.offset(VTOFFSET.anyAmbiguousType.v); return o == 0 ? .none_ : MyGame_Example_AnyAmbiguousAliases(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ }
@@ -1320,6 +1332,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func testrequirednestedflatbuffer(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.testrequirednestedflatbuffer.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) } public func testrequirednestedflatbuffer(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.testrequirednestedflatbuffer.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) }
public var testrequirednestedflatbuffer: [UInt8] { return _accessor.getVector(at: VTOFFSET.testrequirednestedflatbuffer.v) ?? [] } public var testrequirednestedflatbuffer: [UInt8] { return _accessor.getVector(at: VTOFFSET.testrequirednestedflatbuffer.v) ?? [] }
public func mutate(testrequirednestedflatbuffer: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testrequirednestedflatbuffer.v); return _accessor.directMutate(testrequirednestedflatbuffer, index: _accessor.vector(at: o) + index * 1) } public func mutate(testrequirednestedflatbuffer: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testrequirednestedflatbuffer.v); return _accessor.directMutate(testrequirednestedflatbuffer, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToTestrequirednestedflatbuffer<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.testrequirednestedflatbuffer.v, body: body) }
public var hasScalarKeySortedTables: Bool { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? false : true } public var hasScalarKeySortedTables: Bool { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? false : true }
public var scalarKeySortedTablesCount: Int32 { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var scalarKeySortedTablesCount: Int32 { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func scalarKeySortedTables(at index: Int32) -> MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) } public func scalarKeySortedTables(at index: Int32) -> MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) }
@@ -1549,7 +1562,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
} }
public static func sortVectorOfMonster(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset { public static func sortVectorOfMonster(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset {
var off = offsets var off = offsets
off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 10, fbb: fbb.buffer), Table.offset(Int32($0.o), vOffset: 10, fbb: fbb.buffer), fbb: fbb.buffer) < 0 } off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 10, fbb: &fbb), Table.offset(Int32($0.o), vOffset: 10, fbb: &fbb), fbb: &fbb) < 0 }
return fbb.createVector(ofOffsets: off) return fbb.createVector(ofOffsets: off)
} }
fileprivate static func lookupByKey(vector: Int32, key: String, fbb: ByteBuffer) -> MyGame_Example_Monster? { fileprivate static func lookupByKey(vector: Int32, key: String, fbb: ByteBuffer) -> MyGame_Example_Monster? {
@@ -2456,11 +2469,13 @@ public struct MyGame_Example_TypeAliases: FlatBufferObject, Verifiable, ObjectAP
public func v8(at index: Int32) -> Int8 { let o = _accessor.offset(VTOFFSET.v8.v); return o == 0 ? 0 : _accessor.directRead(of: Int8.self, offset: _accessor.vector(at: o) + index * 1) } public func v8(at index: Int32) -> Int8 { let o = _accessor.offset(VTOFFSET.v8.v); return o == 0 ? 0 : _accessor.directRead(of: Int8.self, offset: _accessor.vector(at: o) + index * 1) }
public var v8: [Int8] { return _accessor.getVector(at: VTOFFSET.v8.v) ?? [] } public var v8: [Int8] { return _accessor.getVector(at: VTOFFSET.v8.v) ?? [] }
public func mutate(v8: Int8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.v8.v); return _accessor.directMutate(v8, index: _accessor.vector(at: o) + index * 1) } public func mutate(v8: Int8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.v8.v); return _accessor.directMutate(v8, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToV8<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.v8.v, body: body) }
public var hasVf64: Bool { let o = _accessor.offset(VTOFFSET.vf64.v); return o == 0 ? false : true } public var hasVf64: Bool { let o = _accessor.offset(VTOFFSET.vf64.v); return o == 0 ? false : true }
public var vf64Count: Int32 { let o = _accessor.offset(VTOFFSET.vf64.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var vf64Count: Int32 { let o = _accessor.offset(VTOFFSET.vf64.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func vf64(at index: Int32) -> Double { let o = _accessor.offset(VTOFFSET.vf64.v); return o == 0 ? 0 : _accessor.directRead(of: Double.self, offset: _accessor.vector(at: o) + index * 8) } public func vf64(at index: Int32) -> Double { let o = _accessor.offset(VTOFFSET.vf64.v); return o == 0 ? 0 : _accessor.directRead(of: Double.self, offset: _accessor.vector(at: o) + index * 8) }
public var vf64: [Double] { return _accessor.getVector(at: VTOFFSET.vf64.v) ?? [] } public var vf64: [Double] { return _accessor.getVector(at: VTOFFSET.vf64.v) ?? [] }
public func mutate(vf64: Double, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vf64.v); return _accessor.directMutate(vf64, index: _accessor.vector(at: o) + index * 8) } public func mutate(vf64: Double, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vf64.v); return _accessor.directMutate(vf64, index: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToVf64<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.vf64.v, body: body) }
public static func startTypeAliases(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 12) } public static func startTypeAliases(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 12) }
public static func add(i8: Int8, _ fbb: inout FlatBufferBuilder) { fbb.add(element: i8, def: 0, at: VTOFFSET.i8.p) } public static func add(i8: Int8, _ fbb: inout FlatBufferBuilder) { fbb.add(element: i8, def: 0, at: VTOFFSET.i8.p) }
public static func add(u8: UInt8, _ fbb: inout FlatBufferBuilder) { fbb.add(element: u8, def: 0, at: VTOFFSET.u8.p) } public static func add(u8: UInt8, _ fbb: inout FlatBufferBuilder) { fbb.add(element: u8, def: 0, at: VTOFFSET.u8.p) }

View File

@@ -228,6 +228,7 @@ public struct Monster: FlatBufferObject, Verifiable {
public var inventoryCount: Int32 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var inventoryCount: Int32 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func inventory(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) } public func inventory(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) }
public var inventory: [UInt8] { return _accessor.getVector(at: VTOFFSET.inventory.v) ?? [] } public var inventory: [UInt8] { return _accessor.getVector(at: VTOFFSET.inventory.v) ?? [] }
public func withUnsafePointerToInventory<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.inventory.v, body: body) }
public var color: Color { let o = _accessor.offset(VTOFFSET.color.v); return o == 0 ? .blue : Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .blue } public var color: Color { let o = _accessor.offset(VTOFFSET.color.v); return o == 0 ? .blue : Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .blue }
public static func startMonster(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 7) } public static func startMonster(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 7) }
public static func add(pos: Vec3?, _ fbb: inout FlatBufferBuilder) { guard let pos = pos else { return }; fbb.create(struct: pos, position: VTOFFSET.pos.p) } public static func add(pos: Vec3?, _ fbb: inout FlatBufferBuilder) { guard let pos = pos else { return }; fbb.create(struct: pos, position: VTOFFSET.pos.p) }
@@ -260,7 +261,7 @@ public struct Monster: FlatBufferObject, Verifiable {
} }
public static func sortVectorOfMonster(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset { public static func sortVectorOfMonster(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset {
var off = offsets var off = offsets
off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 10, fbb: fbb.buffer), Table.offset(Int32($0.o), vOffset: 10, fbb: fbb.buffer), fbb: fbb.buffer) < 0 } off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 10, fbb: &fbb), Table.offset(Int32($0.o), vOffset: 10, fbb: &fbb), fbb: &fbb) < 0 }
return fbb.createVector(ofOffsets: off) return fbb.createVector(ofOffsets: off)
} }
fileprivate static func lookupByKey(vector: Int32, key: String, fbb: ByteBuffer) -> Monster? { fileprivate static func lookupByKey(vector: Int32, key: String, fbb: ByteBuffer) -> Monster? {

View File

@@ -0,0 +1,131 @@
/*
* Copyright 2024 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import XCTest
@testable import FlatBuffers
final class ByteBufferTests: XCTestCase {
func testCopyingMemory() {
let count = 100
let ptr = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1)
let byteBuffer = ByteBuffer(copyingMemoryBound: ptr, capacity: count)
byteBuffer.withUnsafeBytes { memory in
XCTAssertNotEqual(memory.baseAddress, ptr)
}
}
func testSamePointer() {
let count = 100
let ptr = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1)
let byteBuffer = ByteBuffer(assumingMemoryBound: ptr, capacity: count)
byteBuffer.withUnsafeBytes { memory in
XCTAssertEqual(memory.baseAddress!, ptr)
}
}
func testSameDataPtr() {
let count = 100
let ptr = Data(repeating: 0, count: count)
let byteBuffer = ByteBuffer(data: ptr)
byteBuffer.withUnsafeBytes { memory in
ptr.withUnsafeBytes { ptr in
XCTAssertEqual(memory.baseAddress!, ptr)
}
}
}
func testSameArrayPtr() {
let count = 100
let ptr: [UInt8] = Array(repeating: 0, count: count)
let byteBuffer = ByteBuffer(bytes: ptr)
ptr.withUnsafeBytes { ptr in
byteBuffer.withUnsafeBytes { memory in
XCTAssertEqual(memory.baseAddress, ptr.baseAddress)
}
}
}
func testReadingDoubleBuffer() {
let count = 8
let array: [Double] = Array(repeating: 8.8, count: count)
var oldBuffer = _InternalByteBuffer(initialSize: 16)
oldBuffer.push(elements: array)
let bytes: [Byte] = oldBuffer.withUnsafeBytes { bytes in
Array(bytes)
}
let byteBuffer = ByteBuffer(bytes: bytes)
byteBuffer.withUnsafePointerToSlice(index: 0, count: count) { ptr in
XCTAssertEqual(ptr.count, count)
bytes.withUnsafeBufferPointer {
XCTAssertEqual(
UnsafeRawPointer($0.baseAddress),
UnsafeRawPointer(ptr.baseAddress))
}
}
}
func testReadingNativeStructs() {
let array = [
MyGame_Example_Vec3(
x: 3.2,
y: 3.2,
z: 3.2,
test1: 8,
test2: .red,
test3: MyGame_Example_Test(a: 8, b: 8)),
MyGame_Example_Vec3(
x: 3.2,
y: 3.2,
z: 3.2,
test1: 8,
test2: .green,
test3: MyGame_Example_Test(a: 16, b: 16)),
MyGame_Example_Vec3(
x: 3.2,
y: 3.2,
z: 3.2,
test1: 8,
test2: .blue,
test3: MyGame_Example_Test(a: 32, b: 32)),
]
let count = array.count
var oldBuffer = _InternalByteBuffer(initialSize: 16)
oldBuffer.push(elements: array)
let bytes: [Byte] = oldBuffer.withUnsafeBytes { bytes in
Array(bytes)
}
let byteBuffer = ByteBuffer(bytes: bytes)
byteBuffer
.withUnsafePointerToSlice(index: 0, count: count) { bufferPointer in
XCTAssertEqual(bufferPointer.count, count)
bytes.withUnsafeBufferPointer { ptr in
XCTAssertEqual(
UnsafeRawPointer(ptr.baseAddress),
UnsafeRawPointer(bufferPointer.baseAddress))
}
}
}
}
private struct TestNativeStructs: NativeStruct {
let x: Double
let y: Double
let z: Int
}
extension MyGame_Example_Color: CaseIterable {
public static var allCases: [MyGame_Example_Color] = [.red, .blue, .green]
}

View File

@@ -39,6 +39,7 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
.appendingPathExtension("mon") .appendingPathExtension("mon")
let data = try! Data(contentsOf: url) let data = try! Data(contentsOf: url)
let _data = ByteBuffer(data: data) let _data = ByteBuffer(data: data)
readVerifiedMonster(fb: _data) readVerifiedMonster(fb: _data)
} }
@@ -48,6 +49,7 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
XCTAssertEqual(bytes.sizedByteArray, [48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0]) XCTAssertEqual(bytes.sizedByteArray, [48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0])
// swiftformat:enable all // swiftformat:enable all
var buffer = bytes.buffer var buffer = bytes.buffer
print(buffer)
let monster: MyGame_Example_Monster = getRoot(byteBuffer: &buffer) let monster: MyGame_Example_Monster = getRoot(byteBuffer: &buffer)
readMonster(monster: monster) readMonster(monster: monster)
mutateMonster(fb: bytes.buffer) mutateMonster(fb: bytes.buffer)
@@ -130,6 +132,7 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
var fbb = FlatBufferBuilder(initialSize: 1) var fbb = FlatBufferBuilder(initialSize: 1)
let name = fbb.create(string: "Frodo") let name = fbb.create(string: "Frodo")
let bools = fbb.createVector(boolArray) let bools = fbb.createVector(boolArray)
let root = Monster.createMonster( let root = Monster.createMonster(
&fbb, &fbb,
nameOffset: name, nameOffset: name,
@@ -176,8 +179,7 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
let unlignedPtr = UnsafeMutableRawPointer(baseAddress) let unlignedPtr = UnsafeMutableRawPointer(baseAddress)
var bytes = ByteBuffer( var bytes = ByteBuffer(
assumingMemoryBound: unlignedPtr, assumingMemoryBound: unlignedPtr,
capacity: ptr.count - 1, capacity: ptr.count - 1)
allowReadingUnalignedBuffers: true)
var monster: Monster = getRoot(byteBuffer: &bytes) var monster: Monster = getRoot(byteBuffer: &bytes)
self.readFlatbufferMonster(monster: &monster) self.readFlatbufferMonster(monster: &monster)
return true return true
@@ -186,7 +188,7 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
XCTAssertEqual(testUnaligned(), true) XCTAssertEqual(testUnaligned(), true)
} }
func testCopyUnalignedToAlignedBuffers() { func testReadingRemovedSizeUnalignedBuffer() {
// Aligned read // Aligned read
let fbb = createMonster(withPrefix: true) let fbb = createMonster(withPrefix: true)
let testUnaligned: () -> Bool = { let testUnaligned: () -> Bool = {
@@ -201,8 +203,7 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
let unlignedPtr = UnsafeMutableRawPointer(baseAddress) let unlignedPtr = UnsafeMutableRawPointer(baseAddress)
let bytes = ByteBuffer( let bytes = ByteBuffer(
assumingMemoryBound: unlignedPtr, assumingMemoryBound: unlignedPtr,
capacity: ptr.count - 1, capacity: ptr.count - 1)
allowReadingUnalignedBuffers: false)
var newBuf = FlatBuffersUtils.removeSizePrefix(bb: bytes) var newBuf = FlatBuffersUtils.removeSizePrefix(bb: bytes)
var monster: Monster = getRoot(byteBuffer: &newBuf) var monster: Monster = getRoot(byteBuffer: &newBuf)
self.readFlatbufferMonster(monster: &monster) self.readFlatbufferMonster(monster: &monster)
@@ -212,6 +213,37 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
XCTAssertEqual(testUnaligned(), true) XCTAssertEqual(testUnaligned(), true)
} }
func testCreateMessage() {
let fbb = createMonster(withPrefix: false)
let byteBuffer = fbb.buffer
let firstMessage = Message<Monster>(byteBuffer: byteBuffer)
firstMessage.withUnsafeReadableBytes { ptr in
var bytes = ByteBuffer(contiguousBytes: ptr, count: ptr.count)
var monster: Monster = getRoot(byteBuffer: &bytes)
self.readFlatbufferMonster(monster: &monster)
}
let secondByteBuffer = fbb.sizedBuffer
let secondMessage = Message<Monster>(byteBuffer: secondByteBuffer)
secondMessage.withUnsafeReadableBytes { ptr in
var bytes = ByteBuffer(contiguousBytes: ptr, count: ptr.count)
var monster: Monster = getRoot(byteBuffer: &bytes)
self.readFlatbufferMonster(monster: &monster)
}
}
func testForceRetainedObject() {
let byteBuffer = {
// swiftformat:disable all
var data: Data? = Data([48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0])
// swiftformat:enable all
let buffer = ByteBuffer(data: data!)
data = nil
return buffer
}()
readVerifiedMonster(fb: byteBuffer)
}
func readMonster(monster: Monster) { func readMonster(monster: Monster) {
var monster = monster var monster = monster
readFlatbufferMonster(monster: &monster) readFlatbufferMonster(monster: &monster)
@@ -286,6 +318,7 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
func mutateMonster(fb: ByteBuffer) { func mutateMonster(fb: ByteBuffer) {
var fb = fb var fb = fb
print(fb)
let monster: Monster = getRoot(byteBuffer: &fb) let monster: Monster = getRoot(byteBuffer: &fb)
XCTAssertFalse(monster.mutate(mana: 10)) XCTAssertFalse(monster.mutate(mana: 10))
XCTAssertEqual(monster.testarrayoftables(at: 0)?.name, "Barney") XCTAssertEqual(monster.testarrayoftables(at: 0)?.name, "Barney")
@@ -352,6 +385,15 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
sum += monster.inventory(at: i) sum += monster.inventory(at: i)
} }
XCTAssertEqual(sum, 10) XCTAssertEqual(sum, 10)
monster.withUnsafePointerToInventory { ptr in
var sum: UInt8 = 0
for pointee in ptr.startIndex..<ptr.endIndex {
sum += ptr[pointee]
}
XCTAssertEqual(sum, 10)
}
XCTAssertEqual(monster.test4Count, 2) XCTAssertEqual(monster.test4Count, 2)
let test0 = monster.test4(at: 0) let test0 = monster.test4(at: 0)
@@ -366,6 +408,23 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
} }
XCTAssertEqual(sum0 + sum1, 100) XCTAssertEqual(sum0 + sum1, 100)
monster.withUnsafePointerToTest4 { ptr in
guard let ptr = ptr.baseAddress else { return }
let bindedMemory: UnsafeBufferPointer<MyGame_Example_Test> =
UnsafeBufferPointer(
start: ptr.bindMemory(
to: MyGame_Example_Test.self,
capacity: Int(monster.test4Count)),
count: Int(monster.test4Count))
var pointerSum = 0
for pointee in bindedMemory.startIndex..<bindedMemory.endIndex {
pointerSum += Int(bindedMemory[pointee].a) +
Int(bindedMemory[pointee].b)
}
XCTAssertEqual(pointerSum, 100)
}
let mutableTest0 = monster.mutableTest4(at: 0) let mutableTest0 = monster.mutableTest4(at: 0)
let mutableTest1 = monster.mutableTest4(at: 1) let mutableTest1 = monster.mutableTest4(at: 1)
var sum2 = 0 var sum2 = 0
@@ -498,7 +557,8 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
let monster: Monster = getRoot(byteBuffer: &buffer) let monster: Monster = getRoot(byteBuffer: &buffer)
let values = monster.inventory let values = monster.inventory
XCTAssertEqual(byteArray, values) monster.withUnsafePointerToInventory {
XCTAssertEqual(Array($0), values)
}
} }
} }

View File

@@ -93,7 +93,8 @@ final class FlatBuffersUnionTests: XCTestCase {
// swiftformat:disable all // swiftformat:disable all
XCTAssertEqual(builder.sizedByteArray, [12, 0, 0, 0, 0, 0, 6, 0, 8, 0, 4, 0, 6, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0]) XCTAssertEqual(builder.sizedByteArray, [12, 0, 0, 0, 0, 0, 6, 0, 8, 0, 4, 0, 6, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0])
// swiftformat:enable all // swiftformat:enable all
let monster = ColorsNameSpace.Monster.getRootAsMonster(bb: builder.buffer) let monster = ColorsNameSpace.Monster
.getRootAsMonster(bb: builder.sizedBuffer)
XCTAssertEqual(monster.colorsCount, 2) XCTAssertEqual(monster.colorsCount, 2)
XCTAssertEqual(monster.colors(at: 0), .blue) XCTAssertEqual(monster.colors(at: 0), .blue)
XCTAssertEqual(monster.colors(at: 1), .green) XCTAssertEqual(monster.colors(at: 1), .green)
@@ -122,7 +123,7 @@ final class FlatBuffersUnionTests: XCTestCase {
charactersVectorOffset: characterVector) charactersVectorOffset: characterVector)
Movie.finish(&fb, end: end) Movie.finish(&fb, end: end)
var buffer = fb.buffer var buffer = fb.sizedBuffer
var movie: Movie = getRoot(byteBuffer: &buffer) var movie: Movie = getRoot(byteBuffer: &buffer)
XCTAssertEqual(movie.charactersTypeCount, Int32(characterType.count)) XCTAssertEqual(movie.charactersTypeCount, Int32(characterType.count))
XCTAssertEqual(movie.charactersCount, Int32(characters.count)) XCTAssertEqual(movie.charactersCount, Int32(characters.count))
@@ -152,7 +153,7 @@ final class FlatBuffersUnionTests: XCTestCase {
let newMovie = Movie.pack(&fb, obj: &objc) let newMovie = Movie.pack(&fb, obj: &objc)
fb.finish(offset: newMovie) fb.finish(offset: newMovie)
var _buffer = fb.buffer var _buffer = fb.sizedBuffer
let packedMovie: Movie = getRoot(byteBuffer: &_buffer) let packedMovie: Movie = getRoot(byteBuffer: &_buffer)
XCTAssertEqual( XCTAssertEqual(
@@ -203,7 +204,7 @@ final class FlatBuffersUnionTests: XCTestCase {
let newMovie = Movie.pack(&fb, obj: &objc) let newMovie = Movie.pack(&fb, obj: &objc)
fb.finish(offset: newMovie) fb.finish(offset: newMovie)
var _buffer = fb.buffer var _buffer = fb.sizedBuffer
let packedMovie: Movie = getRoot(byteBuffer: &_buffer) let packedMovie: Movie = getRoot(byteBuffer: &_buffer)
XCTAssertEqual(packedMovie.mainCharacter(type: String.self), string) XCTAssertEqual(packedMovie.mainCharacter(type: String.self), string)
XCTAssertEqual( XCTAssertEqual(

View File

@@ -143,6 +143,18 @@ final class FlatBuffersVectors: XCTestCase {
XCTAssertEqual(msg.hasArray, true) XCTAssertEqual(msg.hasArray, true)
XCTAssertEqual(msg.arrayCount, 3) XCTAssertEqual(msg.arrayCount, 3)
XCTAssertEqual(msg.array, [1, 2, 3]) XCTAssertEqual(msg.array, [1, 2, 3])
let array = msg.withUnsafePointerToArray { ptr in
let ptr: UnsafeBufferPointer<UInt64> = UnsafeBufferPointer(
start: ptr.baseAddress?.bindMemory(
to: UInt64.self,
capacity: Int(msg.arrayCount)),
count: Int(msg.arrayCount))
return Array(ptr)
}
XCTAssertEqual(array, [1, 2, 3])
} }
} }

View File

@@ -20,11 +20,9 @@ import XCTest
final class FlatbuffersVerifierTests: XCTestCase { final class FlatbuffersVerifierTests: XCTestCase {
lazy var validStorage: ByteBuffer.Storage = ByteBuffer.Storage( lazy var validStorage: ByteBuffer.Storage = ByteBuffer.Storage(
count: Int(FlatBufferMaxSize) - 1, count: Int(FlatBufferMaxSize) - 1)
alignment: 1)
lazy var errorStorage: ByteBuffer.Storage = ByteBuffer.Storage( lazy var errorStorage: ByteBuffer.Storage = ByteBuffer.Storage(
count: Int(FlatBufferMaxSize) + 1, count: Int(FlatBufferMaxSize) + 1)
alignment: 1)
var buffer: ByteBuffer! var buffer: ByteBuffer!
@@ -35,7 +33,8 @@ final class FlatbuffersVerifierTests: XCTestCase {
override func setUp() { override func setUp() {
// swiftformat:disable all // swiftformat:disable all
buffer = ByteBuffer(initialSize: 32) let memory = UnsafeMutableRawPointer.allocate(byteCount: 32, alignment: 1)
buffer = ByteBuffer(assumingMemoryBound: memory, capacity: 32)
add(buffer: &buffer, v: 4, p: 16) add(buffer: &buffer, v: 4, p: 16)
add(buffer: &buffer, v: 4, p: 20) add(buffer: &buffer, v: 4, p: 20)
@@ -52,13 +51,15 @@ final class FlatbuffersVerifierTests: XCTestCase {
} }
func testVeriferInitPassing() { func testVeriferInitPassing() {
var buffer = ByteBuffer(initialSize: 0) let memory = UnsafeMutableRawPointer.allocate(byteCount: 8, alignment: 1)
var buffer = ByteBuffer(assumingMemoryBound: memory, capacity: 8)
buffer._storage = validStorage buffer._storage = validStorage
XCTAssertNoThrow(try Verifier(buffer: &buffer)) XCTAssertNoThrow(try Verifier(buffer: &buffer))
} }
func testVeriferInitFailing() { func testVeriferInitFailing() {
var buffer = ByteBuffer(initialSize: 0) let memory = UnsafeMutableRawPointer.allocate(byteCount: 8, alignment: 1)
var buffer = ByteBuffer(assumingMemoryBound: memory, capacity: 8)
buffer._storage = errorStorage buffer._storage = errorStorage
XCTAssertThrowsError(try Verifier(buffer: &buffer)) XCTAssertThrowsError(try Verifier(buffer: &buffer))
} }

View File

@@ -17,8 +17,7 @@ public extension GRPCFlatBufPayload {
self.init(byteBuffer: FlatBuffers.ByteBuffer(contiguousBytes: serializedByteBuffer.readableBytesView, count: serializedByteBuffer.readableBytes)) self.init(byteBuffer: FlatBuffers.ByteBuffer(contiguousBytes: serializedByteBuffer.readableBytesView, count: serializedByteBuffer.readableBytes))
} }
func serialize(into buffer: inout NIO.ByteBuffer) throws { func serialize(into buffer: inout NIO.ByteBuffer) throws {
let buf = UnsafeRawBufferPointer(start: self.rawPointer, count: Int(self.size)) withUnsafeReadableBytes { buffer.writeBytes($0) }
buffer.writeBytes(buf)
} }
} }
extension Message: GRPCFlatBufPayload {} extension Message: GRPCFlatBufPayload {}

View File

@@ -906,7 +906,7 @@ public struct MyGame_Example_Stat: FlatBufferObject, Verifiable, ObjectAPIPacker
} }
public static func sortVectorOfStat(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset { public static func sortVectorOfStat(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset {
var off = offsets var off = offsets
off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 8, fbb: fbb.buffer), Table.offset(Int32($0.o), vOffset: 8, fbb: fbb.buffer), fbb: fbb.buffer) < 0 } off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 8, fbb: &fbb), Table.offset(Int32($0.o), vOffset: 8, fbb: &fbb), fbb: &fbb) < 0 }
return fbb.createVector(ofOffsets: off) return fbb.createVector(ofOffsets: off)
} }
fileprivate static func lookupByKey(vector: Int32, key: UInt16, fbb: ByteBuffer) -> MyGame_Example_Stat? { fileprivate static func lookupByKey(vector: Int32, key: UInt16, fbb: ByteBuffer) -> MyGame_Example_Stat? {
@@ -1033,7 +1033,7 @@ public struct MyGame_Example_Referrable: FlatBufferObject, Verifiable, ObjectAPI
} }
public static func sortVectorOfReferrable(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset { public static func sortVectorOfReferrable(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset {
var off = offsets var off = offsets
off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 4, fbb: fbb.buffer), Table.offset(Int32($0.o), vOffset: 4, fbb: fbb.buffer), fbb: fbb.buffer) < 0 } off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 4, fbb: &fbb), Table.offset(Int32($0.o), vOffset: 4, fbb: &fbb), fbb: &fbb) < 0 }
return fbb.createVector(ofOffsets: off) return fbb.createVector(ofOffsets: off)
} }
fileprivate static func lookupByKey(vector: Int32, key: UInt64, fbb: ByteBuffer) -> MyGame_Example_Referrable? { fileprivate static func lookupByKey(vector: Int32, key: UInt64, fbb: ByteBuffer) -> MyGame_Example_Referrable? {
@@ -1197,6 +1197,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func inventory(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) } public func inventory(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) }
public var inventory: [UInt8] { return _accessor.getVector(at: VTOFFSET.inventory.v) ?? [] } public var inventory: [UInt8] { return _accessor.getVector(at: VTOFFSET.inventory.v) ?? [] }
public func mutate(inventory: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.inventory.v); return _accessor.directMutate(inventory, index: _accessor.vector(at: o) + index * 1) } public func mutate(inventory: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.inventory.v); return _accessor.directMutate(inventory, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToInventory<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.inventory.v, body: body) }
public var color: MyGame_Example_Color { let o = _accessor.offset(VTOFFSET.color.v); return o == 0 ? .blue : MyGame_Example_Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .blue } public var color: MyGame_Example_Color { let o = _accessor.offset(VTOFFSET.color.v); return o == 0 ? .blue : MyGame_Example_Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .blue }
@discardableResult public func mutate(color: MyGame_Example_Color) -> Bool {let o = _accessor.offset(VTOFFSET.color.v); return _accessor.mutate(color.rawValue, index: o) } @discardableResult public func mutate(color: MyGame_Example_Color) -> Bool {let o = _accessor.offset(VTOFFSET.color.v); return _accessor.mutate(color.rawValue, index: o) }
public var testType: MyGame_Example_Any_ { let o = _accessor.offset(VTOFFSET.testType.v); return o == 0 ? .none_ : MyGame_Example_Any_(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ } public var testType: MyGame_Example_Any_ { let o = _accessor.offset(VTOFFSET.testType.v); return o == 0 ? .none_ : MyGame_Example_Any_(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ }
@@ -1205,6 +1206,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public var test4Count: Int32 { let o = _accessor.offset(VTOFFSET.test4.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var test4Count: Int32 { let o = _accessor.offset(VTOFFSET.test4.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func test4(at index: Int32) -> MyGame_Example_Test? { let o = _accessor.offset(VTOFFSET.test4.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Example_Test.self, offset: _accessor.vector(at: o) + index * 4) } public func test4(at index: Int32) -> MyGame_Example_Test? { let o = _accessor.offset(VTOFFSET.test4.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Example_Test.self, offset: _accessor.vector(at: o) + index * 4) }
public func mutableTest4(at index: Int32) -> MyGame_Example_Test_Mutable? { let o = _accessor.offset(VTOFFSET.test4.v); return o == 0 ? nil : MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 4) } public func mutableTest4(at index: Int32) -> MyGame_Example_Test_Mutable? { let o = _accessor.offset(VTOFFSET.test4.v); return o == 0 ? nil : MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 4) }
public func withUnsafePointerToTest4<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.test4.v, body: body) }
public var hasTestarrayofstring: Bool { let o = _accessor.offset(VTOFFSET.testarrayofstring.v); return o == 0 ? false : true } public var hasTestarrayofstring: Bool { let o = _accessor.offset(VTOFFSET.testarrayofstring.v); return o == 0 ? false : true }
public var testarrayofstringCount: Int32 { let o = _accessor.offset(VTOFFSET.testarrayofstring.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var testarrayofstringCount: Int32 { let o = _accessor.offset(VTOFFSET.testarrayofstring.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func testarrayofstring(at index: Int32) -> String? { let o = _accessor.offset(VTOFFSET.testarrayofstring.v); return o == 0 ? nil : _accessor.directString(at: _accessor.vector(at: o) + index * 4) } public func testarrayofstring(at index: Int32) -> String? { let o = _accessor.offset(VTOFFSET.testarrayofstring.v); return o == 0 ? nil : _accessor.directString(at: _accessor.vector(at: o) + index * 4) }
@@ -1220,6 +1222,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func testnestedflatbuffer(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) } public func testnestedflatbuffer(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) }
public var testnestedflatbuffer: [UInt8] { return _accessor.getVector(at: VTOFFSET.testnestedflatbuffer.v) ?? [] } public var testnestedflatbuffer: [UInt8] { return _accessor.getVector(at: VTOFFSET.testnestedflatbuffer.v) ?? [] }
public func mutate(testnestedflatbuffer: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return _accessor.directMutate(testnestedflatbuffer, index: _accessor.vector(at: o) + index * 1) } public func mutate(testnestedflatbuffer: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testnestedflatbuffer.v); return _accessor.directMutate(testnestedflatbuffer, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToTestnestedflatbuffer<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.testnestedflatbuffer.v, body: body) }
public var testempty: MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.testempty.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) } public var testempty: MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.testempty.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) }
public var testbool: Bool { let o = _accessor.offset(VTOFFSET.testbool.v); return o == 0 ? false : _accessor.readBuffer(of: Bool.self, at: o) } public var testbool: Bool { let o = _accessor.offset(VTOFFSET.testbool.v); return o == 0 ? false : _accessor.readBuffer(of: Bool.self, at: o) }
@discardableResult public func mutate(testbool: Bool) -> Bool {let o = _accessor.offset(VTOFFSET.testbool.v); return _accessor.mutate(testbool, index: o) } @discardableResult public func mutate(testbool: Bool) -> Bool {let o = _accessor.offset(VTOFFSET.testbool.v); return _accessor.mutate(testbool, index: o) }
@@ -1244,6 +1247,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func testarrayofbools(at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testarrayofbools.v); return o == 0 ? true : _accessor.directRead(of: Bool.self, offset: _accessor.vector(at: o) + index * 1) } public func testarrayofbools(at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testarrayofbools.v); return o == 0 ? true : _accessor.directRead(of: Bool.self, offset: _accessor.vector(at: o) + index * 1) }
public var testarrayofbools: [Bool] { return _accessor.getVector(at: VTOFFSET.testarrayofbools.v) ?? [] } public var testarrayofbools: [Bool] { return _accessor.getVector(at: VTOFFSET.testarrayofbools.v) ?? [] }
public func mutate(testarrayofbools: Bool, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testarrayofbools.v); return _accessor.directMutate(testarrayofbools, index: _accessor.vector(at: o) + index * 1) } public func mutate(testarrayofbools: Bool, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testarrayofbools.v); return _accessor.directMutate(testarrayofbools, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToTestarrayofbools<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.testarrayofbools.v, body: body) }
public var testf: Float32 { let o = _accessor.offset(VTOFFSET.testf.v); return o == 0 ? 3.14159 : _accessor.readBuffer(of: Float32.self, at: o) } public var testf: Float32 { let o = _accessor.offset(VTOFFSET.testf.v); return o == 0 ? 3.14159 : _accessor.readBuffer(of: Float32.self, at: o) }
@discardableResult public func mutate(testf: Float32) -> Bool {let o = _accessor.offset(VTOFFSET.testf.v); return _accessor.mutate(testf, index: o) } @discardableResult public func mutate(testf: Float32) -> Bool {let o = _accessor.offset(VTOFFSET.testf.v); return _accessor.mutate(testf, index: o) }
public var testf2: Float32 { let o = _accessor.offset(VTOFFSET.testf2.v); return o == 0 ? 3.0 : _accessor.readBuffer(of: Float32.self, at: o) } public var testf2: Float32 { let o = _accessor.offset(VTOFFSET.testf2.v); return o == 0 ? 3.0 : _accessor.readBuffer(of: Float32.self, at: o) }
@@ -1257,25 +1261,30 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public var testarrayofsortedstructCount: Int32 { let o = _accessor.offset(VTOFFSET.testarrayofsortedstruct.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var testarrayofsortedstructCount: Int32 { let o = _accessor.offset(VTOFFSET.testarrayofsortedstruct.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func testarrayofsortedstruct(at index: Int32) -> MyGame_Example_Ability? { let o = _accessor.offset(VTOFFSET.testarrayofsortedstruct.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Example_Ability.self, offset: _accessor.vector(at: o) + index * 8) } public func testarrayofsortedstruct(at index: Int32) -> MyGame_Example_Ability? { let o = _accessor.offset(VTOFFSET.testarrayofsortedstruct.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Example_Ability.self, offset: _accessor.vector(at: o) + index * 8) }
public func mutableTestarrayofsortedstruct(at index: Int32) -> MyGame_Example_Ability_Mutable? { let o = _accessor.offset(VTOFFSET.testarrayofsortedstruct.v); return o == 0 ? nil : MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 8) } public func mutableTestarrayofsortedstruct(at index: Int32) -> MyGame_Example_Ability_Mutable? { let o = _accessor.offset(VTOFFSET.testarrayofsortedstruct.v); return o == 0 ? nil : MyGame_Example_Ability_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToTestarrayofsortedstruct<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.testarrayofsortedstruct.v, body: body) }
public var hasFlex: Bool { let o = _accessor.offset(VTOFFSET.flex.v); return o == 0 ? false : true } public var hasFlex: Bool { let o = _accessor.offset(VTOFFSET.flex.v); return o == 0 ? false : true }
public var flexCount: Int32 { let o = _accessor.offset(VTOFFSET.flex.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var flexCount: Int32 { let o = _accessor.offset(VTOFFSET.flex.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func flex(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.flex.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) } public func flex(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.flex.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) }
public var flex: [UInt8] { return _accessor.getVector(at: VTOFFSET.flex.v) ?? [] } public var flex: [UInt8] { return _accessor.getVector(at: VTOFFSET.flex.v) ?? [] }
public func mutate(flex: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.flex.v); return _accessor.directMutate(flex, index: _accessor.vector(at: o) + index * 1) } public func mutate(flex: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.flex.v); return _accessor.directMutate(flex, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToFlex<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.flex.v, body: body) }
public var hasTest5: Bool { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? false : true } public var hasTest5: Bool { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? false : true }
public var test5Count: Int32 { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var test5Count: Int32 { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func test5(at index: Int32) -> MyGame_Example_Test? { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Example_Test.self, offset: _accessor.vector(at: o) + index * 4) } public func test5(at index: Int32) -> MyGame_Example_Test? { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Example_Test.self, offset: _accessor.vector(at: o) + index * 4) }
public func mutableTest5(at index: Int32) -> MyGame_Example_Test_Mutable? { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? nil : MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 4) } public func mutableTest5(at index: Int32) -> MyGame_Example_Test_Mutable? { let o = _accessor.offset(VTOFFSET.test5.v); return o == 0 ? nil : MyGame_Example_Test_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 4) }
public func withUnsafePointerToTest5<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.test5.v, body: body) }
public var hasVectorOfLongs: Bool { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return o == 0 ? false : true } public var hasVectorOfLongs: Bool { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return o == 0 ? false : true }
public var vectorOfLongsCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var vectorOfLongsCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func vectorOfLongs(at index: Int32) -> Int64 { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return o == 0 ? 0 : _accessor.directRead(of: Int64.self, offset: _accessor.vector(at: o) + index * 8) } public func vectorOfLongs(at index: Int32) -> Int64 { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return o == 0 ? 0 : _accessor.directRead(of: Int64.self, offset: _accessor.vector(at: o) + index * 8) }
public var vectorOfLongs: [Int64] { return _accessor.getVector(at: VTOFFSET.vectorOfLongs.v) ?? [] } public var vectorOfLongs: [Int64] { return _accessor.getVector(at: VTOFFSET.vectorOfLongs.v) ?? [] }
public func mutate(vectorOfLongs: Int64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return _accessor.directMutate(vectorOfLongs, index: _accessor.vector(at: o) + index * 8) } public func mutate(vectorOfLongs: Int64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfLongs.v); return _accessor.directMutate(vectorOfLongs, index: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToVectorOfLongs<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.vectorOfLongs.v, body: body) }
public var hasVectorOfDoubles: Bool { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? false : true } public var hasVectorOfDoubles: Bool { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? false : true }
public var vectorOfDoublesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var vectorOfDoublesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func vectorOfDoubles(at index: Int32) -> Double { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? 0 : _accessor.directRead(of: Double.self, offset: _accessor.vector(at: o) + index * 8) } public func vectorOfDoubles(at index: Int32) -> Double { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return o == 0 ? 0 : _accessor.directRead(of: Double.self, offset: _accessor.vector(at: o) + index * 8) }
public var vectorOfDoubles: [Double] { return _accessor.getVector(at: VTOFFSET.vectorOfDoubles.v) ?? [] } public var vectorOfDoubles: [Double] { return _accessor.getVector(at: VTOFFSET.vectorOfDoubles.v) ?? [] }
public func mutate(vectorOfDoubles: Double, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return _accessor.directMutate(vectorOfDoubles, index: _accessor.vector(at: o) + index * 8) } public func mutate(vectorOfDoubles: Double, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfDoubles.v); return _accessor.directMutate(vectorOfDoubles, index: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToVectorOfDoubles<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.vectorOfDoubles.v, body: body) }
public var parentNamespaceTest: MyGame_InParentNamespace? { let o = _accessor.offset(VTOFFSET.parentNamespaceTest.v); return o == 0 ? nil : MyGame_InParentNamespace(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) } public var parentNamespaceTest: MyGame_InParentNamespace? { let o = _accessor.offset(VTOFFSET.parentNamespaceTest.v); return o == 0 ? nil : MyGame_InParentNamespace(_accessor.bb, o: _accessor.indirect(o + _accessor.position)) }
public var hasVectorOfReferrables: Bool { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? false : true } public var hasVectorOfReferrables: Bool { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? false : true }
public var vectorOfReferrablesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var vectorOfReferrablesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfReferrables.v); return o == 0 ? 0 : _accessor.vector(count: o) }
@@ -1288,6 +1297,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func vectorOfWeakReferences(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.vectorOfWeakReferences.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) } public func vectorOfWeakReferences(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.vectorOfWeakReferences.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) }
public var vectorOfWeakReferences: [UInt64] { return _accessor.getVector(at: VTOFFSET.vectorOfWeakReferences.v) ?? [] } public var vectorOfWeakReferences: [UInt64] { return _accessor.getVector(at: VTOFFSET.vectorOfWeakReferences.v) ?? [] }
public func mutate(vectorOfWeakReferences: UInt64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfWeakReferences.v); return _accessor.directMutate(vectorOfWeakReferences, index: _accessor.vector(at: o) + index * 8) } public func mutate(vectorOfWeakReferences: UInt64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfWeakReferences.v); return _accessor.directMutate(vectorOfWeakReferences, index: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToVectorOfWeakReferences<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.vectorOfWeakReferences.v, body: body) }
public var hasVectorOfStrongReferrables: Bool { let o = _accessor.offset(VTOFFSET.vectorOfStrongReferrables.v); return o == 0 ? false : true } public var hasVectorOfStrongReferrables: Bool { let o = _accessor.offset(VTOFFSET.vectorOfStrongReferrables.v); return o == 0 ? false : true }
public var vectorOfStrongReferrablesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfStrongReferrables.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var vectorOfStrongReferrablesCount: Int32 { let o = _accessor.offset(VTOFFSET.vectorOfStrongReferrables.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func vectorOfStrongReferrables(at index: Int32) -> MyGame_Example_Referrable? { let o = _accessor.offset(VTOFFSET.vectorOfStrongReferrables.v); return o == 0 ? nil : MyGame_Example_Referrable(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) } public func vectorOfStrongReferrables(at index: Int32) -> MyGame_Example_Referrable? { let o = _accessor.offset(VTOFFSET.vectorOfStrongReferrables.v); return o == 0 ? nil : MyGame_Example_Referrable(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) }
@@ -1299,6 +1309,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func vectorOfCoOwningReferences(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.vectorOfCoOwningReferences.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) } public func vectorOfCoOwningReferences(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.vectorOfCoOwningReferences.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) }
public var vectorOfCoOwningReferences: [UInt64] { return _accessor.getVector(at: VTOFFSET.vectorOfCoOwningReferences.v) ?? [] } public var vectorOfCoOwningReferences: [UInt64] { return _accessor.getVector(at: VTOFFSET.vectorOfCoOwningReferences.v) ?? [] }
public func mutate(vectorOfCoOwningReferences: UInt64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfCoOwningReferences.v); return _accessor.directMutate(vectorOfCoOwningReferences, index: _accessor.vector(at: o) + index * 8) } public func mutate(vectorOfCoOwningReferences: UInt64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfCoOwningReferences.v); return _accessor.directMutate(vectorOfCoOwningReferences, index: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToVectorOfCoOwningReferences<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.vectorOfCoOwningReferences.v, body: body) }
public var nonOwningReference: UInt64 { let o = _accessor.offset(VTOFFSET.nonOwningReference.v); return o == 0 ? 0 : _accessor.readBuffer(of: UInt64.self, at: o) } public var nonOwningReference: UInt64 { let o = _accessor.offset(VTOFFSET.nonOwningReference.v); return o == 0 ? 0 : _accessor.readBuffer(of: UInt64.self, at: o) }
@discardableResult public func mutate(nonOwningReference: UInt64) -> Bool {let o = _accessor.offset(VTOFFSET.nonOwningReference.v); return _accessor.mutate(nonOwningReference, index: o) } @discardableResult public func mutate(nonOwningReference: UInt64) -> Bool {let o = _accessor.offset(VTOFFSET.nonOwningReference.v); return _accessor.mutate(nonOwningReference, index: o) }
public var hasVectorOfNonOwningReferences: Bool { let o = _accessor.offset(VTOFFSET.vectorOfNonOwningReferences.v); return o == 0 ? false : true } public var hasVectorOfNonOwningReferences: Bool { let o = _accessor.offset(VTOFFSET.vectorOfNonOwningReferences.v); return o == 0 ? false : true }
@@ -1306,6 +1317,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func vectorOfNonOwningReferences(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.vectorOfNonOwningReferences.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) } public func vectorOfNonOwningReferences(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.vectorOfNonOwningReferences.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) }
public var vectorOfNonOwningReferences: [UInt64] { return _accessor.getVector(at: VTOFFSET.vectorOfNonOwningReferences.v) ?? [] } public var vectorOfNonOwningReferences: [UInt64] { return _accessor.getVector(at: VTOFFSET.vectorOfNonOwningReferences.v) ?? [] }
public func mutate(vectorOfNonOwningReferences: UInt64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfNonOwningReferences.v); return _accessor.directMutate(vectorOfNonOwningReferences, index: _accessor.vector(at: o) + index * 8) } public func mutate(vectorOfNonOwningReferences: UInt64, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vectorOfNonOwningReferences.v); return _accessor.directMutate(vectorOfNonOwningReferences, index: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToVectorOfNonOwningReferences<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.vectorOfNonOwningReferences.v, body: body) }
public var anyUniqueType: MyGame_Example_AnyUniqueAliases { let o = _accessor.offset(VTOFFSET.anyUniqueType.v); return o == 0 ? .none_ : MyGame_Example_AnyUniqueAliases(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ } public var anyUniqueType: MyGame_Example_AnyUniqueAliases { let o = _accessor.offset(VTOFFSET.anyUniqueType.v); return o == 0 ? .none_ : MyGame_Example_AnyUniqueAliases(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ }
public func anyUnique<T: FlatbuffersInitializable>(type: T.Type) -> T? { let o = _accessor.offset(VTOFFSET.anyUnique.v); return o == 0 ? nil : _accessor.union(o) } public func anyUnique<T: FlatbuffersInitializable>(type: T.Type) -> T? { let o = _accessor.offset(VTOFFSET.anyUnique.v); return o == 0 ? nil : _accessor.union(o) }
public var anyAmbiguousType: MyGame_Example_AnyAmbiguousAliases { let o = _accessor.offset(VTOFFSET.anyAmbiguousType.v); return o == 0 ? .none_ : MyGame_Example_AnyAmbiguousAliases(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ } public var anyAmbiguousType: MyGame_Example_AnyAmbiguousAliases { let o = _accessor.offset(VTOFFSET.anyAmbiguousType.v); return o == 0 ? .none_ : MyGame_Example_AnyAmbiguousAliases(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ }
@@ -1320,6 +1332,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
public func testrequirednestedflatbuffer(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.testrequirednestedflatbuffer.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) } public func testrequirednestedflatbuffer(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.testrequirednestedflatbuffer.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) }
public var testrequirednestedflatbuffer: [UInt8] { return _accessor.getVector(at: VTOFFSET.testrequirednestedflatbuffer.v) ?? [] } public var testrequirednestedflatbuffer: [UInt8] { return _accessor.getVector(at: VTOFFSET.testrequirednestedflatbuffer.v) ?? [] }
public func mutate(testrequirednestedflatbuffer: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testrequirednestedflatbuffer.v); return _accessor.directMutate(testrequirednestedflatbuffer, index: _accessor.vector(at: o) + index * 1) } public func mutate(testrequirednestedflatbuffer: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.testrequirednestedflatbuffer.v); return _accessor.directMutate(testrequirednestedflatbuffer, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToTestrequirednestedflatbuffer<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.testrequirednestedflatbuffer.v, body: body) }
public var hasScalarKeySortedTables: Bool { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? false : true } public var hasScalarKeySortedTables: Bool { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? false : true }
public var scalarKeySortedTablesCount: Int32 { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var scalarKeySortedTablesCount: Int32 { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func scalarKeySortedTables(at index: Int32) -> MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) } public func scalarKeySortedTables(at index: Int32) -> MyGame_Example_Stat? { let o = _accessor.offset(VTOFFSET.scalarKeySortedTables.v); return o == 0 ? nil : MyGame_Example_Stat(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) }
@@ -1549,7 +1562,7 @@ public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPac
} }
public static func sortVectorOfMonster(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset { public static func sortVectorOfMonster(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset {
var off = offsets var off = offsets
off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 10, fbb: fbb.buffer), Table.offset(Int32($0.o), vOffset: 10, fbb: fbb.buffer), fbb: fbb.buffer) < 0 } off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 10, fbb: &fbb), Table.offset(Int32($0.o), vOffset: 10, fbb: &fbb), fbb: &fbb) < 0 }
return fbb.createVector(ofOffsets: off) return fbb.createVector(ofOffsets: off)
} }
fileprivate static func lookupByKey(vector: Int32, key: String, fbb: ByteBuffer) -> MyGame_Example_Monster? { fileprivate static func lookupByKey(vector: Int32, key: String, fbb: ByteBuffer) -> MyGame_Example_Monster? {
@@ -2456,11 +2469,13 @@ public struct MyGame_Example_TypeAliases: FlatBufferObject, Verifiable, ObjectAP
public func v8(at index: Int32) -> Int8 { let o = _accessor.offset(VTOFFSET.v8.v); return o == 0 ? 0 : _accessor.directRead(of: Int8.self, offset: _accessor.vector(at: o) + index * 1) } public func v8(at index: Int32) -> Int8 { let o = _accessor.offset(VTOFFSET.v8.v); return o == 0 ? 0 : _accessor.directRead(of: Int8.self, offset: _accessor.vector(at: o) + index * 1) }
public var v8: [Int8] { return _accessor.getVector(at: VTOFFSET.v8.v) ?? [] } public var v8: [Int8] { return _accessor.getVector(at: VTOFFSET.v8.v) ?? [] }
public func mutate(v8: Int8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.v8.v); return _accessor.directMutate(v8, index: _accessor.vector(at: o) + index * 1) } public func mutate(v8: Int8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.v8.v); return _accessor.directMutate(v8, index: _accessor.vector(at: o) + index * 1) }
public func withUnsafePointerToV8<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.v8.v, body: body) }
public var hasVf64: Bool { let o = _accessor.offset(VTOFFSET.vf64.v); return o == 0 ? false : true } public var hasVf64: Bool { let o = _accessor.offset(VTOFFSET.vf64.v); return o == 0 ? false : true }
public var vf64Count: Int32 { let o = _accessor.offset(VTOFFSET.vf64.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var vf64Count: Int32 { let o = _accessor.offset(VTOFFSET.vf64.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func vf64(at index: Int32) -> Double { let o = _accessor.offset(VTOFFSET.vf64.v); return o == 0 ? 0 : _accessor.directRead(of: Double.self, offset: _accessor.vector(at: o) + index * 8) } public func vf64(at index: Int32) -> Double { let o = _accessor.offset(VTOFFSET.vf64.v); return o == 0 ? 0 : _accessor.directRead(of: Double.self, offset: _accessor.vector(at: o) + index * 8) }
public var vf64: [Double] { return _accessor.getVector(at: VTOFFSET.vf64.v) ?? [] } public var vf64: [Double] { return _accessor.getVector(at: VTOFFSET.vf64.v) ?? [] }
public func mutate(vf64: Double, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vf64.v); return _accessor.directMutate(vf64, index: _accessor.vector(at: o) + index * 8) } public func mutate(vf64: Double, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.vf64.v); return _accessor.directMutate(vf64, index: _accessor.vector(at: o) + index * 8) }
public func withUnsafePointerToVf64<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.vf64.v, body: body) }
public static func startTypeAliases(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 12) } public static func startTypeAliases(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 12) }
public static func add(i8: Int8, _ fbb: inout FlatBufferBuilder) { fbb.add(element: i8, def: 0, at: VTOFFSET.i8.p) } public static func add(i8: Int8, _ fbb: inout FlatBufferBuilder) { fbb.add(element: i8, def: 0, at: VTOFFSET.i8.p) }
public static func add(u8: UInt8, _ fbb: inout FlatBufferBuilder) { fbb.add(element: u8, def: 0, at: VTOFFSET.u8.p) } public static func add(u8: UInt8, _ fbb: inout FlatBufferBuilder) { fbb.add(element: u8, def: 0, at: VTOFFSET.u8.p) }

View File

@@ -51,10 +51,12 @@ public struct MoreDefaults: FlatBufferObject, Verifiable, ObjectAPIPacker {
public var intsCount: Int32 { let o = _accessor.offset(VTOFFSET.ints.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var intsCount: Int32 { let o = _accessor.offset(VTOFFSET.ints.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func ints(at index: Int32) -> Int32 { let o = _accessor.offset(VTOFFSET.ints.v); return o == 0 ? 0 : _accessor.directRead(of: Int32.self, offset: _accessor.vector(at: o) + index * 4) } public func ints(at index: Int32) -> Int32 { let o = _accessor.offset(VTOFFSET.ints.v); return o == 0 ? 0 : _accessor.directRead(of: Int32.self, offset: _accessor.vector(at: o) + index * 4) }
public var ints: [Int32] { return _accessor.getVector(at: VTOFFSET.ints.v) ?? [] } public var ints: [Int32] { return _accessor.getVector(at: VTOFFSET.ints.v) ?? [] }
public func withUnsafePointerToInts<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.ints.v, body: body) }
public var hasFloats: Bool { let o = _accessor.offset(VTOFFSET.floats.v); return o == 0 ? false : true } public var hasFloats: Bool { let o = _accessor.offset(VTOFFSET.floats.v); return o == 0 ? false : true }
public var floatsCount: Int32 { let o = _accessor.offset(VTOFFSET.floats.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var floatsCount: Int32 { let o = _accessor.offset(VTOFFSET.floats.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func floats(at index: Int32) -> Float32 { let o = _accessor.offset(VTOFFSET.floats.v); return o == 0 ? 0 : _accessor.directRead(of: Float32.self, offset: _accessor.vector(at: o) + index * 4) } public func floats(at index: Int32) -> Float32 { let o = _accessor.offset(VTOFFSET.floats.v); return o == 0 ? 0 : _accessor.directRead(of: Float32.self, offset: _accessor.vector(at: o) + index * 4) }
public var floats: [Float32] { return _accessor.getVector(at: VTOFFSET.floats.v) ?? [] } public var floats: [Float32] { return _accessor.getVector(at: VTOFFSET.floats.v) ?? [] }
public func withUnsafePointerToFloats<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.floats.v, body: body) }
public var emptyString: String? { let o = _accessor.offset(VTOFFSET.emptyString.v); return o == 0 ? "" : _accessor.string(at: o) } public var emptyString: String? { let o = _accessor.offset(VTOFFSET.emptyString.v); return o == 0 ? "" : _accessor.string(at: o) }
public var emptyStringSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.emptyString.v) } public var emptyStringSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.emptyString.v) }
public var someString: String? { let o = _accessor.offset(VTOFFSET.someString.v); return o == 0 ? "some" : _accessor.string(at: o) } public var someString: String? { let o = _accessor.offset(VTOFFSET.someString.v); return o == 0 ? "some" : _accessor.string(at: o) }
@@ -66,6 +68,7 @@ public struct MoreDefaults: FlatBufferObject, Verifiable, ObjectAPIPacker {
public var boolsCount: Int32 { let o = _accessor.offset(VTOFFSET.bools.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var boolsCount: Int32 { let o = _accessor.offset(VTOFFSET.bools.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func bools(at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.bools.v); return o == 0 ? true : _accessor.directRead(of: Bool.self, offset: _accessor.vector(at: o) + index * 1) } public func bools(at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.bools.v); return o == 0 ? true : _accessor.directRead(of: Bool.self, offset: _accessor.vector(at: o) + index * 1) }
public var bools: [Bool] { return _accessor.getVector(at: VTOFFSET.bools.v) ?? [] } public var bools: [Bool] { return _accessor.getVector(at: VTOFFSET.bools.v) ?? [] }
public func withUnsafePointerToBools<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.bools.v, body: body) }
public static func startMoreDefaults(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 6) } public static func startMoreDefaults(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 6) }
public static func addVectorOf(ints: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: ints, at: VTOFFSET.ints.p) } public static func addVectorOf(ints: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: ints, at: VTOFFSET.ints.p) }
public static func addVectorOf(floats: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: floats, at: VTOFFSET.floats.p) } public static func addVectorOf(floats: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: floats, at: VTOFFSET.floats.p) }

View File

@@ -25,14 +25,17 @@ public struct Swift_Tests_Vectors: FlatBufferObject, Verifiable {
public var none_Count: Int32 { let o = _accessor.offset(VTOFFSET.none_.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var none_Count: Int32 { let o = _accessor.offset(VTOFFSET.none_.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func none_(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.none_.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) } public func none_(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.none_.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) }
public var none_: [UInt64] { return _accessor.getVector(at: VTOFFSET.none_.v) ?? [] } public var none_: [UInt64] { return _accessor.getVector(at: VTOFFSET.none_.v) ?? [] }
public func withUnsafePointerToNone<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.none_.v, body: body) }
public var hasEmpty: Bool { let o = _accessor.offset(VTOFFSET.empty.v); return o == 0 ? false : true } public var hasEmpty: Bool { let o = _accessor.offset(VTOFFSET.empty.v); return o == 0 ? false : true }
public var emptyCount: Int32 { let o = _accessor.offset(VTOFFSET.empty.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var emptyCount: Int32 { let o = _accessor.offset(VTOFFSET.empty.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func empty(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.empty.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) } public func empty(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.empty.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) }
public var empty: [UInt64] { return _accessor.getVector(at: VTOFFSET.empty.v) ?? [] } public var empty: [UInt64] { return _accessor.getVector(at: VTOFFSET.empty.v) ?? [] }
public func withUnsafePointerToEmpty<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.empty.v, body: body) }
public var hasArray: Bool { let o = _accessor.offset(VTOFFSET.array.v); return o == 0 ? false : true } public var hasArray: Bool { let o = _accessor.offset(VTOFFSET.array.v); return o == 0 ? false : true }
public var arrayCount: Int32 { let o = _accessor.offset(VTOFFSET.array.v); return o == 0 ? 0 : _accessor.vector(count: o) } public var arrayCount: Int32 { let o = _accessor.offset(VTOFFSET.array.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func array(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.array.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) } public func array(at index: Int32) -> UInt64 { let o = _accessor.offset(VTOFFSET.array.v); return o == 0 ? 0 : _accessor.directRead(of: UInt64.self, offset: _accessor.vector(at: o) + index * 8) }
public var array: [UInt64] { return _accessor.getVector(at: VTOFFSET.array.v) ?? [] } public var array: [UInt64] { return _accessor.getVector(at: VTOFFSET.array.v) ?? [] }
public func withUnsafePointerToArray<T>(_ body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.array.v, body: body) }
public static func startVectors(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 3) } public static func startVectors(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 3) }
public static func addVectorOf(none_: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: none_, at: VTOFFSET.none_.p) } public static func addVectorOf(none_: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: none_, at: VTOFFSET.none_.p) }
public static func addVectorOf(empty: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: empty, at: VTOFFSET.empty.p) } public static func addVectorOf(empty: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: empty, at: VTOFFSET.empty.p) }