xref: /aosp_15_r20/external/flatbuffers/swift/Sources/FlatBuffers/Mutable.swift (revision 890232f25432b36107d06881e0a25aaa6b473652)
1 /*
2  * Copyright 2021 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 
17 #if !os(WASI)
18 import Foundation
19 #else
20 import SwiftOverlayShims
21 #endif
22 
23 /// Mutable is a protocol that allows us to mutate Scalar values within a ``ByteBuffer``
24 public protocol Mutable {
25   /// makes Flatbuffer accessed within the Protocol
26   var bb: ByteBuffer { get }
27   /// makes position of the ``Table``/``struct`` accessed within the Protocol
28   var postion: Int32 { get }
29 }
30 
31 extension Mutable {
32 
33   /// Mutates the memory in the buffer, this is only called from the access function of ``Table`` and ``struct``
34   /// - Parameters:
35   ///   - value: New value to be inserted to the buffer
36   ///   - index: index of the Element
mutate<T: Scalar>null37   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   }
42 }
43 
44 extension Mutable where Self == Table {
45 
46   /// Mutates a value by calling mutate with respect to the position in a ``Table``
47   /// - Parameters:
48   ///   - value: New value to be inserted to the buffer
49   ///   - index: index of the Element
mutate<T: Scalar>null50   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
directMutate<T: Scalar>null61   public func directMutate<T: Scalar>(_ value: T, index: Int32) -> Bool {
62     mutate(value: value, o: index)
63   }
64 }
65 
66 extension Mutable where Self == Struct {
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
mutate<T: Scalar>null72   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
directMutate<T: Scalar>null82   public func directMutate<T: Scalar>(_ value: T, index: Int32) -> Bool {
83     mutate(value: value, o: index)
84   }
85 }
86 
87 extension Struct: Mutable {}
88 extension Table: Mutable {}
89