Skip to content

Commit

Permalink
Removed cast extensions on IUnknownPointer/IInspectablePointer
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle committed Mar 23, 2024
1 parent c1d501d commit 02ee970
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 61 deletions.
8 changes: 4 additions & 4 deletions Generator/Sources/SwiftWinRT/Writing/ClassDefinition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ fileprivate func writeActivatableInitializers(
fileprivate func writeSupportComposableInitializers(
defaultInterface: BoundInterface, projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws {
// public init(_transferringRef pointer: UnsafeMutablePointer<CWinRTComponent.SWRT_IFoo>) {
// super.init(pointer.cast())
// super.init(_transferringRef: .init(OpaquePointer(pointer))
// }
// Should use a COMReference<> but this runs into compiler bugs.
let param = SwiftParam(label: "_transferringRef", name: "pointer",
type: .unsafeMutablePointer(to: try projection.toABIType(defaultInterface.asBoundType)))
let pointerType: SwiftType = .unsafeMutablePointer(to: try projection.toABIType(defaultInterface.asBoundType))
let param = SwiftParam(label: "_transferringRef", name: "pointer", type: pointerType)
writer.writeInit(visibility: .public, params: [param]) { writer in
writer.writeStatement("super.init(_transferringRef: IUnknownPointer.cast(pointer).cast())")
writer.writeStatement("super.init(_transferringRef: .init(OpaquePointer(pointer)))")
}

// public init<Interface>(_compose: Bool, _factory: ComposableFactory<Interface>) throws {
Expand Down
2 changes: 1 addition & 1 deletion Support/Sources/COM/COMExport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ open class COMExport<Projection: COMTwoWayProjection>: COMExportBase {

open var implementation: Projection.SwiftObject { self as! Projection.SwiftObject }
public var comPointer: Projection.COMPointer {
comInterface.unknownPointer.cast(to: Projection.COMInterface.self)
Projection.COMPointer(OpaquePointer(comInterface.unknownPointer))
}

public init() {
Expand Down
8 changes: 4 additions & 4 deletions Support/Sources/COM/COMExportedInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct COMExportedInterface {
public var unknownPointer: IUnknownPointer {
mutating get {
withUnsafeMutablePointer(to: &comObject) {
IUnknownPointer.cast($0)
IUnknownPointer(OpaquePointer($0))
}
}
}
Expand Down Expand Up @@ -73,7 +73,7 @@ extension COMExportedInterface {
return 1
}

let unmanaged = toUnmanagedUnsafe(IUnknownPointer.cast(this))
let unmanaged = toUnmanagedUnsafe(IUnknownPointer(OpaquePointer(this)))
_ = unmanaged.retain()
// Best effort refcount
return UInt32(_getRetainCount(unmanaged.takeUnretainedValue()))
Expand All @@ -85,7 +85,7 @@ extension COMExportedInterface {
return 0
}

let unmanaged = toUnmanagedUnsafe(IUnknownPointer.cast(this))
let unmanaged = toUnmanagedUnsafe(IUnknownPointer(OpaquePointer(this)))
let oldRetainCount = _getRetainCount(unmanaged.takeUnretainedValue())
unmanaged.release()
// Best effort refcount
Expand All @@ -108,7 +108,7 @@ extension COMExportedInterface {

return HResult.catchValue {
let id = GUIDProjection.toSwift(iid.pointee)
let this = IUnknownPointer.cast(this)
let this = IUnknownPointer(OpaquePointer(this))
let reference = id == markerInterfaceId
? IUnknownReference(addingRef: this)
: try (unwrapUnsafe(this) as! IUnknown)._queryInterface(id)
Expand Down
2 changes: 0 additions & 2 deletions Support/Sources/COM/COMReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,3 @@ public struct COMReference<Interface>: ~Copyable /* where Interface: COMIUnknown
interop.release()
}
}

public typealias IUnknownReference = COMReference<WindowsRuntime_ABI.SWRT_IUnknown>
3 changes: 1 addition & 2 deletions Support/Sources/COM/IErrorInfo.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import WindowsRuntime_ABI
import struct Foundation.UUID

public typealias IErrorInfo = any IErrorInfoProtocol
public protocol IErrorInfoProtocol: IUnknownProtocol {
var guid: Foundation.UUID { get throws }
var source: String? { get throws }
Expand All @@ -9,8 +10,6 @@ public protocol IErrorInfoProtocol: IUnknownProtocol {
var helpContext: UInt32 { get throws }
}

public typealias IErrorInfo = any IErrorInfoProtocol

public enum IErrorInfoProjection: COMTwoWayProjection {
public typealias SwiftObject = IErrorInfo
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IErrorInfo
Expand Down
7 changes: 5 additions & 2 deletions Support/Sources/COM/IUnknown.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import WindowsRuntime_ABI

public typealias IUnknown = any IUnknownProtocol
public protocol IUnknownProtocol: AnyObject {
func _queryInterface(_ id: COMInterfaceID) throws -> IUnknownReference
}
public typealias IUnknown = any IUnknownProtocol

extension IUnknownProtocol {
public func _queryInterface<Projection: COMProjection>(_: Projection.Type) throws -> COMReference<Projection.COMInterface> {
Expand Down Expand Up @@ -42,4 +42,7 @@ public enum IUnknownProjection: COMTwoWayProjection {

extension WindowsRuntime_ABI.SWRT_IUnknown: /* @retroactive */ COMIUnknownStruct {
public static let iid = COMInterfaceID(0x00000000, 0x0000, 0x0000, 0xC000, 0x000000000046)
}
}

public typealias IUnknownPointer = IUnknownProjection.COMPointer
public typealias IUnknownReference = COMReference<IUnknownProjection.COMInterface>
19 changes: 0 additions & 19 deletions Support/Sources/COM/IUnknownPointer.swift

This file was deleted.

3 changes: 1 addition & 2 deletions Support/Sources/WindowsRuntime/IActivationFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ public func getActivationFactory(activatableId: String) throws -> COM.COMReferen
try getActivationFactory(activatableId: activatableId, id: WindowsRuntime_ABI.SWRT_IActivationFactory.iid)
}

public typealias IActivationFactory = any IActivationFactoryProtocol
public protocol IActivationFactoryProtocol: IInspectableProtocol {
func activateInstance() throws -> IInspectable
}

public typealias IActivationFactory = any IActivationFactoryProtocol

public enum IActivationFactoryProjection: WinRTProjection {
public typealias SwiftObject = IActivationFactory
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IActivationFactory
Expand Down
2 changes: 1 addition & 1 deletion Support/Sources/WindowsRuntime/IBufferByteAccess.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import WindowsRuntime_ABI
import COM

public typealias IBufferByteAccess = any IBufferByteAccessProtocol
public protocol IBufferByteAccessProtocol: IUnknownProtocol {
var buffer: UnsafeMutablePointer<UInt8> { get throws }
}
public typealias IBufferByteAccess = any IBufferByteAccessProtocol

public enum IBufferByteAccessProjection: COMTwoWayProjection {
public typealias SwiftObject = IBufferByteAccess
Expand Down
5 changes: 4 additions & 1 deletion Support/Sources/WindowsRuntime/IInspectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import COM
import WindowsRuntime_ABI
import struct Foundation.UUID

public typealias IInspectable = any IInspectableProtocol
public protocol IInspectableProtocol: IUnknownProtocol {
func getIids() throws -> [COMInterfaceID]
func getRuntimeClassName() throws -> String
func getTrustLevel() throws -> TrustLevel
}
public typealias IInspectable = any IInspectableProtocol

public enum IInspectableProjection: WinRTTwoWayProjection {
public typealias SwiftObject = IInspectable
Expand Down Expand Up @@ -49,6 +49,9 @@ extension WindowsRuntime_ABI.SWRT_IInspectable: /* @retroactive */ COMIInspectab
public static let iid = COMInterfaceID(0xAF86E2E0, 0xB12D, 0x4C6A, 0x9C5A, 0xD7AA65101E90)
}

public typealias IInspectablePointer = IInspectableProjection.COMPointer
public typealias IInspectableReference = COMReference<IInspectableProjection.COMInterface>

extension COMInterop where Interface: /* @retroactive */ COMIInspectableStruct {
private var inspectable: UnsafeMutablePointer<WindowsRuntime_ABI.SWRT_IInspectable>{
this.withMemoryRebound(to: WindowsRuntime_ABI.SWRT_IInspectable.self, capacity: 1) { $0 }
Expand Down
12 changes: 0 additions & 12 deletions Support/Sources/WindowsRuntime/IInspectablePointer.swift

This file was deleted.

3 changes: 1 addition & 2 deletions Support/Sources/WindowsRuntime/IRestrictedErrorInfo.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import WindowsRuntime_ABI
import struct Foundation.UUID

public typealias IRestrictedErrorInfo = any IRestrictedErrorInfoProtocol
public protocol IRestrictedErrorInfoProtocol: IUnknownProtocol {
func getErrorDetails(
description: inout String?,
Expand All @@ -10,8 +11,6 @@ public protocol IRestrictedErrorInfoProtocol: IUnknownProtocol {
var reference: String? { get throws }
}

public typealias IRestrictedErrorInfo = any IRestrictedErrorInfoProtocol

public enum IRestrictedErrorInfoProjection: COMTwoWayProjection {
public typealias SwiftObject = IRestrictedErrorInfo
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IRestrictedErrorInfo
Expand Down
3 changes: 1 addition & 2 deletions Support/Sources/WindowsRuntime/IStringable.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import WindowsRuntime_ABI

public typealias IStringable = any IStringableProtocol
public protocol IStringableProtocol: IUnknownProtocol {
func toString() throws -> String
}

public typealias IStringable = any IStringableProtocol

public enum IStringableProjection: COMTwoWayProjection {
public typealias SwiftObject = IStringable
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IStringable
Expand Down
3 changes: 1 addition & 2 deletions Support/Sources/WindowsRuntime/IWeakReference.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import WindowsRuntime_ABI

public typealias IWeakReference = any IWeakReferenceProtocol
public protocol IWeakReferenceProtocol: IUnknownProtocol {
func resolve() throws -> IInspectable?
}

public typealias IWeakReference = any IWeakReferenceProtocol

public enum IWeakReferenceProjection: COMTwoWayProjection {
public typealias SwiftObject = IWeakReference
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IWeakReference
Expand Down
3 changes: 1 addition & 2 deletions Support/Sources/WindowsRuntime/IWeakReferenceSource.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import WindowsRuntime_ABI

public typealias IWeakReferenceSource = any IWeakReferenceSourceProtocol
public protocol IWeakReferenceSourceProtocol: IUnknownProtocol {
func getWeakReference() throws -> IWeakReference
}

public typealias IWeakReferenceSource = any IWeakReferenceSourceProtocol

public enum IWeakReferenceSourceProjection: COMTwoWayProjection {
public typealias SwiftObject = IWeakReferenceSource
public typealias COMInterface = WindowsRuntime_ABI.SWRT_IWeakReferenceSource
Expand Down
6 changes: 3 additions & 3 deletions Support/Sources/WindowsRuntime/WinRTComposableClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ open class WinRTComposableClass: IInspectableProtocol {
// - Factory needs an initialized outer pointer pointing to self
// - self.inner needs to be initialized before being able to reference self
self.outer = .uninitialized
self.innerWithRef = IInspectablePointer.cast(outer.unknownPointer) // We need to assign inner to something, it doesn't matter what.
self.innerWithRef = IInspectablePointer(OpaquePointer(outer.unknownPointer)) // We need to assign inner to something, it doesn't matter what.
self.outer = .init(swiftObject: self, virtualTable: IInspectableProjection.virtualTablePointer)

// Like C++/WinRT, discard the returned composed object and only use the inner object
// The composed object is useful only when not providing an outer object.
var inner: IInspectablePointer? = nil
let composed = try _factory(IInspectablePointer.cast(outer.unknownPointer), &inner)
let composed = try _factory(IInspectablePointer(OpaquePointer(outer.unknownPointer)), &inner)
if let composed { COMInterop(composed).release() }
guard let inner else { throw HResult.Error.fail }
self.innerWithRef = inner
Expand All @@ -56,7 +56,7 @@ open class WinRTComposableClass: IInspectableProtocol {
var inner: IInspectablePointer? = nil
defer { IInspectableProjection.release(&inner) }
guard let composed = try _factory(nil, &inner) else { throw HResult.Error.fail }
self.innerWithRef = IInspectablePointer.cast(composed)
self.innerWithRef = IInspectablePointer(OpaquePointer(composed))
}
}

Expand Down

0 comments on commit 02ee970

Please sign in to comment.