FbConstants.swift (3092B)
1 /* 2 * Copyright 2023 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 #if !os(WASI) 18 #if os(Linux) 19 import CoreFoundation 20 #else 21 import Foundation 22 #endif 23 #else 24 import SwiftOverlayShims 25 #endif 26 27 /// A boolean to see if the system is littleEndian 28 let isLitteEndian: Bool = { 29 let number: UInt32 = 0x12345678 30 return number == number.littleEndian 31 }() 32 /// Constant for the file id length 33 let FileIdLength = 4 34 /// Type aliases 35 public typealias Byte = UInt8 36 public typealias UOffset = UInt32 37 public typealias SOffset = Int32 38 public typealias VOffset = UInt16 39 /// Maximum size for a buffer 40 public let FlatBufferMaxSize = UInt32 41 .max << ((MemoryLayout<SOffset>.size * 8 - 1) - 1) 42 43 /// Protocol that All Scalars should conform to 44 /// 45 /// Scalar is used to conform all the numbers that can be represented in a FlatBuffer. It's used to write/read from the buffer. 46 public protocol Scalar: Equatable { 47 associatedtype NumericValue 48 var convertedEndian: NumericValue { get } 49 } 50 51 extension Scalar where Self: Verifiable {} 52 53 extension Scalar where Self: FixedWidthInteger { 54 /// 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 } 60 } 61 62 extension Double: Scalar, Verifiable { 63 public typealias NumericValue = UInt64 64 65 public var convertedEndian: UInt64 { 66 bitPattern.littleEndian 67 } 68 } 69 70 extension Float32: Scalar, Verifiable { 71 public typealias NumericValue = UInt32 72 73 public var convertedEndian: UInt32 { 74 bitPattern.littleEndian 75 } 76 } 77 78 extension Bool: Scalar, Verifiable { 79 public var convertedEndian: UInt8 { 80 self == true ? 1 : 0 81 } 82 83 public typealias NumericValue = UInt8 84 } 85 86 extension Int: Scalar, Verifiable { 87 public typealias NumericValue = Int 88 } 89 90 extension Int8: Scalar, Verifiable { 91 public typealias NumericValue = Int8 92 } 93 94 extension Int16: Scalar, Verifiable { 95 public typealias NumericValue = Int16 96 } 97 98 extension Int32: Scalar, Verifiable { 99 public typealias NumericValue = Int32 100 } 101 102 extension Int64: Scalar, Verifiable { 103 public typealias NumericValue = Int64 104 } 105 106 extension UInt8: Scalar, Verifiable { 107 public typealias NumericValue = UInt8 108 } 109 110 extension UInt16: Scalar, Verifiable { 111 public typealias NumericValue = UInt16 112 } 113 114 extension UInt32: Scalar, Verifiable { 115 public typealias NumericValue = UInt32 116 } 117 118 extension UInt64: Scalar, Verifiable { 119 public typealias NumericValue = UInt64 120 } 121 122 public func FlatBuffersVersion_23_5_26() {}