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/FlatBufferReader.swift b/FlatBuffersSwift/FlatBufferReader.swift deleted file mode 100644 index 8301d93..0000000 --- a/FlatBuffersSwift/FlatBufferReader.swift +++ /dev/null @@ -1,287 +0,0 @@ -// -// Reader.swift -// FBSwift3 -// -// Created by Maxim Zaks on 28.10.16. -// Copyright © 2016 Maxim Zaks. All rights reserved. -// - -import Foundation - -public protocol FBReader { - func fromByteArray(position : Int) throws -> T - func buffer(position : Int, length : Int) throws -> UnsafeBufferPointer - var cache : FBReaderCache? {get} - func isEqual(other : FBReader) -> Bool -} - - -fileprivate enum FBReaderError : Error { - case OutOfBufferBounds - case CanNotSetProperty -} - -public class FBReaderCache { - public var objectPool : [Offset : AnyObject] = [:] - func reset(){ - objectPool.removeAll(keepingCapacity: true) - } - public init(){} -} - -public extension FBReader { - - private func getPropertyOffset(objectOffset : Offset, propertyIndex : Int) -> Int { - guard propertyIndex >= 0 else { - return 0 - } - do { - let offset = Int(objectOffset) - let localOffset : Int32 = try fromByteArray(position: offset) - let vTableOffset : Int = offset - Int(localOffset) - let vTableLength : Int16 = try fromByteArray(position: vTableOffset) - let objectLength : Int16 = try fromByteArray(position: vTableOffset + 2) - let positionInVTable = 4 + propertyIndex * 2 - if(vTableLength<=Int16(positionInVTable)) { - return 0 - } - let propertyStart = vTableOffset + positionInVTable - let propertyOffset : Int16 = try fromByteArray(position: propertyStart) - if(objectLength<=propertyOffset) { - return 0 - } - return Int(propertyOffset) - } catch { - return 0 // Currently don't want to propagate the error - } - } - - public func getOffset(objectOffset : Offset, propertyIndex : Int) -> Offset? { - - let propertyOffset = getPropertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) - if propertyOffset == 0 { - return nil - } - - let position = objectOffset + propertyOffset - do { - let localObjectOffset : Int32 = try fromByteArray(position: Int(position)) - let offset = position + localObjectOffset - - if localObjectOffset == 0 { - return nil - } - return offset - } catch { - return nil - } - - } - - public func getVectorLength(vectorOffset : Offset?) -> Int { - guard let vectorOffset = vectorOffset else { - return 0 - } - let vectorPosition = Int(vectorOffset) - do { - let length2 : Int32 = try fromByteArray(position: vectorPosition) - return Int(length2) - } catch { - return 0 - } - } - - public func getVectorOffsetElement(vectorOffset : Offset?, index : Int) -> Offset? { - guard let vectorOffset = vectorOffset else { - return nil - } - guard index >= 0 else{ - return nil - } - guard index < getVectorLength(vectorOffset: vectorOffset) else { - return nil - } - let valueStartPosition = Int(vectorOffset + MemoryLayout.stride + (index * MemoryLayout.stride)) - do { - let localOffset : Int32 = try fromByteArray(position: valueStartPosition) - if(localOffset == 0){ - return nil - } - return localOffset + valueStartPosition - } catch { - return nil - } - } - - public func getVectorScalarElement(vectorOffset : Offset?, index : Int) -> T? { - guard let vectorOffset = vectorOffset else { - return nil - } - guard index >= 0 else{ - return nil - } - guard index < getVectorLength(vectorOffset: vectorOffset) else { - return nil - } - - let valueStartPosition = Int(vectorOffset + MemoryLayout.stride + (index * MemoryLayout.stride)) - - do { - return try fromByteArray(position: valueStartPosition) as T - } catch { - return nil - } - } - - public func get(objectOffset : Offset, propertyIndex : Int, defaultValue : T) -> T { - let propertyOffset = getPropertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) - if propertyOffset == 0 { - return defaultValue - } - let position = Int(objectOffset + propertyOffset) - do { - return try fromByteArray(position: position) - } catch { - return defaultValue - } - } - - public func get(objectOffset : Offset, propertyIndex : Int) -> T? { - let propertyOffset = getPropertyOffset(objectOffset: objectOffset, propertyIndex: propertyIndex) - if propertyOffset == 0 { - return nil - } - let position = Int(objectOffset + propertyOffset) - do { - return try fromByteArray(position: position) as T - } catch { - return nil - } - } - - public func getStringBuffer(stringOffset : Offset?) -> UnsafeBufferPointer? { - guard let stringOffset = stringOffset else { - return nil - } - let stringPosition = Int(stringOffset) - do { - let stringLength : Int32 = try fromByteArray(position: stringPosition) - let stringCharactersPosition = stringPosition + MemoryLayout.stride - - return try buffer(position: stringCharactersPosition, length: Int(stringLength)) - } catch { - return nil - } - } - - public var rootObjectOffset : Offset? { - do { - return try fromByteArray(position: 0) as Offset - } catch { - return nil - } - } -} - -public struct FBMemoryReader : FBReader { - - private let count : Int - public let cache : FBReaderCache? - private let buffer : UnsafeRawPointer - - public init(buffer : UnsafeRawPointer, count : Int, cache : FBReaderCache? = FBReaderCache()) { - self.buffer = buffer - self.count = count - self.cache = cache - } - - public init(data : Data, cache : FBReaderCache? = FBReaderCache()) { - self.count = data.count - self.cache = cache - var pointer : UnsafePointer! = nil - data.withUnsafeBytes { (u8Ptr: UnsafePointer) in - pointer = u8Ptr - } - self.buffer = UnsafeRawPointer(pointer) - } - - public func fromByteArray(position : Int) throws -> T { - if position + MemoryLayout.stride > count || position < 0 { - throw FBReaderError.OutOfBufferBounds - } - - return buffer.load(fromByteOffset: position, as: T.self) - } - - public func buffer(position : Int, length : Int) throws -> UnsafeBufferPointer { - if Int(position + length) > count { - throw FBReaderError.OutOfBufferBounds - } - let pointer = buffer.advanced(by:position).bindMemory(to: UInt8.self, capacity: length) - return UnsafeBufferPointer.init(start: pointer, count: Int(length)) - } - - public func isEqual(other: FBReader) -> Bool{ - guard let other = other as? FBMemoryReader else { - return false - } - return self.buffer == other.buffer - } -} - -public struct FBFileReader : FBReader { - - private let fileSize : UInt64 - private let fileHandle : FileHandle - public let cache : FBReaderCache? - - public init(fileHandle : FileHandle, cache : FBReaderCache? = FBReaderCache()){ - self.fileHandle = fileHandle - fileSize = fileHandle.seekToEndOfFile() - - self.cache = cache - } - - public func fromByteArray(position : Int) throws -> T { - let seekPosition = UInt64(position) - if seekPosition + UInt64(MemoryLayout.stride) > fileSize { - throw FBReaderError.OutOfBufferBounds - } - fileHandle.seek(toFileOffset: seekPosition) - let data = fileHandle.readData(ofLength:MemoryLayout.stride) - let pointer = UnsafeMutablePointer.allocate(capacity: MemoryLayout.stride) - let t : UnsafeMutableBufferPointer = UnsafeMutableBufferPointer(start: pointer, count: 1) - _ = data.copyBytes(to: t) - if let result = t.baseAddress?.pointee { - pointer.deinitialize() - return result - } - throw FBReaderError.OutOfBufferBounds - } - - public func buffer(position : Int, length : Int) throws -> UnsafeBufferPointer { - if UInt64(position + length) > fileSize { - throw FBReaderError.OutOfBufferBounds - } - fileHandle.seek(toFileOffset: UInt64(position)) - let data = fileHandle.readData(ofLength:Int(length)) - let pointer = UnsafeMutablePointer.allocate(capacity: 1) - let t : UnsafeMutableBufferPointer = UnsafeMutableBufferPointer(start: pointer, count: length) - _ = data.copyBytes(to: t) - pointer.deinitialize() - return UnsafeBufferPointer(start: t.baseAddress, count: length) - } - - public func isEqual(other: FBReader) -> Bool{ - guard let other = other as? FBFileReader else { - return false - } - return self.fileHandle === other.fileHandle - } -} - -postfix operator § - -public postfix func §(value: UnsafeBufferPointer) -> String? { - return String.init(bytesNoCopy: UnsafeMutablePointer(mutating: value.baseAddress!), length: value.count, encoding: String.Encoding.utf8, freeWhenDone: false) -} diff --git a/FlatBuffersSwift/FlatBufferBuilder.swift b/FlatBuffersSwift/FlatBuffersBuilder.swift similarity index 57% rename from FlatBuffersSwift/FlatBufferBuilder.swift rename to FlatBuffersSwift/FlatBuffersBuilder.swift index 583a865..a8b3417 100644 --- a/FlatBuffersSwift/FlatBufferBuilder.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,20 +45,21 @@ public struct FBBuildConfig { } } -public enum FBBuildError : Error { - case ObjectIsNotClosed - case NoOpenObject - case PropertyIndexIsInvalid - case OffsetIsTooBig - case CursorIsInvalid - case BadFileIdentifier - case UnsupportedType +public enum FlatBuffersBuildError : Error { + case objectIsNotClosed + case noOpenObject + case propertyIndexIsInvalid + case offsetIsTooBig + case cursorIsInvalid + case badFileIdentifier + 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,18 +76,41 @@ 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 and returns a Data object initialized from the builder backing store + + - 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 } @@ -100,17 +126,30 @@ public final class FBBuilder { _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){ + /** + Add a scalar value to the buffer + + - parameters: + - value: The value to add to the buffer + */ + public func insert(value : T){ let c = MemoryLayout.stride(ofValue: value) if c > 8 { align(size: 8, additionalBytes: c) @@ -118,51 +157,77 @@ public final class FBBuilder { 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 add 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 insert(offset : Offset?) throws -> Int { guard let offset = offset else { - put(value: Offset(0)) + insert(value: Offset(0)) return cursor } guard offset <= Int32(cursor) else { - throw FBBuildError.OffsetIsTooBig + throw FlatBuffersBuildError.offsetIsTooBig } if offset == Int32(0) { - put(value: Offset(0)) + insert(value: Offset(0)) return cursor } align(size: 4, additionalBytes: 0) let _offset = Int32(cursor) - offset + MemoryLayout.stride; - put(value: _offset) + insert(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 FBBuildError.OffsetIsTooBig + throw FlatBuffersBuildError.offsetIsTooBig } guard jumpCursor <= cursor else { - throw FBBuildError.CursorIsInvalid + throw FlatBuffersBuildError.cursorIsInvalid } let _offset = Int32(jumpCursor) - offset; _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) } - public func openObject(numOfProperties : Int) throws { + /** + Start an object construction sequence + + - parameters: + - numOfProperties: The number of properties we will update + */ + public func startObject(numOfProperties : Int) throws { guard objectStart == -1 && vectorNumElems == -1 else { - throw FBBuildError.ObjectIsNotClosed + throw FlatBuffersBuildError.objectIsNotClosed } currentVTable.removeAll(keepingCapacity: true) currentVTable.reserveCapacity(numOfProperties) @@ -172,51 +237,78 @@ public final class FBBuilder { objectStart = Int32(cursor) } + /** + Add an offset into the buffer for the currently open 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 insert(offset : Offset, toStartedObjectAt propertyIndex : Int) 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) + _ = try insert(offset: offset) currentVTable[propertyIndex] = Int32(cursor) return cursor } - - public func addPropertyToOpenObject(propertyIndex : Int, value : T, defaultValue : T) throws { + + /** + Add a scalar into the buffer for the currently open 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 insert(value : T, defaultValue : T, toStartedObjectAt 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 } if(config.forceDefaults == false && value == defaultValue) { return } - put(value: value) + insert(value: value) currentVTable[propertyIndex] = Int32(cursor) } - public func addCurrentOffsetAsPropertyToOpenObject(propertyIndex : Int) throws { + /** + Add the current cursor position into the buffer for the currently open object + + - parameters: + - propertyIndex: The index of the property to update + */ + public func insertCurrentOffsetAsProperty(toStartedObjectAt 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 { + + /** + Close the current open object. + */ + public func endObject() throws -> Offset { guard objectStart > -1 else { - throw FBBuildError.NoOpenObject + 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 @@ -226,14 +318,14 @@ public final class FBBuilder { 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); + insert(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 + 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 @@ -267,31 +359,50 @@ public final class FBBuilder { let indexLocation = cursor - vtableloc - put(value: Int32(foundVTableOffset), at: indexLocation) + update(value: Int32(foundVTableOffset), at: indexLocation) objectStart = -1 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 { - throw FBBuildError.ObjectIsNotClosed + throw FlatBuffersBuildError.objectIsNotClosed } vectorNumElems = Int32(count) } + /** + Finish vector update operation + */ public func endVector() -> Offset { - put(value: vectorNumElems) + insert(value: vectorNumElems) vectorNumElems = -1 return Int32(cursor) } private var stringCache : [String:Offset] = [:] - public func createString(value : String?) throws -> Offset { + + /** + 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 insert(value : String?) throws -> Offset { guard objectStart == -1 && vectorNumElems == -1 else { - throw FBBuildError.ObjectIsNotClosed + throw FlatBuffersBuildError.objectIsNotClosed } guard let value = value else { return 0 @@ -307,20 +418,20 @@ public final class FBBuilder { 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) + insert(value: c) } - put(value: Int32(length - 1)) + insert(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) + insert(value: c) } - put(value: Int32(length)) + insert(value: Int32(length)) } let o = Offset(cursor) @@ -332,10 +443,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,10 +455,10 @@ 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) + insert(value: c) } } else { align(size: minalign, additionalBytes: prefixLength) @@ -355,6 +466,6 @@ public final class FBBuilder { let v = (Int32(cursor + 4) - offset) - put(value: v) + insert(value: v) } } diff --git a/FlatBuffersSwift/FlatBuffersReader.swift b/FlatBuffersSwift/FlatBuffersReader.swift new file mode 100644 index 0000000..c70670c --- /dev/null +++ b/FlatBuffersSwift/FlatBuffersReader.swift @@ -0,0 +1,413 @@ +// +// 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 FlatBuffersReader { + var cache : FlatBuffersReaderCache? {get} + + /** + 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 scalar(at offset: Int) throws -> T + + /** + 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 : FlatBuffersReader) -> Bool +} + + +fileprivate enum FlatBuffersReaderError : Error { + case outOfBufferBounds /// Trying to address outside of the bounds of the underlying buffer + case canNotSetProperty +} + +public class FlatBuffersReaderCache { + public var objectPool : [Offset : AnyObject] = [:] + func reset(){ + objectPool.removeAll(keepingCapacity: true) + } + public init(){} +} + +public extension FlatBuffersReader { + /** + 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 propertyOffset(objectOffset : Offset, propertyIndex : Int) -> Int { + guard propertyIndex >= 0 else { + return 0 + } + do { + let offset = Int(objectOffset) + let localOffset : Int32 = try scalar(at: offset) + let vTableOffset : Int = offset - Int(localOffset) + let vTableLength : Int16 = try scalar(at: vTableOffset) + let objectLength : Int16 = try scalar(at: vTableOffset + 2) + let positionInVTable = 4 + propertyIndex * 2 + if (vTableLength<=Int16(positionInVTable)) { + return 0 + } + let propertyStart = vTableOffset + positionInVTable + let propOffset : Int16 = try scalar(at: propertyStart) + if (objectLength<=propOffset) { + return 0 + } + return Int(propOffset) + } catch { + return 0 // Currently don't want to propagate the error + } + } + + /** + 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) + if propOffset == 0 { + return nil + } + + let position = objectOffset + propOffset + do { + let localObjectOffset : Int32 = try scalar(at: Int(position)) + let offset = position + localObjectOffset + + if localObjectOffset == 0 { + return nil + } + return offset + } catch { + return nil + } + + } + + /** + 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 + } + let vectorPosition = Int(vectorOffset) + do { + let length2 : Int32 = try scalar(at: vectorPosition) + return Int(length2) + } catch { + return 0 + } + } + + /** + 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 + } + guard index >= 0 else{ + return nil + } + guard index < vectorElementCount(vectorOffset: vectorOffset) else { + return nil + } + let valueStartPosition = Int(vectorOffset + MemoryLayout.stride + (index * MemoryLayout.stride)) + do { + let localOffset : Int32 = try scalar(at: valueStartPosition) + if(localOffset == 0){ + return nil + } + return localOffset + valueStartPosition + } catch { + return nil + } + } + + /** + 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 + } + guard index >= 0 else{ + return nil + } + guard index < vectorElementCount(vectorOffset: vectorOffset) else { + return nil + } + + let valueStartPosition = Int(vectorOffset + MemoryLayout.stride + (index * MemoryLayout.stride)) + + do { + return try scalar(at: valueStartPosition) as T + } catch { + return nil + } + } + + /** + 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 { + return defaultValue + } + let position = Int(objectOffset + propOffset) + do { + return try scalar(at: position) + } catch { + return defaultValue + } + } + + /** + Retrieve a scalar optional value (return nil 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 { + return nil + } + let position = Int(objectOffset + propOffset) + do { + return try scalar(at: position) as T + } catch { + return nil + } + } + + /** + 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 + } + let stringPosition = Int(stringOffset) + do { + let stringLength : Int32 = try scalar(at: stringPosition) + let stringCharactersPosition = stringPosition + MemoryLayout.stride + + return try bytes(at: stringCharactersPosition, length: Int(stringLength)) + } catch { + return nil + } + } + + /** + 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 + } catch { + return nil + } + } +} + +/// A FlatBuffers reader subclass that by default reads directly from a memory buffer, but also supports initialization from Data objects for convenience +public struct FlatBuffersMemoryReader : FlatBuffersReader { + + private let count : Int + public let cache : FlatBuffersReaderCache? + private let buffer : UnsafeRawPointer + + /** + 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 FlatBuffers reader ready for use. + */ + public init(buffer : UnsafeRawPointer, count : Int, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()) { + self.buffer = buffer + self.count = count + self.cache = cache + } + + /** + 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 FlatBuffers reader ready for use. + */ + public init(data : Data, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()) { + self.count = data.count + self.cache = cache + var pointer : UnsafePointer! = nil + data.withUnsafeBytes { (u8Ptr: UnsafePointer) in + pointer = u8Ptr + } + self.buffer = UnsafeRawPointer(pointer) + } + + public func scalar(at offset: Int) throws -> T { + if offset + MemoryLayout.stride > count || offset < 0 { + throw FlatBuffersReaderError.outOfBufferBounds + } + + return buffer.load(fromByteOffset: offset, as: T.self) + } + + public func bytes(at offset : Int, length : Int) throws -> UnsafeBufferPointer { + if Int(offset + length) > count { + 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: FlatBuffersReader) -> Bool{ + guard let other = other as? FlatBuffersMemoryReader else { + return false + } + return self.buffer == other.buffer + } +} + +/// A FlatBuffers reader subclass that reads directly from a file handle +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() + + self.cache = cache + } + + public func scalar(at offset: Int) throws -> T { + let seekPosition = UInt64(offset) + if seekPosition + UInt64(MemoryLayout.stride) > fileSize { + throw FlatBuffersReaderError.outOfBufferBounds + } + fileHandle.seek(toFileOffset: seekPosition) + let data = fileHandle.readData(ofLength:MemoryLayout.stride) + let pointer = UnsafeMutablePointer.allocate(capacity: MemoryLayout.stride) + let t : UnsafeMutableBufferPointer = UnsafeMutableBufferPointer(start: pointer, count: 1) + _ = data.copyBytes(to: t) + if let result = t.baseAddress?.pointee { + pointer.deinitialize() + return result + } + throw FlatBuffersReaderError.outOfBufferBounds + } + + public func bytes(at offset : Int, length : Int) throws -> UnsafeBufferPointer { + if UInt64(offset + length) > fileSize { + throw FlatBuffersReaderError.outOfBufferBounds + } + 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) + _ = data.copyBytes(to: t) + pointer.deinitialize() + return UnsafeBufferPointer(start: t.baseAddress, count: length) + } + + public func isEqual(other: FlatBuffersReader) -> Bool{ + guard let other = other as? FlatBuffersFileReader else { + return false + } + return self.fileHandle === other.fileHandle + } +} + +postfix operator § + +public postfix func §(value: UnsafeBufferPointer) -> String? { + return String.init(bytesNoCopy: UnsafeMutablePointer(mutating: value.baseAddress!), length: value.count, encoding: String.Encoding.utf8, freeWhenDone: false) +} diff --git a/FlatBuffersSwiftTests/BuilderTest.swift b/FlatBuffersSwiftTests/BuilderTest.swift index d500b9e..a4ba800 100644 --- a/FlatBuffersSwiftTests/BuilderTest.swift +++ b/FlatBuffersSwiftTests/BuilderTest.swift @@ -11,26 +11,26 @@ 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() } 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.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.put(value: Int16(-12)) - builder.put(value: Int16(12)) - builder.put(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.put(value: Int32(-12)) - builder.put(value: Int32(12)) - builder.put(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.put(value: Int(-12)) - builder.put(value: Int(12)) - builder.put(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.put(value: Float32(12.5)) - builder.put(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.put(value: Float64(12.5)) - builder.put(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.put(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.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.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.put(value: Int16(45)) - builder.put(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.createString(value: "maxim") + let o = try!builder.insert(value: "maxim") XCTAssertEqual(o, 12) @@ -237,9 +237,9 @@ 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") + let o = try!builder.insert(value: "maxim") XCTAssertEqual(o, 12) @@ -257,8 +257,8 @@ class BuilderTest: XCTestCase { } func testStringWithPrimitives(){ - builder.put(value: Int16(25)) - let o = try!builder.createString(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.put(value: Int16(25)) - let o = try!builder.createString(value: "maxim") - builder.put(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.put(value: Int16(25)) - let o = try!builder.createString(value: "maxim") - builder.put(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,11 +350,11 @@ 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) - let o = try!builder.closeObject() + let s = try!builder.insert(value: "maxim") + try!builder.startObject(numOfProperties: 2) + 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) 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.addPropertyToOpenObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: s) - try!builder.addPropertyOffsetToOpenObject(propertyIndex: 2, offset: s2) - let o = try!builder.closeObject() + let s = try!builder.insert(value: "maxim") + let s2 = try!builder.insert(value: "maxim") + try!builder.startObject(numOfProperties: 3) + 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) assertByte(with: 16) @@ -449,14 +449,14 @@ class BuilderTest: XCTestCase { } func testObjectWithoutNameReuseAndAge() { - builder = FBBuilder(config: FBBuildConfig(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.addPropertyToOpenObject(propertyIndex: 1, value: UInt8(35), defaultValue: 0) - try!builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: s) - try!builder.addPropertyOffsetToOpenObject(propertyIndex: 2, offset: s2) - let o = try!builder.closeObject() + builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: false, uniqueTables: true, uniqueVTables: true, forceDefaults: false, nullTerminatedUTF8: false)) + let s = try!builder.insert(value: "maxim") + let s2 = try!builder.insert(value: "maxim") + try!builder.startObject(numOfProperties: 3) + 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) assertByte(with: 16) @@ -516,13 +516,13 @@ 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) - let o = try!builder.closeObject() - try!builder.replaceOffset(offset: o, atCursor: cursor) + let s = try!builder.insert(value: "maxim") + try!builder.startObject(numOfProperties: 3) + 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) assertByte(with: 16) @@ -571,11 +571,11 @@ 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)) - try! builder.addCurrentOffsetAsPropertyToOpenObject(propertyIndex: 0) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 2) + try!builder.insert(value: UInt8(35), defaultValue: 0, toStartedObjectAt:1) + builder.insert(value: S1(i1: 45, i2: 78)) + try! builder.insertCurrentOffsetAsProperty(toStartedObjectAt: 0) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 12) @@ -610,12 +610,12 @@ class BuilderTest: XCTestCase { func testObjectWithVectorOfInt16(){ try!builder.startVector(count: 2, elementSize: 2) - builder.put(value: Int16(15)) - builder.put(value: Int16(19)) + builder.insert(value: Int16(15)) + builder.insert(value: Int16(19)) let v = builder.endVector() - try!builder.openObject(numOfProperties: 1) - try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 1) + try! builder.insert(offset: v, toStartedObjectAt: 0) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 12) @@ -652,12 +652,12 @@ class BuilderTest: XCTestCase { func testObjectWithVectorOfBoolean(){ try!builder.startVector(count: 2, elementSize: 1) - builder.put(value: true) - builder.put(value: false) + builder.insert(value: true) + builder.insert(value: false) let v = builder.endVector() - try!builder.openObject(numOfProperties: 1) - try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 1) + try! builder.insert(offset: v, toStartedObjectAt: 0) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 12) @@ -693,12 +693,12 @@ 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.insert(value: S1(i1: 12, i2: 19)) + builder.insert(value: S1(i1: 13, i2: 17)) let v = builder.endVector() - try!builder.openObject(numOfProperties: 1) - try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 1) + try! builder.insert(offset: v, toStartedObjectAt: 0) + 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.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) - 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) - let o2 = try!builder.closeObject() + try!builder.startObject(numOfProperties: 3) + 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(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) - try!builder.putOffset(offset: o1) - try!builder.putOffset(offset: o2) + try!builder.insert(offset: o1) + try!builder.insert(offset: o2) let v = builder.endVector() - try!builder.openObject(numOfProperties: 1) - try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 1) + try! builder.insert(offset: v, toStartedObjectAt: 0) + 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.addPropertyToOpenObject(propertyIndex: 0, value: Int16(12), defaultValue: 0) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: Int16(13), defaultValue: 0) - let o1 = try!builder.closeObject() + try!builder.startObject(numOfProperties: 2) + 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.openObject(numOfProperties: 2) - try!builder.addPropertyToOpenObject(propertyIndex: 0, value: Int16(22), defaultValue: 0) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: Int16(23), defaultValue: 0) - let o2 = try!builder.closeObject() + try!builder.startObject(numOfProperties: 2) + 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) - try!builder.putOffset(offset: o1) - try!builder.putOffset(offset: o2) + try!builder.insert(offset: o1) + try!builder.insert(offset: o2) let v = builder.endVector() - try!builder.openObject(numOfProperties: 1) - try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 1) + try! builder.insert(offset: v, toStartedObjectAt: 0) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 12) @@ -939,25 +939,25 @@ class BuilderTest: XCTestCase { } func testObjectWithVectorToOtherTwoObjectsAndVTableWithoutReuese(){ - builder = FBBuilder(config: FBBuildConfig(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) - let o1 = try!builder.closeObject() + builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: false, forceDefaults: false, nullTerminatedUTF8: false)) + try!builder.startObject(numOfProperties: 2) + 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.openObject(numOfProperties: 2) - try!builder.addPropertyToOpenObject(propertyIndex: 0, value: Int16(22), defaultValue: 0) - try!builder.addPropertyToOpenObject(propertyIndex: 1, value: Int16(23), defaultValue: 0) - let o2 = try!builder.closeObject() + try!builder.startObject(numOfProperties: 2) + 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) - try!builder.putOffset(offset: o1) - try!builder.putOffset(offset: o2) + try!builder.insert(offset: o1) + try!builder.insert(offset: o2) let v = builder.endVector() - try!builder.openObject(numOfProperties: 1) - try! builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: v) - let o = try!builder.closeObject() + try!builder.startObject(numOfProperties: 1) + try! builder.insert(offset: v, toStartedObjectAt: 0) + let o = try!builder.endObject() try!builder.finish(offset: o, fileIdentifier: nil) assertByte(with: 12) @@ -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.insert(offset: nil) + let c2 = try!builder.insert(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.startObject(numOfProperties: 1) + try! builder.insert(offset: v, toStartedObjectAt: 0) + let o = try!builder.endObject() - 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.insert(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 } @@ -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.addPropertyToOpenObject(propertyIndex: 0, value: 1, defaultValue: 0) + try builder.insert(value: 1, defaultValue: 0, toStartedObjectAt: 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.addPropertyToOpenObject(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.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: 0) + try builder.insert(offset: 0, toStartedObjectAt: 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.addPropertyOffsetToOpenObject(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.addCurrentOffsetAsPropertyToOpenObject(propertyIndex: 0) + try builder.insertCurrentOffsetAsProperty(toStartedObjectAt: 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.addCurrentOffsetAsPropertyToOpenObject(propertyIndex: 1) + try builder.insertCurrentOffsetAsProperty(toStartedObjectAt: 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.insert(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.insert(value: nil) XCTAssertEqual(c, 0) } 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.closeObject() + try!builder.startObject(numOfProperties: 2) + 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) assertByte(with: 0) @@ -1292,11 +1292,11 @@ class BuilderTest: XCTestCase { } func testDefaultValuesForced(){ - builder = FBBuilder(config: FBBuildConfig(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.closeObject() + builder = FlatBuffersBuilder(config: FlatBuffersBuildConfig(initialCapacity: 1, uniqueStrings: true, uniqueTables: true, uniqueVTables: true, forceDefaults: true, nullTerminatedUTF8: false)) + try!builder.startObject(numOfProperties: 2) + 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) assertByte(with: 0) @@ -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/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 7773881..ff8539e 100644 --- a/FlatBuffersSwiftTests/FBReaderTest.swift +++ b/FlatBuffersSwiftTests/FBReaderTest.swift @@ -9,21 +9,21 @@ import XCTest @testable import FlatBuffersSwift -class FBReaderTest: XCTestCase { +class FlatBuffersReaderTest: XCTestCase { func testReadDirect() { let data = createSimpleObject() - let reader = FBMemoryReader(data: data) + let reader = FlatBuffersMemoryReader(data: data) 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) + let stringBuffer = reader.stringBuffer(stringOffset: stringOffset) XCTAssertEqual(stringBuffer?§, "max") let booleanValue1 : Bool? = reader.get(objectOffset: objectOffset!, propertyIndex: 0) @@ -39,52 +39,52 @@ class FBReaderTest: XCTestCase { func testReadDirectWithVector() { let data = createObjectWithVectors() - let reader = FBMemoryReader(data: data) + let reader = FlatBuffersMemoryReader(data: data) 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.getVectorLength(vectorOffset: sVectorOffset) + let sVectorLength = reader.vectorElementCount(vectorOffset: sVectorOffset) XCTAssertEqual(sVectorLength, 3) - let sOffset1 = reader.getVectorOffsetElement(vectorOffset: sVectorOffset!, index: 0) + let sOffset1 = reader.vectorElementOffset(vectorOffset: sVectorOffset!, index: 0) XCTAssertEqual(sOffset1, 48) - let sOffset2 = reader.getVectorOffsetElement(vectorOffset: sVectorOffset!, index: 1) + let sOffset2 = reader.vectorElementOffset(vectorOffset: sVectorOffset!, index: 1) XCTAssertEqual(sOffset2, 56) - let sOffset3 = reader.getVectorOffsetElement(vectorOffset: sVectorOffset!, index: 2) + 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.getOffset(objectOffset: objectOffset!, propertyIndex: 1) + let bVectorOffset = reader.offset(objectOffset: objectOffset!, propertyIndex: 1) XCTAssertEqual(bVectorOffset, 24) - let bVectorLength = reader.getVectorLength(vectorOffset: bVectorOffset) + let bVectorLength = reader.vectorElementCount(vectorOffset: bVectorOffset) XCTAssertEqual(bVectorLength, 2) - let b1 : Bool? = reader.getVectorScalarElement(vectorOffset: bVectorOffset!, index: 0) + let b1 : Bool? = reader.vectorElementScalar(vectorOffset: bVectorOffset!, index: 0) XCTAssertEqual(b1, false) - let b2 : Bool? = reader.getVectorScalarElement(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 FBReaderTest: 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 FBReaderTest: 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,10 +114,10 @@ class FBReaderTest: XCTestCase { func testReadPropertyOffestWithWrongPropertyIndex() { let data = createObjectWithVectors() - let reader = FBMemoryReader(data: data) + let reader = FlatBuffersMemoryReader(data: data) let root = reader.rootObjectOffset - let o = reader.getOffset(objectOffset: root!, propertyIndex: -1) + let o = reader.offset(objectOffset: root!, propertyIndex: -1) XCTAssertNil(o) } @@ -125,7 +125,7 @@ class FBReaderTest: 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 FBReaderTest: XCTestCase { } fh.write(data) - let reader = FBFileReader(fileHandle : fh) + let reader = FlatBuffersFileReader(fileHandle : fh) 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) + let stringBuffer = reader.stringBuffer(stringOffset: stringOffset) XCTAssertEqual(stringBuffer?§, "max") let booleanValue1 : Bool? = reader.get(objectOffset: objectOffset!, propertyIndex: 0) @@ -176,45 +176,45 @@ class FBReaderTest: XCTestCase { } fh.write(data) - let reader = FBFileReader(fileHandle : fh) + let reader = FlatBuffersFileReader(fileHandle : fh) 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.getVectorLength(vectorOffset: sVectorOffset) + let sVectorLength = reader.vectorElementCount(vectorOffset: sVectorOffset) XCTAssertEqual(sVectorLength, 3) - let sOffset1 = reader.getVectorOffsetElement(vectorOffset: sVectorOffset!, index: 0) + let sOffset1 = reader.vectorElementOffset(vectorOffset: sVectorOffset!, index: 0) XCTAssertEqual(sOffset1, 48) - let sOffset2 = reader.getVectorOffsetElement(vectorOffset: sVectorOffset!, index: 1) + let sOffset2 = reader.vectorElementOffset(vectorOffset: sVectorOffset!, index: 1) XCTAssertEqual(sOffset2, 56) - let sOffset3 = reader.getVectorOffsetElement(vectorOffset: sVectorOffset!, index: 2) + 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.getOffset(objectOffset: objectOffset!, propertyIndex: 1) + let bVectorOffset = reader.offset(objectOffset: objectOffset!, propertyIndex: 1) XCTAssertEqual(bVectorOffset, 24) - let bVectorLength = reader.getVectorLength(vectorOffset: bVectorOffset) + let bVectorLength = reader.vectorElementCount(vectorOffset: bVectorOffset) XCTAssertEqual(bVectorLength, 2) - let b1 : Bool? = reader.getVectorScalarElement(vectorOffset: bVectorOffset!, index: 0) + let b1 : Bool? = reader.vectorElementScalar(vectorOffset: bVectorOffset!, index: 0) XCTAssertEqual(b1, false) - let b2 : Bool? = reader.getVectorScalarElement(vectorOffset: bVectorOffset!, index: 1) + let b2 : Bool? = reader.vectorElementScalar(vectorOffset: bVectorOffset!, index: 1) XCTAssertEqual(b2, true) } @@ -229,7 +229,7 @@ class FBReaderTest: XCTestCase { } fh.write(Data(bytes: data)) - let reader = FBFileReader(fileHandle : fh) + let reader = FlatBuffersFileReader(fileHandle : fh) let objectOffset = reader.rootObjectOffset XCTAssertNil(objectOffset) @@ -237,7 +237,7 @@ class FBReaderTest: XCTestCase { } func createSimpleObject() -> Data { - let fbb = FBBuilder(config:FBBuildConfig( + let fbb = FlatBuffersBuilder(config:FlatBuffersBuildConfig( initialCapacity : 1, uniqueStrings : true, uniqueTables : true, @@ -245,19 +245,19 @@ class FBReaderTest: XCTestCase { forceDefaults : false, nullTerminatedUTF8 : false) ) - 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) - let oOffset = try! fbb.closeObject() + let sOffset = try! fbb.insert(value: "max") + try! fbb.startObject(numOfProperties: 3) + 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.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, @@ -265,27 +265,27 @@ class FBReaderTest: 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.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.putOffset(offset: sOffset1) - try!fbb.putOffset(offset: sOffset2) - try!fbb.putOffset(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.put(value: true) - fbb.put(value: false) + fbb.insert(value: true) + fbb.insert(value: false) let bVectorOffset = fbb.endVector() - try! fbb.openObject(numOfProperties: 2) - try! fbb.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: sVectorOffset) - try! fbb.addPropertyOffsetToOpenObject(propertyIndex: 1, offset: bVectorOffset) - let oOffset = try! fbb.closeObject() + try! fbb.startObject(numOfProperties: 2) + 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.data + let data = fbb.makeData return data } 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 ad2f2a0..80f3e33 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.getVectorLength(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.getVectorOffsetElement(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,29 +42,29 @@ public extension ContactList { } } public extension ContactList { - public static func from(data : Data, cache : FBReaderCache? = FBReaderCache()) -> ContactList? { - let reader = FBMemoryReader(data: data, cache: cache) + public static func from(data : Data, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()) -> ContactList? { + let reader = FlatBuffersMemoryReader(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) } } 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 } } -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.getVectorLength(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.getVectorOffsetElement(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 @@ -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 @@ -114,17 +114,17 @@ 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.insert(offset: offsets[index]) index -= 1 } offset1 = builder.endVector() } - try builder.openObject(numOfProperties: 2) + try builder.startObject(numOfProperties: 2) if entries.count > 0 { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 1, offset: offset1) + try builder.insert(offset: offset1, toStartedObjectAt:1) } - try builder.addPropertyToOpenObject(propertyIndex: 0, value : lastModified, defaultValue : 0) - let myOffset = try builder.closeObject() + try builder.insert(value : lastModified, defaultValue : 0, toStartedObjectAt: 0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -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.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.getOffset(objectOffset: objectOffset, propertyIndex: 3) - let length_tags = reader.getVectorLength(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.getVectorOffsetElement(vectorOffset: offset_tags, index: index))?§ + let element = reader.stringBuffer(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.getVectorLength(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.getVectorOffsetElement(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.getVectorLength(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.vectorElementScalar(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.getVectorLength(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.vectorElementScalar(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.stringBuffer(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.getVectorLength(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.getVectorOffsetElement(vectorOffset: offsetList, index: index) { - return reader.getStringBuffer(stringOffset: ofs) + let offsetList = reader.offset(objectOffset: myOffset, propertyIndex: 3) + if let ofs = reader.vectorElementOffset(vectorOffset: offsetList, index: index) { + return reader.stringBuffer(stringOffset: ofs) } return nil } public var addressEntriesCount : Int { - return reader.getVectorLength(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.getVectorOffsetElement(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.getVectorLength(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.vectorElementScalar(vectorOffset: offsetList, index: index) return result } public var moodsCount : Int { - return reader.getVectorLength(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.vectorElementScalar(vectorOffset: offsetList, index: index) else { return nil } return Mood(rawValue: rawValue) @@ -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 @@ -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.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.put(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.putOffset(offset: offsets[index]) + try builder.insert(offset: offsets[index]) index -= 1 } offset4 = builder.endVector() @@ -341,42 +341,42 @@ 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.insert(value: tags[index]) index -= 1 } try builder.startVector(count: tags.count, elementSize: MemoryLayout.stride) index = tags.count - 1 while(index >= 0){ - try builder.putOffset(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.createString(value: name) - try builder.openObject(numOfProperties: 8) + let offset0 = try builder.insert(value: name) + try builder.startObject(numOfProperties: 8) if moods.count > 0 { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 7, offset: offset7) + try builder.insert(offset: offset7, toStartedObjectAt: 7) } if previousLocations.count > 0 { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 6, offset: offset6) + try builder.insert(offset: offset6, toStartedObjectAt: 6) } if let currentLoccation = currentLoccation { - builder.put(value: currentLoccation) - try builder.addCurrentOffsetAsPropertyToOpenObject(propertyIndex: 5) + builder.insert(value: currentLoccation) + try builder.insertCurrentOffsetAsProperty(toStartedObjectAt: 5) } if addressEntries.count > 0 { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 4, offset: offset4) + try builder.insert(offset: offset4, toStartedObjectAt: 4) } if tags.count > 0 { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 3, offset: offset3) + try builder.insert(offset: offset3, toStartedObjectAt: 3) } - try builder.addPropertyToOpenObject(propertyIndex: 2, value : gender?.rawValue ?? 0, defaultValue : 0) + try builder.insert(value : gender?.rawValue ?? 0, defaultValue : 0, toStartedObjectAt: 2) if birthday != nil { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 1, offset: offset1) + try builder.insert(offset: offset1, toStartedObjectAt: 1) } - try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) - let myOffset = try builder.closeObject() + try builder.insert(offset: offset0, toStartedObjectAt: 0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -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){ @@ -435,17 +435,17 @@ 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 } } - 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) - let myOffset = try builder.closeObject() + try builder.startObject(numOfProperties: 3) + 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 } @@ -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){ @@ -506,20 +506,20 @@ 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 } } let offset1 = try addToByteArray_Address(builder, union: address) - try builder.openObject(numOfProperties: 3) + try builder.startObject(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.insert(offset: offset1, toStartedObjectAt: 2) + try builder.insert(value : unionCase_Address(object), defaultValue : 0, toStartedObjectAt: 1) } - try builder.addPropertyToOpenObject(propertyIndex: 0, value : order, defaultValue : 0) - let myOffset = try builder.closeObject() + try builder.insert(value : order, defaultValue : 0, toStartedObjectAt: 0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -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,47 +552,47 @@ 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.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.getOffset(objectOffset: objectOffset, propertyIndex: 3))?§ + _result.streetAndNumber = reader.stringBuffer(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.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.getOffset(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 { 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 } } - 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.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) - let myOffset = try builder.closeObject() + 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.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 } @@ -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,34 +619,34 @@ 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.stringBuffer(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.stringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var hashValue: Int { return Int(myOffset) } } public func ==(t1 : EmailAddress_Direct, t2 : EmailAddress_Direct) -> Bool { 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 } } - let offset0 = try builder.createString(value: mailto) - try builder.openObject(numOfProperties: 1) - try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) - let myOffset = try builder.closeObject() + let offset0 = try builder.insert(value: mailto) + try builder.startObject(numOfProperties: 1) + try builder.insert(offset: offset0, toStartedObjectAt: 0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -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,34 +673,34 @@ 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.stringBuffer(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.stringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var hashValue: Int { return Int(myOffset) } } 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 } } - let offset0 = try builder.createString(value: url) - try builder.openObject(numOfProperties: 1) - try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) - let myOffset = try builder.closeObject() + let offset0 = try builder.insert(value: url) + try builder.startObject(numOfProperties: 1) + try builder.insert(offset: offset0, toStartedObjectAt: 0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -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,34 +727,34 @@ 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.stringBuffer(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.stringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var hashValue: Int { return Int(myOffset) } } public func ==(t1 : TelephoneNumber_Direct, t2 : TelephoneNumber_Direct) -> Bool { 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 } } - let offset0 = try builder.createString(value: number) - try builder.openObject(numOfProperties: 1) - try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) - let myOffset = try builder.closeObject() + let offset0 = try builder.insert(value: number) + try builder.startObject(numOfProperties: 1) + try builder.insert(offset: offset0, toStartedObjectAt: 0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -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 { @@ -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 5460696..707da3c 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.getVectorLength(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.getVectorOffsetElement(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,30 +39,30 @@ public extension PeopleList { } } public extension PeopleList { - public static func from(data : Data, cache : FBReaderCache? = FBReaderCache()) -> PeopleList? { - let reader = FBMemoryReader(data: data, cache: cache) + public static func from(data : Data, cache : FlatBuffersReaderCache? = FlatBuffersReaderCache()) -> PeopleList? { + let reader = FlatBuffersMemoryReader(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) } } 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 } } -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.getVectorLength(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.getVectorOffsetElement(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 @@ -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 @@ -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.insert(offset: offsets[index]) if offsets[index] == 0 { deferedBindingCursors[index] = cursor } @@ -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.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) + try builder.insert(offset: offset0, toStartedObjectAt: 0) } - let myOffset = try builder.closeObject() + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -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.getVectorLength(vectorOffset: offset_friends) + _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){ 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.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.stringBuffer(stringOffset: reader.offset(objectOffset: myOffset, propertyIndex:0)) } } public var friendsCount : Int { - return reader.getVectorLength(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.getVectorOffsetElement(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 @@ -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 @@ -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.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.createString(value: name) - try builder.openObject(numOfProperties: 6) + let offset0 = try builder.insert(value: name) + try builder.startObject(numOfProperties: 6) if let object = lover { - let cursor4 = try builder.addPropertyOffsetToOpenObject(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.addPropertyToOpenObject(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.addPropertyOffsetToOpenObject(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.addPropertyOffsetToOpenObject(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,10 +298,10 @@ public extension Friend { } } if friends.count > 0 { - try builder.addPropertyOffsetToOpenObject(propertyIndex: 1, offset: offset1) + try builder.insert(offset: offset1, toStartedObjectAt: 1) } - try builder.addPropertyOffsetToOpenObject(propertyIndex: 0, offset: offset0) - let myOffset = try builder.closeObject() + try builder.insert(offset: offset0, toStartedObjectAt: 0) + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -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 @@ -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 @@ -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.addPropertyOffsetToOpenObject(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)) } } } - let myOffset = try builder.closeObject() + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -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 @@ -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 @@ -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.addPropertyOffsetToOpenObject(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)) } } } - let myOffset = try builder.closeObject() + let myOffset = try builder.endObject() if builder.config.uniqueTables { builder.cache[ObjectIdentifier(self)] = myOffset } @@ -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 { @@ -493,20 +493,20 @@ 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) - 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 } }