Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 1 | /* |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 2 | * Copyright 2021 Google Inc. All rights reserved. |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 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 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 17 | #if !os(WASI) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 18 | import Foundation |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 19 | #else |
| 20 | import SwiftOverlayShims |
| 21 | #endif |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 22 | |
| 23 | /// FlatBufferGRPCMessage protocol that should allow us to invoke |
| 24 | /// initializers directly from the GRPC generated code |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 25 | public protocol FlatBufferGRPCMessage { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 26 | |
| 27 | /// Raw pointer which would be pointing to the beginning of the readable bytes |
| 28 | var rawPointer: UnsafeMutableRawPointer { get } |
| 29 | |
| 30 | /// Size of readable bytes in the buffer |
| 31 | var size: Int { get } |
| 32 | |
| 33 | init(byteBuffer: ByteBuffer) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /// Message is a wrapper around Buffers to to able to send Flatbuffers `Buffers` through the |
| 37 | /// GRPC library |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 38 | public struct Message<T: FlatBufferObject>: FlatBufferGRPCMessage { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 39 | internal var buffer: ByteBuffer |
| 40 | |
| 41 | /// Returns the an object of type T that would be read from the buffer |
| 42 | public var object: T { |
| 43 | T.init( |
| 44 | buffer, |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 45 | o: Int32(buffer.read(def: UOffset.self, position: buffer.reader)) + |
| 46 | Int32(buffer.reader)) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 49 | public var rawPointer: UnsafeMutableRawPointer { |
| 50 | buffer.memory.advanced(by: buffer.reader) } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 51 | |
| 52 | public var size: Int { Int(buffer.size) } |
| 53 | |
| 54 | /// Initializes the message with the type Flatbuffer.Bytebuffer that is transmitted over |
| 55 | /// GRPC |
| 56 | /// - Parameter byteBuffer: Flatbuffer ByteBuffer object |
| 57 | public init(byteBuffer: ByteBuffer) { |
| 58 | buffer = byteBuffer |
| 59 | } |
| 60 | |
| 61 | /// Initializes the message by copying the buffer to the message to be sent. |
| 62 | /// from the builder |
| 63 | /// - Parameter builder: FlatbufferBuilder that has the bytes created in |
| 64 | /// - Note: Use `builder.finish(offset)` before passing the builder without prefixing anything to it |
| 65 | public init(builder: inout FlatBufferBuilder) { |
| 66 | buffer = builder.sizedBuffer |
| 67 | builder.clear() |
| 68 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 69 | } |