mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-11 07:27:27 +00:00
Adds bool support in structs + updates grpc support + CI upgrades (#5943)
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 {}
|
||||
|
||||
68
swift/Sources/FlatBuffers/Mutable.swift
Normal file
68
swift/Sources/FlatBuffers/Mutable.swift
Normal file
@@ -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 {}
|
||||
Reference in New Issue
Block a user