blob: adcaa1f4466a92fe12838ee3056daad6ca24e237 [file] [log] [blame]
Austin Schuh58b9b472020-11-25 19:12:44 -08001/*
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 Schuh272c6132020-11-14 16:37:52 -080017import Foundation
18
19/// Mutable is a protocol that allows us to mutate Scalar values within the buffer
20public protocol Mutable {
Austin Schuh58b9b472020-11-25 19:12:44 -080021 /// 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 Schuh272c6132020-11-14 16:37:52 -080025}
26
27extension Mutable {
Austin Schuh58b9b472020-11-25 19:12:44 -080028
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 Schuh272c6132020-11-14 16:37:52 -080038}
39
40extension Mutable where Self == Table {
Austin Schuh58b9b472020-11-25 19:12:44 -080041
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 Schuh272c6132020-11-14 16:37:52 -080060}
61
62extension Mutable where Self == Struct {
Austin Schuh58b9b472020-11-25 19:12:44 -080063
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 Schuh272c6132020-11-14 16:37:52 -080081}
82
83extension Struct: Mutable {}
84extension Table: Mutable {}