mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-17 17:56:31 +00:00
[Swift] Object API support (#5826)
* Adds Object-api support to swift * Fixed indentation issues * Removed indentation within namespaces
This commit is contained in:
@@ -131,6 +131,16 @@ public struct ByteBuffer {
|
||||
push(value: s, len: MemoryLayout.size(ofValue: s))
|
||||
}
|
||||
}
|
||||
|
||||
///Adds an array of type Bool to the buffer memory
|
||||
/// - Parameter elements: An array of Bool
|
||||
@usableFromInline mutating func push(elements: [Bool]) {
|
||||
let size = elements.count * MemoryLayout<Bool>.size
|
||||
ensureSpace(size: UInt32(size))
|
||||
elements.lazy.reversed().forEach { (s) in
|
||||
push(value: s ? 1 : 0, len: MemoryLayout.size(ofValue: s))
|
||||
}
|
||||
}
|
||||
|
||||
/// A custom type of structs that are padded according to the flatbuffer padding,
|
||||
/// - Parameters:
|
||||
@@ -174,8 +184,6 @@ public struct ByteBuffer {
|
||||
/// - len: Size of string
|
||||
@usableFromInline mutating internal func push(bytes: UnsafeBufferPointer<String.UTF8View.Element>, len: Int) -> Bool {
|
||||
memcpy(_storage.memory.advanced(by: writerIndex - len), UnsafeRawPointer(bytes.baseAddress!), len)
|
||||
// _memory.advanced(by: writerIndex - len).copyMemory(from:
|
||||
// UnsafeRawPointer(bytes.baseAddress!), byteCount: len)
|
||||
_writerSize += len
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public struct FlatBufferBuilder {
|
||||
/// Returns A sized Buffer from the readable bytes
|
||||
public var sizedBuffer: ByteBuffer {
|
||||
assert(finished, "Data shouldn't be called before finish()")
|
||||
return ByteBuffer(memory: _bb.memory.advanced(by: _bb.reader), count: _bb.reader)
|
||||
return ByteBuffer(memory: _bb.memory.advanced(by: _bb.reader), count: Int(_bb.size))
|
||||
}
|
||||
|
||||
// MARK: - Init
|
||||
@@ -276,6 +276,24 @@ 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
|
||||
|
||||
@@ -6,6 +6,14 @@ public protocol FlatBufferObject {
|
||||
init(_ bb: ByteBuffer, o: Int32)
|
||||
}
|
||||
|
||||
public protocol NativeTable {}
|
||||
|
||||
public protocol ObjectAPI {
|
||||
associatedtype T
|
||||
static func pack(_ builder: inout FlatBufferBuilder, obj: inout T) -> Offset<UOffset>
|
||||
mutating func unpack() -> T
|
||||
}
|
||||
|
||||
/// Readable is structures all the Flatbuffers structs
|
||||
///
|
||||
/// Readable is a procotol that each Flatbuffer struct should confirm to since
|
||||
|
||||
Reference in New Issue
Block a user