Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a generic overload to _queryInterface #117

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Support/Sources/COM/COMInterop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public struct COMInterop<Interface> /* where Interface: COMIUnknownStruct */ {
let pointer = rawPointer.bindMemory(to: WindowsRuntime_ABI.SWRT_IUnknown.self, capacity: 1)
return COMReference(transferringRef: pointer)
}

public func queryInterface<Other /* COMIUnknownStruct */>(
_ id: COMInterfaceID, type: Other.Type = Other.self) throws -> COMReference<Other> {
(try queryInterface(id) as IUnknownReference).reinterpret(to: type)
}
}
3 changes: 1 addition & 2 deletions Support/Sources/COM/IAgileObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public enum IAgileObjectProjection: COMProjection {
public static var interfaceID: COMInterfaceID { COMInterface.iid }

public static func _wrap(_ reference: consuming COMReference<COMInterface>) -> SwiftObject {
let reference = reference.reinterpret(to: WindowsRuntime_ABI.SWRT_IUnknown.self)
return IUnknownProjection._wrap(consume reference)
IUnknownProjection._wrap(reference.reinterpret())
}

public static func toCOM(_ object: SwiftObject) throws -> COMReference<COMInterface> {
Expand Down
10 changes: 7 additions & 3 deletions Support/Sources/COM/IUnknown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ public protocol IUnknownProtocol: AnyObject {
}

extension IUnknownProtocol {
public func _queryInterface<Interface /* COMIUnknownStruct */>(
_ id: COMInterfaceID, type: Interface.Type = Interface.self) throws -> COMReference<Interface> {
(try _queryInterface(id) as IUnknownReference).reinterpret(to: type)
}

public func _queryInterface<Projection: COMProjection>(_: Projection.Type) throws -> COMReference<Projection.COMInterface> {
try _queryInterface(Projection.interfaceID).reinterpret()
try _queryInterface(Projection.interfaceID)
}

public func queryInterface<Projection: COMProjection>(_: Projection.Type) throws -> Projection.SwiftObject {
let reference = try self._queryInterface(Projection.self)
return Projection.toSwift(consume reference)
Projection.toSwift(try self._queryInterface(Projection.self))
}
}

Expand Down
3 changes: 1 addition & 2 deletions Support/Sources/WindowsRuntime/IActivationFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ extension COMInterop where Interface == WindowsRuntime_ABI.SWRT_IActivationFacto
defer { IInspectableProjection.release(&inspectable) }
guard let inspectable else { throw COM.HResult.Error.noInterface }
return try COMInterop<IInspectableProjection.COMInterface>(inspectable)
.queryInterface(projection.interfaceID)
.reinterpret(to: Projection.COMInterface.self)
.queryInterface(projection.interfaceID, type: Projection.COMInterface.self)
.detach()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public enum IReferenceUnboxingProjection {
}

public static func toCOM(_ value: SwiftObject) throws -> COMReference<COMInterface> {
try Projection.box(value)._queryInterface(interfaceID).reinterpret()
try Projection.box(value)._queryInterface(interfaceID)
}
}
}
6 changes: 3 additions & 3 deletions Support/Sources/WindowsRuntime/IWeakReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public enum IWeakReferenceProjection: COMTwoWayProjection {
var inspectable = try IInspectableProjection.toABI($0.resolve())
defer { IInspectableProjection.release(&inspectable) }
guard let inspectable else { return }
let targetUnknown = try COMInterop(inspectable).queryInterface(GUIDProjection.toSwift(iid.pointee))
let target = targetUnknown.reinterpret(to: WindowsRuntime_ABI.SWRT_IInspectable.self)
objectReference.pointee = target.detach()
objectReference.pointee = try COMInterop(inspectable)
.queryInterface(GUIDProjection.toSwift(iid.pointee), type: SWRT_IInspectable.self)
.detach()
} })
}

Expand Down
10 changes: 5 additions & 5 deletions Support/Sources/WindowsRuntime/WeakReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import WindowsRuntime_ABI

/// A weak reference to a WinRT object.
public final class WeakReference<Projection: WinRTReferenceTypeProjection> {
private var weakReference: COMReference<WindowsRuntime_ABI.SWRT_IWeakReference>
private var weakReference: COMReference<SWRT_IWeakReference>

public init(_ target: Projection.SwiftObject) throws {
guard let targetInspectable = target as? IInspectable else { throw HResult.Error.invalidArg }
let source = try targetInspectable._queryInterface(WindowsRuntime_ABI.SWRT_IWeakReferenceSource.iid)
.reinterpret(to: WindowsRuntime_ABI.SWRT_IWeakReferenceSource.self)
var weakReference: UnsafeMutablePointer<WindowsRuntime_ABI.SWRT_IWeakReference>?
let source = try targetInspectable._queryInterface(
SWRT_IWeakReferenceSource.iid, type: SWRT_IWeakReferenceSource.self)
var weakReference: UnsafeMutablePointer<SWRT_IWeakReference>?
try WinRTError.throwIfFailed(source.pointer.pointee.VirtualTable.pointee.GetWeakReference(source.pointer, &weakReference))
guard let weakReference else { throw HResult.Error.fail }
self.weakReference = .init(transferringRef: weakReference)
}

public func resolve() throws -> Projection.SwiftObject? {
var inspectableTarget: UnsafeMutablePointer<WindowsRuntime_ABI.SWRT_IInspectable>? = nil
var inspectableTarget: UnsafeMutablePointer<SWRT_IInspectable>? = nil
var iid = GUIDProjection.toABI(Projection.interfaceID)
try WinRTError.throwIfFailed(weakReference.pointer.pointee.VirtualTable.pointee.Resolve(
weakReference.pointer, &iid, &inspectableTarget))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ open class WinRTMetaclassResolver {

open func getActivationFactory<COMInterface>(runtimeClass: String, interfaceID: COMInterfaceID) throws -> COMReference<COMInterface> {
let activationFactory = try getActivationFactory(runtimeClass: runtimeClass)
return try activationFactory.interop.queryInterface(interfaceID).reinterpret(to: COMInterface.self)
return try activationFactory.interop.queryInterface(interfaceID)
}

open func getActivationFactory(runtimeClass: String) throws -> COMReference<SWRT_IActivationFactory> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension WinRTBoxableProjection {

public static func unbox(_ inspectable: IInspectable) -> SwiftValue? {
do {
let ireference = try inspectable._queryInterface(ireferenceID).reinterpret(to: SWRT_WindowsFoundation_IReference.self)
let ireference = try inspectable._queryInterface(ireferenceID, type: SWRT_WindowsFoundation_IReference.self)
var abiValue = abiDefaultValue
try withUnsafeMutablePointer(to: &abiValue) { abiValuePointer in
_ = try WinRTError.throwIfFailed(ireference.pointer.pointee.VirtualTable.pointee.get_Value(ireference.pointer, abiValuePointer))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension WindowsFoundation_IReferenceProtocol {

public enum WindowsFoundation_IReferenceProjection<TProjection: WinRTBoxableProjection>: WinRTInterfaceProjection {
public typealias SwiftObject = WindowsFoundation_IReference<TProjection.SwiftValue>
public typealias COMInterface = WindowsRuntime_ABI.SWRT_WindowsFoundation_IReference
public typealias COMInterface = SWRT_WindowsFoundation_IReference

public static var typeName: String { fatalError("Windows.Foundation.IReference`1<\(TProjection.typeName)>") }
public static var interfaceID: COMInterfaceID { TProjection.ireferenceID }
Expand All @@ -42,12 +42,11 @@ public enum WindowsFoundation_IReferenceProjection<TProjection: WinRTBoxableProj
WindowsFoundation_IReferenceProtocol {
public typealias T = TProjection.SwiftValue

private var _lazyIPropertyValue: COMLazyReference<WindowsRuntime_ABI.SWRT_WindowsFoundation_IPropertyValue> = .init()
public var _ipropertyValue: COMInterop<WindowsRuntime_ABI.SWRT_WindowsFoundation_IPropertyValue> {
private var _lazyIPropertyValue: COMLazyReference<SWRT_WindowsFoundation_IPropertyValue> = .init()
public var _ipropertyValue: COMInterop<SWRT_WindowsFoundation_IPropertyValue> {
get throws {
try _lazyIPropertyValue.getInterop {
try _queryInterface(WindowsRuntime_ABI.SWRT_WindowsFoundation_IPropertyValue.iid)
.reinterpret(to: WindowsRuntime_ABI.SWRT_WindowsFoundation_IPropertyValue.self)
try _queryInterface(SWRT_WindowsFoundation_IPropertyValue.iid)
}
}
}
Expand Down Expand Up @@ -76,7 +75,7 @@ public enum WindowsFoundation_IReferenceProjection<TProjection: WinRTBoxableProj

// A generic type cannot have stored properties,
// and closures converted to C function pointers cannot capture generic arguments.
fileprivate var virtualTable: WindowsRuntime_ABI.SWRT_WindowsFoundation_IReferenceVTable = .init(
fileprivate var virtualTable: SWRT_WindowsFoundation_IReferenceVTable = .init(
QueryInterface: { COMExportedInterface.QueryInterface($0, $1, $2) },
AddRef: { COMExportedInterface.AddRef($0) },
Release: { COMExportedInterface.Release($0) },
Expand All @@ -93,12 +92,12 @@ fileprivate var virtualTable: WindowsRuntime_ABI.SWRT_WindowsFoundation_IReferen
})

#if swift(>=5.10)
extension WindowsRuntime_ABI.SWRT_WindowsFoundation_IReference: @retroactive WindowsRuntime.COMIInspectableStruct {}
extension SWRT_WindowsFoundation_IReference: @retroactive WindowsRuntime.COMIInspectableStruct {}
#else
extension WindowsRuntime_ABI.SWRT_WindowsFoundation_IReference: WindowsRuntime.COMIInspectableStruct {}
extension SWRT_WindowsFoundation_IReference: WindowsRuntime.COMIInspectableStruct {}
#endif

extension COMInterop where Interface == WindowsRuntime_ABI.SWRT_WindowsFoundation_IReference {
extension COMInterop where Interface == SWRT_WindowsFoundation_IReference {
public func get_Value(_ value: UnsafeMutableRawPointer) throws {
try HResult.throwIfFailed(this.pointee.VirtualTable.pointee.get_Value(this, value))
}
Expand Down
Loading