Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 1 | /* |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 2 | * Copyright 2021 Google Inc. All rights reserved. |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 17 | import Foundation |
| 18 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 19 | /// ``FlatBufferBuilder`` builds a `FlatBuffer` through manipulating its internal state. |
| 20 | /// |
| 21 | /// This is done by creating a ``ByteBuffer`` that hosts the incoming data and |
| 22 | /// has a hardcoded growth limit of `2GiB` which is set by the Flatbuffers standards. |
| 23 | /// |
| 24 | /// ```swift |
| 25 | /// var builder = FlatBufferBuilder() |
| 26 | /// ``` |
| 27 | /// The builder should be always created as a variable, since it would be passed into the writers |
| 28 | /// |
| 29 | @frozen |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 30 | public struct FlatBufferBuilder { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 31 | |
| 32 | /// Storage for the Vtables used in the buffer are stored in here, so they would be written later in EndTable |
| 33 | @usableFromInline internal var _vtableStorage = VTableStorage() |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 34 | /// Flatbuffer data will be written into |
| 35 | @usableFromInline internal var _bb: ByteBuffer |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 36 | |
| 37 | /// Reference Vtables that were already written to the buffer |
| 38 | private var _vtables: [UOffset] = [] |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 39 | /// A check if the buffer is being written into by a different table |
| 40 | private var isNested = false |
| 41 | /// Dictonary that stores a map of all the strings that were written to the buffer |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 42 | private var stringOffsetMap: [String: Offset] = [:] |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 43 | /// A check to see if finish(::) was ever called to retreive data object |
| 44 | private var finished = false |
| 45 | /// A check to see if the buffer should serialize Default values |
| 46 | private var serializeDefaults: Bool |
| 47 | |
| 48 | /// Current alignment for the buffer |
| 49 | var _minAlignment: Int = 0 { |
| 50 | didSet { |
| 51 | _bb.alignment = _minAlignment |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 52 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /// Gives a read access to the buffer's size |
| 56 | public var size: UOffset { _bb.size } |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 57 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 58 | /// Data representation of the buffer |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 59 | /// |
| 60 | /// Should only be used after ``finish(offset:addPrefix:)`` is called |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 61 | public var data: Data { |
| 62 | assert(finished, "Data shouldn't be called before finish()") |
| 63 | return Data( |
| 64 | bytes: _bb.memory.advanced(by: _bb.writerIndex), |
| 65 | count: _bb.capacity &- _bb.writerIndex) |
| 66 | } |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 67 | |
| 68 | /// Returns the underlying bytes in the ``ByteBuffer`` |
| 69 | /// |
| 70 | /// Note: This should be used with caution. |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 71 | public var fullSizedByteArray: [UInt8] { |
| 72 | let ptr = UnsafeBufferPointer( |
| 73 | start: _bb.memory.assumingMemoryBound(to: UInt8.self), |
| 74 | count: _bb.capacity) |
| 75 | return Array(ptr) |
| 76 | } |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 77 | |
| 78 | /// Returns the written bytes into the ``ByteBuffer`` |
| 79 | /// |
| 80 | /// Should only be used after ``finish(offset:addPrefix:)`` is called |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 81 | public var sizedByteArray: [UInt8] { |
| 82 | assert(finished, "Data shouldn't be called before finish()") |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 83 | return _bb.underlyingBytes |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 84 | } |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 85 | |
| 86 | /// Returns the original ``ByteBuffer`` |
| 87 | /// |
| 88 | /// Returns the current buffer that was just created |
| 89 | /// with the offsets, and data written to it. |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 90 | public var buffer: ByteBuffer { _bb } |
| 91 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 92 | /// Returns a newly created sized ``ByteBuffer`` |
| 93 | /// |
| 94 | /// returns a new buffer that is sized to the data written |
| 95 | /// to the main buffer |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 96 | public var sizedBuffer: ByteBuffer { |
| 97 | assert(finished, "Data shouldn't be called before finish()") |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 98 | return ByteBuffer( |
| 99 | memory: _bb.memory.advanced(by: _bb.reader), |
| 100 | count: Int(_bb.size)) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // MARK: - Init |
| 104 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 105 | /// Initialize the buffer with a size |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 106 | /// - Parameters: |
| 107 | /// - initialSize: Initial size for the buffer |
| 108 | /// - force: Allows default to be serialized into the buffer |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 109 | /// |
| 110 | /// This initializes a new builder with an initialSize that would initialize |
| 111 | /// a new ``ByteBuffer``. ``FlatBufferBuilder`` by default doesnt serialize defaults |
| 112 | /// however the builder can be force by passing true for `serializeDefaults` |
| 113 | public init( |
| 114 | initialSize: Int32 = 1024, |
| 115 | serializeDefaults force: Bool = false) |
| 116 | { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 117 | assert(initialSize > 0, "Size should be greater than zero!") |
| 118 | guard isLitteEndian else { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 119 | fatalError( |
| 120 | "Reading/Writing a buffer in big endian machine is not supported on swift") |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 121 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 122 | serializeDefaults = force |
| 123 | _bb = ByteBuffer(initialSize: Int(initialSize)) |
| 124 | } |
| 125 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 126 | /// Clears the builder and the buffer from the written data. |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 127 | mutating public func clear() { |
| 128 | _minAlignment = 0 |
| 129 | isNested = false |
| 130 | stringOffsetMap = [:] |
| 131 | _vtables = [] |
| 132 | _vtableStorage.clear() |
| 133 | _bb.clear() |
| 134 | } |
| 135 | |
| 136 | // MARK: - Create Tables |
| 137 | |
| 138 | /// Checks if the required fields were serialized into the buffer |
| 139 | /// - Parameters: |
| 140 | /// - table: offset for the table |
| 141 | /// - fields: Array of all the important fields to be serialized |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 142 | /// |
| 143 | /// *NOTE: Never call this function, this is only supposed to be called |
| 144 | /// by the generated code* |
| 145 | mutating public func require(table: Offset, fields: [Int32]) { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 146 | for field in fields { |
| 147 | let start = _bb.capacity &- Int(table.o) |
| 148 | let startTable = start &- Int(_bb.read(def: Int32.self, position: start)) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 149 | let isOkay = _bb.read( |
| 150 | def: VOffset.self, |
| 151 | position: startTable &+ Int(field)) != 0 |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 152 | assert(isOkay, "Flatbuffers requires the following field") |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 153 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /// Finished the buffer by adding the file id and then calling finish |
| 157 | /// - Parameters: |
| 158 | /// - offset: Offset of the table |
| 159 | /// - fileId: Takes the fileId |
| 160 | /// - prefix: if false it wont add the size of the buffer |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 161 | /// |
| 162 | /// ``finish(offset:fileId:addPrefix:)`` should be called at the end of creating |
| 163 | /// a table |
| 164 | /// ```swift |
| 165 | /// var root = SomeObject |
| 166 | /// .createObject(&builder, |
| 167 | /// name: nameOffset) |
| 168 | /// builder.finish( |
| 169 | /// offset: root, |
| 170 | /// fileId: "ax1a", |
| 171 | /// addPrefix: true) |
| 172 | /// ``` |
| 173 | /// File id would append a file id name at the end of the written bytes before, |
| 174 | /// finishing the buffer. |
| 175 | /// |
| 176 | /// Whereas, if `addPrefix` is true, the written bytes would |
| 177 | /// include the size of the current buffer. |
| 178 | mutating public func finish( |
| 179 | offset: Offset, |
| 180 | fileId: String, |
| 181 | addPrefix prefix: Bool = false) |
| 182 | { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 183 | let size = MemoryLayout<UOffset>.size |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 184 | preAlign( |
| 185 | len: size &+ (prefix ? size : 0) &+ FileIdLength, |
| 186 | alignment: _minAlignment) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 187 | assert(fileId.count == FileIdLength, "Flatbuffers requires file id to be 4") |
| 188 | _bb.push(string: fileId, len: 4) |
| 189 | finish(offset: offset, addPrefix: prefix) |
| 190 | } |
| 191 | |
| 192 | /// Finished the buffer by adding the file id, offset, and prefix to it. |
| 193 | /// - Parameters: |
| 194 | /// - offset: Offset of the table |
| 195 | /// - prefix: if false it wont add the size of the buffer |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 196 | /// |
| 197 | /// ``finish(offset:addPrefix:)`` should be called at the end of creating |
| 198 | /// a table |
| 199 | /// ```swift |
| 200 | /// var root = SomeObject |
| 201 | /// .createObject(&builder, |
| 202 | /// name: nameOffset) |
| 203 | /// builder.finish( |
| 204 | /// offset: root, |
| 205 | /// addPrefix: true) |
| 206 | /// ``` |
| 207 | /// If `addPrefix` is true, the written bytes would |
| 208 | /// include the size of the current buffer. |
| 209 | mutating public func finish( |
| 210 | offset: Offset, |
| 211 | addPrefix prefix: Bool = false) |
| 212 | { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 213 | notNested() |
| 214 | let size = MemoryLayout<UOffset>.size |
| 215 | preAlign(len: size &+ (prefix ? size : 0), alignment: _minAlignment) |
| 216 | push(element: refer(to: offset.o)) |
| 217 | if prefix { push(element: _bb.size) } |
| 218 | _vtableStorage.clear() |
| 219 | finished = true |
| 220 | } |
| 221 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 222 | /// ``startTable(with:)`` will let the builder know, that a new object is being serialized. |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 223 | /// |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 224 | /// The function will fatalerror if called while there is another object being serialized. |
| 225 | /// ```swift |
| 226 | /// let start = Monster |
| 227 | /// .startMonster(&fbb) |
| 228 | /// ``` |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 229 | /// - Parameter numOfFields: Number of elements to be written to the buffer |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 230 | /// - Returns: Offset of the newly started table |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 231 | mutating public func startTable(with numOfFields: Int) -> UOffset { |
| 232 | notNested() |
| 233 | isNested = true |
| 234 | _vtableStorage.start(count: numOfFields) |
| 235 | return _bb.size |
| 236 | } |
| 237 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 238 | /// ``endTable(at:)`` will let the ``FlatBufferBuilder`` know that the |
| 239 | /// object that's written to it is completed |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 240 | /// |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 241 | /// This would be called after all the elements are serialized, |
| 242 | /// it will add the current vtable into the ``ByteBuffer``. |
| 243 | /// The functions will `fatalError` in case the object is called |
| 244 | /// without ``startTable(with:)``, or the object has exceeded the limit of 2GB. |
| 245 | /// |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 246 | /// - Parameter startOffset:Start point of the object written |
| 247 | /// - returns: The root of the table |
| 248 | mutating public func endTable(at startOffset: UOffset) -> UOffset { |
| 249 | assert(isNested, "Calling endtable without calling starttable") |
| 250 | let sizeofVoffset = MemoryLayout<VOffset>.size |
| 251 | let vTableOffset = push(element: SOffset(0)) |
| 252 | |
| 253 | let tableObjectSize = vTableOffset &- startOffset |
| 254 | assert(tableObjectSize < 0x10000, "Buffer can't grow beyond 2 Gigabytes") |
| 255 | let _max = Int(_vtableStorage.maxOffset) &+ sizeofVoffset |
| 256 | |
| 257 | _bb.fill(padding: _max) |
| 258 | _bb.write( |
| 259 | value: VOffset(tableObjectSize), |
| 260 | index: _bb.writerIndex &+ sizeofVoffset, |
| 261 | direct: true) |
| 262 | _bb.write(value: VOffset(_max), index: _bb.writerIndex, direct: true) |
| 263 | |
| 264 | var itr = 0 |
| 265 | while itr < _vtableStorage.writtenIndex { |
| 266 | let loaded = _vtableStorage.load(at: itr) |
| 267 | itr = itr &+ _vtableStorage.size |
| 268 | guard loaded.offset != 0 else { continue } |
| 269 | let _index = (_bb.writerIndex &+ Int(loaded.position)) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 270 | _bb.write( |
| 271 | value: VOffset(vTableOffset &- loaded.offset), |
| 272 | index: _index, |
| 273 | direct: true) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 276 | _vtableStorage.clear() |
| 277 | let vt_use = _bb.size |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 278 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 279 | var isAlreadyAdded: Int? |
| 280 | |
| 281 | let vt2 = _bb.memory.advanced(by: _bb.writerIndex) |
| 282 | let len2 = vt2.load(fromByteOffset: 0, as: Int16.self) |
| 283 | |
| 284 | for table in _vtables { |
| 285 | let position = _bb.capacity &- Int(table) |
| 286 | let vt1 = _bb.memory.advanced(by: position) |
| 287 | let len1 = _bb.read(def: Int16.self, position: position) |
| 288 | if len2 != len1 || 0 != memcmp(vt1, vt2, Int(len2)) { continue } |
| 289 | |
| 290 | isAlreadyAdded = Int(table) |
| 291 | break |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 294 | if let offset = isAlreadyAdded { |
| 295 | let vTableOff = Int(vTableOffset) |
| 296 | let space = _bb.capacity &- vTableOff |
| 297 | _bb.write(value: Int32(offset &- vTableOff), index: space, direct: true) |
| 298 | _bb.pop(_bb.capacity &- space) |
| 299 | } else { |
| 300 | _bb.write(value: Int32(vt_use &- vTableOffset), index: Int(vTableOffset)) |
| 301 | _vtables.append(_bb.size) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 302 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 303 | isNested = false |
| 304 | return vTableOffset |
| 305 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 306 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 307 | // MARK: - Builds Buffer |
| 308 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 309 | /// Asserts to see if the object is not nested |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 310 | @usableFromInline |
| 311 | mutating internal func notNested() { |
| 312 | assert(!isNested, "Object serialization must not be nested") |
| 313 | } |
| 314 | |
| 315 | /// Changes the minimuim alignment of the buffer |
| 316 | /// - Parameter size: size of the current alignment |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 317 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 318 | mutating internal func minAlignment(size: Int) { |
| 319 | if size > _minAlignment { |
| 320 | _minAlignment = size |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 321 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /// Gets the padding for the current element |
| 325 | /// - Parameters: |
| 326 | /// - bufSize: Current size of the buffer + the offset of the object to be written |
| 327 | /// - elementSize: Element size |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 328 | @inline(__always) |
| 329 | mutating internal func padding( |
| 330 | bufSize: UInt32, |
| 331 | elementSize: UInt32) -> UInt32 |
| 332 | { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 333 | ((~bufSize) &+ 1) & (elementSize - 1) |
| 334 | } |
| 335 | |
| 336 | /// Prealigns the buffer before writting a new object into the buffer |
| 337 | /// - Parameters: |
| 338 | /// - len:Length of the object |
| 339 | /// - alignment: Alignment type |
| 340 | @usableFromInline |
| 341 | mutating internal func preAlign(len: Int, alignment: Int) { |
| 342 | minAlignment(size: alignment) |
| 343 | _bb.fill(padding: Int(padding( |
| 344 | bufSize: _bb.size &+ UOffset(len), |
| 345 | elementSize: UOffset(alignment)))) |
| 346 | } |
| 347 | |
| 348 | /// Prealigns the buffer before writting a new object into the buffer |
| 349 | /// - Parameters: |
| 350 | /// - len: Length of the object |
| 351 | /// - type: Type of the object to be written |
| 352 | @usableFromInline |
| 353 | mutating internal func preAlign<T: Scalar>(len: Int, type: T.Type) { |
| 354 | preAlign(len: len, alignment: MemoryLayout<T>.size) |
| 355 | } |
| 356 | |
| 357 | /// Refers to an object that's written in the buffer |
| 358 | /// - Parameter off: the objects index value |
| 359 | @usableFromInline |
| 360 | mutating internal func refer(to off: UOffset) -> UOffset { |
| 361 | let size = MemoryLayout<UOffset>.size |
| 362 | preAlign(len: size, alignment: size) |
| 363 | return _bb.size &- off &+ UInt32(size) |
| 364 | } |
| 365 | |
| 366 | /// Tracks the elements written into the buffer |
| 367 | /// - Parameters: |
| 368 | /// - offset: The offset of the element witten |
| 369 | /// - position: The position of the element |
| 370 | @usableFromInline |
| 371 | mutating internal func track(offset: UOffset, at position: VOffset) { |
| 372 | _vtableStorage.add(loc: FieldLoc(offset: offset, position: position)) |
| 373 | } |
| 374 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 375 | // MARK: - Inserting Vectors |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 376 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 377 | /// ``startVector(_:elementSize:)`` creates a new vector within buffer |
| 378 | /// |
| 379 | /// The function checks if there is a current object being written, if |
| 380 | /// the check passes it creates a buffer alignment of `length * elementSize` |
| 381 | /// ```swift |
| 382 | /// builder.startVector( |
| 383 | /// int32Values.count, elementSize: 4) |
| 384 | /// ``` |
| 385 | /// |
| 386 | /// - Parameters: |
| 387 | /// - len: Length of vector to be created |
| 388 | /// - elementSize: Size of object type to be written |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 389 | mutating public func startVector(_ len: Int, elementSize: Int) { |
| 390 | notNested() |
| 391 | isNested = true |
| 392 | preAlign(len: len &* elementSize, type: UOffset.self) |
| 393 | preAlign(len: len &* elementSize, alignment: elementSize) |
| 394 | } |
| 395 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 396 | /// ``endVector(len:)`` ends the currently created vector |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 397 | /// |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 398 | /// Calling ``endVector(len:)`` requires the length, of the current |
| 399 | /// vector. The length would be pushed to indicate the count of numbers |
| 400 | /// within the vector. If ``endVector(len:)`` is called without |
| 401 | /// ``startVector(_:elementSize:)`` it asserts. |
| 402 | /// |
| 403 | /// ```swift |
| 404 | /// let vectorOffset = builder. |
| 405 | /// endVector(len: int32Values.count) |
| 406 | /// ``` |
| 407 | /// |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 408 | /// - Parameter len: Length of the buffer |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 409 | /// - Returns: Returns the current ``Offset`` in the ``ByteBuffer`` |
| 410 | mutating public func endVector(len: Int) -> Offset { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 411 | assert(isNested, "Calling endVector without calling startVector") |
| 412 | isNested = false |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 413 | return Offset(offset: push(element: Int32(len))) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 414 | } |
| 415 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 416 | /// Creates a vector of type ``Scalar`` into the ``ByteBuffer`` |
| 417 | /// |
| 418 | /// ``createVector(_:)-4swl0`` writes a vector of type Scalars into |
| 419 | /// ``ByteBuffer``. This is a convenient method instead of calling, |
| 420 | /// ``startVector(_:elementSize:)`` and then ``endVector(len:)`` |
| 421 | /// ```swift |
| 422 | /// let vectorOffset = builder. |
| 423 | /// createVector([1, 2, 3, 4]) |
| 424 | /// ``` |
| 425 | /// |
| 426 | /// The underlying implementation simply calls ``createVector(_:size:)-4lhrv`` |
| 427 | /// |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 428 | /// - Parameter elements: elements to be written into the buffer |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 429 | /// - returns: ``Offset`` of the vector |
| 430 | mutating public func createVector<T: Scalar>(_ elements: [T]) -> Offset { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 431 | createVector(elements, size: elements.count) |
| 432 | } |
| 433 | |
| 434 | /// Creates a vector of type Scalar in the buffer |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 435 | /// |
| 436 | /// ``createVector(_:)-4swl0`` writes a vector of type Scalars into |
| 437 | /// ``ByteBuffer``. This is a convenient method instead of calling, |
| 438 | /// ``startVector(_:elementSize:)`` and then ``endVector(len:)`` |
| 439 | /// ```swift |
| 440 | /// let vectorOffset = builder. |
| 441 | /// createVector([1, 2, 3, 4], size: 4) |
| 442 | /// ``` |
| 443 | /// |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 444 | /// - Parameter elements: Elements to be written into the buffer |
| 445 | /// - Parameter size: Count of elements |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 446 | /// - returns: ``Offset`` of the vector |
| 447 | mutating public func createVector<T: Scalar>( |
| 448 | _ elements: [T], |
| 449 | size: Int) -> Offset |
| 450 | { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 451 | let size = size |
| 452 | startVector(size, elementSize: MemoryLayout<T>.size) |
| 453 | _bb.push(elements: elements) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 454 | return endVector(len: size) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 455 | } |
| 456 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 457 | /// Creates a vector of type ``Enum`` into the ``ByteBuffer`` |
| 458 | /// |
| 459 | /// ``createVector(_:)-9h189`` writes a vector of type ``Enum`` into |
| 460 | /// ``ByteBuffer``. This is a convenient method instead of calling, |
| 461 | /// ``startVector(_:elementSize:)`` and then ``endVector(len:)`` |
| 462 | /// ```swift |
| 463 | /// let vectorOffset = builder. |
| 464 | /// createVector([.swift, .cpp]) |
| 465 | /// ``` |
| 466 | /// |
| 467 | /// The underlying implementation simply calls ``createVector(_:size:)-7cx6z`` |
| 468 | /// |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 469 | /// - Parameter elements: elements to be written into the buffer |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 470 | /// - returns: ``Offset`` of the vector |
| 471 | mutating public func createVector<T: Enum>(_ elements: [T]) -> Offset { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 472 | createVector(elements, size: elements.count) |
| 473 | } |
| 474 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 475 | /// Creates a vector of type ``Enum`` into the ``ByteBuffer`` |
| 476 | /// |
| 477 | /// ``createVector(_:)-9h189`` writes a vector of type ``Enum`` into |
| 478 | /// ``ByteBuffer``. This is a convenient method instead of calling, |
| 479 | /// ``startVector(_:elementSize:)`` and then ``endVector(len:)`` |
| 480 | /// ```swift |
| 481 | /// let vectorOffset = builder. |
| 482 | /// createVector([.swift, .cpp]) |
| 483 | /// ``` |
| 484 | /// |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 485 | /// - Parameter elements: Elements to be written into the buffer |
| 486 | /// - Parameter size: Count of elements |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 487 | /// - returns: ``Offset`` of the vector |
| 488 | mutating public func createVector<T: Enum>( |
| 489 | _ elements: [T], |
| 490 | size: Int) -> Offset |
| 491 | { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 492 | let size = size |
| 493 | startVector(size, elementSize: T.byteSize) |
| 494 | for e in elements.reversed() { |
| 495 | _bb.push(value: e.value, len: T.byteSize) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 496 | } |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 497 | return endVector(len: size) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 498 | } |
| 499 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 500 | /// Creates a vector of already written offsets |
| 501 | /// |
| 502 | /// ``createVector(ofOffsets:)`` creates a vector of ``Offset`` into |
| 503 | /// ``ByteBuffer``. This is a convenient method instead of calling, |
| 504 | /// ``startVector(_:elementSize:)`` and then ``endVector(len:)``. |
| 505 | /// |
| 506 | /// The underlying implementation simply calls ``createVector(ofOffsets:len:)`` |
| 507 | /// |
| 508 | /// ```swift |
| 509 | /// let namesOffsets = builder. |
| 510 | /// createVector(ofOffsets: [name1, name2]) |
| 511 | /// ``` |
| 512 | /// - Parameter offsets: Array of offsets of type ``Offset`` |
| 513 | /// - returns: ``Offset`` of the vector |
| 514 | mutating public func createVector(ofOffsets offsets: [Offset]) -> Offset { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 515 | createVector(ofOffsets: offsets, len: offsets.count) |
| 516 | } |
| 517 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 518 | /// Creates a vector of already written offsets |
| 519 | /// |
| 520 | /// ``createVector(ofOffsets:)`` creates a vector of ``Offset`` into |
| 521 | /// ``ByteBuffer``. This is a convenient method instead of calling, |
| 522 | /// ``startVector(_:elementSize:)`` and then ``endVector(len:)`` |
| 523 | /// |
| 524 | /// ```swift |
| 525 | /// let namesOffsets = builder. |
| 526 | /// createVector(ofOffsets: [name1, name2]) |
| 527 | /// ``` |
| 528 | /// |
| 529 | /// - Parameter offsets: Array of offsets of type ``Offset`` |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 530 | /// - Parameter size: Count of elements |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 531 | /// - returns: ``Offset`` of the vector |
| 532 | mutating public func createVector( |
| 533 | ofOffsets offsets: [Offset], |
| 534 | len: Int) -> Offset |
| 535 | { |
| 536 | startVector(len, elementSize: MemoryLayout<Offset>.size) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 537 | for o in offsets.reversed() { |
| 538 | push(element: o) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 539 | } |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 540 | return endVector(len: len) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 541 | } |
| 542 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 543 | /// Creates a vector of strings |
| 544 | /// |
| 545 | /// ``createVector(ofStrings:)`` creates a vector of `String` into |
| 546 | /// ``ByteBuffer``. This is a convenient method instead of manually |
| 547 | /// creating the string offsets, you simply pass it to this function |
| 548 | /// and it would write the strings into the ``ByteBuffer``. |
| 549 | /// After that it calls ``createVector(ofOffsets:)`` |
| 550 | /// |
| 551 | /// ```swift |
| 552 | /// let namesOffsets = builder. |
| 553 | /// createVector(ofStrings: ["Name", "surname"]) |
| 554 | /// ``` |
| 555 | /// |
| 556 | /// - Parameter str: Array of string |
| 557 | /// - returns: ``Offset`` of the vector |
| 558 | mutating public func createVector(ofStrings str: [String]) -> Offset { |
| 559 | var offsets: [Offset] = [] |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 560 | for s in str { |
| 561 | offsets.append(create(string: s)) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 562 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 563 | return createVector(ofOffsets: offsets) |
| 564 | } |
| 565 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 566 | /// Creates a vector of type ``NativeStruct``. |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 567 | /// |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 568 | /// Any swift struct in the generated code, should confirm to |
| 569 | /// ``NativeStruct``. Since the generated swift structs are padded |
| 570 | /// to the `FlatBuffers` standards. |
| 571 | /// |
| 572 | /// ```swift |
| 573 | /// let offsets = builder. |
| 574 | /// createVector(ofStructs: [NativeStr(num: 1), NativeStr(num: 2)]) |
| 575 | /// ``` |
| 576 | /// |
| 577 | /// - Parameter structs: A vector of ``NativeStruct`` |
| 578 | /// - Returns: ``Offset`` of the vector |
| 579 | mutating public func createVector<T: NativeStruct>(ofStructs structs: [T]) |
| 580 | -> Offset |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 581 | { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 582 | startVector( |
| 583 | structs.count * MemoryLayout<T>.size, |
| 584 | elementSize: MemoryLayout<T>.alignment) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 585 | for i in structs.reversed() { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 586 | _ = create(struct: i) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 587 | } |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 588 | return endVector(len: structs.count) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | // MARK: - Inserting Structs |
| 592 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 593 | /// Writes a ``NativeStruct`` into the ``ByteBuffer`` |
| 594 | /// |
| 595 | /// Adds a native struct that's build and padded according |
| 596 | /// to `FlatBuffers` standards. with a predefined position. |
| 597 | /// |
| 598 | /// ```swift |
| 599 | /// let offset = builder.create( |
| 600 | /// struct: NativeStr(num: 1), |
| 601 | /// position: 10) |
| 602 | /// ``` |
| 603 | /// |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 604 | /// - Parameters: |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 605 | /// - s: ``NativeStruct`` to be inserted into the ``ByteBuffer`` |
| 606 | /// - position: The predefined position of the object |
| 607 | /// - Returns: ``Offset`` of written struct |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 608 | @discardableResult |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 609 | mutating public func create<T: NativeStruct>( |
| 610 | struct s: T, position: VOffset) -> Offset |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 611 | { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 612 | let offset = create(struct: s) |
| 613 | _vtableStorage.add(loc: FieldLoc( |
| 614 | offset: _bb.size, |
| 615 | position: VOffset(position))) |
| 616 | return offset |
| 617 | } |
| 618 | |
| 619 | /// Writes a ``NativeStruct`` into the ``ByteBuffer`` |
| 620 | /// |
| 621 | /// Adds a native struct that's build and padded according |
| 622 | /// to `FlatBuffers` standards, directly into the buffer without |
| 623 | /// a predefined position. |
| 624 | /// |
| 625 | /// ```swift |
| 626 | /// let offset = builder.create( |
| 627 | /// struct: NativeStr(num: 1)) |
| 628 | /// ``` |
| 629 | /// |
| 630 | /// - Parameters: |
| 631 | /// - s: ``NativeStruct`` to be inserted into the ``ByteBuffer`` |
| 632 | /// - Returns: ``Offset`` of written struct |
| 633 | @discardableResult |
| 634 | mutating public func create<T: NativeStruct>( |
| 635 | struct s: T) -> Offset |
| 636 | { |
| 637 | let size = MemoryLayout<T>.size |
| 638 | preAlign(len: size, alignment: MemoryLayout<T>.alignment) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 639 | _bb.push(struct: s, size: size) |
| 640 | return Offset(offset: _bb.size) |
| 641 | } |
| 642 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 643 | // MARK: - Inserting Strings |
| 644 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 645 | /// Insets a string into the buffer of type `UTF8` |
| 646 | /// |
| 647 | /// Adds a swift string into ``ByteBuffer`` by encoding it |
| 648 | /// using `UTF8` |
| 649 | /// |
| 650 | /// ```swift |
| 651 | /// let nameOffset = builder |
| 652 | /// .create(string: "welcome") |
| 653 | /// ``` |
| 654 | /// |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 655 | /// - Parameter str: String to be serialized |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 656 | /// - returns: ``Offset`` of inserted string |
| 657 | mutating public func create(string str: String?) -> Offset { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 658 | guard let str = str else { return Offset() } |
| 659 | let len = str.utf8.count |
| 660 | notNested() |
| 661 | preAlign(len: len &+ 1, type: UOffset.self) |
| 662 | _bb.fill(padding: 1) |
| 663 | _bb.push(string: str, len: len) |
| 664 | push(element: UOffset(len)) |
| 665 | return Offset(offset: _bb.size) |
| 666 | } |
| 667 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 668 | /// Insets a shared string into the buffer of type `UTF8` |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 669 | /// |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 670 | /// Adds a swift string into ``ByteBuffer`` by encoding it |
| 671 | /// using `UTF8`. The function will check if the string, |
| 672 | /// is already written to the ``ByteBuffer`` |
| 673 | /// |
| 674 | /// ```swift |
| 675 | /// let nameOffset = builder |
| 676 | /// .createShared(string: "welcome") |
| 677 | /// |
| 678 | /// |
| 679 | /// let secondOffset = builder |
| 680 | /// .createShared(string: "welcome") |
| 681 | /// |
| 682 | /// assert(nameOffset.o == secondOffset.o) |
| 683 | /// ``` |
| 684 | /// |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 685 | /// - Parameter str: String to be serialized |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 686 | /// - returns: ``Offset`` of inserted string |
| 687 | mutating public func createShared(string str: String?) -> Offset { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 688 | guard let str = str else { return Offset() } |
| 689 | if let offset = stringOffsetMap[str] { |
| 690 | return offset |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 691 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 692 | let offset = create(string: str) |
| 693 | stringOffsetMap[str] = offset |
| 694 | return offset |
| 695 | } |
| 696 | |
| 697 | // MARK: - Inseting offsets |
| 698 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 699 | /// Writes the ``Offset`` of an already written table |
| 700 | /// |
| 701 | /// Writes the ``Offset`` of a table if not empty into the |
| 702 | /// ``ByteBuffer`` |
| 703 | /// |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 704 | /// - Parameters: |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 705 | /// - offset: ``Offset`` of another object to be written |
| 706 | /// - position: The predefined position of the object |
| 707 | mutating public func add(offset: Offset, at position: VOffset) { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 708 | if offset.isEmpty { return } |
| 709 | add(element: refer(to: offset.o), def: 0, at: position) |
| 710 | } |
| 711 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 712 | /// Pushes a value of type ``Offset`` into the ``ByteBuffer`` |
| 713 | /// - Parameter o: ``Offset`` |
| 714 | /// - returns: Current position of the ``Offset`` |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 715 | @discardableResult |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 716 | mutating public func push(element o: Offset) -> UOffset { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 717 | push(element: refer(to: o.o)) |
| 718 | } |
| 719 | |
| 720 | // MARK: - Inserting Scalars to Buffer |
| 721 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 722 | /// Writes a ``Scalar`` value into ``ByteBuffer`` |
| 723 | /// |
| 724 | /// ``add(element:def:at:)`` takes in a default value, and current value |
| 725 | /// and the position within the `VTable`. The default value would not |
| 726 | /// be serialized if the value is the same as the current value or |
| 727 | /// `serializeDefaults` is equal to false. |
| 728 | /// |
| 729 | /// If serializing defaults is important ``init(initialSize:serializeDefaults:)``, |
| 730 | /// passing true for `serializeDefaults` would do the job. |
| 731 | /// |
| 732 | /// ```swift |
| 733 | /// // Adds 10 to the buffer |
| 734 | /// builder.add(element: Int(10), def: 1, position 12) |
| 735 | /// ``` |
| 736 | /// |
| 737 | /// *NOTE: Never call this manually* |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 738 | /// |
| 739 | /// - Parameters: |
| 740 | /// - element: Element to insert |
| 741 | /// - def: Default value for that element |
| 742 | /// - position: The predefined position of the element |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 743 | mutating public func add<T: Scalar>( |
| 744 | element: T, |
| 745 | def: T, |
| 746 | at position: VOffset) |
| 747 | { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 748 | if element == def && !serializeDefaults { return } |
| 749 | track(offset: push(element: element), at: position) |
| 750 | } |
| 751 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 752 | /// Writes a optional ``Scalar`` value into ``ByteBuffer`` |
| 753 | /// |
| 754 | /// Takes an optional value to be written into the ``ByteBuffer`` |
| 755 | /// |
| 756 | /// *NOTE: Never call this manually* |
| 757 | /// |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 758 | /// - Parameters: |
| 759 | /// - element: Optional element of type scalar |
| 760 | /// - position: The predefined position of the element |
| 761 | mutating public func add<T: Scalar>(element: T?, at position: VOffset) { |
| 762 | guard let element = element else { return } |
| 763 | track(offset: push(element: element), at: position) |
| 764 | } |
| 765 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 766 | /// Pushes a values of type ``Scalar`` into the ``ByteBuffer`` |
| 767 | /// |
| 768 | /// *NOTE: Never call this manually* |
| 769 | /// |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 770 | /// - Parameter element: Element to insert |
| 771 | /// - returns: Postion of the Element |
| 772 | @discardableResult |
| 773 | mutating public func push<T: Scalar>(element: T) -> UOffset { |
| 774 | let size = MemoryLayout<T>.size |
| 775 | preAlign( |
| 776 | len: size, |
| 777 | alignment: size) |
| 778 | _bb.push(value: element, len: size) |
| 779 | return _bb.size |
| 780 | } |
| 781 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | extension FlatBufferBuilder: CustomDebugStringConvertible { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 785 | |
| 786 | public var debugDescription: String { |
| 787 | """ |
| 788 | buffer debug: |
| 789 | \(_bb) |
| 790 | builder debug: |
| 791 | { finished: \(finished), serializeDefaults: \(serializeDefaults), isNested: \(isNested) } |
| 792 | """ |
| 793 | } |
| 794 | |
| 795 | /// VTableStorage is a class to contain the VTable buffer that would be serialized into buffer |
| 796 | @usableFromInline |
| 797 | internal class VTableStorage { |
| 798 | /// Memory check since deallocating each time we want to clear would be expensive |
| 799 | /// and memory leaks would happen if we dont deallocate the first allocated memory. |
| 800 | /// memory is promised to be available before adding `FieldLoc` |
| 801 | private var memoryInUse = false |
| 802 | /// Size of FieldLoc in memory |
| 803 | let size = MemoryLayout<FieldLoc>.stride |
| 804 | /// Memeory buffer |
| 805 | var memory: UnsafeMutableRawBufferPointer! |
| 806 | /// Capacity of the current buffer |
| 807 | var capacity: Int = 0 |
| 808 | /// Maximuim offset written to the class |
| 809 | var maxOffset: VOffset = 0 |
| 810 | /// number of fields written into the buffer |
| 811 | var numOfFields: Int = 0 |
| 812 | /// Last written Index |
| 813 | var writtenIndex: Int = 0 |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 814 | |
| 815 | /// Creates the memory to store the buffer in |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 816 | @usableFromInline |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 817 | init() { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 818 | memory = UnsafeMutableRawBufferPointer.allocate( |
| 819 | byteCount: 0, |
| 820 | alignment: 0) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 821 | } |
| 822 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 823 | deinit { |
| 824 | memory.deallocate() |
| 825 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 826 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 827 | /// Builds a buffer with byte count of fieldloc.size * count of field numbers |
| 828 | /// - Parameter count: number of fields to be written |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 829 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 830 | func start(count: Int) { |
| 831 | assert(count >= 0, "number of fields should NOT be negative") |
| 832 | let capacity = count &* size |
| 833 | ensure(space: capacity) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 834 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 835 | |
| 836 | /// Adds a FieldLoc into the buffer, which would track how many have been written, |
| 837 | /// and max offset |
| 838 | /// - Parameter loc: Location of encoded element |
| 839 | func add(loc: FieldLoc) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 840 | memory.baseAddress?.advanced(by: writtenIndex).storeBytes( |
| 841 | of: loc, |
| 842 | as: FieldLoc.self) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 843 | writtenIndex = writtenIndex &+ size |
| 844 | numOfFields = numOfFields &+ 1 |
| 845 | maxOffset = max(loc.position, maxOffset) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 846 | } |
| 847 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 848 | /// Clears the data stored related to the encoded buffer |
| 849 | func clear() { |
| 850 | maxOffset = 0 |
| 851 | numOfFields = 0 |
| 852 | writtenIndex = 0 |
| 853 | } |
| 854 | |
| 855 | /// Ensure that the buffer has enough space instead of recreating the buffer each time. |
| 856 | /// - Parameter space: space required for the new vtable |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 857 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 858 | func ensure(space: Int) { |
| 859 | guard space &+ writtenIndex > capacity else { return } |
| 860 | memory.deallocate() |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 861 | memory = UnsafeMutableRawBufferPointer.allocate( |
| 862 | byteCount: space, |
| 863 | alignment: size) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 864 | capacity = space |
| 865 | } |
| 866 | |
| 867 | /// Loads an object of type `FieldLoc` from buffer memory |
| 868 | /// - Parameter index: index of element |
| 869 | /// - Returns: a FieldLoc at index |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 870 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 871 | func load(at index: Int) -> FieldLoc { |
| 872 | memory.load(fromByteOffset: index, as: FieldLoc.self) |
| 873 | } |
| 874 | |
| 875 | } |
| 876 | |
| 877 | internal struct FieldLoc { |
| 878 | var offset: UOffset |
| 879 | var position: VOffset |
| 880 | } |
| 881 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 882 | } |