2020-11-16 16:52:38 +00:00
|
|
|
/*
|
2024-05-29 20:07:54 +00:00
|
|
|
* Copyright 2024 Google Inc. All rights reserved.
|
2020-11-16 16:52:38 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
[Swift] Swift implementation 🎉🎉 (#5603)
* Implemented the swift version of Flatbuffers
Implemented serailzing, reading, and mutating data from object monster
Fixes mis-aligned pointer issue
Fixes issue when shared strings are removed from table
Adds swift enum, structs code gen
Fixed namespace issues + started implementing the table gen
Added Mutate function to the code generator
Generated linux test cases
Fixed an issue with bools, and structs readers in table writer
Swift docker image added
Updated the test cases, and removed a method parameters in swift
Fixed createVector api when called with scalars
Fixed issues with scalar arrays, and fixed the code gen namespaces, added sample_binary.swift
Cleaned up project
Added enum vectors, and their readers
Refactored code
Added swift into the support document
Added documentation in docs, and fixed a small issue with Data() not being returned correctly
Fixes Lowercase issue, and prevents generating lookups for deprecated keys
* Made all the required funcs to have const + removed unneeded code + fix lowercase func
* Removed transform from lowercased and moved it to function
* Fixes an issue with iOS allocation from read
* Refactored cpp code to be more readable
* casts position into int for position
* Fix enums issue, moves scalar writer code to use memcpy
* Removed c_str from struct function
* Fixed script to generate new objects when ran on travis ci: fix
* Handles deallocating space allocated for structs
* Updated the test cases to adhere to the fileprivate lookup, no mutation for unions, and updated the names of the vector functions
2020-01-09 20:12:10 +00:00
|
|
|
import Foundation
|
|
|
|
|
2021-05-14 17:59:28 +00:00
|
|
|
/// `ByteBuffer` is the interface that stores the data for a `Flatbuffers` object
|
|
|
|
/// it allows users to write and read data directly from memory thus the use of its
|
|
|
|
/// functions should be used
|
2021-05-06 04:55:03 +00:00
|
|
|
@frozen
|
2020-04-02 19:55:30 +00:00
|
|
|
public struct ByteBuffer {
|
|
|
|
|
2020-11-16 16:52:38 +00:00
|
|
|
/// Storage is a container that would hold the memory pointer to solve the issue of
|
|
|
|
/// deallocating the memory that was held by (memory: UnsafeMutableRawPointer)
|
|
|
|
@usableFromInline
|
|
|
|
final class Storage {
|
|
|
|
// This storage doesn't own the memory, therefore, we won't deallocate on deinit.
|
|
|
|
private let unowned: Bool
|
|
|
|
/// pointer to the start of the buffer object in memory
|
|
|
|
var memory: UnsafeMutableRawPointer
|
|
|
|
/// Capacity of UInt8 the buffer can hold
|
|
|
|
var capacity: Int
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2021-05-06 04:55:03 +00:00
|
|
|
@usableFromInline
|
2020-11-16 16:52:38 +00:00
|
|
|
init(count: Int, alignment: Int) {
|
2021-05-14 17:59:28 +00:00
|
|
|
memory = UnsafeMutableRawPointer.allocate(
|
|
|
|
byteCount: count,
|
|
|
|
alignment: alignment)
|
2020-11-16 16:52:38 +00:00
|
|
|
capacity = count
|
|
|
|
unowned = false
|
2020-06-30 18:37:43 +00:00
|
|
|
}
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2021-05-06 04:55:03 +00:00
|
|
|
@usableFromInline
|
2020-11-16 16:52:38 +00:00
|
|
|
init(memory: UnsafeMutableRawPointer, capacity: Int, unowned: Bool) {
|
|
|
|
self.memory = memory
|
|
|
|
self.capacity = capacity
|
|
|
|
self.unowned = unowned
|
2020-02-24 17:27:41 +00:00
|
|
|
}
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2020-11-16 16:52:38 +00:00
|
|
|
deinit {
|
|
|
|
if !unowned {
|
|
|
|
memory.deallocate()
|
|
|
|
}
|
[Swift] Swift implementation 🎉🎉 (#5603)
* Implemented the swift version of Flatbuffers
Implemented serailzing, reading, and mutating data from object monster
Fixes mis-aligned pointer issue
Fixes issue when shared strings are removed from table
Adds swift enum, structs code gen
Fixed namespace issues + started implementing the table gen
Added Mutate function to the code generator
Generated linux test cases
Fixed an issue with bools, and structs readers in table writer
Swift docker image added
Updated the test cases, and removed a method parameters in swift
Fixed createVector api when called with scalars
Fixed issues with scalar arrays, and fixed the code gen namespaces, added sample_binary.swift
Cleaned up project
Added enum vectors, and their readers
Refactored code
Added swift into the support document
Added documentation in docs, and fixed a small issue with Data() not being returned correctly
Fixes Lowercase issue, and prevents generating lookups for deprecated keys
* Made all the required funcs to have const + removed unneeded code + fix lowercase func
* Removed transform from lowercased and moved it to function
* Fixes an issue with iOS allocation from read
* Refactored cpp code to be more readable
* casts position into int for position
* Fix enums issue, moves scalar writer code to use memcpy
* Removed c_str from struct function
* Fixed script to generate new objects when ran on travis ci: fix
* Handles deallocating space allocated for structs
* Updated the test cases to adhere to the fileprivate lookup, no mutation for unions, and updated the names of the vector functions
2020-01-09 20:12:10 +00:00
|
|
|
}
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2021-05-06 04:55:03 +00:00
|
|
|
@usableFromInline
|
2020-11-16 16:52:38 +00:00
|
|
|
func copy(from ptr: UnsafeRawPointer, count: Int) {
|
|
|
|
assert(
|
|
|
|
!unowned,
|
|
|
|
"copy should NOT be called on a buffer that is built by assumingMemoryBound")
|
|
|
|
memory.copyMemory(from: ptr, byteCount: count)
|
[Swift] Swift implementation 🎉🎉 (#5603)
* Implemented the swift version of Flatbuffers
Implemented serailzing, reading, and mutating data from object monster
Fixes mis-aligned pointer issue
Fixes issue when shared strings are removed from table
Adds swift enum, structs code gen
Fixed namespace issues + started implementing the table gen
Added Mutate function to the code generator
Generated linux test cases
Fixed an issue with bools, and structs readers in table writer
Swift docker image added
Updated the test cases, and removed a method parameters in swift
Fixed createVector api when called with scalars
Fixed issues with scalar arrays, and fixed the code gen namespaces, added sample_binary.swift
Cleaned up project
Added enum vectors, and their readers
Refactored code
Added swift into the support document
Added documentation in docs, and fixed a small issue with Data() not being returned correctly
Fixes Lowercase issue, and prevents generating lookups for deprecated keys
* Made all the required funcs to have const + removed unneeded code + fix lowercase func
* Removed transform from lowercased and moved it to function
* Fixes an issue with iOS allocation from read
* Refactored cpp code to be more readable
* casts position into int for position
* Fix enums issue, moves scalar writer code to use memcpy
* Removed c_str from struct function
* Fixed script to generate new objects when ran on travis ci: fix
* Handles deallocating space allocated for structs
* Updated the test cases to adhere to the fileprivate lookup, no mutation for unions, and updated the names of the vector functions
2020-01-09 20:12:10 +00:00
|
|
|
}
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2021-05-06 04:55:03 +00:00
|
|
|
@usableFromInline
|
2020-11-16 16:52:38 +00:00
|
|
|
func initialize(for size: Int) {
|
|
|
|
assert(
|
|
|
|
!unowned,
|
|
|
|
"initalize should NOT be called on a buffer that is built by assumingMemoryBound")
|
|
|
|
memset(memory, 0, size)
|
2020-09-17 14:10:59 +00:00
|
|
|
}
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2020-11-16 16:52:38 +00:00
|
|
|
/// Reallocates the buffer incase the object to be written doesnt fit in the current buffer
|
|
|
|
/// - Parameter size: Size of the current object
|
|
|
|
@usableFromInline
|
2021-05-14 17:59:28 +00:00
|
|
|
func reallocate(_ size: Int, writerSize: Int, alignment: Int) {
|
2020-11-16 16:52:38 +00:00
|
|
|
let currentWritingIndex = capacity &- writerSize
|
|
|
|
while capacity <= writerSize &+ size {
|
|
|
|
capacity = capacity << 1
|
|
|
|
}
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2020-11-16 16:52:38 +00:00
|
|
|
/// solution take from Apple-NIO
|
|
|
|
capacity = capacity.convertToPowerofTwo
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2021-05-14 17:59:28 +00:00
|
|
|
let newData = UnsafeMutableRawPointer.allocate(
|
|
|
|
byteCount: capacity,
|
|
|
|
alignment: alignment)
|
2020-11-16 16:52:38 +00:00
|
|
|
memset(newData, 0, capacity &- writerSize)
|
|
|
|
memcpy(
|
|
|
|
newData.advanced(by: capacity &- writerSize),
|
|
|
|
memory.advanced(by: currentWritingIndex),
|
|
|
|
writerSize)
|
|
|
|
memory.deallocate()
|
|
|
|
memory = newData
|
[Swift] Swift implementation 🎉🎉 (#5603)
* Implemented the swift version of Flatbuffers
Implemented serailzing, reading, and mutating data from object monster
Fixes mis-aligned pointer issue
Fixes issue when shared strings are removed from table
Adds swift enum, structs code gen
Fixed namespace issues + started implementing the table gen
Added Mutate function to the code generator
Generated linux test cases
Fixed an issue with bools, and structs readers in table writer
Swift docker image added
Updated the test cases, and removed a method parameters in swift
Fixed createVector api when called with scalars
Fixed issues with scalar arrays, and fixed the code gen namespaces, added sample_binary.swift
Cleaned up project
Added enum vectors, and their readers
Refactored code
Added swift into the support document
Added documentation in docs, and fixed a small issue with Data() not being returned correctly
Fixes Lowercase issue, and prevents generating lookups for deprecated keys
* Made all the required funcs to have const + removed unneeded code + fix lowercase func
* Removed transform from lowercased and moved it to function
* Fixes an issue with iOS allocation from read
* Refactored cpp code to be more readable
* casts position into int for position
* Fix enums issue, moves scalar writer code to use memcpy
* Removed c_str from struct function
* Fixed script to generate new objects when ran on travis ci: fix
* Handles deallocating space allocated for structs
* Updated the test cases to adhere to the fileprivate lookup, no mutation for unions, and updated the names of the vector functions
2020-01-09 20:12:10 +00:00
|
|
|
}
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2020-11-16 16:52:38 +00:00
|
|
|
@usableFromInline var _storage: Storage
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2020-11-16 16:52:38 +00:00
|
|
|
/// The size of the elements written to the buffer + their paddings
|
|
|
|
private var _writerSize: Int = 0
|
2024-08-20 03:22:00 +00:00
|
|
|
/// Alignment of the current memory being written to the buffer
|
2021-05-14 17:59:28 +00:00
|
|
|
var alignment = 1
|
2020-11-16 16:52:38 +00:00
|
|
|
/// Current Index which is being used to write to the buffer, it is written from the end to the start of the buffer
|
2021-05-14 17:59:28 +00:00
|
|
|
var writerIndex: Int { _storage.capacity &- _writerSize }
|
2020-11-16 16:52:38 +00:00
|
|
|
|
|
|
|
/// Reader is the position of the current Writer Index (capacity - size)
|
|
|
|
public var reader: Int { writerIndex }
|
|
|
|
/// Current size of the buffer
|
|
|
|
public var size: UOffset { UOffset(_writerSize) }
|
|
|
|
/// Public Pointer to the buffer object in memory. This should NOT be modified for any reason
|
|
|
|
public var memory: UnsafeMutableRawPointer { _storage.memory }
|
|
|
|
/// Current capacity for the buffer
|
|
|
|
public var capacity: Int { _storage.capacity }
|
2023-09-27 05:50:03 +00:00
|
|
|
/// Crash if the trying to read an unaligned buffer instead of allowing users to read them.
|
|
|
|
public let allowReadingUnalignedBuffers: Bool
|
2020-11-16 16:52:38 +00:00
|
|
|
|
|
|
|
/// Constructor that creates a Flatbuffer object from a UInt8
|
2024-05-29 20:07:54 +00:00
|
|
|
/// - Parameter
|
2023-11-20 22:52:19 +00:00
|
|
|
/// - bytes: Array of UInt8
|
|
|
|
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
|
|
|
public init(
|
2024-05-29 20:07:54 +00:00
|
|
|
bytes: [UInt8],
|
2023-11-20 22:52:19 +00:00
|
|
|
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
|
|
|
|
{
|
2020-11-16 16:52:38 +00:00
|
|
|
var b = bytes
|
|
|
|
_storage = Storage(count: bytes.count, alignment: alignment)
|
|
|
|
_writerSize = _storage.capacity
|
2023-11-20 22:52:19 +00:00
|
|
|
allowReadingUnalignedBuffers = allowUnalignedBuffers
|
2020-11-16 16:52:38 +00:00
|
|
|
b.withUnsafeMutableBytes { bufferPointer in
|
2024-05-29 20:07:54 +00:00
|
|
|
_storage.copy(from: bufferPointer.baseAddress!, count: bytes.count)
|
2020-06-30 18:37:43 +00:00
|
|
|
}
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 18:56:33 +00:00
|
|
|
#if !os(WASI)
|
2020-11-16 16:52:38 +00:00
|
|
|
/// Constructor that creates a Flatbuffer from the Swift Data type object
|
2024-05-29 20:07:54 +00:00
|
|
|
/// - Parameter
|
2023-11-20 22:52:19 +00:00
|
|
|
/// - data: Swift data Object
|
|
|
|
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
|
|
|
public init(
|
2024-05-29 20:07:54 +00:00
|
|
|
data: Data,
|
2023-11-20 22:52:19 +00:00
|
|
|
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
|
|
|
|
{
|
2020-11-16 16:52:38 +00:00
|
|
|
var b = data
|
|
|
|
_storage = Storage(count: data.count, alignment: alignment)
|
|
|
|
_writerSize = _storage.capacity
|
2023-11-20 22:52:19 +00:00
|
|
|
allowReadingUnalignedBuffers = allowUnalignedBuffers
|
2020-11-16 16:52:38 +00:00
|
|
|
b.withUnsafeMutableBytes { bufferPointer in
|
2024-05-29 20:07:54 +00:00
|
|
|
_storage.copy(from: bufferPointer.baseAddress!, count: data.count)
|
[Swift] Swift implementation 🎉🎉 (#5603)
* Implemented the swift version of Flatbuffers
Implemented serailzing, reading, and mutating data from object monster
Fixes mis-aligned pointer issue
Fixes issue when shared strings are removed from table
Adds swift enum, structs code gen
Fixed namespace issues + started implementing the table gen
Added Mutate function to the code generator
Generated linux test cases
Fixed an issue with bools, and structs readers in table writer
Swift docker image added
Updated the test cases, and removed a method parameters in swift
Fixed createVector api when called with scalars
Fixed issues with scalar arrays, and fixed the code gen namespaces, added sample_binary.swift
Cleaned up project
Added enum vectors, and their readers
Refactored code
Added swift into the support document
Added documentation in docs, and fixed a small issue with Data() not being returned correctly
Fixes Lowercase issue, and prevents generating lookups for deprecated keys
* Made all the required funcs to have const + removed unneeded code + fix lowercase func
* Removed transform from lowercased and moved it to function
* Fixes an issue with iOS allocation from read
* Refactored cpp code to be more readable
* casts position into int for position
* Fix enums issue, moves scalar writer code to use memcpy
* Removed c_str from struct function
* Fixed script to generate new objects when ran on travis ci: fix
* Handles deallocating space allocated for structs
* Updated the test cases to adhere to the fileprivate lookup, no mutation for unions, and updated the names of the vector functions
2020-01-09 20:12:10 +00:00
|
|
|
}
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
2022-05-29 18:56:33 +00:00
|
|
|
#endif
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2020-11-16 16:52:38 +00:00
|
|
|
/// Constructor that creates a Flatbuffer instance with a size
|
2023-11-20 22:52:19 +00:00
|
|
|
/// - Parameter:
|
|
|
|
/// - size: Length of the buffer
|
|
|
|
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
2020-11-16 16:52:38 +00:00
|
|
|
init(initialSize size: Int) {
|
|
|
|
let size = size.convertToPowerofTwo
|
|
|
|
_storage = Storage(count: size, alignment: alignment)
|
|
|
|
_storage.initialize(for: size)
|
2023-09-27 05:50:03 +00:00
|
|
|
allowReadingUnalignedBuffers = false
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 18:56:33 +00:00
|
|
|
#if swift(>=5.0) && !os(WASI)
|
2020-11-16 16:52:38 +00:00
|
|
|
/// Constructor that creates a Flatbuffer object from a ContiguousBytes
|
|
|
|
/// - Parameters:
|
|
|
|
/// - contiguousBytes: Binary stripe to use as the buffer
|
|
|
|
/// - count: amount of readable bytes
|
2023-11-20 22:52:19 +00:00
|
|
|
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
2020-11-16 16:52:38 +00:00
|
|
|
public init<Bytes: ContiguousBytes>(
|
|
|
|
contiguousBytes: Bytes,
|
2024-05-29 20:07:54 +00:00
|
|
|
count: Int,
|
2023-11-20 22:52:19 +00:00
|
|
|
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
|
2020-11-16 16:52:38 +00:00
|
|
|
{
|
|
|
|
_storage = Storage(count: count, alignment: alignment)
|
|
|
|
_writerSize = _storage.capacity
|
2023-11-20 22:52:19 +00:00
|
|
|
allowReadingUnalignedBuffers = allowUnalignedBuffers
|
2020-11-16 16:52:38 +00:00
|
|
|
contiguousBytes.withUnsafeBytes { buf in
|
|
|
|
_storage.copy(from: buf.baseAddress!, count: buf.count)
|
[Swift] Swift implementation 🎉🎉 (#5603)
* Implemented the swift version of Flatbuffers
Implemented serailzing, reading, and mutating data from object monster
Fixes mis-aligned pointer issue
Fixes issue when shared strings are removed from table
Adds swift enum, structs code gen
Fixed namespace issues + started implementing the table gen
Added Mutate function to the code generator
Generated linux test cases
Fixed an issue with bools, and structs readers in table writer
Swift docker image added
Updated the test cases, and removed a method parameters in swift
Fixed createVector api when called with scalars
Fixed issues with scalar arrays, and fixed the code gen namespaces, added sample_binary.swift
Cleaned up project
Added enum vectors, and their readers
Refactored code
Added swift into the support document
Added documentation in docs, and fixed a small issue with Data() not being returned correctly
Fixes Lowercase issue, and prevents generating lookups for deprecated keys
* Made all the required funcs to have const + removed unneeded code + fix lowercase func
* Removed transform from lowercased and moved it to function
* Fixes an issue with iOS allocation from read
* Refactored cpp code to be more readable
* casts position into int for position
* Fix enums issue, moves scalar writer code to use memcpy
* Removed c_str from struct function
* Fixed script to generate new objects when ran on travis ci: fix
* Handles deallocating space allocated for structs
* Updated the test cases to adhere to the fileprivate lookup, no mutation for unions, and updated the names of the vector functions
2020-01-09 20:12:10 +00:00
|
|
|
}
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/// Constructor that creates a Flatbuffer from unsafe memory region without copying
|
2023-11-20 22:52:19 +00:00
|
|
|
/// - Parameter:
|
|
|
|
/// - assumingMemoryBound: The unsafe memory region
|
|
|
|
/// - capacity: The size of the given memory region
|
|
|
|
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
2021-09-27 18:59:19 +00:00
|
|
|
public init(
|
|
|
|
assumingMemoryBound memory: UnsafeMutableRawPointer,
|
2023-09-27 05:50:03 +00:00
|
|
|
capacity: Int,
|
|
|
|
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
|
2021-09-27 18:59:19 +00:00
|
|
|
{
|
2020-11-16 16:52:38 +00:00
|
|
|
_storage = Storage(memory: memory, capacity: capacity, unowned: true)
|
|
|
|
_writerSize = capacity
|
2023-09-27 05:50:03 +00:00
|
|
|
allowReadingUnalignedBuffers = allowUnalignedBuffers
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a copy of the buffer that's being built by calling sizedBuffer
|
|
|
|
/// - Parameters:
|
|
|
|
/// - memory: Current memory of the buffer
|
|
|
|
/// - count: count of bytes
|
2023-11-20 22:52:19 +00:00
|
|
|
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
|
|
|
init(
|
2024-05-29 20:07:54 +00:00
|
|
|
memory: UnsafeMutableRawPointer,
|
2023-11-20 22:52:19 +00:00
|
|
|
count: Int,
|
|
|
|
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
|
|
|
|
{
|
2020-11-16 16:52:38 +00:00
|
|
|
_storage = Storage(count: count, alignment: alignment)
|
|
|
|
_storage.copy(from: memory, count: count)
|
|
|
|
_writerSize = _storage.capacity
|
2023-11-20 22:52:19 +00:00
|
|
|
allowReadingUnalignedBuffers = allowUnalignedBuffers
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a copy of the existing flatbuffer, by copying it to a different memory.
|
|
|
|
/// - Parameters:
|
|
|
|
/// - memory: Current memory of the buffer
|
|
|
|
/// - count: count of bytes
|
|
|
|
/// - removeBytes: Removes a number of bytes from the current size
|
2023-11-20 22:52:19 +00:00
|
|
|
/// - allowReadingUnalignedBuffers: allow reading from unaligned buffer
|
2021-05-14 17:59:28 +00:00
|
|
|
init(
|
|
|
|
memory: UnsafeMutableRawPointer,
|
|
|
|
count: Int,
|
2023-11-20 22:52:19 +00:00
|
|
|
removing removeBytes: Int,
|
2024-05-29 20:07:54 +00:00
|
|
|
allowReadingUnalignedBuffers allowUnalignedBuffers: Bool = false)
|
2021-05-14 17:59:28 +00:00
|
|
|
{
|
2020-11-16 16:52:38 +00:00
|
|
|
_storage = Storage(count: count, alignment: alignment)
|
|
|
|
_storage.copy(from: memory, count: count)
|
|
|
|
_writerSize = removeBytes
|
2023-11-20 22:52:19 +00:00
|
|
|
allowReadingUnalignedBuffers = allowUnalignedBuffers
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fills the buffer with padding by adding to the writersize
|
|
|
|
/// - Parameter padding: Amount of padding between two to be serialized objects
|
2022-04-20 06:59:47 +00:00
|
|
|
@inline(__always)
|
2020-11-16 16:52:38 +00:00
|
|
|
@usableFromInline
|
|
|
|
mutating func fill(padding: Int) {
|
|
|
|
assert(padding >= 0, "Fill should be larger than or equal to zero")
|
|
|
|
ensureSpace(size: padding)
|
|
|
|
_writerSize = _writerSize &+ (MemoryLayout<UInt8>.size &* padding)
|
|
|
|
}
|
|
|
|
|
2020-12-17 22:55:32 +00:00
|
|
|
/// Adds an array of type Scalar to the buffer memory
|
2020-11-16 16:52:38 +00:00
|
|
|
/// - Parameter elements: An array of Scalars
|
2022-04-20 06:59:47 +00:00
|
|
|
@inline(__always)
|
2020-11-16 16:52:38 +00:00
|
|
|
@usableFromInline
|
|
|
|
mutating func push<T: Scalar>(elements: [T]) {
|
2023-11-20 16:47:11 +00:00
|
|
|
elements.withUnsafeBytes { ptr in
|
|
|
|
ensureSpace(size: ptr.count)
|
|
|
|
memcpy(
|
|
|
|
_storage.memory.advanced(by: writerIndex &- ptr.count),
|
2024-11-19 06:02:47 +00:00
|
|
|
ptr.baseAddress!,
|
2023-11-20 16:47:11 +00:00
|
|
|
ptr.count)
|
2024-05-29 20:07:54 +00:00
|
|
|
_writerSize = _writerSize &+ ptr.count
|
2023-11-20 16:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-23 00:08:55 +00:00
|
|
|
/// Adds an array of type Scalar to the buffer memory
|
|
|
|
/// - Parameter elements: An array of Scalars
|
|
|
|
@inline(__always)
|
|
|
|
@usableFromInline
|
|
|
|
mutating func push<T: NativeStruct>(elements: [T]) {
|
|
|
|
elements.withUnsafeBytes { ptr in
|
|
|
|
ensureSpace(size: ptr.count)
|
2024-11-19 06:02:47 +00:00
|
|
|
memcpy(
|
|
|
|
_storage.memory.advanced(by: writerIndex &- ptr.count),
|
|
|
|
ptr.baseAddress!,
|
|
|
|
ptr.count)
|
2024-05-29 20:07:54 +00:00
|
|
|
_writerSize = _writerSize &+ ptr.count
|
2023-11-23 00:08:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 16:47:11 +00:00
|
|
|
/// Adds a `ContiguousBytes` to buffer memory
|
|
|
|
/// - Parameter value: bytes to copy
|
|
|
|
#if swift(>=5.0) && !os(WASI)
|
|
|
|
@inline(__always)
|
|
|
|
@usableFromInline
|
|
|
|
mutating func push(bytes: ContiguousBytes) {
|
|
|
|
bytes.withUnsafeBytes { ptr in
|
|
|
|
ensureSpace(size: ptr.count)
|
|
|
|
memcpy(
|
|
|
|
_storage.memory.advanced(by: writerIndex &- ptr.count),
|
2024-11-19 06:02:47 +00:00
|
|
|
ptr.baseAddress!,
|
2023-11-23 00:08:55 +00:00
|
|
|
ptr.count)
|
2024-05-29 20:07:54 +00:00
|
|
|
_writerSize = _writerSize &+ ptr.count
|
[Swift] Swift implementation 🎉🎉 (#5603)
* Implemented the swift version of Flatbuffers
Implemented serailzing, reading, and mutating data from object monster
Fixes mis-aligned pointer issue
Fixes issue when shared strings are removed from table
Adds swift enum, structs code gen
Fixed namespace issues + started implementing the table gen
Added Mutate function to the code generator
Generated linux test cases
Fixed an issue with bools, and structs readers in table writer
Swift docker image added
Updated the test cases, and removed a method parameters in swift
Fixed createVector api when called with scalars
Fixed issues with scalar arrays, and fixed the code gen namespaces, added sample_binary.swift
Cleaned up project
Added enum vectors, and their readers
Refactored code
Added swift into the support document
Added documentation in docs, and fixed a small issue with Data() not being returned correctly
Fixes Lowercase issue, and prevents generating lookups for deprecated keys
* Made all the required funcs to have const + removed unneeded code + fix lowercase func
* Removed transform from lowercased and moved it to function
* Fixes an issue with iOS allocation from read
* Refactored cpp code to be more readable
* casts position into int for position
* Fix enums issue, moves scalar writer code to use memcpy
* Removed c_str from struct function
* Fixed script to generate new objects when ran on travis ci: fix
* Handles deallocating space allocated for structs
* Updated the test cases to adhere to the fileprivate lookup, no mutation for unions, and updated the names of the vector functions
2020-01-09 20:12:10 +00:00
|
|
|
}
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
2023-11-20 16:47:11 +00:00
|
|
|
#endif
|
2020-11-16 16:52:38 +00:00
|
|
|
|
2020-12-17 22:55:32 +00:00
|
|
|
/// Adds an object of type NativeStruct into the buffer
|
2020-11-16 16:52:38 +00:00
|
|
|
/// - Parameters:
|
2020-12-17 22:55:32 +00:00
|
|
|
/// - value: Object that will be written to the buffer
|
|
|
|
/// - size: size to subtract from the WriterIndex
|
2022-04-20 06:59:47 +00:00
|
|
|
@usableFromInline
|
2020-12-17 22:55:32 +00:00
|
|
|
@inline(__always)
|
|
|
|
mutating func push<T: NativeStruct>(struct value: T, size: Int) {
|
2020-11-16 16:52:38 +00:00
|
|
|
ensureSpace(size: size)
|
2024-11-19 06:02:47 +00:00
|
|
|
withUnsafePointer(to: value) {
|
2023-09-27 05:50:03 +00:00
|
|
|
memcpy(
|
|
|
|
_storage.memory.advanced(by: writerIndex &- size),
|
2024-11-19 06:02:47 +00:00
|
|
|
$0,
|
2023-09-27 05:50:03 +00:00
|
|
|
size)
|
2024-05-29 20:07:54 +00:00
|
|
|
_writerSize = _writerSize &+ size
|
2023-09-27 05:50:03 +00:00
|
|
|
}
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds an object of type Scalar into the buffer
|
|
|
|
/// - Parameters:
|
|
|
|
/// - value: Object that will be written to the buffer
|
|
|
|
/// - len: Offset to subtract from the WriterIndex
|
2022-04-20 06:59:47 +00:00
|
|
|
@inline(__always)
|
2020-11-16 16:52:38 +00:00
|
|
|
@usableFromInline
|
|
|
|
mutating func push<T: Scalar>(value: T, len: Int) {
|
|
|
|
ensureSpace(size: len)
|
2024-11-19 06:02:47 +00:00
|
|
|
withUnsafePointer(to: value) {
|
2023-09-27 05:50:03 +00:00
|
|
|
memcpy(
|
|
|
|
_storage.memory.advanced(by: writerIndex &- len),
|
2024-11-19 06:02:47 +00:00
|
|
|
$0,
|
2023-09-27 05:50:03 +00:00
|
|
|
len)
|
2024-05-29 20:07:54 +00:00
|
|
|
_writerSize = _writerSize &+ len
|
2023-09-27 05:50:03 +00:00
|
|
|
}
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a string to the buffer using swift.utf8 object
|
|
|
|
/// - Parameter str: String that will be added to the buffer
|
|
|
|
/// - Parameter len: length of the string
|
2022-04-20 06:59:47 +00:00
|
|
|
@inline(__always)
|
2020-11-16 16:52:38 +00:00
|
|
|
@usableFromInline
|
|
|
|
mutating func push(string str: String, len: Int) {
|
|
|
|
ensureSpace(size: len)
|
2021-09-27 18:59:19 +00:00
|
|
|
if str.utf8
|
|
|
|
.withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) !=
|
|
|
|
nil
|
|
|
|
{
|
2020-11-16 16:52:38 +00:00
|
|
|
} else {
|
|
|
|
let utf8View = str.utf8
|
|
|
|
for c in utf8View.reversed() {
|
|
|
|
push(value: c, len: 1)
|
|
|
|
}
|
[Swift] Swift implementation 🎉🎉 (#5603)
* Implemented the swift version of Flatbuffers
Implemented serailzing, reading, and mutating data from object monster
Fixes mis-aligned pointer issue
Fixes issue when shared strings are removed from table
Adds swift enum, structs code gen
Fixed namespace issues + started implementing the table gen
Added Mutate function to the code generator
Generated linux test cases
Fixed an issue with bools, and structs readers in table writer
Swift docker image added
Updated the test cases, and removed a method parameters in swift
Fixed createVector api when called with scalars
Fixed issues with scalar arrays, and fixed the code gen namespaces, added sample_binary.swift
Cleaned up project
Added enum vectors, and their readers
Refactored code
Added swift into the support document
Added documentation in docs, and fixed a small issue with Data() not being returned correctly
Fixes Lowercase issue, and prevents generating lookups for deprecated keys
* Made all the required funcs to have const + removed unneeded code + fix lowercase func
* Removed transform from lowercased and moved it to function
* Fixes an issue with iOS allocation from read
* Refactored cpp code to be more readable
* casts position into int for position
* Fix enums issue, moves scalar writer code to use memcpy
* Removed c_str from struct function
* Fixed script to generate new objects when ran on travis ci: fix
* Handles deallocating space allocated for structs
* Updated the test cases to adhere to the fileprivate lookup, no mutation for unions, and updated the names of the vector functions
2020-01-09 20:12:10 +00:00
|
|
|
}
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Writes a string to Bytebuffer using UTF8View
|
|
|
|
/// - Parameters:
|
|
|
|
/// - bytes: Pointer to the view
|
|
|
|
/// - len: Size of string
|
2022-04-20 06:59:47 +00:00
|
|
|
@usableFromInline
|
2020-12-17 22:55:32 +00:00
|
|
|
@inline(__always)
|
2021-05-14 17:59:28 +00:00
|
|
|
mutating func push(
|
2020-11-16 16:52:38 +00:00
|
|
|
bytes: UnsafeBufferPointer<String.UTF8View.Element>,
|
|
|
|
len: Int) -> Bool
|
|
|
|
{
|
|
|
|
memcpy(
|
|
|
|
_storage.memory.advanced(by: writerIndex &- len),
|
2024-11-19 06:02:47 +00:00
|
|
|
bytes.baseAddress!,
|
2020-11-16 16:52:38 +00:00
|
|
|
len)
|
|
|
|
_writerSize = _writerSize &+ len
|
|
|
|
return true
|
|
|
|
}
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2020-11-16 16:52:38 +00:00
|
|
|
/// Write stores an object into the buffer directly or indirectly.
|
|
|
|
///
|
|
|
|
/// Direct: ignores the capacity of buffer which would mean we are referring to the direct point in memory
|
|
|
|
/// indirect: takes into respect the current capacity of the buffer (capacity - index), writing to the buffer from the end
|
|
|
|
/// - Parameters:
|
|
|
|
/// - value: Value that needs to be written to the buffer
|
|
|
|
/// - index: index to write to
|
|
|
|
/// - direct: Should take into consideration the capacity of the buffer
|
2022-04-20 06:59:47 +00:00
|
|
|
@inline(__always)
|
2020-11-16 16:52:38 +00:00
|
|
|
func write<T>(value: T, index: Int, direct: Bool = false) {
|
|
|
|
var index = index
|
|
|
|
if !direct {
|
|
|
|
index = _storage.capacity &- index
|
[Swift] Swift implementation 🎉🎉 (#5603)
* Implemented the swift version of Flatbuffers
Implemented serailzing, reading, and mutating data from object monster
Fixes mis-aligned pointer issue
Fixes issue when shared strings are removed from table
Adds swift enum, structs code gen
Fixed namespace issues + started implementing the table gen
Added Mutate function to the code generator
Generated linux test cases
Fixed an issue with bools, and structs readers in table writer
Swift docker image added
Updated the test cases, and removed a method parameters in swift
Fixed createVector api when called with scalars
Fixed issues with scalar arrays, and fixed the code gen namespaces, added sample_binary.swift
Cleaned up project
Added enum vectors, and their readers
Refactored code
Added swift into the support document
Added documentation in docs, and fixed a small issue with Data() not being returned correctly
Fixes Lowercase issue, and prevents generating lookups for deprecated keys
* Made all the required funcs to have const + removed unneeded code + fix lowercase func
* Removed transform from lowercased and moved it to function
* Fixes an issue with iOS allocation from read
* Refactored cpp code to be more readable
* casts position into int for position
* Fix enums issue, moves scalar writer code to use memcpy
* Removed c_str from struct function
* Fixed script to generate new objects when ran on travis ci: fix
* Handles deallocating space allocated for structs
* Updated the test cases to adhere to the fileprivate lookup, no mutation for unions, and updated the names of the vector functions
2020-01-09 20:12:10 +00:00
|
|
|
}
|
2020-11-16 16:52:38 +00:00
|
|
|
assert(index < _storage.capacity, "Write index is out of writing bound")
|
|
|
|
assert(index >= 0, "Writer index should be above zero")
|
2024-11-19 06:02:47 +00:00
|
|
|
withUnsafePointer(to: value) {
|
|
|
|
memcpy(
|
|
|
|
_storage.memory.advanced(by: index),
|
|
|
|
$0,
|
|
|
|
MemoryLayout<T>.size)
|
|
|
|
}
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2020-11-16 16:52:38 +00:00
|
|
|
/// Makes sure that buffer has enouch space for each of the objects that will be written into it
|
|
|
|
/// - Parameter size: size of object
|
|
|
|
@discardableResult
|
2022-04-20 06:59:47 +00:00
|
|
|
@usableFromInline
|
2020-12-17 22:55:32 +00:00
|
|
|
@inline(__always)
|
2020-11-16 16:52:38 +00:00
|
|
|
mutating func ensureSpace(size: Int) -> Int {
|
|
|
|
if size &+ _writerSize > _storage.capacity {
|
|
|
|
_storage.reallocate(size, writerSize: _writerSize, alignment: alignment)
|
[Swift] Swift implementation 🎉🎉 (#5603)
* Implemented the swift version of Flatbuffers
Implemented serailzing, reading, and mutating data from object monster
Fixes mis-aligned pointer issue
Fixes issue when shared strings are removed from table
Adds swift enum, structs code gen
Fixed namespace issues + started implementing the table gen
Added Mutate function to the code generator
Generated linux test cases
Fixed an issue with bools, and structs readers in table writer
Swift docker image added
Updated the test cases, and removed a method parameters in swift
Fixed createVector api when called with scalars
Fixed issues with scalar arrays, and fixed the code gen namespaces, added sample_binary.swift
Cleaned up project
Added enum vectors, and their readers
Refactored code
Added swift into the support document
Added documentation in docs, and fixed a small issue with Data() not being returned correctly
Fixes Lowercase issue, and prevents generating lookups for deprecated keys
* Made all the required funcs to have const + removed unneeded code + fix lowercase func
* Removed transform from lowercased and moved it to function
* Fixes an issue with iOS allocation from read
* Refactored cpp code to be more readable
* casts position into int for position
* Fix enums issue, moves scalar writer code to use memcpy
* Removed c_str from struct function
* Fixed script to generate new objects when ran on travis ci: fix
* Handles deallocating space allocated for structs
* Updated the test cases to adhere to the fileprivate lookup, no mutation for unions, and updated the names of the vector functions
2020-01-09 20:12:10 +00:00
|
|
|
}
|
2020-11-16 16:52:38 +00:00
|
|
|
assert(size < FlatBufferMaxSize, "Buffer can't grow beyond 2 Gigabytes")
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
|
|
|
/// pops the written VTable if it's already written into the buffer
|
|
|
|
/// - Parameter size: size of the `VTable`
|
2022-04-20 06:59:47 +00:00
|
|
|
@usableFromInline
|
2020-12-17 22:55:32 +00:00
|
|
|
@inline(__always)
|
2021-05-14 17:59:28 +00:00
|
|
|
mutating func pop(_ size: Int) {
|
2021-09-27 18:59:19 +00:00
|
|
|
assert(
|
|
|
|
(_writerSize &- size) > 0,
|
|
|
|
"New size should NOT be a negative number")
|
2020-11-16 16:52:38 +00:00
|
|
|
memset(_storage.memory.advanced(by: writerIndex), 0, _writerSize &- size)
|
|
|
|
_writerSize = size
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clears the current size of the buffer
|
2022-04-20 06:59:47 +00:00
|
|
|
@inline(__always)
|
2020-11-16 16:52:38 +00:00
|
|
|
mutating public func clearSize() {
|
|
|
|
_writerSize = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clears the current instance of the buffer, replacing it with new memory
|
2022-04-20 06:59:47 +00:00
|
|
|
@inline(__always)
|
2020-11-16 16:52:38 +00:00
|
|
|
mutating public func clear() {
|
|
|
|
_writerSize = 0
|
|
|
|
alignment = 1
|
|
|
|
_storage.initialize(for: _storage.capacity)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads an object from the buffer
|
|
|
|
/// - Parameters:
|
|
|
|
/// - def: Type of the object
|
|
|
|
/// - position: the index of the object in the buffer
|
2022-04-20 06:59:47 +00:00
|
|
|
@inline(__always)
|
2020-11-16 16:52:38 +00:00
|
|
|
public func read<T>(def: T.Type, position: Int) -> T {
|
2023-09-27 05:50:03 +00:00
|
|
|
if allowReadingUnalignedBuffers {
|
|
|
|
return _storage.memory.advanced(by: position).loadUnaligned(as: T.self)
|
|
|
|
}
|
|
|
|
return _storage.memory.advanced(by: position).load(as: T.self)
|
2020-11-16 16:52:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads a slice from the memory assuming a type of T
|
|
|
|
/// - Parameters:
|
|
|
|
/// - index: index of the object to be read from the buffer
|
|
|
|
/// - count: count of bytes in memory
|
2020-12-17 22:55:32 +00:00
|
|
|
@inline(__always)
|
2020-11-16 16:52:38 +00:00
|
|
|
public func readSlice<T>(
|
2021-05-14 17:59:28 +00:00
|
|
|
index: Int,
|
|
|
|
count: Int) -> [T]
|
2020-11-16 16:52:38 +00:00
|
|
|
{
|
2021-05-14 17:59:28 +00:00
|
|
|
assert(
|
|
|
|
index + count <= _storage.capacity,
|
|
|
|
"Reading out of bounds is illegal")
|
2021-09-27 18:59:19 +00:00
|
|
|
let start = _storage.memory.advanced(by: index)
|
|
|
|
.assumingMemoryBound(to: T.self)
|
2021-05-14 17:59:28 +00:00
|
|
|
let array = UnsafeBufferPointer(start: start, count: count)
|
2020-11-16 16:52:38 +00:00
|
|
|
return Array(array)
|
|
|
|
}
|
|
|
|
|
2022-05-29 18:56:33 +00:00
|
|
|
#if !os(WASI)
|
2020-11-16 16:52:38 +00:00
|
|
|
/// Reads a string from the buffer and encodes it to a swift string
|
|
|
|
/// - Parameters:
|
|
|
|
/// - index: index of the string in the buffer
|
|
|
|
/// - count: length of the string
|
|
|
|
/// - type: Encoding of the string
|
2022-04-20 06:59:47 +00:00
|
|
|
@inline(__always)
|
2020-11-16 16:52:38 +00:00
|
|
|
public func readString(
|
2021-05-14 17:59:28 +00:00
|
|
|
at index: Int,
|
|
|
|
count: Int,
|
2020-11-16 16:52:38 +00:00
|
|
|
type: String.Encoding = .utf8) -> String?
|
|
|
|
{
|
2021-05-14 17:59:28 +00:00
|
|
|
assert(
|
|
|
|
index + count <= _storage.capacity,
|
|
|
|
"Reading out of bounds is illegal")
|
2021-09-27 18:59:19 +00:00
|
|
|
let start = _storage.memory.advanced(by: index)
|
|
|
|
.assumingMemoryBound(to: UInt8.self)
|
2021-05-14 17:59:28 +00:00
|
|
|
let bufprt = UnsafeBufferPointer(start: start, count: count)
|
2020-11-16 16:52:38 +00:00
|
|
|
return String(bytes: Array(bufprt), encoding: type)
|
|
|
|
}
|
2022-05-29 18:56:33 +00:00
|
|
|
#else
|
|
|
|
/// Reads a string from the buffer and encodes it to a swift string
|
|
|
|
/// - Parameters:
|
|
|
|
/// - index: index of the string in the buffer
|
|
|
|
/// - count: length of the string
|
|
|
|
@inline(__always)
|
|
|
|
public func readString(
|
|
|
|
at index: Int,
|
|
|
|
count: Int) -> String?
|
|
|
|
{
|
|
|
|
assert(
|
|
|
|
index + count <= _storage.capacity,
|
|
|
|
"Reading out of bounds is illegal")
|
|
|
|
let start = _storage.memory.advanced(by: index)
|
|
|
|
.assumingMemoryBound(to: UInt8.self)
|
|
|
|
let bufprt = UnsafeBufferPointer(start: start, count: count)
|
|
|
|
return String(cString: bufprt.baseAddress!)
|
|
|
|
}
|
|
|
|
#endif
|
2020-11-16 16:52:38 +00:00
|
|
|
|
|
|
|
/// Creates a new Flatbuffer object that's duplicated from the current one
|
|
|
|
/// - Parameter removeBytes: the amount of bytes to remove from the current Size
|
2022-04-20 06:59:47 +00:00
|
|
|
@inline(__always)
|
2020-11-16 16:52:38 +00:00
|
|
|
public func duplicate(removing removeBytes: Int = 0) -> ByteBuffer {
|
|
|
|
assert(removeBytes > 0, "Can NOT remove negative bytes")
|
2021-05-14 17:59:28 +00:00
|
|
|
assert(
|
|
|
|
removeBytes < _storage.capacity,
|
|
|
|
"Can NOT remove more bytes than the ones allocated")
|
2020-11-16 16:52:38 +00:00
|
|
|
return ByteBuffer(
|
|
|
|
memory: _storage.memory,
|
|
|
|
count: _storage.capacity,
|
|
|
|
removing: _writerSize &- removeBytes)
|
|
|
|
}
|
2021-05-14 17:59:28 +00:00
|
|
|
|
2022-02-11 21:26:16 +00:00
|
|
|
/// Returns the written bytes into the ``ByteBuffer``
|
2023-01-07 00:40:40 +00:00
|
|
|
public var underlyingBytes: [UInt8] {
|
|
|
|
let cp = capacity &- writerIndex
|
|
|
|
let start = memory.advanced(by: writerIndex)
|
|
|
|
.bindMemory(to: UInt8.self, capacity: cp)
|
2022-02-11 21:26:16 +00:00
|
|
|
|
2023-01-07 00:40:40 +00:00
|
|
|
let ptr = UnsafeBufferPointer<UInt8>(start: start, count: cp)
|
|
|
|
return Array(ptr)
|
2022-02-11 21:26:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:28 +00:00
|
|
|
/// SkipPrefix Skips the first 4 bytes in case one of the following
|
|
|
|
/// functions are called `getPrefixedSizeCheckedRoot` & `getPrefixedSizeRoot`
|
|
|
|
/// which allows us to skip the first 4 bytes instead of recreating the buffer
|
2022-04-06 20:54:01 +00:00
|
|
|
@discardableResult
|
2021-05-14 17:59:28 +00:00
|
|
|
@usableFromInline
|
2022-04-20 06:59:47 +00:00
|
|
|
@inline(__always)
|
2022-04-06 20:54:01 +00:00
|
|
|
mutating func skipPrefix() -> Int32 {
|
2021-05-14 17:59:28 +00:00
|
|
|
_writerSize = _writerSize &- MemoryLayout<Int32>.size
|
2022-04-06 20:54:01 +00:00
|
|
|
return read(def: Int32.self, position: 0)
|
2021-05-14 17:59:28 +00:00
|
|
|
}
|
2022-04-06 20:54:01 +00:00
|
|
|
|
2020-01-27 18:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension ByteBuffer: CustomDebugStringConvertible {
|
2020-04-02 19:55:30 +00:00
|
|
|
|
2020-11-16 16:52:38 +00:00
|
|
|
public var debugDescription: String {
|
|
|
|
"""
|
|
|
|
buffer located at: \(_storage.memory), with capacity of \(_storage.capacity)
|
2024-05-29 20:07:54 +00:00
|
|
|
{ writerSize: \(_writerSize), readerSize: \(reader), writerIndex: \(
|
|
|
|
writerIndex) }
|
2020-11-16 16:52:38 +00:00
|
|
|
"""
|
|
|
|
}
|
[Swift] Swift implementation 🎉🎉 (#5603)
* Implemented the swift version of Flatbuffers
Implemented serailzing, reading, and mutating data from object monster
Fixes mis-aligned pointer issue
Fixes issue when shared strings are removed from table
Adds swift enum, structs code gen
Fixed namespace issues + started implementing the table gen
Added Mutate function to the code generator
Generated linux test cases
Fixed an issue with bools, and structs readers in table writer
Swift docker image added
Updated the test cases, and removed a method parameters in swift
Fixed createVector api when called with scalars
Fixed issues with scalar arrays, and fixed the code gen namespaces, added sample_binary.swift
Cleaned up project
Added enum vectors, and their readers
Refactored code
Added swift into the support document
Added documentation in docs, and fixed a small issue with Data() not being returned correctly
Fixes Lowercase issue, and prevents generating lookups for deprecated keys
* Made all the required funcs to have const + removed unneeded code + fix lowercase func
* Removed transform from lowercased and moved it to function
* Fixes an issue with iOS allocation from read
* Refactored cpp code to be more readable
* casts position into int for position
* Fix enums issue, moves scalar writer code to use memcpy
* Removed c_str from struct function
* Fixed script to generate new objects when ran on travis ci: fix
* Handles deallocating space allocated for structs
* Updated the test cases to adhere to the fileprivate lookup, no mutation for unions, and updated the names of the vector functions
2020-01-09 20:12:10 +00:00
|
|
|
}
|