blob: 2b55250d33a92e1c24730ebce1d562b72756049d [file] [log] [blame]
Austin Schuh272c6132020-11-14 16:37:52 -08001#if os(Linux)
2import CoreFoundation
3#else
4import Foundation
5#endif
6
7/// A boolean to see if the system is littleEndian
8let isLitteEndian = CFByteOrderGetCurrent() == Int(CFByteOrderLittleEndian.rawValue)
9/// Constant for the file id length
10let FileIdLength = 4
11/// Type aliases
12public typealias Byte = UInt8
13public typealias UOffset = UInt32
14public typealias SOffset = Int32
15public typealias VOffset = UInt16
16/// Maximum size for a buffer
17public 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.
22public protocol Scalar: Equatable {
23 associatedtype NumericValue
24 var convertedEndian: NumericValue { get }
25}
26
27extension 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
36extension Double: Scalar {
37 public typealias NumericValue = UInt64
38
39 public var convertedEndian: UInt64 {
40 return self.bitPattern.littleEndian
41 }
42}
43
44extension Float32: Scalar {
45 public typealias NumericValue = UInt32
46
47 public var convertedEndian: UInt32 {
48 return self.bitPattern.littleEndian
49 }
50}
51
52extension Bool: Scalar {
53 public var convertedEndian: UInt8 {
54 return self == true ? 1 : 0
55 }
56
57 public typealias NumericValue = UInt8
58}
59
60extension Int: Scalar {
61 public typealias NumericValue = Int
62}
63
64extension Int8: Scalar {
65 public typealias NumericValue = Int8
66}
67
68extension Int16: Scalar {
69 public typealias NumericValue = Int16
70}
71
72extension Int32: Scalar {
73 public typealias NumericValue = Int32
74}
75
76extension Int64: Scalar {
77 public typealias NumericValue = Int64
78}
79
80extension UInt8: Scalar {
81 public typealias NumericValue = UInt8
82}
83
84extension UInt16: Scalar {
85 public typealias NumericValue = UInt16
86}
87
88extension UInt32: Scalar {
89 public typealias NumericValue = UInt32
90}
91
92extension UInt64: Scalar {
93 public typealias NumericValue = UInt64
94}
95
96public func FlatBuffersVersion_1_12_0() {}