Skip to content

Commit

Permalink
Extract an ABIBindings module (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle authored Sep 16, 2024
1 parent 8b8c62f commit 1589508
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 22 deletions.
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ let package = Package(
dependencies: ["COM_ABI", "WindowsRuntime_ABI"],
path: "Support/Sources/InternalABI",
exclude: ["CMakeLists.txt"]),
.target(
name: "ABIBindings",
path: "Support/Sources/ABIBindings",
exclude: ["CMakeLists.txt"]),
.target(
name: "COM",
dependencies: ["COM_ABI", "InternalABI"],
dependencies: ["ABIBindings", "COM_ABI", "InternalABI"],
path: "Support/Sources/COM",
exclude: ["CMakeLists.txt"]),
.target(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ extension ABIBinding {
}
}

public protocol IdentityBinding: PODBinding where SwiftValue == ABIValue {}

extension IdentityBinding {
public static func toABI(_ value: SwiftValue) -> ABIValue { value }
public static func toSwift(_ value: ABIValue) -> SwiftValue { value }
}

public enum ABIBindingError: Error {
case unsupported(Any.Type)
}
2 changes: 2 additions & 0 deletions Support/Sources/ABIBindings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
file(GLOB_RECURSE SOURCES *.swift)
add_library(ABIBindings STATIC ${SOURCES})
11 changes: 11 additions & 0 deletions Support/Sources/ABIBindings/ClosedEnumBinding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// Binding for an enumeration whose ABI representation is an integer,
/// and for which allowed values are limited to a set of defined enumerants.
public protocol ClosedEnumBinding: RawRepresentable, PODBinding
where ABIValue == RawValue, SwiftValue == Self, RawValue: FixedWidthInteger & Hashable {
}

extension ClosedEnumBinding {
public static var abiDefaultValue: RawValue { RawValue.zero }
public static func toSwift(_ value: RawValue) -> Self { Self(rawValue: value)! }
public static func toABI(_ value: Self) -> RawValue { value.rawValue }
}
7 changes: 7 additions & 0 deletions Support/Sources/ABIBindings/IdentityBinding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// Binds a Swift type to an identical ABI type.
public protocol IdentityBinding: PODBinding where SwiftValue == ABIValue {}

extension IdentityBinding {
public static func toABI(_ value: SwiftValue) -> ABIValue { value }
public static func toSwift(_ value: ABIValue) -> SwiftValue { value }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,3 @@ extension OpenEnumBinding {
public static func toSwift(_ value: RawValue) -> Self { Self(rawValue: value) }
public static func toABI(_ value: Self) -> RawValue { value.rawValue }
}

/// Binding for an enumeration whose ABI representation is an integer,
/// and for which allowed values are limited to a set of defined enumerants.
public protocol ClosedEnumBinding: RawRepresentable, PODBinding
where ABIValue == RawValue, SwiftValue == Self, RawValue: FixedWidthInteger & Hashable {
}

extension ClosedEnumBinding {
public static var abiDefaultValue: RawValue { RawValue.zero }
public static func toSwift(_ value: RawValue) -> Self { Self(rawValue: value)! }
public static func toABI(_ value: Self) -> RawValue { value.rawValue }
}
File renamed without changes.
1 change: 1 addition & 0 deletions Support/Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ add_subdirectory(COM_ABI)
add_subdirectory(WindowsRuntime_ABI)
add_subdirectory(InternalABI)

add_subdirectory(ABIBindings)
add_subdirectory(COM)
add_subdirectory(WindowsRuntime)
1 change: 1 addition & 0 deletions Support/Sources/COM/ABIBindings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@_exported import ABIBindings
3 changes: 2 additions & 1 deletion Support/Sources/COM/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
file(GLOB_RECURSE SOURCES *.swift)
add_library(COM STATIC ${SOURCES})
target_link_libraries(COM PRIVATE COM_ABI SWRT_InternalABI)
target_link_libraries(COM PUBLIC ABIBindings COM_ABI SWRT_InternalABI)
target_link_libraries(COM PRIVATE SWRT_InternalABI)
3 changes: 2 additions & 1 deletion Support/Sources/WindowsRuntime/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
file(GLOB_RECURSE SOURCES *.swift)
add_library(WindowsRuntime STATIC ${SOURCES})
target_link_libraries(WindowsRuntime PRIVATE COM WindowsRuntime_ABI SWRT_InternalABI)
target_link_libraries(WindowsRuntime PUBLIC COM WindowsRuntime_ABI)
target_link_libraries(WindowsRuntime PRIVATE SWRT_InternalABI)

0 comments on commit 1589508

Please sign in to comment.