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