Skip to content

Commit

Permalink
POC for .NET interface adapters
Browse files Browse the repository at this point in the history
See #75
  • Loading branch information
lemonmojo committed Jul 2, 2024
1 parent c585b7a commit 64fad13
Show file tree
Hide file tree
Showing 5 changed files with 501 additions and 6 deletions.
19 changes: 19 additions & 0 deletions Samples/Beyond.NET.Sample.Managed/Source/InterfaceTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
namespace Beyond.NET.Sample;

// This is declared by the user.
public interface IInterface1
{
void MethodInIInterface1();
}

// This should be auto-generated to allow Swift to provide implementations for .NET interfaces.
public class IInterface1_DelegateAdapter : IInterface1
{
public delegate void MethodInIInterface1_Delegate();
private MethodInIInterface1_Delegate _MethodInIInterface1_Adapter;

public IInterface1_DelegateAdapter(MethodInIInterface1_Delegate methodInIInterface1_Adapter)
{
_MethodInIInterface1_Adapter = methodInIInterface1_Adapter;
}

public void MethodInIInterface1()
{
_MethodInIInterface1_Adapter();
}
}

public interface IInterface2
{
int PropertyInIInterface2 { get; set; }
Expand Down Expand Up @@ -45,6 +63,7 @@ public void MethodInIInterface3()
}
}

// This is declared by the user.
public class TypeThatUsesInterfaces
{
public void CallMethod1InIInterface1(IInterface1 interface1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,22 @@ final class InterfaceTests: XCTestCase {
XCTAssertEqual(val, retVal)
try typeThatUsesInterfaces.callMethod1InIInterface3(interface3)
}

func testInterfaceAdapter() throws {
let typeThatUsesInterfaces = try Beyond_NET_Sample_TypeThatUsesInterfaces()

let methodInIInterface1CalledExpectation = expectation(description: "IInterface1.MethodInIInterface1 called in Swift")

// Compiler ensures we provide all interface requirements.
// Ideally, we'd have an auto-generated Swift wrapper type that sets up all the delegate -> closure callbacks and allows us to just override the members required to satisfy the interface/protocol requirements.
let interface1Adapter = try Beyond_NET_Sample_IInterface1_DelegateAdapter(.init({
print("IInterface1.MethodInIInterface1 called in Swift")

methodInIInterface1CalledExpectation.fulfill()
}))

try typeThatUsesInterfaces.callMethod1InIInterface1(try interface1Adapter.castTo())

wait(for: [ methodInIInterface1CalledExpectation ])
}
}
79 changes: 77 additions & 2 deletions Samples/Generated/Generated_C.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Number of generated types: 1813
// Number of generated members: 6096
// Number of generated types: 1815
// Number of generated members: 6102

#pragma mark - BEGIN Header
#ifndef TypeDefinitions_h
Expand Down Expand Up @@ -5622,6 +5622,8 @@ typedef void* Beyond_NET_Sample_IndexerTests_t;

typedef void* Beyond_NET_Sample_IInterface1_t;

typedef void* Beyond_NET_Sample_IInterface1_DelegateAdapter_t;

typedef void* Beyond_NET_Sample_IInterface2_t;

typedef void* Beyond_NET_Sample_IInterface3_t;
Expand Down Expand Up @@ -5953,6 +5955,14 @@ typedef void (*Beyond_NET_Sample_EventTests_ValueChangedDelegate_CFunction_t)(
);


typedef void* Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_t;
typedef void (*Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_CDestructorFunction_t)(void* context);

typedef void (*Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_CFunction_t)(
void* context
);


typedef void* Beyond_NET_Sample_Person_NumberOfChildrenChangedDelegate_t;
typedef void (*Beyond_NET_Sample_Person_NumberOfChildrenChangedDelegate_CDestructorFunction_t)(void* context);

Expand Down Expand Up @@ -45718,6 +45728,31 @@ Beyond_NET_Sample_IInterface1_Destroy(

#pragma mark - END APIs of Beyond.NET.Sample.IInterface1

#pragma mark - BEGIN APIs of Beyond.NET.Sample.IInterface1_DelegateAdapter
void /* System.Void */
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1(
Beyond_NET_Sample_IInterface1_DelegateAdapter_t _Nullable /* Beyond.NET.Sample.IInterface1_DelegateAdapter */ self,
System_Exception_t _Nullable* _Nullable /* System.Exception */ outException
);

Beyond_NET_Sample_IInterface1_DelegateAdapter_t _Nonnull /* Beyond.NET.Sample.IInterface1_DelegateAdapter */
Beyond_NET_Sample_IInterface1_DelegateAdapter_Create(
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_t _Nonnull /* Beyond.NET.Sample.IInterface1_DelegateAdapter.MethodInIInterface1_Delegate */ methodInIInterface1_Adapter,
System_Exception_t _Nullable* _Nullable /* System.Exception */ outException
);

System_Type_t _Nonnull /* System.Type */
Beyond_NET_Sample_IInterface1_DelegateAdapter_TypeOf(
void
);

void /* System.Void */
Beyond_NET_Sample_IInterface1_DelegateAdapter_Destroy(
Beyond_NET_Sample_IInterface1_DelegateAdapter_t _Nullable /* Beyond.NET.Sample.IInterface1_DelegateAdapter */ self
);

#pragma mark - END APIs of Beyond.NET.Sample.IInterface1_DelegateAdapter

#pragma mark - BEGIN APIs of Beyond.NET.Sample.IInterface2
int32_t /* System.Int32 */
Beyond_NET_Sample_IInterface2_PropertyInIInterface2_Get(
Expand Down Expand Up @@ -51016,6 +51051,46 @@ Beyond_NET_Sample_EventTests_ValueChangedDelegate_Destroy(

#pragma mark - END APIs of Beyond.NET.Sample.EventTests.ValueChangedDelegate

#pragma mark - BEGIN APIs of Beyond.NET.Sample.IInterface1_DelegateAdapter.MethodInIInterface1_Delegate
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_t _Nonnull /* Beyond.NET.Sample.IInterface1_DelegateAdapter.MethodInIInterface1_Delegate */
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_Create(
const void* context,
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_CFunction_t function,
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_CDestructorFunction_t destructorFunction
);

void
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_Invoke(
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_t /* Beyond.NET.Sample.IInterface1_DelegateAdapter.MethodInIInterface1_Delegate */ self, System_Exception_t* /* System.Exception */ outException
);

const void*
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_Context_Get(
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_t /* Beyond.NET.Sample.IInterface1_DelegateAdapter.MethodInIInterface1_Delegate */ self
);

Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_CFunction_t
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_CFunction_Get(
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_t /* Beyond.NET.Sample.IInterface1_DelegateAdapter.MethodInIInterface1_Delegate */ self
);

Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_CDestructorFunction_t
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_CDestructorFunction_Get(
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_t /* Beyond.NET.Sample.IInterface1_DelegateAdapter.MethodInIInterface1_Delegate */ self
);

System_Type_t _Nonnull /* System.Type */
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_TypeOf(
void
);

void /* System.Void */
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_Destroy(
Beyond_NET_Sample_IInterface1_DelegateAdapter_MethodInIInterface1_Delegate_t _Nullable /* Beyond.NET.Sample.IInterface1_DelegateAdapter.MethodInIInterface1_Delegate */ self
);

#pragma mark - END APIs of Beyond.NET.Sample.IInterface1_DelegateAdapter.MethodInIInterface1_Delegate

#pragma mark - BEGIN APIs of Beyond.NET.Sample.Person.NumberOfChildrenChangedDelegate
Beyond_NET_Sample_Person_NumberOfChildrenChangedDelegate_t _Nonnull /* Beyond.NET.Sample.Person.NumberOfChildrenChangedDelegate */
Beyond_NET_Sample_Person_NumberOfChildrenChangedDelegate_Create(
Expand Down
Loading

0 comments on commit 64fad13

Please sign in to comment.