blob: 8e643fdf899b643d015b96c3359dd94de3590f54 [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 Schuh272c6132020-11-14 16:37:52 -080017#if os(Linux)
18import CoreFoundation
19#else
20import Foundation
21#endif
22
23/// A boolean to see if the system is littleEndian
James Kuszmaul8e62b022022-03-22 09:33:25 -070024let isLitteEndian = CFByteOrderGetCurrent() ==
25 Int(CFByteOrderLittleEndian.rawValue)
Austin Schuh272c6132020-11-14 16:37:52 -080026/// Constant for the file id length
27let FileIdLength = 4
28/// Type aliases
29public typealias Byte = UInt8
30public typealias UOffset = UInt32
31public typealias SOffset = Int32
32public typealias VOffset = UInt16
33/// Maximum size for a buffer
James Kuszmaul8e62b022022-03-22 09:33:25 -070034public let FlatBufferMaxSize = UInt32
35 .max << ((MemoryLayout<SOffset>.size * 8 - 1) - 1)
Austin Schuh272c6132020-11-14 16:37:52 -080036
James Kuszmaul8e62b022022-03-22 09:33:25 -070037/// Protocol that All Scalars should conform to
Austin Schuh272c6132020-11-14 16:37:52 -080038///
James Kuszmaul8e62b022022-03-22 09:33:25 -070039/// 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 -080040public protocol Scalar: Equatable {
Austin Schuh58b9b472020-11-25 19:12:44 -080041 associatedtype NumericValue
42 var convertedEndian: NumericValue { get }
Austin Schuh272c6132020-11-14 16:37:52 -080043}
44
James Kuszmaul8e62b022022-03-22 09:33:25 -070045extension Scalar where Self: Verifiable {}
46
Austin Schuh272c6132020-11-14 16:37:52 -080047extension Scalar where Self: FixedWidthInteger {
Austin Schuh58b9b472020-11-25 19:12:44 -080048 /// Converts the value from BigEndian to LittleEndian
49 ///
50 /// Converts values to little endian on machines that work with BigEndian, however this is NOT TESTED yet.
51 public var convertedEndian: NumericValue {
52 self as! Self.NumericValue
53 }
Austin Schuh272c6132020-11-14 16:37:52 -080054}
55
James Kuszmaul8e62b022022-03-22 09:33:25 -070056extension Double: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080057 public typealias NumericValue = UInt64
58
59 public var convertedEndian: UInt64 {
60 bitPattern.littleEndian
61 }
Austin Schuh272c6132020-11-14 16:37:52 -080062}
63
James Kuszmaul8e62b022022-03-22 09:33:25 -070064extension Float32: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080065 public typealias NumericValue = UInt32
66
67 public var convertedEndian: UInt32 {
68 bitPattern.littleEndian
69 }
Austin Schuh272c6132020-11-14 16:37:52 -080070}
71
James Kuszmaul8e62b022022-03-22 09:33:25 -070072extension Bool: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080073 public var convertedEndian: UInt8 {
74 self == true ? 1 : 0
75 }
76
77 public typealias NumericValue = UInt8
Austin Schuh272c6132020-11-14 16:37:52 -080078}
79
James Kuszmaul8e62b022022-03-22 09:33:25 -070080extension Int: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080081 public typealias NumericValue = Int
Austin Schuh272c6132020-11-14 16:37:52 -080082}
83
James Kuszmaul8e62b022022-03-22 09:33:25 -070084extension Int8: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080085 public typealias NumericValue = Int8
Austin Schuh272c6132020-11-14 16:37:52 -080086}
87
James Kuszmaul8e62b022022-03-22 09:33:25 -070088extension Int16: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080089 public typealias NumericValue = Int16
Austin Schuh272c6132020-11-14 16:37:52 -080090}
91
James Kuszmaul8e62b022022-03-22 09:33:25 -070092extension Int32: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080093 public typealias NumericValue = Int32
Austin Schuh272c6132020-11-14 16:37:52 -080094}
95
James Kuszmaul8e62b022022-03-22 09:33:25 -070096extension Int64: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -080097 public typealias NumericValue = Int64
Austin Schuh272c6132020-11-14 16:37:52 -080098}
99
James Kuszmaul8e62b022022-03-22 09:33:25 -0700100extension UInt8: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -0800101 public typealias NumericValue = UInt8
Austin Schuh272c6132020-11-14 16:37:52 -0800102}
103
James Kuszmaul8e62b022022-03-22 09:33:25 -0700104extension UInt16: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -0800105 public typealias NumericValue = UInt16
Austin Schuh272c6132020-11-14 16:37:52 -0800106}
107
James Kuszmaul8e62b022022-03-22 09:33:25 -0700108extension UInt32: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -0800109 public typealias NumericValue = UInt32
Austin Schuh272c6132020-11-14 16:37:52 -0800110}
111
James Kuszmaul8e62b022022-03-22 09:33:25 -0700112extension UInt64: Scalar, Verifiable {
Austin Schuh58b9b472020-11-25 19:12:44 -0800113 public typealias NumericValue = UInt64
Austin Schuh272c6132020-11-14 16:37:52 -0800114}
115
James Kuszmaul8e62b022022-03-22 09:33:25 -0700116public func FlatBuffersVersion_2_0_0() {}