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 /// NativeObject is a protocol that all of the `Object-API` generated code should be 24*890232f2SAndroid Build Coastguard Worker /// conforming to since it allows developers the ease of use to pack and unpack their 25*890232f2SAndroid Build Coastguard Worker /// Flatbuffers objects 26*890232f2SAndroid Build Coastguard Worker public protocol NativeObject {} 27*890232f2SAndroid Build Coastguard Worker 28*890232f2SAndroid Build Coastguard Worker extension NativeObject { 29*890232f2SAndroid Build Coastguard Worker 30*890232f2SAndroid Build Coastguard Worker /// Serialize is a helper function that serailizes the data from the Object API to a bytebuffer directly th 31*890232f2SAndroid Build Coastguard Worker /// - Parameter type: Type of the Flatbuffer object 32*890232f2SAndroid Build Coastguard Worker /// - Returns: returns the encoded sized ByteBuffer serialize<T: ObjectAPIPacker>null33*890232f2SAndroid Build Coastguard Worker public func serialize<T: ObjectAPIPacker>(type: T.Type) -> ByteBuffer 34*890232f2SAndroid Build Coastguard Worker where T.T == Self 35*890232f2SAndroid Build Coastguard Worker { 36*890232f2SAndroid Build Coastguard Worker var builder = FlatBufferBuilder(initialSize: 1024) 37*890232f2SAndroid Build Coastguard Worker return serialize(builder: &builder, type: type.self) 38*890232f2SAndroid Build Coastguard Worker } 39*890232f2SAndroid Build Coastguard Worker 40*890232f2SAndroid Build Coastguard Worker /// Serialize is a helper function that serailizes the data from the Object API to a bytebuffer directly. 41*890232f2SAndroid Build Coastguard Worker /// 42*890232f2SAndroid Build Coastguard Worker /// - Parameters: 43*890232f2SAndroid Build Coastguard Worker /// - builder: A FlatBufferBuilder 44*890232f2SAndroid Build Coastguard Worker /// - type: Type of the Flatbuffer object 45*890232f2SAndroid Build Coastguard Worker /// - Returns: returns the encoded sized ByteBuffer 46*890232f2SAndroid Build Coastguard Worker /// - Note: The `serialize(builder:type)` can be considered as a function that allows you to create smaller builder instead of the default `1024`. 47*890232f2SAndroid Build Coastguard Worker /// It can be considered less expensive in terms of memory allocation 48*890232f2SAndroid Build Coastguard Worker public func serialize<T: ObjectAPIPacker>( 49*890232f2SAndroid Build Coastguard Worker builder: inout FlatBufferBuilder, 50*890232f2SAndroid Build Coastguard Worker type: T.Type) -> ByteBuffer where T.T == Self 51*890232f2SAndroid Build Coastguard Worker { 52*890232f2SAndroid Build Coastguard Worker var s = self 53*890232f2SAndroid Build Coastguard Worker let root = type.pack(&builder, obj: &s) 54*890232f2SAndroid Build Coastguard Worker builder.finish(offset: root) 55*890232f2SAndroid Build Coastguard Worker return builder.sizedBuffer 56*890232f2SAndroid Build Coastguard Worker } 57*890232f2SAndroid Build Coastguard Worker } 58