mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-05 05:27:03 +00:00
[Swift] Implements FlatbuffersVector which confirms to RandomAccessCollection (#8752)
* Implements FlatbuffersVector in swift Implements FlatbuffersVector which confirms to RandomAccessCollection, this would give us semi-native sugary syntax to all the arrays in swift port. This work will also be the foundation of using arrays in swift * Fix failing tests for Swift
This commit is contained in:
@@ -8,7 +8,7 @@ import Common
|
||||
|
||||
import FlatBuffers
|
||||
|
||||
public enum ABC: Int32, Enum, Verifiable {
|
||||
public enum ABC: Int32, FlatbuffersVectorInitializable, Enum, Verifiable {
|
||||
public typealias T = Int32
|
||||
public static var byteSize: Int { return MemoryLayout<Int32>.size }
|
||||
public var value: Int32 { return self.rawValue }
|
||||
@@ -31,7 +31,7 @@ extension ABC: Encodable {
|
||||
}
|
||||
}
|
||||
|
||||
public struct MoreDefaults: FlatBufferObject, Verifiable, ObjectAPIPacker {
|
||||
public struct MoreDefaults: FlatBufferTable, FlatbuffersVectorInitializable, Verifiable, ObjectAPIPacker {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_25_9_23() }
|
||||
public var __buffer: ByteBuffer! { return _accessor.bb }
|
||||
@@ -51,28 +51,17 @@ public struct MoreDefaults: FlatBufferObject, Verifiable, ObjectAPIPacker {
|
||||
var p: VOffset { self.rawValue }
|
||||
}
|
||||
|
||||
public var hasInts: Bool { let o = _accessor.offset(VTOFFSET.ints.v); return o == 0 ? false : true }
|
||||
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 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 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 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 ints: FlatbufferVector<Int32> { return _accessor.vector(at: VTOFFSET.ints.v, byteSize: 4) }
|
||||
public func withUnsafePointerToInts<T>(_ body: (UnsafeRawBufferPointer, Int) throws -> T) rethrows -> T? { return try _accessor.withUnsafePointerToSlice(at: VTOFFSET.ints.v, body: body) }
|
||||
public var floats: FlatbufferVector<Float32> { return _accessor.vector(at: VTOFFSET.floats.v, byteSize: 4) }
|
||||
public func withUnsafePointerToFloats<T>(_ body: (UnsafeRawBufferPointer, Int) 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 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 someStringSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.someString.v) }
|
||||
public var hasAbcs: Bool { let o = _accessor.offset(VTOFFSET.abcs.v); return o == 0 ? false : true }
|
||||
public var abcsCount: Int32 { let o = _accessor.offset(VTOFFSET.abcs.v); return o == 0 ? 0 : _accessor.vector(count: o) }
|
||||
public func abcs(at index: Int32) -> ABC? { let o = _accessor.offset(VTOFFSET.abcs.v); return o == 0 ? ABC.a : ABC(rawValue: _accessor.directRead(of: Int32.self, offset: _accessor.vector(at: o) + index * 4)) }
|
||||
public var hasBools: Bool { let o = _accessor.offset(VTOFFSET.bools.v); return o == 0 ? false : true }
|
||||
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 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 var abcs: FlatbufferVector<ABC> { return _accessor.vector(at: VTOFFSET.abcs.v, byteSize: 4) }
|
||||
public var bools: FlatbufferVector<Bool> { return _accessor.vector(at: VTOFFSET.bools.v, byteSize: 1) }
|
||||
public func withUnsafePointerToBools<T>(_ body: (UnsafeRawBufferPointer, Int) 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 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) }
|
||||
@@ -162,24 +151,12 @@ extension MoreDefaults: Encodable {
|
||||
}
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
if intsCount > 0 {
|
||||
try container.encodeIfPresent(ints, forKey: .ints)
|
||||
}
|
||||
if floatsCount > 0 {
|
||||
try container.encodeIfPresent(floats, forKey: .floats)
|
||||
}
|
||||
try container.encodeIfPresent(ints, forKey: .ints)
|
||||
try container.encodeIfPresent(floats, forKey: .floats)
|
||||
try container.encodeIfPresent(emptyString, forKey: .emptyString)
|
||||
try container.encodeIfPresent(someString, forKey: .someString)
|
||||
if abcsCount > 0 {
|
||||
var contentEncoder = container.nestedUnkeyedContainer(forKey: .abcs)
|
||||
for index in 0..<abcsCount {
|
||||
guard let type = abcs(at: index) else { continue }
|
||||
try contentEncoder.encode(type)
|
||||
}
|
||||
}
|
||||
if boolsCount > 0 {
|
||||
try container.encodeIfPresent(bools, forKey: .bools)
|
||||
}
|
||||
try container.encodeIfPresent(abcs, forKey: .abcs)
|
||||
try container.encodeIfPresent(bools, forKey: .bools)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,23 +171,15 @@ public class MoreDefaultsT: NativeObject {
|
||||
|
||||
public init(_ _t: inout MoreDefaults) {
|
||||
ints = []
|
||||
for index in 0..<_t.intsCount {
|
||||
ints.append(_t.ints(at: index))
|
||||
}
|
||||
ints.append(contentsOf: _t.ints)
|
||||
floats = []
|
||||
for index in 0..<_t.floatsCount {
|
||||
floats.append(_t.floats(at: index))
|
||||
}
|
||||
floats.append(contentsOf: _t.floats)
|
||||
emptyString = _t.emptyString
|
||||
someString = _t.someString
|
||||
abcs = []
|
||||
for index in 0..<_t.abcsCount {
|
||||
abcs.append(_t.abcs(at: index)!)
|
||||
}
|
||||
abcs.append(contentsOf: _t.abcs)
|
||||
bools = []
|
||||
for index in 0..<_t.boolsCount {
|
||||
bools.append(_t.bools(at: index))
|
||||
}
|
||||
bools.append(contentsOf: _t.bools)
|
||||
}
|
||||
|
||||
public init() {
|
||||
|
||||
Reference in New Issue
Block a user