xref: /aosp_15_r20/external/flatbuffers/swift/Sources/FlatBuffers/Struct.swift (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Worker /*
2*890232f2SAndroid Build Coastguard Worker  * Copyright 2021 Google Inc. All rights reserved.
3*890232f2SAndroid Build Coastguard Worker  *
4*890232f2SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*890232f2SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*890232f2SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*890232f2SAndroid Build Coastguard Worker  *
8*890232f2SAndroid Build Coastguard Worker  *     http://www.apache.org/licenses/LICENSE-2.0
9*890232f2SAndroid Build Coastguard Worker  *
10*890232f2SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*890232f2SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*890232f2SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*890232f2SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*890232f2SAndroid Build Coastguard Worker  * limitations under the License.
15*890232f2SAndroid Build Coastguard Worker  */
16*890232f2SAndroid Build Coastguard Worker 
17*890232f2SAndroid Build Coastguard Worker #if !os(WASI)
18*890232f2SAndroid Build Coastguard Worker import Foundation
19*890232f2SAndroid Build Coastguard Worker #else
20*890232f2SAndroid Build Coastguard Worker import SwiftOverlayShims
21*890232f2SAndroid Build Coastguard Worker #endif
22*890232f2SAndroid Build Coastguard Worker 
23*890232f2SAndroid Build Coastguard Worker /// Struct is a representation of a mutable `Flatbuffers` struct
24*890232f2SAndroid Build Coastguard Worker /// since native structs are value types and cant be mutated
25*890232f2SAndroid Build Coastguard Worker @frozen
26*890232f2SAndroid Build Coastguard Worker public struct Struct {
27*890232f2SAndroid Build Coastguard Worker 
28*890232f2SAndroid Build Coastguard Worker   /// Hosting Bytebuffer
29*890232f2SAndroid Build Coastguard Worker   public private(set) var bb: ByteBuffer
30*890232f2SAndroid Build Coastguard Worker   /// Current position of the struct
31*890232f2SAndroid Build Coastguard Worker   public private(set) var postion: Int32
32*890232f2SAndroid Build Coastguard Worker 
33*890232f2SAndroid Build Coastguard Worker   /// Initializer for a mutable flatbuffers struct
34*890232f2SAndroid Build Coastguard Worker   /// - Parameters:
35*890232f2SAndroid Build Coastguard Worker   ///   - bb: Current hosting Bytebuffer
36*890232f2SAndroid Build Coastguard Worker   ///   - position: Current position for the struct in the ByteBuffer
37*890232f2SAndroid Build Coastguard Worker   public init(bb: ByteBuffer, position: Int32 = 0) {
38*890232f2SAndroid Build Coastguard Worker     self.bb = bb
39*890232f2SAndroid Build Coastguard Worker     postion = position
40*890232f2SAndroid Build Coastguard Worker   }
41*890232f2SAndroid Build Coastguard Worker 
42*890232f2SAndroid Build Coastguard Worker   /// Reads data from the buffer directly at offset O
43*890232f2SAndroid Build Coastguard Worker   /// - Parameters:
44*890232f2SAndroid Build Coastguard Worker   ///   - type: Type of data to be read
45*890232f2SAndroid Build Coastguard Worker   ///   - o: Current offset of the data
46*890232f2SAndroid Build Coastguard Worker   /// - Returns: Data of Type T that conforms to type Scalar
readBuffer<T: Scalar>null47*890232f2SAndroid Build Coastguard Worker   public func readBuffer<T: Scalar>(of type: T.Type, at o: Int32) -> T {
48*890232f2SAndroid Build Coastguard Worker     let r = bb.read(def: T.self, position: Int(o + postion))
49*890232f2SAndroid Build Coastguard Worker     return r
50*890232f2SAndroid Build Coastguard Worker   }
51*890232f2SAndroid Build Coastguard Worker }
52