1 #if os(Linux) 2 import CoreFoundation 3 #else 4 import Foundation 5 #endif 6 7 /// A boolean to see if the system is littleEndian 8 let isLitteEndian = CFByteOrderGetCurrent() == Int(CFByteOrderLittleEndian.rawValue) 9 /// Constant for the file id length 10 let FileIdLength = 4 11 /// Type aliases 12 public typealias Byte = UInt8 13 public typealias UOffset = UInt32 14 public typealias SOffset = Int32 15 public typealias VOffset = UInt16 16 /// Maximum size for a buffer 17 public let FlatBufferMaxSize = UInt32.max << ((MemoryLayout<SOffset>.size * 8 - 1) - 1) 18 19 /// Protocol that confirms all the numbers 20 /// 21 /// Scalar is used to confirm all the numbers that can be represented in a FlatBuffer. It's used to write/read from the buffer. 22 public protocol Scalar: Equatable { 23 associatedtype NumericValue 24 var convertedEndian: NumericValue { get } 25 } 26 27 extension Scalar where Self: FixedWidthInteger { 28 /// Converts the value from BigEndian to LittleEndian 29 /// 30 /// Converts values to little endian on machines that work with BigEndian, however this is NOT TESTED yet. 31 public var convertedEndian: NumericValue { 32 if isLitteEndian { return self as! Self.NumericValue } 33 fatalError("This is not tested! please report an issue on the offical flatbuffers repo") 34 } 35 } 36 37 extension Double: Scalar { 38 public typealias NumericValue = UInt64 39 40 public var convertedEndian: UInt64 { 41 if isLitteEndian { return self.bitPattern } 42 return self.bitPattern.littleEndian 43 } 44 } 45 46 extension Float32: Scalar { 47 public typealias NumericValue = UInt32 48 49 public var convertedEndian: UInt32 { 50 if isLitteEndian { return self.bitPattern } 51 return self.bitPattern.littleEndian 52 } 53 } 54 55 extension Int: Scalar { 56 public typealias NumericValue = Int 57 } 58 59 extension Int8: Scalar { 60 public typealias NumericValue = Int8 61 } 62 63 extension Int16: Scalar { 64 public typealias NumericValue = Int16 65 } 66 67 extension Int32: Scalar { 68 public typealias NumericValue = Int32 69 } 70 71 extension Int64: Scalar { 72 public typealias NumericValue = Int64 73 } 74 75 extension UInt8: Scalar { 76 public typealias NumericValue = UInt8 77 } 78 79 extension UInt16: Scalar { 80 public typealias NumericValue = UInt16 81 } 82 83 extension UInt32: Scalar { 84 public typealias NumericValue = UInt32 85 } 86 87 extension UInt64: Scalar { 88 public typealias NumericValue = UInt64 89 } 90 FlatBuffersVersion_1_12_0null91public func FlatBuffersVersion_1_12_0() {} 92