xref: /aosp_15_r20/external/flatbuffers/swift/Sources/FlatBuffers/FlatBufferObject.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 /// NativeStruct is a protocol that indicates if the struct is a native `swift` struct
24*890232f2SAndroid Build Coastguard Worker /// since now we will be serializing native structs into the buffer.
25*890232f2SAndroid Build Coastguard Worker public protocol NativeStruct {}
26*890232f2SAndroid Build Coastguard Worker 
27*890232f2SAndroid Build Coastguard Worker /// FlatbuffersInitializable is a protocol that allows any object to be
28*890232f2SAndroid Build Coastguard Worker /// Initialized from a ByteBuffer
29*890232f2SAndroid Build Coastguard Worker public protocol FlatbuffersInitializable {
30*890232f2SAndroid Build Coastguard Worker   /// Any flatbuffers object that confirms to this protocol is going to be
31*890232f2SAndroid Build Coastguard Worker   /// initializable through this initializer
32*890232f2SAndroid Build Coastguard Worker   init(_ bb: ByteBuffer, o: Int32)
33*890232f2SAndroid Build Coastguard Worker }
34*890232f2SAndroid Build Coastguard Worker 
35*890232f2SAndroid Build Coastguard Worker /// FlatbufferObject structures all the Flatbuffers objects
36*890232f2SAndroid Build Coastguard Worker public protocol FlatBufferObject: FlatbuffersInitializable {
37*890232f2SAndroid Build Coastguard Worker   var __buffer: ByteBuffer! { get }
38*890232f2SAndroid Build Coastguard Worker }
39*890232f2SAndroid Build Coastguard Worker 
40*890232f2SAndroid Build Coastguard Worker /// ``ObjectAPIPacker`` is a protocol that allows object to pack and unpack from a
41*890232f2SAndroid Build Coastguard Worker /// ``NativeObject`` to a flatbuffers Object and vice versa.
42*890232f2SAndroid Build Coastguard Worker public protocol ObjectAPIPacker {
43*890232f2SAndroid Build Coastguard Worker   /// associatedtype to the object that should be unpacked.
44*890232f2SAndroid Build Coastguard Worker   associatedtype T
45*890232f2SAndroid Build Coastguard Worker 
46*890232f2SAndroid Build Coastguard Worker   /// ``pack(_:obj:)-3ptws`` tries to pacs the variables of a native Object into the `ByteBuffer` by using
47*890232f2SAndroid Build Coastguard Worker   /// a FlatBufferBuilder
48*890232f2SAndroid Build Coastguard Worker   /// - Parameters:
49*890232f2SAndroid Build Coastguard Worker   ///   - builder: FlatBufferBuilder that will host incoming data
50*890232f2SAndroid Build Coastguard Worker   ///   - obj: Object of associatedtype to the current implementer
51*890232f2SAndroid Build Coastguard Worker   ///
52*890232f2SAndroid Build Coastguard Worker   /// ``pack(_:obj:)-3ptws`` can be called by passing through an already initialized ``FlatBufferBuilder``
53*890232f2SAndroid Build Coastguard Worker   /// or it can be called by using the public API that will create a new ``FlatBufferBuilder``
packnull54*890232f2SAndroid Build Coastguard Worker   static func pack(_ builder: inout FlatBufferBuilder, obj: inout T?) -> Offset
55*890232f2SAndroid Build Coastguard Worker 
56*890232f2SAndroid Build Coastguard Worker   /// ``pack(_:obj:)-20ipk`` packs the variables of a native Object into the `ByteBuffer` by using
57*890232f2SAndroid Build Coastguard Worker   /// the FlatBufferBuilder
58*890232f2SAndroid Build Coastguard Worker   /// - Parameters:
59*890232f2SAndroid Build Coastguard Worker   ///   - builder: FlatBufferBuilder that will host incoming data
60*890232f2SAndroid Build Coastguard Worker   ///   - obj: Object of associatedtype to the current implementer
61*890232f2SAndroid Build Coastguard Worker   ///
62*890232f2SAndroid Build Coastguard Worker   /// ``pack(_:obj:)-20ipk`` can be called by passing through an already initialized ``FlatBufferBuilder``
63*890232f2SAndroid Build Coastguard Worker   /// or it can be called by using the public API that will create a new ``FlatBufferBuilder``
64*890232f2SAndroid Build Coastguard Worker   static func pack(_ builder: inout FlatBufferBuilder, obj: inout T) -> Offset
65*890232f2SAndroid Build Coastguard Worker 
66*890232f2SAndroid Build Coastguard Worker   /// ``unpack()`` unpacks a ``FlatBuffers`` object into a Native swift object.
67*890232f2SAndroid Build Coastguard Worker   mutating func unpack() -> T
68*890232f2SAndroid Build Coastguard Worker }
69