Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 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 | return self as! Self.NumericValue |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | extension Double: Scalar { |
| 37 | public typealias NumericValue = UInt64 |
| 38 | |
| 39 | public var convertedEndian: UInt64 { |
| 40 | return self.bitPattern.littleEndian |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | extension Float32: Scalar { |
| 45 | public typealias NumericValue = UInt32 |
| 46 | |
| 47 | public var convertedEndian: UInt32 { |
| 48 | return self.bitPattern.littleEndian |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | extension Bool: Scalar { |
| 53 | public var convertedEndian: UInt8 { |
| 54 | return self == true ? 1 : 0 |
| 55 | } |
| 56 | |
| 57 | public typealias NumericValue = UInt8 |
| 58 | } |
| 59 | |
| 60 | extension Int: Scalar { |
| 61 | public typealias NumericValue = Int |
| 62 | } |
| 63 | |
| 64 | extension Int8: Scalar { |
| 65 | public typealias NumericValue = Int8 |
| 66 | } |
| 67 | |
| 68 | extension Int16: Scalar { |
| 69 | public typealias NumericValue = Int16 |
| 70 | } |
| 71 | |
| 72 | extension Int32: Scalar { |
| 73 | public typealias NumericValue = Int32 |
| 74 | } |
| 75 | |
| 76 | extension Int64: Scalar { |
| 77 | public typealias NumericValue = Int64 |
| 78 | } |
| 79 | |
| 80 | extension UInt8: Scalar { |
| 81 | public typealias NumericValue = UInt8 |
| 82 | } |
| 83 | |
| 84 | extension UInt16: Scalar { |
| 85 | public typealias NumericValue = UInt16 |
| 86 | } |
| 87 | |
| 88 | extension UInt32: Scalar { |
| 89 | public typealias NumericValue = UInt32 |
| 90 | } |
| 91 | |
| 92 | extension UInt64: Scalar { |
| 93 | public typealias NumericValue = UInt64 |
| 94 | } |
| 95 | |
| 96 | public func FlatBuffersVersion_1_12_0() {} |