diff options
author | mustiikhalil <mustii@mmk.one> | 2020-06-04 19:14:18 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-04 09:14:18 -0700 |
commit | 666800da3644e75b3dc0cdc56402f104fe201655 (patch) | |
tree | 50c6ed51b39fcba2a9cde8b2188f0d21f22f4949 /swift | |
parent | 38ed69eb3dc339b6ffb963081aecc4f1dc4acb83 (diff) | |
download | flatbuffers-666800da3644e75b3dc0cdc56402f104fe201655.tar.gz flatbuffers-666800da3644e75b3dc0cdc56402f104fe201655.tar.bz2 flatbuffers-666800da3644e75b3dc0cdc56402f104fe201655.zip |
Adds bool support in structs + updates grpc support + CI upgrades (#5943)
Diffstat (limited to 'swift')
-rw-r--r-- | swift/Sources/FlatBuffers/Constants.swift | 8 | ||||
-rw-r--r-- | swift/Sources/FlatBuffers/FlatBufferBuilder.swift | 19 | ||||
-rw-r--r-- | swift/Sources/FlatBuffers/FlatBufferObject.swift | 66 | ||||
-rw-r--r-- | swift/Sources/FlatBuffers/Mutable.swift | 68 |
4 files changed, 77 insertions, 84 deletions
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<UOffset> { - 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<UOffset> { - let size = size - startVector(size, elementSize: MemoryLayout<Bool>.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<T: Scalar>(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<T: Scalar>(_ 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<T: Scalar>(_ 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<T: Scalar>(_ 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<T: Scalar>(_ 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<T: Scalar>(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<T: Scalar>(_ 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<T: Scalar>(_ 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<T: Scalar>(_ 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<T: Scalar>(_ value: T, index: Int32) -> Bool { + return mutate(value: value, o: index) + } +} + +extension Struct: Mutable {} +extension Table: Mutable {} |