blob: 9763c3fd2384256eff354426280cd2af74a0c320 [file] [log] [blame]
Austin Schuh58b9b472020-11-25 19:12:44 -08001/*
James Kuszmaul8e62b022022-03-22 09:33:25 -07002 * Copyright 2021 Google Inc. All rights reserved.
Austin Schuh58b9b472020-11-25 19:12:44 -08003 *
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 Schuh2dd86a92022-09-14 21:19:23 -070017#if !os(WASI)
Austin Schuh272c6132020-11-14 16:37:52 -080018import Foundation
Austin Schuh2dd86a92022-09-14 21:19:23 -070019#else
20import SwiftOverlayShims
21#endif
Austin Schuh272c6132020-11-14 16:37:52 -080022
James Kuszmaul8e62b022022-03-22 09:33:25 -070023/// Mutable is a protocol that allows us to mutate Scalar values within a ``ByteBuffer``
Austin Schuh272c6132020-11-14 16:37:52 -080024public protocol Mutable {
Austin Schuh58b9b472020-11-25 19:12:44 -080025 /// makes Flatbuffer accessed within the Protocol
26 var bb: ByteBuffer { get }
James Kuszmaul8e62b022022-03-22 09:33:25 -070027 /// makes position of the ``Table``/``struct`` accessed within the Protocol
Austin Schuh58b9b472020-11-25 19:12:44 -080028 var postion: Int32 { get }
Austin Schuh272c6132020-11-14 16:37:52 -080029}
30
31extension Mutable {
Austin Schuh58b9b472020-11-25 19:12:44 -080032
James Kuszmaul8e62b022022-03-22 09:33:25 -070033 /// Mutates the memory in the buffer, this is only called from the access function of ``Table`` and ``struct``
Austin Schuh58b9b472020-11-25 19:12:44 -080034 /// - 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 Schuh272c6132020-11-14 16:37:52 -080042}
43
44extension Mutable where Self == Table {
Austin Schuh58b9b472020-11-25 19:12:44 -080045
James Kuszmaul8e62b022022-03-22 09:33:25 -070046 /// Mutates a value by calling mutate with respect to the position in a ``Table``
Austin Schuh58b9b472020-11-25 19:12:44 -080047 /// - 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 Schuh272c6132020-11-14 16:37:52 -080064}
65
66extension Mutable where Self == Struct {
Austin Schuh58b9b472020-11-25 19:12:44 -080067
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 Schuh272c6132020-11-14 16:37:52 -080085}
86
87extension Struct: Mutable {}
88extension Table: Mutable {}