Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Rename COMExport to COMExportBase and COMTearOff to COMTearOffBase #436

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Docs/How it works.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ enum IFooBinding: COMBinding { // COMBinding conforms to ABIBinding
}
```

### COMExport Base Class
### COMExportBase Base Class

What if we want to implement `IFooProtocol` in Swift and pass it to a COM method?

Expand All @@ -144,11 +144,11 @@ This is a different problem. Now we have a Swift native object that we must turn
For example:

```swift
class MyFoo: COMExport<IFooBinding>, IFooProtocol {
class MyFoo: COMExportBase<IFooBinding>, IFooProtocol {
public func getName() throws -> String { "George" }
}

// COMExport has a field that looks like a COM object:
// COMExportBase has a field that looks like a COM object:
// typedef struct SWRT_COMEmbedding {
// const void* virtualTable;
// void* swiftEmbedder; // Will point back to MyFoo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension Sequence {
}
}

fileprivate class SequenceIterable<S: Sequence>: WinRTExport<IInspectableBinding>, WindowsFoundationCollections_IIterableProtocol {
fileprivate class SequenceIterable<S: Sequence>: WinRTExportBase<IInspectableBinding>, WindowsFoundationCollections_IIterableProtocol {
typealias T = S.Element

private let sequence: S
Expand All @@ -23,7 +23,7 @@ fileprivate class SequenceIterable<S: Sequence>: WinRTExport<IInspectableBinding
}
}

internal class SequenceIterator<I: IteratorProtocol>: WinRTExport<IInspectableBinding>, WindowsFoundationCollections_IIteratorProtocol {
internal class SequenceIterator<I: IteratorProtocol>: WinRTExportBase<IInspectableBinding>, WindowsFoundationCollections_IIteratorProtocol {
typealias T = I.Element

private var iterator: I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension Collection where Index == Int, Element: Equatable {
}
}

fileprivate class CollectionVectorView<C: Collection>: WinRTExport<IInspectableBinding>,
fileprivate class CollectionVectorView<C: Collection>: WinRTExportBase<IInspectableBinding>,
WindowsFoundationCollections_IVectorViewProtocol
where C.Index == Int {
public typealias T = C.Element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension Array where Element: Equatable {
}

/// Wraps a Swift array into a type implementing WinRT's Windows.Foundation.Collections.IVector<T>.
public class ArrayVector<T>: WinRTExport<IInspectableBinding>,
public class ArrayVector<T>: WinRTExportBase<IInspectableBinding>,
WindowsFoundationCollections_IVectorProtocol, WindowsFoundationCollections_IVectorViewProtocol {
public var array: [T]
public var elementEquals: (T, T) -> Bool
Expand Down
2 changes: 1 addition & 1 deletion InteropTests/Tests/EventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class EventTests: WinRTTestCase {
try eventSource.fire()
XCTAssertEqual(try counter.count, 1)

class EventSource: WinRTExport<IEventSourceBinding>, IEventSourceProtocol {
class EventSource: WinRTExportBase<IEventSourceBinding>, IEventSourceProtocol {
private var invocationList: EventInvocationList<MinimalDelegate> = .init()

@discardableResult
Expand Down
2 changes: 1 addition & 1 deletion InteropTests/Tests/InterfaceImplementationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import WinRTComponent

class InterfaceImplementationTests: WinRTTestCase {
func testWithSwiftObject() throws {
class Exported: WinRTExport<IInspectableBinding>, IMinimalInterfaceProtocol {
class Exported: WinRTExportBase<IInspectableBinding>, IMinimalInterfaceProtocol {
override class var queriableInterfaces: [any COMTwoWayBinding.Type] { [
IMinimalInterfaceBinding.self
] }
Expand Down
4 changes: 2 additions & 2 deletions InteropTests/Tests/ObjectExportingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import WindowsRuntime
import WinRTComponent

class ObjectExportingTests: WinRTTestCase {
class ExportedClass: WinRTExport<IInspectableBinding> {}
class ExportedClass: WinRTExportBase<IInspectableBinding> {}

func testBalancedAddRefRelease() throws {
let obj: ExportedClass = .init()
Expand Down Expand Up @@ -42,7 +42,7 @@ class ObjectExportingTests: WinRTTestCase {
}

func testWeakReference() throws {
class Exported: WinRTExport<IInspectableBinding> {}
class Exported: WinRTExportBase<IInspectableBinding> {}

var instance: Exported? = Exported()
let weakReferencer = try WeakReferencer(XCTUnwrap(instance))
Expand Down
2 changes: 1 addition & 1 deletion InteropTests/Tests/ValueOutParamRoundtripTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class ValueOutParamRoundtripTests: WinRTTestCase {
XCTAssertNil(roundtripped)
}

class OutputArgumentImplementation: WinRTExport<IOutputArgumentBinding>, IOutputArgumentProtocol {
class OutputArgumentImplementation: WinRTExportBase<IOutputArgumentBinding>, IOutputArgumentProtocol {
func int32(_ value: Int32, _ result: inout Int32) throws { result = value }
func string(_ value: String, _ result: inout String) throws { result = value }
func object(_ value: WindowsRuntime.IInspectable?, _ result: inout WindowsRuntime.IInspectable?) throws { result = value }
Expand Down
2 changes: 1 addition & 1 deletion InteropTests/Tests/ValueReturnRoundtripTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ValueReturnRoundtripTests: WinRTTestCase {
XCTAssertNil(try twoWay.reference(nil))
}

class ReturnArgumentImplementation: WinRTExport<IReturnArgumentBinding>, IReturnArgumentProtocol {
class ReturnArgumentImplementation: WinRTExportBase<IReturnArgumentBinding>, IReturnArgumentProtocol {
func int32(_ value: Int32) throws -> Int32 { value }
func string(_ value: String) throws -> String { value }
func object(_ value: WindowsRuntime.IInspectable?) throws -> WindowsRuntime.IInspectable { try NullResult.unwrap(value) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import COM_ABI

/// Exposes a secondary COM interface whose implementation is delegated to a primary Swift exported object.
public final class COMDelegatingExport: COMEmbedderEx {
/// A COM tear-off object providing a COM virtual table for an interface implemented by the Swift owner object.
public final class COMDelegatingTearOff: COMEmbedderEx {
private var comEmbedding: COMEmbedding

public init(virtualTable: UnsafeRawPointer, implementer: IUnknown) {
Expand Down
2 changes: 1 addition & 1 deletion Support/Sources/COM/COMError+SwiftErrorInfo.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extension COMError {
/// Wraps a Swift Error object into an `IErrorInfo` to preserve it across COM boundaries.
internal final class SwiftErrorInfo: COMExport<IErrorInfoBinding>, IErrorInfoProtocol {
internal final class SwiftErrorInfo: COMExportBase<IErrorInfoBinding>, IErrorInfoProtocol {
public let error: Error

public init(error: Error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Base for Swift classes that implement one or more COM interfaces and owns the COM object identity,
/// meaning that they own the object returned when using QueryInterface for IUnknown.
/// The generic Binding parameter determines the virtual table of the identity object.
open class COMExport<PrimaryInterfaceBinding: COMTwoWayBinding>: IUnknownProtocol {
open class COMExportBase<PrimaryInterfaceBinding: COMTwoWayBinding>: IUnknownProtocol {
/// Gets the interfaces that can be queried for using queryInterface.
open class var queriableInterfaces: [any COMTwoWayBinding.Type] { [] }
open class var implementIAgileObject: Bool { true }
Expand All @@ -26,7 +26,7 @@ open class COMExport<PrimaryInterfaceBinding: COMTwoWayBinding>: IUnknownProtoco
return try FreeThreadedMarshal(self).toCOM().cast()
default:
if let interfaceBinding = Self.queriableInterfaces.first(where: { $0.interfaceID == id }) {
return COMDelegatingExport(virtualTable: interfaceBinding.virtualTablePointer, implementer: self).toCOM()
return COMDelegatingTearOff(virtualTable: interfaceBinding.virtualTablePointer, implementer: self).toCOM()
}
throw COMError.noInterface
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// A COM-exported object providing a secondary interface to a primary exported object.
open class COMTearOff<InterfaceBinding: COMTwoWayBinding>: IUnknownProtocol {
open class COMTearOffBase<InterfaceBinding: COMTwoWayBinding>: IUnknownProtocol {
private var implements: COMImplements<InterfaceBinding> = .init()
public let owner: IUnknown

Expand Down
2 changes: 1 addition & 1 deletion Support/Sources/COM/FreeThreadedMarshal.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import COM_ABI

/// Provides an implementation of IMarshal based on the COM free-threaded marshaler.
public final class FreeThreadedMarshal: COMTearOff<FreeThreadedMarshalBinding> {
public final class FreeThreadedMarshal: COMTearOffBase<FreeThreadedMarshalBinding> {
private let marshaler: COMReference<SWRT_IMarshal>

public init(_ owner: IUnknown) throws {
Expand Down
2 changes: 1 addition & 1 deletion Support/Sources/WindowsRuntime/ComposableClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ open class ComposableClass: IInspectableProtocol {

// Check for additional implemented interfaces.
if let interfaceBinding = Self.queriableInterfaces.first(where: { $0.interfaceID == id }) {
return COMDelegatingExport(virtualTable: interfaceBinding.virtualTablePointer, implementer: self).toCOM()
return COMDelegatingTearOff(virtualTable: interfaceBinding.virtualTablePointer, implementer: self).toCOM()
}
}

Expand Down
2 changes: 1 addition & 1 deletion Support/Sources/WindowsRuntime/ReferenceArrayImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import WindowsRuntime_ABI

/// Implements IReferenceArray<T> for any boxable T not provided by the UWP PropertyValue class (value types and delegates).
internal class ReferenceArrayImpl<TBinding: IReferenceableBinding>
: WinRTExport<WindowsFoundation_IReferenceArrayBinding<TBinding>>,
: WinRTExportBase<WindowsFoundation_IReferenceArrayBinding<TBinding>>,
WindowsFoundation_IReferenceArrayProtocol {
public typealias T = TBinding.SwiftValue

Expand Down
2 changes: 1 addition & 1 deletion Support/Sources/WindowsRuntime/ReferenceImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import WindowsRuntime_ABI

/// Implements IReference<T> for any boxable T not provided by the UWP PropertyValue class (value types and delegates).
internal class ReferenceImpl<TBinding: IReferenceableBinding>
: WinRTExport<WindowsFoundation_IReferenceBinding<TBinding>>,
: WinRTExportBase<WindowsFoundation_IReferenceBinding<TBinding>>,
WindowsFoundation_IReferenceProtocol {
public typealias T = TBinding.SwiftValue

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extension WinRTError {
/// Wraps a Swift Error object so it can be associated with an `IRestrictedErrorInfo`.
internal final class LanguageException: COMExport<IUnknownBinding> {
internal final class LanguageException: COMExportBase<IUnknownBinding> {
public let error: Error

public init(error: Error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import COM

/// Base for classes exported to WinRT and COM consumers.
open class WinRTExport<PrimaryInterfaceBinding: InterfaceBinding>: COMExport<PrimaryInterfaceBinding>, IInspectableProtocol {
open class WinRTExportBase<PrimaryInterfaceBinding: InterfaceBinding>: COMExportBase<PrimaryInterfaceBinding>, IInspectableProtocol {
open class var _runtimeClassName: String { String(describing: Self.self) }
open class var _trustLevel: TrustLevel { .base }
open class var implementIStringable: Bool { true }
Expand Down Expand Up @@ -36,18 +36,7 @@ open class WinRTExport<PrimaryInterfaceBinding: InterfaceBinding>: COMExport<Pri
public final func getTrustLevel() throws -> TrustLevel { Self._trustLevel }
}

open class WinRTTearOff<Binding: InterfaceBinding>: COMTearOff<Binding>, IInspectableProtocol {
public init(owner: IInspectable) {
super.init(owner: owner)
}

// Delegate to the identity object, though we should be using that object's IInspectable implementation in the first place.
public func getIids() throws -> [COMInterfaceID] { try (owner as! IInspectable).getIids() }
public func getRuntimeClassName() throws -> String { try (owner as! IInspectable).getRuntimeClassName() }
public func getTrustLevel() throws -> TrustLevel { try (owner as! IInspectable).getTrustLevel() }
}

fileprivate class StringableTearOff: WinRTTearOff<WindowsFoundation_IStringableBinding>, WindowsFoundation_IStringableProtocol {
fileprivate class StringableTearOff: WinRTTearOffBase<WindowsFoundation_IStringableBinding>, WindowsFoundation_IStringableProtocol {
private let implementation: any CustomStringConvertible

init(owner: IInspectable, implementation: any CustomStringConvertible) {
Expand All @@ -58,12 +47,12 @@ fileprivate class StringableTearOff: WinRTTearOff<WindowsFoundation_IStringableB
func toString() throws -> String { implementation.description }
}

fileprivate class WeakReferenceSourceTearOff: COMTearOff<IWeakReferenceSourceBinding>, IWeakReferenceSourceProtocol {
fileprivate class WeakReferenceSourceTearOff: COMTearOffBase<IWeakReferenceSourceBinding>, IWeakReferenceSourceProtocol {
init(owner: IInspectable) { super.init(owner: owner) }
func getWeakReference() throws -> IWeakReference { ExportedWeakReference(target: owner as! IInspectable) }
}

fileprivate class ExportedWeakReference: COMExport<IWeakReferenceBinding>, IWeakReferenceProtocol {
fileprivate class ExportedWeakReference: COMExportBase<IWeakReferenceBinding>, IWeakReferenceProtocol {
weak var target: IInspectable?
init(target: IInspectable) { self.target = target }
func resolve() throws -> IInspectable? { target }
Expand Down
13 changes: 13 additions & 0 deletions Support/Sources/WindowsRuntime/WinRTTearOffBase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import COM

/// Base class for COM tear-off objects for interfaces derived from IInspectable.
open class WinRTTearOffBase<Binding: InterfaceBinding>: COMTearOffBase<Binding>, IInspectableProtocol {
public init(owner: IInspectable) {
super.init(owner: owner)
}

// Delegate to the identity object, though we should be using that object's IInspectable implementation in the first place.
public func getIids() throws -> [COMInterfaceID] { try (owner as! IInspectable).getIids() }
public func getRuntimeClassName() throws -> String { try (owner as! IInspectable).getRuntimeClassName() }
public func getTrustLevel() throws -> TrustLevel { try (owner as! IInspectable).getTrustLevel() }
}
14 changes: 7 additions & 7 deletions Support/Tests/Tests/COMExportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import XCTest
import WindowsRuntime

internal final class COMExportTests: XCTestCase {
final class TestObject: COMExport<IUnknownBinding>, ICOMTestProtocol, ICOMTest2Protocol {
final class TestObject: COMExportBase<IUnknownBinding>, ICOMTestProtocol, ICOMTest2Protocol {
override class var queriableInterfaces: [any COMTwoWayBinding.Type] { [
ICOMTestBinding.self,
ICOMTest2Binding.self
Expand Down Expand Up @@ -38,11 +38,11 @@ internal final class COMExportTests: XCTestCase {
}

func testIAgileObject() throws {
final class AgileObject: COMExport<IUnknownBinding> {
final class AgileObject: COMExportBase<IUnknownBinding> {
override class var implementIAgileObject: Bool { true }
}

final class NonAgileObject: COMExport<IUnknownBinding> {
final class NonAgileObject: COMExportBase<IUnknownBinding> {
override class var implementIAgileObject: Bool { false }
}

Expand All @@ -51,11 +51,11 @@ internal final class COMExportTests: XCTestCase {
}

func testFreeThreadedMarshalability() throws {
final class Marshalable: COMExport<IUnknownBinding> {
final class Marshalable: COMExportBase<IUnknownBinding> {
override class var implementFreeThreadedMarshaling: Bool { true }
}

final class NonMarshalable: COMExport<IUnknownBinding> {
final class NonMarshalable: COMExportBase<IUnknownBinding> {
override class var implementFreeThreadedMarshaling: Bool { false }
}

Expand All @@ -65,7 +65,7 @@ internal final class COMExportTests: XCTestCase {
}

func testSecondaryInterface() throws {
final class CallCounter: COMExport<IUnknownBinding>, ICOMTestProtocol {
final class CallCounter: COMExportBase<IUnknownBinding>, ICOMTestProtocol {
override class var queriableInterfaces: [any COMTwoWayBinding.Type] { [
ICOMTestBinding.self
] }
Expand All @@ -84,7 +84,7 @@ internal final class COMExportTests: XCTestCase {
}

func testEmbeddedSecondaryInterface() throws {
final class CallCounter: COMExport<IUnknownBinding>, ICOMTestProtocol {
final class CallCounter: COMExportBase<IUnknownBinding>, ICOMTestProtocol {
override class var queriableInterfaces: [any COMTwoWayBinding.Type] { [
ICOMTestBinding.self
] }
Expand Down
10 changes: 5 additions & 5 deletions Support/Tests/Tests/WinRTExportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import WindowsRuntime

internal final class WinRTExportTests: XCTestCase {
func testIInspectableIdentityRule() throws {
final class TestObject: WinRTExport<IInspectableBinding>, ICOMTestProtocol, IWinRTTestProtocol {
final class TestObject: WinRTExportBase<IInspectableBinding>, ICOMTestProtocol, IWinRTTestProtocol {
override class var queriableInterfaces: [any COMTwoWayBinding.Type] { [
ICOMTestBinding.self,
IWinRTTestBinding.self
Expand All @@ -23,7 +23,7 @@ internal final class WinRTExportTests: XCTestCase {
}

func testGetIids() throws {
final class TestObject: WinRTExport<IInspectableBinding>, ICOMTestProtocol, IWinRTTestProtocol {
final class TestObject: WinRTExportBase<IInspectableBinding>, ICOMTestProtocol, IWinRTTestProtocol {
override class var queriableInterfaces: [any COMTwoWayBinding.Type] { [
ICOMTestBinding.self,
IWinRTTestBinding.self
Expand All @@ -41,19 +41,19 @@ internal final class WinRTExportTests: XCTestCase {
}

func testIStringable() throws {
final class Stringable: WinRTExport<IInspectableBinding>, CustomStringConvertible {
final class Stringable: WinRTExportBase<IInspectableBinding>, CustomStringConvertible {
var description: String { "hello" }
}

XCTAssertEqual(try Stringable().queryInterface(WindowsFoundation_IStringableBinding.self).toString(), "hello")
}

func testIWeakReferenceSource() throws {
final class WeakReferenceSource: WinRTExport<IInspectableBinding> {
final class WeakReferenceSource: WinRTExportBase<IInspectableBinding> {
override class var implementIWeakReferenceSource: Bool { true }
}

final class NonWeakReferenceSource: WinRTExport<IInspectableBinding> {
final class NonWeakReferenceSource: WinRTExportBase<IInspectableBinding> {
override class var implementIWeakReferenceSource: Bool { false }
}

Expand Down
Loading