From b556868ce4ceeb885983824129f71dd7c4d7ec72 Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Sun, 22 May 2016 12:19:46 +0200 Subject: [PATCH 01/16] Minor codegen improvement clarifying intent. --- FlatBuffersPerformanceTestDesktop/bench.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/FlatBuffersPerformanceTestDesktop/bench.swift b/FlatBuffersPerformanceTestDesktop/bench.swift index b58b881..91d3d85 100644 --- a/FlatBuffersPerformanceTestDesktop/bench.swift +++ b/FlatBuffersPerformanceTestDesktop/bench.swift @@ -148,8 +148,8 @@ public func ==(t1 : FooBar.LazyAccess, t2 : FooBar.LazyAccess) -> Bool { extension FooBar { public struct Fast : Hashable { - private var buffer : UnsafePointer = nil - private var myOffset : Offset = 0 + private let buffer : UnsafePointer + private let myOffset : Offset public init(buffer: UnsafePointer, myOffset: Offset){ self.buffer = buffer self.myOffset = myOffset @@ -397,8 +397,8 @@ public func ==(t1 : FooBarContainer.LazyAccess, t2 : FooBarContainer.LazyAccess) extension FooBarContainer { public struct Fast : Hashable { - private var buffer : UnsafePointer = nil - private var myOffset : Offset = 0 + private let buffer : UnsafePointer + private let myOffset : Offset public init(buffer: UnsafePointer, myOffset: Offset){ self.buffer = buffer self.myOffset = myOffset @@ -411,8 +411,8 @@ public struct Fast : Hashable { return buffer } public struct ListVector { - private var buffer : UnsafePointer = nil - private var myOffset : Offset = 0 + private let buffer : UnsafePointer + private let myOffset : Offset private let offsetList : Offset? private init(buffer b: UnsafePointer, myOffset o: Offset ) { buffer = b From 726af2e225ef27f885e3d7b245fde433d2f32d41 Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Tue, 10 Jan 2017 13:23:07 +0100 Subject: [PATCH 02/16] Change enum to camelCase according to Swift API guidelines. --- FlatBuffersSwift/FlatBufferBuilder.swift | 46 ++++++++++++------------ FlatBuffersSwift/FlatBufferReader.swift | 14 ++++---- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/FlatBuffersSwift/FlatBufferBuilder.swift b/FlatBuffersSwift/FlatBufferBuilder.swift index 583a865..c6e1708 100644 --- a/FlatBuffersSwift/FlatBufferBuilder.swift +++ b/FlatBuffersSwift/FlatBufferBuilder.swift @@ -44,13 +44,13 @@ public struct FBBuildConfig { } public enum FBBuildError : Error { - case ObjectIsNotClosed - case NoOpenObject - case PropertyIndexIsInvalid - case OffsetIsTooBig - case CursorIsInvalid - case BadFileIdentifier - case UnsupportedType + case objectIsNotClosed + case noOpenObject + case propertyIndexIsInvalid + case offsetIsTooBig + case cursorIsInvalid + case badFileIdentifier + case unsupportedType } public final class FBBuilder { @@ -131,7 +131,7 @@ public final class FBBuilder { return cursor } guard offset <= Int32(cursor) else { - throw FBBuildError.OffsetIsTooBig + throw FBBuildError.offsetIsTooBig } if offset == Int32(0) { @@ -146,10 +146,10 @@ public final class FBBuilder { public func replaceOffset(offset : Offset, atCursor jumpCursor: Int) throws{ guard offset <= Int32(cursor) else { - throw FBBuildError.OffsetIsTooBig + throw FBBuildError.offsetIsTooBig } guard jumpCursor <= cursor else { - throw FBBuildError.CursorIsInvalid + throw FBBuildError.cursorIsInvalid } let _offset = Int32(jumpCursor) - offset; @@ -162,7 +162,7 @@ public final class FBBuilder { public func openObject(numOfProperties : Int) throws { guard objectStart == -1 && vectorNumElems == -1 else { - throw FBBuildError.ObjectIsNotClosed + throw FBBuildError.objectIsNotClosed } currentVTable.removeAll(keepingCapacity: true) currentVTable.reserveCapacity(numOfProperties) @@ -175,10 +175,10 @@ public final class FBBuilder { @discardableResult public func addPropertyOffsetToOpenObject(propertyIndex : Int, offset : Offset) throws -> Int{ guard objectStart > -1 else { - throw FBBuildError.NoOpenObject + throw FBBuildError.noOpenObject } guard propertyIndex >= 0 && propertyIndex < currentVTable.count else { - throw FBBuildError.PropertyIndexIsInvalid + throw FBBuildError.propertyIndexIsInvalid } _ = try putOffset(offset: offset) currentVTable[propertyIndex] = Int32(cursor) @@ -187,10 +187,10 @@ public final class FBBuilder { public func addPropertyToOpenObject(propertyIndex : Int, value : T, defaultValue : T) throws { guard objectStart > -1 else { - throw FBBuildError.NoOpenObject + throw FBBuildError.noOpenObject } guard propertyIndex >= 0 && propertyIndex < currentVTable.count else { - throw FBBuildError.PropertyIndexIsInvalid + throw FBBuildError.propertyIndexIsInvalid } if(config.forceDefaults == false && value == defaultValue) { @@ -203,17 +203,17 @@ public final class FBBuilder { public func addCurrentOffsetAsPropertyToOpenObject(propertyIndex : Int) throws { guard objectStart > -1 else { - throw FBBuildError.NoOpenObject + throw FBBuildError.noOpenObject } guard propertyIndex >= 0 && propertyIndex < currentVTable.count else { - throw FBBuildError.PropertyIndexIsInvalid + throw FBBuildError.propertyIndexIsInvalid } currentVTable[propertyIndex] = Int32(cursor) } public func closeObject() throws -> Offset { guard objectStart > -1 else { - throw FBBuildError.NoOpenObject + throw FBBuildError.noOpenObject } align(size: 4, additionalBytes: 0) increaseCapacity(size: 4) @@ -277,7 +277,7 @@ public final class FBBuilder { public func startVector(count : Int, elementSize : Int) throws{ align(size: 4, additionalBytes: count * elementSize) guard objectStart == -1 && vectorNumElems == -1 else { - throw FBBuildError.ObjectIsNotClosed + throw FBBuildError.objectIsNotClosed } vectorNumElems = Int32(count) } @@ -291,7 +291,7 @@ public final class FBBuilder { private var stringCache : [String:Offset] = [:] public func createString(value : String?) throws -> Offset { guard objectStart == -1 && vectorNumElems == -1 else { - throw FBBuildError.ObjectIsNotClosed + throw FBBuildError.objectIsNotClosed } guard let value = value else { return 0 @@ -332,10 +332,10 @@ public final class FBBuilder { public func finish(offset : Offset, fileIdentifier : String?) throws -> Void { guard offset <= Int32(cursor) else { - throw FBBuildError.OffsetIsTooBig + throw FBBuildError.offsetIsTooBig } guard objectStart == -1 && vectorNumElems == -1 else { - throw FBBuildError.ObjectIsNotClosed + throw FBBuildError.objectIsNotClosed } var prefixLength = 4 if let fileIdentifier = fileIdentifier { @@ -344,7 +344,7 @@ public final class FBBuilder { let utf8View = fileIdentifier.utf8 let count = utf8View.count guard count == 4 else { - throw FBBuildError.BadFileIdentifier + throw FBBuildError.badFileIdentifier } for c in utf8View.lazy.reversed() { put(value: c) diff --git a/FlatBuffersSwift/FlatBufferReader.swift b/FlatBuffersSwift/FlatBufferReader.swift index 8301d93..ce5a04e 100644 --- a/FlatBuffersSwift/FlatBufferReader.swift +++ b/FlatBuffersSwift/FlatBufferReader.swift @@ -17,8 +17,8 @@ public protocol FBReader { fileprivate enum FBReaderError : Error { - case OutOfBufferBounds - case CanNotSetProperty + case outOfBufferBounds + case canNotSetProperty } public class FBReaderCache { @@ -207,7 +207,7 @@ public struct FBMemoryReader : FBReader { public func fromByteArray(position : Int) throws -> T { if position + MemoryLayout.stride > count || position < 0 { - throw FBReaderError.OutOfBufferBounds + throw FBReaderError.outOfBufferBounds } return buffer.load(fromByteOffset: position, as: T.self) @@ -215,7 +215,7 @@ public struct FBMemoryReader : FBReader { public func buffer(position : Int, length : Int) throws -> UnsafeBufferPointer { if Int(position + length) > count { - throw FBReaderError.OutOfBufferBounds + throw FBReaderError.outOfBufferBounds } let pointer = buffer.advanced(by:position).bindMemory(to: UInt8.self, capacity: length) return UnsafeBufferPointer.init(start: pointer, count: Int(length)) @@ -245,7 +245,7 @@ public struct FBFileReader : FBReader { public func fromByteArray(position : Int) throws -> T { let seekPosition = UInt64(position) if seekPosition + UInt64(MemoryLayout.stride) > fileSize { - throw FBReaderError.OutOfBufferBounds + throw FBReaderError.outOfBufferBounds } fileHandle.seek(toFileOffset: seekPosition) let data = fileHandle.readData(ofLength:MemoryLayout.stride) @@ -256,12 +256,12 @@ public struct FBFileReader : FBReader { pointer.deinitialize() return result } - throw FBReaderError.OutOfBufferBounds + throw FBReaderError.outOfBufferBounds } public func buffer(position : Int, length : Int) throws -> UnsafeBufferPointer { if UInt64(position + length) > fileSize { - throw FBReaderError.OutOfBufferBounds + throw FBReaderError.outOfBufferBounds } fileHandle.seek(toFileOffset: UInt64(position)) let data = fileHandle.readData(ofLength:Int(length)) From 55f9ebd48d4d6783858143d4cfac0658c65b609f Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Tue, 10 Jan 2017 13:46:42 +0100 Subject: [PATCH 03/16] Trying to name reader protocol methods in line with Swift API guidelines / Data. --- FlatBuffersSwift/FlatBufferReader.swift | 54 +++++++++++++------------ 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/FlatBuffersSwift/FlatBufferReader.swift b/FlatBuffersSwift/FlatBufferReader.swift index ce5a04e..c8ccc42 100644 --- a/FlatBuffersSwift/FlatBufferReader.swift +++ b/FlatBuffersSwift/FlatBufferReader.swift @@ -9,9 +9,11 @@ import Foundation public protocol FBReader { - func fromByteArray(position : Int) throws -> T - func buffer(position : Int, length : Int) throws -> UnsafeBufferPointer + var cache : FBReaderCache? {get} + + func getScalar(at offset: Int) throws -> T + func bytes(at offset : Int, length : Int) throws -> UnsafeBufferPointer func isEqual(other : FBReader) -> Bool } @@ -37,16 +39,16 @@ public extension FBReader { } do { let offset = Int(objectOffset) - let localOffset : Int32 = try fromByteArray(position: offset) + let localOffset : Int32 = try getScalar(at: offset) let vTableOffset : Int = offset - Int(localOffset) - let vTableLength : Int16 = try fromByteArray(position: vTableOffset) - let objectLength : Int16 = try fromByteArray(position: vTableOffset + 2) + let vTableLength : Int16 = try getScalar(at: vTableOffset) + let objectLength : Int16 = try getScalar(at: vTableOffset + 2) let positionInVTable = 4 + propertyIndex * 2 if(vTableLength<=Int16(positionInVTable)) { return 0 } let propertyStart = vTableOffset + positionInVTable - let propertyOffset : Int16 = try fromByteArray(position: propertyStart) + let propertyOffset : Int16 = try getScalar(at: propertyStart) if(objectLength<=propertyOffset) { return 0 } @@ -65,7 +67,7 @@ public extension FBReader { let position = objectOffset + propertyOffset do { - let localObjectOffset : Int32 = try fromByteArray(position: Int(position)) + let localObjectOffset : Int32 = try getScalar(at: Int(position)) let offset = position + localObjectOffset if localObjectOffset == 0 { @@ -84,7 +86,7 @@ public extension FBReader { } let vectorPosition = Int(vectorOffset) do { - let length2 : Int32 = try fromByteArray(position: vectorPosition) + let length2 : Int32 = try getScalar(at: vectorPosition) return Int(length2) } catch { return 0 @@ -103,7 +105,7 @@ public extension FBReader { } let valueStartPosition = Int(vectorOffset + MemoryLayout.stride + (index * MemoryLayout.stride)) do { - let localOffset : Int32 = try fromByteArray(position: valueStartPosition) + let localOffset : Int32 = try getScalar(at: valueStartPosition) if(localOffset == 0){ return nil } @@ -127,7 +129,7 @@ public extension FBReader { let valueStartPosition = Int(vectorOffset + MemoryLayout.stride + (index * MemoryLayout.stride)) do { - return try fromByteArray(position: valueStartPosition) as T + return try getScalar(at: valueStartPosition) as T } catch { return nil } @@ -140,7 +142,7 @@ public extension FBReader { } let position = Int(objectOffset + propertyOffset) do { - return try fromByteArray(position: position) + return try getScalar(at: position) } catch { return defaultValue } @@ -153,7 +155,7 @@ public extension FBReader { } let position = Int(objectOffset + propertyOffset) do { - return try fromByteArray(position: position) as T + return try getScalar(at: position) as T } catch { return nil } @@ -165,10 +167,10 @@ public extension FBReader { } let stringPosition = Int(stringOffset) do { - let stringLength : Int32 = try fromByteArray(position: stringPosition) + let stringLength : Int32 = try getScalar(at: stringPosition) let stringCharactersPosition = stringPosition + MemoryLayout.stride - return try buffer(position: stringCharactersPosition, length: Int(stringLength)) + return try bytes(at: stringCharactersPosition, length: Int(stringLength)) } catch { return nil } @@ -176,7 +178,7 @@ public extension FBReader { public var rootObjectOffset : Offset? { do { - return try fromByteArray(position: 0) as Offset + return try getScalar(at: 0) as Offset } catch { return nil } @@ -205,19 +207,19 @@ public struct FBMemoryReader : FBReader { self.buffer = UnsafeRawPointer(pointer) } - public func fromByteArray(position : Int) throws -> T { - if position + MemoryLayout.stride > count || position < 0 { + public func getScalar(at offset: Int) throws -> T { + if offset + MemoryLayout.stride > count || offset < 0 { throw FBReaderError.outOfBufferBounds } - return buffer.load(fromByteOffset: position, as: T.self) + return buffer.load(fromByteOffset: offset, as: T.self) } - public func buffer(position : Int, length : Int) throws -> UnsafeBufferPointer { - if Int(position + length) > count { + public func bytes(at offset : Int, length : Int) throws -> UnsafeBufferPointer { + if Int(offset + length) > count { throw FBReaderError.outOfBufferBounds } - let pointer = buffer.advanced(by:position).bindMemory(to: UInt8.self, capacity: length) + let pointer = buffer.advanced(by:offset).bindMemory(to: UInt8.self, capacity: length) return UnsafeBufferPointer.init(start: pointer, count: Int(length)) } @@ -242,8 +244,8 @@ public struct FBFileReader : FBReader { self.cache = cache } - public func fromByteArray(position : Int) throws -> T { - let seekPosition = UInt64(position) + public func getScalar(at offset: Int) throws -> T { + let seekPosition = UInt64(offset) if seekPosition + UInt64(MemoryLayout.stride) > fileSize { throw FBReaderError.outOfBufferBounds } @@ -259,11 +261,11 @@ public struct FBFileReader : FBReader { throw FBReaderError.outOfBufferBounds } - public func buffer(position : Int, length : Int) throws -> UnsafeBufferPointer { - if UInt64(position + length) > fileSize { + public func bytes(at offset : Int, length : Int) throws -> UnsafeBufferPointer { + if UInt64(offset + length) > fileSize { throw FBReaderError.outOfBufferBounds } - fileHandle.seek(toFileOffset: UInt64(position)) + fileHandle.seek(toFileOffset: UInt64(offset)) let data = fileHandle.readData(ofLength:Int(length)) let pointer = UnsafeMutablePointer.allocate(capacity: 1) let t : UnsafeMutableBufferPointer = UnsafeMutableBufferPointer(start: pointer, count: length) From 76d753b8e51e396f0f890a919c1f0aa58b9d8f67 Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Tue, 10 Jan 2017 14:59:23 +0100 Subject: [PATCH 04/16] Add comments to reader methods, refine naming for vector access methods. --- FlatBuffersSwift/FlatBufferReader.swift | 29 +++++++++++++++------ FlatBuffersSwiftTests/FBReaderTest.swift | 20 +++++++-------- FlatBuffersSwiftTests/contacts_fb.swift | 32 ++++++++++++------------ FlatBuffersSwiftTests/friends_fb.swift | 16 ++++++------ 4 files changed, 55 insertions(+), 42 deletions(-) diff --git a/FlatBuffersSwift/FlatBufferReader.swift b/FlatBuffersSwift/FlatBufferReader.swift index c8ccc42..f2821f3 100644 --- a/FlatBuffersSwift/FlatBufferReader.swift +++ b/FlatBuffersSwift/FlatBufferReader.swift @@ -9,17 +9,18 @@ import Foundation public protocol FBReader { - var cache : FBReaderCache? {get} + /// **Returns** a scalar value at a given offset from the buffer func getScalar(at offset: Int) throws -> T + /// **Returns** a direct pointer to a subrange from the underlying reader buffer func bytes(at offset : Int, length : Int) throws -> UnsafeBufferPointer func isEqual(other : FBReader) -> Bool } fileprivate enum FBReaderError : Error { - case outOfBufferBounds + case outOfBufferBounds /// Trying to address outside of the bounds of the underlying buffer case canNotSetProperty } @@ -32,7 +33,7 @@ public class FBReaderCache { } public extension FBReader { - + /// **Returns** the object-local offset of a given property by looking it up in the vtable private func getPropertyOffset(objectOffset : Offset, propertyIndex : Int) -> Int { guard propertyIndex >= 0 else { return 0 @@ -58,6 +59,7 @@ public extension FBReader { } } + /// **Returns** the final offset in the reader buffer to access a given property for a given object-offset public func getOffset(objectOffset : Offset, propertyIndex : Int) -> Offset? { let propertyOffset = getPropertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) @@ -80,7 +82,8 @@ public extension FBReader { } - public func getVectorLength(vectorOffset : Offset?) -> Int { + /// **Returns** the length of vector + public func getVectorElementCount(vectorOffset : Offset?) -> Int { guard let vectorOffset = vectorOffset else { return 0 } @@ -93,14 +96,15 @@ public extension FBReader { } } - public func getVectorOffsetElement(vectorOffset : Offset?, index : Int) -> Offset? { + /// **Returns** the offset in the buffer for a given vector element + public func getVectorElementOffset(vectorOffset : Offset?, index : Int) -> Offset? { guard let vectorOffset = vectorOffset else { return nil } guard index >= 0 else{ return nil } - guard index < getVectorLength(vectorOffset: vectorOffset) else { + guard index < getVectorElementCount(vectorOffset: vectorOffset) else { return nil } let valueStartPosition = Int(vectorOffset + MemoryLayout.stride + (index * MemoryLayout.stride)) @@ -115,6 +119,7 @@ public extension FBReader { } } + /// **Returns** a scalar value directly from a vector for a given index public func getVectorScalarElement(vectorOffset : Offset?, index : Int) -> T? { guard let vectorOffset = vectorOffset else { return nil @@ -122,7 +127,7 @@ public extension FBReader { guard index >= 0 else{ return nil } - guard index < getVectorLength(vectorOffset: vectorOffset) else { + guard index < getVectorElementCount(vectorOffset: vectorOffset) else { return nil } @@ -134,7 +139,8 @@ public extension FBReader { return nil } } - + + /// **Returns** a scalar value directly from a vector for a given index public func get(objectOffset : Offset, propertyIndex : Int, defaultValue : T) -> T { let propertyOffset = getPropertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) if propertyOffset == 0 { @@ -148,6 +154,7 @@ public extension FBReader { } } + /// **Returns** a scalar value for a given property from an object public func get(objectOffset : Offset, propertyIndex : Int) -> T? { let propertyOffset = getPropertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) if propertyOffset == 0 { @@ -161,6 +168,7 @@ public extension FBReader { } } + /// **Returns** a buffer pointer to the subrange of the reader buffer occupied by a string public func getStringBuffer(stringOffset : Offset?) -> UnsafeBufferPointer? { guard let stringOffset = stringOffset else { return nil @@ -176,6 +184,7 @@ public extension FBReader { } } + /// **Returns** the offset for the root table object public var rootObjectOffset : Offset? { do { return try getScalar(at: 0) as Offset @@ -185,18 +194,21 @@ public extension FBReader { } } +/// A FlatBuffers reader subclass that by default reads directly from a memory buffer, but also supports initialization from Data objects for convenience public struct FBMemoryReader : FBReader { private let count : Int public let cache : FBReaderCache? private let buffer : UnsafeRawPointer + /// Initialize the reader directly from a raw memory buffer public init(buffer : UnsafeRawPointer, count : Int, cache : FBReaderCache? = FBReaderCache()) { self.buffer = buffer self.count = count self.cache = cache } + /// Initialize the reader from a Data object, will probably keep a copy after https://github.com/mzaks/FlatBuffersSwift/issues/73 is resolved public init(data : Data, cache : FBReaderCache? = FBReaderCache()) { self.count = data.count self.cache = cache @@ -231,6 +243,7 @@ public struct FBMemoryReader : FBReader { } } +/// A FlatBuffers reader subclass that reads directly from a file handle public struct FBFileReader : FBReader { private let fileSize : UInt64 diff --git a/FlatBuffersSwiftTests/FBReaderTest.swift b/FlatBuffersSwiftTests/FBReaderTest.swift index 7773881..0a694a7 100644 --- a/FlatBuffersSwiftTests/FBReaderTest.swift +++ b/FlatBuffersSwiftTests/FBReaderTest.swift @@ -47,16 +47,16 @@ class FBReaderTest: XCTestCase { let sVectorOffset = reader.getOffset(objectOffset: objectOffset!, propertyIndex: 0) XCTAssertEqual(sVectorOffset, 32) - let sVectorLength = reader.getVectorLength(vectorOffset: sVectorOffset) + let sVectorLength = reader.getVectorElementCount(vectorOffset: sVectorOffset) XCTAssertEqual(sVectorLength, 3) - let sOffset1 = reader.getVectorOffsetElement(vectorOffset: sVectorOffset!, index: 0) + let sOffset1 = reader.getVectorElementOffset(vectorOffset: sVectorOffset!, index: 0) XCTAssertEqual(sOffset1, 48) - let sOffset2 = reader.getVectorOffsetElement(vectorOffset: sVectorOffset!, index: 1) + let sOffset2 = reader.getVectorElementOffset(vectorOffset: sVectorOffset!, index: 1) XCTAssertEqual(sOffset2, 56) - let sOffset3 = reader.getVectorOffsetElement(vectorOffset: sVectorOffset!, index: 2) + let sOffset3 = reader.getVectorElementOffset(vectorOffset: sVectorOffset!, index: 2) XCTAssertEqual(sOffset3, 64) let stringBuffer1 = reader.getStringBuffer(stringOffset: sOffset1) @@ -71,7 +71,7 @@ class FBReaderTest: XCTestCase { let bVectorOffset = reader.getOffset(objectOffset: objectOffset!, propertyIndex: 1) XCTAssertEqual(bVectorOffset, 24) - let bVectorLength = reader.getVectorLength(vectorOffset: bVectorOffset) + let bVectorLength = reader.getVectorElementCount(vectorOffset: bVectorOffset) XCTAssertEqual(bVectorLength, 2) let b1 : Bool? = reader.getVectorScalarElement(vectorOffset: bVectorOffset!, index: 0) @@ -184,16 +184,16 @@ class FBReaderTest: XCTestCase { let sVectorOffset = reader.getOffset(objectOffset: objectOffset!, propertyIndex: 0) XCTAssertEqual(sVectorOffset, 32) - let sVectorLength = reader.getVectorLength(vectorOffset: sVectorOffset) + let sVectorLength = reader.getVectorElementCount(vectorOffset: sVectorOffset) XCTAssertEqual(sVectorLength, 3) - let sOffset1 = reader.getVectorOffsetElement(vectorOffset: sVectorOffset!, index: 0) + let sOffset1 = reader.getVectorElementOffset(vectorOffset: sVectorOffset!, index: 0) XCTAssertEqual(sOffset1, 48) - let sOffset2 = reader.getVectorOffsetElement(vectorOffset: sVectorOffset!, index: 1) + let sOffset2 = reader.getVectorElementOffset(vectorOffset: sVectorOffset!, index: 1) XCTAssertEqual(sOffset2, 56) - let sOffset3 = reader.getVectorOffsetElement(vectorOffset: sVectorOffset!, index: 2) + let sOffset3 = reader.getVectorElementOffset(vectorOffset: sVectorOffset!, index: 2) XCTAssertEqual(sOffset3, 64) let stringBuffer1 = reader.getStringBuffer(stringOffset: sOffset1) @@ -208,7 +208,7 @@ class FBReaderTest: XCTestCase { let bVectorOffset = reader.getOffset(objectOffset: objectOffset!, propertyIndex: 1) XCTAssertEqual(bVectorOffset, 24) - let bVectorLength = reader.getVectorLength(vectorOffset: bVectorOffset) + let bVectorLength = reader.getVectorElementCount(vectorOffset: bVectorOffset) XCTAssertEqual(bVectorLength, 2) let b1 : Bool? = reader.getVectorScalarElement(vectorOffset: bVectorOffset!, index: 0) diff --git a/FlatBuffersSwiftTests/contacts_fb.swift b/FlatBuffersSwiftTests/contacts_fb.swift index ad2f2a0..35ce1a0 100644 --- a/FlatBuffersSwiftTests/contacts_fb.swift +++ b/FlatBuffersSwiftTests/contacts_fb.swift @@ -28,12 +28,12 @@ public extension ContactList { } _result.lastModified = reader.get(objectOffset: objectOffset, propertyIndex: 0, defaultValue: 0) let offset_entries : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 1) - let length_entries = reader.getVectorLength(vectorOffset: offset_entries) + let length_entries = reader.getVectorElementCount(vectorOffset: offset_entries) if(length_entries > 0){ var index = 0 _result.entries.reserveCapacity(length_entries) while index < length_entries { - let element = Contact.create(reader, objectOffset: reader.getVectorOffsetElement(vectorOffset: offset_entries, index: index)) + let element = Contact.create(reader, objectOffset: reader.getVectorElementOffset(vectorOffset: offset_entries, index: index)) _result.entries.append(element) index += 1 } @@ -82,11 +82,11 @@ public struct ContactList_Direct : Hashable { get { return reader.get(objectOffset: myOffset, propertyIndex: 0, defaultValue: 0) } } public var entriesCount : Int { - return reader.getVectorLength(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 1)) + return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 1)) } public func getEntriesElement(atIndex index : Int) -> Contact_Direct? { let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 1) - if let ofs = reader.getVectorOffsetElement(vectorOffset: offsetList, index: index) { + if let ofs = reader.getVectorElementOffset(vectorOffset: offsetList, index: index) { return Contact_Direct(reader: reader, myOffset: ofs) } return nil @@ -175,30 +175,30 @@ public extension Contact { _result.birthday = Date.create(reader, objectOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 1)) _result.gender = Gender(rawValue: reader.get(objectOffset: objectOffset, propertyIndex: 2, defaultValue: Gender.Male.rawValue)) let offset_tags : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 3) - let length_tags = reader.getVectorLength(vectorOffset: offset_tags) + let length_tags = reader.getVectorElementCount(vectorOffset: offset_tags) if(length_tags > 0){ var index = 0 _result.tags.reserveCapacity(length_tags) while index < length_tags { - let element = reader.getStringBuffer(stringOffset: reader.getVectorOffsetElement(vectorOffset: offset_tags, index: index))?§ + let element = reader.getStringBuffer(stringOffset: reader.getVectorElementOffset(vectorOffset: offset_tags, index: index))?§ _result.tags.append(element) index += 1 } } let offset_addressEntries : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 4) - let length_addressEntries = reader.getVectorLength(vectorOffset: offset_addressEntries) + let length_addressEntries = reader.getVectorElementCount(vectorOffset: offset_addressEntries) if(length_addressEntries > 0){ var index = 0 _result.addressEntries.reserveCapacity(length_addressEntries) while index < length_addressEntries { - let element = AddressEntry.create(reader, objectOffset: reader.getVectorOffsetElement(vectorOffset: offset_addressEntries, index: index)) + let element = AddressEntry.create(reader, objectOffset: reader.getVectorElementOffset(vectorOffset: offset_addressEntries, index: index)) _result.addressEntries.append(element) index += 1 } } _result.currentLoccation = reader.get(objectOffset: objectOffset, propertyIndex: 5) let offset_previousLocations : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 6) - let length_previousLocations = reader.getVectorLength(vectorOffset: offset_previousLocations) + let length_previousLocations = reader.getVectorElementCount(vectorOffset: offset_previousLocations) if(length_previousLocations > 0){ var index = 0 _result.previousLocations.reserveCapacity(length_previousLocations) @@ -209,7 +209,7 @@ public extension Contact { } } let offset_moods : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 7) - let length_moods = reader.getVectorLength(vectorOffset: offset_moods) + let length_moods = reader.getVectorElementCount(vectorOffset: offset_moods) if(length_moods > 0){ var index = 0 _result.moods.reserveCapacity(length_moods) @@ -244,21 +244,21 @@ public struct Contact_Direct : Hashable { get { return Gender(rawValue: reader.get(objectOffset: myOffset, propertyIndex: 2, defaultValue: Gender.Male.rawValue)) } } public var tagsCount : Int { - return reader.getVectorLength(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 3)) + return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 3)) } public func getTagsElement(atIndex index : Int) -> UnsafeBufferPointer? { let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 3) - if let ofs = reader.getVectorOffsetElement(vectorOffset: offsetList, index: index) { + if let ofs = reader.getVectorElementOffset(vectorOffset: offsetList, index: index) { return reader.getStringBuffer(stringOffset: ofs) } return nil } public var addressEntriesCount : Int { - return reader.getVectorLength(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 4)) + return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 4)) } public func getAddressEntriesElement(atIndex index : Int) -> AddressEntry_Direct? { let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 4) - if let ofs = reader.getVectorOffsetElement(vectorOffset: offsetList, index: index) { + if let ofs = reader.getVectorElementOffset(vectorOffset: offsetList, index: index) { return AddressEntry_Direct(reader: reader, myOffset: ofs) } return nil @@ -267,7 +267,7 @@ public struct Contact_Direct : Hashable { get { return reader.get(objectOffset: myOffset, propertyIndex: 5)} } public var previousLocationsCount : Int { - return reader.getVectorLength(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 6)) + return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 6)) } public func getPreviousLocationsElement(atIndex index : Int) -> GeoLocation? { let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 6) @@ -275,7 +275,7 @@ public struct Contact_Direct : Hashable { return result } public var moodsCount : Int { - return reader.getVectorLength(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 7)) + return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 7)) } public func getMoodsElement(atIndex index : Int) -> Mood? { let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 7) diff --git a/FlatBuffersSwiftTests/friends_fb.swift b/FlatBuffersSwiftTests/friends_fb.swift index 5460696..4b136dd 100644 --- a/FlatBuffersSwiftTests/friends_fb.swift +++ b/FlatBuffersSwiftTests/friends_fb.swift @@ -25,12 +25,12 @@ public extension PeopleList { cache.objectPool[objectOffset] = _result } let offset_people : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 0) - let length_people = reader.getVectorLength(vectorOffset: offset_people) + let length_people = reader.getVectorElementCount(vectorOffset: offset_people) if(length_people > 0){ var index = 0 _result.people.reserveCapacity(length_people) while index < length_people { - let element = Friend.create(reader, objectOffset: reader.getVectorOffsetElement(vectorOffset: offset_people, index: index)) + let element = Friend.create(reader, objectOffset: reader.getVectorElementOffset(vectorOffset: offset_people, index: index)) _result.people.append(element) index += 1 } @@ -77,11 +77,11 @@ public struct PeopleList_Direct : Hashable { self.myOffset = offest } public var peopleCount : Int { - return reader.getVectorLength(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 0)) + return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 0)) } public func getPeopleElement(atIndex index : Int) -> Friend_Direct? { let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 0) - if let ofs = reader.getVectorOffsetElement(vectorOffset: offsetList, index: index) { + if let ofs = reader.getVectorElementOffset(vectorOffset: offsetList, index: index) { return Friend_Direct(reader: reader, myOffset: ofs) } return nil @@ -174,12 +174,12 @@ public extension Friend { } _result.name = reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 0))?§ let offset_friends : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 1) - let length_friends = reader.getVectorLength(vectorOffset: offset_friends) + let length_friends = reader.getVectorElementCount(vectorOffset: offset_friends) if(length_friends > 0){ var index = 0 _result.friends.reserveCapacity(length_friends) while index < length_friends { - let element = Friend.create(reader, objectOffset: reader.getVectorOffsetElement(vectorOffset: offset_friends, index: index)) + let element = Friend.create(reader, objectOffset: reader.getVectorElementOffset(vectorOffset: offset_friends, index: index)) _result.friends.append(element) index += 1 } @@ -199,11 +199,11 @@ public struct Friend_Direct : Hashable { } public var name : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: myOffset, propertyIndex:0)) } } public var friendsCount : Int { - return reader.getVectorLength(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 1)) + return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 1)) } public func getFriendsElement(atIndex index : Int) -> Friend_Direct? { let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 1) - if let ofs = reader.getVectorOffsetElement(vectorOffset: offsetList, index: index) { + if let ofs = reader.getVectorElementOffset(vectorOffset: offsetList, index: index) { return Friend_Direct(reader: reader, myOffset: ofs) } return nil From 776a227aa9cfa9480a4481f18bd9e51a4fac4386 Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Tue, 10 Jan 2017 16:38:51 +0100 Subject: [PATCH 05/16] Start polishing the documentation to also doc parameters. --- FlatBuffersSwift/FlatBufferReader.swift | 54 ++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/FlatBuffersSwift/FlatBufferReader.swift b/FlatBuffersSwift/FlatBufferReader.swift index f2821f3..1f29285 100644 --- a/FlatBuffersSwift/FlatBufferReader.swift +++ b/FlatBuffersSwift/FlatBufferReader.swift @@ -11,16 +11,32 @@ import Foundation public protocol FBReader { var cache : FBReaderCache? {get} - /// **Returns** a scalar value at a given offset from the buffer + /** + Access a scalar value directly from the underlying reader buffer. + + - parameters: + - offset: The offset to read from in the buffer + + - Returns: a scalar value at a given offset from the buffer. + */ func getScalar(at offset: Int) throws -> T - /// **Returns** a direct pointer to a subrange from the underlying reader buffer + + /** + Access a subrange from the underlying reader buffer + + - parameters: + - offset: The offset to read from in the buffer + - length: The amount of data to include from the buffer + + - Returns: a direct pointer to a subrange from the underlying reader buffer. + */ func bytes(at offset : Int, length : Int) throws -> UnsafeBufferPointer func isEqual(other : FBReader) -> Bool } fileprivate enum FBReaderError : Error { - case outOfBufferBounds /// Trying to address outside of the bounds of the underlying buffer + case outOfBufferBounds /// Trying to address outside of the bounds of the underlying buffer case canNotSetProperty } @@ -33,7 +49,15 @@ public class FBReaderCache { } public extension FBReader { - /// **Returns** the object-local offset of a given property by looking it up in the vtable + /** + Retrieve the offset of a property from the vtable. + + - parameters: + - objectOffset: The offset of the object + - propertyIndex: The property to extract + + - Returns: the object-local offset of a given property by looking it up in the vtable + */ private func getPropertyOffset(objectOffset : Offset, propertyIndex : Int) -> Int { guard propertyIndex >= 0 else { return 0 @@ -201,14 +225,32 @@ public struct FBMemoryReader : FBReader { public let cache : FBReaderCache? private let buffer : UnsafeRawPointer - /// Initialize the reader directly from a raw memory buffer + /** + Initializes the reader directly from a raw memory buffer. + + - parameters: + - buffer: A raw pointer to the underlying data to be parsed + - count: The size of the data buffer + - cache: An optional cache of reader objects for reuse + + - Returns: A FB reader ready for use. + */ public init(buffer : UnsafeRawPointer, count : Int, cache : FBReaderCache? = FBReaderCache()) { self.buffer = buffer self.count = count self.cache = cache } - /// Initialize the reader from a Data object, will probably keep a copy after https://github.com/mzaks/FlatBuffersSwift/issues/73 is resolved + /** + Initializes the reader from a Data object. + This method will probably keep a copy after https://github.com/mzaks/FlatBuffersSwift/issues/73 is resolved + - parameters: + - data: A Data object holding the data to be parsed, the contents may be copied, for performance sensitive implementations, + the UnsafeRawsPointer initializer should be used. + - cache: An optional cache of reader objects for reuse + + - Returns: A FB reader ready for use. + */ public init(data : Data, cache : FBReaderCache? = FBReaderCache()) { self.count = data.count self.cache = cache From e6296565c960b341c1900a86ac2f65dc2ef71168 Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Tue, 10 Jan 2017 16:55:43 +0100 Subject: [PATCH 06/16] Remove obsolete bench.swift Remove the obsolete bench.swift file. --- FlatBuffersPerformanceTestDesktop/bench.swift | 506 ------------------ 1 file changed, 506 deletions(-) delete mode 100644 FlatBuffersPerformanceTestDesktop/bench.swift diff --git a/FlatBuffersPerformanceTestDesktop/bench.swift b/FlatBuffersPerformanceTestDesktop/bench.swift deleted file mode 100644 index 91d3d85..0000000 --- a/FlatBuffersPerformanceTestDesktop/bench.swift +++ /dev/null @@ -1,506 +0,0 @@ - -// generated with FlatBuffersSchemaEditor https://github.com/mzaks/FlatBuffersSchemaEditor - -import Foundation - -public enum Enum : Int16 { - case Apples, Pears, Bananas -} -public struct Foo : Scalar { - public let id : UInt64 - public let count : Int16 - public let prefix : Int8 - public let length : UInt32 -} -public func ==(v1:Foo, v2:Foo) -> Bool { - return v1.id==v2.id && v1.count==v2.count && v1.prefix==v2.prefix && v1.length==v2.length -} -public struct Bar : Scalar { - public let parent : Foo - public let time : Int32 - public let ratio : Float32 - public let size : UInt16 -} -public func ==(v1:Bar, v2:Bar) -> Bool { - return v1.parent==v2.parent && v1.time==v2.time && v1.ratio==v2.ratio && v1.size==v2.size -} -public final class FooBar { - public static var instancePoolMutex : pthread_mutex_t = FooBar.setupInstancePoolMutex() - public static var maxInstanceCacheSize : UInt = 0 - public static var instancePool : ContiguousArray = [] - public var sibling : Bar? = nil - public var name : String? { - get { - if let s = name_s { - return s - } - if let s = name_ss { - name_s = s.stringValue - } - if let s = name_b { - name_s = String.init(bytesNoCopy: UnsafeMutablePointer(s.baseAddress), length: s.count, encoding: NSUTF8StringEncoding, freeWhenDone: false) - } - return name_s - } - set { - name_s = newValue - name_ss = nil - name_b = nil - } - } - public func nameStaticString(newValue : StaticString) { - name_ss = newValue - name_s = nil - name_b = nil - } - private var name_b : UnsafeBufferPointer? = nil - public var nameBuffer : UnsafeBufferPointer? {return name_b} - private var name_s : String? = nil - private var name_ss : StaticString? = nil - - public var rating : Float64 = 0 - public var postfix : UInt8 = 0 - public init(){} - public init(sibling: Bar?, name: String?, rating: Float64, postfix: UInt8){ - self.sibling = sibling - self.name_s = name - self.rating = rating - self.postfix = postfix - } - public init(sibling: Bar?, name: StaticString?, rating: Float64, postfix: UInt8){ - self.sibling = sibling - self.name_ss = name - self.rating = rating - self.postfix = postfix - } -} - -extension FooBar : PoolableInstances { - public func reset() { - sibling = nil - name = nil - rating = 0 - postfix = 0 - } -} -public extension FooBar { - private static func create(reader : FlatBufferReader, objectOffset : Offset?) -> FooBar? { - guard let objectOffset = objectOffset else { - return nil - } - if reader.config.uniqueTables { - if let o = reader.objectPool[objectOffset]{ - return o as? FooBar - } - } - let _result = FooBar.createInstance() - if reader.config.uniqueTables { - reader.objectPool[objectOffset] = _result - } - _result.sibling = reader.get(objectOffset, propertyIndex: 0) - _result.name_b = reader.getStringBuffer(reader.getOffset(objectOffset, propertyIndex: 1)) - _result.rating = reader.get(objectOffset, propertyIndex: 2, defaultValue: 0) - _result.postfix = reader.get(objectOffset, propertyIndex: 3, defaultValue: 0) - return _result - } -} -public extension FooBar { - public final class LazyAccess : Hashable { - private let _reader : FlatBufferReader! - private let _objectOffset : Offset! - private init?(reader : FlatBufferReader, objectOffset : Offset?){ - guard let objectOffset = objectOffset else { - _reader = nil - _objectOffset = nil - return nil - } - _reader = reader - _objectOffset = objectOffset - } - - public var sibling : Bar? { - get { return self._reader.get(_objectOffset, propertyIndex: 0)} - set { - if let value = newValue{ - try!_reader.set(_objectOffset, propertyIndex: 0, value: value) - } - } - } - public lazy var name : String? = self._reader.getString(self._reader.getOffset(self._objectOffset, propertyIndex: 1)) - public var rating : Float64 { - get { return _reader.get(_objectOffset, propertyIndex: 2, defaultValue:0)} - set { try!_reader.set(_objectOffset, propertyIndex: 2, value: newValue)} - } - public var postfix : UInt8 { - get { return _reader.get(_objectOffset, propertyIndex: 3, defaultValue:0)} - set { try!_reader.set(_objectOffset, propertyIndex: 3, value: newValue)} - } - - public var createEagerVersion : FooBar? { return FooBar.create(_reader, objectOffset: _objectOffset) } - - public var hashValue: Int { return Int(_objectOffset) } - } -} - -public func ==(t1 : FooBar.LazyAccess, t2 : FooBar.LazyAccess) -> Bool { - return t1._objectOffset == t2._objectOffset && t1._reader === t2._reader -} - -extension FooBar { -public struct Fast : Hashable { - private let buffer : UnsafePointer - private let myOffset : Offset - public init(buffer: UnsafePointer, myOffset: Offset){ - self.buffer = buffer - self.myOffset = myOffset - } - public var sibling : Bar? { - get { return FlatBufferReaderFast.get(buffer, myOffset, propertyIndex: 0)} - set { - if let newValue = newValue { - try!FlatBufferReaderFast.set(UnsafeMutablePointer(buffer), myOffset, propertyIndex: 0, value: newValue) - } - } - } - public var name : UnsafeBufferPointer? { get { return FlatBufferReaderFast.getStringBuffer(buffer, FlatBufferReaderFast.getOffset(buffer, myOffset, propertyIndex:1)) } } - public var rating : Float64 { - get { return FlatBufferReaderFast.get(buffer, myOffset, propertyIndex: 2, defaultValue: 0) } - set { try!FlatBufferReaderFast.set(UnsafeMutablePointer(buffer), myOffset, propertyIndex: 2, value: newValue) } - } - public var postfix : UInt8 { - get { return FlatBufferReaderFast.get(buffer, myOffset, propertyIndex: 3, defaultValue: 0) } - set { try!FlatBufferReaderFast.set(UnsafeMutablePointer(buffer), myOffset, propertyIndex: 3, value: newValue) } - } - public var hashValue: Int { return Int(myOffset) } -} -} -public func ==(t1 : FooBar.Fast, t2 : FooBar.Fast) -> Bool { - return t1.buffer == t2.buffer && t1.myOffset == t2.myOffset -} -public extension FooBar { - private func addToByteArray(builder : FlatBufferBuilder) -> Offset { - if builder.config.uniqueTables { - if let myOffset = builder.cache[ObjectIdentifier(self)] { - return myOffset - } - } - // let offset1 = try! builder.createString(name) - var offset1 : Offset - if let s = name_b { - offset1 = try! builder.createString(s) - } else if let s = name_ss { - offset1 = try! builder.createStaticString(s) - } else { - offset1 = try! builder.createString(name) - } - try! builder.openObject(4) - try! builder.addPropertyToOpenObject(3, value : postfix, defaultValue : 0) - try! builder.addPropertyToOpenObject(2, value : rating, defaultValue : 0) - try! builder.addPropertyOffsetToOpenObject(1, offset: offset1) - if let sibling = sibling { - builder.put(sibling) - try! builder.addCurrentOffsetAsPropertyToOpenObject(0) - } - let myOffset = try! builder.closeObject() - if builder.config.uniqueTables { - builder.cache[ObjectIdentifier(self)] = myOffset - } - return myOffset - } -} -public final class FooBarContainer { - public static var instancePoolMutex : pthread_mutex_t = FooBarContainer.setupInstancePoolMutex() - public static var maxInstanceCacheSize : UInt = 0 - public static var instancePool : ContiguousArray = [] - public var list : ContiguousArray = [] - public var initialized : Bool = false - public var fruit : Enum? = Enum.Apples - public var location : String? { - get { - if let s = location_s { - return s - } - if let s = location_ss { - location_s = s.stringValue - } - if let s = location_b { - location_s = String.init(bytesNoCopy: UnsafeMutablePointer(s.baseAddress), length: s.count, encoding: NSUTF8StringEncoding, freeWhenDone: false) - } - return location_s - } - set { - location_s = newValue - location_ss = nil - location_b = nil - } - } - public func locationStaticString(newValue : StaticString) { - location_ss = newValue - location_s = nil - location_b = nil - } - private var location_b : UnsafeBufferPointer? = nil - public var locationBuffer : UnsafeBufferPointer? {return location_b} - private var location_s : String? = nil - private var location_ss : StaticString? = nil - - public init(){} - public init(list: ContiguousArray, initialized: Bool, fruit: Enum?, location: String?){ - self.list = list - self.initialized = initialized - self.fruit = fruit - self.location_s = location - } - public init(list: ContiguousArray, initialized: Bool, fruit: Enum?, location: StaticString?){ - self.list = list - self.initialized = initialized - self.fruit = fruit - self.location_ss = location - } -} - -extension FooBarContainer : PoolableInstances { - public func reset() { - while (list.count > 0) { - var x = list.removeLast()! - FooBar.reuseInstance(&x) - } - initialized = false - fruit = Enum.Apples - location = nil - } -} -public extension FooBarContainer { - private static func create(reader : FlatBufferReader, objectOffset : Offset?) -> FooBarContainer? { - guard let objectOffset = objectOffset else { - return nil - } - if reader.config.uniqueTables { - if let o = reader.objectPool[objectOffset]{ - return o as? FooBarContainer - } - } - let _result = FooBarContainer.createInstance() - if reader.config.uniqueTables { - reader.objectPool[objectOffset] = _result - } - let offset_list : Offset? = reader.getOffset(objectOffset, propertyIndex: 0) - let length_list = reader.getVectorLength(offset_list) - if(length_list > 0){ - var index = 0 - _result.list.reserveCapacity(length_list) - while index < length_list { - _result.list.append(FooBar.create(reader, objectOffset: reader.getVectorOffsetElement(offset_list!, index: index))) - index += 1 - } - } - _result.initialized = reader.get(objectOffset, propertyIndex: 1, defaultValue: false) - _result.fruit = Enum(rawValue: reader.get(objectOffset, propertyIndex: 2, defaultValue: Enum.Apples.rawValue)) - _result.location_b = reader.getStringBuffer(reader.getOffset(objectOffset, propertyIndex: 3)) - return _result - } -} -public extension FooBarContainer { - public static func fromByteArray(data : UnsafeBufferPointer, config : BinaryReadConfig = BinaryReadConfig()) -> FooBarContainer { - let reader = FlatBufferReader.create(data, config: config) - let objectOffset = reader.rootObjectOffset - let result = create(reader, objectOffset : objectOffset)! - FlatBufferReader.reuse(reader) - return result - } - public static func fromRawMemory(data : UnsafeMutablePointer, count : Int, config : BinaryReadConfig = BinaryReadConfig()) -> FooBarContainer { - let reader = FlatBufferReader.create(data, count: count, config: config) - let objectOffset = reader.rootObjectOffset - let result = create(reader, objectOffset : objectOffset)! - FlatBufferReader.reuse(reader) - return result - } - public static func fromFlatBufferReader(flatBufferReader : FlatBufferReader) -> FooBarContainer { - return create(flatBufferReader, objectOffset : flatBufferReader.rootObjectOffset)! - } -} -public extension FooBarContainer { - public func toByteArray (config : BinaryBuildConfig = BinaryBuildConfig()) -> [UInt8] { - let builder = FlatBufferBuilder.create(config) - let offset = addToByteArray(builder) - performLateBindings(builder) - try! builder.finish(offset, fileIdentifier: nil) - let result = builder.data - FlatBufferBuilder.reuse(builder) - return result - } -} - -public extension FooBarContainer { - public func toFlatBufferBuilder (builder : FlatBufferBuilder) -> Void { - let offset = addToByteArray(builder) - performLateBindings(builder) - try! builder.finish(offset, fileIdentifier: nil) - } -} - -public extension FooBarContainer { - public final class LazyAccess : Hashable { - private let _reader : FlatBufferReader! - private let _objectOffset : Offset! - public init(data : UnsafeBufferPointer, config : BinaryReadConfig = BinaryReadConfig()){ - _reader = FlatBufferReader.create(data, config: config) - _objectOffset = _reader.rootObjectOffset - } - deinit{ - FlatBufferReader.reuse(_reader) - } - public var data : [UInt8] { - return _reader.data - } - private init?(reader : FlatBufferReader, objectOffset : Offset?){ - guard let objectOffset = objectOffset else { - _reader = nil - _objectOffset = nil - return nil - } - _reader = reader - _objectOffset = objectOffset - } - - public lazy var list : LazyVector = { [self] - let vectorOffset : Offset? = self._reader.getOffset(self._objectOffset, propertyIndex: 0) - let vectorLength = self._reader.getVectorLength(vectorOffset) - let reader = self._reader - return LazyVector(count: vectorLength){ [reader] in - FooBar.LazyAccess(reader: reader, objectOffset : reader.getVectorOffsetElement(vectorOffset!, index: $0)) - } - }() - public var initialized : Bool { - get { return _reader.get(_objectOffset, propertyIndex: 1, defaultValue:false)} - set { try!_reader.set(_objectOffset, propertyIndex: 1, value: newValue)} - } - public var fruit : Enum? { - get { return Enum(rawValue: _reader.get(self._objectOffset, propertyIndex: 2, defaultValue:Enum.Apples.rawValue))} - set { - if let value = newValue{ - try!_reader.set(_objectOffset, propertyIndex: 2, value: value.rawValue) - } - } - } - public lazy var location : String? = self._reader.getString(self._reader.getOffset(self._objectOffset, propertyIndex: 3)) - - public var createEagerVersion : FooBarContainer? { return FooBarContainer.create(_reader, objectOffset: _objectOffset) } - - public var hashValue: Int { return Int(_objectOffset) } - } -} - -public func ==(t1 : FooBarContainer.LazyAccess, t2 : FooBarContainer.LazyAccess) -> Bool { - return t1._objectOffset == t2._objectOffset && t1._reader === t2._reader -} - -extension FooBarContainer { -public struct Fast : Hashable { - private let buffer : UnsafePointer - private let myOffset : Offset - public init(buffer: UnsafePointer, myOffset: Offset){ - self.buffer = buffer - self.myOffset = myOffset - } - public init(_ data : UnsafePointer) { - self.buffer = data - self.myOffset = UnsafePointer(buffer.advancedBy(0)).memory - } - public func getData() -> UnsafePointer { - return buffer - } - public struct ListVector { - private let buffer : UnsafePointer - private let myOffset : Offset - private let offsetList : Offset? - private init(buffer b: UnsafePointer, myOffset o: Offset ) { - buffer = b - myOffset = o - offsetList = FlatBufferReaderFast.getOffset(buffer, myOffset, propertyIndex: 0) - } - public var count : Int { get { return FlatBufferReaderFast.getVectorLength(buffer, offsetList) } } - public subscript (index : Int) -> FooBar.Fast? { - get { - if let ofs = FlatBufferReaderFast.getVectorOffsetElement(buffer, offsetList!, index: index) { - return FooBar.Fast(buffer: buffer, myOffset: ofs) - } - return nil - } - } - } - public lazy var list : ListVector = ListVector(buffer: self.buffer, myOffset: self.myOffset) - public var initialized : Bool { - get { return FlatBufferReaderFast.get(buffer, myOffset, propertyIndex: 1, defaultValue: false) } - set { try!FlatBufferReaderFast.set(UnsafeMutablePointer(buffer), myOffset, propertyIndex: 1, value: newValue) } - } - public var fruit : Enum? { - get { return Enum(rawValue: FlatBufferReaderFast.get(buffer, myOffset, propertyIndex: 2, defaultValue: Enum.Apples.rawValue)) } - set { - if let newValue = newValue { - try!FlatBufferReaderFast.set(UnsafeMutablePointer(buffer), myOffset, propertyIndex: 2, value: newValue.rawValue) - } - } - } - public var location : UnsafeBufferPointer? { get { return FlatBufferReaderFast.getStringBuffer(buffer, FlatBufferReaderFast.getOffset(buffer, myOffset, propertyIndex:3)) } } - public var hashValue: Int { return Int(myOffset) } -} -} -public func ==(t1 : FooBarContainer.Fast, t2 : FooBarContainer.Fast) -> Bool { - return t1.buffer == t2.buffer && t1.myOffset == t2.myOffset -} -public extension FooBarContainer { - private func addToByteArray(builder : FlatBufferBuilder) -> Offset { - if builder.config.uniqueTables { - if let myOffset = builder.cache[ObjectIdentifier(self)] { - return myOffset - } - } - // let offset3 = try! builder.createString(location) - var offset3 : Offset - if let s = location_b { - offset3 = try! builder.createString(s) - } else if let s = location_ss { - offset3 = try! builder.createStaticString(s) - } else { - offset3 = try! builder.createString(location) - } - var offset0 = Offset(0) - if list.count > 0{ - var offsets = [Offset?](count: list.count, repeatedValue: nil) - var index = list.count - 1 - while(index >= 0){ - offsets[index] = list[index]?.addToByteArray(builder) - index -= 1 - } - try! builder.startVector(list.count) - index = list.count - 1 - while(index >= 0){ - try! builder.putOffset(offsets[index]) - index -= 1 - } - offset0 = builder.endVector() - } - try! builder.openObject(4) - try! builder.addPropertyOffsetToOpenObject(3, offset: offset3) - try! builder.addPropertyToOpenObject(2, value : fruit!.rawValue, defaultValue : 0) - try! builder.addPropertyToOpenObject(1, value : initialized, defaultValue : false) - if list.count > 0 { - try! builder.addPropertyOffsetToOpenObject(0, offset: offset0) - } - let myOffset = try! builder.closeObject() - if builder.config.uniqueTables { - builder.cache[ObjectIdentifier(self)] = myOffset - } - return myOffset - } -} -private func performLateBindings(builder : FlatBufferBuilder) { - for binding in builder.deferedBindings { - switch binding.object { - case let object as FooBar: try! builder.replaceOffset(object.addToByteArray(builder), atCursor: binding.cursor) - case let object as FooBarContainer: try! builder.replaceOffset(object.addToByteArray(builder), atCursor: binding.cursor) - default: continue - } - } -} From 60589c8e5eb81d55b8c04fdd410e13d7457513d4 Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Tue, 10 Jan 2017 17:50:23 +0100 Subject: [PATCH 07/16] Spell out acronyms (FB -> FlatBuffers) according to Swift API guidelines, rename source files accordingly. Start dropping get prefix. --- FlatBuffersSwift.xcodeproj/project.pbxproj | 16 +-- ...Builder.swift => FlatBuffersBuilder.swift} | 0 ...erReader.swift => FlatBuffersReader.swift} | 111 +++++++------- FlatBuffersSwiftTests/FBReaderTest.swift | 44 +++--- FlatBuffersSwiftTests/contacts_fb.swift | 136 +++++++++--------- FlatBuffersSwiftTests/friends_fb.swift | 72 +++++----- 6 files changed, 190 insertions(+), 189 deletions(-) rename FlatBuffersSwift/{FlatBufferBuilder.swift => FlatBuffersBuilder.swift} (100%) rename FlatBuffersSwift/{FlatBufferReader.swift => FlatBuffersReader.swift} (72%) diff --git a/FlatBuffersSwift.xcodeproj/project.pbxproj b/FlatBuffersSwift.xcodeproj/project.pbxproj index 375b028..870b5e0 100644 --- a/FlatBuffersSwift.xcodeproj/project.pbxproj +++ b/FlatBuffersSwift.xcodeproj/project.pbxproj @@ -7,7 +7,7 @@ objects = { /* Begin PBXBuildFile section */ - 0B57C1291DCE98F00074EE0E /* FlatBufferReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0BA0E86A1C32B6AD00B896B7 /* FlatBufferReader.swift */; }; + 0B57C1291DCE98F00074EE0E /* FlatBuffersReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0BA0E86A1C32B6AD00B896B7 /* FlatBuffersReader.swift */; }; 0B57C12B1DCE99650074EE0E /* BuilderTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B57C12A1DCE99650074EE0E /* BuilderTest.swift */; }; 0B57C12D1DCE9A2D0074EE0E /* FBReaderTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B57C12C1DCE9A2D0074EE0E /* FBReaderTest.swift */; }; 0B57C12F1DCE9AA80074EE0E /* contacts.fbs in Resources */ = {isa = PBXBuildFile; fileRef = 0B57C12E1DCE9AA80074EE0E /* contacts.fbs */; }; @@ -16,7 +16,7 @@ 0B57C1351DCE9C490074EE0E /* friends_fb.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B57C1331DCE9C490074EE0E /* friends_fb.swift */; }; 0B57C1371DCE9C970074EE0E /* ContactsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B57C1361DCE9C970074EE0E /* ContactsTest.swift */; }; 0B57C1391DCE9CF80074EE0E /* FriendsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B57C1381DCE9CF80074EE0E /* FriendsTest.swift */; }; - 0B62C1F31C43F6A2002500FE /* FlatBufferBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B62C1F01C43F1CD002500FE /* FlatBufferBuilder.swift */; }; + 0B62C1F31C43F6A2002500FE /* FlatBuffersBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B62C1F01C43F1CD002500FE /* FlatBuffersBuilder.swift */; }; 0BA0E8491C32B62200B896B7 /* FlatBuffersSwift.h in Headers */ = {isa = PBXBuildFile; fileRef = 0BA0E8481C32B62200B896B7 /* FlatBuffersSwift.h */; settings = {ATTRIBUTES = (Public, ); }; }; 0BE327781CD69BA00067DAC8 /* FlatBuffersSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0BA0E8451C32B62200B896B7 /* FlatBuffersSwift.framework */; }; /* End PBXBuildFile section */ @@ -40,13 +40,13 @@ 0B57C1331DCE9C490074EE0E /* friends_fb.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = friends_fb.swift; sourceTree = ""; }; 0B57C1361DCE9C970074EE0E /* ContactsTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ContactsTest.swift; sourceTree = ""; }; 0B57C1381DCE9CF80074EE0E /* FriendsTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FriendsTest.swift; sourceTree = ""; }; - 0B62C1F01C43F1CD002500FE /* FlatBufferBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlatBufferBuilder.swift; sourceTree = ""; }; + 0B62C1F01C43F1CD002500FE /* FlatBuffersBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlatBuffersBuilder.swift; sourceTree = ""; }; 0BA0E8451C32B62200B896B7 /* FlatBuffersSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FlatBuffersSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 0BA0E8481C32B62200B896B7 /* FlatBuffersSwift.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FlatBuffersSwift.h; sourceTree = ""; }; 0BA0E84A1C32B62200B896B7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 0BA0E84F1C32B62200B896B7 /* FlatBuffersSwiftTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FlatBuffersSwiftTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 0BA0E8561C32B62200B896B7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 0BA0E86A1C32B6AD00B896B7 /* FlatBufferReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlatBufferReader.swift; sourceTree = ""; }; + 0BA0E86A1C32B6AD00B896B7 /* FlatBuffersReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlatBuffersReader.swift; sourceTree = ""; }; 85EE88611CDE83E00030231F /* generate.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = generate.sh; sourceTree = SOURCE_ROOT; }; 85EE88621CDE84640030231F /* .travis.yml */ = {isa = PBXFileReference; lastKnownFileType = text; name = .travis.yml; path = ../.travis.yml; sourceTree = ""; }; 85EE887C1CE5128E0030231F /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = SOURCE_ROOT; }; @@ -94,8 +94,8 @@ 0BA0E8471C32B62200B896B7 /* FlatBuffersSwift */ = { isa = PBXGroup; children = ( - 0B62C1F01C43F1CD002500FE /* FlatBufferBuilder.swift */, - 0BA0E86A1C32B6AD00B896B7 /* FlatBufferReader.swift */, + 0B62C1F01C43F1CD002500FE /* FlatBuffersBuilder.swift */, + 0BA0E86A1C32B6AD00B896B7 /* FlatBuffersReader.swift */, 0BA0E8481C32B62200B896B7 /* FlatBuffersSwift.h */, 0BA0E84A1C32B62200B896B7 /* Info.plist */, ); @@ -242,8 +242,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 0B57C1291DCE98F00074EE0E /* FlatBufferReader.swift in Sources */, - 0B62C1F31C43F6A2002500FE /* FlatBufferBuilder.swift in Sources */, + 0B57C1291DCE98F00074EE0E /* FlatBuffersReader.swift in Sources */, + 0B62C1F31C43F6A2002500FE /* FlatBuffersBuilder.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/FlatBuffersSwift/FlatBufferBuilder.swift b/FlatBuffersSwift/FlatBuffersBuilder.swift similarity index 100% rename from FlatBuffersSwift/FlatBufferBuilder.swift rename to FlatBuffersSwift/FlatBuffersBuilder.swift diff --git a/FlatBuffersSwift/FlatBufferReader.swift b/FlatBuffersSwift/FlatBuffersReader.swift similarity index 72% rename from FlatBuffersSwift/FlatBufferReader.swift rename to FlatBuffersSwift/FlatBuffersReader.swift index 1f29285..3070baf 100644 --- a/FlatBuffersSwift/FlatBufferReader.swift +++ b/FlatBuffersSwift/FlatBuffersReader.swift @@ -1,15 +1,16 @@ // -// Reader.swift +// FlatBuffersReader.swift // FBSwift3 // // Created by Maxim Zaks on 28.10.16. // Copyright © 2016 Maxim Zaks. All rights reserved. // +// See https://github.com/mzaks/FlatBuffersSwift/graphs/contributors for contributors. import Foundation -public protocol FBReader { - var cache : FBReaderCache? {get} +public protocol FlatBuffersReader { + var cache : FlatBuffersReaderCache? {get} /** Access a scalar value directly from the underlying reader buffer. @@ -19,7 +20,7 @@ public protocol FBReader { - Returns: a scalar value at a given offset from the buffer. */ - func getScalar(at offset: Int) throws -> T + func scalar(at offset: Int) throws -> T /** Access a subrange from the underlying reader buffer @@ -31,16 +32,16 @@ public protocol FBReader { - Returns: a direct pointer to a subrange from the underlying reader buffer. */ func bytes(at offset : Int, length : Int) throws -> UnsafeBufferPointer - func isEqual(other : FBReader) -> Bool + func isEqual(other : FlatBuffersReader) -> Bool } -fileprivate enum FBReaderError : Error { +fileprivate enum FlatBuffersReaderError : Error { case outOfBufferBounds /// Trying to address outside of the bounds of the underlying buffer case canNotSetProperty } -public class FBReaderCache { +public class FlatBuffersReaderCache { public var objectPool : [Offset : AnyObject] = [:] func reset(){ objectPool.removeAll(keepingCapacity: true) @@ -48,7 +49,7 @@ public class FBReaderCache { public init(){} } -public extension FBReader { +public extension FlatBuffersReader { /** Retrieve the offset of a property from the vtable. @@ -58,42 +59,42 @@ public extension FBReader { - Returns: the object-local offset of a given property by looking it up in the vtable */ - private func getPropertyOffset(objectOffset : Offset, propertyIndex : Int) -> Int { + private func propertyOffset(objectOffset : Offset, propertyIndex : Int) -> Int { guard propertyIndex >= 0 else { return 0 } do { let offset = Int(objectOffset) - let localOffset : Int32 = try getScalar(at: offset) + let localOffset : Int32 = try scalar(at: offset) let vTableOffset : Int = offset - Int(localOffset) - let vTableLength : Int16 = try getScalar(at: vTableOffset) - let objectLength : Int16 = try getScalar(at: vTableOffset + 2) + let vTableLength : Int16 = try scalar(at: vTableOffset) + let objectLength : Int16 = try scalar(at: vTableOffset + 2) let positionInVTable = 4 + propertyIndex * 2 - if(vTableLength<=Int16(positionInVTable)) { + if (vTableLength<=Int16(positionInVTable)) { return 0 } let propertyStart = vTableOffset + positionInVTable - let propertyOffset : Int16 = try getScalar(at: propertyStart) - if(objectLength<=propertyOffset) { + let propOffset : Int16 = try scalar(at: propertyStart) + if (objectLength<=propOffset) { return 0 } - return Int(propertyOffset) + return Int(propOffset) } catch { return 0 // Currently don't want to propagate the error } } /// **Returns** the final offset in the reader buffer to access a given property for a given object-offset - public func getOffset(objectOffset : Offset, propertyIndex : Int) -> Offset? { + public func offset(objectOffset : Offset, propertyIndex : Int) -> Offset? { - let propertyOffset = getPropertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) - if propertyOffset == 0 { + let propOffset = propertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) + if propOffset == 0 { return nil } - let position = objectOffset + propertyOffset + let position = objectOffset + propOffset do { - let localObjectOffset : Int32 = try getScalar(at: Int(position)) + let localObjectOffset : Int32 = try scalar(at: Int(position)) let offset = position + localObjectOffset if localObjectOffset == 0 { @@ -107,13 +108,13 @@ public extension FBReader { } /// **Returns** the length of vector - public func getVectorElementCount(vectorOffset : Offset?) -> Int { + public func vectorElementCount(vectorOffset : Offset?) -> Int { guard let vectorOffset = vectorOffset else { return 0 } let vectorPosition = Int(vectorOffset) do { - let length2 : Int32 = try getScalar(at: vectorPosition) + let length2 : Int32 = try scalar(at: vectorPosition) return Int(length2) } catch { return 0 @@ -121,19 +122,19 @@ public extension FBReader { } /// **Returns** the offset in the buffer for a given vector element - public func getVectorElementOffset(vectorOffset : Offset?, index : Int) -> Offset? { + public func vectorElementOffset(vectorOffset : Offset?, index : Int) -> Offset? { guard let vectorOffset = vectorOffset else { return nil } guard index >= 0 else{ return nil } - guard index < getVectorElementCount(vectorOffset: vectorOffset) else { + guard index < vectorElementCount(vectorOffset: vectorOffset) else { return nil } let valueStartPosition = Int(vectorOffset + MemoryLayout.stride + (index * MemoryLayout.stride)) do { - let localOffset : Int32 = try getScalar(at: valueStartPosition) + let localOffset : Int32 = try scalar(at: valueStartPosition) if(localOffset == 0){ return nil } @@ -144,21 +145,21 @@ public extension FBReader { } /// **Returns** a scalar value directly from a vector for a given index - public func getVectorScalarElement(vectorOffset : Offset?, index : Int) -> T? { + public func vectorScalarElement(vectorOffset : Offset?, index : Int) -> T? { guard let vectorOffset = vectorOffset else { return nil } guard index >= 0 else{ return nil } - guard index < getVectorElementCount(vectorOffset: vectorOffset) else { + guard index < vectorElementCount(vectorOffset: vectorOffset) else { return nil } let valueStartPosition = Int(vectorOffset + MemoryLayout.stride + (index * MemoryLayout.stride)) do { - return try getScalar(at: valueStartPosition) as T + return try scalar(at: valueStartPosition) as T } catch { return nil } @@ -166,13 +167,13 @@ public extension FBReader { /// **Returns** a scalar value directly from a vector for a given index public func get(objectOffset : Offset, propertyIndex : Int, defaultValue : T) -> T { - let propertyOffset = getPropertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) - if propertyOffset == 0 { + let propOffset = propertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) + if propOffset == 0 { return defaultValue } - let position = Int(objectOffset + propertyOffset) + let position = Int(objectOffset + propOffset) do { - return try getScalar(at: position) + return try scalar(at: position) } catch { return defaultValue } @@ -180,13 +181,13 @@ public extension FBReader { /// **Returns** a scalar value for a given property from an object public func get(objectOffset : Offset, propertyIndex : Int) -> T? { - let propertyOffset = getPropertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) - if propertyOffset == 0 { + let propOffset = propertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) + if propOffset == 0 { return nil } - let position = Int(objectOffset + propertyOffset) + let position = Int(objectOffset + propOffset) do { - return try getScalar(at: position) as T + return try scalar(at: position) as T } catch { return nil } @@ -199,7 +200,7 @@ public extension FBReader { } let stringPosition = Int(stringOffset) do { - let stringLength : Int32 = try getScalar(at: stringPosition) + let stringLength : Int32 = try scalar(at: stringPosition) let stringCharactersPosition = stringPosition + MemoryLayout.stride return try bytes(at: stringCharactersPosition, length: Int(stringLength)) @@ -211,7 +212,7 @@ public extension FBReader { /// **Returns** the offset for the root table object public var rootObjectOffset : Offset? { do { - return try getScalar(at: 0) as Offset + return try scalar(at: 0) as Offset } catch { return nil } @@ -219,10 +220,10 @@ public extension FBReader { } /// A FlatBuffers reader subclass that by default reads directly from a memory buffer, but also supports initialization from Data objects for convenience -public struct FBMemoryReader : FBReader { +public struct FBMemoryReader : FlatBuffersReader { private let count : Int - public let cache : FBReaderCache? + public let cache : FlatBuffersReaderCache? private let buffer : UnsafeRawPointer /** @@ -235,7 +236,7 @@ public struct FBMemoryReader : FBReader { - Returns: A FB reader ready for use. */ - public init(buffer : UnsafeRawPointer, count : Int, cache : FBReaderCache? = FBReaderCache()) { + public init(buffer : UnsafeRawPointer, count : Int, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()) { self.buffer = buffer self.count = count self.cache = cache @@ -251,7 +252,7 @@ public struct FBMemoryReader : FBReader { - Returns: A FB reader ready for use. */ - public init(data : Data, cache : FBReaderCache? = FBReaderCache()) { + public init(data : Data, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()) { self.count = data.count self.cache = cache var pointer : UnsafePointer! = nil @@ -261,9 +262,9 @@ public struct FBMemoryReader : FBReader { self.buffer = UnsafeRawPointer(pointer) } - public func getScalar(at offset: Int) throws -> T { + public func scalar(at offset: Int) throws -> T { if offset + MemoryLayout.stride > count || offset < 0 { - throw FBReaderError.outOfBufferBounds + throw FlatBuffersReaderError.outOfBufferBounds } return buffer.load(fromByteOffset: offset, as: T.self) @@ -271,13 +272,13 @@ public struct FBMemoryReader : FBReader { public func bytes(at offset : Int, length : Int) throws -> UnsafeBufferPointer { if Int(offset + length) > count { - throw FBReaderError.outOfBufferBounds + throw FlatBuffersReaderError.outOfBufferBounds } let pointer = buffer.advanced(by:offset).bindMemory(to: UInt8.self, capacity: length) return UnsafeBufferPointer.init(start: pointer, count: Int(length)) } - public func isEqual(other: FBReader) -> Bool{ + public func isEqual(other: FlatBuffersReader) -> Bool{ guard let other = other as? FBMemoryReader else { return false } @@ -286,23 +287,23 @@ public struct FBMemoryReader : FBReader { } /// A FlatBuffers reader subclass that reads directly from a file handle -public struct FBFileReader : FBReader { +public struct FBFileReader : FlatBuffersReader { private let fileSize : UInt64 private let fileHandle : FileHandle - public let cache : FBReaderCache? + public let cache : FlatBuffersReaderCache? - public init(fileHandle : FileHandle, cache : FBReaderCache? = FBReaderCache()){ + public init(fileHandle : FileHandle, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()){ self.fileHandle = fileHandle fileSize = fileHandle.seekToEndOfFile() self.cache = cache } - public func getScalar(at offset: Int) throws -> T { + public func scalar(at offset: Int) throws -> T { let seekPosition = UInt64(offset) if seekPosition + UInt64(MemoryLayout.stride) > fileSize { - throw FBReaderError.outOfBufferBounds + throw FlatBuffersReaderError.outOfBufferBounds } fileHandle.seek(toFileOffset: seekPosition) let data = fileHandle.readData(ofLength:MemoryLayout.stride) @@ -313,12 +314,12 @@ public struct FBFileReader : FBReader { pointer.deinitialize() return result } - throw FBReaderError.outOfBufferBounds + throw FlatBuffersReaderError.outOfBufferBounds } public func bytes(at offset : Int, length : Int) throws -> UnsafeBufferPointer { if UInt64(offset + length) > fileSize { - throw FBReaderError.outOfBufferBounds + throw FlatBuffersReaderError.outOfBufferBounds } fileHandle.seek(toFileOffset: UInt64(offset)) let data = fileHandle.readData(ofLength:Int(length)) @@ -329,7 +330,7 @@ public struct FBFileReader : FBReader { return UnsafeBufferPointer(start: t.baseAddress, count: length) } - public func isEqual(other: FBReader) -> Bool{ + public func isEqual(other: FlatBuffersReader) -> Bool{ guard let other = other as? FBFileReader else { return false } diff --git a/FlatBuffersSwiftTests/FBReaderTest.swift b/FlatBuffersSwiftTests/FBReaderTest.swift index 0a694a7..3255768 100644 --- a/FlatBuffersSwiftTests/FBReaderTest.swift +++ b/FlatBuffersSwiftTests/FBReaderTest.swift @@ -9,7 +9,7 @@ import XCTest @testable import FlatBuffersSwift -class FBReaderTest: XCTestCase { +class FlatBuffersReaderTest: XCTestCase { func testReadDirect() { @@ -20,7 +20,7 @@ class FBReaderTest: XCTestCase { let objectOffset = reader.rootObjectOffset XCTAssertEqual(objectOffset, 16) - let stringOffset = reader.getOffset(objectOffset: objectOffset!, propertyIndex: 1) + let stringOffset = reader.offset(objectOffset: objectOffset!, propertyIndex: 1) XCTAssertEqual(stringOffset, 28) let stringBuffer = reader.getStringBuffer(stringOffset: stringOffset) @@ -44,19 +44,19 @@ class FBReaderTest: XCTestCase { let objectOffset = reader.rootObjectOffset XCTAssertEqual(objectOffset, 12) - let sVectorOffset = reader.getOffset(objectOffset: objectOffset!, propertyIndex: 0) + let sVectorOffset = reader.offset(objectOffset: objectOffset!, propertyIndex: 0) XCTAssertEqual(sVectorOffset, 32) - let sVectorLength = reader.getVectorElementCount(vectorOffset: sVectorOffset) + let sVectorLength = reader.vectorElementCount(vectorOffset: sVectorOffset) XCTAssertEqual(sVectorLength, 3) - let sOffset1 = reader.getVectorElementOffset(vectorOffset: sVectorOffset!, index: 0) + let sOffset1 = reader.vectorElementOffset(vectorOffset: sVectorOffset!, index: 0) XCTAssertEqual(sOffset1, 48) - let sOffset2 = reader.getVectorElementOffset(vectorOffset: sVectorOffset!, index: 1) + let sOffset2 = reader.vectorElementOffset(vectorOffset: sVectorOffset!, index: 1) XCTAssertEqual(sOffset2, 56) - let sOffset3 = reader.getVectorElementOffset(vectorOffset: sVectorOffset!, index: 2) + let sOffset3 = reader.vectorElementOffset(vectorOffset: sVectorOffset!, index: 2) XCTAssertEqual(sOffset3, 64) let stringBuffer1 = reader.getStringBuffer(stringOffset: sOffset1) @@ -68,16 +68,16 @@ class FBReaderTest: XCTestCase { let stringBuffer3 = reader.getStringBuffer(stringOffset: sOffset3) XCTAssertEqual(stringBuffer3?§, "max1") - let bVectorOffset = reader.getOffset(objectOffset: objectOffset!, propertyIndex: 1) + let bVectorOffset = reader.offset(objectOffset: objectOffset!, propertyIndex: 1) XCTAssertEqual(bVectorOffset, 24) - let bVectorLength = reader.getVectorElementCount(vectorOffset: bVectorOffset) + let bVectorLength = reader.vectorElementCount(vectorOffset: bVectorOffset) XCTAssertEqual(bVectorLength, 2) - let b1 : Bool? = reader.getVectorScalarElement(vectorOffset: bVectorOffset!, index: 0) + let b1 : Bool? = reader.vectorScalarElement(vectorOffset: bVectorOffset!, index: 0) XCTAssertEqual(b1, false) - let b2 : Bool? = reader.getVectorScalarElement(vectorOffset: bVectorOffset!, index: 1) + let b2 : Bool? = reader.vectorScalarElement(vectorOffset: bVectorOffset!, index: 1) XCTAssertEqual(b2, true) } @@ -117,7 +117,7 @@ class FBReaderTest: XCTestCase { let reader = FBMemoryReader(data: data) let root = reader.rootObjectOffset - let o = reader.getOffset(objectOffset: root!, propertyIndex: -1) + let o = reader.offset(objectOffset: root!, propertyIndex: -1) XCTAssertNil(o) } @@ -147,7 +147,7 @@ class FBReaderTest: XCTestCase { let objectOffset = reader.rootObjectOffset XCTAssertEqual(objectOffset, 16) - let stringOffset = reader.getOffset(objectOffset: objectOffset!, propertyIndex: 1) + let stringOffset = reader.offset(objectOffset: objectOffset!, propertyIndex: 1) XCTAssertEqual(stringOffset, 28) let stringBuffer = reader.getStringBuffer(stringOffset: stringOffset) @@ -181,19 +181,19 @@ class FBReaderTest: XCTestCase { let objectOffset = reader.rootObjectOffset XCTAssertEqual(objectOffset, 12) - let sVectorOffset = reader.getOffset(objectOffset: objectOffset!, propertyIndex: 0) + let sVectorOffset = reader.offset(objectOffset: objectOffset!, propertyIndex: 0) XCTAssertEqual(sVectorOffset, 32) - let sVectorLength = reader.getVectorElementCount(vectorOffset: sVectorOffset) + let sVectorLength = reader.vectorElementCount(vectorOffset: sVectorOffset) XCTAssertEqual(sVectorLength, 3) - let sOffset1 = reader.getVectorElementOffset(vectorOffset: sVectorOffset!, index: 0) + let sOffset1 = reader.vectorElementOffset(vectorOffset: sVectorOffset!, index: 0) XCTAssertEqual(sOffset1, 48) - let sOffset2 = reader.getVectorElementOffset(vectorOffset: sVectorOffset!, index: 1) + let sOffset2 = reader.vectorElementOffset(vectorOffset: sVectorOffset!, index: 1) XCTAssertEqual(sOffset2, 56) - let sOffset3 = reader.getVectorElementOffset(vectorOffset: sVectorOffset!, index: 2) + let sOffset3 = reader.vectorElementOffset(vectorOffset: sVectorOffset!, index: 2) XCTAssertEqual(sOffset3, 64) let stringBuffer1 = reader.getStringBuffer(stringOffset: sOffset1) @@ -205,16 +205,16 @@ class FBReaderTest: XCTestCase { let stringBuffer3 = reader.getStringBuffer(stringOffset: sOffset3) XCTAssertEqual(stringBuffer3?§, "max1") - let bVectorOffset = reader.getOffset(objectOffset: objectOffset!, propertyIndex: 1) + let bVectorOffset = reader.offset(objectOffset: objectOffset!, propertyIndex: 1) XCTAssertEqual(bVectorOffset, 24) - let bVectorLength = reader.getVectorElementCount(vectorOffset: bVectorOffset) + let bVectorLength = reader.vectorElementCount(vectorOffset: bVectorOffset) XCTAssertEqual(bVectorLength, 2) - let b1 : Bool? = reader.getVectorScalarElement(vectorOffset: bVectorOffset!, index: 0) + let b1 : Bool? = reader.vectorScalarElement(vectorOffset: bVectorOffset!, index: 0) XCTAssertEqual(b1, false) - let b2 : Bool? = reader.getVectorScalarElement(vectorOffset: bVectorOffset!, index: 1) + let b2 : Bool? = reader.vectorScalarElement(vectorOffset: bVectorOffset!, index: 1) XCTAssertEqual(b2, true) } diff --git a/FlatBuffersSwiftTests/contacts_fb.swift b/FlatBuffersSwiftTests/contacts_fb.swift index 35ce1a0..2bc3267 100644 --- a/FlatBuffersSwiftTests/contacts_fb.swift +++ b/FlatBuffersSwiftTests/contacts_fb.swift @@ -14,7 +14,7 @@ public final class ContactList { } } public extension ContactList { - fileprivate static func create(_ reader : FBReader, objectOffset : Offset?) -> ContactList? { + fileprivate static func create(_ reader : FlatBuffersReader, objectOffset : Offset?) -> ContactList? { guard let objectOffset = objectOffset else { return nil } @@ -27,13 +27,13 @@ public extension ContactList { cache.objectPool[objectOffset] = _result } _result.lastModified = reader.get(objectOffset: objectOffset, propertyIndex: 0, defaultValue: 0) - let offset_entries : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 1) - let length_entries = reader.getVectorElementCount(vectorOffset: offset_entries) + let offset_entries : Offset? = reader.offset(objectOffset: objectOffset, propertyIndex: 1) + let length_entries = reader.vectorElementCount(vectorOffset: offset_entries) if(length_entries > 0){ var index = 0 _result.entries.reserveCapacity(length_entries) while index < length_entries { - let element = Contact.create(reader, objectOffset: reader.getVectorElementOffset(vectorOffset: offset_entries, index: index)) + let element = Contact.create(reader, objectOffset: reader.vectorElementOffset(vectorOffset: offset_entries, index: index)) _result.entries.append(element) index += 1 } @@ -42,11 +42,11 @@ public extension ContactList { } } public extension ContactList { - public static func from(data : Data, cache : FBReaderCache? = FBReaderCache()) -> ContactList? { + public static func from(data : Data, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()) -> ContactList? { let reader = FBMemoryReader(data: data, cache: cache) return from(reader: reader) } - public static func from(reader : FBReader) -> ContactList? { + public static func from(reader : FlatBuffersReader) -> ContactList? { let objectOffset = reader.rootObjectOffset return create(reader, objectOffset : objectOffset) } @@ -64,7 +64,7 @@ public extension ContactList { } } -public struct ContactList_Direct : Hashable { +public struct ContactList_Direct : Hashable { fileprivate let reader : T fileprivate let myOffset : Offset fileprivate init(reader: T, myOffset: Offset){ @@ -82,11 +82,11 @@ public struct ContactList_Direct : Hashable { get { return reader.get(objectOffset: myOffset, propertyIndex: 0, defaultValue: 0) } } public var entriesCount : Int { - return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 1)) + return reader.vectorElementCount(vectorOffset: reader.offset(objectOffset: myOffset, propertyIndex: 1)) } public func getEntriesElement(atIndex index : Int) -> Contact_Direct? { - let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 1) - if let ofs = reader.getVectorElementOffset(vectorOffset: offsetList, index: index) { + let offsetList = reader.offset(objectOffset: myOffset, propertyIndex: 1) + if let ofs = reader.vectorElementOffset(vectorOffset: offsetList, index: index) { return Contact_Direct(reader: reader, myOffset: ofs) } return nil @@ -159,7 +159,7 @@ public final class Contact { } } public extension Contact { - fileprivate static func create(_ reader : FBReader, objectOffset : Offset?) -> Contact? { + fileprivate static func create(_ reader : FlatBuffersReader, objectOffset : Offset?) -> Contact? { guard let objectOffset = objectOffset else { return nil } @@ -171,50 +171,50 @@ public extension Contact { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.name = reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 0))?§ - _result.birthday = Date.create(reader, objectOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 1)) + _result.name = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ + _result.birthday = Date.create(reader, objectOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 1)) _result.gender = Gender(rawValue: reader.get(objectOffset: objectOffset, propertyIndex: 2, defaultValue: Gender.Male.rawValue)) - let offset_tags : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 3) - let length_tags = reader.getVectorElementCount(vectorOffset: offset_tags) + let offset_tags : Offset? = reader.offset(objectOffset: objectOffset, propertyIndex: 3) + let length_tags = reader.vectorElementCount(vectorOffset: offset_tags) if(length_tags > 0){ var index = 0 _result.tags.reserveCapacity(length_tags) while index < length_tags { - let element = reader.getStringBuffer(stringOffset: reader.getVectorElementOffset(vectorOffset: offset_tags, index: index))?§ + let element = reader.getStringBuffer(stringOffset: reader.vectorElementOffset(vectorOffset: offset_tags, index: index))?§ _result.tags.append(element) index += 1 } } - let offset_addressEntries : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 4) - let length_addressEntries = reader.getVectorElementCount(vectorOffset: offset_addressEntries) + let offset_addressEntries : Offset? = reader.offset(objectOffset: objectOffset, propertyIndex: 4) + let length_addressEntries = reader.vectorElementCount(vectorOffset: offset_addressEntries) if(length_addressEntries > 0){ var index = 0 _result.addressEntries.reserveCapacity(length_addressEntries) while index < length_addressEntries { - let element = AddressEntry.create(reader, objectOffset: reader.getVectorElementOffset(vectorOffset: offset_addressEntries, index: index)) + let element = AddressEntry.create(reader, objectOffset: reader.vectorElementOffset(vectorOffset: offset_addressEntries, index: index)) _result.addressEntries.append(element) index += 1 } } _result.currentLoccation = reader.get(objectOffset: objectOffset, propertyIndex: 5) - let offset_previousLocations : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 6) - let length_previousLocations = reader.getVectorElementCount(vectorOffset: offset_previousLocations) + let offset_previousLocations : Offset? = reader.offset(objectOffset: objectOffset, propertyIndex: 6) + let length_previousLocations = reader.vectorElementCount(vectorOffset: offset_previousLocations) if(length_previousLocations > 0){ var index = 0 _result.previousLocations.reserveCapacity(length_previousLocations) while index < length_previousLocations { - let element : GeoLocation? = reader.getVectorScalarElement(vectorOffset: offset_previousLocations, index: index) + let element : GeoLocation? = reader.vectorScalarElement(vectorOffset: offset_previousLocations, index: index) _result.previousLocations.append(element) index += 1 } } - let offset_moods : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 7) - let length_moods = reader.getVectorElementCount(vectorOffset: offset_moods) + let offset_moods : Offset? = reader.offset(objectOffset: objectOffset, propertyIndex: 7) + let length_moods = reader.vectorElementCount(vectorOffset: offset_moods) if(length_moods > 0){ var index = 0 _result.moods.reserveCapacity(length_moods) while index < length_moods { - if let raw : Int8 = reader.getVectorScalarElement(vectorOffset: offset_moods, index: index){ + if let raw : Int8 = reader.vectorScalarElement(vectorOffset: offset_moods, index: index){ let element : Mood? = Mood(rawValue: raw) _result.moods.append(element) } else { @@ -226,16 +226,16 @@ public extension Contact { return _result } } -public struct Contact_Direct : Hashable { +public struct Contact_Direct : Hashable { fileprivate let reader : T fileprivate let myOffset : Offset fileprivate init(reader: T, myOffset: Offset){ self.reader = reader self.myOffset = myOffset } - public var name : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: myOffset, propertyIndex:0)) } } + public var name : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var birthday : Date_Direct? { get { - if let offset = reader.getOffset(objectOffset: myOffset, propertyIndex: 1) { + if let offset = reader.offset(objectOffset: myOffset, propertyIndex: 1) { return Date_Direct(reader: reader, myOffset: offset) } return nil @@ -244,21 +244,21 @@ public struct Contact_Direct : Hashable { get { return Gender(rawValue: reader.get(objectOffset: myOffset, propertyIndex: 2, defaultValue: Gender.Male.rawValue)) } } public var tagsCount : Int { - return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 3)) + return reader.vectorElementCount(vectorOffset: reader.offset(objectOffset: myOffset, propertyIndex: 3)) } public func getTagsElement(atIndex index : Int) -> UnsafeBufferPointer? { - let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 3) - if let ofs = reader.getVectorElementOffset(vectorOffset: offsetList, index: index) { + let offsetList = reader.offset(objectOffset: myOffset, propertyIndex: 3) + if let ofs = reader.vectorElementOffset(vectorOffset: offsetList, index: index) { return reader.getStringBuffer(stringOffset: ofs) } return nil } public var addressEntriesCount : Int { - return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 4)) + return reader.vectorElementCount(vectorOffset: reader.offset(objectOffset: myOffset, propertyIndex: 4)) } public func getAddressEntriesElement(atIndex index : Int) -> AddressEntry_Direct? { - let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 4) - if let ofs = reader.getVectorElementOffset(vectorOffset: offsetList, index: index) { + let offsetList = reader.offset(objectOffset: myOffset, propertyIndex: 4) + if let ofs = reader.vectorElementOffset(vectorOffset: offsetList, index: index) { return AddressEntry_Direct(reader: reader, myOffset: ofs) } return nil @@ -267,19 +267,19 @@ public struct Contact_Direct : Hashable { get { return reader.get(objectOffset: myOffset, propertyIndex: 5)} } public var previousLocationsCount : Int { - return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 6)) + return reader.vectorElementCount(vectorOffset: reader.offset(objectOffset: myOffset, propertyIndex: 6)) } public func getPreviousLocationsElement(atIndex index : Int) -> GeoLocation? { - let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 6) - let result : GeoLocation? = reader.getVectorScalarElement(vectorOffset: offsetList, index: index) + let offsetList = reader.offset(objectOffset: myOffset, propertyIndex: 6) + let result : GeoLocation? = reader.vectorScalarElement(vectorOffset: offsetList, index: index) return result } public var moodsCount : Int { - return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 7)) + return reader.vectorElementCount(vectorOffset: reader.offset(objectOffset: myOffset, propertyIndex: 7)) } public func getMoodsElement(atIndex index : Int) -> Mood? { - let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 7) - guard let rawValue : Int8 = reader.getVectorScalarElement(vectorOffset: offsetList, index: index) else { + let offsetList = reader.offset(objectOffset: myOffset, propertyIndex: 7) + guard let rawValue : Int8 = reader.vectorScalarElement(vectorOffset: offsetList, index: index) else { return nil } return Mood(rawValue: rawValue) @@ -395,7 +395,7 @@ public final class Date { } } public extension Date { - fileprivate static func create(_ reader : FBReader, objectOffset : Offset?) -> Date? { + fileprivate static func create(_ reader : FlatBuffersReader, objectOffset : Offset?) -> Date? { guard let objectOffset = objectOffset else { return nil } @@ -413,7 +413,7 @@ public extension Date { return _result } } -public struct Date_Direct : Hashable { +public struct Date_Direct : Hashable { fileprivate let reader : T fileprivate let myOffset : Offset fileprivate init(reader: T, myOffset: Offset){ @@ -470,7 +470,7 @@ public final class AddressEntry { } } public extension AddressEntry { - fileprivate static func create(_ reader : FBReader, objectOffset : Offset?) -> AddressEntry? { + fileprivate static func create(_ reader : FlatBuffersReader, objectOffset : Offset?) -> AddressEntry? { guard let objectOffset = objectOffset else { return nil } @@ -487,7 +487,7 @@ public extension AddressEntry { return _result } } -public struct AddressEntry_Direct : Hashable { +public struct AddressEntry_Direct : Hashable { fileprivate let reader : T fileprivate let myOffset : Offset fileprivate init(reader: T, myOffset: Offset){ @@ -540,7 +540,7 @@ public final class PostalAddress { } } public extension PostalAddress { - fileprivate static func create(_ reader : FBReader, objectOffset : Offset?) -> PostalAddress? { + fileprivate static func create(_ reader : FlatBuffersReader, objectOffset : Offset?) -> PostalAddress? { guard let objectOffset = objectOffset else { return nil } @@ -552,26 +552,26 @@ public extension PostalAddress { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.country = reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 0))?§ - _result.city = reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 1))?§ + _result.country = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ + _result.city = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 1))?§ _result.postalCode = reader.get(objectOffset: objectOffset, propertyIndex: 2, defaultValue: 0) - _result.streetAndNumber = reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 3))?§ + _result.streetAndNumber = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 3))?§ return _result } } -public struct PostalAddress_Direct : Hashable { +public struct PostalAddress_Direct : Hashable { fileprivate let reader : T fileprivate let myOffset : Offset fileprivate init(reader: T, myOffset: Offset){ self.reader = reader self.myOffset = myOffset } - public var country : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: myOffset, propertyIndex:0)) } } - public var city : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: myOffset, propertyIndex:1)) } } + public var country : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } + public var city : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:1)) } } public var postalCode : Int32 { get { return reader.get(objectOffset: myOffset, propertyIndex: 2, defaultValue: 0) } } - public var streetAndNumber : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: myOffset, propertyIndex:3)) } } + public var streetAndNumber : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:3)) } } public var hashValue: Int { return Int(myOffset) } } public func ==(t1 : PostalAddress_Direct, t2 : PostalAddress_Direct) -> Bool { @@ -607,7 +607,7 @@ public final class EmailAddress { } } public extension EmailAddress { - fileprivate static func create(_ reader : FBReader, objectOffset : Offset?) -> EmailAddress? { + fileprivate static func create(_ reader : FlatBuffersReader, objectOffset : Offset?) -> EmailAddress? { guard let objectOffset = objectOffset else { return nil } @@ -619,18 +619,18 @@ public extension EmailAddress { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.mailto = reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 0))?§ + _result.mailto = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ return _result } } -public struct EmailAddress_Direct : Hashable { +public struct EmailAddress_Direct : Hashable { fileprivate let reader : T fileprivate let myOffset : Offset fileprivate init(reader: T, myOffset: Offset){ self.reader = reader self.myOffset = myOffset } - public var mailto : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: myOffset, propertyIndex:0)) } } + public var mailto : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var hashValue: Int { return Int(myOffset) } } public func ==(t1 : EmailAddress_Direct, t2 : EmailAddress_Direct) -> Bool { @@ -661,7 +661,7 @@ public final class WebAddress { } } public extension WebAddress { - fileprivate static func create(_ reader : FBReader, objectOffset : Offset?) -> WebAddress? { + fileprivate static func create(_ reader : FlatBuffersReader, objectOffset : Offset?) -> WebAddress? { guard let objectOffset = objectOffset else { return nil } @@ -673,18 +673,18 @@ public extension WebAddress { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.url = reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 0))?§ + _result.url = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ return _result } } -public struct WebAddress_Direct : Hashable { +public struct WebAddress_Direct : Hashable { fileprivate let reader : T fileprivate let myOffset : Offset fileprivate init(reader: T, myOffset: Offset){ self.reader = reader self.myOffset = myOffset } - public var url : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: myOffset, propertyIndex:0)) } } + public var url : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var hashValue: Int { return Int(myOffset) } } public func ==(t1 : WebAddress_Direct, t2 : WebAddress_Direct) -> Bool { @@ -715,7 +715,7 @@ public final class TelephoneNumber { } } public extension TelephoneNumber { - fileprivate static func create(_ reader : FBReader, objectOffset : Offset?) -> TelephoneNumber? { + fileprivate static func create(_ reader : FlatBuffersReader, objectOffset : Offset?) -> TelephoneNumber? { guard let objectOffset = objectOffset else { return nil } @@ -727,18 +727,18 @@ public extension TelephoneNumber { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.number = reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 0))?§ + _result.number = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ return _result } } -public struct TelephoneNumber_Direct : Hashable { +public struct TelephoneNumber_Direct : Hashable { fileprivate let reader : T fileprivate let myOffset : Offset fileprivate init(reader: T, myOffset: Offset){ self.reader = reader self.myOffset = myOffset } - public var number : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: myOffset, propertyIndex:0)) } } + public var number : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var hashValue: Int { return Int(myOffset) } } public func ==(t1 : TelephoneNumber_Direct, t2 : TelephoneNumber_Direct) -> Bool { @@ -771,12 +771,12 @@ extension WebAddress : Address {} extension WebAddress_Direct : Address_Direct {} extension TelephoneNumber : Address {} extension TelephoneNumber_Direct : Address_Direct {} -fileprivate func create_Address(_ reader : FBReader, propertyIndex : Int, objectOffset : Offset?) -> Address? { +fileprivate func create_Address(_ reader : FlatBuffersReader, propertyIndex : Int, objectOffset : Offset?) -> Address? { guard let objectOffset = objectOffset else { return nil } let unionCase : Int8 = reader.get(objectOffset: objectOffset, propertyIndex: propertyIndex, defaultValue: 0) - guard let caseObjectOffset : Offset = reader.getOffset(objectOffset: objectOffset, propertyIndex:propertyIndex + 1) else { + guard let caseObjectOffset : Offset = reader.offset(objectOffset: objectOffset, propertyIndex:propertyIndex + 1) else { return nil } switch unionCase { @@ -788,12 +788,12 @@ fileprivate func create_Address(_ reader : FBReader, propertyIndex : Int, object } } -fileprivate func create_Address_Direct(_ reader : T, propertyIndex : Int, objectOffset : Offset?) -> Address_Direct? { +fileprivate func create_Address_Direct(_ reader : T, propertyIndex : Int, objectOffset : Offset?) -> Address_Direct? { guard let objectOffset = objectOffset else { return nil } let unionCase : Int8 = reader.get(objectOffset: objectOffset, propertyIndex: propertyIndex, defaultValue: 0) - guard let caseObjectOffset : Offset = reader.getOffset(objectOffset: objectOffset, propertyIndex:propertyIndex + 1) else { + guard let caseObjectOffset : Offset = reader.offset(objectOffset: objectOffset, propertyIndex:propertyIndex + 1) else { return nil } switch unionCase { diff --git a/FlatBuffersSwiftTests/friends_fb.swift b/FlatBuffersSwiftTests/friends_fb.swift index 4b136dd..83dde2d 100644 --- a/FlatBuffersSwiftTests/friends_fb.swift +++ b/FlatBuffersSwiftTests/friends_fb.swift @@ -12,7 +12,7 @@ public final class PeopleList { } } public extension PeopleList { - fileprivate static func create(_ reader : FBReader, objectOffset : Offset?) -> PeopleList? { + fileprivate static func create(_ reader : FlatBuffersReader, objectOffset : Offset?) -> PeopleList? { guard let objectOffset = objectOffset else { return nil } @@ -24,13 +24,13 @@ public extension PeopleList { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - let offset_people : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 0) - let length_people = reader.getVectorElementCount(vectorOffset: offset_people) + let offset_people : Offset? = reader.offset(objectOffset: objectOffset, propertyIndex: 0) + let length_people = reader.vectorElementCount(vectorOffset: offset_people) if(length_people > 0){ var index = 0 _result.people.reserveCapacity(length_people) while index < length_people { - let element = Friend.create(reader, objectOffset: reader.getVectorElementOffset(vectorOffset: offset_people, index: index)) + let element = Friend.create(reader, objectOffset: reader.vectorElementOffset(vectorOffset: offset_people, index: index)) _result.people.append(element) index += 1 } @@ -39,11 +39,11 @@ public extension PeopleList { } } public extension PeopleList { - public static func from(data : Data, cache : FBReaderCache? = FBReaderCache()) -> PeopleList? { + public static func from(data : Data, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()) -> PeopleList? { let reader = FBMemoryReader(data: data, cache: cache) return from(reader: reader) } - public static func from(reader : FBReader) -> PeopleList? { + public static func from(reader : FlatBuffersReader) -> PeopleList? { let objectOffset = reader.rootObjectOffset return create(reader, objectOffset : objectOffset) } @@ -62,7 +62,7 @@ public extension PeopleList { } } -public struct PeopleList_Direct : Hashable { +public struct PeopleList_Direct : Hashable { fileprivate let reader : T fileprivate let myOffset : Offset fileprivate init(reader: T, myOffset: Offset){ @@ -77,11 +77,11 @@ public struct PeopleList_Direct : Hashable { self.myOffset = offest } public var peopleCount : Int { - return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 0)) + return reader.vectorElementCount(vectorOffset: reader.offset(objectOffset: myOffset, propertyIndex: 0)) } public func getPeopleElement(atIndex index : Int) -> Friend_Direct? { - let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 0) - if let ofs = reader.getVectorElementOffset(vectorOffset: offsetList, index: index) { + let offsetList = reader.offset(objectOffset: myOffset, propertyIndex: 0) + if let ofs = reader.vectorElementOffset(vectorOffset: offsetList, index: index) { return Friend_Direct(reader: reader, myOffset: ofs) } return nil @@ -160,7 +160,7 @@ public final class Friend { } } public extension Friend { - fileprivate static func create(_ reader : FBReader, objectOffset : Offset?) -> Friend? { + fileprivate static func create(_ reader : FlatBuffersReader, objectOffset : Offset?) -> Friend? { guard let objectOffset = objectOffset else { return nil } @@ -172,50 +172,50 @@ public extension Friend { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.name = reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 0))?§ - let offset_friends : Offset? = reader.getOffset(objectOffset: objectOffset, propertyIndex: 1) - let length_friends = reader.getVectorElementCount(vectorOffset: offset_friends) + _result.name = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ + let offset_friends : Offset? = reader.offset(objectOffset: objectOffset, propertyIndex: 1) + let length_friends = reader.vectorElementCount(vectorOffset: offset_friends) if(length_friends > 0){ var index = 0 _result.friends.reserveCapacity(length_friends) while index < length_friends { - let element = Friend.create(reader, objectOffset: reader.getVectorElementOffset(vectorOffset: offset_friends, index: index)) + let element = Friend.create(reader, objectOffset: reader.vectorElementOffset(vectorOffset: offset_friends, index: index)) _result.friends.append(element) index += 1 } } - _result.father = Friend.create(reader, objectOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 2)) - _result.mother = Friend.create(reader, objectOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 3)) + _result.father = Friend.create(reader, objectOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 2)) + _result.mother = Friend.create(reader, objectOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 3)) _result.lover = create_Human(reader, propertyIndex: 4, objectOffset: objectOffset) return _result } } -public struct Friend_Direct : Hashable { +public struct Friend_Direct : Hashable { fileprivate let reader : T fileprivate let myOffset : Offset fileprivate init(reader: T, myOffset: Offset){ self.reader = reader self.myOffset = myOffset } - public var name : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.getOffset(objectOffset: myOffset, propertyIndex:0)) } } + public var name : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var friendsCount : Int { - return reader.getVectorElementCount(vectorOffset: reader.getOffset(objectOffset: myOffset, propertyIndex: 1)) + return reader.vectorElementCount(vectorOffset: reader.offset(objectOffset: myOffset, propertyIndex: 1)) } public func getFriendsElement(atIndex index : Int) -> Friend_Direct? { - let offsetList = reader.getOffset(objectOffset: myOffset, propertyIndex: 1) - if let ofs = reader.getVectorElementOffset(vectorOffset: offsetList, index: index) { + let offsetList = reader.offset(objectOffset: myOffset, propertyIndex: 1) + if let ofs = reader.vectorElementOffset(vectorOffset: offsetList, index: index) { return Friend_Direct(reader: reader, myOffset: ofs) } return nil } public var father : Friend_Direct? { get { - if let offset = reader.getOffset(objectOffset: myOffset, propertyIndex: 2) { + if let offset = reader.offset(objectOffset: myOffset, propertyIndex: 2) { return Friend_Direct(reader: reader, myOffset: offset) } return nil } } public var mother : Friend_Direct? { get { - if let offset = reader.getOffset(objectOffset: myOffset, propertyIndex: 3) { + if let offset = reader.offset(objectOffset: myOffset, propertyIndex: 3) { return Friend_Direct(reader: reader, myOffset: offset) } return nil @@ -317,7 +317,7 @@ public final class Male { } } public extension Male { - fileprivate static func create(_ reader : FBReader, objectOffset : Offset?) -> Male? { + fileprivate static func create(_ reader : FlatBuffersReader, objectOffset : Offset?) -> Male? { guard let objectOffset = objectOffset else { return nil } @@ -329,11 +329,11 @@ public extension Male { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.ref = Friend.create(reader, objectOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 0)) + _result.ref = Friend.create(reader, objectOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0)) return _result } } -public struct Male_Direct : Hashable { +public struct Male_Direct : Hashable { fileprivate let reader : T fileprivate let myOffset : Offset fileprivate init(reader: T, myOffset: Offset){ @@ -341,7 +341,7 @@ public struct Male_Direct : Hashable { self.myOffset = myOffset } public var ref : Friend_Direct? { get { - if let offset = reader.getOffset(objectOffset: myOffset, propertyIndex: 0) { + if let offset = reader.offset(objectOffset: myOffset, propertyIndex: 0) { return Friend_Direct(reader: reader, myOffset: offset) } return nil @@ -388,7 +388,7 @@ public final class Female { } } public extension Female { - fileprivate static func create(_ reader : FBReader, objectOffset : Offset?) -> Female? { + fileprivate static func create(_ reader : FlatBuffersReader, objectOffset : Offset?) -> Female? { guard let objectOffset = objectOffset else { return nil } @@ -400,11 +400,11 @@ public extension Female { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.ref = Friend.create(reader, objectOffset: reader.getOffset(objectOffset: objectOffset, propertyIndex: 0)) + _result.ref = Friend.create(reader, objectOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0)) return _result } } -public struct Female_Direct : Hashable { +public struct Female_Direct : Hashable { fileprivate let reader : T fileprivate let myOffset : Offset fileprivate init(reader: T, myOffset: Offset){ @@ -412,7 +412,7 @@ public struct Female_Direct : Hashable { self.myOffset = myOffset } public var ref : Friend_Direct? { get { - if let offset = reader.getOffset(objectOffset: myOffset, propertyIndex: 0) { + if let offset = reader.offset(objectOffset: myOffset, propertyIndex: 0) { return Friend_Direct(reader: reader, myOffset: offset) } return nil @@ -457,12 +457,12 @@ extension Male : Human {} extension Male_Direct : Human_Direct {} extension Female : Human {} extension Female_Direct : Human_Direct {} -fileprivate func create_Human(_ reader : FBReader, propertyIndex : Int, objectOffset : Offset?) -> Human? { +fileprivate func create_Human(_ reader : FlatBuffersReader, propertyIndex : Int, objectOffset : Offset?) -> Human? { guard let objectOffset = objectOffset else { return nil } let unionCase : Int8 = reader.get(objectOffset: objectOffset, propertyIndex: propertyIndex, defaultValue: 0) - guard let caseObjectOffset : Offset = reader.getOffset(objectOffset: objectOffset, propertyIndex:propertyIndex + 1) else { + guard let caseObjectOffset : Offset = reader.offset(objectOffset: objectOffset, propertyIndex:propertyIndex + 1) else { return nil } switch unionCase { @@ -472,12 +472,12 @@ fileprivate func create_Human(_ reader : FBReader, propertyIndex : Int, objectOf } } -fileprivate func create_Human_Direct(_ reader : T, propertyIndex : Int, objectOffset : Offset?) -> Human_Direct? { +fileprivate func create_Human_Direct(_ reader : T, propertyIndex : Int, objectOffset : Offset?) -> Human_Direct? { guard let objectOffset = objectOffset else { return nil } let unionCase : Int8 = reader.get(objectOffset: objectOffset, propertyIndex: propertyIndex, defaultValue: 0) - guard let caseObjectOffset : Offset = reader.getOffset(objectOffset: objectOffset, propertyIndex:propertyIndex + 1) else { + guard let caseObjectOffset : Offset = reader.offset(objectOffset: objectOffset, propertyIndex:propertyIndex + 1) else { return nil } switch unionCase { From a39abcf5bf29ca6255e4f47d7f285de352f4b616 Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Tue, 10 Jan 2017 18:19:40 +0100 Subject: [PATCH 08/16] Extend documentation and more FB -> FlatBuffers --- FlatBuffersSwift/FlatBuffersReader.swift | 100 +++++++++++++++++++---- FlatBuffersSwiftTests/ContactsTest.swift | 34 ++++---- FlatBuffersSwiftTests/FBReaderTest.swift | 44 +++++----- FlatBuffersSwiftTests/FriendsTest.swift | 6 +- FlatBuffersSwiftTests/contacts_fb.swift | 42 +++++----- FlatBuffersSwiftTests/friends_fb.swift | 6 +- 6 files changed, 150 insertions(+), 82 deletions(-) diff --git a/FlatBuffersSwift/FlatBuffersReader.swift b/FlatBuffersSwift/FlatBuffersReader.swift index 3070baf..8a816e0 100644 --- a/FlatBuffersSwift/FlatBuffersReader.swift +++ b/FlatBuffersSwift/FlatBuffersReader.swift @@ -84,7 +84,15 @@ public extension FlatBuffersReader { } } - /// **Returns** the final offset in the reader buffer to access a given property for a given object-offset + /** + Retrieve the final offset of a property to be able to access it + + - parameters: + - objectOffset: The offset of the object + - propertyIndex: The property to extract + + - Returns: the final offset in the reader buffer to access a given property for a given object-offset + */ public func offset(objectOffset : Offset, propertyIndex : Int) -> Offset? { let propOffset = propertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) @@ -107,7 +115,14 @@ public extension FlatBuffersReader { } - /// **Returns** the length of vector + /** + Retrieve the count of elements in an embedded vector + + - parameters: + - vectorOffset: The offset of the vector in the buffer + + - Returns: the number of elements in the vector + */ public func vectorElementCount(vectorOffset : Offset?) -> Int { guard let vectorOffset = vectorOffset else { return 0 @@ -121,7 +136,15 @@ public extension FlatBuffersReader { } } - /// **Returns** the offset in the buffer for a given vector element + /** + Retrieve an element offset from a vector + + - parameters: + - vectorOffset: The offset of the vector in the buffer + - index: The index of the element we want the offset for + + - Returns: the offset in the buffer for a given vector element + */ public func vectorElementOffset(vectorOffset : Offset?, index : Int) -> Offset? { guard let vectorOffset = vectorOffset else { return nil @@ -144,8 +167,16 @@ public extension FlatBuffersReader { } } - /// **Returns** a scalar value directly from a vector for a given index - public func vectorScalarElement(vectorOffset : Offset?, index : Int) -> T? { + /** + Retrieve a scalar value from a vector + + - parameters: + - vectorOffset: The offset of the vector in the buffer + - index: The index of the element we want the offset for + + - Returns: a scalar value directly from a vector for a given index + */ + public func vectorElementScalar(vectorOffset : Offset?, index : Int) -> T? { guard let vectorOffset = vectorOffset else { return nil } @@ -165,7 +196,16 @@ public extension FlatBuffersReader { } } - /// **Returns** a scalar value directly from a vector for a given index + /** + Retrieve a scalar value or supply a default if unavailable + + - parameters: + - objectOffset: The offset of the object + - propertyIndex: The property to try to extract + - defaultValue: The default value to return if the property is not in the buffer + + - Returns: a scalar value directly from a vector for a given index + */ public func get(objectOffset : Offset, propertyIndex : Int, defaultValue : T) -> T { let propOffset = propertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) if propOffset == 0 { @@ -179,7 +219,15 @@ public extension FlatBuffersReader { } } - /// **Returns** a scalar value for a given property from an object + /** + Retrieve a scalar optional value (return nill if unavailable) + + - parameters: + - objectOffset: The offset of the object + - propertyIndex: The property to try to extract + + - Returns: a scalar value directly from a vector for a given index + */ public func get(objectOffset : Offset, propertyIndex : Int) -> T? { let propOffset = propertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) if propOffset == 0 { @@ -193,8 +241,15 @@ public extension FlatBuffersReader { } } - /// **Returns** a buffer pointer to the subrange of the reader buffer occupied by a string - public func getStringBuffer(stringOffset : Offset?) -> UnsafeBufferPointer? { + /** + Retrieve a stringbuffer + + - parameters: + - stringOffset: The offset of the string + + - Returns: a buffer pointer to the subrange of the reader buffer occupied by a string + */ + public func stringBuffer(stringOffset : Offset?) -> UnsafeBufferPointer? { guard let stringOffset = stringOffset else { return nil } @@ -209,7 +264,11 @@ public extension FlatBuffersReader { } } - /// **Returns** the offset for the root table object + /** + Retrieve the root object offset + + - Returns: the offset for the root table object + */ public var rootObjectOffset : Offset? { do { return try scalar(at: 0) as Offset @@ -220,7 +279,7 @@ public extension FlatBuffersReader { } /// A FlatBuffers reader subclass that by default reads directly from a memory buffer, but also supports initialization from Data objects for convenience -public struct FBMemoryReader : FlatBuffersReader { +public struct FlatBuffersMemoryReader : FlatBuffersReader { private let count : Int public let cache : FlatBuffersReaderCache? @@ -234,7 +293,7 @@ public struct FBMemoryReader : FlatBuffersReader { - count: The size of the data buffer - cache: An optional cache of reader objects for reuse - - Returns: A FB reader ready for use. + - Returns: A FlatBuffers reader ready for use. */ public init(buffer : UnsafeRawPointer, count : Int, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()) { self.buffer = buffer @@ -250,7 +309,7 @@ public struct FBMemoryReader : FlatBuffersReader { the UnsafeRawsPointer initializer should be used. - cache: An optional cache of reader objects for reuse - - Returns: A FB reader ready for use. + - Returns: A FlatBuffers reader ready for use. */ public init(data : Data, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()) { self.count = data.count @@ -279,7 +338,7 @@ public struct FBMemoryReader : FlatBuffersReader { } public func isEqual(other: FlatBuffersReader) -> Bool{ - guard let other = other as? FBMemoryReader else { + guard let other = other as? FlatBuffersMemoryReader else { return false } return self.buffer == other.buffer @@ -287,12 +346,21 @@ public struct FBMemoryReader : FlatBuffersReader { } /// A FlatBuffers reader subclass that reads directly from a file handle -public struct FBFileReader : FlatBuffersReader { +public struct FlatBuffersFileReader : FlatBuffersReader { private let fileSize : UInt64 private let fileHandle : FileHandle public let cache : FlatBuffersReaderCache? + /** + Initializes the reader from a FileHandle object. + + - parameters: + - fileHandle: A FileHandle object referencing the file we read from. + - cache: An optional cache of reader objects for reuse + + - Returns: A FlatBuffers reader ready for use. + */ public init(fileHandle : FileHandle, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()){ self.fileHandle = fileHandle fileSize = fileHandle.seekToEndOfFile() @@ -331,7 +399,7 @@ public struct FBFileReader : FlatBuffersReader { } public func isEqual(other: FlatBuffersReader) -> Bool{ - guard let other = other as? FBFileReader else { + guard let other = other as? FlatBuffersFileReader else { return false } return self.fileHandle === other.fileHandle diff --git a/FlatBuffersSwiftTests/ContactsTest.swift b/FlatBuffersSwiftTests/ContactsTest.swift index 408faa5..ff8e6ac 100644 --- a/FlatBuffersSwiftTests/ContactsTest.swift +++ b/FlatBuffersSwiftTests/ContactsTest.swift @@ -91,7 +91,7 @@ class ContactsTest: XCTestCase { XCTAssertNotNil(data) - let reader = FBMemoryReader(data: data!) + let reader = FlatBuffersMemoryReader(data: data!) let readContactList = ContactList_Direct(reader)! XCTAssertEqual(readContactList.lastModified, 2349873427654) @@ -115,20 +115,20 @@ class ContactsTest: XCTestCase { XCTAssertEqual(i1.addressEntriesCount, 4) XCTAssertEqual(i1.getAddressEntriesElement(atIndex: 0)?.order, 0) - let mailto = (i1.getAddressEntriesElement(atIndex: 0)?.address as? EmailAddress_Direct)?.mailto?§ + let mailto = (i1.getAddressEntriesElement(atIndex: 0)?.address as? EmailAddress_Direct)?.mailto?§ XCTAssertEqual(mailto, "bla@bla.io") XCTAssertEqual(i1.getAddressEntriesElement(atIndex: 1)?.order, 1) - XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.country?§, "DE") - XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.city?§, "Berlin") - XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.postalCode, 13000) - XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.streetAndNumber?§, "Balstr, 23") + XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.country?§, "DE") + XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.city?§, "Berlin") + XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.postalCode, 13000) + XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.streetAndNumber?§, "Balstr, 23") XCTAssertEqual(i1.getAddressEntriesElement(atIndex: 2)?.order, 2) - XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 2)?.address as? WebAddress_Direct)?.url?§, "http://slkf.com") + XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 2)?.address as? WebAddress_Direct)?.url?§, "http://slkf.com") XCTAssertEqual(i1.getAddressEntriesElement(atIndex: 3)?.order, 3) - XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 3)?.address as? TelephoneNumber_Direct)?.number?§, "+4923452425") + XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 3)?.address as? TelephoneNumber_Direct)?.number?§, "+4923452425") XCTAssertEqual(i1.currentLoccation, GeoLocation(latitude: 23.7, longitude: 34.45, elevation: 45.98)) @@ -162,7 +162,7 @@ class ContactsTest: XCTestCase { XCTAssertNotNil(data) let fileHandle = writeToFileAndReturnHandle(data) - let fileReader = FBFileReader(fileHandle: fileHandle) + let fileReader = FlatBuffersFileReader(fileHandle: fileHandle) let readContactList = ContactList.from(reader: fileReader)! XCTAssertEqual(readContactList.lastModified, 2349873427654) @@ -237,7 +237,7 @@ class ContactsTest: XCTestCase { XCTAssertNotNil(data) let fileHandle = writeToFileAndReturnHandle(data) - let fileReader = FBFileReader(fileHandle: fileHandle) + let fileReader = FlatBuffersFileReader(fileHandle: fileHandle) let readContactList = ContactList_Direct(fileReader)! XCTAssertEqual(readContactList.lastModified, 2349873427654) @@ -261,20 +261,20 @@ class ContactsTest: XCTestCase { XCTAssertEqual(i1.addressEntriesCount, 4) XCTAssertEqual(i1.getAddressEntriesElement(atIndex: 0)?.order, 0) - let mailto = (i1.getAddressEntriesElement(atIndex: 0)?.address as? EmailAddress_Direct)?.mailto?§ + let mailto = (i1.getAddressEntriesElement(atIndex: 0)?.address as? EmailAddress_Direct)?.mailto?§ XCTAssertEqual(mailto, "bla@bla.io") XCTAssertEqual(i1.getAddressEntriesElement(atIndex: 1)?.order, 1) - XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.country?§, "DE") - XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.city?§, "Berlin") - XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.postalCode, 13000) - XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.streetAndNumber?§, "Balstr, 23") + XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.country?§, "DE") + XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.city?§, "Berlin") + XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.postalCode, 13000) + XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 1)?.address as? PostalAddress_Direct)?.streetAndNumber?§, "Balstr, 23") XCTAssertEqual(i1.getAddressEntriesElement(atIndex: 2)?.order, 2) - XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 2)?.address as? WebAddress_Direct)?.url?§, "http://slkf.com") + XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 2)?.address as? WebAddress_Direct)?.url?§, "http://slkf.com") XCTAssertEqual(i1.getAddressEntriesElement(atIndex: 3)?.order, 3) - XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 3)?.address as? TelephoneNumber_Direct)?.number?§, "+4923452425") + XCTAssertEqual((i1.getAddressEntriesElement(atIndex: 3)?.address as? TelephoneNumber_Direct)?.number?§, "+4923452425") XCTAssertEqual(i1.currentLoccation, GeoLocation(latitude: 23.7, longitude: 34.45, elevation: 45.98)) diff --git a/FlatBuffersSwiftTests/FBReaderTest.swift b/FlatBuffersSwiftTests/FBReaderTest.swift index 3255768..292391e 100644 --- a/FlatBuffersSwiftTests/FBReaderTest.swift +++ b/FlatBuffersSwiftTests/FBReaderTest.swift @@ -15,7 +15,7 @@ class FlatBuffersReaderTest: XCTestCase { let data = createSimpleObject() - let reader = FBMemoryReader(data: data) + let reader = FlatBuffersMemoryReader(data: data) let objectOffset = reader.rootObjectOffset XCTAssertEqual(objectOffset, 16) @@ -23,7 +23,7 @@ class FlatBuffersReaderTest: XCTestCase { let stringOffset = reader.offset(objectOffset: objectOffset!, propertyIndex: 1) XCTAssertEqual(stringOffset, 28) - let stringBuffer = reader.getStringBuffer(stringOffset: stringOffset) + let stringBuffer = reader.stringBuffer(stringOffset: stringOffset) XCTAssertEqual(stringBuffer?§, "max") let booleanValue1 : Bool? = reader.get(objectOffset: objectOffset!, propertyIndex: 0) @@ -39,7 +39,7 @@ class FlatBuffersReaderTest: XCTestCase { func testReadDirectWithVector() { let data = createObjectWithVectors() - let reader = FBMemoryReader(data: data) + let reader = FlatBuffersMemoryReader(data: data) let objectOffset = reader.rootObjectOffset XCTAssertEqual(objectOffset, 12) @@ -59,13 +59,13 @@ class FlatBuffersReaderTest: XCTestCase { let sOffset3 = reader.vectorElementOffset(vectorOffset: sVectorOffset!, index: 2) XCTAssertEqual(sOffset3, 64) - let stringBuffer1 = reader.getStringBuffer(stringOffset: sOffset1) + let stringBuffer1 = reader.stringBuffer(stringOffset: sOffset1) XCTAssertEqual(stringBuffer1?§, "max3") - let stringBuffer2 = reader.getStringBuffer(stringOffset: sOffset2) + let stringBuffer2 = reader.stringBuffer(stringOffset: sOffset2) XCTAssertEqual(stringBuffer2?§, "max2") - let stringBuffer3 = reader.getStringBuffer(stringOffset: sOffset3) + let stringBuffer3 = reader.stringBuffer(stringOffset: sOffset3) XCTAssertEqual(stringBuffer3?§, "max1") let bVectorOffset = reader.offset(objectOffset: objectOffset!, propertyIndex: 1) @@ -74,17 +74,17 @@ class FlatBuffersReaderTest: XCTestCase { let bVectorLength = reader.vectorElementCount(vectorOffset: bVectorOffset) XCTAssertEqual(bVectorLength, 2) - let b1 : Bool? = reader.vectorScalarElement(vectorOffset: bVectorOffset!, index: 0) + let b1 : Bool? = reader.vectorElementScalar(vectorOffset: bVectorOffset!, index: 0) XCTAssertEqual(b1, false) - let b2 : Bool? = reader.vectorScalarElement(vectorOffset: bVectorOffset!, index: 1) + let b2 : Bool? = reader.vectorElementScalar(vectorOffset: bVectorOffset!, index: 1) XCTAssertEqual(b2, true) } func testReadInvalidRootDirect() { let data : [UInt8] = [12] - let reader = FBMemoryReader(buffer: UnsafeRawPointer(data), count: data.count) + let reader = FlatBuffersMemoryReader(buffer: UnsafeRawPointer(data), count: data.count) let objectOffset = reader.rootObjectOffset XCTAssertNil(objectOffset) @@ -93,7 +93,7 @@ class FlatBuffersReaderTest: XCTestCase { func testReadPropertyWithHighPropertyIndex() { let data = createObjectWithVectors() - let reader = FBMemoryReader(data: Data(data)) + let reader = FlatBuffersMemoryReader(data: Data(data)) let root = reader.rootObjectOffset @@ -104,7 +104,7 @@ class FlatBuffersReaderTest: XCTestCase { func testReadPropertyWithLowPropertyIndex() { let data = createObjectWithVectors() - let reader = FBMemoryReader(data: data) + let reader = FlatBuffersMemoryReader(data: data) let root = reader.rootObjectOffset let i : Int? = reader.get(objectOffset: root!, propertyIndex: -1) @@ -114,7 +114,7 @@ class FlatBuffersReaderTest: XCTestCase { func testReadPropertyOffestWithWrongPropertyIndex() { let data = createObjectWithVectors() - let reader = FBMemoryReader(data: data) + let reader = FlatBuffersMemoryReader(data: data) let root = reader.rootObjectOffset let o = reader.offset(objectOffset: root!, propertyIndex: -1) @@ -125,7 +125,7 @@ class FlatBuffersReaderTest: XCTestCase { func testReadPropertyWhereVTableReferenceIsBroken() { let data : [UInt8] = [4,0,0,0,5] - let reader = FBMemoryReader(data: Data(data)) + let reader = FlatBuffersMemoryReader(data: Data(data)) let root = reader.rootObjectOffset let i : Int? = reader.get(objectOffset: root!, propertyIndex: 0) @@ -143,14 +143,14 @@ class FlatBuffersReaderTest: XCTestCase { } fh.write(data) - let reader = FBFileReader(fileHandle : fh) + let reader = FlatBuffersFileReader(fileHandle : fh) let objectOffset = reader.rootObjectOffset XCTAssertEqual(objectOffset, 16) let stringOffset = reader.offset(objectOffset: objectOffset!, propertyIndex: 1) XCTAssertEqual(stringOffset, 28) - let stringBuffer = reader.getStringBuffer(stringOffset: stringOffset) + let stringBuffer = reader.stringBuffer(stringOffset: stringOffset) XCTAssertEqual(stringBuffer?§, "max") let booleanValue1 : Bool? = reader.get(objectOffset: objectOffset!, propertyIndex: 0) @@ -176,7 +176,7 @@ class FlatBuffersReaderTest: XCTestCase { } fh.write(data) - let reader = FBFileReader(fileHandle : fh) + let reader = FlatBuffersFileReader(fileHandle : fh) let objectOffset = reader.rootObjectOffset XCTAssertEqual(objectOffset, 12) @@ -196,13 +196,13 @@ class FlatBuffersReaderTest: XCTestCase { let sOffset3 = reader.vectorElementOffset(vectorOffset: sVectorOffset!, index: 2) XCTAssertEqual(sOffset3, 64) - let stringBuffer1 = reader.getStringBuffer(stringOffset: sOffset1) + let stringBuffer1 = reader.stringBuffer(stringOffset: sOffset1) XCTAssertEqual(stringBuffer1?§, "max3") - let stringBuffer2 = reader.getStringBuffer(stringOffset: sOffset2) + let stringBuffer2 = reader.stringBuffer(stringOffset: sOffset2) XCTAssertEqual(stringBuffer2?§, "max2") - let stringBuffer3 = reader.getStringBuffer(stringOffset: sOffset3) + let stringBuffer3 = reader.stringBuffer(stringOffset: sOffset3) XCTAssertEqual(stringBuffer3?§, "max1") let bVectorOffset = reader.offset(objectOffset: objectOffset!, propertyIndex: 1) @@ -211,10 +211,10 @@ class FlatBuffersReaderTest: XCTestCase { let bVectorLength = reader.vectorElementCount(vectorOffset: bVectorOffset) XCTAssertEqual(bVectorLength, 2) - let b1 : Bool? = reader.vectorScalarElement(vectorOffset: bVectorOffset!, index: 0) + let b1 : Bool? = reader.vectorElementScalar(vectorOffset: bVectorOffset!, index: 0) XCTAssertEqual(b1, false) - let b2 : Bool? = reader.vectorScalarElement(vectorOffset: bVectorOffset!, index: 1) + let b2 : Bool? = reader.vectorElementScalar(vectorOffset: bVectorOffset!, index: 1) XCTAssertEqual(b2, true) } @@ -229,7 +229,7 @@ class FlatBuffersReaderTest: XCTestCase { } fh.write(Data(bytes: data)) - let reader = FBFileReader(fileHandle : fh) + let reader = FlatBuffersFileReader(fileHandle : fh) let objectOffset = reader.rootObjectOffset XCTAssertNil(objectOffset) diff --git a/FlatBuffersSwiftTests/FriendsTest.swift b/FlatBuffersSwiftTests/FriendsTest.swift index 6d3c5ef..3503358 100644 --- a/FlatBuffersSwiftTests/FriendsTest.swift +++ b/FlatBuffersSwiftTests/FriendsTest.swift @@ -139,7 +139,7 @@ class FriendsTest: XCTestCase { let data = try!complexList().toData() - let reader = FBMemoryReader(data: data) + let reader = FlatBuffersMemoryReader(data: data) let newList = PeopleList_Direct(reader) @@ -178,7 +178,7 @@ class FriendsTest: XCTestCase { let data = try!complexList().toData() let fileHandle = writeToFileAndReturnHandle(data) - let fileReader = FBFileReader(fileHandle: fileHandle) + let fileReader = FlatBuffersFileReader(fileHandle: fileHandle) let newList = PeopleList.from(reader:fileReader) @@ -210,7 +210,7 @@ class FriendsTest: XCTestCase { let data = try!complexList().toData() let fileHandle = writeToFileAndReturnHandle(data) - let reader = FBFileReader(fileHandle: fileHandle) + let reader = FlatBuffersFileReader(fileHandle: fileHandle) let newList = PeopleList_Direct(reader) diff --git a/FlatBuffersSwiftTests/contacts_fb.swift b/FlatBuffersSwiftTests/contacts_fb.swift index 2bc3267..539896d 100644 --- a/FlatBuffersSwiftTests/contacts_fb.swift +++ b/FlatBuffersSwiftTests/contacts_fb.swift @@ -43,7 +43,7 @@ public extension ContactList { } public extension ContactList { public static func from(data : Data, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()) -> ContactList? { - let reader = FBMemoryReader(data: data, cache: cache) + let reader = FlatBuffersMemoryReader(data: data, cache: cache) return from(reader: reader) } public static func from(reader : FlatBuffersReader) -> ContactList? { @@ -171,7 +171,7 @@ public extension Contact { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.name = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ + _result.name = reader.stringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ _result.birthday = Date.create(reader, objectOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 1)) _result.gender = Gender(rawValue: reader.get(objectOffset: objectOffset, propertyIndex: 2, defaultValue: Gender.Male.rawValue)) let offset_tags : Offset? = reader.offset(objectOffset: objectOffset, propertyIndex: 3) @@ -180,7 +180,7 @@ public extension Contact { var index = 0 _result.tags.reserveCapacity(length_tags) while index < length_tags { - let element = reader.getStringBuffer(stringOffset: reader.vectorElementOffset(vectorOffset: offset_tags, index: index))?§ + let element = reader.stringBuffer(stringOffset: reader.vectorElementOffset(vectorOffset: offset_tags, index: index))?§ _result.tags.append(element) index += 1 } @@ -203,7 +203,7 @@ public extension Contact { var index = 0 _result.previousLocations.reserveCapacity(length_previousLocations) while index < length_previousLocations { - let element : GeoLocation? = reader.vectorScalarElement(vectorOffset: offset_previousLocations, index: index) + let element : GeoLocation? = reader.vectorElementScalar(vectorOffset: offset_previousLocations, index: index) _result.previousLocations.append(element) index += 1 } @@ -214,7 +214,7 @@ public extension Contact { var index = 0 _result.moods.reserveCapacity(length_moods) while index < length_moods { - if let raw : Int8 = reader.vectorScalarElement(vectorOffset: offset_moods, index: index){ + if let raw : Int8 = reader.vectorElementScalar(vectorOffset: offset_moods, index: index){ let element : Mood? = Mood(rawValue: raw) _result.moods.append(element) } else { @@ -233,7 +233,7 @@ public struct Contact_Direct : Hashable { self.reader = reader self.myOffset = myOffset } - public var name : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } + public var name : UnsafeBufferPointer? { get { return reader.stringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var birthday : Date_Direct? { get { if let offset = reader.offset(objectOffset: myOffset, propertyIndex: 1) { return Date_Direct(reader: reader, myOffset: offset) @@ -249,7 +249,7 @@ public struct Contact_Direct : Hashable { public func getTagsElement(atIndex index : Int) -> UnsafeBufferPointer? { let offsetList = reader.offset(objectOffset: myOffset, propertyIndex: 3) if let ofs = reader.vectorElementOffset(vectorOffset: offsetList, index: index) { - return reader.getStringBuffer(stringOffset: ofs) + return reader.stringBuffer(stringOffset: ofs) } return nil } @@ -271,7 +271,7 @@ public struct Contact_Direct : Hashable { } public func getPreviousLocationsElement(atIndex index : Int) -> GeoLocation? { let offsetList = reader.offset(objectOffset: myOffset, propertyIndex: 6) - let result : GeoLocation? = reader.vectorScalarElement(vectorOffset: offsetList, index: index) + let result : GeoLocation? = reader.vectorElementScalar(vectorOffset: offsetList, index: index) return result } public var moodsCount : Int { @@ -279,7 +279,7 @@ public struct Contact_Direct : Hashable { } public func getMoodsElement(atIndex index : Int) -> Mood? { let offsetList = reader.offset(objectOffset: myOffset, propertyIndex: 7) - guard let rawValue : Int8 = reader.vectorScalarElement(vectorOffset: offsetList, index: index) else { + guard let rawValue : Int8 = reader.vectorElementScalar(vectorOffset: offsetList, index: index) else { return nil } return Mood(rawValue: rawValue) @@ -552,10 +552,10 @@ public extension PostalAddress { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.country = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ - _result.city = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 1))?§ + _result.country = reader.stringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ + _result.city = reader.stringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 1))?§ _result.postalCode = reader.get(objectOffset: objectOffset, propertyIndex: 2, defaultValue: 0) - _result.streetAndNumber = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 3))?§ + _result.streetAndNumber = reader.stringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 3))?§ return _result } } @@ -566,12 +566,12 @@ public struct PostalAddress_Direct : Hashable { self.reader = reader self.myOffset = myOffset } - public var country : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } - public var city : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:1)) } } + public var country : UnsafeBufferPointer? { get { return reader.stringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } + public var city : UnsafeBufferPointer? { get { return reader.stringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:1)) } } public var postalCode : Int32 { get { return reader.get(objectOffset: myOffset, propertyIndex: 2, defaultValue: 0) } } - public var streetAndNumber : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:3)) } } + public var streetAndNumber : UnsafeBufferPointer? { get { return reader.stringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:3)) } } public var hashValue: Int { return Int(myOffset) } } public func ==(t1 : PostalAddress_Direct, t2 : PostalAddress_Direct) -> Bool { @@ -619,7 +619,7 @@ public extension EmailAddress { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.mailto = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ + _result.mailto = reader.stringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ return _result } } @@ -630,7 +630,7 @@ public struct EmailAddress_Direct : Hashable { self.reader = reader self.myOffset = myOffset } - public var mailto : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } + public var mailto : UnsafeBufferPointer? { get { return reader.stringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var hashValue: Int { return Int(myOffset) } } public func ==(t1 : EmailAddress_Direct, t2 : EmailAddress_Direct) -> Bool { @@ -673,7 +673,7 @@ public extension WebAddress { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.url = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ + _result.url = reader.stringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ return _result } } @@ -684,7 +684,7 @@ public struct WebAddress_Direct : Hashable { self.reader = reader self.myOffset = myOffset } - public var url : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } + public var url : UnsafeBufferPointer? { get { return reader.stringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var hashValue: Int { return Int(myOffset) } } public func ==(t1 : WebAddress_Direct, t2 : WebAddress_Direct) -> Bool { @@ -727,7 +727,7 @@ public extension TelephoneNumber { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.number = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ + _result.number = reader.stringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ return _result } } @@ -738,7 +738,7 @@ public struct TelephoneNumber_Direct : Hashable { self.reader = reader self.myOffset = myOffset } - public var number : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } + public var number : UnsafeBufferPointer? { get { return reader.stringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var hashValue: Int { return Int(myOffset) } } public func ==(t1 : TelephoneNumber_Direct, t2 : TelephoneNumber_Direct) -> Bool { diff --git a/FlatBuffersSwiftTests/friends_fb.swift b/FlatBuffersSwiftTests/friends_fb.swift index 83dde2d..9217757 100644 --- a/FlatBuffersSwiftTests/friends_fb.swift +++ b/FlatBuffersSwiftTests/friends_fb.swift @@ -40,7 +40,7 @@ public extension PeopleList { } public extension PeopleList { public static func from(data : Data, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()) -> PeopleList? { - let reader = FBMemoryReader(data: data, cache: cache) + let reader = FlatBuffersMemoryReader(data: data, cache: cache) return from(reader: reader) } public static func from(reader : FlatBuffersReader) -> PeopleList? { @@ -172,7 +172,7 @@ public extension Friend { if let cache = reader.cache { cache.objectPool[objectOffset] = _result } - _result.name = reader.getStringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ + _result.name = reader.stringBuffer(stringOffset: reader.offset(objectOffset: objectOffset, propertyIndex: 0))?§ let offset_friends : Offset? = reader.offset(objectOffset: objectOffset, propertyIndex: 1) let length_friends = reader.vectorElementCount(vectorOffset: offset_friends) if(length_friends > 0){ @@ -197,7 +197,7 @@ public struct Friend_Direct : Hashable { self.reader = reader self.myOffset = myOffset } - public var name : UnsafeBufferPointer? { get { return reader.getStringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } + public var name : UnsafeBufferPointer? { get { return reader.stringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var friendsCount : Int { return reader.vectorElementCount(vectorOffset: reader.offset(objectOffset: myOffset, propertyIndex: 1)) } From 4cef8dfba3c177ccfaedb694a00262df30af960b Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Tue, 10 Jan 2017 19:02:19 +0100 Subject: [PATCH 09/16] Fix typo. --- FlatBuffersSwift/FlatBuffersReader.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FlatBuffersSwift/FlatBuffersReader.swift b/FlatBuffersSwift/FlatBuffersReader.swift index 8a816e0..c70670c 100644 --- a/FlatBuffersSwift/FlatBuffersReader.swift +++ b/FlatBuffersSwift/FlatBuffersReader.swift @@ -220,7 +220,7 @@ public extension FlatBuffersReader { } /** - Retrieve a scalar optional value (return nill if unavailable) + Retrieve a scalar optional value (return nil if unavailable) - parameters: - objectOffset: The offset of the object From 6e6d127a8f5ce0302d9a05f1e002c61c7ed90253 Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Wed, 11 Jan 2017 12:24:42 +0100 Subject: [PATCH 10/16] FB -> FlatBuffers data -> makeData according to Swift API guidelines (gives a hint that it is an expensive operation, which is good). --- FlatBuffersSwift/FlatBuffersBuilder.swift | 67 +++++++++++++++-------- FlatBuffersSwiftTests/BuilderTest.swift | 18 +++--- FlatBuffersSwiftTests/FBReaderTest.swift | 8 +-- FlatBuffersSwiftTests/contacts_fb.swift | 26 ++++----- FlatBuffersSwiftTests/friends_fb.swift | 20 +++---- 5 files changed, 79 insertions(+), 60 deletions(-) diff --git a/FlatBuffersSwift/FlatBuffersBuilder.swift b/FlatBuffersSwift/FlatBuffersBuilder.swift index c6e1708..8a099d4 100644 --- a/FlatBuffersSwift/FlatBuffersBuilder.swift +++ b/FlatBuffersSwift/FlatBuffersBuilder.swift @@ -5,6 +5,7 @@ // Created by Maxim Zaks on 27.10.16. // Copyright © 2016 Maxim Zaks. All rights reserved. // +// See https://github.com/mzaks/FlatBuffersSwift/graphs/contributors for contributors. import Foundation @@ -26,7 +27,8 @@ extension UInt : Scalar {} extension Float32 : Scalar {} extension Float64 : Scalar {} -public struct FBBuildConfig { +/// Various configuration settings for the builder +public struct FlatBuffersBuildConfig { public let initialCapacity : Int public let uniqueStrings : Bool public let uniqueTables : Bool @@ -43,7 +45,7 @@ public struct FBBuildConfig { } } -public enum FBBuildError : Error { +public enum FlatBuffersBuildError : Error { case objectIsNotClosed case noOpenObject case propertyIndexIsInvalid @@ -53,10 +55,11 @@ public enum FBBuildError : Error { case unsupportedType } -public final class FBBuilder { +/// A FlatBuffers builder that supports the generation of flatbuffers 'wire' format from an object graph +public final class FlatBuffersBuilder { - private var _config : FBBuildConfig - public var config : FBBuildConfig { return _config } + private var _config : FlatBuffersBuildConfig + public var config : FlatBuffersBuildConfig { return _config } private var capacity : Int private var _data : UnsafeMutableRawPointer private var minalign = 1; @@ -73,14 +76,30 @@ public final class FBBuilder { public var cache : [ObjectIdentifier : Offset] = [:] public var inProgress : Set = [] public var deferedBindings : ContiguousArray<(object:Any, cursor:Int)> = [] - - public init(config : FBBuildConfig = FBBuildConfig()) { + + /** + Initializes the builder + + - parameters: + - config: The configuration settings to use for this builder. + + - Returns: A FlatBuffers builder ready for use. + */ + public init(config : FlatBuffersBuildConfig = FlatBuffersBuildConfig()) { self._config = config self.capacity = config.initialCapacity _data = UnsafeMutableRawPointer.allocate(bytes: capacity, alignedTo: minalign) } - public var data : Data { + /** + Allocates, initializes and returns a Data object from the builder backing store + + - parameters: + - config: The configuration settings to use for this builder. + + - Returns: A FlatBuffers builder ready for use. + */ + public var makeData : Data { return Data(bytes:_data.advanced(by:leftCursor), count: cursor) } @@ -131,7 +150,7 @@ public final class FBBuilder { return cursor } guard offset <= Int32(cursor) else { - throw FBBuildError.offsetIsTooBig + throw FlatBuffersBuildError.offsetIsTooBig } if offset == Int32(0) { @@ -146,10 +165,10 @@ public final class FBBuilder { public func replaceOffset(offset : Offset, atCursor jumpCursor: Int) throws{ guard offset <= Int32(cursor) else { - throw FBBuildError.offsetIsTooBig + throw FlatBuffersBuildError.offsetIsTooBig } guard jumpCursor <= cursor else { - throw FBBuildError.cursorIsInvalid + throw FlatBuffersBuildError.cursorIsInvalid } let _offset = Int32(jumpCursor) - offset; @@ -162,7 +181,7 @@ public final class FBBuilder { public func openObject(numOfProperties : Int) throws { guard objectStart == -1 && vectorNumElems == -1 else { - throw FBBuildError.objectIsNotClosed + throw FlatBuffersBuildError.objectIsNotClosed } currentVTable.removeAll(keepingCapacity: true) currentVTable.reserveCapacity(numOfProperties) @@ -175,10 +194,10 @@ public final class FBBuilder { @discardableResult public func addPropertyOffsetToOpenObject(propertyIndex : Int, offset : Offset) throws -> Int{ guard objectStart > -1 else { - throw FBBuildError.noOpenObject + throw FlatBuffersBuildError.noOpenObject } guard propertyIndex >= 0 && propertyIndex < currentVTable.count else { - throw FBBuildError.propertyIndexIsInvalid + throw FlatBuffersBuildError.propertyIndexIsInvalid } _ = try putOffset(offset: offset) currentVTable[propertyIndex] = Int32(cursor) @@ -187,10 +206,10 @@ public final class FBBuilder { public func addPropertyToOpenObject(propertyIndex : Int, value : T, defaultValue : T) throws { guard objectStart > -1 else { - throw FBBuildError.noOpenObject + throw FlatBuffersBuildError.noOpenObject } guard propertyIndex >= 0 && propertyIndex < currentVTable.count else { - throw FBBuildError.propertyIndexIsInvalid + throw FlatBuffersBuildError.propertyIndexIsInvalid } if(config.forceDefaults == false && value == defaultValue) { @@ -203,17 +222,17 @@ public final class FBBuilder { public func addCurrentOffsetAsPropertyToOpenObject(propertyIndex : Int) throws { guard objectStart > -1 else { - throw FBBuildError.noOpenObject + throw FlatBuffersBuildError.noOpenObject } guard propertyIndex >= 0 && propertyIndex < currentVTable.count else { - throw FBBuildError.propertyIndexIsInvalid + throw FlatBuffersBuildError.propertyIndexIsInvalid } currentVTable[propertyIndex] = Int32(cursor) } public func closeObject() throws -> Offset { guard objectStart > -1 else { - throw FBBuildError.noOpenObject + throw FlatBuffersBuildError.noOpenObject } align(size: 4, additionalBytes: 0) increaseCapacity(size: 4) @@ -277,7 +296,7 @@ public final class FBBuilder { public func startVector(count : Int, elementSize : Int) throws{ align(size: 4, additionalBytes: count * elementSize) guard objectStart == -1 && vectorNumElems == -1 else { - throw FBBuildError.objectIsNotClosed + throw FlatBuffersBuildError.objectIsNotClosed } vectorNumElems = Int32(count) } @@ -291,7 +310,7 @@ public final class FBBuilder { private var stringCache : [String:Offset] = [:] public func createString(value : String?) throws -> Offset { guard objectStart == -1 && vectorNumElems == -1 else { - throw FBBuildError.objectIsNotClosed + throw FlatBuffersBuildError.objectIsNotClosed } guard let value = value else { return 0 @@ -332,10 +351,10 @@ public final class FBBuilder { public func finish(offset : Offset, fileIdentifier : String?) throws -> Void { guard offset <= Int32(cursor) else { - throw FBBuildError.offsetIsTooBig + throw FlatBuffersBuildError.offsetIsTooBig } guard objectStart == -1 && vectorNumElems == -1 else { - throw FBBuildError.objectIsNotClosed + throw FlatBuffersBuildError.objectIsNotClosed } var prefixLength = 4 if let fileIdentifier = fileIdentifier { @@ -344,7 +363,7 @@ public final class FBBuilder { let utf8View = fileIdentifier.utf8 let count = utf8View.count guard count == 4 else { - throw FBBuildError.badFileIdentifier + throw FlatBuffersBuildError.badFileIdentifier } for c in utf8View.lazy.reversed() { put(value: c) diff --git a/FlatBuffersSwiftTests/BuilderTest.swift b/FlatBuffersSwiftTests/BuilderTest.swift index d500b9e..11785b5 100644 --- a/FlatBuffersSwiftTests/BuilderTest.swift +++ b/FlatBuffersSwiftTests/BuilderTest.swift @@ -11,17 +11,17 @@ import XCTest class BuilderTest: XCTestCase { - var builder : FBBuilder! + var builder : FlatBuffersBuilder! var byteIndex = 0 override func setUp() { super.setUp() - builder = FBBuilder(config: FBBuildConfig()) + builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig()) byteIndex = 0 } override func tearDown() { - XCTAssertEqual(builder.data.count, byteIndex) + XCTAssertEqual(builder.makeData.count, byteIndex) super.tearDown() } @@ -237,7 +237,7 @@ class BuilderTest: XCTestCase { func testNullTerminatedString(){ - builder = FBBuilder(config: FBBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: true, forceDefaults: false, nullTerminatedUTF8: true)) + builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: true, forceDefaults: false, nullTerminatedUTF8: true)) let o = try!builder.createString(value: "maxim") @@ -449,7 +449,7 @@ class BuilderTest: XCTestCase { } func testObjectWithoutNameReuseAndAge() { - builder = FBBuilder(config: FBBuildConfig(initialCapacity: 1, uniqueStrings: false, uniqueTables: true, uniqueVTables: true, forceDefaults: false, nullTerminatedUTF8: false)) + builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: false, uniqueTables: true, uniqueVTables: true, forceDefaults: false, nullTerminatedUTF8: false)) let s = try!builder.createString(value: "maxim") let s2 = try!builder.createString(value: "maxim") try!builder.openObject(numOfProperties: 3) @@ -939,7 +939,7 @@ class BuilderTest: XCTestCase { } func testObjectWithVectorToOtherTwoObjectsAndVTableWithoutReuese(){ - builder = FBBuilder(config: FBBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: false, forceDefaults: false, nullTerminatedUTF8: false)) + builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: false, forceDefaults: false, nullTerminatedUTF8: false)) try!builder.openObject(numOfProperties: 2) try!builder.addPropertyToOpenObject(propertyIndex: 0, value: Int16(12), defaultValue: 0) try!builder.addPropertyToOpenObject(propertyIndex: 1, value: Int16(13), defaultValue: 0) @@ -1292,7 +1292,7 @@ class BuilderTest: XCTestCase { } func testDefaultValuesForced(){ - builder = FBBuilder(config: FBBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: true, forceDefaults: true, nullTerminatedUTF8: false)) + builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: true, forceDefaults: true, nullTerminatedUTF8: false)) try!builder.openObject(numOfProperties: 2) try!builder.addPropertyToOpenObject(propertyIndex: 0, value: Int32(12), defaultValue: 0) try!builder.addPropertyToOpenObject(propertyIndex: 1, value: Int32(5), defaultValue: 5) @@ -1323,7 +1323,7 @@ class BuilderTest: XCTestCase { } func assertByte(with: UInt8){ - XCTAssertEqual(builder.data[byteIndex], with) + XCTAssertEqual(builder.makeData[byteIndex], with) byteIndex += 1 } @@ -1332,7 +1332,7 @@ class BuilderTest: XCTestCase { } func printData(){ - dump(builder.data) + dump(builder.makeData) } /* diff --git a/FlatBuffersSwiftTests/FBReaderTest.swift b/FlatBuffersSwiftTests/FBReaderTest.swift index 292391e..97e1315 100644 --- a/FlatBuffersSwiftTests/FBReaderTest.swift +++ b/FlatBuffersSwiftTests/FBReaderTest.swift @@ -237,7 +237,7 @@ class FlatBuffersReaderTest: XCTestCase { } func createSimpleObject() -> Data { - let fbb = FBBuilder(config:FBBuildConfig( + let fbb = FlatBuffersBuilder(config:FlatBuffersBuildConfig( initialCapacity : 1, uniqueStrings : true, uniqueTables : true, @@ -251,13 +251,13 @@ class FlatBuffersReaderTest: XCTestCase { try! fbb.addPropertyOffsetToOpenObject(propertyIndex: 1, offset: sOffset) let oOffset = try! fbb.closeObject() try! fbb.finish(offset: oOffset, fileIdentifier: nil) - let data = fbb.data + let data = fbb.makeData return data } func createObjectWithVectors() -> Data{ - let fbb = FBBuilder(config:FBBuildConfig( + let fbb = FlatBuffersBuilder(config:FlatBuffersBuildConfig( initialCapacity : 1, uniqueStrings : true, uniqueTables : true, @@ -285,7 +285,7 @@ class FlatBuffersReaderTest: XCTestCase { try! fbb.addPropertyOffsetToOpenObject(propertyIndex: 1, offset: bVectorOffset) let oOffset = try! fbb.closeObject() try! fbb.finish(offset: oOffset, fileIdentifier: nil) - let data = fbb.data + let data = fbb.makeData return data } diff --git a/FlatBuffersSwiftTests/contacts_fb.swift b/FlatBuffersSwiftTests/contacts_fb.swift index 539896d..de95d53 100644 --- a/FlatBuffersSwiftTests/contacts_fb.swift +++ b/FlatBuffersSwiftTests/contacts_fb.swift @@ -53,14 +53,14 @@ public extension ContactList { } public extension ContactList { - public func encode(withBuilder builder : FBBuilder) throws -> Void { + public func encode(withBuilder builder : FlatBuffersBuilder) throws -> Void { let offset = try addToByteArray(builder) try builder.finish(offset: offset, fileIdentifier: nil) } - public func toData(withConfig config : FBBuildConfig = FBBuildConfig()) throws -> Data { - let builder = FBBuilder(config: config) + public func toData(withConfig config : FlatBuffersBuildConfig = FlatBuffersBuildConfig()) throws -> Data { + let builder = FlatBuffersBuilder(config: config) try encode(withBuilder: builder) - return builder.data + return builder.makeData } } @@ -97,7 +97,7 @@ public func ==(t1 : ContactList_Direct, t2 : ContactList_Direct) -> Boo return t1.reader.isEqual(other: t2.reader) && t1.myOffset == t2.myOffset } public extension ContactList { - fileprivate func addToByteArray(_ builder : FBBuilder) throws -> Offset { + fileprivate func addToByteArray(_ builder : FlatBuffersBuilder) throws -> Offset { if builder.config.uniqueTables { if let myOffset = builder.cache[ObjectIdentifier(self)] { return myOffset @@ -290,7 +290,7 @@ public func ==(t1 : Contact_Direct, t2 : Contact_Direct) -> Bool { return t1.reader.isEqual(other: t2.reader) && t1.myOffset == t2.myOffset } public extension Contact { - fileprivate func addToByteArray(_ builder : FBBuilder) throws -> Offset { + fileprivate func addToByteArray(_ builder : FlatBuffersBuilder) throws -> Offset { if builder.config.uniqueTables { if let myOffset = builder.cache[ObjectIdentifier(self)] { return myOffset @@ -435,7 +435,7 @@ public func ==(t1 : Date_Direct, t2 : Date_Direct) -> Bool { return t1.reader.isEqual(other: t2.reader) && t1.myOffset == t2.myOffset } public extension Date { - fileprivate func addToByteArray(_ builder : FBBuilder) throws -> Offset { + fileprivate func addToByteArray(_ builder : FlatBuffersBuilder) throws -> Offset { if builder.config.uniqueTables { if let myOffset = builder.cache[ObjectIdentifier(self)] { return myOffset @@ -506,7 +506,7 @@ public func ==(t1 : AddressEntry_Direct, t2 : AddressEntry_Direct) -> B return t1.reader.isEqual(other: t2.reader) && t1.myOffset == t2.myOffset } public extension AddressEntry { - fileprivate func addToByteArray(_ builder : FBBuilder) throws -> Offset { + fileprivate func addToByteArray(_ builder : FlatBuffersBuilder) throws -> Offset { if builder.config.uniqueTables { if let myOffset = builder.cache[ObjectIdentifier(self)] { return myOffset @@ -578,7 +578,7 @@ public func ==(t1 : PostalAddress_Direct, t2 : PostalAddress_Direct) -> return t1.reader.isEqual(other: t2.reader) && t1.myOffset == t2.myOffset } public extension PostalAddress { - fileprivate func addToByteArray(_ builder : FBBuilder) throws -> Offset { + fileprivate func addToByteArray(_ builder : FlatBuffersBuilder) throws -> Offset { if builder.config.uniqueTables { if let myOffset = builder.cache[ObjectIdentifier(self)] { return myOffset @@ -637,7 +637,7 @@ public func ==(t1 : EmailAddress_Direct, t2 : EmailAddress_Direct) -> B return t1.reader.isEqual(other: t2.reader) && t1.myOffset == t2.myOffset } public extension EmailAddress { - fileprivate func addToByteArray(_ builder : FBBuilder) throws -> Offset { + fileprivate func addToByteArray(_ builder : FlatBuffersBuilder) throws -> Offset { if builder.config.uniqueTables { if let myOffset = builder.cache[ObjectIdentifier(self)] { return myOffset @@ -691,7 +691,7 @@ public func ==(t1 : WebAddress_Direct, t2 : WebAddress_Direct) -> Bool return t1.reader.isEqual(other: t2.reader) && t1.myOffset == t2.myOffset } public extension WebAddress { - fileprivate func addToByteArray(_ builder : FBBuilder) throws -> Offset { + fileprivate func addToByteArray(_ builder : FlatBuffersBuilder) throws -> Offset { if builder.config.uniqueTables { if let myOffset = builder.cache[ObjectIdentifier(self)] { return myOffset @@ -745,7 +745,7 @@ public func ==(t1 : TelephoneNumber_Direct, t2 : TelephoneNumber_Direct return t1.reader.isEqual(other: t2.reader) && t1.myOffset == t2.myOffset } public extension TelephoneNumber { - fileprivate func addToByteArray(_ builder : FBBuilder) throws -> Offset { + fileprivate func addToByteArray(_ builder : FlatBuffersBuilder) throws -> Offset { if builder.config.uniqueTables { if let myOffset = builder.cache[ObjectIdentifier(self)] { return myOffset @@ -813,7 +813,7 @@ private func unionCase_Address(_ union : Address?) -> Int8 { default : return 0 } } -fileprivate func addToByteArray_Address(_ builder : FBBuilder, union : Address?) throws -> Offset { +fileprivate func addToByteArray_Address(_ builder : FlatBuffersBuilder, union : Address?) throws -> Offset { switch union { case let u as PostalAddress : return try u.addToByteArray(builder) case let u as EmailAddress : return try u.addToByteArray(builder) diff --git a/FlatBuffersSwiftTests/friends_fb.swift b/FlatBuffersSwiftTests/friends_fb.swift index 9217757..975c317 100644 --- a/FlatBuffersSwiftTests/friends_fb.swift +++ b/FlatBuffersSwiftTests/friends_fb.swift @@ -50,15 +50,15 @@ public extension PeopleList { } public extension PeopleList { - public func encode(withBuilder builder : FBBuilder) throws -> Void { + public func encode(withBuilder builder : FlatBuffersBuilder) throws -> Void { let offset = try addToByteArray(builder) try performLateBindings(builder) try builder.finish(offset: offset, fileIdentifier: "TEST") } - public func toData(withConfig config : FBBuildConfig = FBBuildConfig()) throws -> Data { - let builder = FBBuilder(config: config) + public func toData(withConfig config : FlatBuffersBuildConfig = FlatBuffersBuildConfig()) throws -> Data { + let builder = FlatBuffersBuilder(config: config) try encode(withBuilder: builder) - return builder.data + return builder.makeData } } @@ -92,7 +92,7 @@ public func ==(t1 : PeopleList_Direct, t2 : PeopleList_Direct) -> Bool return t1.reader.isEqual(other: t2.reader) && t1.myOffset == t2.myOffset } public extension PeopleList { - fileprivate func addToByteArray(_ builder : FBBuilder) throws -> Offset { + fileprivate func addToByteArray(_ builder : FlatBuffersBuilder) throws -> Offset { if builder.config.uniqueTables { if let myOffset = builder.cache[ObjectIdentifier(self)] { return myOffset @@ -229,7 +229,7 @@ public func ==(t1 : Friend_Direct, t2 : Friend_Direct) -> Bool { return t1.reader.isEqual(other: t2.reader) && t1.myOffset == t2.myOffset } public extension Friend { - fileprivate func addToByteArray(_ builder : FBBuilder) throws -> Offset { + fileprivate func addToByteArray(_ builder : FlatBuffersBuilder) throws -> Offset { if builder.config.uniqueTables { if let myOffset = builder.cache[ObjectIdentifier(self)] { return myOffset @@ -352,7 +352,7 @@ public func ==(t1 : Male_Direct, t2 : Male_Direct) -> Bool { return t1.reader.isEqual(other: t2.reader) && t1.myOffset == t2.myOffset } public extension Male { - fileprivate func addToByteArray(_ builder : FBBuilder) throws -> Offset { + fileprivate func addToByteArray(_ builder : FlatBuffersBuilder) throws -> Offset { if builder.config.uniqueTables { if let myOffset = builder.cache[ObjectIdentifier(self)] { return myOffset @@ -423,7 +423,7 @@ public func ==(t1 : Female_Direct, t2 : Female_Direct) -> Bool { return t1.reader.isEqual(other: t2.reader) && t1.myOffset == t2.myOffset } public extension Female { - fileprivate func addToByteArray(_ builder : FBBuilder) throws -> Offset { + fileprivate func addToByteArray(_ builder : FlatBuffersBuilder) throws -> Offset { if builder.config.uniqueTables { if let myOffset = builder.cache[ObjectIdentifier(self)] { return myOffset @@ -493,14 +493,14 @@ private func unionCase_Human(_ union : Human?) -> Int8 { default : return 0 } } -fileprivate func addToByteArray_Human(_ builder : FBBuilder, union : Human?) throws -> Offset { +fileprivate func addToByteArray_Human(_ builder : FlatBuffersBuilder, union : Human?) throws -> Offset { switch union { case let u as Male : return try u.addToByteArray(builder) case let u as Female : return try u.addToByteArray(builder) default : return 0 } } -private func performLateBindings(_ builder : FBBuilder) throws { +private func performLateBindings(_ builder : FlatBuffersBuilder) throws { for binding in builder.deferedBindings { switch binding.object { case let object as PeopleList: try builder.replaceOffset(offset: object.addToByteArray(builder), atCursor: binding.cursor) From 8623f13aa40c63c6767ca5eb54759212f33c5764 Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Wed, 11 Jan 2017 13:31:25 +0100 Subject: [PATCH 11/16] Move to append/update --- FlatBuffersSwift/FlatBuffersBuilder.swift | 108 +++++++++++++++------- FlatBuffersSwiftTests/BuilderTest.swift | 108 +++++++++++----------- FlatBuffersSwiftTests/FBReaderTest.swift | 10 +- FlatBuffersSwiftTests/contacts_fb.swift | 12 +-- FlatBuffersSwiftTests/friends_fb.swift | 12 +-- 5 files changed, 145 insertions(+), 105 deletions(-) diff --git a/FlatBuffersSwift/FlatBuffersBuilder.swift b/FlatBuffersSwift/FlatBuffersBuilder.swift index 8a099d4..f60075e 100644 --- a/FlatBuffersSwift/FlatBuffersBuilder.swift +++ b/FlatBuffersSwift/FlatBuffersBuilder.swift @@ -92,18 +92,25 @@ public final class FlatBuffersBuilder { } /** - Allocates, initializes and returns a Data object from the builder backing store + Allocates and returns a Data object initialized from the builder backing store - - parameters: - - config: The configuration settings to use for this builder. - - - Returns: A FlatBuffers builder ready for use. + - Returns: A Data object initilized with the data from the builder backing store */ public var makeData : Data { return Data(bytes:_data.advanced(by:leftCursor), count: cursor) } - private func increaseCapacity(size : Int){ + /** + Reserve enough space to store at a minimum size more data and resize the + underlying buffer if needed. + + The data should be consumed by the builder immediately after reservation. + + - parameters: + - size: The additional size that will be consumed by the builder + immedieatly after the call + */ + private func reserveAdditionalCapacity(size : Int){ guard leftCursor <= size else { return } @@ -119,17 +126,30 @@ public final class FlatBuffersBuilder { _data = newData } + /** + Perform alignment for a value of a given size by performing padding in advance + of actually putting the value to the buffer. + + - parameters: + - size: xxx + - additionalBytes: xxx + */ private func align(size : Int, additionalBytes : Int){ if size > minalign { minalign = size } let alignSize = ((~(cursor + additionalBytes)) + 1) & (size - 1) - increaseCapacity(size: alignSize) + reserveAdditionalCapacity(size: alignSize) cursor += alignSize - } - public func put(value : T){ + /** + Append a scalar value to the buffer + + - parameters: + - value: The value to add to the buffer + */ + public func append(value : T){ let c = MemoryLayout.stride(ofValue: value) if c > 8 { align(size: 8, additionalBytes: c) @@ -137,16 +157,22 @@ public final class FlatBuffersBuilder { align(size: c, additionalBytes: 0) } - increaseCapacity(size: c) + reserveAdditionalCapacity(size: c) _data.storeBytes(of: value, toByteOffset: leftCursor-c, as: T.self) cursor += c } + /** + Make offset relative and append it to the buffer + + - parameters: + - offset: The offset to transform and add to the buffer + */ @discardableResult - public func putOffset(offset : Offset?) throws -> Int { // make offset relative and put it into byte buffer + public func append(offset : Offset?) throws -> Int { guard let offset = offset else { - put(value: Offset(0)) + append(value: Offset(0)) return cursor } guard offset <= Int32(cursor) else { @@ -154,16 +180,23 @@ public final class FlatBuffersBuilder { } if offset == Int32(0) { - put(value: Offset(0)) + append(value: Offset(0)) return cursor } align(size: 4, additionalBytes: 0) let _offset = Int32(cursor) - offset + MemoryLayout.stride; - put(value: _offset) + append(value: _offset) return cursor } - - public func replaceOffset(offset : Offset, atCursor jumpCursor: Int) throws{ + + /** + Update an offset in place + + - parameters: + - offset: The new offset to transform and add to the buffer + - atCursor: The position to put the new offset to + */ + public func update(offset : Offset, atCursor jumpCursor: Int) throws{ guard offset <= Int32(cursor) else { throw FlatBuffersBuildError.offsetIsTooBig } @@ -174,8 +207,15 @@ public final class FlatBuffersBuilder { _data.storeBytes(of: _offset, toByteOffset: capacity - jumpCursor, as: Int32.self) } - - private func put(value : T, at index : Int) { + + /** + Update a scalar in place + + - parameters: + - value: The new value + - index: The position to modify + */ + private func update(value : T, at index : Int) { _data.storeBytes(of: value, toByteOffset: index + leftCursor, as: T.self) } @@ -199,7 +239,7 @@ public final class FlatBuffersBuilder { guard propertyIndex >= 0 && propertyIndex < currentVTable.count else { throw FlatBuffersBuildError.propertyIndexIsInvalid } - _ = try putOffset(offset: offset) + _ = try append(offset: offset) currentVTable[propertyIndex] = Int32(cursor) return cursor } @@ -216,7 +256,7 @@ public final class FlatBuffersBuilder { return } - put(value: value) + append(value: value) currentVTable[propertyIndex] = Int32(cursor) } @@ -235,7 +275,7 @@ public final class FlatBuffersBuilder { throw FlatBuffersBuildError.noOpenObject } align(size: 4, additionalBytes: 0) - increaseCapacity(size: 4) + reserveAdditionalCapacity(size: 4) cursor += 4 // Will be set to vtable offset afterwards let vtableloc = cursor @@ -245,14 +285,14 @@ public final class FlatBuffersBuilder { while(index>=0) { // Offset relative to the start of the table. let off = Int16(currentVTable[index] != 0 ? Int32(vtableloc) - currentVTable[index] : 0); - put(value: off); + append(value: off); index -= 1 } let numberOfstandardFields = 2 - put(value: Int16(Int32(vtableloc) - objectStart)); // standard field 1: lenght of the object data - put(value: Int16((currentVTable.count + numberOfstandardFields) * MemoryLayout.stride)); // standard field 2: length of vtable and standard fields them selves + append(value: Int16(Int32(vtableloc) - objectStart)); // standard field 1: lenght of the object data + append(value: Int16((currentVTable.count + numberOfstandardFields) * MemoryLayout.stride)); // standard field 2: length of vtable and standard fields them selves // search if we already have same vtable let vtableDataLength = cursor - vtableloc @@ -286,7 +326,7 @@ public final class FlatBuffersBuilder { let indexLocation = cursor - vtableloc - put(value: Int32(foundVTableOffset), at: indexLocation) + update(value: Int32(foundVTableOffset), at: indexLocation) objectStart = -1 @@ -302,7 +342,7 @@ public final class FlatBuffersBuilder { } public func endVector() -> Offset { - put(value: vectorNumElems) + append(value: vectorNumElems) vectorNumElems = -1 return Int32(cursor) } @@ -326,20 +366,20 @@ public final class FlatBuffersBuilder { let utf8View = value.utf8CString let length = utf8View.count align(size: 4, additionalBytes: length) - increaseCapacity(size: length) + reserveAdditionalCapacity(size: length) for c in utf8View.lazy.reversed() { - put(value: c) + append(value: c) } - put(value: Int32(length - 1)) + append(value: Int32(length - 1)) } else { let utf8View = value.utf8 let length = utf8View.count align(size: 4, additionalBytes: length) - increaseCapacity(size: length) + reserveAdditionalCapacity(size: length) for c in utf8View.lazy.reversed() { - put(value: c) + append(value: c) } - put(value: Int32(length)) + append(value: Int32(length)) } let o = Offset(cursor) @@ -366,7 +406,7 @@ public final class FlatBuffersBuilder { throw FlatBuffersBuildError.badFileIdentifier } for c in utf8View.lazy.reversed() { - put(value: c) + append(value: c) } } else { align(size: minalign, additionalBytes: prefixLength) @@ -374,6 +414,6 @@ public final class FlatBuffersBuilder { let v = (Int32(cursor + 4) - offset) - put(value: v) + append(value: v) } } diff --git a/FlatBuffersSwiftTests/BuilderTest.swift b/FlatBuffersSwiftTests/BuilderTest.swift index 11785b5..766bc5a 100644 --- a/FlatBuffersSwiftTests/BuilderTest.swift +++ b/FlatBuffersSwiftTests/BuilderTest.swift @@ -26,11 +26,11 @@ class BuilderTest: XCTestCase { } func testPutOneBytePrimitives() { - builder.put(value: true) - builder.put(value: false) - builder.put(value: Int8(-12)) - builder.put(value: Int8(12)) - builder.put(value: UInt8(13)) + builder.append(value: true) + builder.append(value: false) + builder.append(value: Int8(-12)) + builder.append(value: Int8(12)) + builder.append(value: UInt8(13)) assertByte(with: 13) assertByte(with: 12) @@ -40,9 +40,9 @@ class BuilderTest: XCTestCase { } func testPutTwoBytePrimitives() { - builder.put(value: Int16(-12)) - builder.put(value: Int16(12)) - builder.put(value: UInt16(13)) + builder.append(value: Int16(-12)) + builder.append(value: Int16(12)) + builder.append(value: UInt16(13)) assertByte(with: 13) assertByte(with: 0) @@ -53,9 +53,9 @@ class BuilderTest: XCTestCase { } func testPutFourBytePrimitives() { - builder.put(value: Int32(-12)) - builder.put(value: Int32(12)) - builder.put(value: UInt32(13)) + builder.append(value: Int32(-12)) + builder.append(value: Int32(12)) + builder.append(value: UInt32(13)) assertByte(with: 13) assertByte(with: 0) @@ -73,9 +73,9 @@ class BuilderTest: XCTestCase { } func testPutEightBytePrimitives() { - builder.put(value: Int(-12)) - builder.put(value: Int(12)) - builder.put(value: UInt(13)) + builder.append(value: Int(-12)) + builder.append(value: Int(12)) + builder.append(value: UInt(13)) assertByte(with: 13) assertByte(with: 0) @@ -104,8 +104,8 @@ class BuilderTest: XCTestCase { } func testPutFourByteFloat() { - builder.put(value: Float32(12.5)) - builder.put(value: Float32(-12.5)) + builder.append(value: Float32(12.5)) + builder.append(value: Float32(-12.5)) assertByte(with: 0) assertByte(with: 0) @@ -119,8 +119,8 @@ class BuilderTest: XCTestCase { } func testPutEightByteFloat() { - builder.put(value: Float64(12.5)) - builder.put(value: Float64(-12.5)) + builder.append(value: Float64(12.5)) + builder.append(value: Float64(-12.5)) assertByte(with: 0) assertByte(with: 0) @@ -141,7 +141,7 @@ class BuilderTest: XCTestCase { } func testPutStruct() { - builder.put(value: S1(i1: 9999, i2: 13)) + builder.append(value: S1(i1: 9999, i2: 13)) assertByte(with: 15) assertByte(with: 39) @@ -156,13 +156,13 @@ class BuilderTest: XCTestCase { } func testPrimitiveAlingment() { - builder.put(value: true) - builder.put(value: Int32(16)) - builder.put(value: Int16(7)) - builder.put(value: Int16(8)) - builder.put(value: Int(15)) - builder.put(value: Float32(2.5)) - builder.put(value: Float32(3.5)) + builder.append(value: true) + builder.append(value: Int32(16)) + builder.append(value: Int16(7)) + builder.append(value: Int16(8)) + builder.append(value: Int(15)) + builder.append(value: Float32(2.5)) + builder.append(value: Float32(3.5)) assertByte(with: 0) assertByte(with: 0) @@ -198,8 +198,8 @@ class BuilderTest: XCTestCase { } func testAlignmentWithStruct(){ - builder.put(value: Int16(45)) - builder.put(value: S1(i1: 9999, i2: 11)) + builder.append(value: Int16(45)) + builder.append(value: S1(i1: 9999, i2: 11)) assertByte(with: 15) @@ -257,7 +257,7 @@ class BuilderTest: XCTestCase { } func testStringWithPrimitives(){ - builder.put(value: Int16(25)) + builder.append(value: Int16(25)) let o = try!builder.createString(value: "maxim") @@ -280,9 +280,9 @@ class BuilderTest: XCTestCase { } func testStringWithPrimitivesAndFinish(){ - builder.put(value: Int16(25)) + builder.append(value: Int16(25)) let o = try!builder.createString(value: "maxim") - builder.put(value: true) + builder.append(value: true) try!builder.finish(offset: o, fileIdentifier: nil) XCTAssertEqual(o, 12) @@ -313,9 +313,9 @@ class BuilderTest: XCTestCase { } func testStringWithPrimitivesAndFinishWithFileIdentifier(){ - builder.put(value: Int16(25)) + builder.append(value: Int16(25)) let o = try!builder.createString(value: "maxim") - builder.put(value: true) + builder.append(value: true) try!builder.finish(offset: o, fileIdentifier: "test") XCTAssertEqual(o, 12) @@ -522,7 +522,7 @@ class BuilderTest: XCTestCase { try!builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: s) let cursor = try!builder.addPropertyOffsetToOpenObject(propertyIndex: 2, offset: 0) let o = try!builder.closeObject() - try!builder.replaceOffset(offset: o, atCursor: cursor) + try!builder.update(offset: o, atCursor: cursor) try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 16) @@ -573,7 +573,7 @@ class BuilderTest: XCTestCase { func testObjectWithStruct(){ try!builder.openObject(numOfProperties: 2) try!builder.addPropertyToOpenObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - builder.put(value: S1(i1: 45, i2: 78)) + builder.append(value: S1(i1: 45, i2: 78)) try! builder.addCurrentOffsetAsPropertyToOpenObject(propertyIndex: 0) let o = try!builder.closeObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -610,8 +610,8 @@ class BuilderTest: XCTestCase { func testObjectWithVectorOfInt16(){ try!builder.startVector(count: 2, elementSize: 2) - builder.put(value: Int16(15)) - builder.put(value: Int16(19)) + builder.append(value: Int16(15)) + builder.append(value: Int16(19)) let v = builder.endVector() try!builder.openObject(numOfProperties: 1) try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) @@ -652,8 +652,8 @@ class BuilderTest: XCTestCase { func testObjectWithVectorOfBoolean(){ try!builder.startVector(count: 2, elementSize: 1) - builder.put(value: true) - builder.put(value: false) + builder.append(value: true) + builder.append(value: false) let v = builder.endVector() try!builder.openObject(numOfProperties: 1) try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) @@ -693,8 +693,8 @@ class BuilderTest: XCTestCase { func testObjectWithVectorOfStructs(){ try!builder.startVector(count: 2, elementSize: 1) - builder.put(value: S1(i1: 12, i2: 19)) - builder.put(value: S1(i1: 13, i2: 17)) + builder.append(value: S1(i1: 12, i2: 19)) + builder.append(value: S1(i1: 13, i2: 17)) let v = builder.endVector() try!builder.openObject(numOfProperties: 1) try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) @@ -767,8 +767,8 @@ class BuilderTest: XCTestCase { let o2 = try!builder.closeObject() try!builder.startVector(count: 2, elementSize: 1) - try!builder.putOffset(offset: o1) - try!builder.putOffset(offset: o2) + try!builder.append(offset: o1) + try!builder.append(offset: o2) let v = builder.endVector() try!builder.openObject(numOfProperties: 1) @@ -865,8 +865,8 @@ class BuilderTest: XCTestCase { let o2 = try!builder.closeObject() try!builder.startVector(count: 2, elementSize: 1) - try!builder.putOffset(offset: o1) - try!builder.putOffset(offset: o2) + try!builder.append(offset: o1) + try!builder.append(offset: o2) let v = builder.endVector() try!builder.openObject(numOfProperties: 1) @@ -951,8 +951,8 @@ class BuilderTest: XCTestCase { let o2 = try!builder.closeObject() try!builder.startVector(count: 2, elementSize: 1) - try!builder.putOffset(offset: o1) - try!builder.putOffset(offset: o2) + try!builder.append(offset: o1) + try!builder.append(offset: o2) let v = builder.endVector() try!builder.openObject(numOfProperties: 1) @@ -1036,15 +1036,15 @@ class BuilderTest: XCTestCase { func testObjectWithVectorToSelf(){ try!builder.startVector(count: 2, elementSize: 2) - let c1 = try!builder.putOffset(offset: nil) - let c2 = try!builder.putOffset(offset: nil) + let c1 = try!builder.append(offset: nil) + let c2 = try!builder.append(offset: nil) let v = builder.endVector() try!builder.openObject(numOfProperties: 1) try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) let o = try!builder.closeObject() - try!builder.replaceOffset(offset: o, atCursor: c1) - try!builder.replaceOffset(offset: o, atCursor: c2) + try!builder.update(offset: o, atCursor: c1) + try!builder.update(offset: o, atCursor: c2) try!builder.finish(offset: o, fileIdentifier: nil) @@ -1087,7 +1087,7 @@ class BuilderTest: XCTestCase { func testPutBadOffset(){ var itThrows = false do { - try builder.putOffset(offset: 12) + try builder.append(offset: 12) } catch { itThrows = true } @@ -1097,7 +1097,7 @@ class BuilderTest: XCTestCase { func testReplaceWithBadOffset(){ var itThrows = false do { - try builder.replaceOffset(offset: 12, atCursor: 0) + try builder.update(offset: 12, atCursor: 0) } catch { itThrows = true } @@ -1107,7 +1107,7 @@ class BuilderTest: XCTestCase { func testReplaceWithBadCursor(){ var itThrows = false do { - try builder.replaceOffset(offset: 0, atCursor: 12) + try builder.update(offset: 0, atCursor: 12) } catch { itThrows = true } diff --git a/FlatBuffersSwiftTests/FBReaderTest.swift b/FlatBuffersSwiftTests/FBReaderTest.swift index 97e1315..03b3922 100644 --- a/FlatBuffersSwiftTests/FBReaderTest.swift +++ b/FlatBuffersSwiftTests/FBReaderTest.swift @@ -270,14 +270,14 @@ class FlatBuffersReaderTest: XCTestCase { let sOffset3 = try! fbb.createString(value: "max3") try! fbb.startVector(count: 3, elementSize: 4) - try!fbb.putOffset(offset: sOffset1) - try!fbb.putOffset(offset: sOffset2) - try!fbb.putOffset(offset: sOffset3) + try!fbb.append(offset: sOffset1) + try!fbb.append(offset: sOffset2) + try!fbb.append(offset: sOffset3) let sVectorOffset = fbb.endVector() try! fbb.startVector(count: 2, elementSize: 1) - fbb.put(value: true) - fbb.put(value: false) + fbb.append(value: true) + fbb.append(value: false) let bVectorOffset = fbb.endVector() try! fbb.openObject(numOfProperties: 2) diff --git a/FlatBuffersSwiftTests/contacts_fb.swift b/FlatBuffersSwiftTests/contacts_fb.swift index de95d53..310f807 100644 --- a/FlatBuffersSwiftTests/contacts_fb.swift +++ b/FlatBuffersSwiftTests/contacts_fb.swift @@ -114,7 +114,7 @@ public extension ContactList { try builder.startVector(count: entries.count, elementSize: MemoryLayout.stride) index = entries.count - 1 while(index >= 0){ - try builder.putOffset(offset: offsets[index]) + try builder.append(offset: offsets[index]) index -= 1 } offset1 = builder.endVector() @@ -302,7 +302,7 @@ public extension Contact { var index = moods.count - 1 while(index >= 0){ if let value = moods[index]?.rawValue { - builder.put(value: value) + builder.append(value: value) } index -= 1 } @@ -314,7 +314,7 @@ public extension Contact { var index = previousLocations.count - 1 while(index >= 0){ if let value = previousLocations[index] { - builder.put(value: value) + builder.append(value: value) } index -= 1 } @@ -331,7 +331,7 @@ public extension Contact { try builder.startVector(count: addressEntries.count, elementSize: MemoryLayout.stride) index = addressEntries.count - 1 while(index >= 0){ - try builder.putOffset(offset: offsets[index]) + try builder.append(offset: offsets[index]) index -= 1 } offset4 = builder.endVector() @@ -347,7 +347,7 @@ public extension Contact { try builder.startVector(count: tags.count, elementSize: MemoryLayout.stride) index = tags.count - 1 while(index >= 0){ - try builder.putOffset(offset: offsets[index]) + try builder.append(offset: offsets[index]) index -= 1 } offset3 = builder.endVector() @@ -362,7 +362,7 @@ public extension Contact { try builder.addPropertyOffsetToOpenObject(propertyIndex: 6, offset: offset6) } if let currentLoccation = currentLoccation { - builder.put(value: currentLoccation) + builder.append(value: currentLoccation) try builder.addCurrentOffsetAsPropertyToOpenObject(propertyIndex: 5) } if addressEntries.count > 0 { diff --git a/FlatBuffersSwiftTests/friends_fb.swift b/FlatBuffersSwiftTests/friends_fb.swift index 975c317..9804922 100644 --- a/FlatBuffersSwiftTests/friends_fb.swift +++ b/FlatBuffersSwiftTests/friends_fb.swift @@ -118,7 +118,7 @@ public extension PeopleList { index = people.count - 1 var deferedBindingCursors : [Int : Int] = [:] while(index >= 0){ - let cursor = try builder.putOffset(offset: offsets[index]) + let cursor = try builder.append(offset: offsets[index]) if offsets[index] == 0 { deferedBindingCursors[index] = cursor } @@ -258,7 +258,7 @@ public extension Friend { index = friends.count - 1 var deferedBindingCursors : [Int : Int] = [:] while(index >= 0){ - let cursor = try builder.putOffset(offset: offsets[index]) + let cursor = try builder.append(offset: offsets[index]) if offsets[index] == 0 { deferedBindingCursors[index] = cursor } @@ -503,10 +503,10 @@ fileprivate func addToByteArray_Human(_ builder : FlatBuffersBuilder, union : Hu private func performLateBindings(_ builder : FlatBuffersBuilder) throws { for binding in builder.deferedBindings { switch binding.object { - case let object as PeopleList: try builder.replaceOffset(offset: object.addToByteArray(builder), atCursor: binding.cursor) - case let object as Friend: try builder.replaceOffset(offset: object.addToByteArray(builder), atCursor: binding.cursor) - case let object as Male: try builder.replaceOffset(offset: object.addToByteArray(builder), atCursor: binding.cursor) - case let object as Female: try builder.replaceOffset(offset: object.addToByteArray(builder), atCursor: binding.cursor) + case let object as PeopleList: try builder.update(offset: object.addToByteArray(builder), atCursor: binding.cursor) + case let object as Friend: try builder.update(offset: object.addToByteArray(builder), atCursor: binding.cursor) + case let object as Male: try builder.update(offset: object.addToByteArray(builder), atCursor: binding.cursor) + case let object as Female: try builder.update(offset: object.addToByteArray(builder), atCursor: binding.cursor) default: continue } } From de8fb62f1c93e93b6992e473e0dba85c0b803338 Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Wed, 11 Jan 2017 13:47:50 +0100 Subject: [PATCH 12/16] Initial cleanup of object updates. --- FlatBuffersSwift/FlatBuffersBuilder.swift | 43 +++++++++-- FlatBuffersSwiftTests/BuilderTest.swift | 88 +++++++++++------------ FlatBuffersSwiftTests/FBReaderTest.swift | 8 +-- FlatBuffersSwiftTests/contacts_fb.swift | 46 ++++++------ FlatBuffersSwiftTests/friends_fb.swift | 18 ++--- 5 files changed, 118 insertions(+), 85 deletions(-) diff --git a/FlatBuffersSwift/FlatBuffersBuilder.swift b/FlatBuffersSwift/FlatBuffersBuilder.swift index f60075e..77a563f 100644 --- a/FlatBuffersSwift/FlatBuffersBuilder.swift +++ b/FlatBuffersSwift/FlatBuffersBuilder.swift @@ -219,6 +219,12 @@ public final class FlatBuffersBuilder { _data.storeBytes(of: value, toByteOffset: index + leftCursor, as: T.self) } + /** + Start an object update sequence + + - parameters: + - numOfProperties: The number of properties we will update + */ public func openObject(numOfProperties : Int) throws { guard objectStart == -1 && vectorNumElems == -1 else { throw FlatBuffersBuildError.objectIsNotClosed @@ -231,8 +237,17 @@ public final class FlatBuffersBuilder { objectStart = Int32(cursor) } + /** + Append an offset into the buffer for the current object + + - parameters: + - propertyIndex: The index of the property to update + - offset: The offsetnumber of properties we will update + + - Returns: The current cursor position (Note: What is the use case of the return value?) + */ @discardableResult - public func addPropertyOffsetToOpenObject(propertyIndex : Int, offset : Offset) throws -> Int{ + public func appendToObject(propertyIndex : Int, offset : Offset) throws -> Int{ guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject } @@ -243,8 +258,17 @@ public final class FlatBuffersBuilder { currentVTable[propertyIndex] = Int32(cursor) return cursor } - - public func addPropertyToOpenObject(propertyIndex : Int, value : T, defaultValue : T) throws { + + /** + Append an scalar into the buffer for the current object + + - parameters: + - propertyIndex: The index of the property to update + - value: The value to append + - defaultValue: If configured to skip default values, a value + matching this default value will not be written to the buffer. + */ + public func appendToObject(propertyIndex : Int, value : T, defaultValue : T) throws { guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject } @@ -260,7 +284,13 @@ public final class FlatBuffersBuilder { currentVTable[propertyIndex] = Int32(cursor) } - public func addCurrentOffsetAsPropertyToOpenObject(propertyIndex : Int) throws { + /** + Append the current cursor position into the buffer for the current object + + - parameters: + - propertyIndex: The index of the property to update + */ + public func appenCurrentOffsetAsPropertyToObject(propertyIndex : Int) throws { guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject } @@ -269,7 +299,10 @@ public final class FlatBuffersBuilder { } currentVTable[propertyIndex] = Int32(cursor) } - + + /** + Close the current open object. + */ public func closeObject() throws -> Offset { guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject diff --git a/FlatBuffersSwiftTests/BuilderTest.swift b/FlatBuffersSwiftTests/BuilderTest.swift index 766bc5a..093ae30 100644 --- a/FlatBuffersSwiftTests/BuilderTest.swift +++ b/FlatBuffersSwiftTests/BuilderTest.swift @@ -352,8 +352,8 @@ class BuilderTest: XCTestCase { func testObjectWithNameAndAge() { let s = try!builder.createString(value: "maxim") try!builder.openObject(numOfProperties: 2) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: s) + try!builder.appendToObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 0, offset: s) let o = try!builder.closeObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -397,9 +397,9 @@ class BuilderTest: XCTestCase { let s = try!builder.createString(value: "maxim") let s2 = try!builder.createString(value: "maxim") try!builder.openObject(numOfProperties: 3) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: s) - try!builder.addPropertyOffsetToOpenObject(propertyIndex: 2, offset: s2) + try!builder.appendToObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 0, offset: s) + try!builder.appendToObject(propertyIndex: 2, offset: s2) let o = try!builder.closeObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -453,9 +453,9 @@ class BuilderTest: XCTestCase { let s = try!builder.createString(value: "maxim") let s2 = try!builder.createString(value: "maxim") try!builder.openObject(numOfProperties: 3) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: s) - try!builder.addPropertyOffsetToOpenObject(propertyIndex: 2, offset: s2) + try!builder.appendToObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 0, offset: s) + try!builder.appendToObject(propertyIndex: 2, offset: s2) let o = try!builder.closeObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -518,9 +518,9 @@ class BuilderTest: XCTestCase { func testObjectWithNameAndAgeAndReferenceToSelf() { let s = try!builder.createString(value: "maxim") try!builder.openObject(numOfProperties: 3) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: s) - let cursor = try!builder.addPropertyOffsetToOpenObject(propertyIndex: 2, offset: 0) + try!builder.appendToObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 0, offset: s) + let cursor = try!builder.appendToObject(propertyIndex: 2, offset: 0) let o = try!builder.closeObject() try!builder.update(offset: o, atCursor: cursor) try!builder.finish(offset: o, fileIdentifier: nil) @@ -572,9 +572,9 @@ class BuilderTest: XCTestCase { func testObjectWithStruct(){ try!builder.openObject(numOfProperties: 2) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) builder.append(value: S1(i1: 45, i2: 78)) - try! builder.addCurrentOffsetAsPropertyToOpenObject(propertyIndex: 0) + try! builder.appenCurrentOffsetAsPropertyToObject(propertyIndex: 0) let o = try!builder.closeObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -614,7 +614,7 @@ class BuilderTest: XCTestCase { builder.append(value: Int16(19)) let v = builder.endVector() try!builder.openObject(numOfProperties: 1) - try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) + try! builder.appendToObject(propertyIndex: 0, offset: v) let o = try!builder.closeObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -656,7 +656,7 @@ class BuilderTest: XCTestCase { builder.append(value: false) let v = builder.endVector() try!builder.openObject(numOfProperties: 1) - try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) + try! builder.appendToObject(propertyIndex: 0, offset: v) let o = try!builder.closeObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -697,7 +697,7 @@ class BuilderTest: XCTestCase { builder.append(value: S1(i1: 13, i2: 17)) let v = builder.endVector() try!builder.openObject(numOfProperties: 1) - try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) + try! builder.appendToObject(propertyIndex: 0, offset: v) let o = try!builder.closeObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -755,15 +755,15 @@ class BuilderTest: XCTestCase { func testObjectWithVectorToOtherTwoObjects(){ try!builder.openObject(numOfProperties: 3) - try!builder.addPropertyToOpenObject(propertyIndex: 0, value: Int8(12), defaultValue: 0) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: Int8(13), defaultValue: 0) - try!builder.addPropertyToOpenObject(propertyIndex: 2, value: Int8(14), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 0, value: Int8(12), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 1, value: Int8(13), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 2, value: Int8(14), defaultValue: 0) let o1 = try!builder.closeObject() try!builder.openObject(numOfProperties: 3) - try!builder.addPropertyToOpenObject(propertyIndex: 0, value: Int8(22), defaultValue: 0) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: Int8(23), defaultValue: 0) - try!builder.addPropertyToOpenObject(propertyIndex: 2, value: Int8(24), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 0, value: Int8(22), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 1, value: Int8(23), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 2, value: Int8(24), defaultValue: 0) let o2 = try!builder.closeObject() try!builder.startVector(count: 2, elementSize: 1) @@ -772,7 +772,7 @@ class BuilderTest: XCTestCase { let v = builder.endVector() try!builder.openObject(numOfProperties: 1) - try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) + try! builder.appendToObject(propertyIndex: 0, offset: v) let o = try!builder.closeObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -855,13 +855,13 @@ class BuilderTest: XCTestCase { func testObjectWithVectorToOtherTwoObjectsAndVTableReuese(){ try!builder.openObject(numOfProperties: 2) - try!builder.addPropertyToOpenObject(propertyIndex: 0, value: Int16(12), defaultValue: 0) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: Int16(13), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 0, value: Int16(12), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 1, value: Int16(13), defaultValue: 0) let o1 = try!builder.closeObject() try!builder.openObject(numOfProperties: 2) - try!builder.addPropertyToOpenObject(propertyIndex: 0, value: Int16(22), defaultValue: 0) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: Int16(23), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 0, value: Int16(22), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 1, value: Int16(23), defaultValue: 0) let o2 = try!builder.closeObject() try!builder.startVector(count: 2, elementSize: 1) @@ -870,7 +870,7 @@ class BuilderTest: XCTestCase { let v = builder.endVector() try!builder.openObject(numOfProperties: 1) - try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) + try! builder.appendToObject(propertyIndex: 0, offset: v) let o = try!builder.closeObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -941,13 +941,13 @@ class BuilderTest: XCTestCase { func testObjectWithVectorToOtherTwoObjectsAndVTableWithoutReuese(){ builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: false, forceDefaults: false, nullTerminatedUTF8: false)) try!builder.openObject(numOfProperties: 2) - try!builder.addPropertyToOpenObject(propertyIndex: 0, value: Int16(12), defaultValue: 0) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: Int16(13), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 0, value: Int16(12), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 1, value: Int16(13), defaultValue: 0) let o1 = try!builder.closeObject() try!builder.openObject(numOfProperties: 2) - try!builder.addPropertyToOpenObject(propertyIndex: 0, value: Int16(22), defaultValue: 0) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: Int16(23), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 0, value: Int16(22), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 1, value: Int16(23), defaultValue: 0) let o2 = try!builder.closeObject() try!builder.startVector(count: 2, elementSize: 1) @@ -956,7 +956,7 @@ class BuilderTest: XCTestCase { let v = builder.endVector() try!builder.openObject(numOfProperties: 1) - try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) + try! builder.appendToObject(propertyIndex: 0, offset: v) let o = try!builder.closeObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -1040,7 +1040,7 @@ class BuilderTest: XCTestCase { let c2 = try!builder.append(offset: nil) let v = builder.endVector() try!builder.openObject(numOfProperties: 1) - try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) + try! builder.appendToObject(propertyIndex: 0, offset: v) let o = try!builder.closeObject() try!builder.update(offset: o, atCursor: c1) @@ -1128,7 +1128,7 @@ class BuilderTest: XCTestCase { func testAddPropertyWithoutOpenObject(){ var itThrows = false do { - try builder.addPropertyToOpenObject(propertyIndex: 0, value: 1, defaultValue: 0) + try builder.appendToObject(propertyIndex: 0, value: 1, defaultValue: 0) } catch { itThrows = true } @@ -1139,7 +1139,7 @@ class BuilderTest: XCTestCase { var itThrows = false try!builder.openObject(numOfProperties: 1) do { - try builder.addPropertyToOpenObject(propertyIndex: 1, value: 1, defaultValue: 0) + try builder.appendToObject(propertyIndex: 1, value: 1, defaultValue: 0) } catch { itThrows = true } @@ -1149,7 +1149,7 @@ class BuilderTest: XCTestCase { func testAddOffsetWithoutOpenObject(){ var itThrows = false do { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: 0) + try builder.appendToObject(propertyIndex: 0, offset: 0) } catch { itThrows = true } @@ -1160,7 +1160,7 @@ class BuilderTest: XCTestCase { var itThrows = false try!builder.openObject(numOfProperties: 1) do { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 1, offset: 0) + try builder.appendToObject(propertyIndex: 1, offset: 0) } catch { itThrows = true } @@ -1170,7 +1170,7 @@ class BuilderTest: XCTestCase { func testAddCurrentOffsetWithoutOpenObject(){ var itThrows = false do { - try builder.addCurrentOffsetAsPropertyToOpenObject(propertyIndex: 0) + try builder.appenCurrentOffsetAsPropertyToObject(propertyIndex: 0) } catch { itThrows = true } @@ -1181,7 +1181,7 @@ class BuilderTest: XCTestCase { var itThrows = false try!builder.openObject(numOfProperties: 1) do { - try builder.addCurrentOffsetAsPropertyToOpenObject(propertyIndex: 1) + try builder.appenCurrentOffsetAsPropertyToObject(propertyIndex: 1) } catch { itThrows = true } @@ -1269,8 +1269,8 @@ class BuilderTest: XCTestCase { func testDefaultValuesNotSet(){ try!builder.openObject(numOfProperties: 2) - try!builder.addPropertyToOpenObject(propertyIndex: 0, value: Int32(12), defaultValue: 0) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: Int32(5), defaultValue: 5) + try!builder.appendToObject(propertyIndex: 0, value: Int32(12), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 1, value: Int32(5), defaultValue: 5) _ = try!builder.closeObject() assertByte(with: 8) @@ -1294,8 +1294,8 @@ class BuilderTest: XCTestCase { func testDefaultValuesForced(){ builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: true, forceDefaults: true, nullTerminatedUTF8: false)) try!builder.openObject(numOfProperties: 2) - try!builder.addPropertyToOpenObject(propertyIndex: 0, value: Int32(12), defaultValue: 0) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: Int32(5), defaultValue: 5) + try!builder.appendToObject(propertyIndex: 0, value: Int32(12), defaultValue: 0) + try!builder.appendToObject(propertyIndex: 1, value: Int32(5), defaultValue: 5) _ = try!builder.closeObject() assertByte(with: 8) diff --git a/FlatBuffersSwiftTests/FBReaderTest.swift b/FlatBuffersSwiftTests/FBReaderTest.swift index 03b3922..04ffc69 100644 --- a/FlatBuffersSwiftTests/FBReaderTest.swift +++ b/FlatBuffersSwiftTests/FBReaderTest.swift @@ -247,8 +247,8 @@ class FlatBuffersReaderTest: XCTestCase { ) let sOffset = try! fbb.createString(value: "max") try! fbb.openObject(numOfProperties: 3) - try! fbb.addPropertyToOpenObject(propertyIndex: 0, value: true, defaultValue: false) - try! fbb.addPropertyOffsetToOpenObject(propertyIndex: 1, offset: sOffset) + try! fbb.appendToObject(propertyIndex: 0, value: true, defaultValue: false) + try! fbb.appendToObject(propertyIndex: 1, offset: sOffset) let oOffset = try! fbb.closeObject() try! fbb.finish(offset: oOffset, fileIdentifier: nil) let data = fbb.makeData @@ -281,8 +281,8 @@ class FlatBuffersReaderTest: XCTestCase { let bVectorOffset = fbb.endVector() try! fbb.openObject(numOfProperties: 2) - try! fbb.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: sVectorOffset) - try! fbb.addPropertyOffsetToOpenObject(propertyIndex: 1, offset: bVectorOffset) + try! fbb.appendToObject(propertyIndex: 0, offset: sVectorOffset) + try! fbb.appendToObject(propertyIndex: 1, offset: bVectorOffset) let oOffset = try! fbb.closeObject() try! fbb.finish(offset: oOffset, fileIdentifier: nil) let data = fbb.makeData diff --git a/FlatBuffersSwiftTests/contacts_fb.swift b/FlatBuffersSwiftTests/contacts_fb.swift index 310f807..97936d1 100644 --- a/FlatBuffersSwiftTests/contacts_fb.swift +++ b/FlatBuffersSwiftTests/contacts_fb.swift @@ -121,9 +121,9 @@ public extension ContactList { } try builder.openObject(numOfProperties: 2) if entries.count > 0 { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 1, offset: offset1) + try builder.appendToObject(propertyIndex: 1, offset: offset1) } - try builder.addPropertyToOpenObject(propertyIndex: 0, value : lastModified, defaultValue : 0) + try builder.appendToObject(propertyIndex: 0, value : lastModified, defaultValue : 0) let myOffset = try builder.closeObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -356,26 +356,26 @@ public extension Contact { let offset0 = try builder.createString(value: name) try builder.openObject(numOfProperties: 8) if moods.count > 0 { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 7, offset: offset7) + try builder.appendToObject(propertyIndex: 7, offset: offset7) } if previousLocations.count > 0 { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 6, offset: offset6) + try builder.appendToObject(propertyIndex: 6, offset: offset6) } if let currentLoccation = currentLoccation { builder.append(value: currentLoccation) - try builder.addCurrentOffsetAsPropertyToOpenObject(propertyIndex: 5) + try builder.appenCurrentOffsetAsPropertyToObject(propertyIndex: 5) } if addressEntries.count > 0 { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 4, offset: offset4) + try builder.appendToObject(propertyIndex: 4, offset: offset4) } if tags.count > 0 { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 3, offset: offset3) + try builder.appendToObject(propertyIndex: 3, offset: offset3) } - try builder.addPropertyToOpenObject(propertyIndex: 2, value : gender?.rawValue ?? 0, defaultValue : 0) + try builder.appendToObject(propertyIndex: 2, value : gender?.rawValue ?? 0, defaultValue : 0) if birthday != nil { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 1, offset: offset1) + try builder.appendToObject(propertyIndex: 1, offset: offset1) } - try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) + try builder.appendToObject(propertyIndex: 0, offset: offset0) let myOffset = try builder.closeObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -442,9 +442,9 @@ public extension Date { } } try builder.openObject(numOfProperties: 3) - try builder.addPropertyToOpenObject(propertyIndex: 2, value : year, defaultValue : 0) - try builder.addPropertyToOpenObject(propertyIndex: 1, value : month, defaultValue : 0) - try builder.addPropertyToOpenObject(propertyIndex: 0, value : day, defaultValue : 0) + try builder.appendToObject(propertyIndex: 2, value : year, defaultValue : 0) + try builder.appendToObject(propertyIndex: 1, value : month, defaultValue : 0) + try builder.appendToObject(propertyIndex: 0, value : day, defaultValue : 0) let myOffset = try builder.closeObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -515,10 +515,10 @@ public extension AddressEntry { let offset1 = try addToByteArray_Address(builder, union: address) try builder.openObject(numOfProperties: 3) if let object = address { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 2, offset: offset1) - try builder.addPropertyToOpenObject(propertyIndex: 1, value : unionCase_Address(object), defaultValue : 0) + try builder.appendToObject(propertyIndex: 2, offset: offset1) + try builder.appendToObject(propertyIndex: 1, value : unionCase_Address(object), defaultValue : 0) } - try builder.addPropertyToOpenObject(propertyIndex: 0, value : order, defaultValue : 0) + try builder.appendToObject(propertyIndex: 0, value : order, defaultValue : 0) let myOffset = try builder.closeObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -588,10 +588,10 @@ public extension PostalAddress { let offset1 = try builder.createString(value: city) let offset0 = try builder.createString(value: country) try builder.openObject(numOfProperties: 4) - try builder.addPropertyOffsetToOpenObject(propertyIndex: 3, offset: offset3) - try builder.addPropertyToOpenObject(propertyIndex: 2, value : postalCode, defaultValue : 0) - try builder.addPropertyOffsetToOpenObject(propertyIndex: 1, offset: offset1) - try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) + try builder.appendToObject(propertyIndex: 3, offset: offset3) + try builder.appendToObject(propertyIndex: 2, value : postalCode, defaultValue : 0) + try builder.appendToObject(propertyIndex: 1, offset: offset1) + try builder.appendToObject(propertyIndex: 0, offset: offset0) let myOffset = try builder.closeObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -645,7 +645,7 @@ public extension EmailAddress { } let offset0 = try builder.createString(value: mailto) try builder.openObject(numOfProperties: 1) - try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) + try builder.appendToObject(propertyIndex: 0, offset: offset0) let myOffset = try builder.closeObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -699,7 +699,7 @@ public extension WebAddress { } let offset0 = try builder.createString(value: url) try builder.openObject(numOfProperties: 1) - try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) + try builder.appendToObject(propertyIndex: 0, offset: offset0) let myOffset = try builder.closeObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -753,7 +753,7 @@ public extension TelephoneNumber { } let offset0 = try builder.createString(value: number) try builder.openObject(numOfProperties: 1) - try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) + try builder.appendToObject(propertyIndex: 0, offset: offset0) let myOffset = try builder.closeObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset diff --git a/FlatBuffersSwiftTests/friends_fb.swift b/FlatBuffersSwiftTests/friends_fb.swift index 9804922..b21baf0 100644 --- a/FlatBuffersSwiftTests/friends_fb.swift +++ b/FlatBuffersSwiftTests/friends_fb.swift @@ -134,7 +134,7 @@ public extension PeopleList { } try builder.openObject(numOfProperties: 1) if people.count > 0 { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) + try builder.appendToObject(propertyIndex: 0, offset: offset0) } let myOffset = try builder.closeObject() if builder.config.uniqueTables { @@ -275,14 +275,14 @@ public extension Friend { let offset0 = try builder.createString(value: name) try builder.openObject(numOfProperties: 6) if let object = lover { - let cursor4 = try builder.addPropertyOffsetToOpenObject(propertyIndex: 5, offset: offset4) + let cursor4 = try builder.appendToObject(propertyIndex: 5, offset: offset4) if offset4 == 0 { builder.deferedBindings.append((object: object, cursor: cursor4)) } - try builder.addPropertyToOpenObject(propertyIndex: 4, value : unionCase_Human(object), defaultValue : 0) + try builder.appendToObject(propertyIndex: 4, value : unionCase_Human(object), defaultValue : 0) } if mother != nil { - let cursor3 = try builder.addPropertyOffsetToOpenObject(propertyIndex: 3, offset: offset3) + let cursor3 = try builder.appendToObject(propertyIndex: 3, offset: offset3) if offset3 == 0 { if let object = mother { builder.deferedBindings.append((object: object, cursor: cursor3)) @@ -290,7 +290,7 @@ public extension Friend { } } if father != nil { - let cursor2 = try builder.addPropertyOffsetToOpenObject(propertyIndex: 2, offset: offset2) + let cursor2 = try builder.appendToObject(propertyIndex: 2, offset: offset2) if offset2 == 0 { if let object = father { builder.deferedBindings.append((object: object, cursor: cursor2)) @@ -298,9 +298,9 @@ public extension Friend { } } if friends.count > 0 { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 1, offset: offset1) + try builder.appendToObject(propertyIndex: 1, offset: offset1) } - try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) + try builder.appendToObject(propertyIndex: 0, offset: offset0) let myOffset = try builder.closeObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -365,7 +365,7 @@ public extension Male { let offset0 = try ref?.addToByteArray(builder) ?? 0 try builder.openObject(numOfProperties: 1) if ref != nil { - let cursor0 = try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) + let cursor0 = try builder.appendToObject(propertyIndex: 0, offset: offset0) if offset0 == 0 { if let object = ref { builder.deferedBindings.append((object: object, cursor: cursor0)) @@ -436,7 +436,7 @@ public extension Female { let offset0 = try ref?.addToByteArray(builder) ?? 0 try builder.openObject(numOfProperties: 1) if ref != nil { - let cursor0 = try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) + let cursor0 = try builder.appendToObject(propertyIndex: 0, offset: offset0) if offset0 == 0 { if let object = ref { builder.deferedBindings.append((object: object, cursor: cursor0)) From 05ec6149f085e9224e8245e1892b7c2cec38e4ae Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Wed, 11 Jan 2017 14:03:05 +0100 Subject: [PATCH 13/16] Consistent naming between objects and vectors, moving to clean "append" interface throughout. Added additional documentation. --- FlatBuffersSwift/FlatBuffersBuilder.swift | 37 +++- FlatBuffersSwiftTests/BuilderTest.swift | 212 +++++++++++----------- FlatBuffersSwiftTests/FBReaderTest.swift | 24 +-- FlatBuffersSwiftTests/contacts_fb.swift | 94 +++++----- FlatBuffersSwiftTests/friends_fb.swift | 36 ++-- 5 files changed, 211 insertions(+), 192 deletions(-) diff --git a/FlatBuffersSwift/FlatBuffersBuilder.swift b/FlatBuffersSwift/FlatBuffersBuilder.swift index 77a563f..20d7d74 100644 --- a/FlatBuffersSwift/FlatBuffersBuilder.swift +++ b/FlatBuffersSwift/FlatBuffersBuilder.swift @@ -225,7 +225,7 @@ public final class FlatBuffersBuilder { - parameters: - numOfProperties: The number of properties we will update */ - public func openObject(numOfProperties : Int) throws { + public func startObject(numOfProperties : Int) throws { guard objectStart == -1 && vectorNumElems == -1 else { throw FlatBuffersBuildError.objectIsNotClosed } @@ -238,7 +238,7 @@ public final class FlatBuffersBuilder { } /** - Append an offset into the buffer for the current object + Append an offset into the buffer for the currently open object - parameters: - propertyIndex: The index of the property to update @@ -247,7 +247,7 @@ public final class FlatBuffersBuilder { - Returns: The current cursor position (Note: What is the use case of the return value?) */ @discardableResult - public func appendToObject(propertyIndex : Int, offset : Offset) throws -> Int{ + public func append(propertyIndex : Int, offset : Offset) throws -> Int{ guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject } @@ -260,7 +260,7 @@ public final class FlatBuffersBuilder { } /** - Append an scalar into the buffer for the current object + Append an scalar into the buffer for the currently open object - parameters: - propertyIndex: The index of the property to update @@ -268,7 +268,7 @@ public final class FlatBuffersBuilder { - defaultValue: If configured to skip default values, a value matching this default value will not be written to the buffer. */ - public func appendToObject(propertyIndex : Int, value : T, defaultValue : T) throws { + public func append(propertyIndex : Int, value : T, defaultValue : T) throws { guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject } @@ -285,12 +285,12 @@ public final class FlatBuffersBuilder { } /** - Append the current cursor position into the buffer for the current object + Append the current cursor position into the buffer for the currently open object - parameters: - propertyIndex: The index of the property to update */ - public func appenCurrentOffsetAsPropertyToObject(propertyIndex : Int) throws { + public func appendCurrentOffsetAsProperty(propertyIndex : Int) throws { guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject } @@ -303,7 +303,7 @@ public final class FlatBuffersBuilder { /** Close the current open object. */ - public func closeObject() throws -> Offset { + public func endObject() throws -> Offset { guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject } @@ -366,6 +366,13 @@ public final class FlatBuffersBuilder { return Offset(vtableloc) } + /** + Start a vector update operation + + - parameters: + - count: The number of elements in the vector + - elementSize: The size of the vector elements + */ public func startVector(count : Int, elementSize : Int) throws{ align(size: 4, additionalBytes: count * elementSize) guard objectStart == -1 && vectorNumElems == -1 else { @@ -374,6 +381,9 @@ public final class FlatBuffersBuilder { vectorNumElems = Int32(count) } + /** + Finish vector update operation + */ public func endVector() -> Offset { append(value: vectorNumElems) vectorNumElems = -1 @@ -381,7 +391,16 @@ public final class FlatBuffersBuilder { } private var stringCache : [String:Offset] = [:] - public func createString(value : String?) throws -> Offset { + + /** + Append a string to the buffer + + - parameters: + - value: The string to add to the buffer + + - Returns: The current cursor position (Note: What is the use case of the return value?) + */ + public func append(value : String?) throws -> Offset { guard objectStart == -1 && vectorNumElems == -1 else { throw FlatBuffersBuildError.objectIsNotClosed } diff --git a/FlatBuffersSwiftTests/BuilderTest.swift b/FlatBuffersSwiftTests/BuilderTest.swift index 093ae30..26712b2 100644 --- a/FlatBuffersSwiftTests/BuilderTest.swift +++ b/FlatBuffersSwiftTests/BuilderTest.swift @@ -219,7 +219,7 @@ class BuilderTest: XCTestCase { } func testString(){ - let o = try!builder.createString(value: "maxim") + let o = try!builder.append(value: "maxim") XCTAssertEqual(o, 12) @@ -239,7 +239,7 @@ class BuilderTest: XCTestCase { builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: true, forceDefaults: false, nullTerminatedUTF8: true)) - let o = try!builder.createString(value: "maxim") + let o = try!builder.append(value: "maxim") XCTAssertEqual(o, 12) @@ -258,7 +258,7 @@ class BuilderTest: XCTestCase { func testStringWithPrimitives(){ builder.append(value: Int16(25)) - let o = try!builder.createString(value: "maxim") + let o = try!builder.append(value: "maxim") XCTAssertEqual(o, 12) @@ -281,7 +281,7 @@ class BuilderTest: XCTestCase { func testStringWithPrimitivesAndFinish(){ builder.append(value: Int16(25)) - let o = try!builder.createString(value: "maxim") + let o = try!builder.append(value: "maxim") builder.append(value: true) try!builder.finish(offset: o, fileIdentifier: nil) @@ -314,7 +314,7 @@ class BuilderTest: XCTestCase { func testStringWithPrimitivesAndFinishWithFileIdentifier(){ builder.append(value: Int16(25)) - let o = try!builder.createString(value: "maxim") + let o = try!builder.append(value: "maxim") builder.append(value: true) try!builder.finish(offset: o, fileIdentifier: "test") @@ -350,11 +350,11 @@ class BuilderTest: XCTestCase { } func testObjectWithNameAndAge() { - let s = try!builder.createString(value: "maxim") - try!builder.openObject(numOfProperties: 2) - try!builder.appendToObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 0, offset: s) - let o = try!builder.closeObject() + let s = try!builder.append(value: "maxim") + try!builder.startObject(numOfProperties: 2) + try!builder.append(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.append(propertyIndex: 0, offset: s) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 12) @@ -394,13 +394,13 @@ class BuilderTest: XCTestCase { } func testObjectWithNameReuseAndAge() { - let s = try!builder.createString(value: "maxim") - let s2 = try!builder.createString(value: "maxim") - try!builder.openObject(numOfProperties: 3) - try!builder.appendToObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 0, offset: s) - try!builder.appendToObject(propertyIndex: 2, offset: s2) - let o = try!builder.closeObject() + let s = try!builder.append(value: "maxim") + let s2 = try!builder.append(value: "maxim") + try!builder.startObject(numOfProperties: 3) + try!builder.append(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.append(propertyIndex: 0, offset: s) + try!builder.append(propertyIndex: 2, offset: s2) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 16) @@ -450,13 +450,13 @@ class BuilderTest: XCTestCase { func testObjectWithoutNameReuseAndAge() { builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: false, uniqueTables: true, uniqueVTables: true, forceDefaults: false, nullTerminatedUTF8: false)) - let s = try!builder.createString(value: "maxim") - let s2 = try!builder.createString(value: "maxim") - try!builder.openObject(numOfProperties: 3) - try!builder.appendToObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 0, offset: s) - try!builder.appendToObject(propertyIndex: 2, offset: s2) - let o = try!builder.closeObject() + let s = try!builder.append(value: "maxim") + let s2 = try!builder.append(value: "maxim") + try!builder.startObject(numOfProperties: 3) + try!builder.append(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.append(propertyIndex: 0, offset: s) + try!builder.append(propertyIndex: 2, offset: s2) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 16) @@ -516,12 +516,12 @@ class BuilderTest: XCTestCase { } func testObjectWithNameAndAgeAndReferenceToSelf() { - let s = try!builder.createString(value: "maxim") - try!builder.openObject(numOfProperties: 3) - try!builder.appendToObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 0, offset: s) - let cursor = try!builder.appendToObject(propertyIndex: 2, offset: 0) - let o = try!builder.closeObject() + let s = try!builder.append(value: "maxim") + try!builder.startObject(numOfProperties: 3) + try!builder.append(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.append(propertyIndex: 0, offset: s) + let cursor = try!builder.append(propertyIndex: 2, offset: 0) + let o = try!builder.endObject() try!builder.update(offset: o, atCursor: cursor) try!builder.finish(offset: o, fileIdentifier: nil) @@ -571,11 +571,11 @@ class BuilderTest: XCTestCase { } func testObjectWithStruct(){ - try!builder.openObject(numOfProperties: 2) - try!builder.appendToObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.startObject(numOfProperties: 2) + try!builder.append(propertyIndex: 1, value: UInt8(35), defaultValue: 0) builder.append(value: S1(i1: 45, i2: 78)) - try! builder.appenCurrentOffsetAsPropertyToObject(propertyIndex: 0) - let o = try!builder.closeObject() + try! builder.appendCurrentOffsetAsProperty(propertyIndex: 0) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 12) @@ -613,9 +613,9 @@ class BuilderTest: XCTestCase { builder.append(value: Int16(15)) builder.append(value: Int16(19)) let v = builder.endVector() - try!builder.openObject(numOfProperties: 1) - try! builder.appendToObject(propertyIndex: 0, offset: v) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 1) + try! builder.append(propertyIndex: 0, offset: v) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 12) @@ -655,9 +655,9 @@ class BuilderTest: XCTestCase { builder.append(value: true) builder.append(value: false) let v = builder.endVector() - try!builder.openObject(numOfProperties: 1) - try! builder.appendToObject(propertyIndex: 0, offset: v) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 1) + try! builder.append(propertyIndex: 0, offset: v) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 12) @@ -696,9 +696,9 @@ class BuilderTest: XCTestCase { builder.append(value: S1(i1: 12, i2: 19)) builder.append(value: S1(i1: 13, i2: 17)) let v = builder.endVector() - try!builder.openObject(numOfProperties: 1) - try! builder.appendToObject(propertyIndex: 0, offset: v) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 1) + try! builder.append(propertyIndex: 0, offset: v) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 12) @@ -754,26 +754,26 @@ class BuilderTest: XCTestCase { } func testObjectWithVectorToOtherTwoObjects(){ - try!builder.openObject(numOfProperties: 3) - try!builder.appendToObject(propertyIndex: 0, value: Int8(12), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 1, value: Int8(13), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 2, value: Int8(14), defaultValue: 0) - let o1 = try!builder.closeObject() - - try!builder.openObject(numOfProperties: 3) - try!builder.appendToObject(propertyIndex: 0, value: Int8(22), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 1, value: Int8(23), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 2, value: Int8(24), defaultValue: 0) - let o2 = try!builder.closeObject() + try!builder.startObject(numOfProperties: 3) + try!builder.append(propertyIndex: 0, value: Int8(12), defaultValue: 0) + try!builder.append(propertyIndex: 1, value: Int8(13), defaultValue: 0) + try!builder.append(propertyIndex: 2, value: Int8(14), defaultValue: 0) + let o1 = try!builder.endObject() + + try!builder.startObject(numOfProperties: 3) + try!builder.append(propertyIndex: 0, value: Int8(22), defaultValue: 0) + try!builder.append(propertyIndex: 1, value: Int8(23), defaultValue: 0) + try!builder.append(propertyIndex: 2, value: Int8(24), defaultValue: 0) + let o2 = try!builder.endObject() try!builder.startVector(count: 2, elementSize: 1) try!builder.append(offset: o1) try!builder.append(offset: o2) let v = builder.endVector() - try!builder.openObject(numOfProperties: 1) - try! builder.appendToObject(propertyIndex: 0, offset: v) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 1) + try! builder.append(propertyIndex: 0, offset: v) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 12) @@ -854,24 +854,24 @@ class BuilderTest: XCTestCase { } func testObjectWithVectorToOtherTwoObjectsAndVTableReuese(){ - try!builder.openObject(numOfProperties: 2) - try!builder.appendToObject(propertyIndex: 0, value: Int16(12), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 1, value: Int16(13), defaultValue: 0) - let o1 = try!builder.closeObject() + try!builder.startObject(numOfProperties: 2) + try!builder.append(propertyIndex: 0, value: Int16(12), defaultValue: 0) + try!builder.append(propertyIndex: 1, value: Int16(13), defaultValue: 0) + let o1 = try!builder.endObject() - try!builder.openObject(numOfProperties: 2) - try!builder.appendToObject(propertyIndex: 0, value: Int16(22), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 1, value: Int16(23), defaultValue: 0) - let o2 = try!builder.closeObject() + try!builder.startObject(numOfProperties: 2) + try!builder.append(propertyIndex: 0, value: Int16(22), defaultValue: 0) + try!builder.append(propertyIndex: 1, value: Int16(23), defaultValue: 0) + let o2 = try!builder.endObject() try!builder.startVector(count: 2, elementSize: 1) try!builder.append(offset: o1) try!builder.append(offset: o2) let v = builder.endVector() - try!builder.openObject(numOfProperties: 1) - try! builder.appendToObject(propertyIndex: 0, offset: v) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 1) + try! builder.append(propertyIndex: 0, offset: v) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 12) @@ -940,24 +940,24 @@ class BuilderTest: XCTestCase { func testObjectWithVectorToOtherTwoObjectsAndVTableWithoutReuese(){ builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: false, forceDefaults: false, nullTerminatedUTF8: false)) - try!builder.openObject(numOfProperties: 2) - try!builder.appendToObject(propertyIndex: 0, value: Int16(12), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 1, value: Int16(13), defaultValue: 0) - let o1 = try!builder.closeObject() + try!builder.startObject(numOfProperties: 2) + try!builder.append(propertyIndex: 0, value: Int16(12), defaultValue: 0) + try!builder.append(propertyIndex: 1, value: Int16(13), defaultValue: 0) + let o1 = try!builder.endObject() - try!builder.openObject(numOfProperties: 2) - try!builder.appendToObject(propertyIndex: 0, value: Int16(22), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 1, value: Int16(23), defaultValue: 0) - let o2 = try!builder.closeObject() + try!builder.startObject(numOfProperties: 2) + try!builder.append(propertyIndex: 0, value: Int16(22), defaultValue: 0) + try!builder.append(propertyIndex: 1, value: Int16(23), defaultValue: 0) + let o2 = try!builder.endObject() try!builder.startVector(count: 2, elementSize: 1) try!builder.append(offset: o1) try!builder.append(offset: o2) let v = builder.endVector() - try!builder.openObject(numOfProperties: 1) - try! builder.appendToObject(propertyIndex: 0, offset: v) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 1) + try! builder.append(propertyIndex: 0, offset: v) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 12) @@ -1039,9 +1039,9 @@ class BuilderTest: XCTestCase { let c1 = try!builder.append(offset: nil) let c2 = try!builder.append(offset: nil) let v = builder.endVector() - try!builder.openObject(numOfProperties: 1) - try! builder.appendToObject(propertyIndex: 0, offset: v) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 1) + try! builder.append(propertyIndex: 0, offset: v) + let o = try!builder.endObject() try!builder.update(offset: o, atCursor: c1) try!builder.update(offset: o, atCursor: c2) @@ -1116,9 +1116,9 @@ class BuilderTest: XCTestCase { func testOpenObjectWhileStillOpen(){ var itThrows = false - try!builder.openObject(numOfProperties: 1) + try!builder.startObject(numOfProperties: 1) do { - try builder.openObject(numOfProperties: 1) + try builder.startObject(numOfProperties: 1) } catch { itThrows = true } @@ -1128,7 +1128,7 @@ class BuilderTest: XCTestCase { func testAddPropertyWithoutOpenObject(){ var itThrows = false do { - try builder.appendToObject(propertyIndex: 0, value: 1, defaultValue: 0) + try builder.append(propertyIndex: 0, value: 1, defaultValue: 0) } catch { itThrows = true } @@ -1137,9 +1137,9 @@ class BuilderTest: XCTestCase { func testAddPropertyWithoutBadIndex(){ var itThrows = false - try!builder.openObject(numOfProperties: 1) + try!builder.startObject(numOfProperties: 1) do { - try builder.appendToObject(propertyIndex: 1, value: 1, defaultValue: 0) + try builder.append(propertyIndex: 1, value: 1, defaultValue: 0) } catch { itThrows = true } @@ -1149,7 +1149,7 @@ class BuilderTest: XCTestCase { func testAddOffsetWithoutOpenObject(){ var itThrows = false do { - try builder.appendToObject(propertyIndex: 0, offset: 0) + try builder.append(propertyIndex: 0, offset: 0) } catch { itThrows = true } @@ -1158,9 +1158,9 @@ class BuilderTest: XCTestCase { func testAddOffsetWithoutBadIndex(){ var itThrows = false - try!builder.openObject(numOfProperties: 1) + try!builder.startObject(numOfProperties: 1) do { - try builder.appendToObject(propertyIndex: 1, offset: 0) + try builder.append(propertyIndex: 1, offset: 0) } catch { itThrows = true } @@ -1170,7 +1170,7 @@ class BuilderTest: XCTestCase { func testAddCurrentOffsetWithoutOpenObject(){ var itThrows = false do { - try builder.appenCurrentOffsetAsPropertyToObject(propertyIndex: 0) + try builder.appendCurrentOffsetAsProperty(propertyIndex: 0) } catch { itThrows = true } @@ -1179,9 +1179,9 @@ class BuilderTest: XCTestCase { func testAddCurrentOffsetWithoutBadIndex(){ var itThrows = false - try!builder.openObject(numOfProperties: 1) + try!builder.startObject(numOfProperties: 1) do { - try builder.appenCurrentOffsetAsPropertyToObject(propertyIndex: 1) + try builder.appendCurrentOffsetAsProperty(propertyIndex: 1) } catch { itThrows = true } @@ -1191,7 +1191,7 @@ class BuilderTest: XCTestCase { func testCloseObjectWithoutOpen(){ var itThrows = false do { - _ = try builder.closeObject() + _ = try builder.endObject() } catch { itThrows = true } @@ -1242,7 +1242,7 @@ class BuilderTest: XCTestCase { func testFinishWhileObjectIsOpen(){ var itThrows = false - try!builder.openObject(numOfProperties: 1) + try!builder.startObject(numOfProperties: 1) do { try builder.finish(offset: 0, fileIdentifier: nil) } catch { @@ -1253,9 +1253,9 @@ class BuilderTest: XCTestCase { func testCreateStringWhileObjectIsOpen(){ var itThrows = false - try!builder.openObject(numOfProperties: 1) + try!builder.startObject(numOfProperties: 1) do { - _ = try builder.createString(value: "Mo") + _ = try builder.append(value: "Mo") } catch { itThrows = true } @@ -1263,15 +1263,15 @@ class BuilderTest: XCTestCase { } func testCreateNilString(){ - let c = try!builder.createString(value: nil) + let c = try!builder.append(value: nil) XCTAssertEqual(c, 0) } func testDefaultValuesNotSet(){ - try!builder.openObject(numOfProperties: 2) - try!builder.appendToObject(propertyIndex: 0, value: Int32(12), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 1, value: Int32(5), defaultValue: 5) - _ = try!builder.closeObject() + try!builder.startObject(numOfProperties: 2) + try!builder.append(propertyIndex: 0, value: Int32(12), defaultValue: 0) + try!builder.append(propertyIndex: 1, value: Int32(5), defaultValue: 5) + _ = try!builder.endObject() assertByte(with: 8) assertByte(with: 0) @@ -1293,10 +1293,10 @@ class BuilderTest: XCTestCase { func testDefaultValuesForced(){ builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: true, forceDefaults: true, nullTerminatedUTF8: false)) - try!builder.openObject(numOfProperties: 2) - try!builder.appendToObject(propertyIndex: 0, value: Int32(12), defaultValue: 0) - try!builder.appendToObject(propertyIndex: 1, value: Int32(5), defaultValue: 5) - _ = try!builder.closeObject() + try!builder.startObject(numOfProperties: 2) + try!builder.append(propertyIndex: 0, value: Int32(12), defaultValue: 0) + try!builder.append(propertyIndex: 1, value: Int32(5), defaultValue: 5) + _ = try!builder.endObject() assertByte(with: 8) assertByte(with: 0) diff --git a/FlatBuffersSwiftTests/FBReaderTest.swift b/FlatBuffersSwiftTests/FBReaderTest.swift index 04ffc69..3a72360 100644 --- a/FlatBuffersSwiftTests/FBReaderTest.swift +++ b/FlatBuffersSwiftTests/FBReaderTest.swift @@ -245,11 +245,11 @@ class FlatBuffersReaderTest: XCTestCase { forceDefaults : false, nullTerminatedUTF8 : false) ) - let sOffset = try! fbb.createString(value: "max") - try! fbb.openObject(numOfProperties: 3) - try! fbb.appendToObject(propertyIndex: 0, value: true, defaultValue: false) - try! fbb.appendToObject(propertyIndex: 1, offset: sOffset) - let oOffset = try! fbb.closeObject() + let sOffset = try! fbb.append(value: "max") + try! fbb.startObject(numOfProperties: 3) + try! fbb.append(propertyIndex: 0, value: true, defaultValue: false) + try! fbb.append(propertyIndex: 1, offset: sOffset) + let oOffset = try! fbb.endObject() try! fbb.finish(offset: oOffset, fileIdentifier: nil) let data = fbb.makeData @@ -265,9 +265,9 @@ class FlatBuffersReaderTest: XCTestCase { forceDefaults : false, nullTerminatedUTF8 : false) ) - let sOffset1 = try! fbb.createString(value: "max1") - let sOffset2 = try! fbb.createString(value: "max2") - let sOffset3 = try! fbb.createString(value: "max3") + let sOffset1 = try! fbb.append(value: "max1") + let sOffset2 = try! fbb.append(value: "max2") + let sOffset3 = try! fbb.append(value: "max3") try! fbb.startVector(count: 3, elementSize: 4) try!fbb.append(offset: sOffset1) @@ -280,10 +280,10 @@ class FlatBuffersReaderTest: XCTestCase { fbb.append(value: false) let bVectorOffset = fbb.endVector() - try! fbb.openObject(numOfProperties: 2) - try! fbb.appendToObject(propertyIndex: 0, offset: sVectorOffset) - try! fbb.appendToObject(propertyIndex: 1, offset: bVectorOffset) - let oOffset = try! fbb.closeObject() + try! fbb.startObject(numOfProperties: 2) + try! fbb.append(propertyIndex: 0, offset: sVectorOffset) + try! fbb.append(propertyIndex: 1, offset: bVectorOffset) + let oOffset = try! fbb.endObject() try! fbb.finish(offset: oOffset, fileIdentifier: nil) let data = fbb.makeData diff --git a/FlatBuffersSwiftTests/contacts_fb.swift b/FlatBuffersSwiftTests/contacts_fb.swift index 97936d1..c85543f 100644 --- a/FlatBuffersSwiftTests/contacts_fb.swift +++ b/FlatBuffersSwiftTests/contacts_fb.swift @@ -119,12 +119,12 @@ public extension ContactList { } offset1 = builder.endVector() } - try builder.openObject(numOfProperties: 2) + try builder.startObject(numOfProperties: 2) if entries.count > 0 { - try builder.appendToObject(propertyIndex: 1, offset: offset1) + try builder.append(propertyIndex: 1, offset: offset1) } - try builder.appendToObject(propertyIndex: 0, value : lastModified, defaultValue : 0) - let myOffset = try builder.closeObject() + try builder.append(propertyIndex: 0, value : lastModified, defaultValue : 0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -341,7 +341,7 @@ public extension Contact { var offsets = [Offset?](repeating: nil, count: tags.count) var index = tags.count - 1 while(index >= 0){ - offsets[index] = try builder.createString(value: tags[index]) + offsets[index] = try builder.append(value: tags[index]) index -= 1 } try builder.startVector(count: tags.count, elementSize: MemoryLayout.stride) @@ -353,30 +353,30 @@ public extension Contact { offset3 = builder.endVector() } let offset1 = try birthday?.addToByteArray(builder) ?? 0 - let offset0 = try builder.createString(value: name) - try builder.openObject(numOfProperties: 8) + let offset0 = try builder.append(value: name) + try builder.startObject(numOfProperties: 8) if moods.count > 0 { - try builder.appendToObject(propertyIndex: 7, offset: offset7) + try builder.append(propertyIndex: 7, offset: offset7) } if previousLocations.count > 0 { - try builder.appendToObject(propertyIndex: 6, offset: offset6) + try builder.append(propertyIndex: 6, offset: offset6) } if let currentLoccation = currentLoccation { builder.append(value: currentLoccation) - try builder.appenCurrentOffsetAsPropertyToObject(propertyIndex: 5) + try builder.appendCurrentOffsetAsProperty(propertyIndex: 5) } if addressEntries.count > 0 { - try builder.appendToObject(propertyIndex: 4, offset: offset4) + try builder.append(propertyIndex: 4, offset: offset4) } if tags.count > 0 { - try builder.appendToObject(propertyIndex: 3, offset: offset3) + try builder.append(propertyIndex: 3, offset: offset3) } - try builder.appendToObject(propertyIndex: 2, value : gender?.rawValue ?? 0, defaultValue : 0) + try builder.append(propertyIndex: 2, value : gender?.rawValue ?? 0, defaultValue : 0) if birthday != nil { - try builder.appendToObject(propertyIndex: 1, offset: offset1) + try builder.append(propertyIndex: 1, offset: offset1) } - try builder.appendToObject(propertyIndex: 0, offset: offset0) - let myOffset = try builder.closeObject() + try builder.append(propertyIndex: 0, offset: offset0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -441,11 +441,11 @@ public extension Date { return myOffset } } - try builder.openObject(numOfProperties: 3) - try builder.appendToObject(propertyIndex: 2, value : year, defaultValue : 0) - try builder.appendToObject(propertyIndex: 1, value : month, defaultValue : 0) - try builder.appendToObject(propertyIndex: 0, value : day, defaultValue : 0) - let myOffset = try builder.closeObject() + try builder.startObject(numOfProperties: 3) + try builder.append(propertyIndex: 2, value : year, defaultValue : 0) + try builder.append(propertyIndex: 1, value : month, defaultValue : 0) + try builder.append(propertyIndex: 0, value : day, defaultValue : 0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -513,13 +513,13 @@ public extension AddressEntry { } } let offset1 = try addToByteArray_Address(builder, union: address) - try builder.openObject(numOfProperties: 3) + try builder.startObject(numOfProperties: 3) if let object = address { - try builder.appendToObject(propertyIndex: 2, offset: offset1) - try builder.appendToObject(propertyIndex: 1, value : unionCase_Address(object), defaultValue : 0) + try builder.append(propertyIndex: 2, offset: offset1) + try builder.append(propertyIndex: 1, value : unionCase_Address(object), defaultValue : 0) } - try builder.appendToObject(propertyIndex: 0, value : order, defaultValue : 0) - let myOffset = try builder.closeObject() + try builder.append(propertyIndex: 0, value : order, defaultValue : 0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -584,15 +584,15 @@ public extension PostalAddress { return myOffset } } - let offset3 = try builder.createString(value: streetAndNumber) - let offset1 = try builder.createString(value: city) - let offset0 = try builder.createString(value: country) - try builder.openObject(numOfProperties: 4) - try builder.appendToObject(propertyIndex: 3, offset: offset3) - try builder.appendToObject(propertyIndex: 2, value : postalCode, defaultValue : 0) - try builder.appendToObject(propertyIndex: 1, offset: offset1) - try builder.appendToObject(propertyIndex: 0, offset: offset0) - let myOffset = try builder.closeObject() + let offset3 = try builder.append(value: streetAndNumber) + let offset1 = try builder.append(value: city) + let offset0 = try builder.append(value: country) + try builder.startObject(numOfProperties: 4) + try builder.append(propertyIndex: 3, offset: offset3) + try builder.append(propertyIndex: 2, value : postalCode, defaultValue : 0) + try builder.append(propertyIndex: 1, offset: offset1) + try builder.append(propertyIndex: 0, offset: offset0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -643,10 +643,10 @@ public extension EmailAddress { return myOffset } } - let offset0 = try builder.createString(value: mailto) - try builder.openObject(numOfProperties: 1) - try builder.appendToObject(propertyIndex: 0, offset: offset0) - let myOffset = try builder.closeObject() + let offset0 = try builder.append(value: mailto) + try builder.startObject(numOfProperties: 1) + try builder.append(propertyIndex: 0, offset: offset0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -697,10 +697,10 @@ public extension WebAddress { return myOffset } } - let offset0 = try builder.createString(value: url) - try builder.openObject(numOfProperties: 1) - try builder.appendToObject(propertyIndex: 0, offset: offset0) - let myOffset = try builder.closeObject() + let offset0 = try builder.append(value: url) + try builder.startObject(numOfProperties: 1) + try builder.append(propertyIndex: 0, offset: offset0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -751,10 +751,10 @@ public extension TelephoneNumber { return myOffset } } - let offset0 = try builder.createString(value: number) - try builder.openObject(numOfProperties: 1) - try builder.appendToObject(propertyIndex: 0, offset: offset0) - let myOffset = try builder.closeObject() + let offset0 = try builder.append(value: number) + try builder.startObject(numOfProperties: 1) + try builder.append(propertyIndex: 0, offset: offset0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } diff --git a/FlatBuffersSwiftTests/friends_fb.swift b/FlatBuffersSwiftTests/friends_fb.swift index b21baf0..08aa436 100644 --- a/FlatBuffersSwiftTests/friends_fb.swift +++ b/FlatBuffersSwiftTests/friends_fb.swift @@ -132,11 +132,11 @@ public extension PeopleList { } offset0 = builder.endVector() } - try builder.openObject(numOfProperties: 1) + try builder.startObject(numOfProperties: 1) if people.count > 0 { - try builder.appendToObject(propertyIndex: 0, offset: offset0) + try builder.append(propertyIndex: 0, offset: offset0) } - let myOffset = try builder.closeObject() + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -272,17 +272,17 @@ public extension Friend { } offset1 = builder.endVector() } - let offset0 = try builder.createString(value: name) - try builder.openObject(numOfProperties: 6) + let offset0 = try builder.append(value: name) + try builder.startObject(numOfProperties: 6) if let object = lover { - let cursor4 = try builder.appendToObject(propertyIndex: 5, offset: offset4) + let cursor4 = try builder.append(propertyIndex: 5, offset: offset4) if offset4 == 0 { builder.deferedBindings.append((object: object, cursor: cursor4)) } - try builder.appendToObject(propertyIndex: 4, value : unionCase_Human(object), defaultValue : 0) + try builder.append(propertyIndex: 4, value : unionCase_Human(object), defaultValue : 0) } if mother != nil { - let cursor3 = try builder.appendToObject(propertyIndex: 3, offset: offset3) + let cursor3 = try builder.append(propertyIndex: 3, offset: offset3) if offset3 == 0 { if let object = mother { builder.deferedBindings.append((object: object, cursor: cursor3)) @@ -290,7 +290,7 @@ public extension Friend { } } if father != nil { - let cursor2 = try builder.appendToObject(propertyIndex: 2, offset: offset2) + let cursor2 = try builder.append(propertyIndex: 2, offset: offset2) if offset2 == 0 { if let object = father { builder.deferedBindings.append((object: object, cursor: cursor2)) @@ -298,10 +298,10 @@ public extension Friend { } } if friends.count > 0 { - try builder.appendToObject(propertyIndex: 1, offset: offset1) + try builder.append(propertyIndex: 1, offset: offset1) } - try builder.appendToObject(propertyIndex: 0, offset: offset0) - let myOffset = try builder.closeObject() + try builder.append(propertyIndex: 0, offset: offset0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -363,16 +363,16 @@ public extension Male { } builder.inProgress.insert(ObjectIdentifier(self)) let offset0 = try ref?.addToByteArray(builder) ?? 0 - try builder.openObject(numOfProperties: 1) + try builder.startObject(numOfProperties: 1) if ref != nil { - let cursor0 = try builder.appendToObject(propertyIndex: 0, offset: offset0) + let cursor0 = try builder.append(propertyIndex: 0, offset: offset0) if offset0 == 0 { if let object = ref { builder.deferedBindings.append((object: object, cursor: cursor0)) } } } - let myOffset = try builder.closeObject() + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -434,16 +434,16 @@ public extension Female { } builder.inProgress.insert(ObjectIdentifier(self)) let offset0 = try ref?.addToByteArray(builder) ?? 0 - try builder.openObject(numOfProperties: 1) + try builder.startObject(numOfProperties: 1) if ref != nil { - let cursor0 = try builder.appendToObject(propertyIndex: 0, offset: offset0) + let cursor0 = try builder.append(propertyIndex: 0, offset: offset0) if offset0 == 0 { if let object = ref { builder.deferedBindings.append((object: object, cursor: cursor0)) } } } - let myOffset = try builder.closeObject() + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } From f5dd28e51e4af2271f9e372af1ab7a9f14f21ae1 Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Wed, 11 Jan 2017 14:35:07 +0100 Subject: [PATCH 14/16] Fix documentation. --- FlatBuffersSwift/FlatBuffersBuilder.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FlatBuffersSwift/FlatBuffersBuilder.swift b/FlatBuffersSwift/FlatBuffersBuilder.swift index 20d7d74..2fe11f6 100644 --- a/FlatBuffersSwift/FlatBuffersBuilder.swift +++ b/FlatBuffersSwift/FlatBuffersBuilder.swift @@ -220,7 +220,7 @@ public final class FlatBuffersBuilder { } /** - Start an object update sequence + Start an object construction sequence - parameters: - numOfProperties: The number of properties we will update From 3c3107dd6b2886a0ba3cf802f51399d177005a9c Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Wed, 11 Jan 2017 15:14:52 +0100 Subject: [PATCH 15/16] append ---> insert --- FlatBuffersSwift/FlatBuffersBuilder.swift | 54 +++--- FlatBuffersSwiftTests/BuilderTest.swift | 212 +++++++++++----------- FlatBuffersSwiftTests/FBReaderTest.swift | 26 +-- FlatBuffersSwiftTests/contacts_fb.swift | 74 ++++---- FlatBuffersSwiftTests/friends_fb.swift | 24 +-- 5 files changed, 195 insertions(+), 195 deletions(-) diff --git a/FlatBuffersSwift/FlatBuffersBuilder.swift b/FlatBuffersSwift/FlatBuffersBuilder.swift index 2fe11f6..88f14c7 100644 --- a/FlatBuffersSwift/FlatBuffersBuilder.swift +++ b/FlatBuffersSwift/FlatBuffersBuilder.swift @@ -144,12 +144,12 @@ public final class FlatBuffersBuilder { } /** - Append a scalar value to the buffer + Add a scalar value to the buffer - parameters: - value: The value to add to the buffer */ - public func append(value : T){ + public func insert(value : T){ let c = MemoryLayout.stride(ofValue: value) if c > 8 { align(size: 8, additionalBytes: c) @@ -164,15 +164,15 @@ public final class FlatBuffersBuilder { } /** - Make offset relative and append it to the buffer + Make offset relative and add it to the buffer - parameters: - offset: The offset to transform and add to the buffer */ @discardableResult - public func append(offset : Offset?) throws -> Int { + public func insert(offset : Offset?) throws -> Int { guard let offset = offset else { - append(value: Offset(0)) + insert(value: Offset(0)) return cursor } guard offset <= Int32(cursor) else { @@ -180,12 +180,12 @@ public final class FlatBuffersBuilder { } if offset == Int32(0) { - append(value: Offset(0)) + insert(value: Offset(0)) return cursor } align(size: 4, additionalBytes: 0) let _offset = Int32(cursor) - offset + MemoryLayout.stride; - append(value: _offset) + insert(value: _offset) return cursor } @@ -238,7 +238,7 @@ public final class FlatBuffersBuilder { } /** - Append an offset into the buffer for the currently open object + Add an offset into the buffer for the currently open object - parameters: - propertyIndex: The index of the property to update @@ -247,20 +247,20 @@ public final class FlatBuffersBuilder { - Returns: The current cursor position (Note: What is the use case of the return value?) */ @discardableResult - public func append(propertyIndex : Int, offset : Offset) throws -> Int{ + public func insert(propertyIndex : Int, offset : Offset) throws -> Int{ guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject } guard propertyIndex >= 0 && propertyIndex < currentVTable.count else { throw FlatBuffersBuildError.propertyIndexIsInvalid } - _ = try append(offset: offset) + _ = try insert(offset: offset) currentVTable[propertyIndex] = Int32(cursor) return cursor } /** - Append an scalar into the buffer for the currently open object + Add a scalar into the buffer for the currently open object - parameters: - propertyIndex: The index of the property to update @@ -268,7 +268,7 @@ public final class FlatBuffersBuilder { - defaultValue: If configured to skip default values, a value matching this default value will not be written to the buffer. */ - public func append(propertyIndex : Int, value : T, defaultValue : T) throws { + public func insert(propertyIndex : Int, value : T, defaultValue : T) throws { guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject } @@ -280,17 +280,17 @@ public final class FlatBuffersBuilder { return } - append(value: value) + insert(value: value) currentVTable[propertyIndex] = Int32(cursor) } /** - Append the current cursor position into the buffer for the currently open object + Add the current cursor position into the buffer for the currently open object - parameters: - propertyIndex: The index of the property to update */ - public func appendCurrentOffsetAsProperty(propertyIndex : Int) throws { + public func insertCurrentOffsetAsProperty(propertyIndex : Int) throws { guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject } @@ -318,14 +318,14 @@ public final class FlatBuffersBuilder { while(index>=0) { // Offset relative to the start of the table. let off = Int16(currentVTable[index] != 0 ? Int32(vtableloc) - currentVTable[index] : 0); - append(value: off); + insert(value: off); index -= 1 } let numberOfstandardFields = 2 - append(value: Int16(Int32(vtableloc) - objectStart)); // standard field 1: lenght of the object data - append(value: Int16((currentVTable.count + numberOfstandardFields) * MemoryLayout.stride)); // standard field 2: length of vtable and standard fields them selves + insert(value: Int16(Int32(vtableloc) - objectStart)); // standard field 1: lenght of the object data + insert(value: Int16((currentVTable.count + numberOfstandardFields) * MemoryLayout.stride)); // standard field 2: length of vtable and standard fields them selves // search if we already have same vtable let vtableDataLength = cursor - vtableloc @@ -385,7 +385,7 @@ public final class FlatBuffersBuilder { Finish vector update operation */ public func endVector() -> Offset { - append(value: vectorNumElems) + insert(value: vectorNumElems) vectorNumElems = -1 return Int32(cursor) } @@ -393,14 +393,14 @@ public final class FlatBuffersBuilder { private var stringCache : [String:Offset] = [:] /** - Append a string to the buffer + Add a string to the buffer - parameters: - value: The string to add to the buffer - Returns: The current cursor position (Note: What is the use case of the return value?) */ - public func append(value : String?) throws -> Offset { + public func insert(value : String?) throws -> Offset { guard objectStart == -1 && vectorNumElems == -1 else { throw FlatBuffersBuildError.objectIsNotClosed } @@ -420,18 +420,18 @@ public final class FlatBuffersBuilder { align(size: 4, additionalBytes: length) reserveAdditionalCapacity(size: length) for c in utf8View.lazy.reversed() { - append(value: c) + insert(value: c) } - append(value: Int32(length - 1)) + insert(value: Int32(length - 1)) } else { let utf8View = value.utf8 let length = utf8View.count align(size: 4, additionalBytes: length) reserveAdditionalCapacity(size: length) for c in utf8View.lazy.reversed() { - append(value: c) + insert(value: c) } - append(value: Int32(length)) + insert(value: Int32(length)) } let o = Offset(cursor) @@ -458,7 +458,7 @@ public final class FlatBuffersBuilder { throw FlatBuffersBuildError.badFileIdentifier } for c in utf8View.lazy.reversed() { - append(value: c) + insert(value: c) } } else { align(size: minalign, additionalBytes: prefixLength) @@ -466,6 +466,6 @@ public final class FlatBuffersBuilder { let v = (Int32(cursor + 4) - offset) - append(value: v) + insert(value: v) } } diff --git a/FlatBuffersSwiftTests/BuilderTest.swift b/FlatBuffersSwiftTests/BuilderTest.swift index 26712b2..fd61a29 100644 --- a/FlatBuffersSwiftTests/BuilderTest.swift +++ b/FlatBuffersSwiftTests/BuilderTest.swift @@ -26,11 +26,11 @@ class BuilderTest: XCTestCase { } func testPutOneBytePrimitives() { - builder.append(value: true) - builder.append(value: false) - builder.append(value: Int8(-12)) - builder.append(value: Int8(12)) - builder.append(value: UInt8(13)) + builder.insert(value: true) + builder.insert(value: false) + builder.insert(value: Int8(-12)) + builder.insert(value: Int8(12)) + builder.insert(value: UInt8(13)) assertByte(with: 13) assertByte(with: 12) @@ -40,9 +40,9 @@ class BuilderTest: XCTestCase { } func testPutTwoBytePrimitives() { - builder.append(value: Int16(-12)) - builder.append(value: Int16(12)) - builder.append(value: UInt16(13)) + builder.insert(value: Int16(-12)) + builder.insert(value: Int16(12)) + builder.insert(value: UInt16(13)) assertByte(with: 13) assertByte(with: 0) @@ -53,9 +53,9 @@ class BuilderTest: XCTestCase { } func testPutFourBytePrimitives() { - builder.append(value: Int32(-12)) - builder.append(value: Int32(12)) - builder.append(value: UInt32(13)) + builder.insert(value: Int32(-12)) + builder.insert(value: Int32(12)) + builder.insert(value: UInt32(13)) assertByte(with: 13) assertByte(with: 0) @@ -73,9 +73,9 @@ class BuilderTest: XCTestCase { } func testPutEightBytePrimitives() { - builder.append(value: Int(-12)) - builder.append(value: Int(12)) - builder.append(value: UInt(13)) + builder.insert(value: Int(-12)) + builder.insert(value: Int(12)) + builder.insert(value: UInt(13)) assertByte(with: 13) assertByte(with: 0) @@ -104,8 +104,8 @@ class BuilderTest: XCTestCase { } func testPutFourByteFloat() { - builder.append(value: Float32(12.5)) - builder.append(value: Float32(-12.5)) + builder.insert(value: Float32(12.5)) + builder.insert(value: Float32(-12.5)) assertByte(with: 0) assertByte(with: 0) @@ -119,8 +119,8 @@ class BuilderTest: XCTestCase { } func testPutEightByteFloat() { - builder.append(value: Float64(12.5)) - builder.append(value: Float64(-12.5)) + builder.insert(value: Float64(12.5)) + builder.insert(value: Float64(-12.5)) assertByte(with: 0) assertByte(with: 0) @@ -141,7 +141,7 @@ class BuilderTest: XCTestCase { } func testPutStruct() { - builder.append(value: S1(i1: 9999, i2: 13)) + builder.insert(value: S1(i1: 9999, i2: 13)) assertByte(with: 15) assertByte(with: 39) @@ -156,13 +156,13 @@ class BuilderTest: XCTestCase { } func testPrimitiveAlingment() { - builder.append(value: true) - builder.append(value: Int32(16)) - builder.append(value: Int16(7)) - builder.append(value: Int16(8)) - builder.append(value: Int(15)) - builder.append(value: Float32(2.5)) - builder.append(value: Float32(3.5)) + builder.insert(value: true) + builder.insert(value: Int32(16)) + builder.insert(value: Int16(7)) + builder.insert(value: Int16(8)) + builder.insert(value: Int(15)) + builder.insert(value: Float32(2.5)) + builder.insert(value: Float32(3.5)) assertByte(with: 0) assertByte(with: 0) @@ -198,8 +198,8 @@ class BuilderTest: XCTestCase { } func testAlignmentWithStruct(){ - builder.append(value: Int16(45)) - builder.append(value: S1(i1: 9999, i2: 11)) + builder.insert(value: Int16(45)) + builder.insert(value: S1(i1: 9999, i2: 11)) assertByte(with: 15) @@ -219,7 +219,7 @@ class BuilderTest: XCTestCase { } func testString(){ - let o = try!builder.append(value: "maxim") + let o = try!builder.insert(value: "maxim") XCTAssertEqual(o, 12) @@ -239,7 +239,7 @@ class BuilderTest: XCTestCase { builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: true, forceDefaults: false, nullTerminatedUTF8: true)) - let o = try!builder.append(value: "maxim") + let o = try!builder.insert(value: "maxim") XCTAssertEqual(o, 12) @@ -257,8 +257,8 @@ class BuilderTest: XCTestCase { } func testStringWithPrimitives(){ - builder.append(value: Int16(25)) - let o = try!builder.append(value: "maxim") + builder.insert(value: Int16(25)) + let o = try!builder.insert(value: "maxim") XCTAssertEqual(o, 12) @@ -280,9 +280,9 @@ class BuilderTest: XCTestCase { } func testStringWithPrimitivesAndFinish(){ - builder.append(value: Int16(25)) - let o = try!builder.append(value: "maxim") - builder.append(value: true) + builder.insert(value: Int16(25)) + let o = try!builder.insert(value: "maxim") + builder.insert(value: true) try!builder.finish(offset: o, fileIdentifier: nil) XCTAssertEqual(o, 12) @@ -313,9 +313,9 @@ class BuilderTest: XCTestCase { } func testStringWithPrimitivesAndFinishWithFileIdentifier(){ - builder.append(value: Int16(25)) - let o = try!builder.append(value: "maxim") - builder.append(value: true) + builder.insert(value: Int16(25)) + let o = try!builder.insert(value: "maxim") + builder.insert(value: true) try!builder.finish(offset: o, fileIdentifier: "test") XCTAssertEqual(o, 12) @@ -350,10 +350,10 @@ class BuilderTest: XCTestCase { } func testObjectWithNameAndAge() { - let s = try!builder.append(value: "maxim") + let s = try!builder.insert(value: "maxim") try!builder.startObject(numOfProperties: 2) - try!builder.append(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.append(propertyIndex: 0, offset: s) + try!builder.insert(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.insert(propertyIndex: 0, offset: s) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -394,12 +394,12 @@ class BuilderTest: XCTestCase { } func testObjectWithNameReuseAndAge() { - let s = try!builder.append(value: "maxim") - let s2 = try!builder.append(value: "maxim") + let s = try!builder.insert(value: "maxim") + let s2 = try!builder.insert(value: "maxim") try!builder.startObject(numOfProperties: 3) - try!builder.append(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.append(propertyIndex: 0, offset: s) - try!builder.append(propertyIndex: 2, offset: s2) + try!builder.insert(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.insert(propertyIndex: 0, offset: s) + try!builder.insert(propertyIndex: 2, offset: s2) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -450,12 +450,12 @@ class BuilderTest: XCTestCase { func testObjectWithoutNameReuseAndAge() { builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: false, uniqueTables: true, uniqueVTables: true, forceDefaults: false, nullTerminatedUTF8: false)) - let s = try!builder.append(value: "maxim") - let s2 = try!builder.append(value: "maxim") + let s = try!builder.insert(value: "maxim") + let s2 = try!builder.insert(value: "maxim") try!builder.startObject(numOfProperties: 3) - try!builder.append(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.append(propertyIndex: 0, offset: s) - try!builder.append(propertyIndex: 2, offset: s2) + try!builder.insert(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.insert(propertyIndex: 0, offset: s) + try!builder.insert(propertyIndex: 2, offset: s2) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -516,11 +516,11 @@ class BuilderTest: XCTestCase { } func testObjectWithNameAndAgeAndReferenceToSelf() { - let s = try!builder.append(value: "maxim") + let s = try!builder.insert(value: "maxim") try!builder.startObject(numOfProperties: 3) - try!builder.append(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.append(propertyIndex: 0, offset: s) - let cursor = try!builder.append(propertyIndex: 2, offset: 0) + try!builder.insert(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.insert(propertyIndex: 0, offset: s) + let cursor = try!builder.insert(propertyIndex: 2, offset: 0) let o = try!builder.endObject() try!builder.update(offset: o, atCursor: cursor) try!builder.finish(offset: o, fileIdentifier: nil) @@ -572,9 +572,9 @@ class BuilderTest: XCTestCase { func testObjectWithStruct(){ try!builder.startObject(numOfProperties: 2) - try!builder.append(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - builder.append(value: S1(i1: 45, i2: 78)) - try! builder.appendCurrentOffsetAsProperty(propertyIndex: 0) + try!builder.insert(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + builder.insert(value: S1(i1: 45, i2: 78)) + try! builder.insertCurrentOffsetAsProperty(propertyIndex: 0) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -610,11 +610,11 @@ class BuilderTest: XCTestCase { func testObjectWithVectorOfInt16(){ try!builder.startVector(count: 2, elementSize: 2) - builder.append(value: Int16(15)) - builder.append(value: Int16(19)) + builder.insert(value: Int16(15)) + builder.insert(value: Int16(19)) let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.append(propertyIndex: 0, offset: v) + try! builder.insert(propertyIndex: 0, offset: v) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -652,11 +652,11 @@ class BuilderTest: XCTestCase { func testObjectWithVectorOfBoolean(){ try!builder.startVector(count: 2, elementSize: 1) - builder.append(value: true) - builder.append(value: false) + builder.insert(value: true) + builder.insert(value: false) let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.append(propertyIndex: 0, offset: v) + try! builder.insert(propertyIndex: 0, offset: v) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -693,11 +693,11 @@ class BuilderTest: XCTestCase { func testObjectWithVectorOfStructs(){ try!builder.startVector(count: 2, elementSize: 1) - builder.append(value: S1(i1: 12, i2: 19)) - builder.append(value: S1(i1: 13, i2: 17)) + builder.insert(value: S1(i1: 12, i2: 19)) + builder.insert(value: S1(i1: 13, i2: 17)) let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.append(propertyIndex: 0, offset: v) + try! builder.insert(propertyIndex: 0, offset: v) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -755,24 +755,24 @@ class BuilderTest: XCTestCase { func testObjectWithVectorToOtherTwoObjects(){ try!builder.startObject(numOfProperties: 3) - try!builder.append(propertyIndex: 0, value: Int8(12), defaultValue: 0) - try!builder.append(propertyIndex: 1, value: Int8(13), defaultValue: 0) - try!builder.append(propertyIndex: 2, value: Int8(14), defaultValue: 0) + try!builder.insert(propertyIndex: 0, value: Int8(12), defaultValue: 0) + try!builder.insert(propertyIndex: 1, value: Int8(13), defaultValue: 0) + try!builder.insert(propertyIndex: 2, value: Int8(14), defaultValue: 0) let o1 = try!builder.endObject() try!builder.startObject(numOfProperties: 3) - try!builder.append(propertyIndex: 0, value: Int8(22), defaultValue: 0) - try!builder.append(propertyIndex: 1, value: Int8(23), defaultValue: 0) - try!builder.append(propertyIndex: 2, value: Int8(24), defaultValue: 0) + try!builder.insert(propertyIndex: 0, value: Int8(22), defaultValue: 0) + try!builder.insert(propertyIndex: 1, value: Int8(23), defaultValue: 0) + try!builder.insert(propertyIndex: 2, value: Int8(24), defaultValue: 0) let o2 = try!builder.endObject() try!builder.startVector(count: 2, elementSize: 1) - try!builder.append(offset: o1) - try!builder.append(offset: o2) + try!builder.insert(offset: o1) + try!builder.insert(offset: o2) let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.append(propertyIndex: 0, offset: v) + try! builder.insert(propertyIndex: 0, offset: v) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -855,22 +855,22 @@ class BuilderTest: XCTestCase { func testObjectWithVectorToOtherTwoObjectsAndVTableReuese(){ try!builder.startObject(numOfProperties: 2) - try!builder.append(propertyIndex: 0, value: Int16(12), defaultValue: 0) - try!builder.append(propertyIndex: 1, value: Int16(13), defaultValue: 0) + try!builder.insert(propertyIndex: 0, value: Int16(12), defaultValue: 0) + try!builder.insert(propertyIndex: 1, value: Int16(13), defaultValue: 0) let o1 = try!builder.endObject() try!builder.startObject(numOfProperties: 2) - try!builder.append(propertyIndex: 0, value: Int16(22), defaultValue: 0) - try!builder.append(propertyIndex: 1, value: Int16(23), defaultValue: 0) + try!builder.insert(propertyIndex: 0, value: Int16(22), defaultValue: 0) + try!builder.insert(propertyIndex: 1, value: Int16(23), defaultValue: 0) let o2 = try!builder.endObject() try!builder.startVector(count: 2, elementSize: 1) - try!builder.append(offset: o1) - try!builder.append(offset: o2) + try!builder.insert(offset: o1) + try!builder.insert(offset: o2) let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.append(propertyIndex: 0, offset: v) + try! builder.insert(propertyIndex: 0, offset: v) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -941,22 +941,22 @@ class BuilderTest: XCTestCase { func testObjectWithVectorToOtherTwoObjectsAndVTableWithoutReuese(){ builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: false, forceDefaults: false, nullTerminatedUTF8: false)) try!builder.startObject(numOfProperties: 2) - try!builder.append(propertyIndex: 0, value: Int16(12), defaultValue: 0) - try!builder.append(propertyIndex: 1, value: Int16(13), defaultValue: 0) + try!builder.insert(propertyIndex: 0, value: Int16(12), defaultValue: 0) + try!builder.insert(propertyIndex: 1, value: Int16(13), defaultValue: 0) let o1 = try!builder.endObject() try!builder.startObject(numOfProperties: 2) - try!builder.append(propertyIndex: 0, value: Int16(22), defaultValue: 0) - try!builder.append(propertyIndex: 1, value: Int16(23), defaultValue: 0) + try!builder.insert(propertyIndex: 0, value: Int16(22), defaultValue: 0) + try!builder.insert(propertyIndex: 1, value: Int16(23), defaultValue: 0) let o2 = try!builder.endObject() try!builder.startVector(count: 2, elementSize: 1) - try!builder.append(offset: o1) - try!builder.append(offset: o2) + try!builder.insert(offset: o1) + try!builder.insert(offset: o2) let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.append(propertyIndex: 0, offset: v) + try! builder.insert(propertyIndex: 0, offset: v) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -1036,11 +1036,11 @@ class BuilderTest: XCTestCase { func testObjectWithVectorToSelf(){ try!builder.startVector(count: 2, elementSize: 2) - let c1 = try!builder.append(offset: nil) - let c2 = try!builder.append(offset: nil) + let c1 = try!builder.insert(offset: nil) + let c2 = try!builder.insert(offset: nil) let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.append(propertyIndex: 0, offset: v) + try! builder.insert(propertyIndex: 0, offset: v) let o = try!builder.endObject() try!builder.update(offset: o, atCursor: c1) @@ -1087,7 +1087,7 @@ class BuilderTest: XCTestCase { func testPutBadOffset(){ var itThrows = false do { - try builder.append(offset: 12) + try builder.insert(offset: 12) } catch { itThrows = true } @@ -1128,7 +1128,7 @@ class BuilderTest: XCTestCase { func testAddPropertyWithoutOpenObject(){ var itThrows = false do { - try builder.append(propertyIndex: 0, value: 1, defaultValue: 0) + try builder.insert(propertyIndex: 0, value: 1, defaultValue: 0) } catch { itThrows = true } @@ -1139,7 +1139,7 @@ class BuilderTest: XCTestCase { var itThrows = false try!builder.startObject(numOfProperties: 1) do { - try builder.append(propertyIndex: 1, value: 1, defaultValue: 0) + try builder.insert(propertyIndex: 1, value: 1, defaultValue: 0) } catch { itThrows = true } @@ -1149,7 +1149,7 @@ class BuilderTest: XCTestCase { func testAddOffsetWithoutOpenObject(){ var itThrows = false do { - try builder.append(propertyIndex: 0, offset: 0) + try builder.insert(propertyIndex: 0, offset: 0) } catch { itThrows = true } @@ -1160,7 +1160,7 @@ class BuilderTest: XCTestCase { var itThrows = false try!builder.startObject(numOfProperties: 1) do { - try builder.append(propertyIndex: 1, offset: 0) + try builder.insert(propertyIndex: 1, offset: 0) } catch { itThrows = true } @@ -1170,7 +1170,7 @@ class BuilderTest: XCTestCase { func testAddCurrentOffsetWithoutOpenObject(){ var itThrows = false do { - try builder.appendCurrentOffsetAsProperty(propertyIndex: 0) + try builder.insertCurrentOffsetAsProperty(propertyIndex: 0) } catch { itThrows = true } @@ -1181,7 +1181,7 @@ class BuilderTest: XCTestCase { var itThrows = false try!builder.startObject(numOfProperties: 1) do { - try builder.appendCurrentOffsetAsProperty(propertyIndex: 1) + try builder.insertCurrentOffsetAsProperty(propertyIndex: 1) } catch { itThrows = true } @@ -1255,7 +1255,7 @@ class BuilderTest: XCTestCase { var itThrows = false try!builder.startObject(numOfProperties: 1) do { - _ = try builder.append(value: "Mo") + _ = try builder.insert(value: "Mo") } catch { itThrows = true } @@ -1263,14 +1263,14 @@ class BuilderTest: XCTestCase { } func testCreateNilString(){ - let c = try!builder.append(value: nil) + let c = try!builder.insert(value: nil) XCTAssertEqual(c, 0) } func testDefaultValuesNotSet(){ try!builder.startObject(numOfProperties: 2) - try!builder.append(propertyIndex: 0, value: Int32(12), defaultValue: 0) - try!builder.append(propertyIndex: 1, value: Int32(5), defaultValue: 5) + try!builder.insert(propertyIndex: 0, value: Int32(12), defaultValue: 0) + try!builder.insert(propertyIndex: 1, value: Int32(5), defaultValue: 5) _ = try!builder.endObject() assertByte(with: 8) @@ -1294,8 +1294,8 @@ class BuilderTest: XCTestCase { func testDefaultValuesForced(){ builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: true, forceDefaults: true, nullTerminatedUTF8: false)) try!builder.startObject(numOfProperties: 2) - try!builder.append(propertyIndex: 0, value: Int32(12), defaultValue: 0) - try!builder.append(propertyIndex: 1, value: Int32(5), defaultValue: 5) + try!builder.insert(propertyIndex: 0, value: Int32(12), defaultValue: 0) + try!builder.insert(propertyIndex: 1, value: Int32(5), defaultValue: 5) _ = try!builder.endObject() assertByte(with: 8) diff --git a/FlatBuffersSwiftTests/FBReaderTest.swift b/FlatBuffersSwiftTests/FBReaderTest.swift index 3a72360..c3da309 100644 --- a/FlatBuffersSwiftTests/FBReaderTest.swift +++ b/FlatBuffersSwiftTests/FBReaderTest.swift @@ -245,10 +245,10 @@ class FlatBuffersReaderTest: XCTestCase { forceDefaults : false, nullTerminatedUTF8 : false) ) - let sOffset = try! fbb.append(value: "max") + let sOffset = try! fbb.insert(value: "max") try! fbb.startObject(numOfProperties: 3) - try! fbb.append(propertyIndex: 0, value: true, defaultValue: false) - try! fbb.append(propertyIndex: 1, offset: sOffset) + try! fbb.insert(propertyIndex: 0, value: true, defaultValue: false) + try! fbb.insert(propertyIndex: 1, offset: sOffset) let oOffset = try! fbb.endObject() try! fbb.finish(offset: oOffset, fileIdentifier: nil) let data = fbb.makeData @@ -265,24 +265,24 @@ class FlatBuffersReaderTest: XCTestCase { forceDefaults : false, nullTerminatedUTF8 : false) ) - let sOffset1 = try! fbb.append(value: "max1") - let sOffset2 = try! fbb.append(value: "max2") - let sOffset3 = try! fbb.append(value: "max3") + let sOffset1 = try! fbb.insert(value: "max1") + let sOffset2 = try! fbb.insert(value: "max2") + let sOffset3 = try! fbb.insert(value: "max3") try! fbb.startVector(count: 3, elementSize: 4) - try!fbb.append(offset: sOffset1) - try!fbb.append(offset: sOffset2) - try!fbb.append(offset: sOffset3) + try!fbb.insert(offset: sOffset1) + try!fbb.insert(offset: sOffset2) + try!fbb.insert(offset: sOffset3) let sVectorOffset = fbb.endVector() try! fbb.startVector(count: 2, elementSize: 1) - fbb.append(value: true) - fbb.append(value: false) + fbb.insert(value: true) + fbb.insert(value: false) let bVectorOffset = fbb.endVector() try! fbb.startObject(numOfProperties: 2) - try! fbb.append(propertyIndex: 0, offset: sVectorOffset) - try! fbb.append(propertyIndex: 1, offset: bVectorOffset) + try! fbb.insert(propertyIndex: 0, offset: sVectorOffset) + try! fbb.insert(propertyIndex: 1, offset: bVectorOffset) let oOffset = try! fbb.endObject() try! fbb.finish(offset: oOffset, fileIdentifier: nil) let data = fbb.makeData diff --git a/FlatBuffersSwiftTests/contacts_fb.swift b/FlatBuffersSwiftTests/contacts_fb.swift index c85543f..8cd00a1 100644 --- a/FlatBuffersSwiftTests/contacts_fb.swift +++ b/FlatBuffersSwiftTests/contacts_fb.swift @@ -114,16 +114,16 @@ public extension ContactList { try builder.startVector(count: entries.count, elementSize: MemoryLayout.stride) index = entries.count - 1 while(index >= 0){ - try builder.append(offset: offsets[index]) + try builder.insert(offset: offsets[index]) index -= 1 } offset1 = builder.endVector() } try builder.startObject(numOfProperties: 2) if entries.count > 0 { - try builder.append(propertyIndex: 1, offset: offset1) + try builder.insert(propertyIndex: 1, offset: offset1) } - try builder.append(propertyIndex: 0, value : lastModified, defaultValue : 0) + try builder.insert(propertyIndex: 0, value : lastModified, defaultValue : 0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -302,7 +302,7 @@ public extension Contact { var index = moods.count - 1 while(index >= 0){ if let value = moods[index]?.rawValue { - builder.append(value: value) + builder.insert(value: value) } index -= 1 } @@ -314,7 +314,7 @@ public extension Contact { var index = previousLocations.count - 1 while(index >= 0){ if let value = previousLocations[index] { - builder.append(value: value) + builder.insert(value: value) } index -= 1 } @@ -331,7 +331,7 @@ public extension Contact { try builder.startVector(count: addressEntries.count, elementSize: MemoryLayout.stride) index = addressEntries.count - 1 while(index >= 0){ - try builder.append(offset: offsets[index]) + try builder.insert(offset: offsets[index]) index -= 1 } offset4 = builder.endVector() @@ -341,41 +341,41 @@ public extension Contact { var offsets = [Offset?](repeating: nil, count: tags.count) var index = tags.count - 1 while(index >= 0){ - offsets[index] = try builder.append(value: tags[index]) + offsets[index] = try builder.insert(value: tags[index]) index -= 1 } try builder.startVector(count: tags.count, elementSize: MemoryLayout.stride) index = tags.count - 1 while(index >= 0){ - try builder.append(offset: offsets[index]) + try builder.insert(offset: offsets[index]) index -= 1 } offset3 = builder.endVector() } let offset1 = try birthday?.addToByteArray(builder) ?? 0 - let offset0 = try builder.append(value: name) + let offset0 = try builder.insert(value: name) try builder.startObject(numOfProperties: 8) if moods.count > 0 { - try builder.append(propertyIndex: 7, offset: offset7) + try builder.insert(propertyIndex: 7, offset: offset7) } if previousLocations.count > 0 { - try builder.append(propertyIndex: 6, offset: offset6) + try builder.insert(propertyIndex: 6, offset: offset6) } if let currentLoccation = currentLoccation { - builder.append(value: currentLoccation) - try builder.appendCurrentOffsetAsProperty(propertyIndex: 5) + builder.insert(value: currentLoccation) + try builder.insertCurrentOffsetAsProperty(propertyIndex: 5) } if addressEntries.count > 0 { - try builder.append(propertyIndex: 4, offset: offset4) + try builder.insert(propertyIndex: 4, offset: offset4) } if tags.count > 0 { - try builder.append(propertyIndex: 3, offset: offset3) + try builder.insert(propertyIndex: 3, offset: offset3) } - try builder.append(propertyIndex: 2, value : gender?.rawValue ?? 0, defaultValue : 0) + try builder.insert(propertyIndex: 2, value : gender?.rawValue ?? 0, defaultValue : 0) if birthday != nil { - try builder.append(propertyIndex: 1, offset: offset1) + try builder.insert(propertyIndex: 1, offset: offset1) } - try builder.append(propertyIndex: 0, offset: offset0) + try builder.insert(propertyIndex: 0, offset: offset0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -442,9 +442,9 @@ public extension Date { } } try builder.startObject(numOfProperties: 3) - try builder.append(propertyIndex: 2, value : year, defaultValue : 0) - try builder.append(propertyIndex: 1, value : month, defaultValue : 0) - try builder.append(propertyIndex: 0, value : day, defaultValue : 0) + try builder.insert(propertyIndex: 2, value : year, defaultValue : 0) + try builder.insert(propertyIndex: 1, value : month, defaultValue : 0) + try builder.insert(propertyIndex: 0, value : day, defaultValue : 0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -515,10 +515,10 @@ public extension AddressEntry { let offset1 = try addToByteArray_Address(builder, union: address) try builder.startObject(numOfProperties: 3) if let object = address { - try builder.append(propertyIndex: 2, offset: offset1) - try builder.append(propertyIndex: 1, value : unionCase_Address(object), defaultValue : 0) + try builder.insert(propertyIndex: 2, offset: offset1) + try builder.insert(propertyIndex: 1, value : unionCase_Address(object), defaultValue : 0) } - try builder.append(propertyIndex: 0, value : order, defaultValue : 0) + try builder.insert(propertyIndex: 0, value : order, defaultValue : 0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -584,14 +584,14 @@ public extension PostalAddress { return myOffset } } - let offset3 = try builder.append(value: streetAndNumber) - let offset1 = try builder.append(value: city) - let offset0 = try builder.append(value: country) + let offset3 = try builder.insert(value: streetAndNumber) + let offset1 = try builder.insert(value: city) + let offset0 = try builder.insert(value: country) try builder.startObject(numOfProperties: 4) - try builder.append(propertyIndex: 3, offset: offset3) - try builder.append(propertyIndex: 2, value : postalCode, defaultValue : 0) - try builder.append(propertyIndex: 1, offset: offset1) - try builder.append(propertyIndex: 0, offset: offset0) + try builder.insert(propertyIndex: 3, offset: offset3) + try builder.insert(propertyIndex: 2, value : postalCode, defaultValue : 0) + try builder.insert(propertyIndex: 1, offset: offset1) + try builder.insert(propertyIndex: 0, offset: offset0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -643,9 +643,9 @@ public extension EmailAddress { return myOffset } } - let offset0 = try builder.append(value: mailto) + let offset0 = try builder.insert(value: mailto) try builder.startObject(numOfProperties: 1) - try builder.append(propertyIndex: 0, offset: offset0) + try builder.insert(propertyIndex: 0, offset: offset0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -697,9 +697,9 @@ public extension WebAddress { return myOffset } } - let offset0 = try builder.append(value: url) + let offset0 = try builder.insert(value: url) try builder.startObject(numOfProperties: 1) - try builder.append(propertyIndex: 0, offset: offset0) + try builder.insert(propertyIndex: 0, offset: offset0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -751,9 +751,9 @@ public extension TelephoneNumber { return myOffset } } - let offset0 = try builder.append(value: number) + let offset0 = try builder.insert(value: number) try builder.startObject(numOfProperties: 1) - try builder.append(propertyIndex: 0, offset: offset0) + try builder.insert(propertyIndex: 0, offset: offset0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset diff --git a/FlatBuffersSwiftTests/friends_fb.swift b/FlatBuffersSwiftTests/friends_fb.swift index 08aa436..75d0834 100644 --- a/FlatBuffersSwiftTests/friends_fb.swift +++ b/FlatBuffersSwiftTests/friends_fb.swift @@ -118,7 +118,7 @@ public extension PeopleList { index = people.count - 1 var deferedBindingCursors : [Int : Int] = [:] while(index >= 0){ - let cursor = try builder.append(offset: offsets[index]) + let cursor = try builder.insert(offset: offsets[index]) if offsets[index] == 0 { deferedBindingCursors[index] = cursor } @@ -134,7 +134,7 @@ public extension PeopleList { } try builder.startObject(numOfProperties: 1) if people.count > 0 { - try builder.append(propertyIndex: 0, offset: offset0) + try builder.insert(propertyIndex: 0, offset: offset0) } let myOffset = try builder.endObject() if builder.config.uniqueTables { @@ -258,7 +258,7 @@ public extension Friend { index = friends.count - 1 var deferedBindingCursors : [Int : Int] = [:] while(index >= 0){ - let cursor = try builder.append(offset: offsets[index]) + let cursor = try builder.insert(offset: offsets[index]) if offsets[index] == 0 { deferedBindingCursors[index] = cursor } @@ -272,17 +272,17 @@ public extension Friend { } offset1 = builder.endVector() } - let offset0 = try builder.append(value: name) + let offset0 = try builder.insert(value: name) try builder.startObject(numOfProperties: 6) if let object = lover { - let cursor4 = try builder.append(propertyIndex: 5, offset: offset4) + let cursor4 = try builder.insert(propertyIndex: 5, offset: offset4) if offset4 == 0 { builder.deferedBindings.append((object: object, cursor: cursor4)) } - try builder.append(propertyIndex: 4, value : unionCase_Human(object), defaultValue : 0) + try builder.insert(propertyIndex: 4, value : unionCase_Human(object), defaultValue : 0) } if mother != nil { - let cursor3 = try builder.append(propertyIndex: 3, offset: offset3) + let cursor3 = try builder.insert(propertyIndex: 3, offset: offset3) if offset3 == 0 { if let object = mother { builder.deferedBindings.append((object: object, cursor: cursor3)) @@ -290,7 +290,7 @@ public extension Friend { } } if father != nil { - let cursor2 = try builder.append(propertyIndex: 2, offset: offset2) + let cursor2 = try builder.insert(propertyIndex: 2, offset: offset2) if offset2 == 0 { if let object = father { builder.deferedBindings.append((object: object, cursor: cursor2)) @@ -298,9 +298,9 @@ public extension Friend { } } if friends.count > 0 { - try builder.append(propertyIndex: 1, offset: offset1) + try builder.insert(propertyIndex: 1, offset: offset1) } - try builder.append(propertyIndex: 0, offset: offset0) + try builder.insert(propertyIndex: 0, offset: offset0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -365,7 +365,7 @@ public extension Male { let offset0 = try ref?.addToByteArray(builder) ?? 0 try builder.startObject(numOfProperties: 1) if ref != nil { - let cursor0 = try builder.append(propertyIndex: 0, offset: offset0) + let cursor0 = try builder.insert(propertyIndex: 0, offset: offset0) if offset0 == 0 { if let object = ref { builder.deferedBindings.append((object: object, cursor: cursor0)) @@ -436,7 +436,7 @@ public extension Female { let offset0 = try ref?.addToByteArray(builder) ?? 0 try builder.startObject(numOfProperties: 1) if ref != nil { - let cursor0 = try builder.append(propertyIndex: 0, offset: offset0) + let cursor0 = try builder.insert(propertyIndex: 0, offset: offset0) if offset0 == 0 { if let object = ref { builder.deferedBindings.append((object: object, cursor: cursor0)) From c306411039909da0e3599a64149737d2b0e62aa0 Mon Sep 17 00:00:00 2001 From: Joakim Hassila Date: Wed, 11 Jan 2017 15:33:54 +0100 Subject: [PATCH 16/16] Updating object updates according to review feedback --- FlatBuffersSwift/FlatBuffersBuilder.swift | 6 +- FlatBuffersSwiftTests/BuilderTest.swift | 88 +++++++++++------------ FlatBuffersSwiftTests/FBReaderTest.swift | 8 +-- FlatBuffersSwiftTests/contacts_fb.swift | 46 ++++++------ FlatBuffersSwiftTests/friends_fb.swift | 18 ++--- 5 files changed, 83 insertions(+), 83 deletions(-) diff --git a/FlatBuffersSwift/FlatBuffersBuilder.swift b/FlatBuffersSwift/FlatBuffersBuilder.swift index 88f14c7..a8b3417 100644 --- a/FlatBuffersSwift/FlatBuffersBuilder.swift +++ b/FlatBuffersSwift/FlatBuffersBuilder.swift @@ -247,7 +247,7 @@ public final class FlatBuffersBuilder { - Returns: The current cursor position (Note: What is the use case of the return value?) */ @discardableResult - public func insert(propertyIndex : Int, offset : Offset) throws -> Int{ + public func insert(offset : Offset, toStartedObjectAt propertyIndex : Int) throws -> Int{ guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject } @@ -268,7 +268,7 @@ public final class FlatBuffersBuilder { - defaultValue: If configured to skip default values, a value matching this default value will not be written to the buffer. */ - public func insert(propertyIndex : Int, value : T, defaultValue : T) throws { + public func insert(value : T, defaultValue : T, toStartedObjectAt propertyIndex : Int) throws { guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject } @@ -290,7 +290,7 @@ public final class FlatBuffersBuilder { - parameters: - propertyIndex: The index of the property to update */ - public func insertCurrentOffsetAsProperty(propertyIndex : Int) throws { + public func insertCurrentOffsetAsProperty(toStartedObjectAt propertyIndex : Int) throws { guard objectStart > -1 else { throw FlatBuffersBuildError.noOpenObject } diff --git a/FlatBuffersSwiftTests/BuilderTest.swift b/FlatBuffersSwiftTests/BuilderTest.swift index fd61a29..a4ba800 100644 --- a/FlatBuffersSwiftTests/BuilderTest.swift +++ b/FlatBuffersSwiftTests/BuilderTest.swift @@ -352,8 +352,8 @@ class BuilderTest: XCTestCase { func testObjectWithNameAndAge() { let s = try!builder.insert(value: "maxim") try!builder.startObject(numOfProperties: 2) - try!builder.insert(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.insert(propertyIndex: 0, offset: s) + try!builder.insert(value: UInt8(35), defaultValue: 0, toStartedObjectAt: 1) + try!builder.insert(offset: s, toStartedObjectAt: 0) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -397,9 +397,9 @@ class BuilderTest: XCTestCase { let s = try!builder.insert(value: "maxim") let s2 = try!builder.insert(value: "maxim") try!builder.startObject(numOfProperties: 3) - try!builder.insert(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.insert(propertyIndex: 0, offset: s) - try!builder.insert(propertyIndex: 2, offset: s2) + try!builder.insert(value: UInt8(35), defaultValue: 0, toStartedObjectAt: 1) + try!builder.insert(offset: s, toStartedObjectAt: 0) + try!builder.insert(offset: s2, toStartedObjectAt: 2) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -453,9 +453,9 @@ class BuilderTest: XCTestCase { let s = try!builder.insert(value: "maxim") let s2 = try!builder.insert(value: "maxim") try!builder.startObject(numOfProperties: 3) - try!builder.insert(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.insert(propertyIndex: 0, offset: s) - try!builder.insert(propertyIndex: 2, offset: s2) + try!builder.insert(value: UInt8(35), defaultValue: 0, toStartedObjectAt:1) + try!builder.insert(offset: s, toStartedObjectAt: 0) + try!builder.insert(offset: s2, toStartedObjectAt: 2) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -518,9 +518,9 @@ class BuilderTest: XCTestCase { func testObjectWithNameAndAgeAndReferenceToSelf() { let s = try!builder.insert(value: "maxim") try!builder.startObject(numOfProperties: 3) - try!builder.insert(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.insert(propertyIndex: 0, offset: s) - let cursor = try!builder.insert(propertyIndex: 2, offset: 0) + try!builder.insert(value: UInt8(35), defaultValue: 0, toStartedObjectAt: 1) + try!builder.insert(offset: s, toStartedObjectAt: 0) + let cursor = try!builder.insert(offset: 0, toStartedObjectAt: 2) let o = try!builder.endObject() try!builder.update(offset: o, atCursor: cursor) try!builder.finish(offset: o, fileIdentifier: nil) @@ -572,9 +572,9 @@ class BuilderTest: XCTestCase { func testObjectWithStruct(){ try!builder.startObject(numOfProperties: 2) - try!builder.insert(propertyIndex: 1, value: UInt8(35), defaultValue: 0) + try!builder.insert(value: UInt8(35), defaultValue: 0, toStartedObjectAt:1) builder.insert(value: S1(i1: 45, i2: 78)) - try! builder.insertCurrentOffsetAsProperty(propertyIndex: 0) + try! builder.insertCurrentOffsetAsProperty(toStartedObjectAt: 0) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -614,7 +614,7 @@ class BuilderTest: XCTestCase { builder.insert(value: Int16(19)) let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.insert(propertyIndex: 0, offset: v) + try! builder.insert(offset: v, toStartedObjectAt: 0) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -656,7 +656,7 @@ class BuilderTest: XCTestCase { builder.insert(value: false) let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.insert(propertyIndex: 0, offset: v) + try! builder.insert(offset: v, toStartedObjectAt: 0) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -697,7 +697,7 @@ class BuilderTest: XCTestCase { builder.insert(value: S1(i1: 13, i2: 17)) let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.insert(propertyIndex: 0, offset: v) + try! builder.insert(offset: v, toStartedObjectAt: 0) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -755,15 +755,15 @@ class BuilderTest: XCTestCase { func testObjectWithVectorToOtherTwoObjects(){ try!builder.startObject(numOfProperties: 3) - try!builder.insert(propertyIndex: 0, value: Int8(12), defaultValue: 0) - try!builder.insert(propertyIndex: 1, value: Int8(13), defaultValue: 0) - try!builder.insert(propertyIndex: 2, value: Int8(14), defaultValue: 0) + try!builder.insert(value: Int8(12), defaultValue: 0, toStartedObjectAt: 0) + try!builder.insert(value: Int8(13), defaultValue: 0, toStartedObjectAt: 1) + try!builder.insert(value: Int8(14), defaultValue: 0, toStartedObjectAt: 2) let o1 = try!builder.endObject() try!builder.startObject(numOfProperties: 3) - try!builder.insert(propertyIndex: 0, value: Int8(22), defaultValue: 0) - try!builder.insert(propertyIndex: 1, value: Int8(23), defaultValue: 0) - try!builder.insert(propertyIndex: 2, value: Int8(24), defaultValue: 0) + try!builder.insert(value: Int8(22), defaultValue: 0, toStartedObjectAt: 0) + try!builder.insert(value: Int8(23), defaultValue: 0, toStartedObjectAt: 1) + try!builder.insert(value: Int8(24), defaultValue: 0, toStartedObjectAt: 2) let o2 = try!builder.endObject() try!builder.startVector(count: 2, elementSize: 1) @@ -772,7 +772,7 @@ class BuilderTest: XCTestCase { let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.insert(propertyIndex: 0, offset: v) + try! builder.insert(offset: v, toStartedObjectAt: 0) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -855,13 +855,13 @@ class BuilderTest: XCTestCase { func testObjectWithVectorToOtherTwoObjectsAndVTableReuese(){ try!builder.startObject(numOfProperties: 2) - try!builder.insert(propertyIndex: 0, value: Int16(12), defaultValue: 0) - try!builder.insert(propertyIndex: 1, value: Int16(13), defaultValue: 0) + try!builder.insert(value: Int16(12), defaultValue: 0, toStartedObjectAt: 0) + try!builder.insert(value: Int16(13), defaultValue: 0, toStartedObjectAt: 1) let o1 = try!builder.endObject() try!builder.startObject(numOfProperties: 2) - try!builder.insert(propertyIndex: 0, value: Int16(22), defaultValue: 0) - try!builder.insert(propertyIndex: 1, value: Int16(23), defaultValue: 0) + try!builder.insert(value: Int16(22), defaultValue: 0, toStartedObjectAt: 0) + try!builder.insert(value: Int16(23), defaultValue: 0, toStartedObjectAt: 1) let o2 = try!builder.endObject() try!builder.startVector(count: 2, elementSize: 1) @@ -870,7 +870,7 @@ class BuilderTest: XCTestCase { let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.insert(propertyIndex: 0, offset: v) + try! builder.insert(offset: v, toStartedObjectAt: 0) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -941,13 +941,13 @@ class BuilderTest: XCTestCase { func testObjectWithVectorToOtherTwoObjectsAndVTableWithoutReuese(){ builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: false, forceDefaults: false, nullTerminatedUTF8: false)) try!builder.startObject(numOfProperties: 2) - try!builder.insert(propertyIndex: 0, value: Int16(12), defaultValue: 0) - try!builder.insert(propertyIndex: 1, value: Int16(13), defaultValue: 0) + try!builder.insert(value: Int16(12), defaultValue: 0, toStartedObjectAt: 0) + try!builder.insert(value: Int16(13), defaultValue: 0, toStartedObjectAt: 1) let o1 = try!builder.endObject() try!builder.startObject(numOfProperties: 2) - try!builder.insert(propertyIndex: 0, value: Int16(22), defaultValue: 0) - try!builder.insert(propertyIndex: 1, value: Int16(23), defaultValue: 0) + try!builder.insert(value: Int16(22), defaultValue: 0, toStartedObjectAt: 0) + try!builder.insert(value: Int16(23), defaultValue: 0, toStartedObjectAt: 1) let o2 = try!builder.endObject() try!builder.startVector(count: 2, elementSize: 1) @@ -956,7 +956,7 @@ class BuilderTest: XCTestCase { let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.insert(propertyIndex: 0, offset: v) + try! builder.insert(offset: v, toStartedObjectAt: 0) let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) @@ -1040,7 +1040,7 @@ class BuilderTest: XCTestCase { let c2 = try!builder.insert(offset: nil) let v = builder.endVector() try!builder.startObject(numOfProperties: 1) - try! builder.insert(propertyIndex: 0, offset: v) + try! builder.insert(offset: v, toStartedObjectAt: 0) let o = try!builder.endObject() try!builder.update(offset: o, atCursor: c1) @@ -1128,7 +1128,7 @@ class BuilderTest: XCTestCase { func testAddPropertyWithoutOpenObject(){ var itThrows = false do { - try builder.insert(propertyIndex: 0, value: 1, defaultValue: 0) + try builder.insert(value: 1, defaultValue: 0, toStartedObjectAt: 0) } catch { itThrows = true } @@ -1139,7 +1139,7 @@ class BuilderTest: XCTestCase { var itThrows = false try!builder.startObject(numOfProperties: 1) do { - try builder.insert(propertyIndex: 1, value: 1, defaultValue: 0) + try builder.insert(value: 1, defaultValue: 0, toStartedObjectAt: 1) } catch { itThrows = true } @@ -1149,7 +1149,7 @@ class BuilderTest: XCTestCase { func testAddOffsetWithoutOpenObject(){ var itThrows = false do { - try builder.insert(propertyIndex: 0, offset: 0) + try builder.insert(offset: 0, toStartedObjectAt: 0) } catch { itThrows = true } @@ -1160,7 +1160,7 @@ class BuilderTest: XCTestCase { var itThrows = false try!builder.startObject(numOfProperties: 1) do { - try builder.insert(propertyIndex: 1, offset: 0) + try builder.insert(offset: 0, toStartedObjectAt: 1) } catch { itThrows = true } @@ -1170,7 +1170,7 @@ class BuilderTest: XCTestCase { func testAddCurrentOffsetWithoutOpenObject(){ var itThrows = false do { - try builder.insertCurrentOffsetAsProperty(propertyIndex: 0) + try builder.insertCurrentOffsetAsProperty(toStartedObjectAt: 0) } catch { itThrows = true } @@ -1181,7 +1181,7 @@ class BuilderTest: XCTestCase { var itThrows = false try!builder.startObject(numOfProperties: 1) do { - try builder.insertCurrentOffsetAsProperty(propertyIndex: 1) + try builder.insertCurrentOffsetAsProperty(toStartedObjectAt: 1) } catch { itThrows = true } @@ -1269,8 +1269,8 @@ class BuilderTest: XCTestCase { func testDefaultValuesNotSet(){ try!builder.startObject(numOfProperties: 2) - try!builder.insert(propertyIndex: 0, value: Int32(12), defaultValue: 0) - try!builder.insert(propertyIndex: 1, value: Int32(5), defaultValue: 5) + try!builder.insert(value: Int32(12), defaultValue: 0, toStartedObjectAt: 0) + try!builder.insert(value: Int32(5), defaultValue: 5, toStartedObjectAt: 1) _ = try!builder.endObject() assertByte(with: 8) @@ -1294,8 +1294,8 @@ class BuilderTest: XCTestCase { func testDefaultValuesForced(){ builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: true, forceDefaults: true, nullTerminatedUTF8: false)) try!builder.startObject(numOfProperties: 2) - try!builder.insert(propertyIndex: 0, value: Int32(12), defaultValue: 0) - try!builder.insert(propertyIndex: 1, value: Int32(5), defaultValue: 5) + try!builder.insert(value: Int32(12), defaultValue: 0, toStartedObjectAt: 0) + try!builder.insert(value: Int32(5), defaultValue: 5, toStartedObjectAt: 1) _ = try!builder.endObject() assertByte(with: 8) diff --git a/FlatBuffersSwiftTests/FBReaderTest.swift b/FlatBuffersSwiftTests/FBReaderTest.swift index c3da309..ff8539e 100644 --- a/FlatBuffersSwiftTests/FBReaderTest.swift +++ b/FlatBuffersSwiftTests/FBReaderTest.swift @@ -247,8 +247,8 @@ class FlatBuffersReaderTest: XCTestCase { ) let sOffset = try! fbb.insert(value: "max") try! fbb.startObject(numOfProperties: 3) - try! fbb.insert(propertyIndex: 0, value: true, defaultValue: false) - try! fbb.insert(propertyIndex: 1, offset: sOffset) + try! fbb.insert(value: true, defaultValue: false, toStartedObjectAt: 0) + try! fbb.insert(offset: sOffset, toStartedObjectAt: 1) let oOffset = try! fbb.endObject() try! fbb.finish(offset: oOffset, fileIdentifier: nil) let data = fbb.makeData @@ -281,8 +281,8 @@ class FlatBuffersReaderTest: XCTestCase { let bVectorOffset = fbb.endVector() try! fbb.startObject(numOfProperties: 2) - try! fbb.insert(propertyIndex: 0, offset: sVectorOffset) - try! fbb.insert(propertyIndex: 1, offset: bVectorOffset) + try! fbb.insert(offset: sVectorOffset, toStartedObjectAt: 0) + try! fbb.insert(offset: bVectorOffset, toStartedObjectAt: 1) let oOffset = try! fbb.endObject() try! fbb.finish(offset: oOffset, fileIdentifier: nil) let data = fbb.makeData diff --git a/FlatBuffersSwiftTests/contacts_fb.swift b/FlatBuffersSwiftTests/contacts_fb.swift index 8cd00a1..80f3e33 100644 --- a/FlatBuffersSwiftTests/contacts_fb.swift +++ b/FlatBuffersSwiftTests/contacts_fb.swift @@ -121,9 +121,9 @@ public extension ContactList { } try builder.startObject(numOfProperties: 2) if entries.count > 0 { - try builder.insert(propertyIndex: 1, offset: offset1) + try builder.insert(offset: offset1, toStartedObjectAt:1) } - try builder.insert(propertyIndex: 0, value : lastModified, defaultValue : 0) + try builder.insert(value : lastModified, defaultValue : 0, toStartedObjectAt: 0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -356,26 +356,26 @@ public extension Contact { let offset0 = try builder.insert(value: name) try builder.startObject(numOfProperties: 8) if moods.count > 0 { - try builder.insert(propertyIndex: 7, offset: offset7) + try builder.insert(offset: offset7, toStartedObjectAt: 7) } if previousLocations.count > 0 { - try builder.insert(propertyIndex: 6, offset: offset6) + try builder.insert(offset: offset6, toStartedObjectAt: 6) } if let currentLoccation = currentLoccation { builder.insert(value: currentLoccation) - try builder.insertCurrentOffsetAsProperty(propertyIndex: 5) + try builder.insertCurrentOffsetAsProperty(toStartedObjectAt: 5) } if addressEntries.count > 0 { - try builder.insert(propertyIndex: 4, offset: offset4) + try builder.insert(offset: offset4, toStartedObjectAt: 4) } if tags.count > 0 { - try builder.insert(propertyIndex: 3, offset: offset3) + try builder.insert(offset: offset3, toStartedObjectAt: 3) } - try builder.insert(propertyIndex: 2, value : gender?.rawValue ?? 0, defaultValue : 0) + try builder.insert(value : gender?.rawValue ?? 0, defaultValue : 0, toStartedObjectAt: 2) if birthday != nil { - try builder.insert(propertyIndex: 1, offset: offset1) + try builder.insert(offset: offset1, toStartedObjectAt: 1) } - try builder.insert(propertyIndex: 0, offset: offset0) + try builder.insert(offset: offset0, toStartedObjectAt: 0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -442,9 +442,9 @@ public extension Date { } } try builder.startObject(numOfProperties: 3) - try builder.insert(propertyIndex: 2, value : year, defaultValue : 0) - try builder.insert(propertyIndex: 1, value : month, defaultValue : 0) - try builder.insert(propertyIndex: 0, value : day, defaultValue : 0) + try builder.insert(value : year, defaultValue : 0, toStartedObjectAt: 2) + try builder.insert(value : month, defaultValue : 0, toStartedObjectAt: 1) + try builder.insert(value : day, defaultValue : 0, toStartedObjectAt: 0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -515,10 +515,10 @@ public extension AddressEntry { let offset1 = try addToByteArray_Address(builder, union: address) try builder.startObject(numOfProperties: 3) if let object = address { - try builder.insert(propertyIndex: 2, offset: offset1) - try builder.insert(propertyIndex: 1, value : unionCase_Address(object), defaultValue : 0) + try builder.insert(offset: offset1, toStartedObjectAt: 2) + try builder.insert(value : unionCase_Address(object), defaultValue : 0, toStartedObjectAt: 1) } - try builder.insert(propertyIndex: 0, value : order, defaultValue : 0) + try builder.insert(value : order, defaultValue : 0, toStartedObjectAt: 0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -588,10 +588,10 @@ public extension PostalAddress { let offset1 = try builder.insert(value: city) let offset0 = try builder.insert(value: country) try builder.startObject(numOfProperties: 4) - try builder.insert(propertyIndex: 3, offset: offset3) - try builder.insert(propertyIndex: 2, value : postalCode, defaultValue : 0) - try builder.insert(propertyIndex: 1, offset: offset1) - try builder.insert(propertyIndex: 0, offset: offset0) + try builder.insert(offset: offset3, toStartedObjectAt: 3) + try builder.insert(value : postalCode, defaultValue : 0, toStartedObjectAt: 2) + try builder.insert(offset: offset1, toStartedObjectAt: 1) + try builder.insert(offset: offset0, toStartedObjectAt: 0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -645,7 +645,7 @@ public extension EmailAddress { } let offset0 = try builder.insert(value: mailto) try builder.startObject(numOfProperties: 1) - try builder.insert(propertyIndex: 0, offset: offset0) + try builder.insert(offset: offset0, toStartedObjectAt: 0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -699,7 +699,7 @@ public extension WebAddress { } let offset0 = try builder.insert(value: url) try builder.startObject(numOfProperties: 1) - try builder.insert(propertyIndex: 0, offset: offset0) + try builder.insert(offset: offset0, toStartedObjectAt: 0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -753,7 +753,7 @@ public extension TelephoneNumber { } let offset0 = try builder.insert(value: number) try builder.startObject(numOfProperties: 1) - try builder.insert(propertyIndex: 0, offset: offset0) + try builder.insert(offset: offset0, toStartedObjectAt: 0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset diff --git a/FlatBuffersSwiftTests/friends_fb.swift b/FlatBuffersSwiftTests/friends_fb.swift index 75d0834..707da3c 100644 --- a/FlatBuffersSwiftTests/friends_fb.swift +++ b/FlatBuffersSwiftTests/friends_fb.swift @@ -134,7 +134,7 @@ public extension PeopleList { } try builder.startObject(numOfProperties: 1) if people.count > 0 { - try builder.insert(propertyIndex: 0, offset: offset0) + try builder.insert(offset: offset0, toStartedObjectAt: 0) } let myOffset = try builder.endObject() if builder.config.uniqueTables { @@ -275,14 +275,14 @@ public extension Friend { let offset0 = try builder.insert(value: name) try builder.startObject(numOfProperties: 6) if let object = lover { - let cursor4 = try builder.insert(propertyIndex: 5, offset: offset4) + let cursor4 = try builder.insert(offset: offset4, toStartedObjectAt: 5) if offset4 == 0 { builder.deferedBindings.append((object: object, cursor: cursor4)) } - try builder.insert(propertyIndex: 4, value : unionCase_Human(object), defaultValue : 0) + try builder.insert(value : unionCase_Human(object), defaultValue : 0, toStartedObjectAt: 4) } if mother != nil { - let cursor3 = try builder.insert(propertyIndex: 3, offset: offset3) + let cursor3 = try builder.insert(offset: offset3, toStartedObjectAt: 3) if offset3 == 0 { if let object = mother { builder.deferedBindings.append((object: object, cursor: cursor3)) @@ -290,7 +290,7 @@ public extension Friend { } } if father != nil { - let cursor2 = try builder.insert(propertyIndex: 2, offset: offset2) + let cursor2 = try builder.insert(offset: offset2, toStartedObjectAt: 2) if offset2 == 0 { if let object = father { builder.deferedBindings.append((object: object, cursor: cursor2)) @@ -298,9 +298,9 @@ public extension Friend { } } if friends.count > 0 { - try builder.insert(propertyIndex: 1, offset: offset1) + try builder.insert(offset: offset1, toStartedObjectAt: 1) } - try builder.insert(propertyIndex: 0, offset: offset0) + try builder.insert(offset: offset0, toStartedObjectAt: 0) let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset @@ -365,7 +365,7 @@ public extension Male { let offset0 = try ref?.addToByteArray(builder) ?? 0 try builder.startObject(numOfProperties: 1) if ref != nil { - let cursor0 = try builder.insert(propertyIndex: 0, offset: offset0) + let cursor0 = try builder.insert(offset: offset0, toStartedObjectAt: 0) if offset0 == 0 { if let object = ref { builder.deferedBindings.append((object: object, cursor: cursor0)) @@ -436,7 +436,7 @@ public extension Female { let offset0 = try ref?.addToByteArray(builder) ?? 0 try builder.startObject(numOfProperties: 1) if ref != nil { - let cursor0 = try builder.insert(propertyIndex: 0, offset: offset0) + let cursor0 = try builder.insert(offset: offset0, toStartedObjectAt: 0) if offset0 == 0 { if let object = ref { builder.deferedBindings.append((object: object, cursor: cursor0))