blob: a2a108263ca699a08a419b4d319889678a32646b [file] [log] [blame]
Austin Schuh58b9b472020-11-25 19:12:44 -08001/*
James Kuszmaul8e62b022022-03-22 09:33:25 -07002 * Copyright 2021 Google Inc. All rights reserved.
Austin Schuh58b9b472020-11-25 19:12:44 -08003 *
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 Schuh2dd86a92022-09-14 21:19:23 -070017#if !os(WASI)
18 #if os(Linux)
19 import CoreFoundation
20 #else
21 import Foundation
22 #endif
Austin Schuh272c6132020-11-14 16:37:52 -080023#else
Austin Schuh2dd86a92022-09-14 21:19:23 -070024import SwiftOverlayShims
Austin Schuh272c6132020-11-14 16:37:52 -080025#endif
26
27/// A boolean to see if the system is littleEndian
Austin Schuh2dd86a92022-09-14 21:19:23 -070028let isLitteEndian: Bool = {
29 let number: UInt32 = 0x12345678
30 return number == number.littleEndian
31}()
Austin Schuh272c6132020-11-14 16:37:52 -080032/// Constant for the file id length
33let FileIdLength = 4
34/// Type aliases
35public typealias Byte = UInt8
36public typealias UOffset = UInt32
37public typealias SOffset = Int32
38public typealias VOffset = UInt16
39/// Maximum size for a buffer
James Kuszmaul8e62b022022-03-22 09:33:25 -070040public let FlatBufferMaxSize = UInt32
41 .max << ((MemoryLayout<SOffset>.size * 8 - 1) - 1)
Austin Schuh272c6132020-11-14 16:37:52 -080042
James Kuszmaul8e62b022022-03-22 09:33:25 -070043/// Protocol that All Scalars should conform to
Austin Schuh272c6132020-11-14 16:37:52 -080044///
James Kuszmaul8e62b022022-03-22 09:33:25 -070045/// Scalar is used to conform all the numbers that can be represented in a FlatBuffer. It's used to write/read from the buffer.
Austin Schuh272c6132020-11-14 16:37:52 -080046public protocol Scalar: Equatable {
Austin Schuh58b9b472020-11-25 19:12:44 -080047 associatedtype NumericValue
48 var convertedEndian: NumericValue { get }
Austin Schuh272c6132020-11-14 16:37:52 -080049}
50
James Kuszmaul8e62b022022-03-22 09:33:25 -070051extension Scalar where Self: Verifiable {}
52
Austin Schuh272c6132020-11-14 16:37:52 -080053extension Scalar where Self: FixedWidthInteger {
Austin Schuh58b9b472020-11-25 19:12:44 -080054 /// Converts the value from BigEndian to LittleEndian
55 ///
56 /// Converts values to little endian on machines that work with BigEndian, however this is NOT TESTED yet.
57 public var convertedEndian: NumericValue {
58 self as! Self.NumericValue
59 }
Austin Schuh272c6132020-11-14 16:37:52 -080060}
61
James Kuszmaul8e62b022022-03-22 09:33:25 -070062extension Double: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080063 public typealias NumericValue = UInt64
64
65 public var convertedEndian: UInt64 {
66 bitPattern.littleEndian
67 }
Austin Schuh272c6132020-11-14 16:37:52 -080068}
69
James Kuszmaul8e62b022022-03-22 09:33:25 -070070extension Float32: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080071 public typealias NumericValue = UInt32
72
73 public var convertedEndian: UInt32 {
74 bitPattern.littleEndian
75 }
Austin Schuh272c6132020-11-14 16:37:52 -080076}
77
James Kuszmaul8e62b022022-03-22 09:33:25 -070078extension Bool: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080079 public var convertedEndian: UInt8 {
80 self == true ? 1 : 0
81 }
82
83 public typealias NumericValue = UInt8
Austin Schuh272c6132020-11-14 16:37:52 -080084}
85
James Kuszmaul8e62b022022-03-22 09:33:25 -070086extension Int: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080087 public typealias NumericValue = Int
Austin Schuh272c6132020-11-14 16:37:52 -080088}
89
James Kuszmaul8e62b022022-03-22 09:33:25 -070090extension Int8: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080091 public typealias NumericValue = Int8
Austin Schuh272c6132020-11-14 16:37:52 -080092}
93
James Kuszmaul8e62b022022-03-22 09:33:25 -070094extension Int16: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080095 public typealias NumericValue = Int16
Austin Schuh272c6132020-11-14 16:37:52 -080096}
97
James Kuszmaul8e62b022022-03-22 09:33:25 -070098extension Int32: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080099 public typealias NumericValue = Int32
Austin Schuh272c6132020-11-14 16:37:52 -0800100}
101
James Kuszmaul8e62b022022-03-22 09:33:25 -0700102extension Int64: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -0800103 public typealias NumericValue = Int64
Austin Schuh272c6132020-11-14 16:37:52 -0800104}
105
James Kuszmaul8e62b022022-03-22 09:33:25 -0700106extension UInt8: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -0800107 public typealias NumericValue = UInt8
Austin Schuh272c6132020-11-14 16:37:52 -0800108}
109
James Kuszmaul8e62b022022-03-22 09:33:25 -0700110extension UInt16: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -0800111 public typealias NumericValue = UInt16
Austin Schuh272c6132020-11-14 16:37:52 -0800112}
113
James Kuszmaul8e62b022022-03-22 09:33:25 -0700114extension UInt32: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -0800115 public typealias NumericValue = UInt32
Austin Schuh272c6132020-11-14 16:37:52 -0800116}
117
James Kuszmaul8e62b022022-03-22 09:33:25 -0700118extension UInt64: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -0800119 public typealias NumericValue = UInt64
Austin Schuh272c6132020-11-14 16:37:52 -0800120}
121
Austin Schuh2dd86a92022-09-14 21:19:23 -0700122public func FlatBuffersVersion_2_0_8() {}