summaryrefslogtreecommitdiff
path: root/swift
diff options
context:
space:
mode:
authormustiikhalil <mustii@mmk.one>2020-04-13 19:28:56 +0300
committerGitHub <noreply@github.com>2020-04-13 09:28:56 -0700
commitcb4d0f72e38dfe2a15e39d8d449f7e6385300ca1 (patch)
tree122485bc96fb018c0957525b36109704c0a07a0d /swift
parent003e164057e6d4908d87c379c79a9ebdcb8bbc26 (diff)
downloadflatbuffers-cb4d0f72e38dfe2a15e39d8d449f7e6385300ca1.tar.gz
flatbuffers-cb4d0f72e38dfe2a15e39d8d449f7e6385300ca1.tar.bz2
flatbuffers-cb4d0f72e38dfe2a15e39d8d449f7e6385300ca1.zip
[Swift] Object API support (#5826)
* Adds Object-api support to swift * Fixed indentation issues * Removed indentation within namespaces
Diffstat (limited to 'swift')
-rw-r--r--swift/Sources/FlatBuffers/ByteBuffer.swift12
-rw-r--r--swift/Sources/FlatBuffers/FlatBufferBuilder.swift20
-rw-r--r--swift/Sources/FlatBuffers/FlatBufferObject.swift8
3 files changed, 37 insertions, 3 deletions
diff --git a/swift/Sources/FlatBuffers/ByteBuffer.swift b/swift/Sources/FlatBuffers/ByteBuffer.swift
index 586c2702..a35f25da 100644
--- a/swift/Sources/FlatBuffers/ByteBuffer.swift
+++ b/swift/Sources/FlatBuffers/ByteBuffer.swift
@@ -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
}
diff --git a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift
index 7f2ed784..62b5260b 100644
--- a/swift/Sources/FlatBuffers/FlatBufferBuilder.swift
+++ b/swift/Sources/FlatBuffers/FlatBufferBuilder.swift
@@ -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
diff --git a/swift/Sources/FlatBuffers/FlatBufferObject.swift b/swift/Sources/FlatBuffers/FlatBufferObject.swift
index a247b142..3afb24fa 100644
--- a/swift/Sources/FlatBuffers/FlatBufferObject.swift
+++ b/swift/Sources/FlatBuffers/FlatBufferObject.swift
@@ -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