From 666800da3644e75b3dc0cdc56402f104fe201655 Mon Sep 17 00:00:00 2001 From: mustiikhalil Date: Thu, 4 Jun 2020 19:14:18 +0300 Subject: Adds bool support in structs + updates grpc support + CI upgrades (#5943) --- swift/Sources/FlatBuffers/Constants.swift | 8 +++ swift/Sources/FlatBuffers/FlatBufferBuilder.swift | 19 +------ swift/Sources/FlatBuffers/FlatBufferObject.swift | 66 ---------------------- swift/Sources/FlatBuffers/Mutable.swift | 68 +++++++++++++++++++++++ 4 files changed, 77 insertions(+), 84 deletions(-) create mode 100644 swift/Sources/FlatBuffers/Mutable.swift (limited to 'swift') diff --git a/swift/Sources/FlatBuffers/Constants.swift b/swift/Sources/FlatBuffers/Constants.swift index b17e87c6..03ea398b 100644 --- a/swift/Sources/FlatBuffers/Constants.swift +++ b/swift/Sources/FlatBuffers/Constants.swift @@ -52,6 +52,14 @@ extension Float32: Scalar { } } +extension Bool: Scalar { + public var convertedEndian: UInt8 { + return self == true ? 1 : 0 + } + + public typealias NumericValue = UInt8 +} + extension Int: Scalar { public typealias NumericValue = Int } diff --git a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift index 77f25b31..cd2523f9 100644 --- a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift +++ b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift @@ -270,24 +270,6 @@ public struct FlatBufferBuilder { return push(element: Int32(len)) } - /// Creates a vector of type Bool in the buffer - /// - Parameter elements: elements to be written into the buffer - /// - returns: Offset of the vector - mutating public func createVector(_ elements: [Bool]) -> Offset { - return createVector(elements, size: elements.count) - } - - /// Creates a vector of type Bool in the buffer - /// - Parameter elements: Elements to be written into the buffer - /// - Parameter size: Count of elements - /// - returns: Offset of the vector - mutating public func createVector(_ elements: [Bool], size: Int) -> Offset { - let size = size - startVector(size, elementSize: MemoryLayout.size) - _bb.push(elements: elements) - return Offset(offset: endVector(len: size)) - } - /// Creates a vector of type Scalar in the buffer /// - Parameter elements: elements to be written into the buffer /// - returns: Offset of the vector @@ -469,6 +451,7 @@ public struct FlatBufferBuilder { /// - condition: Condition to insert /// - def: Default condition /// - position: The predefined position of the element + @available(*, deprecated, message: "Deprecated, function will be removed in Flatbuffers v0.6.0. Regenerate code") mutating public func add(condition: Bool, def: Bool, at position: VOffset) { if (condition == def && !serializeDefaults) { track(offset: 0, at: position) diff --git a/swift/Sources/FlatBuffers/FlatBufferObject.swift b/swift/Sources/FlatBuffers/FlatBufferObject.swift index 3afb24fa..8fc23221 100644 --- a/swift/Sources/FlatBuffers/FlatBufferObject.swift +++ b/swift/Sources/FlatBuffers/FlatBufferObject.swift @@ -28,69 +28,3 @@ public protocol Enum { static var byteSize: Int { get } var value: T { get } } - -/// Mutable is a protocol that allows us to mutate Scalar values within the buffer -public protocol Mutable { - /// makes Flatbuffer accessed within the Protocol - var bb: ByteBuffer { get } - /// makes position of the table/struct accessed within the Protocol - var postion: Int32 { get } -} - -extension Mutable { - - /// Mutates the memory in the buffer, this is only called from the access function of table and structs - /// - Parameters: - /// - value: New value to be inserted to the buffer - /// - index: index of the Element - func mutate(value: T, o: Int32) -> Bool { - guard o != 0 else { return false } - bb.write(value: value, index: Int(o), direct: true) - return true - } -} - -extension Mutable where Self == Table { - - /// Mutates a value by calling mutate with respect to the position in the table - /// - Parameters: - /// - value: New value to be inserted to the buffer - /// - index: index of the Element - public func mutate(_ value: T, index: Int32) -> Bool { - guard index != 0 else { return false } - return mutate(value: value, o: index + postion) - } - - /// Directly mutates the element by calling mutate - /// - /// Mutates the Element at index ignoring the current position by calling mutate - /// - Parameters: - /// - value: New value to be inserted to the buffer - /// - index: index of the Element - public func directMutate(_ value: T, index: Int32) -> Bool { - return mutate(value: value, o: index) - } -} - -extension Mutable where Self == Struct { - - /// Mutates a value by calling mutate with respect to the position in the struct - /// - Parameters: - /// - value: New value to be inserted to the buffer - /// - index: index of the Element - public func mutate(_ value: T, index: Int32) -> Bool { - return mutate(value: value, o: index + postion) - } - - /// Directly mutates the element by calling mutate - /// - /// Mutates the Element at index ignoring the current position by calling mutate - /// - Parameters: - /// - value: New value to be inserted to the buffer - /// - index: index of the Element - public func directMutate(_ value: T, index: Int32) -> Bool { - return mutate(value: value, o: index) - } -} -extension Struct: Mutable {} -extension Table: Mutable {} diff --git a/swift/Sources/FlatBuffers/Mutable.swift b/swift/Sources/FlatBuffers/Mutable.swift new file mode 100644 index 00000000..90c1d8b1 --- /dev/null +++ b/swift/Sources/FlatBuffers/Mutable.swift @@ -0,0 +1,68 @@ +import Foundation + +/// Mutable is a protocol that allows us to mutate Scalar values within the buffer +public protocol Mutable { + /// makes Flatbuffer accessed within the Protocol + var bb: ByteBuffer { get } + /// makes position of the table/struct accessed within the Protocol + var postion: Int32 { get } +} + +extension Mutable { + + /// Mutates the memory in the buffer, this is only called from the access function of table and structs + /// - Parameters: + /// - value: New value to be inserted to the buffer + /// - index: index of the Element + func mutate(value: T, o: Int32) -> Bool { + guard o != 0 else { return false } + bb.write(value: value, index: Int(o), direct: true) + return true + } +} + +extension Mutable where Self == Table { + + /// Mutates a value by calling mutate with respect to the position in the table + /// - Parameters: + /// - value: New value to be inserted to the buffer + /// - index: index of the Element + public func mutate(_ value: T, index: Int32) -> Bool { + guard index != 0 else { return false } + return mutate(value: value, o: index + postion) + } + + /// Directly mutates the element by calling mutate + /// + /// Mutates the Element at index ignoring the current position by calling mutate + /// - Parameters: + /// - value: New value to be inserted to the buffer + /// - index: index of the Element + public func directMutate(_ value: T, index: Int32) -> Bool { + return mutate(value: value, o: index) + } +} + +extension Mutable where Self == Struct { + + /// Mutates a value by calling mutate with respect to the position in the struct + /// - Parameters: + /// - value: New value to be inserted to the buffer + /// - index: index of the Element + public func mutate(_ value: T, index: Int32) -> Bool { + return mutate(value: value, o: index + postion) + } + + /// Directly mutates the element by calling mutate + /// + /// Mutates the Element at index ignoring the current position by calling mutate + /// - Parameters: + /// - value: New value to be inserted to the buffer + /// - index: index of the Element + public func directMutate(_ value: T, index: Int32) -> Bool { + return mutate(value: value, o: index) + } +} + +extension Struct: Mutable {} +extension Table: Mutable {} -- cgit v1.2.3