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 | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 17 | #if !os(WASI) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 18 | import Foundation |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 19 | #else |
| 20 | import SwiftOverlayShims |
| 21 | #endif |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 22 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 23 | /// Mutable is a protocol that allows us to mutate Scalar values within a ``ByteBuffer`` |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 24 | public protocol Mutable { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 25 | /// makes Flatbuffer accessed within the Protocol |
| 26 | var bb: ByteBuffer { get } |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 27 | /// makes position of the ``Table``/``struct`` accessed within the Protocol |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 28 | var postion: Int32 { get } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | extension Mutable { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 32 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 33 | /// Mutates the memory in the buffer, this is only called from the access function of ``Table`` and ``struct`` |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 34 | /// - Parameters: |
| 35 | /// - value: New value to be inserted to the buffer |
| 36 | /// - index: index of the Element |
| 37 | func mutate<T: Scalar>(value: T, o: Int32) -> Bool { |
| 38 | guard o != 0 else { return false } |
| 39 | bb.write(value: value, index: Int(o), direct: true) |
| 40 | return true |
| 41 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | extension Mutable where Self == Table { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 45 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 46 | /// Mutates a value by calling mutate with respect to the position in a ``Table`` |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 47 | /// - Parameters: |
| 48 | /// - value: New value to be inserted to the buffer |
| 49 | /// - index: index of the Element |
| 50 | public func mutate<T: Scalar>(_ value: T, index: Int32) -> Bool { |
| 51 | guard index != 0 else { return false } |
| 52 | return mutate(value: value, o: index + postion) |
| 53 | } |
| 54 | |
| 55 | /// Directly mutates the element by calling mutate |
| 56 | /// |
| 57 | /// Mutates the Element at index ignoring the current position by calling mutate |
| 58 | /// - Parameters: |
| 59 | /// - value: New value to be inserted to the buffer |
| 60 | /// - index: index of the Element |
| 61 | public func directMutate<T: Scalar>(_ value: T, index: Int32) -> Bool { |
| 62 | mutate(value: value, o: index) |
| 63 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | extension Mutable where Self == Struct { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 67 | |
| 68 | /// Mutates a value by calling mutate with respect to the position in the struct |
| 69 | /// - Parameters: |
| 70 | /// - value: New value to be inserted to the buffer |
| 71 | /// - index: index of the Element |
| 72 | public func mutate<T: Scalar>(_ value: T, index: Int32) -> Bool { |
| 73 | mutate(value: value, o: index + postion) |
| 74 | } |
| 75 | |
| 76 | /// Directly mutates the element by calling mutate |
| 77 | /// |
| 78 | /// Mutates the Element at index ignoring the current position by calling mutate |
| 79 | /// - Parameters: |
| 80 | /// - value: New value to be inserted to the buffer |
| 81 | /// - index: index of the Element |
| 82 | public func directMutate<T: Scalar>(_ value: T, index: Int32) -> Bool { |
| 83 | mutate(value: value, o: index) |
| 84 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | extension Struct: Mutable {} |
| 88 | extension Table: Mutable {} |