From fe63cb9f7e264864681ac75360c48417f32c32a1 Mon Sep 17 00:00:00 2001 From: Steve Kirbach Date: Tue, 14 Nov 2023 11:28:03 -0800 Subject: [PATCH] fix memory leaks (#123) * new comptr stuff is building! * all building...but crashing * all gucci * add comments describing ComPtrs design * remove IUnknown2 * undo unnecessary add * fixing ref count of comptr * Revert "undo unnecessary add" This reverts commit cbabcebe78cf09e6fdf2965c620c0df46153ed28. * working for weak ref, but this might not actually work * fix event subscriptions * fix ref count for aggregated objects * cache interfaces as lazy vars on the classes * properly hide _inner * Update swiftwinrt/Resources/Support/ComPtr.swift Co-authored-by: Jeff * Update swiftwinrt/Resources/Support/ComPtr.swift Co-authored-by: Jeff * return identity and don't use ComObject since classes require strong ref to identity * fix build break * use spi and some other cleanup so i don't need dubious _getRetainCount call * add extra test cases * add deinitializers to ensure proper cleanup * Update swiftwinrt/Resources/Support/Aggregation.swift Co-authored-by: Jeff --------- Co-authored-by: Jeff --- .vscode/launch.json | 4 +- .vscode/settings.json | 5 +- CMakePresets.json | 2 - swiftwinrt/CMakeLists.txt | 7 +- .../Resources/Support/Aggregation.swift | 92 ++- swiftwinrt/Resources/Support/ComPtr.swift | 126 ++++ .../Support/CustomQueryInterface.swift | 17 +- .../Resources/Support/ErrorHandling.swift | 11 +- .../Resources/Support/IInspectable.swift | 29 +- .../Resources/Support/IUnknown+Swift.swift | 43 +- swiftwinrt/Resources/Support/IUnknown.swift | 20 +- .../Resources/Support/IUnknownRef.swift | 42 +- swiftwinrt/Resources/Support/Marshaler.swift | 18 +- .../Resources/Support/PropertyValue+ABI.swift | 170 ++--- .../Resources/Support/PropertyValue.swift | 6 +- swiftwinrt/Resources/Support/RawTyped.swift | 4 + .../Resources/Support/Runtime+Swift.swift | 16 +- .../Resources/Support/WinRTProtocols.swift | 55 +- .../Resources/Support/WinRTWrapperBase.swift | 97 ++- swiftwinrt/code_writers.h | 289 ++++---- swiftwinrt/code_writers/common_writers.h | 16 +- swiftwinrt/code_writers/struct_writers.cpp | 2 +- swiftwinrt/helpers.h | 15 + swiftwinrt/resources.rc | 1 + swiftwinrt/text_writer.h | 89 +-- tests/CMakeLists.txt | 3 - tests/test_app/AggregationTests.swift | 88 ++- tests/test_app/MemoryManagementTests.swift | 63 ++ tests/test_app/main.swift | 2 +- .../test_component/Support/MAKEFROMABI.swift | 31 - .../Windows.Foundation+ABI.swift | 24 +- .../Windows.Foundation+Impl.swift | 26 +- .../Windows.Foundation.Collections+Impl.swift | 22 +- .../Windows.Foundation.Collections.swift | 147 ++-- .../test_component/Windows.Foundation.swift | 34 +- .../test_component/test_component+ABI.swift | 465 +++++++------ .../test_component+Generics.swift | 633 +++++++++--------- .../test_component/test_component+Impl.swift | 60 +- .../test_component.Delegates+Impl.swift | 6 +- .../test_component/test_component.swift | 594 +++++++--------- tests/test_component/cpp/Base.cpp | 4 + tests/test_component/cpp/Base.h | 2 +- tests/test_component/cpp/CMakeLists.txt | 21 +- 43 files changed, 1862 insertions(+), 1539 deletions(-) create mode 100644 swiftwinrt/Resources/Support/ComPtr.swift create mode 100644 tests/test_app/MemoryManagementTests.swift delete mode 100644 tests/test_component/Sources/test_component/Support/MAKEFROMABI.swift diff --git a/.vscode/launch.json b/.vscode/launch.json index 88ec0c72..6e47e5d7 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,10 +1,10 @@ { "configurations": [ { - "type": "lldb", + "type": "swift-lldb", "request": "launch", "name": "test_app", - "program": "${workspaceFolder:swiftwinrt}\\out\\${command:cmake.activeBuildPresetName}\\bin\\test_app", + "program": "${workspaceFolder:swiftwinrt}\\out\\${command:cmake.activeBuildPresetName}\\bin\\test_app.exe", "args": [], "cwd": "${workspaceFolder:swiftwinrt}/tests", "preLaunchTask": "Install" diff --git a/.vscode/settings.json b/.vscode/settings.json index 8f1c90d7..da2e72e5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -34,6 +34,9 @@ "tuple": "cpp", "unordered_map": "cpp", "variant": "cpp", - "xiosbase": "cpp" + "xiosbase": "cpp", + "coroutine": "cpp", + "resumable": "cpp", + "string": "cpp" } } \ No newline at end of file diff --git a/CMakePresets.json b/CMakePresets.json index 410c464a..e19d2ba4 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -15,8 +15,6 @@ "strategy": "external" }, "cacheVariables": { - "CMAKE_C_COMPILER": "cl", - "CMAKE_CXX_COMPILER": "cl", "CMAKE_SYSTEM_VERSION": "10.0.17763.0", "CMAKE_MODULE_PATH": "${sourceDir}/cmake", "CMAKE_SKIP_INSTALL_ALL_DEPENDENCY": true diff --git a/swiftwinrt/CMakeLists.txt b/swiftwinrt/CMakeLists.txt index d606cf82..43ba8a5f 100644 --- a/swiftwinrt/CMakeLists.txt +++ b/swiftwinrt/CMakeLists.txt @@ -1,5 +1,8 @@ project(swiftwinrt) +set(CMAKE_C_COMPILER cl) +set(CMAKE_CXX_COMPILER cl) + set(SWIFTWINRT_VERSION_STRING "0.0.1") set(MicrosoftWindowsWinMD_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/winmd/src) @@ -30,7 +33,7 @@ string(APPEND CMAKE_SHARED_LINKER_FLAGS_RELEASE " /DEBUG /OPT:REF /OPT:ICF /MAP" string(APPEND CMAKE_EXE_LINKER_FLAGS_RELEASE " /DEBUG /OPT:REF /OPT:ICF /MAP") if (CMAKE_CXX_COMPILER MATCHES "clang-cl") - add_compile_options(-Wno-delete-non-virtual-dtor -mcx16 -fno-delayed-template-parsing -Xclang -fcoroutines-ts) + add_compile_options(-Wno-delete-non-virtual-dtor -mcx16 -fno-delayed-template-parsing) else() add_compile_options(/permissive- /await) endif() @@ -47,7 +50,7 @@ endif() add_executable(swiftwinrt "") target_sources(swiftwinrt PUBLIC main.cpp - pch.cpp + pch.cpp metadata_cache.cpp types.cpp metadata_filter.cpp diff --git a/swiftwinrt/Resources/Support/Aggregation.swift b/swiftwinrt/Resources/Support/Aggregation.swift index e542fe32..5d3f07c8 100644 --- a/swiftwinrt/Resources/Support/Aggregation.swift +++ b/swiftwinrt/Resources/Support/Aggregation.swift @@ -1,12 +1,52 @@ import C_BINDINGS_MODULE import Foundation -public protocol ComposableImpl : AbiInterfaceBridge where SwiftABI: IInspectable, SwiftProjection: WinRTClass { +// The WinRTClassWeakReference class is a proxy for properly managing the reference count of +// the WinRTClass that is being aggregated. The aggregated object holds a weak reference to +// the outer IInspectable (swift) that is passed in during construction. In general, the +// swift wrappers we create hold strong references to the objects they are wrapping, as we +// expect an AddRef from WinRT to keep the object alive. Since this doesn't happen for aggregated +// objects, we need a proxy which sits in the middle. The WinRTClassWeakReference object doesn't +// keep a strong ref to the swift object, but it forwards all AddRef/Release calls from WinRT +// to the swift object, to ensure it doesn't get cleaned up. The Swift object in turn holds a strong +// reference to this object so that it stays alive. +@_spi(WinRTInternal) +public final class WinRTClassWeakReference { + fileprivate weak var instance: Class? + public init(_ instance: Class){ + self.instance = instance + } +} + +extension WinRTClassWeakReference: CustomQueryInterface { + @_spi(WinRTImplements) + public func queryInterface(_ iid: SUPPORT_MODULE.IID) -> IUnknownRef? { + guard let instance else { return nil } + return instance.queryInterface(iid) + } +} + +extension WinRTClassWeakReference: CustomAddRef { + func addRef() { + guard let instance else { return } + let unmanaged = Unmanaged.passUnretained(instance) + _ = unmanaged.retain() + } + + func release() { + guard let instance else { return } + let unmanaged = Unmanaged.passUnretained(instance) + unmanaged.release() + } +} + +@_spi(WinRTInternal) +public protocol ComposableImpl : AbiInterfaceBridge where SwiftABI: IInspectable, SwiftProjection: WinRTClassWeakReference { + associatedtype Class: WinRTClass associatedtype Default : AbiInterface where Default.SwiftABI: SUPPORT_MODULE.IInspectable static func makeAbi() -> CABI } - // At a high level, aggregation simply requires the WinRT object to have a pointer back to the Swift world, so that it can call // overridable methods on the class. This Swift pointer is given to the WinRT object during construction. The construction of the // WinRT object returns us two different pointers: @@ -24,11 +64,12 @@ public protocol ComposableImpl : AbiInterfaceBridge where SwiftABI: IInspectable // |---------------|---------------------------|-------------------------|--------------------------| // | Yes | self | stored on swift object | ignored or stored | // | No | nil | ignored | stored on swift object | +@_spi(WinRTInternal) public func MakeComposed( composing: Composable.Type, - _ this: Composable.SwiftProjection, - _ createCallback: (UnsealedWinRTClassWrapper?, inout SUPPORT_MODULE.IInspectable?) -> Composable.Default.SwiftABI) -> SUPPORT_MODULE.IInspectable { - let aggregated = type(of: this) != Composable.SwiftProjection.self + _ this: Composable.Class, + _ createCallback: (UnsealedWinRTClassWrapper?, inout SUPPORT_MODULE.IInspectable?) -> Composable.Default.SwiftABI) { + let aggregated = type(of: this) != Composable.Class.self let wrapper:UnsealedWinRTClassWrapper? = .init(aggregated ? this : nil) var innerInsp: SUPPORT_MODULE.IInspectable? = nil @@ -37,21 +78,36 @@ public func MakeComposed( fatalError("Unexpected nil returned after successful creation") } - return aggregated ? innerInsp : base + if let wrapper { + this.identity = ComPtr(wrapper.toIInspectableABI { $0 }) + // Storing a strong ref to the wrapper adds a ref to ourselves, remove the + // reference + wrapper.swiftObj.release() + } + this._inner = aggregated ? innerInsp : base } +@_spi(WinRTInternal) public class UnsealedWinRTClassWrapper : WinRTAbiBridgeWrapper { override public class var IID: SUPPORT_MODULE.IID { Composable.SwiftABI.IID } - public init?(_ impl: Composable.SwiftProjection?) { + public init?(_ impl: Composable.Class?) { guard let impl = impl else { return nil } let abi = Composable.makeAbi() - super.init(abi, impl) + super.init(abi, Composable.SwiftProjection(impl)) + } + + public static func unwrapFrom(base: ComPtr) -> Composable.Class? { + let overrides: Composable.SwiftABI = try! base.queryInterface() + if let weakRef = tryUnwrapFrom(abi: RawPointer(overrides)) { return weakRef.instance } + guard let instance = makeFrom(abi: overrides) else { + // the derived class doesn't exist, which is fine, just return the type the API specifies. + return make(type: Composable.Class.self, from: overrides) + } + return instance as? Composable.Class } - public static func unwrapFrom(base: UnsafeMutablePointer) -> Composable.SwiftProjection? { - let baseInsp = SUPPORT_MODULE.IInspectable(consuming: base) - let overrides: Composable.SwiftABI = try! baseInsp.QueryInterface() - return unwrapFrom(abi: RawPointer(overrides)) + public static func tryUnwrapFrom(raw pUnk: UnsafeMutableRawPointer?) -> Composable.Class? { + tryUnwrapFromBase(raw: pUnk)?.instance } public func toIInspectableABI(_ body: (UnsafeMutablePointer) throws -> ResultType) @@ -62,13 +118,7 @@ public class UnsealedWinRTClassWrapper : WinRTAbiBri } public extension ComposableImpl { - static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { - guard let abi else { return nil } - let baseInsp = SUPPORT_MODULE.IInspectable(abi) - guard let instance = makeFrom(abi: baseInsp) else { - // the derived class doesn't exist, which is fine, just return the type the API specifies. - return make(type: Self.SwiftProjection.self, from: baseInsp) - } - return instance as? Self.SwiftProjection + static func from(abi: ComPtr?) -> SwiftProjection? { + return nil } -} \ No newline at end of file +} diff --git a/swiftwinrt/Resources/Support/ComPtr.swift b/swiftwinrt/Resources/Support/ComPtr.swift new file mode 100644 index 00000000..87b15809 --- /dev/null +++ b/swiftwinrt/Resources/Support/ComPtr.swift @@ -0,0 +1,126 @@ +// Copyright © 2023 The Browser Company +// SPDX-License-Identifier: BSD-3 +import C_BINDINGS_MODULE + +// ComPtr is a smart pointer for COM interfaces. It holds on to the underlying pointer + +// and the semantics of it are meant to mirror that of the ComPtr class in WRL. The +// design of ComPtr and ComPtrs.intialize is that there should be no use of UnsafeMutablePointer +// anywhere else in the code base. The only place where UnsafeMutablePointer should be used is + +// where it's required at the ABI boundary. +public class ComPtr { + fileprivate var pUnk: UnsafeMutablePointer? + + public init(_ ptr: UnsafeMutablePointer) { + self.pUnk = ptr + asIUnknown { + _ = $0.pointee.lpVtbl.pointee.AddRef($0) + } + } + + public convenience init?(_ ptr: UnsafeMutablePointer?) { + guard let ptr else { return nil } + self.init(ptr) + } + + fileprivate init?(takingOwnership ptr: UnsafeMutablePointer?) { + guard let ptr else { return nil } + self.pUnk = ptr + } + + // Release ownership of the underlying pointer and return it. This is + // useful when assigning to an out parameter and avoids an extra Add/Ref + // release call. + public func detach() -> UnsafeMutableRawPointer? { + let result = pUnk + pUnk = nil + return UnsafeMutableRawPointer(result) + } + + public func get() -> UnsafeMutablePointer { + guard let pUnk else { preconditionFailure("get() called on nil pointer") } + return pUnk + } + + deinit { + release() + } + + private func release() { + guard pUnk != nil else { return } + asIUnknown { + _ = $0.pointee.lpVtbl.pointee.Release($0) + } + } + + func asIUnknown(_ body: (UnsafeMutablePointer) throws -> ResultType) rethrows -> ResultType { + guard let pUnk else { preconditionFailure("asIUnknown called on nil pointer") } + return try pUnk.withMemoryRebound(to: C_IUnknown.self, capacity: 1) { try body($0) } + } +} + +public extension ComPtr { + func queryInterface() throws -> Interface { + let ptr = try self.asIUnknown { pUnk in + var iid = Interface.IID + return try ComPtrs.initialize(to: C_IUnknown.self) { result in + try CHECKED(pUnk.pointee.lpVtbl.pointee.QueryInterface(pUnk, &iid, &result)) + } + } + return .init(ptr!) + } +} + +// ComPtrs properly initializes pointers who have ownership of the underlying raw pointers. This is used at the ABI boundary layer, for example: +// let (return1, return2) = try ComPtrs.initialize { return1Abi, return2Abi in +// try CHECKED(pThis.pointee.lpVtbl.pointee.Method(pThis, &return1Abi, &return2Abi)) +// } +public struct ComPtrs { + // Note: The single initialization methods still return a tuple for ease of code generation + public static func initialize(to: I.Type, _ body: (inout UnsafeMutableRawPointer?) throws -> ()) rethrows -> (ComPtr?) { + var ptrRaw: UnsafeMutableRawPointer? + try body(&ptrRaw) + return (ComPtr(takingOwnership: ptrRaw?.assumingMemoryBound(to: I.self))) + } + + public static func initialize(_ body: (inout UnsafeMutablePointer?) throws -> ()) rethrows -> (ComPtr?) { + var ptr: UnsafeMutablePointer? + try body(&ptr) + return (ComPtr(takingOwnership: ptr)) + } + + public static func initialize(_ body: (inout UnsafeMutablePointer?, inout UnsafeMutablePointer?) throws -> ()) rethrows -> (ComPtr?, ComPtr?) { + var ptr1: UnsafeMutablePointer? + var ptr2: UnsafeMutablePointer? + try body(&ptr1, &ptr2) + return (ComPtr(takingOwnership: ptr1), ComPtr(takingOwnership: ptr2)) + } + + public static func initialize(_ body: (inout UnsafeMutablePointer?, inout UnsafeMutablePointer?, inout UnsafeMutablePointer?) throws -> ()) rethrows -> (ComPtr?, ComPtr?, ComPtr?) { + var ptr1: UnsafeMutablePointer? + var ptr2: UnsafeMutablePointer? + var ptr3: UnsafeMutablePointer? + try body(&ptr1, &ptr2, &ptr3) + return (ComPtr(takingOwnership: ptr1), ComPtr(takingOwnership: ptr2), ComPtr(takingOwnership: ptr3)) + } + + public static func initialize(_ body: (inout UnsafeMutablePointer?, inout UnsafeMutablePointer?, inout UnsafeMutablePointer?, inout UnsafeMutablePointer?) throws -> ()) rethrows -> (ComPtr?, ComPtr?, ComPtr?, ComPtr?) { + var ptr1: UnsafeMutablePointer? + var ptr2: UnsafeMutablePointer? + var ptr3: UnsafeMutablePointer? + var ptr4: UnsafeMutablePointer? + try body(&ptr1, &ptr2, &ptr3, &ptr4) + return (ComPtr(takingOwnership: ptr1), ComPtr(takingOwnership: ptr2), ComPtr(takingOwnership: ptr3), ComPtr(takingOwnership: ptr4)) + } + + public static func initialize(_ body: (inout UnsafeMutablePointer?, inout UnsafeMutablePointer?, inout UnsafeMutablePointer?, inout UnsafeMutablePointer?, inout UnsafeMutablePointer?) throws -> ()) rethrows -> (ComPtr?, ComPtr?, ComPtr?, ComPtr?, ComPtr?) { + var ptr1: UnsafeMutablePointer? + var ptr2: UnsafeMutablePointer? + var ptr3: UnsafeMutablePointer? + var ptr4: UnsafeMutablePointer? + var ptr5: UnsafeMutablePointer? + try body(&ptr1, &ptr2, &ptr3, &ptr4, &ptr5) + return (ComPtr(takingOwnership: ptr1), ComPtr(takingOwnership: ptr2), ComPtr(takingOwnership: ptr3), ComPtr(takingOwnership: ptr4), ComPtr(takingOwnership: ptr5)) + } +} diff --git a/swiftwinrt/Resources/Support/CustomQueryInterface.swift b/swiftwinrt/Resources/Support/CustomQueryInterface.swift index cdf24c34..5a58fd4d 100644 --- a/swiftwinrt/Resources/Support/CustomQueryInterface.swift +++ b/swiftwinrt/Resources/Support/CustomQueryInterface.swift @@ -9,20 +9,15 @@ public protocol CustomQueryInterface { extension IUnknownRef { func queryInterface(_ iid: SUPPORT_MODULE.IID) -> IUnknownRef? { var iid = iid - var result: UnsafeMutableRawPointer? - guard borrow.pointee.lpVtbl.pointee.QueryInterface(borrow, &iid, &result) == S_OK, let result else { return nil } - return IUnknownRef(consuming: result) + let (ptr) = try? ComPtrs.initialize(to: C_IUnknown.self) { result in + try CHECKED(borrow.pointee.lpVtbl.pointee.QueryInterface(borrow, &iid, &result)) + } + guard let ptr else { return nil} + return IUnknownRef(ptr) } } @_spi(WinRTInternal) -public func queryInterface(_ obj: AnyWinRTClass, _ iid: SUPPORT_MODULE.IID) -> IUnknownRef? { +public func queryInterface(_ obj: WinRTClass, _ iid: SUPPORT_MODULE.IID) -> IUnknownRef? { obj._inner.pUnk.queryInterface(iid) } - -extension WinRTClass { - @_spi(WinRTInternal) - public func queryInterface(_ iid: SUPPORT_MODULE.IID) -> IUnknownRef? { - SUPPORT_MODULE.queryInterface(self, iid) - } -} diff --git a/swiftwinrt/Resources/Support/ErrorHandling.swift b/swiftwinrt/Resources/Support/ErrorHandling.swift index e50abeaf..56842a62 100644 --- a/swiftwinrt/Resources/Support/ErrorHandling.swift +++ b/swiftwinrt/Resources/Support/ErrorHandling.swift @@ -3,14 +3,13 @@ import WinSDK -@_alwaysEmitIntoClient @inline(__always) @discardableResult -public func CHECKED(_ body: () -> HRESULT) throws -> HRESULT { +@_alwaysEmitIntoClient @inline(__always) +public func CHECKED(_ body: () -> HRESULT) throws { let hr: HRESULT = body() guard hr >= 0 else { throw Error(hr: hr) } - return hr } -@_alwaysEmitIntoClient @inline(__always) @discardableResult -public func CHECKED(_ body: @autoclosure () -> HRESULT) throws -> HRESULT { - return try CHECKED(body) +@_alwaysEmitIntoClient @inline(__always) +public func CHECKED(_ body: @autoclosure () -> HRESULT) throws { + try CHECKED(body) } diff --git a/swiftwinrt/Resources/Support/IInspectable.swift b/swiftwinrt/Resources/Support/IInspectable.swift index e76231b1..09bd2576 100644 --- a/swiftwinrt/Resources/Support/IInspectable.swift +++ b/swiftwinrt/Resources/Support/IInspectable.swift @@ -33,10 +33,10 @@ open class IInspectable: IUnknown { // follow this pattern should define their composability contract like the following: // internal class IBaseNoOverrides : OverridesImpl { // internal typealias CABI = C_IInspectable -// internal typealias SwiftABI = __ABI_test_component.IBaseNoOverrides +// internal typealias SwiftABI = __ABI_SUPPORT_MODULE.IBaseNoOverrides // internal typealias SwiftProjection = BaseNoOverrides // internal typealias c_defaultABI = __x_ABI_Ctest__component_CIBaseNoOverrides -// internal typealias swift_overrides = test_component.IInspectable +// internal typealias swift_overrides = SUPPORT_MODULE.IInspectable // } // internal typealias Composable = IBaseNoOverrides public enum __ABI_ { @@ -62,15 +62,18 @@ public enum __ABI_ { return try super.toABI(body) } } - public static func unwrapFrom(abi: UnsafeMutablePointer?) -> Any? { + public static func unwrapFrom(abi: ComPtr?) -> Any? { guard let abi = abi else { return nil } if let instance = tryUnwrapFrom(abi: abi) { return instance } - let ref = IInspectable(consuming: abi) + let ref = IInspectable(abi) return makeFrom(abi: ref) ?? ref } + public static func tryUnwrapFrom(raw pUnk: UnsafeMutableRawPointer?) -> AnyObject? { + tryUnwrapFromBase(raw: pUnk) + } } internal static var IInspectableVTable: C_IInspectableVtbl = .init( @@ -88,23 +91,13 @@ public enum __ABI_ { let swiftObj = AnyWrapper.tryUnwrapFrom(raw: pUnk) if let customQueryInterface = swiftObj as? CustomQueryInterface, let result = customQueryInterface.queryInterface(riid.pointee) { - ppvObject.pointee = UnsafeMutableRawPointer(result.ref) + ppvObject.pointee = result.detach() return S_OK } return E_NOINTERFACE }, - - AddRef: { - guard let wrapper = AnyWrapper.fromRaw($0) else { return 1 } - _ = wrapper.retain() - return ULONG(_getRetainCount(wrapper.takeUnretainedValue().swiftObj)) - }, - - Release: { - guard let wrapper = AnyWrapper.fromRaw($0) else { return 1 } - return ULONG(_getRetainCount(wrapper.takeRetainedValue())) - }, - + AddRef: { AnyWrapper.addRef($0) }, + Release: { AnyWrapper.release($0) }, GetIids: { let size = MemoryLayout.size let iids = CoTaskMemAlloc(UInt64(size) * 2).assumingMemoryBound(to: SUPPORT_MODULE.IID.self) @@ -118,7 +111,7 @@ public enum __ABI_ { GetRuntimeClassName: { guard let instance = AnyWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - guard let winrtClass = instance as? AnyWinRTClass else { + guard let winrtClass = instance as? WinRTClass else { let string = String(reflecting: type(of: instance)) $1!.pointee = try! HString(string).detach() return S_OK diff --git a/swiftwinrt/Resources/Support/IUnknown+Swift.swift b/swiftwinrt/Resources/Support/IUnknown+Swift.swift index 031cabbc..6720c274 100644 --- a/swiftwinrt/Resources/Support/IUnknown+Swift.swift +++ b/swiftwinrt/Resources/Support/IUnknown+Swift.swift @@ -4,18 +4,22 @@ import WinSDK extension IUnknown { - public static func from(_ pointer: UnsafeMutableRawPointer?) -> Self? { - guard let pointer = pointer else { return nil } - return Self(pointer) - } - public func QueryInterface() throws -> Interface { var iid = Interface.IID - var pointer: UnsafeMutableRawPointer? - try CHECKED(self.pUnk.borrow.pointee.lpVtbl.pointee.QueryInterface(self.pUnk.borrow, &iid, &pointer)) - // https://learn.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-queryinterface(refiid_void) - // "Upon successful return, *ppvObject (the dereferenced address) contains a pointer to the requested interface" - return Interface(consuming: pointer!.bindMemory(to: C_IUnknown.self, capacity: 1)) + let (pointer) = try ComPtrs.initialize(to: C_IUnknown.self) { abi in + try CHECKED(self.pUnk.borrow.pointee.lpVtbl.pointee.QueryInterface(self.pUnk.borrow, &iid, &abi)) + } + return Interface(pointer!) + } +} + +extension IUnknown { + @_alwaysEmitIntoClient @inline(__always) + public func perform(as type: Type.Type, + _ body: (UnsafeMutablePointer) throws -> ResultType) + throws -> ResultType { + let pThis = UnsafeMutableRawPointer(self.pUnk.borrow).bindMemory(to: Type.self, capacity: 1) + return try body(pThis) } } @@ -27,20 +31,11 @@ extension IUnknown { var clsid = clsid var iid = Interface.IID - var pointer: UnsafeMutableRawPointer? - try CHECKED(CoCreateInstance(&clsid, RawPointer(pUnkOuter), DWORD(dwClsContext.rawValue), &iid, &pointer)) + let (instance) = try ComPtrs.initialize(to: C_IUnknown.self) { instanceAbi in + try CHECKED(CoCreateInstance(&clsid, RawPointer(pUnkOuter), DWORD(dwClsContext.rawValue), &iid, &instanceAbi)) + } // https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cocreateinstance // "Upon successful return, *ppv contains the requested interface pointer." - return Interface(consuming: pointer!.bindMemory(to: C_IUnknown.self, capacity: 1)) + return try instance!.queryInterface() } -} - -extension IUnknown { - @_alwaysEmitIntoClient @inline(__always) - public func perform(as type: Type.Type, - _ body: (UnsafeMutablePointer) throws -> ResultType) - throws -> ResultType { - let pThis = UnsafeMutableRawPointer(self.pUnk.borrow).bindMemory(to: Type.self, capacity: 1) - return try body(pThis) - } -} +} \ No newline at end of file diff --git a/swiftwinrt/Resources/Support/IUnknown.swift b/swiftwinrt/Resources/Support/IUnknown.swift index b9ec2dd9..23a2ee1b 100644 --- a/swiftwinrt/Resources/Support/IUnknown.swift +++ b/swiftwinrt/Resources/Support/IUnknown.swift @@ -11,24 +11,8 @@ open class IUnknown : HasIID { open class var IID: SUPPORT_MODULE.IID { IID_IUnknown } - public required init(_ pointer: UnsafeMutablePointer) { - self.pUnk = IUnknownRef(pointer) - } - - public required init(_ pointer: UnsafeMutableRawPointer) { - let pUnk: UnsafeMutablePointer = - pointer.bindMemory(to: C_IUnknown.self, capacity: 1) - self.pUnk = IUnknownRef(pUnk) - } - - public required init(consuming pointer: UnsafeMutablePointer) { - self.pUnk = IUnknownRef(consuming: pointer) - } - - public init(consuming pointer: UnsafeMutableRawPointer) { - let pUnk: UnsafeMutablePointer = - pointer.bindMemory(to: C_IUnknown.self, capacity: 1) - self.pUnk = IUnknownRef(consuming: pUnk) + public required init(_ pointer: ComPtr) { + self.pUnk = .init(pointer) } @_alwaysEmitIntoClient @inline(__always) diff --git a/swiftwinrt/Resources/Support/IUnknownRef.swift b/swiftwinrt/Resources/Support/IUnknownRef.swift index ff405196..ba3760a0 100644 --- a/swiftwinrt/Resources/Support/IUnknownRef.swift +++ b/swiftwinrt/Resources/Support/IUnknownRef.swift @@ -1,45 +1,21 @@ // Copyright © 2021 Saleem Abdulrasool // SPDX-License-Identifier: BSD-3 -import WinSDK - @_fixed_layout public final class IUnknownRef { - private var pUnk: UnsafeMutablePointer - - init(_ pUnk: UnsafeMutablePointer) { - self.pUnk = pUnk - _ = self.pUnk.pointee.lpVtbl.pointee.AddRef(self.pUnk) - } - - convenience init(_ pointer: UnsafeMutableRawPointer) { - let pUnk = pointer.bindMemory(to: C_IUnknown.self, capacity: 1) - self.init(pUnk) - } - - init(consuming pUnk: UnsafeMutablePointer) { - self.pUnk = pUnk - // TODO: WIN-158 we shouldn't need to addref because these pointers already have an - // added reference coming from winrt. However, this helps the app not crash so - // doing this for now - _ = self.pUnk.pointee.lpVtbl.pointee.AddRef(self.pUnk) - } - - convenience init(consuming pointer: UnsafeMutableRawPointer) { - let pUnk = pointer.bindMemory(to: C_IUnknown.self, capacity: 1) - self.init(consuming: pUnk) - } + private var pUnk: ComPtr - deinit { - _ = self.pUnk.pointee.lpVtbl.pointee.Release(self.pUnk) + init(_ pUnk: ComPtr) { + let pointer: UnsafeMutablePointer = + UnsafeMutableRawPointer(pUnk.get()).bindMemory(to: C_IUnknown.self, capacity: 1) + self.pUnk = .init(pointer) } - public var borrow: UnsafeMutablePointer { - return self.pUnk + func detach() -> UnsafeMutableRawPointer? { + return self.pUnk.detach() } - public var ref: UnsafeMutablePointer { - _ = self.pUnk.pointee.lpVtbl.pointee.AddRef(self.pUnk) - return self.pUnk + public var borrow: UnsafeMutablePointer { + return self.pUnk.get() } } diff --git a/swiftwinrt/Resources/Support/Marshaler.swift b/swiftwinrt/Resources/Support/Marshaler.swift index 7bbe7cf8..7972572d 100644 --- a/swiftwinrt/Resources/Support/Marshaler.swift +++ b/swiftwinrt/Resources/Support/Marshaler.swift @@ -28,9 +28,9 @@ fileprivate enum IMarshalBridge: AbiBridge { return C_IMarshal(lpVtbl: &IMarshalVTable) } - static func from(abi: UnsafeMutablePointer?) -> Marshaler? { + static func from(abi: ComPtr?) -> Marshaler? { guard let abi = abi else { return nil } - return try? Marshaler(IUnknownRef(consuming: abi)) + return try? Marshaler(IUnknownRef(abi)) } typealias CABI = C_IMarshal @@ -45,7 +45,7 @@ private var IMarshalVTable: C_IMarshalVtbl = .init( _ = pUnk.pointee.lpVtbl.pointee.AddRef(pUnk) return S_OK default: - guard let obj = MarshalWrapper.tryUnwrapFrom(raw: pUnk)?.obj else { return E_NOINTERFACE } + guard let obj = MarshalWrapper.tryUnwrapFromBase(raw: pUnk)?.obj else { return E_NOINTERFACE } return obj.copyTo(riid, ppvObject) } }, @@ -84,7 +84,7 @@ private class Marshaler { _ pvDestContext: UnsafeMutableRawPointer?, _ mshlflags: DWORD, _ pCid: UnsafeMutablePointer?) -> HRESULT { - guard let marshaler = MarshalWrapper.tryUnwrapFrom(abi: this)?.marshaler else { return E_FAIL } + guard let marshaler = MarshalWrapper.tryUnwrapFrom(abi: ComPtr(this))?.marshaler else { return E_FAIL } return marshaler.pointee.lpVtbl.pointee.GetUnmarshalClass(marshaler, riid, pv, dwDestContext, pvDestContext, mshlflags, pCid) } @@ -95,7 +95,7 @@ private class Marshaler { _ pvDestContext: UnsafeMutableRawPointer?, _ mshlflags: DWORD, _ pSize: UnsafeMutablePointer?) -> HRESULT { - guard let marshaler = MarshalWrapper.tryUnwrapFrom(abi: this)?.marshaler else { return E_FAIL } + guard let marshaler = MarshalWrapper.tryUnwrapFrom(abi: ComPtr(this))?.marshaler else { return E_FAIL } return marshaler.pointee.lpVtbl.pointee.GetMarshalSizeMax(marshaler, riid, pv, dwDestContext, pvDestContext, mshlflags, pSize) } @@ -107,7 +107,7 @@ private class Marshaler { _ dwDestContext: DWORD, _ pvDestContext: UnsafeMutableRawPointer?, _ mshlflags: DWORD) -> HRESULT { - guard let marshaler = MarshalWrapper.tryUnwrapFrom(abi: this)?.marshaler else { return E_FAIL } + guard let marshaler = MarshalWrapper.tryUnwrapFrom(abi: ComPtr(this))?.marshaler else { return E_FAIL } return marshaler.pointee.lpVtbl.pointee.MarshalInterface(marshaler, pStm, riid, pv, dwDestContext, pvDestContext, mshlflags) } @@ -116,21 +116,21 @@ private class Marshaler { _ pStm: UnsafeMutablePointer?, _ riid: REFIID?, _ ppv: UnsafeMutablePointer?) -> HRESULT { - guard let marshaler = MarshalWrapper.tryUnwrapFrom(abi: this)?.marshaler else { return E_FAIL } + guard let marshaler = MarshalWrapper.tryUnwrapFrom(abi: ComPtr(this))?.marshaler else { return E_FAIL } return marshaler.pointee.lpVtbl.pointee.UnmarshalInterface(marshaler, pStm, riid, ppv) } static func ReleaseMarshalData( _ this: UnsafeMutablePointer?, _ pStm: UnsafeMutablePointer?) -> HRESULT { - guard let marshaler = MarshalWrapper.tryUnwrapFrom(abi: this)?.marshaler else { return E_FAIL } + guard let marshaler = MarshalWrapper.tryUnwrapFrom(abi: ComPtr(this))?.marshaler else { return E_FAIL } return marshaler.pointee.lpVtbl.pointee.ReleaseMarshalData(marshaler, pStm) } static func DisconnectObject( _ this: UnsafeMutablePointer?, _ dwReserved: DWORD) -> HRESULT { - guard let marshaler = MarshalWrapper.tryUnwrapFrom(abi: this)?.marshaler else { return E_FAIL } + guard let marshaler = MarshalWrapper.tryUnwrapFrom(abi: ComPtr(this))?.marshaler else { return E_FAIL } return marshaler.pointee.lpVtbl.pointee.DisconnectObject(marshaler, dwReserved) } } diff --git a/swiftwinrt/Resources/Support/PropertyValue+ABI.swift b/swiftwinrt/Resources/Support/PropertyValue+ABI.swift index d3d70f74..2b4f0f3d 100644 --- a/swiftwinrt/Resources/Support/PropertyValue+ABI.swift +++ b/swiftwinrt/Resources/Support/PropertyValue+ABI.swift @@ -9,154 +9,164 @@ fileprivate var IID___x_ABI_CWindows_CFoundation_CIPropertyValueStatics: SUPPORT internal class IPropertyValueStatics: SUPPORT_MODULE.IInspectable { override public class var IID: SUPPORT_MODULE.IID { IID___x_ABI_CWindows_CFoundation_CIPropertyValueStatics } - internal func CreateUInt8Impl(_ value: UINT8) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateUInt8(pThis, value, &propertyValue)) + internal func CreateUInt8Impl(_ value: UINT8) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateUInt8(pThis, value, &abi)) + } } return propertyValue } - internal func CreateInt16Impl(_ value: INT16) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInt16(pThis, value, &propertyValue)) + internal func CreateInt16Impl(_ value: INT16) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInt16(pThis, value, &abi)) + } } return propertyValue } - internal func CreateUInt16Impl(_ value: UINT16) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateUInt16(pThis, value, &propertyValue)) + internal func CreateUInt16Impl(_ value: UINT16) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateUInt16(pThis, value, &abi)) + } } return propertyValue } - internal func CreateInt32Impl(_ value: INT32) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInt32(pThis, value, &propertyValue)) + internal func CreateInt32Impl(_ value: INT32) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInt32(pThis, value, &abi)) + } } return propertyValue } - internal func CreateUInt32Impl(_ value: UINT32) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateUInt32(pThis, value, &propertyValue)) + internal func CreateUInt32Impl(_ value: UINT32) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateUInt32(pThis, value, &abi)) + } } return propertyValue } - internal func CreateInt64Impl(_ value: INT64) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInt64(pThis, value, &propertyValue)) + internal func CreateInt64Impl(_ value: INT64) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInt64(pThis, value, &abi)) + } } return propertyValue } - internal func CreateUInt64Impl(_ value: UINT64) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateUInt64(pThis, value, &propertyValue)) + internal func CreateUInt64Impl(_ value: UINT64) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateUInt64(pThis, value, &abi)) + } } return propertyValue } - internal func CreateSingleImpl(_ value: FLOAT) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateSingle(pThis, value, &propertyValue)) + internal func CreateSingleImpl(_ value: FLOAT) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateSingle(pThis, value, &abi)) + } } return propertyValue } - internal func CreateDoubleImpl(_ value: DOUBLE) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateDouble(pThis, value, &propertyValue)) + internal func CreateDoubleImpl(_ value: DOUBLE) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateDouble(pThis, value, &abi)) + } } return propertyValue } - internal func CreateChar16Impl(_ value: WCHAR) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateChar16(pThis, value, &propertyValue)) + internal func CreateChar16Impl(_ value: WCHAR) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateChar16(pThis, value, &abi)) + } } return propertyValue } - internal func CreateBooleanImpl(_ value: boolean) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateBoolean(pThis, value, &propertyValue)) + internal func CreateBooleanImpl(_ value: boolean) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateBoolean(pThis, value, &abi)) + } } return propertyValue } - internal func CreateStringImpl(_ value: HSTRING?) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateString(pThis, value, &propertyValue)) + internal func CreateStringImpl(_ value: HSTRING?) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateString(pThis, value, &abi)) + } } return propertyValue } - internal func CreateInspectableImpl(_ value: UnsafeMutablePointer?) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInspectable(pThis, value, &propertyValue)) + internal func CreateGuidImpl(_ value: GUID) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateGuid(pThis, value, &abi)) + } } return propertyValue } - internal func CreateGuidImpl(_ value: GUID) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateGuid(pThis, value, &propertyValue)) + internal func CreateDateTimeImpl(_ value: __x_ABI_CWindows_CFoundation_CDateTime) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateDateTime(pThis, value, &abi)) + } } return propertyValue } - internal func CreateDateTimeImpl(_ value: __x_ABI_CWindows_CFoundation_CDateTime) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateDateTime(pThis, value, &propertyValue)) + internal func CreateTimeSpanImpl(_ value: __x_ABI_CWindows_CFoundation_CTimeSpan) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateTimeSpan(pThis, value, &abi)) + } } return propertyValue } - internal func CreateTimeSpanImpl(_ value: __x_ABI_CWindows_CFoundation_CTimeSpan) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateTimeSpan(pThis, value, &propertyValue)) + internal func CreatePointImpl(_ value: __x_ABI_CWindows_CFoundation_CPoint) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreatePoint(pThis, value, &abi)) + } } return propertyValue } - internal func CreatePointImpl(_ value: __x_ABI_CWindows_CFoundation_CPoint) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreatePoint(pThis, value, &propertyValue)) + internal func CreateSizeImpl(_ value: __x_ABI_CWindows_CFoundation_CSize) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateSize(pThis, value, &abi)) + } } return propertyValue } - internal func CreateSizeImpl(_ value: __x_ABI_CWindows_CFoundation_CSize) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateSize(pThis, value, &propertyValue)) - } - return propertyValue - } - - internal func CreateRectImpl(_ value: __x_ABI_CWindows_CFoundation_CRect) throws -> UnsafeMutablePointer? { - var propertyValue: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateRect(pThis, value, &propertyValue)) + internal func CreateRectImpl(_ value: __x_ABI_CWindows_CFoundation_CRect) throws -> ComPtr? { + let (propertyValue) = try ComPtrs.initialize { abi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIPropertyValueStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateRect(pThis, value, &abi)) + } } return propertyValue } diff --git a/swiftwinrt/Resources/Support/PropertyValue.swift b/swiftwinrt/Resources/Support/PropertyValue.swift index 0991450b..255127ed 100644 --- a/swiftwinrt/Resources/Support/PropertyValue.swift +++ b/swiftwinrt/Resources/Support/PropertyValue.swift @@ -101,17 +101,17 @@ extension PropertyValue return PropertyValue.createInt64(Int64(value)) #elseif arch(i386) || arch(arm) return PropertyValue.createInt32(Int32(value)) - #else + #else fatalError("unknown process architecture size") #endif } - + static func createUInt(_ value: UInt) -> SUPPORT_MODULE.IInspectable { #if arch(x86_64) || arch(arm64) return PropertyValue.createUInt64(UInt64(value)) #elseif arch(i386) || arch(arm) return PropertyValue.createUInt32(UInt32(value)) - #else + #else fatalError("unknown process architecture size") #endif } diff --git a/swiftwinrt/Resources/Support/RawTyped.swift b/swiftwinrt/Resources/Support/RawTyped.swift index 2f8a2d38..79545975 100644 --- a/swiftwinrt/Resources/Support/RawTyped.swift +++ b/swiftwinrt/Resources/Support/RawTyped.swift @@ -7,6 +7,10 @@ public func RawPointer(_ pUnk: T) -> UnsafeMutablePointer { return UnsafeMutableRawPointer(pUnk.pUnk.borrow).bindMemory(to: U.self, capacity: 1) } +public func RawPointer(_ pUnk: T) -> ComPtr { + return ComPtr(UnsafeMutableRawPointer(pUnk.pUnk.borrow).bindMemory(to: U.self, capacity: 1)) +} + public func RawPointer(_ pUnk: T?) -> UnsafeMutablePointer? { guard let pUnk else { return nil } let result: UnsafeMutablePointer = RawPointer(pUnk) diff --git a/swiftwinrt/Resources/Support/Runtime+Swift.swift b/swiftwinrt/Resources/Support/Runtime+Swift.swift index 4d41a8d1..8dc50f59 100644 --- a/swiftwinrt/Resources/Support/Runtime+Swift.swift +++ b/swiftwinrt/Resources/Support/Runtime+Swift.swift @@ -6,17 +6,17 @@ import C_BINDINGS_MODULE public func RoGetActivationFactory(_ activatableClassId: HString) throws -> Factory { var iid = Factory.IID - var factory: UnsafeMutableRawPointer? - try CHECKED(RoGetActivationFactory(activatableClassId.get(), &iid, &factory)) - let inspectable = IInspectable(consuming: factory!.bindMemory(to: C_IUnknown.self, capacity: 1)) - return try inspectable.QueryInterface() + let (factory) = try ComPtrs.initialize(to: C_IInspectable.self) { factoryAbi in + try CHECKED(RoGetActivationFactory(activatableClassId.get(), &iid, &factoryAbi)) + } + return try factory!.queryInterface() } public func RoActivateInstance(_ activatableClassId: HString) throws -> Instance { - var instance: UnsafeMutablePointer? - try CHECKED(RoActivateInstance(activatableClassId.get(), &instance)) - let inspectable = IInspectable(consuming: UnsafeMutableRawPointer(instance!).bindMemory(to: C_IUnknown.self, capacity: 1)) - return try inspectable.QueryInterface() + let (instance) = try ComPtrs.initialize { instanceAbi in + try CHECKED(RoActivateInstance(activatableClassId.get(), &instanceAbi)) + } + return try instance!.queryInterface() } // ISwiftImplemented is a marker interface for code-gen types which are created by swift/winrt. It's used to QI diff --git a/swiftwinrt/Resources/Support/WinRTProtocols.swift b/swiftwinrt/Resources/Support/WinRTProtocols.swift index b4500160..ac6e0a51 100644 --- a/swiftwinrt/Resources/Support/WinRTProtocols.swift +++ b/swiftwinrt/Resources/Support/WinRTProtocols.swift @@ -13,19 +13,48 @@ public protocol IWinRTObject: AnyObject { public protocol WinRTInterface: AnyObject, CustomQueryInterface { } -public protocol WinRTClass : IWinRTObject, CustomQueryInterface, Equatable { +open class WinRTClass : CustomQueryInterface, Equatable { + public init() {} + + @_spi(WinRTInternal) + public init(_ ptr: SUPPORT_MODULE.IInspectable) { + _inner = ptr + } + @_spi(WinRTInternal) - func _getABI() -> UnsafeMutablePointer? + open func _getABI() -> UnsafeMutablePointer? { + if T.self == C_IInspectable.self { + return UnsafeMutableRawPointer(identity?.get())?.bindMemory(to: T.self, capacity: 1) ?? RawPointer(_inner) + } + if T.self == C_IUnknown.self { + return UnsafeMutableRawPointer(identity?.get())?.bindMemory(to: T.self, capacity: 1) ?? RawPointer(_inner) + } + return nil + } + @_spi(WinRTInternal) - var _inner: SUPPORT_MODULE.IInspectable! { get } + public internal(set) var _inner: SUPPORT_MODULE.IInspectable! + + var identity: ComPtr? + + @_spi(WinRTImplements) + open func queryInterface(_ iid: SUPPORT_MODULE.IID) -> IUnknownRef? { + SUPPORT_MODULE.queryInterface(self, iid) + } + + deinit { + // ensure we release the identity pointer before releasing _inner. releasing the _inner + // cleans up the underlying COM object. + identity = nil + _inner = nil + } } -public typealias AnyWinRTClass = any WinRTClass public func ==(_ lhs: T, _ rhs: T) -> Bool { return lhs.thisPtr == rhs.thisPtr } -extension WinRTClass { +extension WinRTClass: IWinRTObject { public var thisPtr: SUPPORT_MODULE.IInspectable { try! _inner.QueryInterface() } } @@ -47,4 +76,20 @@ extension WinRTClass { // need or want the framework to think it's dealing with custom types. return try! _inner.GetRuntimeClassName() } + + fileprivate func aggregated() -> Bool { identity != nil } + + // Get an interface for caching on a class. This method properly handles + // reference counting via releasing the reference added on the Swift object + // in the case of being aggregated. The wrapper still has the +1 ref on it, + // which will be released when the object is destroyed. We can safely let the + // objects be destroyed since _inner is destroyed last. Releasing _inner is what + // cleans up the underlying COM object. + public func getInterfaceForCaching() -> T { + let ptr:T = try! _inner.QueryInterface() + if aggregated() { + Unmanaged.passUnretained(self).release() + } + return ptr + } } diff --git a/swiftwinrt/Resources/Support/WinRTWrapperBase.swift b/swiftwinrt/Resources/Support/WinRTWrapperBase.swift index 2422f0b5..7bfcde29 100644 --- a/swiftwinrt/Resources/Support/WinRTWrapperBase.swift +++ b/swiftwinrt/Resources/Support/WinRTWrapperBase.swift @@ -5,11 +5,6 @@ public protocol Initializable { init() } -public protocol InitializableFromAbi : HasIID { - associatedtype ABI - init?(ref: UnsafeMutablePointer?) -} - public protocol HasIID { static var IID: SUPPORT_MODULE.IID { get } } @@ -20,12 +15,14 @@ public protocol AbiInterface { } // A protocol for defining a type which implements a WinRT interface and defines -// the swift <-> winrt translation +// the swift <-> winrt translation. Note that AbiBridge doesn't depend on the SwiftABI, +// this is because not all conversions between the ABI and Swift have a SwiftABI implementation. +// For example, IReference does not since those types are projected to T? in Swift. public protocol AbiBridge { associatedtype CABI associatedtype SwiftProjection static func makeAbi() -> CABI - static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? + static func from(abi: ComPtr?) -> SwiftProjection? } public protocol ReferenceBridge : AbiBridge, HasIID { @@ -38,25 +35,38 @@ public protocol AbiInterfaceImpl { associatedtype Bridge: AbiInterfaceBridge var _default: Bridge.SwiftABI { get } } + +@_spi(WinRTInternal) +extension AbiInterfaceImpl { + public func getInterfaceForCaching() -> T { + return try! _default.QueryInterface() + } +} + internal typealias AnyAbiInterfaceImpl = any AbiInterfaceImpl public protocol WinRTAbiImpl: AbiInterfaceImpl where Bridge.SwiftABI: IInspectable {} internal typealias AnyWinRTAbiImpl = any WinRTAbiImpl +internal protocol CustomAddRef { + func addRef() + func release() +} + // The WinRTWrapperBase class wraps an AbiBridge and is used for wrapping and unwrapping swift // objects at the ABI layer. The contract for how to do this is defined by the AbiBridge protocol open class WinRTWrapperBase { - public struct ComObject { + public struct ComObjectABI { public var comInterface: CInterface public var wrapper: Unmanaged? } - public var instance: ComObject + public var instance: ComObjectABI public var swiftObj: Prototype! open class var IID: SUPPORT_MODULE.IID { get { fatalError("not implemented") } } public init(_ pointer: CInterface, _ impl: Prototype!) { - self.instance = ComObject(comInterface: pointer) + self.instance = ComObjectABI(comInterface: pointer) self.swiftObj = impl self.instance.wrapper = Unmanaged.passUnretained(self) } @@ -87,62 +97,73 @@ open class WinRTWrapperBase { // Use toABI as derived classes may override this to get the ABI pointer of the swift // object they are holding onto try! toABI { - $0.withMemoryRebound(to: C_IUnknown.self, capacity: 1) { + $0.withMemoryRebound(to: C_IUnknown.self, capacity: 1) { pThis in var iid = iid - var result: UnsafeMutableRawPointer? - guard $0.pointee.lpVtbl.pointee.QueryInterface($0, &iid, &result) == S_OK, let result else { return nil } - return IUnknownRef(consuming: result) + let (ptr) = try? ComPtrs.initialize(to: C_IUnknown.self) { ptrAbi in + try CHECKED(pThis.pointee.lpVtbl.pointee.QueryInterface(pThis, &iid, &ptrAbi)) + } + guard let ptr else { return nil } + return IUnknownRef(ptr) } } } public static func fromRaw(_ pUnk: UnsafeMutableRawPointer?) -> Unmanaged? { guard let pUnk = pUnk else { return nil } - return pUnk.assumingMemoryBound(to: WinRTWrapperBase.ComObject.self).pointee.wrapper + return pUnk.assumingMemoryBound(to: WinRTWrapperBase.ComObjectABI.self).pointee.wrapper } - public static func tryUnwrapFrom(raw pUnk: UnsafeMutableRawPointer?) -> Prototype? { + internal static func tryUnwrapFromBase(raw pUnk: UnsafeMutableRawPointer?) -> Prototype? { guard let pUnk = pUnk else { return nil } return fromRaw(pUnk)?.takeUnretainedValue().swiftObj } // When unwrapping from the abi, we want to see if the object has an existing implementation so we can use // that to get to the existing swift object. if it doesn't exist then we can create a new implementation - public static func tryUnwrapFrom(abi pointer: UnsafeMutablePointer?) -> Prototype? { + public static func tryUnwrapFrom(abi pointer: ComPtr?) -> Prototype? { guard let pointer = pointer else { return nil } - let delegate = IUnknown(pointer) - guard let wrapper: ISwiftImplemented = try? delegate.QueryInterface() else { return nil } + guard let wrapper: ISwiftImplemented = try? pointer.queryInterface() else { return nil } let pUnk = UnsafeMutableRawPointer(wrapper.pUnk.borrow) - // try to get the original wrapper so we can get the apps implementation. if that doesn't - // exist, then return nil - - guard let wrapper = pUnk.bindMemory(to: WinRTWrapperBase.ComObject.self, capacity: 1).pointee.wrapper else { return nil } - return wrapper.takeRetainedValue().swiftObj + // try to get the original wrapper so we can get the apps implementation. if that doesn't + // exist, then return nil + guard let wrapper = pUnk.bindMemory(to: WinRTWrapperBase.ComObjectABI.self, capacity: 1).pointee.wrapper else { return nil } + return wrapper.takeUnretainedValue().swiftObj } public static func addRef(_ pUnk: UnsafeMutablePointer?) -> ULONG { - guard let wrapper = fromRaw(pUnk) else { return 1 } - _ = wrapper.retain() - return ULONG(_getRetainCount(wrapper.takeUnretainedValue())) + guard let unmanaged = fromRaw(pUnk) else { return 1 } + let wrapper = unmanaged.takeUnretainedValue() + _ = unmanaged.retain() + + if let customAddRef = wrapper.swiftObj as? CustomAddRef { + customAddRef.addRef() + } + return ULONG(_getRetainCount(wrapper)) } public static func release(_ pUnk: UnsafeMutablePointer?) -> ULONG { - guard let wrapper = fromRaw(pUnk) else { return 1 } - return ULONG(_getRetainCount(wrapper.takeRetainedValue())) + guard let unmanaged = fromRaw(pUnk) else { return 1 } + let wrapper = unmanaged.takeUnretainedValue() + unmanaged.release() + + if let customAddRef = wrapper.swiftObj as? CustomAddRef { + customAddRef.release() + } + return ULONG(_getRetainCount(wrapper)) } fileprivate static func queryInterfaceBase(_ pUnk: UnsafeMutablePointer, _ riid: UnsafePointer, _ result: UnsafeMutablePointer) -> HRESULT { - guard let instance = tryUnwrapFrom(raw: pUnk) else { return E_FAIL } + guard let instance = tryUnwrapFromBase(raw: pUnk) else { return E_FAIL } do { switch riid.pointee { case IID_IMarshal: - try makeMarshaler(IUnknownRef(pUnk), result) + try makeMarshaler(IUnknownRef(ComPtr(pUnk)), result) default: guard let customQI = instance as? CustomQueryInterface, let iUnknownRef = customQI.queryInterface(riid.pointee) else { return E_NOINTERFACE } - result.pointee = UnsafeMutableRawPointer(iUnknownRef.ref) + result.pointee = iUnknownRef.detach() } return S_OK } catch { @@ -153,7 +174,7 @@ open class WinRTWrapperBase { open class WinRTAbiBridgeWrapper : WinRTWrapperBase { - public static func unwrapFrom(abi pointer: UnsafeMutablePointer?) -> I.SwiftProjection? { + public static func unwrapFrom(abi pointer: ComPtr?) -> I.SwiftProjection? { guard let pointer = pointer else { return nil } guard let unwrapped = tryUnwrapFrom(abi: pointer) else { return I.from(abi: pointer) } return unwrapped @@ -198,6 +219,10 @@ open class InterfaceWrapperBase : WinRTAbiBridgeWrapper I.SwiftProjection? { + tryUnwrapFromBase(raw: pUnk) + } } public class ReferenceWrapperBase: WinRTAbiBridgeWrapper { @@ -217,10 +242,14 @@ public class ReferenceWrapperBase: WinRTAbiBridgeWrapper guard let value = tryUnwrapFrom(raw: pUnk), let wrapper = __ABI_Windows_Foundation.IPropertyValueWrapper(__IMPL_Windows_Foundation.IPropertyValueImpl(value: value)) else { return E_FAIL } guard let iUnk = wrapper.queryInterface(__ABI_Windows_Foundation.IPropertyValueWrapper.IID) else { return E_NOINTERFACE } - ppvObject.pointee = UnsafeMutableRawPointer(iUnk.ref) + ppvObject.pointee = iUnk.detach() return S_OK default: return super.queryInterface(pUnk, riid, ppvObject) } } + + public static func tryUnwrapFrom(raw pUnk: UnsafeMutableRawPointer?) -> I.SwiftProjection? { + tryUnwrapFromBase(raw: pUnk) + } } \ No newline at end of file diff --git a/swiftwinrt/code_writers.h b/swiftwinrt/code_writers.h index 1b33c642..e3b78d16 100644 --- a/swiftwinrt/code_writers.h +++ b/swiftwinrt/code_writers.h @@ -292,7 +292,11 @@ namespace swiftwinrt { auto category = get_category(param.type); bool is_blittable = is_type_blittable(param.signature.Type()); - if (category == param_category::struct_type) + if (needs_wrapper(category)) + { + w.write("&%Abi", local_param_name); + } + else if (category == param_category::struct_type) { if (is_blittable) { @@ -318,21 +322,37 @@ namespace swiftwinrt { s(); auto param_name = function.return_type.value().name; - w.write("&%", param_name); + if (needs_wrapper(get_category(function.return_type->type))) + { + w.write("&%Abi", param_name); + } + else + { + w.write("&%", param_name); + } } } - static void write_init_return_val_abi(writer& w, function_return_type const& signature) + static std::optional write_init_return_val_abi(writer& w, function_return_type const& signature) { auto category = get_category(signature.type); - auto guard{ w.push_mangled_names_if_needed(category) }; - write_type(w, *signature.type, write_type_params::c_abi); - write_default_init_assignment(w, *signature.type, projection_layer::c_abi); + if (needs_wrapper(category)) + { + w.write("let (%) = try ComPtrs.initialize { %Abi in\n", signature.name, signature.name); + return writer::indent_guard(w, 1); + } + else + { + w.write("var %: ", signature.name); + auto guard{ w.push_mangled_names_if_needed(category) }; + write_type(w, *signature.type, write_type_params::c_abi); + write_default_init_assignment(w, *signature.type, projection_layer::c_abi); + w.write("\n"); + return std::optional(); + } } static void write_consume_return_statement(writer& w, function_def const& signature); - static write_scope_guard write_local_param_wrappers(writer& w, function_def const& signature); - static void write_return_type_declaration(writer& w, function_def function, write_type_params const& type_params) { if (!function.return_type) @@ -400,23 +420,26 @@ namespace swiftwinrt returnStatement); { auto function_indent_guard = w.push_indent(); + std::vector initialize_result_indent; if (function.return_type) { - auto return_val = function.return_type.value(); - w.write("var %: %\n", - return_val.name, - bind(return_val)); - } - - if (composableFactory) - { - w.write("var _innerInterface: UnsafeMutablePointer?\n"); - w.write("let _baseInterface = baseInterface?.toIInspectableABI { $0 }\n"); + if (auto result = write_init_return_val_abi(w, function.return_type.value())) + { + initialize_result_indent.push_back(std::move(result.value())); + } } { auto guard = write_local_param_wrappers(w, params); + if (composableFactory) + { + w.write("let _baseInterface = baseInterface?.toIInspectableABI { $0 }\n"); + w.write("let (_innerInterface) = try ComPtrs.initialize { _innerInterfaceAbi in\n"); + guard.push_indent(); + guard.push("}\n"); + guard.push("innerInterface = %.IInspectable(_innerInterface!)\n", w.support); + } w.write(R"(_ = try perform(as: %.self) { pThis in try CHECKED(pThis.pointee.lpVtbl.pointee.%(%)) } @@ -426,17 +449,19 @@ func_name, bind(function)); } + for (auto&& guard : initialize_result_indent) + { + guard.end(); + w.write("}\n"); + } + if (function.return_type && !isInitializer) { w.write("%\n", bind(function)); } else if (isInitializer) { - if (composableFactory) - { - w.write("innerInterface = %.IInspectable(consuming: _innerInterface!)\n", w.support); - } - w.write("return %(consuming: %!)\n", bind_type_abi(classType->default_interface), function.return_type->name); + w.write("return %(%!)\n", bind_type_abi(classType->default_interface), function.return_type->name); } } w.write("}\n\n"); @@ -484,10 +509,10 @@ bind(function)); typealias SwiftProjection = % static var IID: %.IID { IID_% } - static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + static func from(abi: ComPtr?) -> SwiftProjection? { guard let val = abi else { return nil } var result: %% - try! CHECKED(val.pointee.lpVtbl.pointee.get_Value(val, &result)) + try! CHECKED(val.get().pointee.lpVtbl.pointee.get_Value(val.get(), &result)) return % } @@ -503,7 +528,7 @@ bind(function)); type.mangled_name(), c_name, bind(*generic_param, projection_layer::c_abi), - bind(generic_param, "result"), + bind(generic_param, "result", true), type.mangled_name()); } @@ -619,7 +644,7 @@ typealias % = InterfaceWrapperBase<%> std::string from = std::string("swift.").append(get_swift_name(field)); w.write("val.% = %\n", get_abi_name(field), - bind(field.type, from) + bind(field.type, from, false) ); } @@ -660,7 +685,7 @@ typealias % = InterfaceWrapperBase<%> w.write("}\n"); } - static void write_consume_params(writer& w, function_def const& signature) + static void write_convert_vtable_params(writer& w, function_def const& signature) { int param_number = 1; auto full_type_names = w.push_full_type_names(true); @@ -670,7 +695,7 @@ typealias % = InterfaceWrapperBase<%> if (param.signature.Type().is_szarray()) { // TODO: WIN-32 swiftwinrt: add support for arrays - w.write("**TODO: implement szarray in write_consume_params**"); + w.write("**TODO: implement szarray in write_convert_vtable_params**"); } else { @@ -684,14 +709,14 @@ typealias % = InterfaceWrapperBase<%> { w.write("guard let % = % else { return E_INVALIDARG }\n", get_swift_name(param), - bind(param.type, param_name)); + bind(param.type, param_name, false)); } else { w.write("let %: % = %\n", get_swift_name(param), bind(*param.type, write_type_params::swift), - bind(param.type, param_name)); + bind(param.type, param_name, false)); } } else @@ -717,7 +742,7 @@ typealias % = InterfaceWrapperBase<%> auto return_type = signature.return_type.value().type; auto return_param_name = put_in_backticks_if_needed(std::string(signature.return_type.value().name)); - w.write("return %", bind(return_type, return_param_name)); + w.write("return %", bind(return_type, return_param_name, true)); } static void write_consume_args(writer& w, function_def const& function) @@ -824,7 +849,7 @@ bind_bridge_fullname(type)); var _value: Any var propertyType : PropertyType - fileprivate init(_ abi: UnsafeMutablePointer<__x_ABI_CWindows_CFoundation_CIPropertyValue>) { fatalError("not implemented") } + fileprivate init(_ abi: ComPtr<__x_ABI_CWindows_CFoundation_CIPropertyValue>) { fatalError("not implemented") } public init(value: Any) { _value = value if _value is Int32 { @@ -966,11 +991,9 @@ bind_bridge_fullname(type)); auto guard{ w.push_generic_params(info) }; swiftAbi = w.write_temp("%", bind_type_abi(info.type)); } - auto qiFrom = is_class ? "_inner" : "_default"; - w.write("internal lazy var %: % = try! %.QueryInterface()\n", + w.write("private lazy var %: %! = getInterfaceForCaching()\n", get_swift_name(info), - swiftAbi, - qiFrom); + swiftAbi); } if (auto iface = dynamic_cast(info.type)) @@ -1074,7 +1097,7 @@ bind_bridge_fullname(type)); % typealias CABI = % % typealias SwiftABI = % % typealias SwiftProjection = % - % static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + % static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return %(abi) } @@ -1111,7 +1134,7 @@ vtable); fileprivate typealias Bridge = % fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: %.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -1408,7 +1431,7 @@ vtable); % typealias CABI = % % typealias SwiftABI = %.% - % static func from(abi: UnsafeMutablePointer?) -> Handler? { + % static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (%) in @@ -1470,7 +1493,7 @@ vtable); w.write("typealias Bridge = %\n", bind_bridge_name(type)); w.write("let _default: Bridge.SwiftABI\n"); - w.write("init(_ fromAbi: UnsafeMutablePointer) {\n"); + w.write("init(_ fromAbi: ComPtr) {\n"); w.write(" _default = Bridge.SwiftABI(fromAbi)\n"); w.write("}\n\n"); @@ -1521,10 +1544,24 @@ vtable); } } + static void write_param_names(writer& w, std::vector const& params, std::string_view format) + { + separator s{ w }; + for (const auto& param : params) + { + s(); + w.write(format, local_swift_param_name(get_swift_name(param))); + } + } + + // When converting from Swift <-> C we put some local variables on the stack in order to help facilitate + // converting between the two worlds. This method will returns a scope guard which will write any necessary + // code for after the ABI function is called (such as cleaning up references). static write_scope_guard write_local_param_wrappers(writer& w, std::vector const& params) { write_scope_guard guard{ w, w.swift_module }; + std::vector com_ptr_initialize; for (auto& param : params) { TypeDef signature_type{}; @@ -1600,41 +1637,38 @@ vtable); param_name, local_param_name); } - else if (category == param_category::object_type) + else if (needs_wrapper(category)) { - w.write("var %: %\n", - local_param_name, - bind(*param.type, write_type_params::c_abi)); - - guard.push("% = %\n", get_swift_name(param), - bind(param.type, local_param_name)); + com_ptr_initialize.push_back(param); } - else if (category == param_category::generic_type) - { - w.write("var %: %\n", - local_param_name, - bind(*param.type, write_type_params::c_abi)); + } + } - guard.push("% = %.from(abi: %)\n", - param_name, - bind_bridge_fullname(param.type), - local_param_name); - } + // At initial writing, ComPtrs.initialize only has overloads for 5 parameters. If we have more than 5 + // then the generated code won't compile. Rather than check for the number here, just let generated + // code not compile so that we can add the overload to ComPtrs.initialize later on. This would also + // in theory let someone add a new overload to ComPtrs.initialize with a different number of parameters + // on their own as a way to unblock themselves + if (!com_ptr_initialize.empty()) + { + w.write("let (%) = try ComPtrs.initialize { (%) in\n", + bind(com_ptr_initialize, "%"), + bind(com_ptr_initialize, "%Abi")); + guard.push_indent(); + guard.push("}\n"); + + for (const auto& param : com_ptr_initialize) + { + auto param_name = get_swift_name(param); + auto local_param_name = local_swift_param_name(param_name); + guard.push("% = %\n", param_name, + bind(param.type, local_param_name, true)); } } return guard; } - // When converting from Swift <-> C we put some local variables on the stack in order to help facilitate - // converting between the two worlds. This method will returns a scope guard which will write any necessary - // code for after the ABI function is called (such as cleaning up references). - static write_scope_guard write_local_param_wrappers(writer& w, function_def const& signature) - { - return write_local_param_wrappers(w, signature.params); - } - - static void write_factory_body(writer& w, function_def const& method, interface_info const& factory, class_type const& type, metadata_type const& default_interface) { std::string_view func_name = get_abi_name(method); @@ -1651,16 +1685,17 @@ vtable); } else { - w.write("_inner = %\n", func_call); + w.write("super.init(%)\n", func_call); } + } // Check if the type has a default constructor. This is a parameterless constructor - // in Swift. Note that we don't check the args like we do in has_matching_constructor + // in Swift. Note that we don't check the args like we do in base_has_matching_constructor // because composing constructors project as init() when they really have 2 parameters. static bool has_default_constructor(const class_type* type) { - if (type == nullptr) return false; + if (type == nullptr) return true; for (const auto& [_, factory] : type->factories) { @@ -1695,12 +1730,16 @@ vtable); } } - static bool has_matching_constructor(const class_type* type, attributed_type const& factory, function_def const& func) + static bool base_has_matching_constructor(class_type const& type, attributed_type const& factory, function_def const& func) { - if (type == nullptr) return false; auto projectedParams = get_projected_params(factory, func); - for (const auto& [_, baseFactory] : type->factories) + if (type.base_class == nullptr) + { + return projectedParams.size() == 0; + } + + for (const auto& [_, baseFactory] : type.base_class->factories) { // only look at activation or composing constructors if (!baseFactory.activatable && !baseFactory.composable) continue; @@ -1743,7 +1782,7 @@ vtable); { if (!can_write(w, method)) continue; - auto baseHasMatchingConstructor = has_matching_constructor(type.base_class, factory, method); + auto baseHasMatchingConstructor = base_has_matching_constructor(type, factory, method); w.write("%public init(%) {\n", baseHasMatchingConstructor ? "override " : "", bind(method, write_type_params::swift_allow_implicit_unwrap)); @@ -1761,14 +1800,14 @@ vtable); w.write("%public init() {\n", has_default_constructor(base_class) ? "override " : ""); { auto indent = w.push_indent(); - auto activateInstance = w.write_temp("RoActivateInstance(HString(\"%\"))", get_full_type_name(type)); + auto activateInstance = w.write_temp("try! RoActivateInstance(HString(\"%\"))", get_full_type_name(type)); if (base_class) { - w.write("super.init(fromAbi: try! %)\n", activateInstance); + w.write("super.init(fromAbi: %)\n", activateInstance); } else { - w.write("try! _inner = %\n", activateInstance); + w.write("super.init(%)\n", activateInstance); } } w.write("}\n\n"); @@ -1798,7 +1837,8 @@ public init( composing: Composable.Type, _ createCallback: (UnsealedWinRTClassWrapper?, inout %.IInspectable?) -> Composable.Default.SwiftABI) { - self._inner = MakeComposed(composing: composing, (self as! Composable.SwiftProjection), createCallback) + super.init() + MakeComposed(composing: composing, (self as! Composable.Class), createCallback) } )", w.support); } @@ -1819,7 +1859,7 @@ public init( { if (!can_write(w, method)) continue; - auto baseHasMatchingConstructor = has_matching_constructor(type.base_class, factory, method); + auto baseHasMatchingConstructor = base_has_matching_constructor(type, factory, method); std::vector params = get_projected_params(factory, method); @@ -1836,7 +1876,8 @@ public init( } else { - w.write("self._inner = MakeComposed(composing: Self.Composable.self, self) { baseInterface, innerInterface in \n"); + w.write("super.init()\n"); + w.write("MakeComposed(composing: Self.Composable.self, self) { baseInterface, innerInterface in \n"); } w.write(" try! Self.%.%Impl(%)\n", get_swift_name(factory_info), @@ -1858,7 +1899,7 @@ public init( // We unwrap composable types to try and get to any derived type. // If not composable, then create a new instance w.write("@_spi(WinRTInternal)\n"); - w.write("public static func from(abi: UnsafeMutablePointer<%>?) -> %? {\n", + w.write("public static func from(abi: ComPtr<%>?) -> %? {\n", bind_type_mangled(default_interface), type); { auto indent = w.push_indent(); @@ -1869,7 +1910,7 @@ public init( } else { - w.write("return .init(fromAbi: %.IInspectable(consuming: abi))\n", w.support); + w.write("return .init(fromAbi: %.IInspectable(abi))\n", w.support); } } w.write("}\n\n"); @@ -1886,7 +1927,7 @@ public init( } else { - w.write("_inner = fromAbi\n"); + w.write("super.init(fromAbi)\n"); } } w.write("}\n\n"); @@ -1998,16 +2039,25 @@ public init( auto event = def.def; auto format = R"(%var % : Event<%> = { .init( - add: { [weak this = %] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?.% else { return .init() } return try! this.add_%Impl($0) }, - remove: { [weak this = %] in - try? this?.remove_%Impl($0) + remove: { [weak self] in + try? self?.%.remove_%Impl($0) } ) }() +)"; + + auto static_format = R"(%var % : Event<%> = { + .init( + add: { try! %.add_%Impl($0) }, + remove: { try? %.remove_%Impl($0) } + ) +}() + )"; auto type = find_type(event.EventType()); writer::generic_param_guard guard{}; @@ -2028,7 +2078,7 @@ public init( modifier.append("lazy "); } assert(delegate_method.def); - w.write(format, + w.write(iface.attributed ? static_format : format, modifier, // % var get_swift_name(event), // var % def.type, // Event<%> @@ -2112,7 +2162,8 @@ public init( auto format = R"(internal enum % : ComposableImpl { internal typealias CABI = % internal typealias SwiftABI = % - internal typealias SwiftProjection = % + internal typealias Class = % + internal typealias SwiftProjection = WinRTClassWeakReference internal enum Default : AbiInterface { internal typealias CABI = % internal typealias SwiftABI = %.% @@ -2196,7 +2247,9 @@ public init( } else if (default_interface) { - w.write("% class % : WinRTClass", modifier, typeName); + w.write("% class % : WinRTClass", + modifier, + typeName); } else { @@ -2251,13 +2304,6 @@ public init( auto class_indent_guard = w.push_indent(); - if (!base_class && default_interface) - { - w.write(R"(^@_spi(WinRTInternal) -private (set) public var _inner: %.IInspectable! -)", w.support); - } - write_generic_typealiases(w, type); writer::generic_param_guard guard; @@ -2275,57 +2321,35 @@ private (set) public var _inner: %.IInspectable! } auto modifier = composable ? "open" : "public"; - auto override = type.base_class ? "override " : ""; w.write(R"(private typealias SwiftABI = % private typealias CABI = % -private lazy var _default: SwiftABI! = try! _inner.QueryInterface() +private lazy var _default: SwiftABI! = getInterfaceForCaching() ^@_spi(WinRTInternal) -%% func _getABI() -> UnsafeMutablePointer? { +override % func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return % + return super._getABI() } -%% var thisPtr: %.IInspectable { try! _inner.QueryInterface() } - )", swiftAbi, bind_type_mangled(default_interface), - override, - modifier, - base_class ? "super._getABI()" : "nil", - override, - modifier, - w.support); + modifier); write_default_constructor_declarations(w, type, *default_interface); // composable types will always need CustomQueryInterface conformance so that derived types can // override the queryInterface call if (needsCustomQueryInterfaceConformance) { - w.write("%% func queryInterface(_ iid: %.IID) -> IUnknownRef? {\n", override, modifier, w.support); + w.write("override % func queryInterface(_ iid: %.IID) -> IUnknownRef? {\n", modifier, w.support); // A WinRTClass needs CustomQueryInterface conformance when it derives from 1 or more interfaces, // otherwise it won't compile. At the end of the day, the winrt object it's holding onto will appropriately // respond to QueryInterface calls, so call into the default implementation. auto baseComposable = type.base_class && type.base_class->is_composable(); - std::string base_case; - if (base_class) - { - base_case = "super.queryInterface(iid)"; - } - else - { - base_case = w.write_temp("%.queryInterface(self, iid)", w.support); - } + std::string base_case = "super.queryInterface(iid)"; if (overridable_interfaces.empty()) { w.write(" return %\n", base_case); @@ -2363,6 +2387,7 @@ private lazy var _default: SwiftABI! = try! _inner.QueryInterface() bool has_overrides = false; bool has_collection_conformance = false; + std::vector interfaces_to_release; for (const auto& [interface_name, info] : type.required_interfaces) { if (interface_name.empty() || !can_write(w, info.type)) { continue; } @@ -2383,6 +2408,7 @@ private lazy var _default: SwiftABI! = try! _inner.QueryInterface() { continue; } + interfaces_to_release.push_back(get_swift_name(info)); write_interface_impl_members(w, info, /* type_definition: */ type); } @@ -2413,6 +2439,16 @@ private lazy var _default: SwiftABI! = try! _inner.QueryInterface() write_composable_impl(w, type, *default_interface, true); } + if (default_interface) + { + w.write("deinit {\n"); + for (const auto& iface : interfaces_to_release) + { + w.write(" % = nil\n", iface); + } + w.write("}\n"); + } + class_indent_guard.end(); w.write("}\n\n"); } @@ -2647,7 +2683,8 @@ private lazy var _default: SwiftABI! = try! _inner.QueryInterface() auto indent_guard = w.push_indent(); w.write("guard let __unwrapped__instance = %.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG }\n", bind_wrapper_name(type)); - write_consume_params(w, function); + + write_convert_vtable_params(w, function); if (function.return_type) { diff --git a/swiftwinrt/code_writers/common_writers.h b/swiftwinrt/code_writers/common_writers.h index 155f5257..251bf666 100644 --- a/swiftwinrt/code_writers/common_writers.h +++ b/swiftwinrt/code_writers/common_writers.h @@ -263,7 +263,7 @@ namespace swiftwinrt w.write("/// [Open Microsoft documentation](%)\n", doc_url); } - static void write_consume_type(writer& w, metadata_type const* type, std::string_view const& name) + static void write_consume_type(writer& w, metadata_type const* type, std::string_view const& name, bool isOut) { TypeDef signature_type{}; auto category = get_category(type, &signature_type); @@ -273,17 +273,17 @@ namespace swiftwinrt // TODO: WIN-32 swiftwinrt: add support for array types XLANG_ASSERT("**TODO: implement array type in write_consume_return_type"); } - else if (category == param_category::object_type) + else if (needs_wrapper(category)) { + auto ptrVal = isOut ? std::string(name) : w.write_temp("ComPtr(%)", name); if (is_class(type)) { - w.write(".from(abi: %)", name); + w.write(".from(abi: %)", ptrVal); } else { - w.write("%.unwrapFrom(abi: %)", bind_wrapper_fullname(type), name); + w.write("%.unwrapFrom(abi: %)", bind_wrapper_fullname(type), ptrVal); } - } else if (category == param_category::struct_type) { @@ -310,12 +310,6 @@ namespace swiftwinrt auto format = "try! HString(%).detach()"; w.write(format, name); } - else if (category == param_category::generic_type) - { - w.write("%.unwrapFrom(abi: %)", - bind_wrapper_fullname(type), - name); - } else { auto format = ".init(from: %)"; diff --git a/swiftwinrt/code_writers/struct_writers.cpp b/swiftwinrt/code_writers/struct_writers.cpp index b80eaacd..06ac335b 100644 --- a/swiftwinrt/code_writers/struct_writers.cpp +++ b/swiftwinrt/code_writers/struct_writers.cpp @@ -113,7 +113,7 @@ namespace swiftwinrt std::string from = std::string("abi.").append(get_abi_name(field)); w.write("%: %", get_swift_name(field), - bind(field.type, from) + bind(field.type, from, false) ); } } diff --git a/swiftwinrt/helpers.h b/swiftwinrt/helpers.h index 5da1232b..54683404 100644 --- a/swiftwinrt/helpers.h +++ b/swiftwinrt/helpers.h @@ -942,4 +942,19 @@ namespace swiftwinrt { return dynamic_cast(&type) != nullptr; } + + inline bool needs_wrapper(param_category category) + { + return category == param_category::object_type || category == param_category::generic_type; + } + + inline bool is_overridable(metadata_type const& type) + { + if (auto typedefBase = dynamic_cast(&type)) + { + return has_attribute(typedefBase->type(), "Windows.Foundation.Metadata", "OverridableAttribute"); + } + return false; + } + } diff --git a/swiftwinrt/resources.rc b/swiftwinrt/resources.rc index 74433820..0b31f997 100644 --- a/swiftwinrt/resources.rc +++ b/swiftwinrt/resources.rc @@ -3,6 +3,7 @@ Aggregation RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\Aggregation.swift" Array+toVector RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\Array+toVector.swift" COM+Extensions RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\COM+Extensions.swift" +ComPtr RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\ComPtr.swift" CppInteropWorkaround RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\CppInteropWorkaround.swift" CustomQueryInterface RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\CustomQueryInterface.swift" Debugging RESOURCE_TYPE_SWIFT_SUPPORT_FILE "Resources\\Support\\Debugging.swift" diff --git a/swiftwinrt/text_writer.h b/swiftwinrt/text_writer.h index e85dba6e..9dcba16e 100644 --- a/swiftwinrt/text_writer.h +++ b/swiftwinrt/text_writer.h @@ -315,44 +315,6 @@ namespace swiftwinrt std::vector m_first; }; - template - struct write_scope_guard - { - write_scope_guard(writer_base& w, std::string module = "", bool start_on_new_line = true) noexcept : m_writer(w), m_start_on_new_line(start_on_new_line), m_swift_module(module) - { - } - - write_scope_guard(write_scope_guard const&) = delete; - write_scope_guard(write_scope_guard&& rhs) : m_writer(rhs.m_writer), m_lines(std::move(rhs.m_lines)) {} - ~write_scope_guard() noexcept - { - auto on_new_line = m_writer.back() == '\n'; - if (!on_new_line && !m_lines.empty() && m_start_on_new_line) - { - m_writer.write("\n"); - } - for (auto& line : m_lines) - { - m_writer.write(line); - } - } - - template - void push(std::string_view const& value, Args const&... args) - { - T temp_writer; - temp_writer.swift_module = m_swift_module; - m_lines.push_back(temp_writer.write_temp(value, args...)); - } - - private: - writer_base& m_writer; - std::vector m_lines; - bool m_start_on_new_line{}; - std::string m_swift_module; - }; - - template struct indented_writer_base : writer_base { @@ -364,7 +326,6 @@ namespace swiftwinrt } indent_guard(indent_guard&& rhs) noexcept : m_writer(rhs.m_writer), m_offset(rhs.m_offset) { rhs.m_offset = 0; } - //indent_guard& operator=(indent_guard&& rhs) noexcept : m_writer(rhs.m_writer), m_offset(rhs.m_offset) { rhs.m_offset = 0; } ~indent_guard() noexcept { m_writer.m_indent -= m_offset; @@ -460,6 +421,56 @@ namespace swiftwinrt }; + template + struct write_scope_guard + { + using writer_type = indented_writer_base; + write_scope_guard(writer_type& w, std::string module = "", bool start_on_new_line = true) noexcept : m_writer(w), m_start_on_new_line(start_on_new_line), m_swift_module(module) + { + } + + write_scope_guard(write_scope_guard const&) = delete; + write_scope_guard(write_scope_guard&& rhs) : m_writer(rhs.m_writer), m_lines(std::move(rhs.m_lines)) {} + ~write_scope_guard() noexcept + { + if (m_guard.has_value()) + { + m_guard.value().end(); + } + auto on_new_line = m_writer.back() == '\n'; + if (!on_new_line && !m_lines.empty() && m_start_on_new_line) + { + m_writer.write("\n"); + } + for (auto& line : m_lines) + { + m_writer.write(line); + } + } + + template + void push(std::string_view const& value, Args const&... args) + { + T temp_writer; + temp_writer.swift_module = m_swift_module; + m_lines.push_back(temp_writer.write_temp(value, args...)); + } + + void push_indent(indent indent = { 1 }) + { + m_guard.emplace(m_writer.push_indent(indent)); + } + + private: + writer_type& m_writer; + size_t m_offset{}; + std::vector m_lines; + std::optional m_guard; + bool m_start_on_new_line{}; + std::string m_swift_module; + }; + + template auto bind(Args&&... args) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 083fb4c9..9099ee0a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,9 +5,6 @@ if (NOT EXISTS ${CMAKE_TEST_COMPONENT_OUTPUT}) file(MAKE_DIRECTORY ${CMAKE_TEST_COMPONENT_OUTPUT}) endif() -# currently only use codeview since we can't actually build the c++ code with dwarf debug info -set(SWIFT_BUILD_ARGS ${SWIFT_BUILD_ARGS} -Xswiftc -debug-info-format=codeview -Xlinker -debug) - # workaround for swiftc hang: https://linear.app/the-browser-company/issue/WIN-96/fix-swiftc-hanging-on-ci-machines if ($ENV{NUMBER_OF_PROCESSORS} EQUAL 64) set(SWIFT_BUILD_ARGS ${SWIFT_BUILD_ARGS} -j 8) diff --git a/tests/test_app/AggregationTests.swift b/tests/test_app/AggregationTests.swift index 493194e5..8c629e52 100644 --- a/tests/test_app/AggregationTests.swift +++ b/tests/test_app/AggregationTests.swift @@ -6,7 +6,7 @@ import Foundation class AggregationTests : XCTestCase { - public func testAggregation() throws { + public func testAppOverridesFromBase() throws { let derived = Derived() try derived.doTheThing() @@ -17,72 +17,95 @@ class AggregationTests : XCTestCase { print(type(of: b)) try appDerived.doTheThing() - XCTAssertEqual(appDerived.count, 1, "1. count not expected") + XCTAssertEqual(appDerived.count, 1, "count not expected") + } - print("foo"); + public func testAppOverridesFromDerived() throws { let appDerived2 = AppDerived2() - print("foo"); try appDerived2.doTheThing() - print("foo"); - XCTAssertEqual(appDerived2.count, 1, "2. count not expected") + XCTAssertEqual(appDerived2.count, 1, "count not expected") + } - print("foo"); + public func testOverrideMultipleInterface() throws { let appDerived3 = AppDerived3() try appDerived3.doTheThing() - XCTAssertEqual(appDerived3.count, 1, "3. count not expected") - XCTAssertEqual(appDerived3.beforeCount, 1, "4. count not expected") + XCTAssertEqual(appDerived3.count, 1, "count not expected") + XCTAssertEqual(appDerived3.beforeCount, 1, "before count not expected") + } + public func testComposedTypesAsInput() throws { + let appDerived = AppDerived() StaticClass.takeBase(appDerived) - XCTAssertEqual(appDerived.count, 2, "5. count not expected") + XCTAssertEqual(appDerived.count, 1, "derive from base count not expected") + + let appDerived2 = AppDerived2() StaticClass.takeBase(appDerived2) - XCTAssertEqual(appDerived2.count, 2, "6. count not expected") + XCTAssertEqual(appDerived2.count, 1, "derived from derived count not expected") + let appDerived3 = AppDerived3() StaticClass.takeBase(appDerived3) - XCTAssertEqual(appDerived3.count, 2, "7. count not expected") - XCTAssertEqual(appDerived3.beforeCount, 2, "8. count not expected") - + XCTAssertEqual(appDerived3.count, 1, "derived from multiple interfaces count not expected") + XCTAssertEqual(appDerived3.beforeCount, 1, "before count not expected") + } - print("testing unwrapping proper types from return values") + public func testUnwrappingWinRTImplementedComposedType() throws { let classy = Class() - var base_returned = classy.baseProperty! + let base_returned = classy.baseProperty! XCTAssert(type(of: base_returned) == Derived.self) + } - print("testing app derived") + public func testUnwrappingAppImplementedComposedTypeBase() throws { + let classy = Class() + let appDerived = AppDerived() classy.baseProperty = appDerived - base_returned = classy.baseProperty - XCTAssert(type(of: base_returned) == AppDerived.self) + let base_returned = try XCTUnwrap(classy.baseProperty) + XCTAssert(type(of: base_returned) == AppDerived.self, "type doesn't match!") XCTAssertIdentical(base_returned, appDerived) + } - print("testing app derived2") + public func testUnwrappingAppImplementedComposedTypeDerive() throws { + let classy = Class() + let appDerived2 = AppDerived2() classy.baseProperty = appDerived2 - base_returned = classy.baseProperty + let base_returned = try XCTUnwrap(classy.baseProperty) XCTAssert(type(of: base_returned) == AppDerived2.self) XCTAssertIdentical(base_returned, appDerived2) + } - print("testing app derived3") + public func testUnwrappingAppImplementedComposedTypeDerive3() throws { + let classy = Class() + let appDerived3 = AppDerived3() classy.baseProperty = appDerived3 - base_returned = classy.baseProperty + let base_returned = try XCTUnwrap(classy.baseProperty) XCTAssert(type(of: base_returned) == AppDerived3.self) XCTAssertIdentical(base_returned, appDerived3) + } - print("testing app derived no overrides") + // Verifies that a composable type with no override interfaces still works + // as expected + public func testUnwrappingAppImplementedComposedFromBaseNoOverrides() throws { + let classy = Class() let derivedNoOverrides = AppDerivedNoOverrides() classy.baseNoOverridesProperty = derivedNoOverrides - var baseNoOverrides_returned = classy.baseNoOverridesProperty! + let baseNoOverrides_returned = try XCTUnwrap(classy.baseNoOverridesProperty) XCTAssert(type(of: baseNoOverrides_returned) == AppDerivedNoOverrides.self) XCTAssertIdentical(baseNoOverrides_returned, derivedNoOverrides) + } + + public func testUnwrappingAppImplementedComposedFromDerivedNoOverrides() throws { + let classy = Class() let derivedNoOverrides2 = AppDerivedNoOverrides2() classy.baseNoOverridesProperty = derivedNoOverrides2 - baseNoOverrides_returned = classy.baseNoOverridesProperty + let baseNoOverrides_returned = try XCTUnwrap(classy.baseNoOverridesProperty) XCTAssert(type(of: baseNoOverrides_returned) == AppDerivedNoOverrides2.self) XCTAssertIdentical(baseNoOverrides_returned, derivedNoOverrides2) } @@ -112,8 +135,17 @@ class AggregationTests : XCTestCase { var aggregationTests: [XCTestCaseEntry] = [ testCase([ - ("testAggregation", AggregationTests.testAggregation), + ("testAppOverridesFromBase", AggregationTests.testAppOverridesFromBase), + ("testAppOverridesFromDerived", AggregationTests.testAppOverridesFromDerived), + ("testOverrideMultipleInterface", AggregationTests.testOverrideMultipleInterface), + ("testComposedTypesAsInput", AggregationTests.testComposedTypesAsInput), + ("testUnwrappingWinRTImplementedComposedType", AggregationTests.testUnwrappingWinRTImplementedComposedType), + ("testUnwrappingAppImplementedComposedTypeBase", AggregationTests.testUnwrappingAppImplementedComposedTypeBase), + ("testUnwrappingAppImplementedComposedTypeDerive", AggregationTests.testUnwrappingAppImplementedComposedTypeDerive), + ("testUnwrappingAppImplementedComposedTypeDerive3", AggregationTests.testUnwrappingAppImplementedComposedTypeDerive3), + ("testUnwrappingAppImplementedComposedFromBaseNoOverrides", AggregationTests.testUnwrappingAppImplementedComposedFromBaseNoOverrides), + ("testUnwrappingAppImplementedComposedFromDerivedNoOverrides", AggregationTests.testUnwrappingAppImplementedComposedFromDerivedNoOverrides), ("testCustomConstructorOnUnsealedType", AggregationTests.testCustomConstructorOnUnsealedType), - ("testGetRuntimeClassNameReturnsBase", AggregationTests.testGetRuntimeClassNameReturnsBase) + ("testGetRuntimeClassNameReturnsBase", AggregationTests.testGetRuntimeClassNameReturnsBase), ]) ] diff --git a/tests/test_app/MemoryManagementTests.swift b/tests/test_app/MemoryManagementTests.swift new file mode 100644 index 00000000..cefb306a --- /dev/null +++ b/tests/test_app/MemoryManagementTests.swift @@ -0,0 +1,63 @@ +import XCTest +import test_component + +class MemoryManagementTests : XCTestCase { + + public func testAggregatedObject() throws { + weak var weakDerived: AppDerived? = nil + try { + let appDerived = AppDerived() + // invoke a method so that _inner object is QI'ed + try appDerived.doTheThing() + weakDerived = appDerived + }() + XCTAssertNil(weakDerived) + } + + public func testReturningAggregatedObject() throws { + weak var weakDerived: AppDerived? = nil + try { + let classy = Class() + let appDerived = AppDerived() + weakDerived = appDerived + classy.baseProperty = appDerived + + let base_returned = try XCTUnwrap(classy.baseProperty) + XCTAssertNotNil(base_returned) + }() + XCTAssertNil(weakDerived) + } + + public func testNonAggregatedObject() throws { + weak var weakDerived: Derived? = nil + try { + let derived = Derived() + // invoke a method so that _inner object is QI'ed + try derived.doTheThing() + weakDerived = derived + }() + XCTAssertNil(weakDerived) + } + public func testReturningNonAggregatedObject() throws { + weak var weakDerived: Derived? = nil + try { + let classy = Class() + let derived = Derived() + weakDerived = derived + classy.baseProperty = derived + + let base_returned = try XCTUnwrap(classy.baseProperty) + XCTAssertNotNil(base_returned) + }() + XCTAssertNil(weakDerived) + } +} + +var memoryManagementTests: [XCTestCaseEntry] = [ + testCase([ + ("testAggregatedObject", MemoryManagementTests.testAggregatedObject), + ("testNonAggregatedObject", MemoryManagementTests.testNonAggregatedObject), + ("testReturningAggregatedObject", MemoryManagementTests.testReturningAggregatedObject), + ("testReturningNonAggregatedObject", MemoryManagementTests.testReturningNonAggregatedObject), + ]) +] diff --git a/tests/test_app/main.swift b/tests/test_app/main.swift index 51cb2a2d..fa3782cc 100644 --- a/tests/test_app/main.swift +++ b/tests/test_app/main.swift @@ -433,7 +433,7 @@ var tests: [XCTestCaseEntry] = [ ("testUnicode", SwiftWinRTTests.testUnicode), ("testErrorInfo", SwiftWinRTTests.testErrorInfo), ]) -] + valueBoxingTests + eventTests + collectionTests + aggregationTests + asyncTests +] + valueBoxingTests + eventTests + collectionTests + aggregationTests + asyncTests + memoryManagementTests RoInitialize(RO_INIT_MULTITHREADED) XCTMain(tests) diff --git a/tests/test_component/Sources/test_component/Support/MAKEFROMABI.swift b/tests/test_component/Sources/test_component/Support/MAKEFROMABI.swift deleted file mode 100644 index 96b2e1c0..00000000 --- a/tests/test_component/Sources/test_component/Support/MAKEFROMABI.swift +++ /dev/null @@ -1,31 +0,0 @@ -import Foundation - -// Not strongly typed, we lose the type safety of the associatedtype anyways -// when we cast to `any MakeFromAbi`, plus that requires a lot more exported -// simples than we want -public protocol MakeFromAbi { - static func from(typeName: String, abi: test_component.IInspectable) -> Any? -} - -func make(typeName: SwiftTypeName, from abi: test_component.IInspectable) -> Any? { - guard let makerType = NSClassFromString("\(typeName.module).__MakeFromAbi") as? any MakeFromAbi.Type else { - return nil - } - return makerType.from(typeName: typeName.typeName, abi: abi) -} - -func makeFrom(abi: test_component.IInspectable) -> Any? { - // When creating a swift class which represents this type, we want to get the class name that we're trying to create - // via GetRuntimeClassName so that we can create the proper derived type. For example, the API may return UIElement, - // but we want to return a Button type. - - // Note that we'll *never* be trying to create an app implemented object at this point - guard let className = try? abi.GetSwiftTypeName() else { return nil } - - return make(typeName: className, from: abi) -} - -func make(type: T.Type, from abi: test_component.IInspectable) -> T? { - let classString = NSStringFromClass(type).split(separator: ".", maxSplits: 2) - return make(typeName: SwiftTypeName(module: String(classString[0]), typeName: String(classString[1])), from: abi) as? T -} diff --git a/tests/test_component/Sources/test_component/Windows.Foundation+ABI.swift b/tests/test_component/Sources/test_component/Windows.Foundation+ABI.swift index 5c326370..282ac67d 100644 --- a/tests/test_component/Sources/test_component/Windows.Foundation+ABI.swift +++ b/tests/test_component/Sources/test_component/Windows.Foundation+ABI.swift @@ -83,9 +83,10 @@ public enum __ABI_Windows_Foundation { } open func get_CompletedImpl() throws -> test_component.AsyncActionCompletedHandler? { - var handler: UnsafeMutablePointer<__x_ABI_CWindows_CFoundation_CIAsyncActionCompletedHandler>? - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIAsyncAction.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Completed(pThis, &handler)) + let (handler) = try ComPtrs.initialize { handlerAbi in + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIAsyncAction.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Completed(pThis, &handlerAbi)) + } } return __ABI_Windows_Foundation.AsyncActionCompletedHandlerWrapper.unwrapFrom(abi: handler) } @@ -129,7 +130,7 @@ public enum __ABI_Windows_Foundation { put_Completed: { guard let __unwrapped__instance = IAsyncActionWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - guard let handler = __ABI_Windows_Foundation.AsyncActionCompletedHandlerWrapper.unwrapFrom(abi: $1) else { return E_INVALIDARG } + guard let handler = __ABI_Windows_Foundation.AsyncActionCompletedHandlerWrapper.unwrapFrom(abi: ComPtr($1)) else { return E_INVALIDARG } __unwrapped__instance.completed = handler return S_OK }, @@ -324,13 +325,14 @@ public enum __ABI_Windows_Foundation { override public class var IID: test_component.IID { IID___x_ABI_CWindows_CFoundation_CIDeferralFactory } internal func CreateImpl(_ handler: test_component.DeferralCompletedHandler?) throws -> IDeferral { - var result: UnsafeMutablePointer<__x_ABI_CWindows_CFoundation_CIDeferral>? - let handlerWrapper = __ABI_Windows_Foundation.DeferralCompletedHandlerWrapper(handler) - let _handler = try! handlerWrapper?.toABI { $0 } - _ = try perform(as: __x_ABI_CWindows_CFoundation_CIDeferralFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.Create(pThis, _handler, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + let handlerWrapper = __ABI_Windows_Foundation.DeferralCompletedHandlerWrapper(handler) + let _handler = try! handlerWrapper?.toABI { $0 } + _ = try perform(as: __x_ABI_CWindows_CFoundation_CIDeferralFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.Create(pThis, _handler, &resultAbi)) + } } - return IDeferral(consuming: result!) + return IDeferral(result!) } } @@ -845,7 +847,7 @@ extension __ABI_Windows_Foundation { Release: { AsyncActionCompletedHandlerWrapper.release($0) }, Invoke: { guard let __unwrapped__instance = AsyncActionCompletedHandlerWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let asyncInfo: test_component.AnyIAsyncAction? = __ABI_Windows_Foundation.IAsyncActionWrapper.unwrapFrom(abi: $1) + let asyncInfo: test_component.AnyIAsyncAction? = __ABI_Windows_Foundation.IAsyncActionWrapper.unwrapFrom(abi: ComPtr($1)) let asyncStatus: test_component.AsyncStatus = $2 __unwrapped__instance(asyncInfo, asyncStatus) return S_OK diff --git a/tests/test_component/Sources/test_component/Windows.Foundation+Impl.swift b/tests/test_component/Sources/test_component/Windows.Foundation+Impl.swift index 0b22a91d..a4efd2ee 100644 --- a/tests/test_component/Sources/test_component/Windows.Foundation+Impl.swift +++ b/tests/test_component/Sources/test_component/Windows.Foundation+Impl.swift @@ -7,7 +7,7 @@ public enum __IMPL_Windows_Foundation { public typealias CABI = __x_ABI_CWindows_CFoundation_CIAsyncAction public typealias SwiftABI = __ABI_Windows_Foundation.IAsyncAction public typealias SwiftProjection = AnyIAsyncAction - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return IAsyncActionImpl(abi) } @@ -22,7 +22,7 @@ public enum __IMPL_Windows_Foundation { fileprivate typealias Bridge = IAsyncActionBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -37,7 +37,7 @@ public enum __IMPL_Windows_Foundation { set { try! _default.put_CompletedImpl(newValue) } } - internal lazy var _IAsyncInfo: __ABI_Windows_Foundation.IAsyncInfo = try! _default.QueryInterface() + private lazy var _IAsyncInfo: __ABI_Windows_Foundation.IAsyncInfo! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.iasyncaction.cancel) fileprivate func cancel() throws { try _IAsyncInfo.CancelImpl() @@ -69,7 +69,7 @@ public enum __IMPL_Windows_Foundation { public typealias CABI = __x_ABI_CWindows_CFoundation_CIAsyncInfo public typealias SwiftABI = __ABI_Windows_Foundation.IAsyncInfo public typealias SwiftProjection = AnyIAsyncInfo - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return IAsyncInfoImpl(abi) } @@ -84,7 +84,7 @@ public enum __IMPL_Windows_Foundation { fileprivate typealias Bridge = IAsyncInfoBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -119,7 +119,7 @@ public enum __IMPL_Windows_Foundation { public typealias CABI = __x_ABI_CWindows_CFoundation_CIClosable public typealias SwiftABI = __ABI_Windows_Foundation.IClosable public typealias SwiftProjection = AnyIClosable - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return IClosableImpl(abi) } @@ -134,7 +134,7 @@ public enum __IMPL_Windows_Foundation { fileprivate typealias Bridge = IClosableBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -149,7 +149,7 @@ public enum __IMPL_Windows_Foundation { public typealias CABI = __x_ABI_CWindows_CFoundation_CIPropertyValue public typealias SwiftABI = __ABI_Windows_Foundation.IPropertyValue public typealias SwiftProjection = AnyIPropertyValue - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return IPropertyValueImpl(abi) } @@ -165,7 +165,7 @@ public enum __IMPL_Windows_Foundation { var _value: Any var propertyType : PropertyType - fileprivate init(_ abi: UnsafeMutablePointer<__x_ABI_CWindows_CFoundation_CIPropertyValue>) { fatalError("not implemented") } + fileprivate init(_ abi: ComPtr<__x_ABI_CWindows_CFoundation_CIPropertyValue>) { fatalError("not implemented") } public init(value: Any) { _value = value if _value is Int32 { @@ -242,7 +242,7 @@ public enum __IMPL_Windows_Foundation { public typealias CABI = __x_ABI_CWindows_CFoundation_CIStringable public typealias SwiftABI = __ABI_Windows_Foundation.IStringable public typealias SwiftProjection = AnyIStringable - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return IStringableImpl(abi) } @@ -257,7 +257,7 @@ public enum __IMPL_Windows_Foundation { fileprivate typealias Bridge = IStringableBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -273,7 +273,7 @@ public enum __IMPL_Windows_Foundation { public typealias CABI = __x_ABI_CWindows_CFoundation_CIAsyncActionCompletedHandler public typealias SwiftABI = __ABI_Windows_Foundation.AsyncActionCompletedHandler - public static func from(abi: UnsafeMutablePointer?) -> Handler? { + public static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (asyncInfo, asyncStatus) in @@ -287,7 +287,7 @@ public enum __IMPL_Windows_Foundation { public typealias CABI = __x_ABI_CWindows_CFoundation_CIDeferralCompletedHandler public typealias SwiftABI = __ABI_Windows_Foundation.DeferralCompletedHandler - public static func from(abi: UnsafeMutablePointer?) -> Handler? { + public static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { () in diff --git a/tests/test_component/Sources/test_component/Windows.Foundation.Collections+Impl.swift b/tests/test_component/Sources/test_component/Windows.Foundation.Collections+Impl.swift index 82de192b..69559c4e 100644 --- a/tests/test_component/Sources/test_component/Windows.Foundation.Collections+Impl.swift +++ b/tests/test_component/Sources/test_component/Windows.Foundation.Collections+Impl.swift @@ -7,7 +7,7 @@ public enum __IMPL_Windows_Foundation_Collections { public typealias CABI = __x_ABI_CWindows_CFoundation_CCollections_CIPropertySet public typealias SwiftABI = __ABI_Windows_Foundation_Collections.IPropertySet public typealias SwiftProjection = AnyIPropertySet - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return IPropertySetImpl(abi) } @@ -22,28 +22,28 @@ public enum __IMPL_Windows_Foundation_Collections { fileprivate typealias Bridge = IPropertySetBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } public typealias K = String public typealias V = Any? public typealias T = AnyIKeyValuePair? - internal lazy var _IObservableMap: IObservableMapString_Any = try! _default.QueryInterface() + private lazy var _IObservableMap: IObservableMapString_Any! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.ipropertyset.mapchanged) fileprivate lazy var mapChanged : Event> = { .init( - add: { [weak this = _IObservableMap] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._IObservableMap else { return .init() } return try! this.add_MapChangedImpl($0) }, - remove: { [weak this = _IObservableMap] in - try? this?.remove_MapChangedImpl($0) + remove: { [weak self] in + try? self?._IObservableMap.remove_MapChangedImpl($0) } ) }() - internal lazy var _IMap: IMapString_Any = try! _default.QueryInterface() + private lazy var _IMap: IMapString_Any! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.ipropertyset.lookup) fileprivate func lookup(_ key: String) -> Any? { try! _IMap.LookupImpl(key) @@ -79,7 +79,7 @@ public enum __IMPL_Windows_Foundation_Collections { get { try! _IMap.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIKeyValuePairString_Any = try! _default.QueryInterface() + private lazy var _IIterable: IIterableIKeyValuePairString_Any! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.ipropertyset.first) fileprivate func first() -> AnyIIterator?>? { try! _IIterable.FirstImpl() @@ -91,7 +91,7 @@ public enum __IMPL_Windows_Foundation_Collections { public typealias CABI = __x_ABI_CWindows_CFoundation_CCollections_CIVectorChangedEventArgs public typealias SwiftABI = __ABI_Windows_Foundation_Collections.IVectorChangedEventArgs public typealias SwiftProjection = AnyIVectorChangedEventArgs - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return IVectorChangedEventArgsImpl(abi) } @@ -106,7 +106,7 @@ public enum __IMPL_Windows_Foundation_Collections { fileprivate typealias Bridge = IVectorChangedEventArgsBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } diff --git a/tests/test_component/Sources/test_component/Windows.Foundation.Collections.swift b/tests/test_component/Sources/test_component/Windows.Foundation.Collections.swift index a053d7c6..9e44cd63 100644 --- a/tests/test_component/Sources/test_component/Windows.Foundation.Collections.swift +++ b/tests/test_component/Sources/test_component/Windows.Foundation.Collections.swift @@ -6,63 +6,53 @@ import Ctest_component public typealias CollectionChange = __x_ABI_CWindows_CFoundation_CCollections_CCollectionChange /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.propertyset) public final class PropertySet : WinRTClass, IObservableMap, IMap, IIterable, IPropertySet { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! public typealias K = String public typealias V = Any? public typealias T = AnyIKeyValuePair? private typealias SwiftABI = __ABI_Windows_Foundation_Collections.IPropertySet private typealias CABI = __x_ABI_CWindows_CFoundation_CCollections_CIPropertySet - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_CWindows_CFoundation_CCollections_CIPropertySet>?) -> PropertySet? { + public static func from(abi: ComPtr<__x_ABI_CWindows_CFoundation_CCollections_CIPropertySet>?) -> PropertySet? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } - public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { - return test_component.queryInterface(self, iid) + override public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + return super.queryInterface(iid) } - public init() { - try! _inner = RoActivateInstance(HString("Windows.Foundation.Collections.PropertySet")) + override public init() { + super.init(try! RoActivateInstance(HString("Windows.Foundation.Collections.PropertySet"))) } - internal lazy var _IObservableMap: IObservableMapString_Any = try! _inner.QueryInterface() + private lazy var _IObservableMap: IObservableMapString_Any! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.propertyset.mapchanged) public lazy var mapChanged : Event> = { .init( - add: { [weak this = _IObservableMap] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._IObservableMap else { return .init() } return try! this.add_MapChangedImpl($0) }, - remove: { [weak this = _IObservableMap] in - try? this?.remove_MapChangedImpl($0) + remove: { [weak self] in + try? self?._IObservableMap.remove_MapChangedImpl($0) } ) }() - internal lazy var _IMap: IMapString_Any = try! _inner.QueryInterface() + private lazy var _IMap: IMapString_Any! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.propertyset.lookup) public func lookup(_ key: String) -> Any? { try! _IMap.LookupImpl(key) @@ -98,56 +88,52 @@ public final class PropertySet : WinRTClass, IObservableMap, IMap, IIterable, IP get { try! _IMap.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIKeyValuePairString_Any = try! _inner.QueryInterface() + private lazy var _IIterable: IIterableIKeyValuePairString_Any! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.propertyset.first) public func first() -> AnyIIterator?>? { try! _IIterable.FirstImpl() } + deinit { + _IObservableMap = nil + _IMap = nil + _IIterable = nil + _default = nil + } } /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.stringmap) public final class StringMap : WinRTClass, IMap, IIterable, IObservableMap { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! public typealias K = String public typealias V = String public typealias T = AnyIKeyValuePair? private typealias SwiftABI = IMapString_String private typealias CABI = __x_ABI_C__FIMap_2_HSTRING_HSTRING - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_C__FIMap_2_HSTRING_HSTRING>?) -> StringMap? { + public static func from(abi: ComPtr<__x_ABI_C__FIMap_2_HSTRING_HSTRING>?) -> StringMap? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } - public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { - return test_component.queryInterface(self, iid) + override public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + return super.queryInterface(iid) } - public init() { - try! _inner = RoActivateInstance(HString("Windows.Foundation.Collections.StringMap")) + override public init() { + super.init(try! RoActivateInstance(HString("Windows.Foundation.Collections.StringMap"))) } /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.stringmap.lookup) @@ -185,87 +171,82 @@ public final class StringMap : WinRTClass, IMap, IIterable, IObservableMap { get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIKeyValuePairString_String = try! _inner.QueryInterface() + private lazy var _IIterable: IIterableIKeyValuePairString_String! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.stringmap.first) public func first() -> AnyIIterator?>? { try! _IIterable.FirstImpl() } - internal lazy var _IObservableMap: IObservableMapString_String = try! _inner.QueryInterface() + private lazy var _IObservableMap: IObservableMapString_String! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.stringmap.mapchanged) public lazy var mapChanged : Event> = { .init( - add: { [weak this = _IObservableMap] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._IObservableMap else { return .init() } return try! this.add_MapChangedImpl($0) }, - remove: { [weak this = _IObservableMap] in - try? this?.remove_MapChangedImpl($0) + remove: { [weak self] in + try? self?._IObservableMap.remove_MapChangedImpl($0) } ) }() + deinit { + _default = nil + _IIterable = nil + _IObservableMap = nil + } } /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.valueset) public final class ValueSet : WinRTClass, IObservableMap, IMap, IIterable, IPropertySet { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! public typealias K = String public typealias V = Any? public typealias T = AnyIKeyValuePair? private typealias SwiftABI = __ABI_Windows_Foundation_Collections.IPropertySet private typealias CABI = __x_ABI_CWindows_CFoundation_CCollections_CIPropertySet - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_CWindows_CFoundation_CCollections_CIPropertySet>?) -> ValueSet? { + public static func from(abi: ComPtr<__x_ABI_CWindows_CFoundation_CCollections_CIPropertySet>?) -> ValueSet? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } - public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { - return test_component.queryInterface(self, iid) + override public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + return super.queryInterface(iid) } - public init() { - try! _inner = RoActivateInstance(HString("Windows.Foundation.Collections.ValueSet")) + override public init() { + super.init(try! RoActivateInstance(HString("Windows.Foundation.Collections.ValueSet"))) } - internal lazy var _IObservableMap: IObservableMapString_Any = try! _inner.QueryInterface() + private lazy var _IObservableMap: IObservableMapString_Any! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.valueset.mapchanged) public lazy var mapChanged : Event> = { .init( - add: { [weak this = _IObservableMap] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._IObservableMap else { return .init() } return try! this.add_MapChangedImpl($0) }, - remove: { [weak this = _IObservableMap] in - try? this?.remove_MapChangedImpl($0) + remove: { [weak self] in + try? self?._IObservableMap.remove_MapChangedImpl($0) } ) }() - internal lazy var _IMap: IMapString_Any = try! _inner.QueryInterface() + private lazy var _IMap: IMapString_Any! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.valueset.lookup) public func lookup(_ key: String) -> Any? { try! _IMap.LookupImpl(key) @@ -301,12 +282,18 @@ public final class ValueSet : WinRTClass, IObservableMap, IMap, IIterable, IProp get { try! _IMap.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIKeyValuePairString_Any = try! _inner.QueryInterface() + private lazy var _IIterable: IIterableIKeyValuePairString_Any! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.valueset.first) public func first() -> AnyIIterator?>? { try! _IIterable.FirstImpl() } + deinit { + _IObservableMap = nil + _IMap = nil + _IIterable = nil + _default = nil + } } public typealias MapChangedEventHandler = (AnyIObservableMap?, AnyIMapChangedEventArgs?) -> () diff --git a/tests/test_component/Sources/test_component/Windows.Foundation.swift b/tests/test_component/Sources/test_component/Windows.Foundation.swift index 2854f06b..c50b2548 100644 --- a/tests/test_component/Sources/test_component/Windows.Foundation.swift +++ b/tests/test_component/Sources/test_component/Windows.Foundation.swift @@ -8,47 +8,37 @@ public typealias AsyncStatus = __x_ABI_CWindows_CFoundation_CAsyncStatus public typealias PropertyType = __x_ABI_CWindows_CFoundation_CPropertyType /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.deferral) public final class Deferral : WinRTClass, IClosable { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! private typealias SwiftABI = __ABI_Windows_Foundation.IDeferral private typealias CABI = __x_ABI_CWindows_CFoundation_CIDeferral - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_CWindows_CFoundation_CIDeferral>?) -> Deferral? { + public static func from(abi: ComPtr<__x_ABI_CWindows_CFoundation_CIDeferral>?) -> Deferral? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } - public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { - return test_component.queryInterface(self, iid) + override public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + return super.queryInterface(iid) } private static let _IDeferralFactory: __ABI_Windows_Foundation.IDeferralFactory = try! RoGetActivationFactory(HString("Windows.Foundation.Deferral")) public init(_ handler: DeferralCompletedHandler!) { - _inner = try! Self._IDeferralFactory.CreateImpl(handler) + super.init(try! Self._IDeferralFactory.CreateImpl(handler)) } - internal lazy var _IClosable: __ABI_Windows_Foundation.IClosable = try! _inner.QueryInterface() + private lazy var _IClosable: __ABI_Windows_Foundation.IClosable! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.deferral.close) public func close() throws { try _IClosable.CloseImpl() @@ -59,6 +49,10 @@ public final class Deferral : WinRTClass, IClosable { try _default.CompleteImpl() } + deinit { + _IClosable = nil + _default = nil + } } public typealias AsyncActionCompletedHandler = (AnyIAsyncAction?, AsyncStatus) -> () diff --git a/tests/test_component/Sources/test_component/test_component+ABI.swift b/tests/test_component/Sources/test_component/test_component+ABI.swift index e8612739..07c19cce 100644 --- a/tests/test_component/Sources/test_component/test_component+ABI.swift +++ b/tests/test_component/Sources/test_component/test_component+ABI.swift @@ -191,25 +191,28 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIAsyncMethodsStatics } internal func GetCompletedAsyncImpl(_ result: Int32) throws -> test_component.AnyIAsyncOperation? { - var operation: UnsafeMutablePointer<__x_ABI_C__FIAsyncOperation_1_int>? - _ = try perform(as: __x_ABI_Ctest__component_CIAsyncMethodsStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetCompletedAsync(pThis, result, &operation)) + let (operation) = try ComPtrs.initialize { operationAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIAsyncMethodsStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetCompletedAsync(pThis, result, &operationAbi)) + } } return test_component.__x_ABI_C__FIAsyncOperation_1_intWrapper.unwrapFrom(abi: operation) } internal func GetCompletedWithErrorAsyncImpl(_ errorCode: HRESULT) throws -> test_component.AnyIAsyncOperation? { - var operation: UnsafeMutablePointer<__x_ABI_C__FIAsyncOperation_1_int>? - _ = try perform(as: __x_ABI_Ctest__component_CIAsyncMethodsStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetCompletedWithErrorAsync(pThis, errorCode, &operation)) + let (operation) = try ComPtrs.initialize { operationAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIAsyncMethodsStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetCompletedWithErrorAsync(pThis, errorCode, &operationAbi)) + } } return test_component.__x_ABI_C__FIAsyncOperation_1_intWrapper.unwrapFrom(abi: operation) } internal func GetPendingAsyncImpl() throws -> test_component.AsyncOperationInt? { - var result: UnsafeMutablePointer<__x_ABI_Ctest__component_CIAsyncOperationInt>? - _ = try perform(as: __x_ABI_Ctest__component_CIAsyncMethodsStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetPendingAsync(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIAsyncMethodsStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetPendingAsync(pThis, &resultAbi)) + } } return .from(abi: result) } @@ -220,9 +223,10 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIAsyncMethodsWithProgress } open func OperationWithProgressImpl(_ value: test_component.DateTime) throws -> test_component.AnyIAsyncOperationWithProgress? { - var operation: UnsafeMutablePointer<__x_ABI_C__FIAsyncOperationWithProgress_2_int_double>? - _ = try perform(as: __x_ABI_Ctest__component_CIAsyncMethodsWithProgress.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.OperationWithProgress(pThis, .from(swift: value), &operation)) + let (operation) = try ComPtrs.initialize { operationAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIAsyncMethodsWithProgress.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.OperationWithProgress(pThis, .from(swift: value), &operationAbi)) + } } return test_component.__x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleWrapper.unwrapFrom(abi: operation) } @@ -355,14 +359,16 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIBaseNoOverridesProtectedFactory } internal func CreateInstanceImpl(_ baseInterface: UnsealedWinRTClassWrapper?, _ innerInterface: inout test_component.IInspectable?) throws -> IBaseNoOverrides { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBaseNoOverrides>? - var _innerInterface: UnsafeMutablePointer? - let _baseInterface = baseInterface?.toIInspectableABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIBaseNoOverridesProtectedFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _baseInterface, &_innerInterface, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let _baseInterface = baseInterface?.toIInspectableABI { $0 } + let (_innerInterface) = try ComPtrs.initialize { _innerInterfaceAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIBaseNoOverridesProtectedFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _baseInterface, &_innerInterfaceAbi, &valueAbi)) + } + } + innerInterface = test_component.IInspectable(_innerInterface!) } - innerInterface = test_component.IInspectable(consuming: _innerInterface!) - return IBaseNoOverrides(consuming: value!) + return IBaseNoOverrides(value!) } } @@ -382,14 +388,16 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIBaseProtectedFactory } internal func CreateInstanceImpl(_ baseInterface: UnsealedWinRTClassWrapper?, _ innerInterface: inout test_component.IInspectable?) throws -> IBase { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBase>? - var _innerInterface: UnsafeMutablePointer? - let _baseInterface = baseInterface?.toIInspectableABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIBaseProtectedFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _baseInterface, &_innerInterface, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let _baseInterface = baseInterface?.toIInspectableABI { $0 } + let (_innerInterface) = try ComPtrs.initialize { _innerInterfaceAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIBaseProtectedFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _baseInterface, &_innerInterfaceAbi, &valueAbi)) + } + } + innerInterface = test_component.IInspectable(_innerInterface!) } - innerInterface = test_component.IInspectable(consuming: _innerInterface!) - return IBase(consuming: value!) + return IBase(value!) } } @@ -447,9 +455,10 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIClass } internal func get_ImplementationImpl() throws -> test_component.AnyIBasic? { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBasic>? - _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Implementation(pThis, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Implementation(pThis, &valueAbi)) + } } return __ABI_test_component.IBasicWrapper.unwrapFrom(abi: value) } @@ -471,9 +480,10 @@ public enum __ABI_test_component { } internal func GetDelegateImpl() throws -> test_component.AnyISimpleDelegate? { - var result: UnsafeMutablePointer<__x_ABI_Ctest__component_CISimpleDelegate>? - _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetDelegate(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetDelegate(pThis, &resultAbi)) + } } return __ABI_test_component.ISimpleDelegateWrapper.unwrapFrom(abi: result) } @@ -536,17 +546,19 @@ public enum __ABI_test_component { } internal func OutObjectImpl(_ value: inout Any?) throws { - var _value: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.OutObject(pThis, &_value)) + let (_value) = try ComPtrs.initialize { (_valueAbi) in + _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.OutObject(pThis, &_valueAbi)) + } } value = __ABI_.AnyWrapper.unwrapFrom(abi: _value) } internal func OutStringableImpl(_ value: inout test_component.AnyIStringable?) throws { - var _value: UnsafeMutablePointer<__x_ABI_CWindows_CFoundation_CIStringable>? - _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.OutStringable(pThis, &_value)) + let (_value) = try ComPtrs.initialize { (_valueAbi) in + _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.OutStringable(pThis, &_valueAbi)) + } } value = __ABI_Windows_Foundation.IStringableWrapper.unwrapFrom(abi: _value) } @@ -574,9 +586,10 @@ public enum __ABI_test_component { } internal func ReturnObjectImpl() throws -> Any? { - var result: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.ReturnObject(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.ReturnObject(pThis, &resultAbi)) + } } return __ABI_.AnyWrapper.unwrapFrom(abi: result) } @@ -590,9 +603,10 @@ public enum __ABI_test_component { } internal func ReturnReferenceEnumImpl() throws -> test_component.Signed? { - var result: UnsafeMutablePointer<__x_ABI_C__FIReference_1___x_ABI_Ctest__zcomponent__CSigned>? - _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.ReturnReferenceEnum(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.ReturnReferenceEnum(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIReference_1___x_ABI_Ctest__zcomponent__CSignedWrapper.unwrapFrom(abi: result) } @@ -650,9 +664,10 @@ public enum __ABI_test_component { } internal func RaiseDeferrableEventAsyncImpl() throws -> test_component.AnyIAsyncOperation? { - var operation: UnsafeMutablePointer<__x_ABI_C__FIAsyncOperation_1_int>? - _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.RaiseDeferrableEventAsync(pThis, &operation)) + let (operation) = try ComPtrs.initialize { operationAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.RaiseDeferrableEventAsync(pThis, &operationAbi)) + } } return test_component.__x_ABI_C__FIAsyncOperation_1_intWrapper.unwrapFrom(abi: operation) } @@ -682,9 +697,10 @@ public enum __ABI_test_component { } internal func get_StartValueImpl() throws -> Int32? { - var value: UnsafeMutablePointer<__x_ABI_C__FIReference_1_int>? - _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_StartValue(pThis, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_StartValue(pThis, &valueAbi)) + } } return test_component.__x_ABI_C__FIReference_1_intWrapper.unwrapFrom(abi: value) } @@ -698,9 +714,10 @@ public enum __ABI_test_component { } internal func get_IdImpl() throws -> test_component.GUID? { - var value: UnsafeMutablePointer<__x_ABI_C__FIReference_1_GUID>? - _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Id(pThis, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Id(pThis, &valueAbi)) + } } return test_component.__x_ABI_C__FIReference_1_GUIDWrapper.unwrapFrom(abi: value) } @@ -714,9 +731,10 @@ public enum __ABI_test_component { } internal func get_BasePropertyImpl() throws -> test_component.Base? { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBase>? - _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_BaseProperty(pThis, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_BaseProperty(pThis, &valueAbi)) + } } return .from(abi: value) } @@ -728,9 +746,10 @@ public enum __ABI_test_component { } internal func get_BaseNoOverridesPropertyImpl() throws -> test_component.BaseNoOverrides? { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBaseNoOverrides>? - _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_BaseNoOverridesProperty(pThis, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIClass.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_BaseNoOverridesProperty(pThis, &valueAbi)) + } } return .from(abi: value) } @@ -747,61 +766,67 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIClassFactory } internal func CreateInstanceImpl(_ name: String) throws -> IClass { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIClass>? - let _name = try! HString(name) - _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _name.get(), &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let _name = try! HString(name) + _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _name.get(), &valueAbi)) + } } - return IClass(consuming: value!) + return IClass(value!) } internal func CreateInstance2Impl(_ name: String, _ fruit: test_component.Fruit) throws -> IClass { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIClass>? - let _name = try! HString(name) - _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance2(pThis, _name.get(), fruit, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let _name = try! HString(name) + _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance2(pThis, _name.get(), fruit, &valueAbi)) + } } - return IClass(consuming: value!) + return IClass(value!) } internal func CreateInstance3Impl(_ arg: test_component.AnyIMap?, _ dummy1: Int32, _ dummy2: Int32, _ dummy3: Int32) throws -> IClass { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIClass>? - let argWrapper = test_component.__x_ABI_C__FIMap_2_HSTRING_HSTRINGWrapper(arg) - let _arg = try! argWrapper?.toABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance3(pThis, _arg, dummy1, dummy2, dummy3, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let argWrapper = test_component.__x_ABI_C__FIMap_2_HSTRING_HSTRINGWrapper(arg) + let _arg = try! argWrapper?.toABI { $0 } + _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance3(pThis, _arg, dummy1, dummy2, dummy3, &valueAbi)) + } } - return IClass(consuming: value!) + return IClass(value!) } internal func CreateInstance4Impl(_ arg: test_component.AnyIMapView?, _ dummy1: Int32, _ dummy2: Int32, _ dummy3: Int32, _ dummy4: Int32) throws -> IClass { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIClass>? - let argWrapper = test_component.__x_ABI_C__FIMapView_2_HSTRING_HSTRINGWrapper(arg) - let _arg = try! argWrapper?.toABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance4(pThis, _arg, dummy1, dummy2, dummy3, dummy4, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let argWrapper = test_component.__x_ABI_C__FIMapView_2_HSTRING_HSTRINGWrapper(arg) + let _arg = try! argWrapper?.toABI { $0 } + _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance4(pThis, _arg, dummy1, dummy2, dummy3, dummy4, &valueAbi)) + } } - return IClass(consuming: value!) + return IClass(value!) } internal func CreateInstance5Impl(_ arg: test_component.AnyIVector?, _ dummy1: Int32, _ dummy2: Int32, _ dummy3: Int32, _ dummy4: Int32, _ dummy5: Int32) throws -> IClass { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIClass>? - let argWrapper = test_component.__x_ABI_C__FIVector_1_HSTRINGWrapper(arg) - let _arg = try! argWrapper?.toABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance5(pThis, _arg, dummy1, dummy2, dummy3, dummy4, dummy5, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let argWrapper = test_component.__x_ABI_C__FIVector_1_HSTRINGWrapper(arg) + let _arg = try! argWrapper?.toABI { $0 } + _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance5(pThis, _arg, dummy1, dummy2, dummy3, dummy4, dummy5, &valueAbi)) + } } - return IClass(consuming: value!) + return IClass(value!) } internal func CreateInstance6Impl(_ arg: test_component.AnyIVectorView?, _ dummy1: Int32, _ dummy2: Int32, _ dummy3: Int32, _ dummy4: Int32, _ dummy5: Int32, _ dummy6: Int32) throws -> IClass { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIClass>? - let argWrapper = test_component.__x_ABI_C__FIVectorView_1_HSTRINGWrapper(arg) - let _arg = try! argWrapper?.toABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance6(pThis, _arg, dummy1, dummy2, dummy3, dummy4, dummy5, dummy6, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let argWrapper = test_component.__x_ABI_C__FIVectorView_1_HSTRINGWrapper(arg) + let _arg = try! argWrapper?.toABI { $0 } + _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance6(pThis, _arg, dummy1, dummy2, dummy3, dummy4, dummy5, dummy6, &valueAbi)) + } } - return IClass(consuming: value!) + return IClass(value!) } } @@ -810,14 +835,15 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIClassFactory2 } internal func CreateInstanceImpl(_ name: String, _ fruit: test_component.Fruit, _ implementation: test_component.AnyIIAmImplementable?) throws -> IClass { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIClass>? - let _name = try! HString(name) - let implementationWrapper = __ABI_test_component.IIAmImplementableWrapper(implementation) - let _implementation = try! implementationWrapper?.toABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory2.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _name.get(), fruit, _implementation, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let _name = try! HString(name) + let implementationWrapper = __ABI_test_component.IIAmImplementableWrapper(implementation) + let _implementation = try! implementationWrapper?.toABI { $0 } + _ = try perform(as: __x_ABI_Ctest__component_CIClassFactory2.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _name.get(), fruit, _implementation, &valueAbi)) + } } - return IClass(consuming: value!) + return IClass(value!) } } @@ -880,17 +906,19 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CICollectionTester } internal func ReturnStoredStringVectorImpl() throws -> test_component.AnyIVector? { - var result: UnsafeMutablePointer<__x_ABI_C__FIVector_1_HSTRING>? - _ = try perform(as: __x_ABI_Ctest__component_CICollectionTester.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.ReturnStoredStringVector(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CICollectionTester.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.ReturnStoredStringVector(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIVector_1_HSTRINGWrapper.unwrapFrom(abi: result) } internal func ReturnMapFromStringToStringImpl() throws -> test_component.AnyIMap? { - var result: UnsafeMutablePointer<__x_ABI_C__FIMap_2_HSTRING_HSTRING>? - _ = try perform(as: __x_ABI_Ctest__component_CICollectionTester.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.ReturnMapFromStringToString(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CICollectionTester.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.ReturnMapFromStringToString(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIMap_2_HSTRING_HSTRINGWrapper.unwrapFrom(abi: result) } @@ -956,9 +984,10 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIDeferrableEventArgs } internal func GetDeferralImpl() throws -> test_component.Deferral? { - var result: UnsafeMutablePointer<__x_ABI_CWindows_CFoundation_CIDeferral>? - _ = try perform(as: __x_ABI_Ctest__component_CIDeferrableEventArgs.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetDeferral(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIDeferrableEventArgs.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetDeferral(pThis, &resultAbi)) + } } return .from(abi: result) } @@ -1038,13 +1067,14 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIEventTesterFactory } internal func CreateInstanceImpl(_ impl: test_component.AnyIIAmImplementable?) throws -> IEventTester { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIEventTester>? - let implWrapper = __ABI_test_component.IIAmImplementableWrapper(impl) - let _impl = try! implWrapper?.toABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIEventTesterFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _impl, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let implWrapper = __ABI_test_component.IIAmImplementableWrapper(impl) + let _impl = try! implWrapper?.toABI { $0 } + _ = try perform(as: __x_ABI_Ctest__component_CIEventTesterFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _impl, &valueAbi)) + } } - return IEventTester(consuming: value!) + return IEventTester(value!) } } @@ -1103,9 +1133,10 @@ public enum __ABI_test_component { } open func OutObjectImpl(_ value: inout Any?) throws { - var _value: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_Ctest__component_CIIAmImplementable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.OutObject(pThis, &_value)) + let (_value) = try ComPtrs.initialize { (_valueAbi) in + _ = try perform(as: __x_ABI_Ctest__component_CIIAmImplementable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.OutObject(pThis, &_valueAbi)) + } } value = __ABI_.AnyWrapper.unwrapFrom(abi: _value) } @@ -1133,9 +1164,10 @@ public enum __ABI_test_component { } open func ReturnObjectImpl() throws -> Any? { - var result: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_Ctest__component_CIIAmImplementable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.ReturnObject(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIIAmImplementable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.ReturnObject(pThis, &resultAbi)) + } } return __ABI_.AnyWrapper.unwrapFrom(abi: result) } @@ -1163,9 +1195,10 @@ public enum __ABI_test_component { } open func get_IdImpl() throws -> test_component.GUID? { - var value: UnsafeMutablePointer<__x_ABI_C__FIReference_1_GUID>? - _ = try perform(as: __x_ABI_Ctest__component_CIIAmImplementable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Id(pThis, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIIAmImplementable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Id(pThis, &valueAbi)) + } } return test_component.__x_ABI_C__FIReference_1_GUIDWrapper.unwrapFrom(abi: value) } @@ -1254,7 +1287,7 @@ public enum __ABI_test_component { InObject: { do { guard let __unwrapped__instance = IIAmImplementableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: $1) + let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: ComPtr($1)) let result = try __unwrapped__instance.inObject(value) $2?.initialize(to: try! HString(result).detach()) return S_OK @@ -1376,14 +1409,14 @@ public enum __ABI_test_component { put_Id: { guard let __unwrapped__instance = IIAmImplementableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let value: test_component.GUID? = test_component.__x_ABI_C__FIReference_1_GUIDWrapper.unwrapFrom(abi: $1) + let value: test_component.GUID? = test_component.__x_ABI_C__FIReference_1_GUIDWrapper.unwrapFrom(abi: ComPtr($1)) __unwrapped__instance.id = value return S_OK }, add_ImplementableEvent: { guard let __unwrapped__instance = IIAmImplementableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - guard let handler = __ABI_test_component_Delegates.InDelegateWrapper.unwrapFrom(abi: $1) else { return E_INVALIDARG } + guard let handler = __ABI_test_component_Delegates.InDelegateWrapper.unwrapFrom(abi: ComPtr($1)) else { return E_INVALIDARG } let token = __unwrapped__instance.implementableEvent.addHandler(handler) $2?.initialize(to: .from(swift: token)) return S_OK @@ -1451,7 +1484,7 @@ public enum __ABI_test_component { TakeObservable: { do { guard let __unwrapped__instance = IInterfaceWithObservableVectorWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let basics: test_component.AnyIObservableVector? = test_component.__x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.unwrapFrom(abi: $1) + let basics: test_component.AnyIObservableVector? = test_component.__x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.unwrapFrom(abi: ComPtr($1)) try __unwrapped__instance.takeObservable(basics) return S_OK } catch { return failWith(err: E_FAIL) } @@ -1511,41 +1544,46 @@ public enum __ABI_test_component { } internal func GetNullObjectImpl() throws -> Any? { - var result: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_Ctest__component_CINullValuesStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetNullObject(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CINullValuesStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetNullObject(pThis, &resultAbi)) + } } return __ABI_.AnyWrapper.unwrapFrom(abi: result) } internal func GetNullInterfaceImpl() throws -> test_component.AnyIClosable? { - var result: UnsafeMutablePointer<__x_ABI_CWindows_CFoundation_CIClosable>? - _ = try perform(as: __x_ABI_Ctest__component_CINullValuesStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetNullInterface(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CINullValuesStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetNullInterface(pThis, &resultAbi)) + } } return __ABI_Windows_Foundation.IClosableWrapper.unwrapFrom(abi: result) } internal func GetNullGenericInterfaceImpl() throws -> test_component.AnyIVector? { - var result: UnsafeMutablePointer<__x_ABI_C__FIVector_1_HSTRING>? - _ = try perform(as: __x_ABI_Ctest__component_CINullValuesStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetNullGenericInterface(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CINullValuesStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetNullGenericInterface(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIVector_1_HSTRINGWrapper.unwrapFrom(abi: result) } internal func GetNullClassImpl() throws -> test_component.NoopClosable? { - var result: UnsafeMutablePointer<__x_ABI_CWindows_CFoundation_CIClosable>? - _ = try perform(as: __x_ABI_Ctest__component_CINullValuesStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetNullClass(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CINullValuesStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetNullClass(pThis, &resultAbi)) + } } return .from(abi: result) } internal func GetNullDelegateImpl() throws -> test_component.VoidToVoidDelegate? { - var result: UnsafeMutablePointer<__x_ABI_Ctest__component_CIVoidToVoidDelegate>? - _ = try perform(as: __x_ABI_Ctest__component_CINullValuesStatics.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetNullDelegate(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CINullValuesStatics.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetNullDelegate(pThis, &resultAbi)) + } } return __ABI_test_component.VoidToVoidDelegateWrapper.unwrapFrom(abi: result) } @@ -1562,25 +1600,28 @@ public enum __ABI_test_component { } internal func OperationImpl(_ value: test_component.DateTime) throws -> test_component.AnyIAsyncOperation? { - var operation: UnsafeMutablePointer<__x_ABI_C__FIAsyncOperation_1_int>? - _ = try perform(as: __x_ABI_Ctest__component_CISimple.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.Operation(pThis, .from(swift: value), &operation)) + let (operation) = try ComPtrs.initialize { operationAbi in + _ = try perform(as: __x_ABI_Ctest__component_CISimple.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.Operation(pThis, .from(swift: value), &operationAbi)) + } } return test_component.__x_ABI_C__FIAsyncOperation_1_intWrapper.unwrapFrom(abi: operation) } internal func ActionImpl(_ value: test_component.DateTime) throws -> test_component.AnyIAsyncAction? { - var operation: UnsafeMutablePointer<__x_ABI_CWindows_CFoundation_CIAsyncAction>? - _ = try perform(as: __x_ABI_Ctest__component_CISimple.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.Action(pThis, .from(swift: value), &operation)) + let (operation) = try ComPtrs.initialize { operationAbi in + _ = try perform(as: __x_ABI_Ctest__component_CISimple.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.Action(pThis, .from(swift: value), &operationAbi)) + } } return __ABI_Windows_Foundation.IAsyncActionWrapper.unwrapFrom(abi: operation) } internal func ObjectImpl(_ value: test_component.DateTime) throws -> Any? { - var result: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_Ctest__component_CISimple.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.Object(pThis, .from(swift: value), &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_Ctest__component_CISimple.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.Object(pThis, .from(swift: value), &resultAbi)) + } } return __ABI_.AnyWrapper.unwrapFrom(abi: result) } @@ -1892,14 +1933,16 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIUnsealedDerived2Factory } internal func CreateInstanceImpl(_ prop: Int32, _ baseInterface: UnsealedWinRTClassWrapper?, _ innerInterface: inout test_component.IInspectable?) throws -> IUnsealedDerived2 { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIUnsealedDerived2>? - var _innerInterface: UnsafeMutablePointer? - let _baseInterface = baseInterface?.toIInspectableABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerived2Factory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, prop, _baseInterface, &_innerInterface, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let _baseInterface = baseInterface?.toIInspectableABI { $0 } + let (_innerInterface) = try ComPtrs.initialize { _innerInterfaceAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerived2Factory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, prop, _baseInterface, &_innerInterfaceAbi, &valueAbi)) + } + } + innerInterface = test_component.IInspectable(_innerInterface!) } - innerInterface = test_component.IInspectable(consuming: _innerInterface!) - return IUnsealedDerived2(consuming: value!) + return IUnsealedDerived2(value!) } } @@ -1908,14 +1951,16 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIUnsealedDerived2ProtectedFactory } internal func CreateInstanceImpl(_ baseInterface: UnsealedWinRTClassWrapper?, _ innerInterface: inout test_component.IInspectable?) throws -> IUnsealedDerived2 { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIUnsealedDerived2>? - var _innerInterface: UnsafeMutablePointer? - let _baseInterface = baseInterface?.toIInspectableABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerived2ProtectedFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _baseInterface, &_innerInterface, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let _baseInterface = baseInterface?.toIInspectableABI { $0 } + let (_innerInterface) = try ComPtrs.initialize { _innerInterfaceAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerived2ProtectedFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _baseInterface, &_innerInterfaceAbi, &valueAbi)) + } + } + innerInterface = test_component.IInspectable(_innerInterface!) } - innerInterface = test_component.IInspectable(consuming: _innerInterface!) - return IUnsealedDerived2(consuming: value!) + return IUnsealedDerived2(value!) } } @@ -1924,37 +1969,43 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIUnsealedDerivedFactory } internal func CreateInstanceImpl(_ baseInterface: UnsealedWinRTClassWrapper?, _ innerInterface: inout test_component.IInspectable?) throws -> IUnsealedDerived { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIUnsealedDerived>? - var _innerInterface: UnsafeMutablePointer? - let _baseInterface = baseInterface?.toIInspectableABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerivedFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _baseInterface, &_innerInterface, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let _baseInterface = baseInterface?.toIInspectableABI { $0 } + let (_innerInterface) = try ComPtrs.initialize { _innerInterfaceAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerivedFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _baseInterface, &_innerInterfaceAbi, &valueAbi)) + } + } + innerInterface = test_component.IInspectable(_innerInterface!) } - innerInterface = test_component.IInspectable(consuming: _innerInterface!) - return IUnsealedDerived(consuming: value!) + return IUnsealedDerived(value!) } internal func CreateInstance2Impl(_ prop: Int32, _ baseInterface: UnsealedWinRTClassWrapper?, _ innerInterface: inout test_component.IInspectable?) throws -> IUnsealedDerived { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIUnsealedDerived>? - var _innerInterface: UnsafeMutablePointer? - let _baseInterface = baseInterface?.toIInspectableABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerivedFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance2(pThis, prop, _baseInterface, &_innerInterface, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let _baseInterface = baseInterface?.toIInspectableABI { $0 } + let (_innerInterface) = try ComPtrs.initialize { _innerInterfaceAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerivedFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance2(pThis, prop, _baseInterface, &_innerInterfaceAbi, &valueAbi)) + } + } + innerInterface = test_component.IInspectable(_innerInterface!) } - innerInterface = test_component.IInspectable(consuming: _innerInterface!) - return IUnsealedDerived(consuming: value!) + return IUnsealedDerived(value!) } internal func CreateInstance3Impl(_ prop1: String, _ prop2: test_component.Base?, _ baseInterface: UnsealedWinRTClassWrapper?, _ innerInterface: inout test_component.IInspectable?) throws -> IUnsealedDerived { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIUnsealedDerived>? - var _innerInterface: UnsafeMutablePointer? - let _baseInterface = baseInterface?.toIInspectableABI { $0 } - let _prop1 = try! HString(prop1) - _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerivedFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance3(pThis, _prop1.get(), RawPointer(prop2), _baseInterface, &_innerInterface, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let _prop1 = try! HString(prop1) + let _baseInterface = baseInterface?.toIInspectableABI { $0 } + let (_innerInterface) = try ComPtrs.initialize { _innerInterfaceAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerivedFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance3(pThis, _prop1.get(), RawPointer(prop2), _baseInterface, &_innerInterfaceAbi, &valueAbi)) + } + } + innerInterface = test_component.IInspectable(_innerInterface!) } - innerInterface = test_component.IInspectable(consuming: _innerInterface!) - return IUnsealedDerived(consuming: value!) + return IUnsealedDerived(value!) } } @@ -1968,14 +2019,16 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIUnsealedDerivedFromNoConstructorFactory } internal func CreateInstanceImpl(_ baseInterface: UnsealedWinRTClassWrapper?, _ innerInterface: inout test_component.IInspectable?) throws -> IUnsealedDerivedFromNoConstructor { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIUnsealedDerivedFromNoConstructor>? - var _innerInterface: UnsafeMutablePointer? - let _baseInterface = baseInterface?.toIInspectableABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerivedFromNoConstructorFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _baseInterface, &_innerInterface, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let _baseInterface = baseInterface?.toIInspectableABI { $0 } + let (_innerInterface) = try ComPtrs.initialize { _innerInterfaceAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerivedFromNoConstructorFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _baseInterface, &_innerInterfaceAbi, &valueAbi)) + } + } + innerInterface = test_component.IInspectable(_innerInterface!) } - innerInterface = test_component.IInspectable(consuming: _innerInterface!) - return IUnsealedDerivedFromNoConstructor(consuming: value!) + return IUnsealedDerivedFromNoConstructor(value!) } } @@ -1999,14 +2052,16 @@ public enum __ABI_test_component { override public class var IID: test_component.IID { IID___x_ABI_Ctest__component_CIUnsealedDerivedNoOverridesProtectedFactory } internal func CreateInstanceImpl(_ baseInterface: UnsealedWinRTClassWrapper?, _ innerInterface: inout test_component.IInspectable?) throws -> IUnsealedDerivedNoOverrides { - var value: UnsafeMutablePointer<__x_ABI_Ctest__component_CIUnsealedDerivedNoOverrides>? - var _innerInterface: UnsafeMutablePointer? - let _baseInterface = baseInterface?.toIInspectableABI { $0 } - _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerivedNoOverridesProtectedFactory.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _baseInterface, &_innerInterface, &value)) + let (value) = try ComPtrs.initialize { valueAbi in + let _baseInterface = baseInterface?.toIInspectableABI { $0 } + let (_innerInterface) = try ComPtrs.initialize { _innerInterfaceAbi in + _ = try perform(as: __x_ABI_Ctest__component_CIUnsealedDerivedNoOverridesProtectedFactory.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.CreateInstance(pThis, _baseInterface, &_innerInterfaceAbi, &valueAbi)) + } + } + innerInterface = test_component.IInspectable(_innerInterface!) } - innerInterface = test_component.IInspectable(consuming: _innerInterface!) - return IUnsealedDerivedNoOverrides(consuming: value!) + return IUnsealedDerivedNoOverrides(value!) } } @@ -2084,7 +2139,7 @@ public enum __ABI_test_component { add_EventWithReturn: { guard let __unwrapped__instance = InterfaceWithReturnDelegateWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - guard let handler = __ABI_test_component_Delegates.ReturnInt32DelegateWrapper.unwrapFrom(abi: $1) else { return E_INVALIDARG } + guard let handler = __ABI_test_component_Delegates.ReturnInt32DelegateWrapper.unwrapFrom(abi: ComPtr($1)) else { return E_INVALIDARG } let token = __unwrapped__instance.eventWithReturn.addHandler(handler) $2?.initialize(to: .from(swift: token)) return S_OK @@ -2195,7 +2250,7 @@ public enum __ABI_test_component { add_Repeat: { guard let __unwrapped__instance = WithKeywordWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - guard let handler = test_component.__x_ABI_C__FIEventHandler_1_IInspectableWrapper.unwrapFrom(abi: $1) else { return E_INVALIDARG } + guard let handler = test_component.__x_ABI_C__FIEventHandler_1_IInspectableWrapper.unwrapFrom(abi: ComPtr($1)) else { return E_INVALIDARG } let token = __unwrapped__instance.`repeat`.addHandler(handler) $2?.initialize(to: .from(swift: token)) return S_OK @@ -2423,7 +2478,7 @@ extension __ABI_test_component { Release: { ObjectHandlerWrapper.release($0) }, Invoke: { guard let __unwrapped__instance = ObjectHandlerWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let item: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: $1) + let item: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: ComPtr($1)) __unwrapped__instance(item) return S_OK } diff --git a/tests/test_component/Sources/test_component/test_component+Generics.swift b/tests/test_component/Sources/test_component/test_component+Generics.swift index cd5dc8e1..123378b9 100644 --- a/tests/test_component/Sources/test_component/test_component+Generics.swift +++ b/tests/test_component/Sources/test_component/test_component+Generics.swift @@ -19,7 +19,7 @@ internal var __x_ABI_C__FIAsyncOperationCompletedHandler_1_intVTable: __x_ABI_C_ Release: { __x_ABI_C__FIAsyncOperationCompletedHandler_1_intWrapper.release($0) }, Invoke: { guard let __unwrapped__instance = __x_ABI_C__FIAsyncOperationCompletedHandler_1_intWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let asyncInfo: test_component.AnyIAsyncOperation? = test_component.__x_ABI_C__FIAsyncOperation_1_intWrapper.unwrapFrom(abi: $1) + let asyncInfo: test_component.AnyIAsyncOperation? = test_component.__x_ABI_C__FIAsyncOperation_1_intWrapper.unwrapFrom(abi: ComPtr($1)) let asyncStatus: test_component.AsyncStatus = $2 __unwrapped__instance(asyncInfo, asyncStatus) return S_OK @@ -44,7 +44,7 @@ internal class __x_ABI_C__FIAsyncOperationCompletedHandler_1_intBridge : WinRTDe internal typealias CABI = __x_ABI_C__FIAsyncOperationCompletedHandler_1_int internal typealias SwiftABI = test_component.AsyncOperationCompletedHandlerInt32 - internal static func from(abi: UnsafeMutablePointer?) -> Handler? { + internal static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (asyncInfo, asyncStatus) in @@ -70,7 +70,7 @@ internal var __x_ABI_C__FIAsyncOperationProgressHandler_2_int_doubleVTable: __x_ Release: { __x_ABI_C__FIAsyncOperationProgressHandler_2_int_doubleWrapper.release($0) }, Invoke: { guard let __unwrapped__instance = __x_ABI_C__FIAsyncOperationProgressHandler_2_int_doubleWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let asyncInfo: test_component.AnyIAsyncOperationWithProgress? = test_component.__x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleWrapper.unwrapFrom(abi: $1) + let asyncInfo: test_component.AnyIAsyncOperationWithProgress? = test_component.__x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleWrapper.unwrapFrom(abi: ComPtr($1)) let progressInfo: Double = $2 __unwrapped__instance(asyncInfo, progressInfo) return S_OK @@ -95,7 +95,7 @@ internal class __x_ABI_C__FIAsyncOperationProgressHandler_2_int_doubleBridge : W internal typealias CABI = __x_ABI_C__FIAsyncOperationProgressHandler_2_int_double internal typealias SwiftABI = test_component.AsyncOperationProgressHandlerInt32_Double - internal static func from(abi: UnsafeMutablePointer?) -> Handler? { + internal static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (asyncInfo, progressInfo) in @@ -121,7 +121,7 @@ internal var __x_ABI_C__FIAsyncOperationWithProgressCompletedHandler_2_int_doubl Release: { __x_ABI_C__FIAsyncOperationWithProgressCompletedHandler_2_int_doubleWrapper.release($0) }, Invoke: { guard let __unwrapped__instance = __x_ABI_C__FIAsyncOperationWithProgressCompletedHandler_2_int_doubleWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let asyncInfo: test_component.AnyIAsyncOperationWithProgress? = test_component.__x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleWrapper.unwrapFrom(abi: $1) + let asyncInfo: test_component.AnyIAsyncOperationWithProgress? = test_component.__x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleWrapper.unwrapFrom(abi: ComPtr($1)) let asyncStatus: test_component.AsyncStatus = $2 __unwrapped__instance(asyncInfo, asyncStatus) return S_OK @@ -146,7 +146,7 @@ internal class __x_ABI_C__FIAsyncOperationWithProgressCompletedHandler_2_int_dou internal typealias CABI = __x_ABI_C__FIAsyncOperationWithProgressCompletedHandler_2_int_double internal typealias SwiftABI = test_component.AsyncOperationWithProgressCompletedHandlerInt32_Double - internal static func from(abi: UnsafeMutablePointer?) -> Handler? { + internal static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (asyncInfo, asyncStatus) in @@ -200,9 +200,10 @@ internal class IIterableAny: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIIterable_1_IInspectable } internal func FirstImpl() throws -> test_component.AnyIIterator? { - var result: UnsafeMutablePointer<__x_ABI_C__FIIterator_1_IInspectable>? - _ = try perform(as: __x_ABI_C__FIIterable_1_IInspectable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIIterable_1_IInspectable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIIterator_1_IInspectableWrapper.unwrapFrom(abi: result) } @@ -213,7 +214,7 @@ internal enum __x_ABI_C__FIIterable_1_IInspectableBridge : AbiInterfaceBridge { internal typealias CABI = __x_ABI_C__FIIterable_1_IInspectable internal typealias SwiftABI = IIterableAny internal typealias SwiftProjection = AnyIIterable - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterable_1_IInspectableImpl(abi) } @@ -228,7 +229,7 @@ fileprivate class __x_ABI_C__FIIterable_1_IInspectableImpl : IIterable, AbiInter typealias T = Any? typealias Bridge = __x_ABI_C__FIIterable_1_IInspectableBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -286,9 +287,10 @@ internal class IIterableString: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIIterable_1_HSTRING } internal func FirstImpl() throws -> test_component.AnyIIterator? { - var result: UnsafeMutablePointer<__x_ABI_C__FIIterator_1_HSTRING>? - _ = try perform(as: __x_ABI_C__FIIterable_1_HSTRING.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIIterable_1_HSTRING.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIIterator_1_HSTRINGWrapper.unwrapFrom(abi: result) } @@ -299,7 +301,7 @@ internal enum __x_ABI_C__FIIterable_1_HSTRINGBridge : AbiInterfaceBridge { internal typealias CABI = __x_ABI_C__FIIterable_1_HSTRING internal typealias SwiftABI = IIterableString internal typealias SwiftProjection = AnyIIterable - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterable_1_HSTRINGImpl(abi) } @@ -314,7 +316,7 @@ fileprivate class __x_ABI_C__FIIterable_1_HSTRINGImpl : IIterable, AbiInterfaceI typealias T = String typealias Bridge = __x_ABI_C__FIIterable_1_HSTRINGBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -372,9 +374,10 @@ internal class IIterableIKeyValuePairString_Any: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectable } internal func FirstImpl() throws -> test_component.AnyIIterator?>? { - var result: UnsafeMutablePointer<__x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectable>? - _ = try perform(as: __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectableWrapper.unwrapFrom(abi: result) } @@ -385,7 +388,7 @@ internal enum __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspe internal typealias CABI = __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectable internal typealias SwiftABI = IIterableIKeyValuePairString_Any internal typealias SwiftProjection = AnyIIterable?> - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectableImpl(abi) } @@ -400,7 +403,7 @@ fileprivate class __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_II typealias T = AnyIKeyValuePair? typealias Bridge = __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectableBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -458,9 +461,10 @@ internal class IIterableIKeyValuePairString_String: test_component.IInspectable override public class var IID: test_component.IID { IID___x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRING } internal func FirstImpl() throws -> test_component.AnyIIterator?>? { - var result: UnsafeMutablePointer<__x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRING>? - _ = try perform(as: __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRING.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRING.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRINGWrapper.unwrapFrom(abi: result) } @@ -471,7 +475,7 @@ internal enum __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRIN internal typealias CABI = __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRING internal typealias SwiftABI = IIterableIKeyValuePairString_String internal typealias SwiftProjection = AnyIIterable?> - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRINGImpl(abi) } @@ -486,7 +490,7 @@ fileprivate class __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HS typealias T = AnyIKeyValuePair? typealias Bridge = __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRINGBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -544,9 +548,10 @@ internal class IIterableIKeyValuePairString_Base: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBase } internal func FirstImpl() throws -> test_component.AnyIIterator?>? { - var result: UnsafeMutablePointer<__x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBase>? - _ = try perform(as: __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseWrapper.unwrapFrom(abi: result) } @@ -557,7 +562,7 @@ internal enum __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_AB internal typealias CABI = __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBase internal typealias SwiftABI = IIterableIKeyValuePairString_Base internal typealias SwiftProjection = AnyIIterable?> - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseImpl(abi) } @@ -572,7 +577,7 @@ fileprivate class __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING___ typealias T = AnyIKeyValuePair? typealias Bridge = __x_ABI_C__FIIterable_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -630,9 +635,10 @@ internal class IIterableBase: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CBase } internal func FirstImpl() throws -> test_component.AnyIIterator? { - var result: UnsafeMutablePointer<__x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CBase>? - _ = try perform(as: __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CBase.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CBase.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CBaseWrapper.unwrapFrom(abi: result) } @@ -643,7 +649,7 @@ internal enum __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CBaseBridge : A internal typealias CABI = __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CBase internal typealias SwiftABI = IIterableBase internal typealias SwiftProjection = AnyIIterable - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CBaseImpl(abi) } @@ -658,7 +664,7 @@ fileprivate class __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CBaseImpl : typealias T = Base? typealias Bridge = __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CBaseBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -716,9 +722,10 @@ internal class IIterableIBasic: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CIBasic } internal func FirstImpl() throws -> test_component.AnyIIterator? { - var result: UnsafeMutablePointer<__x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CIBasic>? - _ = try perform(as: __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CIBasic.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CIBasic.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.First(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.unwrapFrom(abi: result) } @@ -729,7 +736,7 @@ internal enum __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CIBasicBridge : internal typealias CABI = __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CIBasic internal typealias SwiftABI = IIterableIBasic internal typealias SwiftProjection = AnyIIterable - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CIBasicImpl(abi) } @@ -744,7 +751,7 @@ fileprivate class __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CIBasicImpl typealias T = AnyIBasic? typealias Bridge = __x_ABI_C__FIIterable_1___x_ABI_Ctest__zcomponent__CIBasicBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -818,9 +825,10 @@ internal class IIteratorAny: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIIterator_1_IInspectable } internal func get_CurrentImpl() throws -> Any? { - var result: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_C__FIIterator_1_IInspectable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Current(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIIterator_1_IInspectable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Current(pThis, &resultAbi)) + } } return __ABI_.AnyWrapper.unwrapFrom(abi: result) } @@ -847,7 +855,7 @@ internal enum __x_ABI_C__FIIterator_1_IInspectableBridge : AbiInterfaceBridge { internal typealias CABI = __x_ABI_C__FIIterator_1_IInspectable internal typealias SwiftABI = IIteratorAny internal typealias SwiftProjection = AnyIIterator - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterator_1_IInspectableImpl(abi) } @@ -862,7 +870,7 @@ fileprivate class __x_ABI_C__FIIterator_1_IInspectableImpl : IIterator, AbiInter typealias T = Any? typealias Bridge = __x_ABI_C__FIIterator_1_IInspectableBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -974,7 +982,7 @@ internal enum __x_ABI_C__FIIterator_1_HSTRINGBridge : AbiInterfaceBridge { internal typealias CABI = __x_ABI_C__FIIterator_1_HSTRING internal typealias SwiftABI = IIteratorString internal typealias SwiftProjection = AnyIIterator - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterator_1_HSTRINGImpl(abi) } @@ -989,7 +997,7 @@ fileprivate class __x_ABI_C__FIIterator_1_HSTRINGImpl : IIterator, AbiInterfaceI typealias T = String typealias Bridge = __x_ABI_C__FIIterator_1_HSTRINGBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -1073,9 +1081,10 @@ internal class IIteratorIKeyValuePairString_Any: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectable } internal func get_CurrentImpl() throws -> test_component.AnyIKeyValuePair? { - var result: UnsafeMutablePointer<__x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectable>? - _ = try perform(as: __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Current(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Current(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectableWrapper.unwrapFrom(abi: result) } @@ -1102,7 +1111,7 @@ internal enum __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspe internal typealias CABI = __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectable internal typealias SwiftABI = IIteratorIKeyValuePairString_Any internal typealias SwiftProjection = AnyIIterator?> - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectableImpl(abi) } @@ -1117,7 +1126,7 @@ fileprivate class __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_II typealias T = AnyIKeyValuePair? typealias Bridge = __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectableBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -1201,9 +1210,10 @@ internal class IIteratorIKeyValuePairString_String: test_component.IInspectable override public class var IID: test_component.IID { IID___x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRING } internal func get_CurrentImpl() throws -> test_component.AnyIKeyValuePair? { - var result: UnsafeMutablePointer<__x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRING>? - _ = try perform(as: __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRING.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Current(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRING.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Current(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRINGWrapper.unwrapFrom(abi: result) } @@ -1230,7 +1240,7 @@ internal enum __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRIN internal typealias CABI = __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRING internal typealias SwiftABI = IIteratorIKeyValuePairString_String internal typealias SwiftProjection = AnyIIterator?> - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRINGImpl(abi) } @@ -1245,7 +1255,7 @@ fileprivate class __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HS typealias T = AnyIKeyValuePair? typealias Bridge = __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRINGBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -1329,9 +1339,10 @@ internal class IIteratorIKeyValuePairString_Base: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBase } internal func get_CurrentImpl() throws -> test_component.AnyIKeyValuePair? { - var result: UnsafeMutablePointer<__x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBase>? - _ = try perform(as: __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Current(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Current(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseWrapper.unwrapFrom(abi: result) } @@ -1358,7 +1369,7 @@ internal enum __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_AB internal typealias CABI = __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBase internal typealias SwiftABI = IIteratorIKeyValuePairString_Base internal typealias SwiftProjection = AnyIIterator?> - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseImpl(abi) } @@ -1373,7 +1384,7 @@ fileprivate class __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING___ typealias T = AnyIKeyValuePair? typealias Bridge = __x_ABI_C__FIIterator_1___x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -1456,9 +1467,10 @@ internal class IIteratorBase: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CBase } internal func get_CurrentImpl() throws -> test_component.Base? { - var result: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBase>? - _ = try perform(as: __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CBase.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Current(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CBase.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Current(pThis, &resultAbi)) + } } return .from(abi: result) } @@ -1485,7 +1497,7 @@ internal enum __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CBaseBridge : A internal typealias CABI = __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CBase internal typealias SwiftABI = IIteratorBase internal typealias SwiftProjection = AnyIIterator - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CBaseImpl(abi) } @@ -1500,7 +1512,7 @@ fileprivate class __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CBaseImpl : typealias T = Base? typealias Bridge = __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CBaseBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -1584,9 +1596,10 @@ internal class IIteratorIBasic: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CIBasic } internal func get_CurrentImpl() throws -> test_component.AnyIBasic? { - var result: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBasic>? - _ = try perform(as: __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CIBasic.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Current(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CIBasic.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Current(pThis, &resultAbi)) + } } return __ABI_test_component.IBasicWrapper.unwrapFrom(abi: result) } @@ -1613,7 +1626,7 @@ internal enum __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CIBasicBridge : internal typealias CABI = __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CIBasic internal typealias SwiftABI = IIteratorIBasic internal typealias SwiftProjection = AnyIIterator - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CIBasicImpl(abi) } @@ -1628,7 +1641,7 @@ fileprivate class __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CIBasicImpl typealias T = AnyIBasic? typealias Bridge = __x_ABI_C__FIIterator_1___x_ABI_Ctest__zcomponent__CIBasicBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -1711,9 +1724,10 @@ internal class IKeyValuePairString_Any: test_component.IInspectable { } internal func get_ValueImpl() throws -> Any? { - var result: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Value(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Value(pThis, &resultAbi)) + } } return __ABI_.AnyWrapper.unwrapFrom(abi: result) } @@ -1724,7 +1738,7 @@ internal enum __x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectableBridge : AbiInterf internal typealias CABI = __x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectable internal typealias SwiftABI = IKeyValuePairString_Any internal typealias SwiftProjection = AnyIKeyValuePair - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectableImpl(abi) } @@ -1740,7 +1754,7 @@ fileprivate class __x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectableImpl : IKeyVal typealias V = Any? typealias Bridge = __x_ABI_C__FIKeyValuePair_2_HSTRING_IInspectableBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -1830,7 +1844,7 @@ internal enum __x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRINGBridge : AbiInterfaceBr internal typealias CABI = __x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRING internal typealias SwiftABI = IKeyValuePairString_String internal typealias SwiftProjection = AnyIKeyValuePair - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRINGImpl(abi) } @@ -1846,7 +1860,7 @@ fileprivate class __x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRINGImpl : IKeyValuePai typealias V = String typealias Bridge = __x_ABI_C__FIKeyValuePair_2_HSTRING_HSTRINGBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -1923,9 +1937,10 @@ internal class IKeyValuePairString_Base: test_component.IInspectable { } internal func get_ValueImpl() throws -> test_component.Base? { - var result: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBase>? - _ = try perform(as: __x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Value(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Value(pThis, &resultAbi)) + } } return .from(abi: result) } @@ -1936,7 +1951,7 @@ internal enum __x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBa internal typealias CABI = __x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBase internal typealias SwiftABI = IKeyValuePairString_Base internal typealias SwiftProjection = AnyIKeyValuePair - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseImpl(abi) } @@ -1952,7 +1967,7 @@ fileprivate class __x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent_ typealias V = Base? typealias Bridge = __x_ABI_C__FIKeyValuePair_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -2042,7 +2057,7 @@ internal enum __x_ABI_C__FIMapChangedEventArgs_1_HSTRINGBridge : AbiInterfaceBri internal typealias CABI = __x_ABI_C__FIMapChangedEventArgs_1_HSTRING internal typealias SwiftABI = IMapChangedEventArgsString internal typealias SwiftProjection = AnyIMapChangedEventArgs - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIMapChangedEventArgs_1_HSTRINGImpl(abi) } @@ -2057,7 +2072,7 @@ fileprivate class __x_ABI_C__FIMapChangedEventArgs_1_HSTRINGImpl : IMapChangedEv typealias K = String typealias Bridge = __x_ABI_C__FIMapChangedEventArgs_1_HSTRINGBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -2149,10 +2164,11 @@ internal class IMapViewString_Any: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIMapView_2_HSTRING_IInspectable } internal func LookupImpl(_ key: String) throws -> Any? { - var result: UnsafeMutablePointer? - let _key = try! HString(key) - _ = try perform(as: __x_ABI_C__FIMapView_2_HSTRING_IInspectable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.Lookup(pThis, _key.get(), &result)) + let (result) = try ComPtrs.initialize { resultAbi in + let _key = try! HString(key) + _ = try perform(as: __x_ABI_C__FIMapView_2_HSTRING_IInspectable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.Lookup(pThis, _key.get(), &resultAbi)) + } } return __ABI_.AnyWrapper.unwrapFrom(abi: result) } @@ -2175,13 +2191,13 @@ internal class IMapViewString_Any: test_component.IInspectable { } internal func SplitImpl(_ first: inout test_component.AnyIMapView?, _ second: inout test_component.AnyIMapView?) throws { - var _first: UnsafeMutablePointer<__x_ABI_C__FIMapView_2_HSTRING_IInspectable>? - var _second: UnsafeMutablePointer<__x_ABI_C__FIMapView_2_HSTRING_IInspectable>? - _ = try perform(as: __x_ABI_C__FIMapView_2_HSTRING_IInspectable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.Split(pThis, &_first, &_second)) + let (_first, _second) = try ComPtrs.initialize { (_firstAbi, _secondAbi) in + _ = try perform(as: __x_ABI_C__FIMapView_2_HSTRING_IInspectable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.Split(pThis, &_firstAbi, &_secondAbi)) + } } - first = test_component.__x_ABI_C__FIMapView_2_HSTRING_IInspectableBridge.from(abi: _first) - second = test_component.__x_ABI_C__FIMapView_2_HSTRING_IInspectableBridge.from(abi: _second) + first = test_component.__x_ABI_C__FIMapView_2_HSTRING_IInspectableWrapper.unwrapFrom(abi: _first) + second = test_component.__x_ABI_C__FIMapView_2_HSTRING_IInspectableWrapper.unwrapFrom(abi: _second) } } @@ -2190,7 +2206,7 @@ internal enum __x_ABI_C__FIMapView_2_HSTRING_IInspectableBridge : AbiInterfaceBr internal typealias CABI = __x_ABI_C__FIMapView_2_HSTRING_IInspectable internal typealias SwiftABI = IMapViewString_Any internal typealias SwiftProjection = AnyIMapView - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIMapView_2_HSTRING_IInspectableImpl(abi) } @@ -2207,7 +2223,7 @@ fileprivate class __x_ABI_C__FIMapView_2_HSTRING_IInspectableImpl : IMapView, Ab typealias V = Any? typealias Bridge = __x_ABI_C__FIMapView_2_HSTRING_IInspectableBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -2232,7 +2248,7 @@ fileprivate class __x_ABI_C__FIMapView_2_HSTRING_IInspectableImpl : IMapView, Ab get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIKeyValuePairString_Any = try! _default.QueryInterface() + private lazy var _IIterable: IIterableIKeyValuePairString_Any! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.imapview-2.first) fileprivate func first() -> AnyIIterator?>? { try! _IIterable.FirstImpl() @@ -2340,13 +2356,13 @@ internal class IMapViewString_String: test_component.IInspectable { } internal func SplitImpl(_ first: inout test_component.AnyIMapView?, _ second: inout test_component.AnyIMapView?) throws { - var _first: UnsafeMutablePointer<__x_ABI_C__FIMapView_2_HSTRING_HSTRING>? - var _second: UnsafeMutablePointer<__x_ABI_C__FIMapView_2_HSTRING_HSTRING>? - _ = try perform(as: __x_ABI_C__FIMapView_2_HSTRING_HSTRING.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.Split(pThis, &_first, &_second)) + let (_first, _second) = try ComPtrs.initialize { (_firstAbi, _secondAbi) in + _ = try perform(as: __x_ABI_C__FIMapView_2_HSTRING_HSTRING.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.Split(pThis, &_firstAbi, &_secondAbi)) + } } - first = test_component.__x_ABI_C__FIMapView_2_HSTRING_HSTRINGBridge.from(abi: _first) - second = test_component.__x_ABI_C__FIMapView_2_HSTRING_HSTRINGBridge.from(abi: _second) + first = test_component.__x_ABI_C__FIMapView_2_HSTRING_HSTRINGWrapper.unwrapFrom(abi: _first) + second = test_component.__x_ABI_C__FIMapView_2_HSTRING_HSTRINGWrapper.unwrapFrom(abi: _second) } } @@ -2355,7 +2371,7 @@ internal enum __x_ABI_C__FIMapView_2_HSTRING_HSTRINGBridge : AbiInterfaceBridge internal typealias CABI = __x_ABI_C__FIMapView_2_HSTRING_HSTRING internal typealias SwiftABI = IMapViewString_String internal typealias SwiftProjection = AnyIMapView - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIMapView_2_HSTRING_HSTRINGImpl(abi) } @@ -2372,7 +2388,7 @@ fileprivate class __x_ABI_C__FIMapView_2_HSTRING_HSTRINGImpl : IMapView, AbiInte typealias V = String typealias Bridge = __x_ABI_C__FIMapView_2_HSTRING_HSTRINGBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -2397,7 +2413,7 @@ fileprivate class __x_ABI_C__FIMapView_2_HSTRING_HSTRINGImpl : IMapView, AbiInte get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIKeyValuePairString_String = try! _default.QueryInterface() + private lazy var _IIterable: IIterableIKeyValuePairString_String! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.imapview-2.first) fileprivate func first() -> AnyIIterator?>? { try! _IIterable.FirstImpl() @@ -2479,10 +2495,11 @@ internal class IMapViewString_Base: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBase } internal func LookupImpl(_ key: String) throws -> test_component.Base? { - var result: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBase>? - let _key = try! HString(key) - _ = try perform(as: __x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.Lookup(pThis, _key.get(), &result)) + let (result) = try ComPtrs.initialize { resultAbi in + let _key = try! HString(key) + _ = try perform(as: __x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.Lookup(pThis, _key.get(), &resultAbi)) + } } return .from(abi: result) } @@ -2505,13 +2522,13 @@ internal class IMapViewString_Base: test_component.IInspectable { } internal func SplitImpl(_ first: inout test_component.AnyIMapView?, _ second: inout test_component.AnyIMapView?) throws { - var _first: UnsafeMutablePointer<__x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBase>? - var _second: UnsafeMutablePointer<__x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBase>? - _ = try perform(as: __x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.Split(pThis, &_first, &_second)) + let (_first, _second) = try ComPtrs.initialize { (_firstAbi, _secondAbi) in + _ = try perform(as: __x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.Split(pThis, &_firstAbi, &_secondAbi)) + } } - first = test_component.__x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseBridge.from(abi: _first) - second = test_component.__x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseBridge.from(abi: _second) + first = test_component.__x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseWrapper.unwrapFrom(abi: _first) + second = test_component.__x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseWrapper.unwrapFrom(abi: _second) } } @@ -2520,7 +2537,7 @@ internal enum __x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseBri internal typealias CABI = __x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBase internal typealias SwiftABI = IMapViewString_Base internal typealias SwiftProjection = AnyIMapView - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseImpl(abi) } @@ -2537,7 +2554,7 @@ fileprivate class __x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBas typealias V = Base? typealias Bridge = __x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -2562,7 +2579,7 @@ fileprivate class __x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBas get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIKeyValuePairString_Base = try! _default.QueryInterface() + private lazy var _IIterable: IIterableIKeyValuePairString_Base! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.imapview-2.first) fileprivate func first() -> AnyIIterator?>? { try! _IIterable.FirstImpl() @@ -2639,7 +2656,7 @@ internal var __x_ABI_C__FIMap_2_HSTRING_IInspectableVTable: __x_ABI_C__FIMap_2_H Insert: { guard let __unwrapped__instance = __x_ABI_C__FIMap_2_HSTRING_IInspectableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } let key: String = .init(from: $1) - let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: $2) + let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: ComPtr($2)) let result = __unwrapped__instance.insert(key, value) $3?.initialize(to: .init(from: result)) return S_OK @@ -2663,10 +2680,11 @@ internal class IMapString_Any: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIMap_2_HSTRING_IInspectable } internal func LookupImpl(_ key: String) throws -> Any? { - var result: UnsafeMutablePointer? - let _key = try! HString(key) - _ = try perform(as: __x_ABI_C__FIMap_2_HSTRING_IInspectable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.Lookup(pThis, _key.get(), &result)) + let (result) = try ComPtrs.initialize { resultAbi in + let _key = try! HString(key) + _ = try perform(as: __x_ABI_C__FIMap_2_HSTRING_IInspectable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.Lookup(pThis, _key.get(), &resultAbi)) + } } return __ABI_.AnyWrapper.unwrapFrom(abi: result) } @@ -2689,9 +2707,10 @@ internal class IMapString_Any: test_component.IInspectable { } internal func GetViewImpl() throws -> test_component.AnyIMapView? { - var result: UnsafeMutablePointer<__x_ABI_C__FIMapView_2_HSTRING_IInspectable>? - _ = try perform(as: __x_ABI_C__FIMap_2_HSTRING_IInspectable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIMap_2_HSTRING_IInspectable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIMapView_2_HSTRING_IInspectableWrapper.unwrapFrom(abi: result) } @@ -2726,7 +2745,7 @@ internal enum __x_ABI_C__FIMap_2_HSTRING_IInspectableBridge : AbiInterfaceBridge internal typealias CABI = __x_ABI_C__FIMap_2_HSTRING_IInspectable internal typealias SwiftABI = IMapString_Any internal typealias SwiftProjection = AnyIMap - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIMap_2_HSTRING_IInspectableImpl(abi) } @@ -2743,7 +2762,7 @@ fileprivate class __x_ABI_C__FIMap_2_HSTRING_IInspectableImpl : IMap, AbiInterfa typealias V = Any? typealias Bridge = __x_ABI_C__FIMap_2_HSTRING_IInspectableBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -2783,7 +2802,7 @@ fileprivate class __x_ABI_C__FIMap_2_HSTRING_IInspectableImpl : IMap, AbiInterfa get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIKeyValuePairString_Any = try! _default.QueryInterface() + private lazy var _IIterable: IIterableIKeyValuePairString_Any! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.imap-2.first) fileprivate func first() -> AnyIIterator?>? { try! _IIterable.FirstImpl() @@ -2909,9 +2928,10 @@ internal class IMapString_String: test_component.IInspectable { } internal func GetViewImpl() throws -> test_component.AnyIMapView? { - var result: UnsafeMutablePointer<__x_ABI_C__FIMapView_2_HSTRING_HSTRING>? - _ = try perform(as: __x_ABI_C__FIMap_2_HSTRING_HSTRING.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIMap_2_HSTRING_HSTRING.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIMapView_2_HSTRING_HSTRINGWrapper.unwrapFrom(abi: result) } @@ -2945,7 +2965,7 @@ internal enum __x_ABI_C__FIMap_2_HSTRING_HSTRINGBridge : AbiInterfaceBridge { internal typealias CABI = __x_ABI_C__FIMap_2_HSTRING_HSTRING internal typealias SwiftABI = IMapString_String internal typealias SwiftProjection = AnyIMap - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIMap_2_HSTRING_HSTRINGImpl(abi) } @@ -2962,7 +2982,7 @@ fileprivate class __x_ABI_C__FIMap_2_HSTRING_HSTRINGImpl : IMap, AbiInterfaceImp typealias V = String typealias Bridge = __x_ABI_C__FIMap_2_HSTRING_HSTRINGBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -3002,7 +3022,7 @@ fileprivate class __x_ABI_C__FIMap_2_HSTRING_HSTRINGImpl : IMap, AbiInterfaceImp get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIKeyValuePairString_String = try! _default.QueryInterface() + private lazy var _IIterable: IIterableIKeyValuePairString_String! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.imap-2.first) fileprivate func first() -> AnyIIterator?>? { try! _IIterable.FirstImpl() @@ -3078,7 +3098,7 @@ internal var __x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseVTable: Insert: { guard let __unwrapped__instance = __x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } let key: String = .init(from: $1) - let value: test_component.Base? = .from(abi: $2) + let value: test_component.Base? = .from(abi: ComPtr($2)) let result = __unwrapped__instance.insert(key, value) $3?.initialize(to: .init(from: result)) return S_OK @@ -3102,10 +3122,11 @@ internal class IMapString_Base: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBase } internal func LookupImpl(_ key: String) throws -> test_component.Base? { - var result: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBase>? - let _key = try! HString(key) - _ = try perform(as: __x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.Lookup(pThis, _key.get(), &result)) + let (result) = try ComPtrs.initialize { resultAbi in + let _key = try! HString(key) + _ = try perform(as: __x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.Lookup(pThis, _key.get(), &resultAbi)) + } } return .from(abi: result) } @@ -3128,9 +3149,10 @@ internal class IMapString_Base: test_component.IInspectable { } internal func GetViewImpl() throws -> test_component.AnyIMapView? { - var result: UnsafeMutablePointer<__x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBase>? - _ = try perform(as: __x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBase.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIMapView_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseWrapper.unwrapFrom(abi: result) } @@ -3163,7 +3185,7 @@ internal enum __x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseBridge internal typealias CABI = __x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBase internal typealias SwiftABI = IMapString_Base internal typealias SwiftProjection = AnyIMap - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseImpl(abi) } @@ -3180,7 +3202,7 @@ fileprivate class __x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseImp typealias V = Base? typealias Bridge = __x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -3220,7 +3242,7 @@ fileprivate class __x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBaseImp get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIKeyValuePairString_Base = try! _default.QueryInterface() + private lazy var _IIterable: IIterableIKeyValuePairString_Base! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.imap-2.first) fileprivate func first() -> AnyIIterator?>? { try! _IIterable.FirstImpl() @@ -3265,7 +3287,7 @@ internal var __x_ABI_C__FIObservableMap_2_HSTRING_IInspectableVTable: __x_ABI_C_ add_MapChanged: { guard let __unwrapped__instance = __x_ABI_C__FIObservableMap_2_HSTRING_IInspectableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - guard let vhnd = test_component.__x_ABI_C__FMapChangedEventHandler_2_HSTRING_IInspectableWrapper.unwrapFrom(abi: $1) else { return E_INVALIDARG } + guard let vhnd = test_component.__x_ABI_C__FMapChangedEventHandler_2_HSTRING_IInspectableWrapper.unwrapFrom(abi: ComPtr($1)) else { return E_INVALIDARG } let result = __unwrapped__instance.mapChanged.addHandler(vhnd) $2?.initialize(to: .from(swift: result)) return S_OK @@ -3304,7 +3326,7 @@ internal enum __x_ABI_C__FIObservableMap_2_HSTRING_IInspectableBridge : AbiInter internal typealias CABI = __x_ABI_C__FIObservableMap_2_HSTRING_IInspectable internal typealias SwiftABI = IObservableMapString_Any internal typealias SwiftProjection = AnyIObservableMap - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIObservableMap_2_HSTRING_IInspectableImpl(abi) } @@ -3321,7 +3343,7 @@ fileprivate class __x_ABI_C__FIObservableMap_2_HSTRING_IInspectableImpl : IObser typealias T = AnyIKeyValuePair? typealias Bridge = __x_ABI_C__FIObservableMap_2_HSTRING_IInspectableBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -3329,17 +3351,17 @@ fileprivate class __x_ABI_C__FIObservableMap_2_HSTRING_IInspectableImpl : IObser /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.iobservablemap-2.mapchanged) fileprivate lazy var mapChanged : Event> = { .init( - add: { [weak this = _default] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._default else { return .init() } return try! this.add_MapChangedImpl($0) }, - remove: { [weak this = _default] in - try? this?.remove_MapChangedImpl($0) + remove: { [weak self] in + try? self?._default.remove_MapChangedImpl($0) } ) }() - internal lazy var _IMap: IMapString_Any = try! _default.QueryInterface() + private lazy var _IMap: IMapString_Any! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.iobservablemap-2.lookup) fileprivate func lookup(_ key: String) -> Any? { try! _IMap.LookupImpl(key) @@ -3375,7 +3397,7 @@ fileprivate class __x_ABI_C__FIObservableMap_2_HSTRING_IInspectableImpl : IObser get { try! _IMap.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIKeyValuePairString_Any = try! _default.QueryInterface() + private lazy var _IIterable: IIterableIKeyValuePairString_Any! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.iobservablemap-2.first) fileprivate func first() -> AnyIIterator?>? { try! _IIterable.FirstImpl() @@ -3420,7 +3442,7 @@ internal var __x_ABI_C__FIObservableMap_2_HSTRING_HSTRINGVTable: __x_ABI_C__FIOb add_MapChanged: { guard let __unwrapped__instance = __x_ABI_C__FIObservableMap_2_HSTRING_HSTRINGWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - guard let vhnd = test_component.__x_ABI_C__FMapChangedEventHandler_2_HSTRING_HSTRINGWrapper.unwrapFrom(abi: $1) else { return E_INVALIDARG } + guard let vhnd = test_component.__x_ABI_C__FMapChangedEventHandler_2_HSTRING_HSTRINGWrapper.unwrapFrom(abi: ComPtr($1)) else { return E_INVALIDARG } let result = __unwrapped__instance.mapChanged.addHandler(vhnd) $2?.initialize(to: .from(swift: result)) return S_OK @@ -3459,7 +3481,7 @@ internal enum __x_ABI_C__FIObservableMap_2_HSTRING_HSTRINGBridge : AbiInterfaceB internal typealias CABI = __x_ABI_C__FIObservableMap_2_HSTRING_HSTRING internal typealias SwiftABI = IObservableMapString_String internal typealias SwiftProjection = AnyIObservableMap - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIObservableMap_2_HSTRING_HSTRINGImpl(abi) } @@ -3476,7 +3498,7 @@ fileprivate class __x_ABI_C__FIObservableMap_2_HSTRING_HSTRINGImpl : IObservable typealias T = AnyIKeyValuePair? typealias Bridge = __x_ABI_C__FIObservableMap_2_HSTRING_HSTRINGBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -3484,17 +3506,17 @@ fileprivate class __x_ABI_C__FIObservableMap_2_HSTRING_HSTRINGImpl : IObservable /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.iobservablemap-2.mapchanged) fileprivate lazy var mapChanged : Event> = { .init( - add: { [weak this = _default] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._default else { return .init() } return try! this.add_MapChangedImpl($0) }, - remove: { [weak this = _default] in - try? this?.remove_MapChangedImpl($0) + remove: { [weak self] in + try? self?._default.remove_MapChangedImpl($0) } ) }() - internal lazy var _IMap: IMapString_String = try! _default.QueryInterface() + private lazy var _IMap: IMapString_String! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.iobservablemap-2.lookup) fileprivate func lookup(_ key: String) -> String { try! _IMap.LookupImpl(key) @@ -3530,7 +3552,7 @@ fileprivate class __x_ABI_C__FIObservableMap_2_HSTRING_HSTRINGImpl : IObservable get { try! _IMap.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIKeyValuePairString_String = try! _default.QueryInterface() + private lazy var _IIterable: IIterableIKeyValuePairString_String! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.iobservablemap-2.first) fileprivate func first() -> AnyIIterator?>? { try! _IIterable.FirstImpl() @@ -3575,7 +3597,7 @@ internal var __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBaseVTa add_VectorChanged: { guard let __unwrapped__instance = __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBaseWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - guard let vhnd = test_component.__x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent__CBaseWrapper.unwrapFrom(abi: $1) else { return E_INVALIDARG } + guard let vhnd = test_component.__x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent__CBaseWrapper.unwrapFrom(abi: ComPtr($1)) else { return E_INVALIDARG } let result = __unwrapped__instance.vectorChanged.addHandler(vhnd) $2?.initialize(to: .from(swift: result)) return S_OK @@ -3614,7 +3636,7 @@ internal enum __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBaseBr internal typealias CABI = __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBase internal typealias SwiftABI = IObservableVectorBase internal typealias SwiftProjection = AnyIObservableVector - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBaseImpl(abi) } @@ -3629,7 +3651,7 @@ fileprivate class __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBa typealias T = Base? typealias Bridge = __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBaseBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -3667,17 +3689,17 @@ fileprivate class __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBa /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.iobservablevector-1.vectorchanged) fileprivate lazy var vectorChanged : Event> = { .init( - add: { [weak this = _default] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._default else { return .init() } return try! this.add_VectorChangedImpl($0) }, - remove: { [weak this = _default] in - try? this?.remove_VectorChangedImpl($0) + remove: { [weak self] in + try? self?._default.remove_VectorChangedImpl($0) } ) }() - internal lazy var _IVector: IVectorBase = try! _default.QueryInterface() + private lazy var _IVector: IVectorBase! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.iobservablevector-1.getat) fileprivate func getAt(_ index: UInt32) -> Base? { try! _IVector.GetAtImpl(index) @@ -3728,7 +3750,7 @@ fileprivate class __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBa get { try! _IVector.get_SizeImpl() } } - internal lazy var _IIterable: IIterableBase = try! _default.QueryInterface() + private lazy var _IIterable: IIterableBase! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.iobservablevector-1.first) fileprivate func first() -> AnyIIterator? { try! _IIterable.FirstImpl() @@ -3773,7 +3795,7 @@ internal var __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIBasicV add_VectorChanged: { guard let __unwrapped__instance = __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - guard let vhnd = test_component.__x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.unwrapFrom(abi: $1) else { return E_INVALIDARG } + guard let vhnd = test_component.__x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.unwrapFrom(abi: ComPtr($1)) else { return E_INVALIDARG } let result = __unwrapped__instance.vectorChanged.addHandler(vhnd) $2?.initialize(to: .from(swift: result)) return S_OK @@ -3812,7 +3834,7 @@ internal enum __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIBasic internal typealias CABI = __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIBasic internal typealias SwiftABI = IObservableVectorIBasic internal typealias SwiftProjection = AnyIObservableVector - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIBasicImpl(abi) } @@ -3827,7 +3849,7 @@ fileprivate class __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIB typealias T = AnyIBasic? typealias Bridge = __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIBasicBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -3865,17 +3887,17 @@ fileprivate class __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIB /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.iobservablevector-1.vectorchanged) fileprivate lazy var vectorChanged : Event> = { .init( - add: { [weak this = _default] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._default else { return .init() } return try! this.add_VectorChangedImpl($0) }, - remove: { [weak this = _default] in - try? this?.remove_VectorChangedImpl($0) + remove: { [weak self] in + try? self?._default.remove_VectorChangedImpl($0) } ) }() - internal lazy var _IVector: IVectorIBasic = try! _default.QueryInterface() + private lazy var _IVector: IVectorIBasic! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.iobservablevector-1.getat) fileprivate func getAt(_ index: UInt32) -> AnyIBasic? { try! _IVector.GetAtImpl(index) @@ -3926,7 +3948,7 @@ fileprivate class __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIB get { try! _IVector.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIBasic = try! _default.QueryInterface() + private lazy var _IIterable: IIterableIBasic! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.iobservablevector-1.first) fileprivate func first() -> AnyIIterator? { try! _IIterable.FirstImpl() @@ -3986,7 +4008,7 @@ internal var __x_ABI_C__FIVectorView_1_IInspectableVTable: __x_ABI_C__FIVectorVi IndexOf: { guard let __unwrapped__instance = __x_ABI_C__FIVectorView_1_IInspectableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: $1) + let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: ComPtr($1)) var index: UInt32 = 0 let result = __unwrapped__instance.indexOf(value, &index) $2?.initialize(to: index) @@ -4001,9 +4023,10 @@ internal class IVectorViewAny: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIVectorView_1_IInspectable } internal func GetAtImpl(_ index: UInt32) throws -> Any? { - var result: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_C__FIVectorView_1_IInspectable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetAt(pThis, index, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIVectorView_1_IInspectable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetAt(pThis, index, &resultAbi)) + } } return __ABI_.AnyWrapper.unwrapFrom(abi: result) } @@ -4032,7 +4055,7 @@ internal enum __x_ABI_C__FIVectorView_1_IInspectableBridge : AbiInterfaceBridge internal typealias CABI = __x_ABI_C__FIVectorView_1_IInspectable internal typealias SwiftABI = IVectorViewAny internal typealias SwiftProjection = AnyIVectorView - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIVectorView_1_IInspectableImpl(abi) } @@ -4047,7 +4070,7 @@ fileprivate class __x_ABI_C__FIVectorView_1_IInspectableImpl : IVectorView, AbiI typealias T = Any? typealias Bridge = __x_ABI_C__FIVectorView_1_IInspectableBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -4088,7 +4111,7 @@ fileprivate class __x_ABI_C__FIVectorView_1_IInspectableImpl : IVectorView, AbiI get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableAny = try! _default.QueryInterface() + private lazy var _IIterable: IIterableAny! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.ivectorview-1.first) fileprivate func first() -> AnyIIterator? { try! _IIterable.FirstImpl() @@ -4192,7 +4215,7 @@ internal enum __x_ABI_C__FIVectorView_1_HSTRINGBridge : AbiInterfaceBridge { internal typealias CABI = __x_ABI_C__FIVectorView_1_HSTRING internal typealias SwiftABI = IVectorViewString internal typealias SwiftProjection = AnyIVectorView - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIVectorView_1_HSTRINGImpl(abi) } @@ -4207,7 +4230,7 @@ fileprivate class __x_ABI_C__FIVectorView_1_HSTRINGImpl : IVectorView, AbiInterf typealias T = String typealias Bridge = __x_ABI_C__FIVectorView_1_HSTRINGBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -4248,7 +4271,7 @@ fileprivate class __x_ABI_C__FIVectorView_1_HSTRINGImpl : IVectorView, AbiInterf get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableString = try! _default.QueryInterface() + private lazy var _IIterable: IIterableString! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.ivectorview-1.first) fileprivate func first() -> AnyIIterator? { try! _IIterable.FirstImpl() @@ -4307,7 +4330,7 @@ internal var __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBaseVTable: _ IndexOf: { guard let __unwrapped__instance = __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBaseWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let value: test_component.Base? = .from(abi: $1) + let value: test_component.Base? = .from(abi: ComPtr($1)) var index: UInt32 = 0 let result = __unwrapped__instance.indexOf(value, &index) $2?.initialize(to: index) @@ -4322,9 +4345,10 @@ internal class IVectorViewBase: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBase } internal func GetAtImpl(_ index: UInt32) throws -> test_component.Base? { - var result: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBase>? - _ = try perform(as: __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBase.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetAt(pThis, index, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBase.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetAt(pThis, index, &resultAbi)) + } } return .from(abi: result) } @@ -4351,7 +4375,7 @@ internal enum __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBaseBridge : internal typealias CABI = __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBase internal typealias SwiftABI = IVectorViewBase internal typealias SwiftProjection = AnyIVectorView - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBaseImpl(abi) } @@ -4366,7 +4390,7 @@ fileprivate class __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBaseImpl typealias T = Base? typealias Bridge = __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBaseBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -4407,7 +4431,7 @@ fileprivate class __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBaseImpl get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableBase = try! _default.QueryInterface() + private lazy var _IIterable: IIterableBase! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.ivectorview-1.first) fileprivate func first() -> AnyIIterator? { try! _IIterable.FirstImpl() @@ -4467,7 +4491,7 @@ internal var __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasicVTable: IndexOf: { guard let __unwrapped__instance = __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let value: test_component.AnyIBasic? = __ABI_test_component.IBasicWrapper.unwrapFrom(abi: $1) + let value: test_component.AnyIBasic? = __ABI_test_component.IBasicWrapper.unwrapFrom(abi: ComPtr($1)) var index: UInt32 = 0 let result = __unwrapped__instance.indexOf(value, &index) $2?.initialize(to: index) @@ -4482,9 +4506,10 @@ internal class IVectorViewIBasic: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasic } internal func GetAtImpl(_ index: UInt32) throws -> test_component.AnyIBasic? { - var result: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBasic>? - _ = try perform(as: __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasic.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetAt(pThis, index, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasic.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetAt(pThis, index, &resultAbi)) + } } return __ABI_test_component.IBasicWrapper.unwrapFrom(abi: result) } @@ -4513,7 +4538,7 @@ internal enum __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasicBridge internal typealias CABI = __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasic internal typealias SwiftABI = IVectorViewIBasic internal typealias SwiftProjection = AnyIVectorView - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasicImpl(abi) } @@ -4528,7 +4553,7 @@ fileprivate class __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasicIm typealias T = AnyIBasic? typealias Bridge = __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasicBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -4569,7 +4594,7 @@ fileprivate class __x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasicIm get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIBasic = try! _default.QueryInterface() + private lazy var _IIterable: IIterableIBasic! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.ivectorview-1.first) fileprivate func first() -> AnyIIterator? { try! _IIterable.FirstImpl() @@ -4637,7 +4662,7 @@ internal var __x_ABI_C__FIVector_1_IInspectableVTable: __x_ABI_C__FIVector_1_IIn IndexOf: { guard let __unwrapped__instance = __x_ABI_C__FIVector_1_IInspectableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: $1) + let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: ComPtr($1)) var index: UInt32 = 0 let result = __unwrapped__instance.indexOf(value, &index) $2?.initialize(to: index) @@ -4648,7 +4673,7 @@ internal var __x_ABI_C__FIVector_1_IInspectableVTable: __x_ABI_C__FIVector_1_IIn SetAt: { guard let __unwrapped__instance = __x_ABI_C__FIVector_1_IInspectableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } let index: UInt32 = $1 - let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: $2) + let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: ComPtr($2)) __unwrapped__instance.setAt(index, value) return S_OK }, @@ -4656,7 +4681,7 @@ internal var __x_ABI_C__FIVector_1_IInspectableVTable: __x_ABI_C__FIVector_1_IIn InsertAt: { guard let __unwrapped__instance = __x_ABI_C__FIVector_1_IInspectableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } let index: UInt32 = $1 - let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: $2) + let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: ComPtr($2)) __unwrapped__instance.insertAt(index, value) return S_OK }, @@ -4670,7 +4695,7 @@ internal var __x_ABI_C__FIVector_1_IInspectableVTable: __x_ABI_C__FIVector_1_IIn Append: { guard let __unwrapped__instance = __x_ABI_C__FIVector_1_IInspectableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: $1) + let value: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: ComPtr($1)) __unwrapped__instance.append(value) return S_OK }, @@ -4696,9 +4721,10 @@ internal class IVectorAny: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIVector_1_IInspectable } internal func GetAtImpl(_ index: UInt32) throws -> Any? { - var result: UnsafeMutablePointer? - _ = try perform(as: __x_ABI_C__FIVector_1_IInspectable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetAt(pThis, index, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIVector_1_IInspectable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetAt(pThis, index, &resultAbi)) + } } return __ABI_.AnyWrapper.unwrapFrom(abi: result) } @@ -4712,9 +4738,10 @@ internal class IVectorAny: test_component.IInspectable { } internal func GetViewImpl() throws -> test_component.AnyIVectorView? { - var result: UnsafeMutablePointer<__x_ABI_C__FIVectorView_1_IInspectable>? - _ = try perform(as: __x_ABI_C__FIVector_1_IInspectable.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIVector_1_IInspectable.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIVectorView_1_IInspectableWrapper.unwrapFrom(abi: result) } @@ -4777,7 +4804,7 @@ internal enum __x_ABI_C__FIVector_1_IInspectableBridge : AbiInterfaceBridge { internal typealias CABI = __x_ABI_C__FIVector_1_IInspectable internal typealias SwiftABI = IVectorAny internal typealias SwiftProjection = AnyIVector - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIVector_1_IInspectableImpl(abi) } @@ -4792,7 +4819,7 @@ fileprivate class __x_ABI_C__FIVector_1_IInspectableImpl : IVector, AbiInterface typealias T = Any? typealias Bridge = __x_ABI_C__FIVector_1_IInspectableBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -4877,7 +4904,7 @@ fileprivate class __x_ABI_C__FIVector_1_IInspectableImpl : IVector, AbiInterface get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableAny = try! _default.QueryInterface() + private lazy var _IIterable: IIterableAny! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.ivector-1.first) fileprivate func first() -> AnyIIterator? { try! _IIterable.FirstImpl() @@ -5019,9 +5046,10 @@ internal class IVectorString: test_component.IInspectable { } internal func GetViewImpl() throws -> test_component.AnyIVectorView? { - var result: UnsafeMutablePointer<__x_ABI_C__FIVectorView_1_HSTRING>? - _ = try perform(as: __x_ABI_C__FIVector_1_HSTRING.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIVector_1_HSTRING.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIVectorView_1_HSTRINGWrapper.unwrapFrom(abi: result) } @@ -5080,7 +5108,7 @@ internal enum __x_ABI_C__FIVector_1_HSTRINGBridge : AbiInterfaceBridge { internal typealias CABI = __x_ABI_C__FIVector_1_HSTRING internal typealias SwiftABI = IVectorString internal typealias SwiftProjection = AnyIVector - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIVector_1_HSTRINGImpl(abi) } @@ -5095,7 +5123,7 @@ fileprivate class __x_ABI_C__FIVector_1_HSTRINGImpl : IVector, AbiInterfaceImpl typealias T = String typealias Bridge = __x_ABI_C__FIVector_1_HSTRINGBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -5180,7 +5208,7 @@ fileprivate class __x_ABI_C__FIVector_1_HSTRINGImpl : IVector, AbiInterfaceImpl get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableString = try! _default.QueryInterface() + private lazy var _IIterable: IIterableString! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.ivector-1.first) fileprivate func first() -> AnyIIterator? { try! _IIterable.FirstImpl() @@ -5247,7 +5275,7 @@ internal var __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseVTable: __x_A IndexOf: { guard let __unwrapped__instance = __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let value: test_component.Base? = .from(abi: $1) + let value: test_component.Base? = .from(abi: ComPtr($1)) var index: UInt32 = 0 let result = __unwrapped__instance.indexOf(value, &index) $2?.initialize(to: index) @@ -5258,7 +5286,7 @@ internal var __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseVTable: __x_A SetAt: { guard let __unwrapped__instance = __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } let index: UInt32 = $1 - let value: test_component.Base? = .from(abi: $2) + let value: test_component.Base? = .from(abi: ComPtr($2)) __unwrapped__instance.setAt(index, value) return S_OK }, @@ -5266,7 +5294,7 @@ internal var __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseVTable: __x_A InsertAt: { guard let __unwrapped__instance = __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } let index: UInt32 = $1 - let value: test_component.Base? = .from(abi: $2) + let value: test_component.Base? = .from(abi: ComPtr($2)) __unwrapped__instance.insertAt(index, value) return S_OK }, @@ -5280,7 +5308,7 @@ internal var __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseVTable: __x_A Append: { guard let __unwrapped__instance = __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let value: test_component.Base? = .from(abi: $1) + let value: test_component.Base? = .from(abi: ComPtr($1)) __unwrapped__instance.append(value) return S_OK }, @@ -5306,9 +5334,10 @@ internal class IVectorBase: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBase } internal func GetAtImpl(_ index: UInt32) throws -> test_component.Base? { - var result: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBase>? - _ = try perform(as: __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBase.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetAt(pThis, index, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBase.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetAt(pThis, index, &resultAbi)) + } } return .from(abi: result) } @@ -5322,9 +5351,10 @@ internal class IVectorBase: test_component.IInspectable { } internal func GetViewImpl() throws -> test_component.AnyIVectorView? { - var result: UnsafeMutablePointer<__x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBase>? - _ = try perform(as: __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBase.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBase.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CBaseWrapper.unwrapFrom(abi: result) } @@ -5379,7 +5409,7 @@ internal enum __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseBridge : Abi internal typealias CABI = __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBase internal typealias SwiftABI = IVectorBase internal typealias SwiftProjection = AnyIVector - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseImpl(abi) } @@ -5394,7 +5424,7 @@ fileprivate class __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseImpl : I typealias T = Base? typealias Bridge = __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -5479,7 +5509,7 @@ fileprivate class __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBaseImpl : I get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableBase = try! _default.QueryInterface() + private lazy var _IIterable: IIterableBase! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.ivector-1.first) fileprivate func first() -> AnyIIterator? { try! _IIterable.FirstImpl() @@ -5547,7 +5577,7 @@ internal var __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicVTable: __x IndexOf: { guard let __unwrapped__instance = __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let value: test_component.AnyIBasic? = __ABI_test_component.IBasicWrapper.unwrapFrom(abi: $1) + let value: test_component.AnyIBasic? = __ABI_test_component.IBasicWrapper.unwrapFrom(abi: ComPtr($1)) var index: UInt32 = 0 let result = __unwrapped__instance.indexOf(value, &index) $2?.initialize(to: index) @@ -5558,7 +5588,7 @@ internal var __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicVTable: __x SetAt: { guard let __unwrapped__instance = __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } let index: UInt32 = $1 - let value: test_component.AnyIBasic? = __ABI_test_component.IBasicWrapper.unwrapFrom(abi: $2) + let value: test_component.AnyIBasic? = __ABI_test_component.IBasicWrapper.unwrapFrom(abi: ComPtr($2)) __unwrapped__instance.setAt(index, value) return S_OK }, @@ -5566,7 +5596,7 @@ internal var __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicVTable: __x InsertAt: { guard let __unwrapped__instance = __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } let index: UInt32 = $1 - let value: test_component.AnyIBasic? = __ABI_test_component.IBasicWrapper.unwrapFrom(abi: $2) + let value: test_component.AnyIBasic? = __ABI_test_component.IBasicWrapper.unwrapFrom(abi: ComPtr($2)) __unwrapped__instance.insertAt(index, value) return S_OK }, @@ -5580,7 +5610,7 @@ internal var __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicVTable: __x Append: { guard let __unwrapped__instance = __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let value: test_component.AnyIBasic? = __ABI_test_component.IBasicWrapper.unwrapFrom(abi: $1) + let value: test_component.AnyIBasic? = __ABI_test_component.IBasicWrapper.unwrapFrom(abi: ComPtr($1)) __unwrapped__instance.append(value) return S_OK }, @@ -5606,9 +5636,10 @@ internal class IVectorIBasic: test_component.IInspectable { override public class var IID: test_component.IID { IID___x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasic } internal func GetAtImpl(_ index: UInt32) throws -> test_component.AnyIBasic? { - var result: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBasic>? - _ = try perform(as: __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasic.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetAt(pThis, index, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasic.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetAt(pThis, index, &resultAbi)) + } } return __ABI_test_component.IBasicWrapper.unwrapFrom(abi: result) } @@ -5622,9 +5653,10 @@ internal class IVectorIBasic: test_component.IInspectable { } internal func GetViewImpl() throws -> test_component.AnyIVectorView? { - var result: UnsafeMutablePointer<__x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasic>? - _ = try perform(as: __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasic.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasic.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.GetView(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIVectorView_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.unwrapFrom(abi: result) } @@ -5687,7 +5719,7 @@ internal enum __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicBridge : A internal typealias CABI = __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasic internal typealias SwiftABI = IVectorIBasic internal typealias SwiftProjection = AnyIVector - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicImpl(abi) } @@ -5702,7 +5734,7 @@ fileprivate class __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicImpl : typealias T = AnyIBasic? typealias Bridge = __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -5787,7 +5819,7 @@ fileprivate class __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CIBasicImpl : get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIBasic = try! _default.QueryInterface() + private lazy var _IIterable: IIterableIBasic! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.collections.ivector-1.first) fileprivate func first() -> AnyIIterator? { try! _IIterable.FirstImpl() @@ -5813,8 +5845,8 @@ internal var __x_ABI_C__FMapChangedEventHandler_2_HSTRING_IInspectableVTable: __ Release: { __x_ABI_C__FMapChangedEventHandler_2_HSTRING_IInspectableWrapper.release($0) }, Invoke: { guard let __unwrapped__instance = __x_ABI_C__FMapChangedEventHandler_2_HSTRING_IInspectableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let sender: test_component.AnyIObservableMap? = test_component.__x_ABI_C__FIObservableMap_2_HSTRING_IInspectableWrapper.unwrapFrom(abi: $1) - let event: test_component.AnyIMapChangedEventArgs? = test_component.__x_ABI_C__FIMapChangedEventArgs_1_HSTRINGWrapper.unwrapFrom(abi: $2) + let sender: test_component.AnyIObservableMap? = test_component.__x_ABI_C__FIObservableMap_2_HSTRING_IInspectableWrapper.unwrapFrom(abi: ComPtr($1)) + let event: test_component.AnyIMapChangedEventArgs? = test_component.__x_ABI_C__FIMapChangedEventArgs_1_HSTRINGWrapper.unwrapFrom(abi: ComPtr($2)) __unwrapped__instance(sender, event) return S_OK } @@ -5840,7 +5872,7 @@ internal class __x_ABI_C__FMapChangedEventHandler_2_HSTRING_IInspectableBridge : internal typealias CABI = __x_ABI_C__FMapChangedEventHandler_2_HSTRING_IInspectable internal typealias SwiftABI = test_component.MapChangedEventHandlerString_Any - internal static func from(abi: UnsafeMutablePointer?) -> Handler? { + internal static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (sender, event) in @@ -5866,8 +5898,8 @@ internal var __x_ABI_C__FMapChangedEventHandler_2_HSTRING_HSTRINGVTable: __x_ABI Release: { __x_ABI_C__FMapChangedEventHandler_2_HSTRING_HSTRINGWrapper.release($0) }, Invoke: { guard let __unwrapped__instance = __x_ABI_C__FMapChangedEventHandler_2_HSTRING_HSTRINGWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let sender: test_component.AnyIObservableMap? = test_component.__x_ABI_C__FIObservableMap_2_HSTRING_HSTRINGWrapper.unwrapFrom(abi: $1) - let event: test_component.AnyIMapChangedEventArgs? = test_component.__x_ABI_C__FIMapChangedEventArgs_1_HSTRINGWrapper.unwrapFrom(abi: $2) + let sender: test_component.AnyIObservableMap? = test_component.__x_ABI_C__FIObservableMap_2_HSTRING_HSTRINGWrapper.unwrapFrom(abi: ComPtr($1)) + let event: test_component.AnyIMapChangedEventArgs? = test_component.__x_ABI_C__FIMapChangedEventArgs_1_HSTRINGWrapper.unwrapFrom(abi: ComPtr($2)) __unwrapped__instance(sender, event) return S_OK } @@ -5893,7 +5925,7 @@ internal class __x_ABI_C__FMapChangedEventHandler_2_HSTRING_HSTRINGBridge : WinR internal typealias CABI = __x_ABI_C__FMapChangedEventHandler_2_HSTRING_HSTRING internal typealias SwiftABI = test_component.MapChangedEventHandlerString_String - internal static func from(abi: UnsafeMutablePointer?) -> Handler? { + internal static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (sender, event) in @@ -5919,8 +5951,8 @@ internal var __x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent__ Release: { __x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent__CBaseWrapper.release($0) }, Invoke: { guard let __unwrapped__instance = __x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent__CBaseWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let sender: test_component.AnyIObservableVector? = test_component.__x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBaseWrapper.unwrapFrom(abi: $1) - let event: test_component.AnyIVectorChangedEventArgs? = __ABI_Windows_Foundation_Collections.IVectorChangedEventArgsWrapper.unwrapFrom(abi: $2) + let sender: test_component.AnyIObservableVector? = test_component.__x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBaseWrapper.unwrapFrom(abi: ComPtr($1)) + let event: test_component.AnyIVectorChangedEventArgs? = __ABI_Windows_Foundation_Collections.IVectorChangedEventArgsWrapper.unwrapFrom(abi: ComPtr($2)) __unwrapped__instance(sender, event) return S_OK } @@ -5946,7 +5978,7 @@ internal class __x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent internal typealias CABI = __x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent__CBase internal typealias SwiftABI = test_component.VectorChangedEventHandlerBase - internal static func from(abi: UnsafeMutablePointer?) -> Handler? { + internal static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (sender, event) in @@ -5972,8 +6004,8 @@ internal var __x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent__ Release: { __x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.release($0) }, Invoke: { guard let __unwrapped__instance = __x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let sender: test_component.AnyIObservableVector? = test_component.__x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.unwrapFrom(abi: $1) - let event: test_component.AnyIVectorChangedEventArgs? = __ABI_Windows_Foundation_Collections.IVectorChangedEventArgsWrapper.unwrapFrom(abi: $2) + let sender: test_component.AnyIObservableVector? = test_component.__x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CIBasicWrapper.unwrapFrom(abi: ComPtr($1)) + let event: test_component.AnyIVectorChangedEventArgs? = __ABI_Windows_Foundation_Collections.IVectorChangedEventArgsWrapper.unwrapFrom(abi: ComPtr($2)) __unwrapped__instance(sender, event) return S_OK } @@ -5999,7 +6031,7 @@ internal class __x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent internal typealias CABI = __x_ABI_C__FVectorChangedEventHandler_1___x_ABI_Ctest__zcomponent__CIBasic internal typealias SwiftABI = test_component.VectorChangedEventHandlerIBasic - internal static func from(abi: UnsafeMutablePointer?) -> Handler? { + internal static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (sender, event) in @@ -6025,8 +6057,8 @@ internal var __x_ABI_C__FIEventHandler_1_IInspectableVTable: __x_ABI_C__FIEventH Release: { __x_ABI_C__FIEventHandler_1_IInspectableWrapper.release($0) }, Invoke: { guard let __unwrapped__instance = __x_ABI_C__FIEventHandler_1_IInspectableWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let sender: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: $1) - let args: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: $2) + let sender: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: ComPtr($1)) + let args: Any? = __ABI_.AnyWrapper.unwrapFrom(abi: ComPtr($2)) __unwrapped__instance(sender, args) return S_OK } @@ -6052,7 +6084,7 @@ internal class __x_ABI_C__FIEventHandler_1_IInspectableBridge : WinRTDelegateBri internal typealias CABI = __x_ABI_C__FIEventHandler_1_IInspectable internal typealias SwiftABI = test_component.EventHandlerAny - internal static func from(abi: UnsafeMutablePointer?) -> Handler? { + internal static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (sender, args) in @@ -6096,7 +6128,7 @@ internal var __x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleVTable: __x_ABI put_Progress: { guard let __unwrapped__instance = __x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - guard let handler = test_component.__x_ABI_C__FIAsyncOperationProgressHandler_2_int_doubleWrapper.unwrapFrom(abi: $1) else { return E_INVALIDARG } + guard let handler = test_component.__x_ABI_C__FIAsyncOperationProgressHandler_2_int_doubleWrapper.unwrapFrom(abi: ComPtr($1)) else { return E_INVALIDARG } __unwrapped__instance.progress = handler return S_OK }, @@ -6111,7 +6143,7 @@ internal var __x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleVTable: __x_ABI put_Completed: { guard let __unwrapped__instance = __x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - guard let handler = test_component.__x_ABI_C__FIAsyncOperationWithProgressCompletedHandler_2_int_doubleWrapper.unwrapFrom(abi: $1) else { return E_INVALIDARG } + guard let handler = test_component.__x_ABI_C__FIAsyncOperationWithProgressCompletedHandler_2_int_doubleWrapper.unwrapFrom(abi: ComPtr($1)) else { return E_INVALIDARG } __unwrapped__instance.completed = handler return S_OK }, @@ -6146,9 +6178,10 @@ internal class IAsyncOperationWithProgressInt32_Double: test_component.IInspecta } internal func get_ProgressImpl() throws -> AsyncOperationProgressHandler? { - var result: UnsafeMutablePointer<__x_ABI_C__FIAsyncOperationProgressHandler_2_int_double>? - _ = try perform(as: __x_ABI_C__FIAsyncOperationWithProgress_2_int_double.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Progress(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIAsyncOperationWithProgress_2_int_double.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Progress(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIAsyncOperationProgressHandler_2_int_doubleWrapper.unwrapFrom(abi: result) } @@ -6162,9 +6195,10 @@ internal class IAsyncOperationWithProgressInt32_Double: test_component.IInspecta } internal func get_CompletedImpl() throws -> AsyncOperationWithProgressCompletedHandler? { - var result: UnsafeMutablePointer<__x_ABI_C__FIAsyncOperationWithProgressCompletedHandler_2_int_double>? - _ = try perform(as: __x_ABI_C__FIAsyncOperationWithProgress_2_int_double.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Completed(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIAsyncOperationWithProgress_2_int_double.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Completed(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIAsyncOperationWithProgressCompletedHandler_2_int_doubleWrapper.unwrapFrom(abi: result) } @@ -6183,7 +6217,7 @@ internal enum __x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleBridge : AbiIn internal typealias CABI = __x_ABI_C__FIAsyncOperationWithProgress_2_int_double internal typealias SwiftABI = IAsyncOperationWithProgressInt32_Double internal typealias SwiftProjection = AnyIAsyncOperationWithProgress - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleImpl(abi) } @@ -6199,7 +6233,7 @@ fileprivate class __x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleImpl : IAs typealias TProgress = Double typealias Bridge = __x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -6221,7 +6255,7 @@ fileprivate class __x_ABI_C__FIAsyncOperationWithProgress_2_int_doubleImpl : IAs set { try! _default.put_CompletedImpl(newValue) } } - internal lazy var _IAsyncInfo: __ABI_Windows_Foundation.IAsyncInfo = try! _default.QueryInterface() + private lazy var _IAsyncInfo: __ABI_Windows_Foundation.IAsyncInfo! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.iasyncoperationwithprogress-2.cancel) fileprivate func cancel() throws { try _IAsyncInfo.CancelImpl() @@ -6285,7 +6319,7 @@ internal var __x_ABI_C__FIAsyncOperation_1_intVTable: __x_ABI_C__FIAsyncOperatio put_Completed: { guard let __unwrapped__instance = __x_ABI_C__FIAsyncOperation_1_intWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - guard let handler = test_component.__x_ABI_C__FIAsyncOperationCompletedHandler_1_intWrapper.unwrapFrom(abi: $1) else { return E_INVALIDARG } + guard let handler = test_component.__x_ABI_C__FIAsyncOperationCompletedHandler_1_intWrapper.unwrapFrom(abi: ComPtr($1)) else { return E_INVALIDARG } __unwrapped__instance.completed = handler return S_OK }, @@ -6320,9 +6354,10 @@ internal class IAsyncOperationInt32: test_component.IInspectable { } internal func get_CompletedImpl() throws -> AsyncOperationCompletedHandler? { - var result: UnsafeMutablePointer<__x_ABI_C__FIAsyncOperationCompletedHandler_1_int>? - _ = try perform(as: __x_ABI_C__FIAsyncOperation_1_int.self) { pThis in - try CHECKED(pThis.pointee.lpVtbl.pointee.get_Completed(pThis, &result)) + let (result) = try ComPtrs.initialize { resultAbi in + _ = try perform(as: __x_ABI_C__FIAsyncOperation_1_int.self) { pThis in + try CHECKED(pThis.pointee.lpVtbl.pointee.get_Completed(pThis, &resultAbi)) + } } return test_component.__x_ABI_C__FIAsyncOperationCompletedHandler_1_intWrapper.unwrapFrom(abi: result) } @@ -6341,7 +6376,7 @@ internal enum __x_ABI_C__FIAsyncOperation_1_intBridge : AbiInterfaceBridge { internal typealias CABI = __x_ABI_C__FIAsyncOperation_1_int internal typealias SwiftABI = IAsyncOperationInt32 internal typealias SwiftProjection = AnyIAsyncOperation - internal static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + internal static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return __x_ABI_C__FIAsyncOperation_1_intImpl(abi) } @@ -6356,7 +6391,7 @@ fileprivate class __x_ABI_C__FIAsyncOperation_1_intImpl : IAsyncOperation, AbiIn typealias TResult = Int32 typealias Bridge = __x_ABI_C__FIAsyncOperation_1_intBridge let _default: Bridge.SwiftABI - init(_ fromAbi: UnsafeMutablePointer) { + init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -6372,7 +6407,7 @@ fileprivate class __x_ABI_C__FIAsyncOperation_1_intImpl : IAsyncOperation, AbiIn set { try! _default.put_CompletedImpl(newValue) } } - internal lazy var _IAsyncInfo: __ABI_Windows_Foundation.IAsyncInfo = try! _default.QueryInterface() + private lazy var _IAsyncInfo: __ABI_Windows_Foundation.IAsyncInfo! = getInterfaceForCaching() /// [Open Microsoft documentation](https://learn.microsoft.com/uwp/api/windows.foundation.iasyncoperation-1.cancel) fileprivate func cancel() throws { try _IAsyncInfo.CancelImpl() @@ -6410,10 +6445,10 @@ internal enum __x_ABI_C__FIReference_1_GUIDBridge: ReferenceBridge { typealias SwiftProjection = GUID static var IID: test_component.IID { IID___x_ABI_C__FIReference_1_GUID } - static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + static func from(abi: ComPtr?) -> SwiftProjection? { guard let val = abi else { return nil } var result: GUID = .init() - try! CHECKED(val.pointee.lpVtbl.pointee.get_Value(val, &result)) + try! CHECKED(val.get().pointee.lpVtbl.pointee.get_Value(val.get(), &result)) return result } @@ -6468,10 +6503,10 @@ internal enum __x_ABI_C__FIReference_1_intBridge: ReferenceBridge { typealias SwiftProjection = Int32 static var IID: test_component.IID { IID___x_ABI_C__FIReference_1_int } - static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + static func from(abi: ComPtr?) -> SwiftProjection? { guard let val = abi else { return nil } var result: INT32 = 0 - try! CHECKED(val.pointee.lpVtbl.pointee.get_Value(val, &result)) + try! CHECKED(val.get().pointee.lpVtbl.pointee.get_Value(val.get(), &result)) return result } @@ -6526,10 +6561,10 @@ internal enum __x_ABI_C__FIReference_1___x_ABI_Ctest__zcomponent__CSignedBridge: typealias SwiftProjection = test_component.Signed static var IID: test_component.IID { IID___x_ABI_C__FIReference_1___x_ABI_Ctest__zcomponent__CSigned } - static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + static func from(abi: ComPtr?) -> SwiftProjection? { guard let val = abi else { return nil } var result: Signed = .init(0) - try! CHECKED(val.pointee.lpVtbl.pointee.get_Value(val, &result)) + try! CHECKED(val.get().pointee.lpVtbl.pointee.get_Value(val.get(), &result)) return result } @@ -6592,8 +6627,8 @@ internal var __x_ABI_C__FITypedEventHandler_2___x_ABI_Ctest__zcomponent__CClass_ Release: { __x_ABI_C__FITypedEventHandler_2___x_ABI_Ctest__zcomponent__CClass___x_ABI_Ctest__zcomponent__CDeferrableEventArgsWrapper.release($0) }, Invoke: { guard let __unwrapped__instance = __x_ABI_C__FITypedEventHandler_2___x_ABI_Ctest__zcomponent__CClass___x_ABI_Ctest__zcomponent__CDeferrableEventArgsWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let sender: test_component.Class? = .from(abi: $1) - let args: test_component.DeferrableEventArgs? = .from(abi: $2) + let sender: test_component.Class? = .from(abi: ComPtr($1)) + let args: test_component.DeferrableEventArgs? = .from(abi: ComPtr($2)) __unwrapped__instance(sender, args) return S_OK } @@ -6615,7 +6650,7 @@ internal class __x_ABI_C__FITypedEventHandler_2___x_ABI_Ctest__zcomponent__CClas internal typealias CABI = __x_ABI_C__FITypedEventHandler_2___x_ABI_Ctest__zcomponent__CClass___x_ABI_Ctest__zcomponent__CDeferrableEventArgs internal typealias SwiftABI = test_component.TypedEventHandlerClass_DeferrableEventArgs - internal static func from(abi: UnsafeMutablePointer?) -> Handler? { + internal static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (sender, args) in @@ -6641,7 +6676,7 @@ internal var __x_ABI_C__FITypedEventHandler_2___x_ABI_Ctest__zcomponent__CSimple Release: { __x_ABI_C__FITypedEventHandler_2___x_ABI_Ctest__zcomponent__CSimple___x_ABI_Ctest__zcomponent__CSimpleEventArgsWrapper.release($0) }, Invoke: { guard let __unwrapped__instance = __x_ABI_C__FITypedEventHandler_2___x_ABI_Ctest__zcomponent__CSimple___x_ABI_Ctest__zcomponent__CSimpleEventArgsWrapper.tryUnwrapFrom(raw: $0) else { return E_INVALIDARG } - let sender: test_component.Simple? = .from(abi: $1) + let sender: test_component.Simple? = .from(abi: ComPtr($1)) let args: test_component.SimpleEventArgs = .from(abi: $2) __unwrapped__instance(sender, args) return S_OK @@ -6664,7 +6699,7 @@ internal class __x_ABI_C__FITypedEventHandler_2___x_ABI_Ctest__zcomponent__CSimp internal typealias CABI = __x_ABI_C__FITypedEventHandler_2___x_ABI_Ctest__zcomponent__CSimple___x_ABI_Ctest__zcomponent__CSimpleEventArgs internal typealias SwiftABI = test_component.TypedEventHandlerSimple_SimpleEventArgs - internal static func from(abi: UnsafeMutablePointer?) -> Handler? { + internal static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (sender, args) in diff --git a/tests/test_component/Sources/test_component/test_component+Impl.swift b/tests/test_component/Sources/test_component/test_component+Impl.swift index 2ba3131c..fd34877b 100644 --- a/tests/test_component/Sources/test_component/test_component+Impl.swift +++ b/tests/test_component/Sources/test_component/test_component+Impl.swift @@ -7,7 +7,7 @@ public enum __IMPL_test_component { public typealias CABI = __x_ABI_Ctest__component_CIAsyncMethodsWithProgress public typealias SwiftABI = __ABI_test_component.IAsyncMethodsWithProgress public typealias SwiftProjection = AnyIAsyncMethodsWithProgress - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return IAsyncMethodsWithProgressImpl(abi) } @@ -22,7 +22,7 @@ public enum __IMPL_test_component { fileprivate typealias Bridge = IAsyncMethodsWithProgressBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -36,7 +36,7 @@ public enum __IMPL_test_component { public typealias CABI = __x_ABI_Ctest__component_CIAsyncOperationInt public typealias SwiftABI = __ABI_test_component.IAsyncOperationInt public typealias SwiftProjection = AnyIAsyncOperationInt - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return IAsyncOperationIntImpl(abi) } @@ -51,7 +51,7 @@ public enum __IMPL_test_component { fileprivate typealias Bridge = IAsyncOperationIntBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -69,7 +69,7 @@ public enum __IMPL_test_component { public typealias CABI = __x_ABI_Ctest__component_CIBasic public typealias SwiftABI = __ABI_test_component.IBasic public typealias SwiftProjection = AnyIBasic - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return IBasicImpl(abi) } @@ -84,7 +84,7 @@ public enum __IMPL_test_component { fileprivate typealias Bridge = IBasicBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -98,7 +98,7 @@ public enum __IMPL_test_component { public typealias CABI = __x_ABI_Ctest__component_CIIAmImplementable public typealias SwiftABI = __ABI_test_component.IIAmImplementable public typealias SwiftProjection = AnyIIAmImplementable - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return IIAmImplementableImpl(abi) } @@ -113,7 +113,7 @@ public enum __IMPL_test_component { fileprivate typealias Bridge = IIAmImplementableBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -181,12 +181,12 @@ public enum __IMPL_test_component { fileprivate lazy var implementableEvent : Event = { .init( - add: { [weak this = _default] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._default else { return .init() } return try! this.add_ImplementableEventImpl($0) }, - remove: { [weak this = _default] in - try? this?.remove_ImplementableEventImpl($0) + remove: { [weak self] in + try? self?._default.remove_ImplementableEventImpl($0) } ) }() @@ -197,7 +197,7 @@ public enum __IMPL_test_component { public typealias CABI = __x_ABI_Ctest__component_CIInterfaceWithObservableVector public typealias SwiftABI = __ABI_test_component.IInterfaceWithObservableVector public typealias SwiftProjection = AnyIInterfaceWithObservableVector - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return IInterfaceWithObservableVectorImpl(abi) } @@ -212,7 +212,7 @@ public enum __IMPL_test_component { fileprivate typealias Bridge = IInterfaceWithObservableVectorBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -226,7 +226,7 @@ public enum __IMPL_test_component { public typealias CABI = __x_ABI_Ctest__component_CISimpleDelegate public typealias SwiftABI = __ABI_test_component.ISimpleDelegate public typealias SwiftProjection = AnyISimpleDelegate - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return ISimpleDelegateImpl(abi) } @@ -241,7 +241,7 @@ public enum __IMPL_test_component { fileprivate typealias Bridge = ISimpleDelegateBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -259,7 +259,7 @@ public enum __IMPL_test_component { public typealias CABI = __x_ABI_Ctest__component_CInterfaceWithReturnDelegate public typealias SwiftABI = __ABI_test_component.InterfaceWithReturnDelegate public typealias SwiftProjection = AnyInterfaceWithReturnDelegate - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return InterfaceWithReturnDelegateImpl(abi) } @@ -274,18 +274,18 @@ public enum __IMPL_test_component { fileprivate typealias Bridge = InterfaceWithReturnDelegateBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } fileprivate lazy var eventWithReturn : Event = { .init( - add: { [weak this = _default] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._default else { return .init() } return try! this.add_EventWithReturnImpl($0) }, - remove: { [weak this = _default] in - try? this?.remove_EventWithReturnImpl($0) + remove: { [weak self] in + try? self?._default.remove_EventWithReturnImpl($0) } ) }() @@ -296,7 +296,7 @@ public enum __IMPL_test_component { public typealias CABI = __x_ABI_Ctest__component_CWithKeyword public typealias SwiftABI = __ABI_test_component.WithKeyword public typealias SwiftProjection = AnyWithKeyword - public static func from(abi: UnsafeMutablePointer?) -> SwiftProjection? { + public static func from(abi: ComPtr?) -> SwiftProjection? { guard let abi = abi else { return nil } return WithKeywordImpl(abi) } @@ -311,7 +311,7 @@ public enum __IMPL_test_component { fileprivate typealias Bridge = WithKeywordBridge fileprivate let _default: Bridge.SwiftABI fileprivate var thisPtr: test_component.IInspectable { _default } - fileprivate init(_ fromAbi: UnsafeMutablePointer) { + fileprivate init(_ fromAbi: ComPtr) { _default = Bridge.SwiftABI(fromAbi) } @@ -326,12 +326,12 @@ public enum __IMPL_test_component { fileprivate lazy var `repeat` : Event> = { .init( - add: { [weak this = _default] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._default else { return .init() } return try! this.add_RepeatImpl($0) }, - remove: { [weak this = _default] in - try? this?.remove_RepeatImpl($0) + remove: { [weak self] in + try? self?._default.remove_RepeatImpl($0) } ) }() @@ -343,7 +343,7 @@ public enum __IMPL_test_component { public typealias CABI = __x_ABI_Ctest__component_CIObjectHandler public typealias SwiftABI = __ABI_test_component.ObjectHandler - public static func from(abi: UnsafeMutablePointer?) -> Handler? { + public static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (item) in @@ -357,7 +357,7 @@ public enum __IMPL_test_component { public typealias CABI = __x_ABI_Ctest__component_CIVoidToVoidDelegate public typealias SwiftABI = __ABI_test_component.VoidToVoidDelegate - public static func from(abi: UnsafeMutablePointer?) -> Handler? { + public static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { () in diff --git a/tests/test_component/Sources/test_component/test_component.Delegates+Impl.swift b/tests/test_component/Sources/test_component/test_component.Delegates+Impl.swift index c12e8278..4aca4452 100644 --- a/tests/test_component/Sources/test_component/test_component.Delegates+Impl.swift +++ b/tests/test_component/Sources/test_component/test_component.Delegates+Impl.swift @@ -8,7 +8,7 @@ public enum __IMPL_test_component_Delegates { public typealias CABI = __x_ABI_Ctest__component_CDelegates_CIInDelegate public typealias SwiftABI = __ABI_test_component_Delegates.InDelegate - public static func from(abi: UnsafeMutablePointer?) -> Handler? { + public static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { (value) in @@ -22,7 +22,7 @@ public enum __IMPL_test_component_Delegates { public typealias CABI = __x_ABI_Ctest__component_CDelegates_CIReturnInt32Delegate public typealias SwiftABI = __ABI_test_component_Delegates.ReturnInt32Delegate - public static func from(abi: UnsafeMutablePointer?) -> Handler? { + public static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { () in @@ -36,7 +36,7 @@ public enum __IMPL_test_component_Delegates { public typealias CABI = __x_ABI_Ctest__component_CDelegates_CISignalDelegate public typealias SwiftABI = __ABI_test_component_Delegates.SignalDelegate - public static func from(abi: UnsafeMutablePointer?) -> Handler? { + public static func from(abi: ComPtr?) -> Handler? { guard let abi = abi else { return nil } let _default = SwiftABI(abi) let handler: Handler = { () in diff --git a/tests/test_component/Sources/test_component/test_component.swift b/tests/test_component/Sources/test_component/test_component.swift index 75f9fa46..6a611fa6 100644 --- a/tests/test_component/Sources/test_component/test_component.swift +++ b/tests/test_component/Sources/test_component/test_component.swift @@ -24,41 +24,31 @@ public final class AsyncMethods { } public final class AsyncOperationInt : WinRTClass, IAsyncOperationInt, IAsyncOperation, test_component.IAsyncInfo { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! public typealias TResult = Int32 private typealias SwiftABI = __ABI_test_component.IAsyncOperationInt private typealias CABI = __x_ABI_Ctest__component_CIAsyncOperationInt - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CIAsyncOperationInt>?) -> AsyncOperationInt? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CIAsyncOperationInt>?) -> AsyncOperationInt? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } - public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { - return test_component.queryInterface(self, iid) + override public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + return super.queryInterface(iid) } public func complete(_ result: Int32) throws { try _default.CompleteImpl(result) @@ -68,7 +58,7 @@ public final class AsyncOperationInt : WinRTClass, IAsyncOperationInt, IAsyncOpe try _default.CompleteWithErrorImpl(errorCode) } - internal lazy var _IAsyncOperation: IAsyncOperationInt32 = try! _inner.QueryInterface() + private lazy var _IAsyncOperation: IAsyncOperationInt32! = getInterfaceForCaching() public func getResults() throws -> Int32 { try _IAsyncOperation.GetResultsImpl() } @@ -78,7 +68,7 @@ public final class AsyncOperationInt : WinRTClass, IAsyncOperationInt, IAsyncOpe set { try! _IAsyncOperation.put_CompletedImpl(newValue) } } - internal lazy var _IAsyncInfo: __ABI_Windows_Foundation.IAsyncInfo = try! _inner.QueryInterface() + private lazy var _IAsyncInfo: __ABI_Windows_Foundation.IAsyncInfo! = getInterfaceForCaching() public func cancel() throws { try _IAsyncInfo.CancelImpl() } @@ -99,39 +89,34 @@ public final class AsyncOperationInt : WinRTClass, IAsyncOperationInt, IAsyncOpe get { try! _IAsyncInfo.get_StatusImpl() } } + deinit { + _default = nil + _IAsyncOperation = nil + _IAsyncInfo = nil + } } open class Base : WinRTClass { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! private typealias SwiftABI = __ABI_test_component.IBase private typealias CABI = __x_ABI_Ctest__component_CIBase - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - open func _getABI() -> UnsafeMutablePointer? { + override open func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - open var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBase>?) -> Base? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CIBase>?) -> Base? { guard let abi = abi else { return nil } return UnsealedWinRTClassWrapper.unwrapFrom(base: abi) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } @_spi(WinRTInternal) @@ -139,20 +124,22 @@ open class Base : WinRTClass { composing: Composable.Type, _ createCallback: (UnsealedWinRTClassWrapper?, inout test_component.IInspectable?) -> Composable.Default.SwiftABI) { - self._inner = MakeComposed(composing: composing, (self as! Composable.SwiftProjection), createCallback) + super.init() + MakeComposed(composing: composing, (self as! Composable.Class), createCallback) } - open func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + override open func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { switch iid { case __ABI_test_component.IBaseOverridesWrapper.IID: let wrapper = __ABI_test_component.IBaseOverridesWrapper(self) return wrapper!.queryInterface(iid) - default: return test_component.queryInterface(self, iid) + default: return super.queryInterface(iid) } } private static var _IBaseProtectedFactory : __ABI_test_component.IBaseProtectedFactory = try! RoGetActivationFactory(HString("test_component.Base")) - public init() { - self._inner = MakeComposed(composing: Self.Composable.self, self) { baseInterface, innerInterface in + override public init() { + super.init() + MakeComposed(composing: Self.Composable.self, self) { baseInterface, innerInterface in try! Self._IBaseProtectedFactory.CreateInstanceImpl(baseInterface, &innerInterface) } } @@ -161,7 +148,7 @@ open class Base : WinRTClass { try _default.DoTheThingImpl() } - internal lazy var _IBaseOverrides: __ABI_test_component.IBaseOverrides = try! _inner.QueryInterface() + private lazy var _IBaseOverrides: __ABI_test_component.IBaseOverrides! = getInterfaceForCaching() open func onDoTheThing() throws { try _IBaseOverrides.OnDoTheThingImpl() } @@ -169,51 +156,46 @@ open class Base : WinRTClass { internal enum IBaseOverrides : ComposableImpl { internal typealias CABI = __x_ABI_Ctest__component_CIBaseOverrides internal typealias SwiftABI = __ABI_test_component.IBaseOverrides - internal typealias SwiftProjection = Base + internal typealias Class = Base + internal typealias SwiftProjection = WinRTClassWeakReference internal enum Default : AbiInterface { internal typealias CABI = __x_ABI_Ctest__component_CIBase internal typealias SwiftABI = __ABI_test_component.IBase } } internal typealias Composable = IBaseOverrides + deinit { + _default = nil + _IBaseOverrides = nil + } } public final class BaseCollection : WinRTClass, IVector, IIterable { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! public typealias T = Base? private typealias SwiftABI = IVectorBase private typealias CABI = __x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBase - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBase>?) -> BaseCollection? { + public static func from(abi: ComPtr<__x_ABI_C__FIVector_1___x_ABI_Ctest__zcomponent__CBase>?) -> BaseCollection? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } - public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { - return test_component.queryInterface(self, iid) + override public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + return super.queryInterface(iid) } // MARK: Collection public typealias Element = T @@ -286,51 +268,45 @@ public final class BaseCollection : WinRTClass, IVector, IIterable { get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableBase = try! _inner.QueryInterface() + private lazy var _IIterable: IIterableBase! = getInterfaceForCaching() public func first() -> AnyIIterator? { try! _IIterable.FirstImpl() } + deinit { + _default = nil + _IIterable = nil + } } public final class BaseMapCollection : WinRTClass, IMap, IIterable { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! public typealias K = String public typealias V = Base? public typealias T = AnyIKeyValuePair? private typealias SwiftABI = IMapString_Base private typealias CABI = __x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBase - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBase>?) -> BaseMapCollection? { + public static func from(abi: ComPtr<__x_ABI_C__FIMap_2_HSTRING___x_ABI_Ctest__zcomponent__CBase>?) -> BaseMapCollection? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } - public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { - return test_component.queryInterface(self, iid) + override public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + return super.queryInterface(iid) } public func lookup(_ key: String) -> Base? { try! _default.LookupImpl(key) @@ -360,44 +336,38 @@ public final class BaseMapCollection : WinRTClass, IMap, IIterable { get { try! _default.get_SizeImpl() } } - internal lazy var _IIterable: IIterableIKeyValuePairString_Base = try! _inner.QueryInterface() + private lazy var _IIterable: IIterableIKeyValuePairString_Base! = getInterfaceForCaching() public func first() -> AnyIIterator?>? { try! _IIterable.FirstImpl() } + deinit { + _default = nil + _IIterable = nil + } } open class BaseNoOverrides : WinRTClass { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! private typealias SwiftABI = __ABI_test_component.IBaseNoOverrides private typealias CABI = __x_ABI_Ctest__component_CIBaseNoOverrides - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - open func _getABI() -> UnsafeMutablePointer? { + override open func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - open var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CIBaseNoOverrides>?) -> BaseNoOverrides? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CIBaseNoOverrides>?) -> BaseNoOverrides? { guard let abi = abi else { return nil } return UnsealedWinRTClassWrapper.unwrapFrom(base: abi) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } @_spi(WinRTInternal) @@ -405,15 +375,17 @@ open class BaseNoOverrides : WinRTClass { composing: Composable.Type, _ createCallback: (UnsealedWinRTClassWrapper?, inout test_component.IInspectable?) -> Composable.Default.SwiftABI) { - self._inner = MakeComposed(composing: composing, (self as! Composable.SwiftProjection), createCallback) + super.init() + MakeComposed(composing: composing, (self as! Composable.Class), createCallback) } - open func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { - return test_component.queryInterface(self, iid) + override open func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + return super.queryInterface(iid) } private static var _IBaseNoOverridesProtectedFactory : __ABI_test_component.IBaseNoOverridesProtectedFactory = try! RoGetActivationFactory(HString("test_component.BaseNoOverrides")) - public init() { - self._inner = MakeComposed(composing: Self.Composable.self, self) { baseInterface, innerInterface in + override public init() { + super.init() + MakeComposed(composing: Self.Composable.self, self) { baseInterface, innerInterface in try! Self._IBaseNoOverridesProtectedFactory.CreateInstanceImpl(baseInterface, &innerInterface) } } @@ -421,51 +393,45 @@ open class BaseNoOverrides : WinRTClass { internal enum IBaseNoOverrides : ComposableImpl { internal typealias CABI = C_IInspectable internal typealias SwiftABI = test_component.IInspectable - internal typealias SwiftProjection = BaseNoOverrides + internal typealias Class = BaseNoOverrides + internal typealias SwiftProjection = WinRTClassWeakReference internal enum Default : AbiInterface { internal typealias CABI = __x_ABI_Ctest__component_CIBaseNoOverrides internal typealias SwiftABI = __ABI_test_component.IBaseNoOverrides } } internal typealias Composable = IBaseNoOverrides + deinit { + _default = nil + } } public final class BaseObservableCollection : WinRTClass, IObservableVector, IVector, IIterable { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! public typealias T = Base? private typealias SwiftABI = IObservableVectorBase private typealias CABI = __x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBase - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBase>?) -> BaseObservableCollection? { + public static func from(abi: ComPtr<__x_ABI_C__FIObservableVector_1___x_ABI_Ctest__zcomponent__CBase>?) -> BaseObservableCollection? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } - public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { - return test_component.queryInterface(self, iid) + override public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + return super.queryInterface(iid) } // MARK: Collection public typealias Element = T @@ -500,17 +466,17 @@ public final class BaseObservableCollection : WinRTClass, IObservableVector, IVe // MARK: WinRT public lazy var vectorChanged : Event> = { .init( - add: { [weak this = _default] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._default else { return .init() } return try! this.add_VectorChangedImpl($0) }, - remove: { [weak this = _default] in - try? this?.remove_VectorChangedImpl($0) + remove: { [weak self] in + try? self?._default.remove_VectorChangedImpl($0) } ) }() - internal lazy var _IVector: IVectorBase = try! _inner.QueryInterface() + private lazy var _IVector: IVectorBase! = getInterfaceForCaching() public func getAt(_ index: UInt32) -> Base? { try! _IVector.GetAtImpl(index) } @@ -551,81 +517,76 @@ public final class BaseObservableCollection : WinRTClass, IObservableVector, IVe get { try! _IVector.get_SizeImpl() } } - internal lazy var _IIterable: IIterableBase = try! _inner.QueryInterface() + private lazy var _IIterable: IIterableBase! = getInterfaceForCaching() public func first() -> AnyIIterator? { try! _IIterable.FirstImpl() } + deinit { + _default = nil + _IVector = nil + _IIterable = nil + } } public final class Class : WinRTClass, IBasic { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! private typealias SwiftABI = __ABI_test_component.IClass private typealias CABI = __x_ABI_Ctest__component_CIClass - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CIClass>?) -> Class? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CIClass>?) -> Class? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } - public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { - return test_component.queryInterface(self, iid) + override public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + return super.queryInterface(iid) } - public init() { - try! _inner = RoActivateInstance(HString("test_component.Class")) + override public init() { + super.init(try! RoActivateInstance(HString("test_component.Class"))) } private static let _IClassFactory: __ABI_test_component.IClassFactory = try! RoGetActivationFactory(HString("test_component.Class")) public init(_ name: String) { - _inner = try! Self._IClassFactory.CreateInstanceImpl(name) + super.init(try! Self._IClassFactory.CreateInstanceImpl(name)) } public init(_ name: String, _ fruit: Fruit) { - _inner = try! Self._IClassFactory.CreateInstance2Impl(name, fruit) + super.init(try! Self._IClassFactory.CreateInstance2Impl(name, fruit)) } public init(_ arg: AnyIMap!, _ dummy1: Int32, _ dummy2: Int32, _ dummy3: Int32) { - _inner = try! Self._IClassFactory.CreateInstance3Impl(arg, dummy1, dummy2, dummy3) + super.init(try! Self._IClassFactory.CreateInstance3Impl(arg, dummy1, dummy2, dummy3)) } public init(_ arg: AnyIMapView!, _ dummy1: Int32, _ dummy2: Int32, _ dummy3: Int32, _ dummy4: Int32) { - _inner = try! Self._IClassFactory.CreateInstance4Impl(arg, dummy1, dummy2, dummy3, dummy4) + super.init(try! Self._IClassFactory.CreateInstance4Impl(arg, dummy1, dummy2, dummy3, dummy4)) } public init(_ arg: AnyIVector!, _ dummy1: Int32, _ dummy2: Int32, _ dummy3: Int32, _ dummy4: Int32, _ dummy5: Int32) { - _inner = try! Self._IClassFactory.CreateInstance5Impl(arg, dummy1, dummy2, dummy3, dummy4, dummy5) + super.init(try! Self._IClassFactory.CreateInstance5Impl(arg, dummy1, dummy2, dummy3, dummy4, dummy5)) } public init(_ arg: AnyIVectorView!, _ dummy1: Int32, _ dummy2: Int32, _ dummy3: Int32, _ dummy4: Int32, _ dummy5: Int32, _ dummy6: Int32) { - _inner = try! Self._IClassFactory.CreateInstance6Impl(arg, dummy1, dummy2, dummy3, dummy4, dummy5, dummy6) + super.init(try! Self._IClassFactory.CreateInstance6Impl(arg, dummy1, dummy2, dummy3, dummy4, dummy5, dummy6)) } private static let _IClassFactory2: __ABI_test_component.IClassFactory2 = try! RoGetActivationFactory(HString("test_component.Class")) public init(_ name: String, _ fruit: Fruit, _ implementation: AnyIIAmImplementable!) { - _inner = try! Self._IClassFactory2.CreateInstanceImpl(name, fruit, implementation) + super.init(try! Self._IClassFactory2.CreateInstanceImpl(name, fruit, implementation)) } private static let _IClassStatics: __ABI_test_component.IClassStatics = try! RoGetActivationFactory(HString("test_component.Class")) @@ -779,58 +740,52 @@ public final class Class : WinRTClass, IBasic { public lazy var deferrableEvent : Event> = { .init( - add: { [weak this = _default] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._default else { return .init() } return try! this.add_DeferrableEventImpl($0) }, - remove: { [weak this = _default] in - try? this?.remove_DeferrableEventImpl($0) + remove: { [weak self] in + try? self?._default.remove_DeferrableEventImpl($0) } ) }() - internal lazy var _IBasic: __ABI_test_component.IBasic = try! _inner.QueryInterface() + private lazy var _IBasic: __ABI_test_component.IBasic! = getInterfaceForCaching() public func method() { try! _IBasic.MethodImpl() } + deinit { + _default = nil + _IBasic = nil + } } public final class CollectionTester : WinRTClass { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! private typealias SwiftABI = __ABI_test_component.ICollectionTester private typealias CABI = __x_ABI_Ctest__component_CICollectionTester - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CICollectionTester>?) -> CollectionTester? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CICollectionTester>?) -> CollectionTester? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } - public init() { - try! _inner = RoActivateInstance(HString("test_component.CollectionTester")) + override public init() { + super.init(try! RoActivateInstance(HString("test_component.CollectionTester"))) } private static let _ICollectionTesterStatics: __ABI_test_component.ICollectionTesterStatics = try! RoGetActivationFactory(HString("test_component.CollectionTester")) @@ -862,39 +817,32 @@ public final class CollectionTester : WinRTClass { try _default.ReturnMapFromStringToStringImpl() } + deinit { + _default = nil + } } public final class DeferrableEventArgs : WinRTClass { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! private typealias SwiftABI = __ABI_test_component.IDeferrableEventArgs private typealias CABI = __x_ABI_Ctest__component_CIDeferrableEventArgs - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CIDeferrableEventArgs>?) -> DeferrableEventArgs? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CIDeferrableEventArgs>?) -> DeferrableEventArgs? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } public func getDeferral() throws -> test_component.Deferral! { @@ -905,32 +853,27 @@ public final class DeferrableEventArgs : WinRTClass { try _default.IncrementCounterImpl() } + deinit { + _default = nil + } } public final class Derived : test_component.Base { private typealias SwiftABI = __ABI_test_component.IDerived private typealias CABI = __x_ABI_Ctest__component_CIDerived - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } return super._getABI() } - override public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CIDerived>?) -> Derived? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CIDerived>?) -> Derived? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) @@ -950,39 +893,35 @@ public final class Derived : test_component.Base { internal enum IBaseOverrides : ComposableImpl { internal typealias CABI = __x_ABI_Ctest__component_CIBaseOverrides internal typealias SwiftABI = __ABI_test_component.IBaseOverrides - internal typealias SwiftProjection = Derived + internal typealias Class = Derived + internal typealias SwiftProjection = WinRTClassWeakReference internal enum Default : AbiInterface { internal typealias CABI = __x_ABI_Ctest__component_CIDerived internal typealias SwiftABI = __ABI_test_component.IDerived } } internal typealias Composable = IBaseOverrides + deinit { + _default = nil + } } public final class DerivedFromNoConstructor : test_component.UnsealedDerivedNoConstructor { private typealias SwiftABI = __ABI_test_component.IDerivedFromNoConstructor private typealias CABI = __x_ABI_Ctest__component_CIDerivedFromNoConstructor - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } return super._getABI() } - override public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CIDerivedFromNoConstructor>?) -> DerivedFromNoConstructor? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CIDerivedFromNoConstructor>?) -> DerivedFromNoConstructor? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) @@ -997,51 +936,45 @@ public final class DerivedFromNoConstructor : test_component.UnsealedDerivedNoCo internal enum IBaseOverrides : ComposableImpl { internal typealias CABI = __x_ABI_Ctest__component_CIBaseOverrides internal typealias SwiftABI = __ABI_test_component.IBaseOverrides - internal typealias SwiftProjection = DerivedFromNoConstructor + internal typealias Class = DerivedFromNoConstructor + internal typealias SwiftProjection = WinRTClassWeakReference internal enum Default : AbiInterface { internal typealias CABI = __x_ABI_Ctest__component_CIDerivedFromNoConstructor internal typealias SwiftABI = __ABI_test_component.IDerivedFromNoConstructor } } internal typealias Composable = IBaseOverrides + deinit { + _default = nil + } } public final class EventTester : WinRTClass { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! private typealias SwiftABI = __ABI_test_component.IEventTester private typealias CABI = __x_ABI_Ctest__component_CIEventTester - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CIEventTester>?) -> EventTester? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CIEventTester>?) -> EventTester? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } private static let _IEventTesterFactory: __ABI_test_component.IEventTesterFactory = try! RoGetActivationFactory(HString("test_component.EventTester")) public init(_ impl: AnyIIAmImplementable!) { - _inner = try! Self._IEventTesterFactory.CreateInstanceImpl(impl) + super.init(try! Self._IEventTesterFactory.CreateInstanceImpl(impl)) } public func subscribe() throws { @@ -1060,52 +993,48 @@ public final class EventTester : WinRTClass { get { try! _default.get_CountImpl() } } + deinit { + _default = nil + } } public final class NoopClosable : WinRTClass, test_component.IClosable { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! private typealias SwiftABI = __ABI_Windows_Foundation.IClosable private typealias CABI = __x_ABI_CWindows_CFoundation_CIClosable - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_CWindows_CFoundation_CIClosable>?) -> NoopClosable? { + public static func from(abi: ComPtr<__x_ABI_CWindows_CFoundation_CIClosable>?) -> NoopClosable? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } - public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { - return test_component.queryInterface(self, iid) + override public func queryInterface(_ iid: test_component.IID) -> IUnknownRef? { + return super.queryInterface(iid) } - public init() { - try! _inner = RoActivateInstance(HString("test_component.NoopClosable")) + override public init() { + super.init(try! RoActivateInstance(HString("test_component.NoopClosable"))) } public func close() throws { try _default.CloseImpl() } + deinit { + _default = nil + } } public final class NullValues { @@ -1153,40 +1082,30 @@ public final class NullValues { } public final class Simple : WinRTClass { - @_spi(WinRTInternal) - private (set) public var _inner: test_component.IInspectable! private typealias SwiftABI = __ABI_test_component.ISimple private typealias CABI = __x_ABI_Ctest__component_CISimple - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) - public func _getABI() -> UnsafeMutablePointer? { + override public func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } - return nil + return super._getABI() } - public var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CISimple>?) -> Simple? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CISimple>?) -> Simple? { guard let abi = abi else { return nil } - return .init(fromAbi: test_component.IInspectable(consuming: abi)) + return .init(fromAbi: test_component.IInspectable(abi)) } @_spi(WinRTInternal) public init(fromAbi: test_component.IInspectable) { - _inner = fromAbi + super.init(fromAbi) } - public init() { - try! _inner = RoActivateInstance(HString("test_component.Simple")) + override public init() { + super.init(try! RoActivateInstance(HString("test_component.Simple"))) } private static let _ISimpleStatics: __ABI_test_component.ISimpleStatics = try! RoGetActivationFactory(HString("test_component.Simple")) @@ -1196,13 +1115,8 @@ public final class Simple : WinRTClass { public static var staticEvent : Event> = { .init( - add: { [weak this = _ISimpleStatics] in - guard let this else { return .init() } - return try! this.add_StaticEventImpl($0) - }, - remove: { [weak this = _ISimpleStatics] in - try? this?.remove_StaticEventImpl($0) - } + add: { try! _ISimpleStatics.add_StaticEventImpl($0) }, + remove: { try? _ISimpleStatics.remove_StaticEventImpl($0) } ) }() @@ -1259,40 +1173,43 @@ public final class Simple : WinRTClass { public lazy var inEvent : Event = { .init( - add: { [weak this = _default] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._default else { return .init() } return try! this.add_InEventImpl($0) }, - remove: { [weak this = _default] in - try? this?.remove_InEventImpl($0) + remove: { [weak self] in + try? self?._default.remove_InEventImpl($0) } ) }() public lazy var signalEvent : Event = { .init( - add: { [weak this = _default] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._default else { return .init() } return try! this.add_SignalEventImpl($0) }, - remove: { [weak this = _default] in - try? this?.remove_SignalEventImpl($0) + remove: { [weak self] in + try? self?._default.remove_SignalEventImpl($0) } ) }() public lazy var simpleEvent : Event> = { .init( - add: { [weak this = _default] in - guard let this else { return .init() } + add: { [weak self] in + guard let this = self?._default else { return .init() } return try! this.add_SimpleEventImpl($0) }, - remove: { [weak this = _default] in - try? this?.remove_SimpleEventImpl($0) + remove: { [weak self] in + try? self?._default.remove_SimpleEventImpl($0) } ) }() + deinit { + _default = nil + } } public final class StaticClass { @@ -1319,25 +1236,17 @@ public final class StaticClass { open class UnsealedDerived : test_component.Base { private typealias SwiftABI = __ABI_test_component.IUnsealedDerived private typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerived - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) override open func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } return super._getABI() } - override open var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CIUnsealedDerived>?) -> UnsealedDerived? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CIUnsealedDerived>?) -> UnsealedDerived? { guard let abi = abi else { return nil } return UnsealedWinRTClassWrapper.unwrapFrom(base: abi) } @@ -1390,7 +1299,7 @@ open class UnsealedDerived : test_component.Base { set { try! _default.put_PropImpl(newValue) } } - internal lazy var _IUnsealedDerivedOverloads2: __ABI_test_component.IUnsealedDerivedOverloads2 = try! _inner.QueryInterface() + private lazy var _IUnsealedDerivedOverloads2: __ABI_test_component.IUnsealedDerivedOverloads2! = getInterfaceForCaching() open func onAfterDoTheThing() throws { try _IUnsealedDerivedOverloads2.OnAfterDoTheThingImpl() } @@ -1398,14 +1307,15 @@ open class UnsealedDerived : test_component.Base { internal enum IUnsealedDerivedOverloads2 : ComposableImpl { internal typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerivedOverloads2 internal typealias SwiftABI = __ABI_test_component.IUnsealedDerivedOverloads2 - internal typealias SwiftProjection = UnsealedDerived + internal typealias Class = UnsealedDerived + internal typealias SwiftProjection = WinRTClassWeakReference internal enum Default : AbiInterface { internal typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerived internal typealias SwiftABI = __ABI_test_component.IUnsealedDerived } } internal typealias Composable = IUnsealedDerivedOverloads2 - internal lazy var _IUnsealedDerivedOverrides: __ABI_test_component.IUnsealedDerivedOverrides = try! _inner.QueryInterface() + private lazy var _IUnsealedDerivedOverrides: __ABI_test_component.IUnsealedDerivedOverrides! = getInterfaceForCaching() open func onBeforeDoTheThing() throws { try _IUnsealedDerivedOverrides.OnBeforeDoTheThingImpl() } @@ -1413,36 +1323,34 @@ open class UnsealedDerived : test_component.Base { internal enum IUnsealedDerivedOverrides : ComposableImpl { internal typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerivedOverrides internal typealias SwiftABI = __ABI_test_component.IUnsealedDerivedOverrides - internal typealias SwiftProjection = UnsealedDerived + internal typealias Class = UnsealedDerived + internal typealias SwiftProjection = WinRTClassWeakReference internal enum Default : AbiInterface { internal typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerived internal typealias SwiftABI = __ABI_test_component.IUnsealedDerived } } + deinit { + _default = nil + _IUnsealedDerivedOverloads2 = nil + _IUnsealedDerivedOverrides = nil + } } open class UnsealedDerived2 : test_component.UnsealedDerived { private typealias SwiftABI = __ABI_test_component.IUnsealedDerived2 private typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerived2 - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) override open func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } return super._getABI() } - override open var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CIUnsealedDerived2>?) -> UnsealedDerived2? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CIUnsealedDerived2>?) -> UnsealedDerived2? { guard let abi = abi else { return nil } return UnsealedWinRTClassWrapper.unwrapFrom(base: abi) } @@ -1485,37 +1393,33 @@ open class UnsealedDerived2 : test_component.UnsealedDerived { internal enum IUnsealedDerivedOverloads2 : ComposableImpl { internal typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerivedOverloads2 internal typealias SwiftABI = __ABI_test_component.IUnsealedDerivedOverloads2 - internal typealias SwiftProjection = UnsealedDerived2 + internal typealias Class = UnsealedDerived2 + internal typealias SwiftProjection = WinRTClassWeakReference internal enum Default : AbiInterface { internal typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerived2 internal typealias SwiftABI = __ABI_test_component.IUnsealedDerived2 } } internal typealias Composable = IUnsealedDerivedOverloads2 + deinit { + _default = nil + } } open class UnsealedDerivedFromNoConstructor : test_component.UnsealedDerivedNoConstructor { private typealias SwiftABI = __ABI_test_component.IUnsealedDerivedFromNoConstructor private typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerivedFromNoConstructor - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) override open func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } return super._getABI() } - override open var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CIUnsealedDerivedFromNoConstructor>?) -> UnsealedDerivedFromNoConstructor? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CIUnsealedDerivedFromNoConstructor>?) -> UnsealedDerivedFromNoConstructor? { guard let abi = abi else { return nil } return UnsealedWinRTClassWrapper.unwrapFrom(base: abi) } @@ -1546,37 +1450,33 @@ open class UnsealedDerivedFromNoConstructor : test_component.UnsealedDerivedNoCo internal enum IBaseOverrides : ComposableImpl { internal typealias CABI = __x_ABI_Ctest__component_CIBaseOverrides internal typealias SwiftABI = __ABI_test_component.IBaseOverrides - internal typealias SwiftProjection = UnsealedDerivedFromNoConstructor + internal typealias Class = UnsealedDerivedFromNoConstructor + internal typealias SwiftProjection = WinRTClassWeakReference internal enum Default : AbiInterface { internal typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerivedFromNoConstructor internal typealias SwiftABI = __ABI_test_component.IUnsealedDerivedFromNoConstructor } } internal typealias Composable = IBaseOverrides + deinit { + _default = nil + } } open class UnsealedDerivedNoConstructor : test_component.Base { private typealias SwiftABI = __ABI_test_component.IUnsealedDerivedNoConstructor private typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerivedNoConstructor - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) override open func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } return super._getABI() } - override open var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CIUnsealedDerivedNoConstructor>?) -> UnsealedDerivedNoConstructor? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CIUnsealedDerivedNoConstructor>?) -> UnsealedDerivedNoConstructor? { guard let abi = abi else { return nil } return UnsealedWinRTClassWrapper.unwrapFrom(base: abi) } @@ -1601,37 +1501,33 @@ open class UnsealedDerivedNoConstructor : test_component.Base { internal enum IBaseOverrides : ComposableImpl { internal typealias CABI = __x_ABI_Ctest__component_CIBaseOverrides internal typealias SwiftABI = __ABI_test_component.IBaseOverrides - internal typealias SwiftProjection = UnsealedDerivedNoConstructor + internal typealias Class = UnsealedDerivedNoConstructor + internal typealias SwiftProjection = WinRTClassWeakReference internal enum Default : AbiInterface { internal typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerivedNoConstructor internal typealias SwiftABI = __ABI_test_component.IUnsealedDerivedNoConstructor } } internal typealias Composable = IBaseOverrides + deinit { + _default = nil + } } open class UnsealedDerivedNoOverrides : test_component.BaseNoOverrides { private typealias SwiftABI = __ABI_test_component.IUnsealedDerivedNoOverrides private typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerivedNoOverrides - private lazy var _default: SwiftABI! = try! _inner.QueryInterface() + private lazy var _default: SwiftABI! = getInterfaceForCaching() @_spi(WinRTInternal) override open func _getABI() -> UnsafeMutablePointer? { if T.self == CABI.self { return RawPointer(_default) } - if T.self == C_IInspectable.self { - return RawPointer(_default) - } - if T.self == C_IUnknown.self { - return RawPointer(_default) - } return super._getABI() } - override open var thisPtr: test_component.IInspectable { try! _inner.QueryInterface() } - @_spi(WinRTInternal) - public static func from(abi: UnsafeMutablePointer<__x_ABI_Ctest__component_CIUnsealedDerivedNoOverrides>?) -> UnsealedDerivedNoOverrides? { + public static func from(abi: ComPtr<__x_ABI_Ctest__component_CIUnsealedDerivedNoOverrides>?) -> UnsealedDerivedNoOverrides? { guard let abi = abi else { return nil } return UnsealedWinRTClassWrapper.unwrapFrom(base: abi) } @@ -1662,13 +1558,17 @@ open class UnsealedDerivedNoOverrides : test_component.BaseNoOverrides { internal enum IUnsealedDerivedNoOverrides : ComposableImpl { internal typealias CABI = C_IInspectable internal typealias SwiftABI = test_component.IInspectable - internal typealias SwiftProjection = UnsealedDerivedNoOverrides + internal typealias Class = UnsealedDerivedNoOverrides + internal typealias SwiftProjection = WinRTClassWeakReference internal enum Default : AbiInterface { internal typealias CABI = __x_ABI_Ctest__component_CIUnsealedDerivedNoOverrides internal typealias SwiftABI = __ABI_test_component.IUnsealedDerivedNoOverrides } } internal typealias Composable = IUnsealedDerivedNoOverrides + deinit { + _default = nil + } } public typealias ObjectHandler = (Any?) -> () diff --git a/tests/test_component/cpp/Base.cpp b/tests/test_component/cpp/Base.cpp index 36a62da1..75427763 100644 --- a/tests/test_component/cpp/Base.cpp +++ b/tests/test_component/cpp/Base.cpp @@ -4,6 +4,10 @@ namespace winrt::test_component::implementation { + Base::~Base() + { + + } void Base::DoTheThing() { overridable().OnDoTheThing(); diff --git a/tests/test_component/cpp/Base.h b/tests/test_component/cpp/Base.h index e523fb2f..43a3dfc8 100644 --- a/tests/test_component/cpp/Base.h +++ b/tests/test_component/cpp/Base.h @@ -6,7 +6,7 @@ namespace winrt::test_component::implementation struct Base : BaseT { Base() = default; - + ~Base(); virtual void DoTheThing(); virtual void OnDoTheThing(); }; diff --git a/tests/test_component/cpp/CMakeLists.txt b/tests/test_component/cpp/CMakeLists.txt index 5b362d33..61f8929f 100644 --- a/tests/test_component/cpp/CMakeLists.txt +++ b/tests/test_component/cpp/CMakeLists.txt @@ -1,6 +1,19 @@ project(test_component_cpp) include(nuget) +# For debug builds, build with clang which enables us to use DWARF symbols +# and debug both Swift and C++ code using lldb. +if (${CMAKE_BUILD_TYPE} STREQUAL "DEBUG") + set(CMAKE_C_COMPILER clang-cl) + set(CMAKE_CXX_COMPILER clang-cl) +else() + # For release builds don't use clang because the C++/WinRT generated code doesn't compile. + # It's not immediately clear if this is an issue with C++/WinRT or the compilation flags + # being used. But we are only using clang in debug builds to make debugging easier, so + # using this little hack for now. + set(CMAKE_C_COMPILER cl) + set(CMAKE_CXX_COMPILER cl) +endif() set(H_FILE ${CMAKE_TEST_COMPONENT_OUTPUT}/test_component.h ) @@ -24,7 +37,7 @@ set(MIDL_OUTPUT ${WINMD_FILE} ) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) # change the warning level to 4 @@ -43,6 +56,9 @@ foreach(build_type RELEASE MINSIZEREL RELWITHDEBINFO) # Figure out the best way to make these changes to the build and linker flags # string(REPLACE "/INCREMENTAL" "" CMAKE_EXE_LINKER_FLAGS_${build_type} "${CMAKE_EXE_LINKER_FLAGS_${build_type}}") # string(APPEND CMAKE_EXE_LINKER_FLAGS_${build_type} " /LTCG:INCREMENTAL /OPT:REF") + if (CMAKE_CXX_COMPILER MATCHES "clang-cl") + string(APPEND CMAKE_EXE_LINKER_FLAGS_${build_type} " -fuse-ld=lld -Xlinker -debug:dwarf") + endif() endforeach() # Always generate symbols for release builds @@ -51,7 +67,8 @@ string(APPEND CMAKE_SHARED_LINKER_FLAGS_RELEASE " /DEBUG /OPT:REF /OPT:ICF /MAP" string(APPEND CMAKE_EXE_LINKER_FLAGS_RELEASE " /DEBUG /OPT:REF /OPT:ICF /MAP") if (CMAKE_CXX_COMPILER MATCHES "clang-cl") - add_compile_options(-Wno-delete-non-virtual-dtor -mcx16 -fno-delayed-template-parsing -Xclang -fcoroutines-ts) + add_compile_options(-Wno-delete-non-virtual-dtor -mcx16 -fno-delayed-template-parsing) + add_compile_options(-gdwarf) else() add_compile_options(/permissive- /await) endif()