• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import Foundation
18 
19 extension String: FlatbuffersInitializable {
20 
21   /// Initailizes a string from a Flatbuffers ByteBuffer
22   /// - Parameters:
23   ///   - bb: ByteBuffer containing the readable string
24   ///   - o: Current position
25   public init(_ bb: ByteBuffer, o: Int32) {
26     let count = bb.read(def: Int32.self, position: Int(o))
27     self = bb.readString(
28       at: Int32(MemoryLayout<Int32>.size) + o,
29       count: count) ?? ""
30   }
31 }
32 
33 extension String: ObjectAPIPacker {
34 
packnull35   public static func pack(_ builder: inout FlatBufferBuilder, obj: inout String?) -> Offset {
36     guard var obj = obj else { return Offset() }
37     return pack(&builder, obj: &obj)
38   }
39 
packnull40   public static func pack(_ builder: inout FlatBufferBuilder, obj: inout String) -> Offset {
41     builder.create(string: obj)
42   }
43 
unpacknull44   public mutating func unpack() -> String {
45     self
46   }
47 
48 }
49 
50 extension String: NativeObject {
51 
serialize<T: ObjectAPIPacker>null52   public func serialize<T: ObjectAPIPacker>(type: T.Type) -> ByteBuffer where T.T == Self {
53     fatalError("serialize should never be called from string directly")
54   }
55 
serialize<T: ObjectAPIPacker>null56   public func serialize<T: ObjectAPIPacker>(builder: inout FlatBufferBuilder, type: T.Type) -> ByteBuffer where T.T == Self {
57     fatalError("serialize should never be called from string directly")
58   }
59 }
60