• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 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 import Foundation
18 
19 /// A boolean to see if the system is littleEndian
20 let isLitteEndian: Bool = {
21   let number: UInt32 = 0x12345678
22   return number == number.littleEndian
23 }()
24 /// Constant for the file id length
25 let FileIdLength = 4
26 /// Type aliases
27 public typealias Byte = UInt8
28 public typealias UOffset = UInt32
29 public typealias SOffset = Int32
30 public typealias VOffset = UInt16
31 /// Maximum size for a buffer
32 public let FlatBufferMaxSize = UInt32
33   .max << ((MemoryLayout<SOffset>.size * 8 - 1) - 1)
34 
35 /// Protocol that All Scalars should conform to
36 ///
37 /// Scalar is used to conform all the numbers that can be represented in a FlatBuffer. It's used to write/read from the buffer.
38 public protocol Scalar: Equatable {
39   associatedtype NumericValue
40   var convertedEndian: NumericValue { get }
41 }
42 
43 extension Scalar where Self: Verifiable {}
44 
45 extension Scalar where Self: FixedWidthInteger {
46   /// Converts the value from BigEndian to LittleEndian
47   ///
48   /// Converts values to little endian on machines that work with BigEndian, however this is NOT TESTED yet.
49   public var convertedEndian: NumericValue {
50     self as! Self.NumericValue
51   }
52 }
53 
54 extension Double: Scalar, Verifiable {
55   public typealias NumericValue = UInt64
56 
57   public var convertedEndian: UInt64 {
58     bitPattern.littleEndian
59   }
60 }
61 
62 extension Float32: Scalar, Verifiable {
63   public typealias NumericValue = UInt32
64 
65   public var convertedEndian: UInt32 {
66     bitPattern.littleEndian
67   }
68 }
69 
70 extension Bool: Scalar, Verifiable {
71   public var convertedEndian: UInt8 {
72     self == true ? 1 : 0
73   }
74 
75   public typealias NumericValue = UInt8
76 }
77 
78 extension Int: Scalar, Verifiable {
79   public typealias NumericValue = Int
80 }
81 
82 extension Int8: Scalar, Verifiable {
83   public typealias NumericValue = Int8
84 }
85 
86 extension Int16: Scalar, Verifiable {
87   public typealias NumericValue = Int16
88 }
89 
90 extension Int32: Scalar, Verifiable {
91   public typealias NumericValue = Int32
92 }
93 
94 extension Int64: Scalar, Verifiable {
95   public typealias NumericValue = Int64
96 }
97 
98 extension UInt8: Scalar, Verifiable {
99   public typealias NumericValue = UInt8
100 }
101 
102 extension UInt16: Scalar, Verifiable {
103   public typealias NumericValue = UInt16
104 }
105 
106 extension UInt32: Scalar, Verifiable {
107   public typealias NumericValue = UInt32
108 }
109 
110 extension UInt64: Scalar, Verifiable {
111   public typealias NumericValue = UInt64
112 }
113 
FlatBuffersVersion_25_1_24null114 public func FlatBuffersVersion_25_1_24() {}
115