Skip to content

Commit 0e5c395

Browse files
committed
Offical Swift port for FlexBuffers
This is the offical port for FlexBuffers within swift, and it introcudes a Common Module where code is shared between flatbuffers and flexbuffers. Writing most supported values like maps, vectors, nil and scalars into a flexbuffer buffer. And includes tests to verify that its similar to cpp
1 parent 5822c1c commit 0e5c395

File tree

20 files changed

+2420
-104
lines changed

20 files changed

+2420
-104
lines changed

Package.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,21 @@ let package = Package(
2727
.library(
2828
name: "FlatBuffers",
2929
targets: ["FlatBuffers"]),
30+
.library(
31+
name: "FlexBuffers",
32+
targets: ["FlexBuffers"]),
3033
],
3134
targets: [
3235
.target(
3336
name: "FlatBuffers",
37+
dependencies: ["Common"],
38+
path: "swift/Sources/FlatBuffers"),
39+
.target(
40+
name: "FlexBuffers",
41+
dependencies: ["Common"],
42+
path: "swift/Sources/FlexBuffers"),
43+
.target(
44+
name: "Common",
3445
dependencies: [],
35-
path: "swift/Sources"),
46+
path: "swift/Sources/Common"),
3647
])
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
public let isLitteEndian: Bool = {
21+
let number: UInt32 = 0x12345678
22+
return number == number.littleEndian
23+
}()
24+
25+
/// Constant for the file id length
26+
public let FileIdLength = 4
27+
28+
/// Protocol that All Scalars should conform to
29+
///
30+
/// Scalar is used to conform all the numbers that can be represented in a FlatBuffer. It's used to write/read from the buffer.
31+
public protocol Scalar: Equatable {
32+
associatedtype NumericValue
33+
var convertedEndian: NumericValue { get }
34+
}
35+
36+
extension Scalar where Self: FixedWidthInteger {
37+
/// Converts the value from BigEndian to LittleEndian
38+
///
39+
/// Converts values to little endian on machines that work with BigEndian, however this is NOT TESTED yet.
40+
public var convertedEndian: NumericValue {
41+
self as! Self.NumericValue
42+
}
43+
}
44+
45+
extension Double: Scalar {
46+
public typealias NumericValue = UInt64
47+
48+
public var convertedEndian: UInt64 {
49+
bitPattern.littleEndian
50+
}
51+
}
52+
53+
extension Float32: Scalar {
54+
public typealias NumericValue = UInt32
55+
56+
public var convertedEndian: UInt32 {
57+
bitPattern.littleEndian
58+
}
59+
}
60+
61+
extension Bool: Scalar {
62+
public var convertedEndian: UInt8 {
63+
self == true ? 1 : 0
64+
}
65+
66+
public typealias NumericValue = UInt8
67+
}
68+
69+
extension Int: Scalar {
70+
public typealias NumericValue = Int
71+
}
72+
73+
extension Int8: Scalar {
74+
public typealias NumericValue = Int8
75+
}
76+
77+
extension Int16: Scalar {
78+
public typealias NumericValue = Int16
79+
}
80+
81+
extension Int32: Scalar {
82+
public typealias NumericValue = Int32
83+
}
84+
85+
extension Int64: Scalar {
86+
public typealias NumericValue = Int64
87+
}
88+
89+
extension UInt8: Scalar {
90+
public typealias NumericValue = UInt8
91+
}
92+
93+
extension UInt16: Scalar {
94+
public typealias NumericValue = UInt16
95+
}
96+
97+
extension UInt32: Scalar {
98+
public typealias NumericValue = UInt32
99+
}
100+
101+
extension UInt64: Scalar {
102+
public typealias NumericValue = UInt64
103+
}

swift/Sources/FlatBuffers/Int+extension.swift renamed to swift/Sources/Common/Int+extension.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ extension Int {
2222
///
2323
/// This is used since the UnsafeMutableRawPointer will face issues when writing/reading
2424
/// if the buffer alignment exceeds that actual size of the buffer
25-
var convertToPowerofTwo: Int {
25+
public var convertToPowerofTwo: Int {
2626
guard self > 0 else { return 1 }
27-
var n = UOffset(self)
27+
var n = UInt32(self)
2828

2929
#if arch(arm) || arch(i386)
3030
let max = UInt32(Int.max)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
/// Gets the padding for the current element
20+
/// - Parameters:
21+
/// - bufSize: Current size of the buffer + the offset of the object to be written
22+
/// - elementSize: Element size
23+
@inline(__always)
24+
public func padding(
25+
bufSize: UInt,
26+
elementSize: UInt) -> UInt
27+
{
28+
((~bufSize) &+ 1) & (elementSize &- 1)
29+
}

swift/Sources/FlatBuffers/ByteBuffer.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import Common
1718
import Foundation
1819

1920
/// `ByteBuffer` is the interface that stores the data for a `Flatbuffers` object

swift/Sources/FlatBuffers/Constants.swift

Lines changed: 27 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
@_exported import Common
1718
import Foundation
1819

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
2620
/// Type aliases
2721
public typealias Byte = UInt8
2822
public typealias UOffset = UInt32
@@ -35,80 +29,31 @@ public let FlatBufferMaxSize = UInt32
3529
/// Protocol that All Scalars should conform to
3630
///
3731
/// 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-
}
32+
33+
extension Scalar where Self: FixedWidthInteger {}
34+
35+
extension Double: Verifiable {}
36+
37+
extension Float32: Verifiable {}
38+
39+
extension Bool: Verifiable {}
40+
41+
extension Int: Verifiable {}
42+
43+
extension Int8: Verifiable {}
44+
45+
extension Int16: Verifiable {}
46+
47+
extension Int32: Verifiable {}
48+
49+
extension Int64: Verifiable {}
50+
51+
extension UInt8: Verifiable {}
52+
53+
extension UInt16: Verifiable {}
54+
55+
extension UInt32: Verifiable {}
56+
57+
extension UInt64: Verifiable {}
11358

11459
public func FlatBuffersVersion_25_2_10() {}

swift/Sources/FlatBuffers/FlatBufferBuilder.swift

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import Common
1718
import Foundation
1819

1920
/// ``FlatBufferBuilder`` builds a `FlatBuffer` through manipulating its internal state.
@@ -343,19 +344,6 @@ public struct FlatBufferBuilder {
343344
}
344345
}
345346

346-
/// Gets the padding for the current element
347-
/// - Parameters:
348-
/// - bufSize: Current size of the buffer + the offset of the object to be written
349-
/// - elementSize: Element size
350-
@inline(__always)
351-
@usableFromInline
352-
mutating internal func padding(
353-
bufSize: UInt32,
354-
elementSize: UInt32) -> UInt32
355-
{
356-
((~bufSize) &+ 1) & (elementSize &- 1)
357-
}
358-
359347
/// Prealigns the buffer before writting a new object into the buffer
360348
/// - Parameters:
361349
/// - len:Length of the object
@@ -364,11 +352,9 @@ public struct FlatBufferBuilder {
364352
@usableFromInline
365353
mutating internal func preAlign(len: Int, alignment: Int) {
366354
minAlignment(size: alignment)
367-
_bb.fill(
368-
padding: Int(
369-
padding(
370-
bufSize: _bb.size &+ UOffset(len),
371-
elementSize: UOffset(alignment))))
355+
_bb.fill(padding: numericCast(padding(
356+
bufSize: numericCast(_bb.size) &+ numericCast(len),
357+
elementSize: numericCast(alignment))))
372358
}
373359

374360
/// Prealigns the buffer before writting a new object into the buffer

swift/Sources/FlatBuffers/Table.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import Common
1718
import Foundation
1819

1920
/// `Table` is a Flatbuffers object that can read,

0 commit comments

Comments
 (0)