This repository was archived by the owner on Dec 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathSwiftIteratorProtocol.cs
126 lines (102 loc) · 3.93 KB
/
SwiftIteratorProtocol.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using SwiftRuntimeLibrary.SwiftMarshal;
#if !TOM_SWIFTY
using Foundation;
using ObjCRuntime;
#endif
namespace SwiftRuntimeLibrary {
// this code is preliminary and is likely to change
// do not depend on this
[SwiftProtocolType (typeof (SwiftIteratorProtocolProxy<>), SwiftCoreConstants.LibSwiftCore, "$sStMp", true)]
public interface IIteratorProtocol<T> {
SwiftOptional<T> Next ();
}
#if __IOS__
[MonoNativeFunctionWrapper]
#endif
delegate void NextFunc (IntPtr returnVal, IntPtr self);
struct SwiftIteratorProtocolVtable {
[MarshalAs (UnmanagedType.FunctionPtr)]
public NextFunc func0;
}
public class SwiftIteratorProtocolProxy<T> : SwiftNativeObject, IIteratorProtocol<T> {
static SwiftIteratorProtocolProxy ()
{
SetVTable ();
}
static void SetVTable ()
{
var vt = new SwiftIteratorProtocolVtable ();
vt.func0 = NextFuncReceiver;
IteratorProtocolPinvokes.SetVtable (ref vt, StructMarshal.Marshaler.Metatypeof (typeof (T)));
}
static void NextFuncReceiver (IntPtr returnVal, IntPtr self)
{
var instance = SwiftObjectRegistry.Registry.CSObjectForSwiftObject<SwiftIteratorProtocolProxy<T>> (self);
var result = instance.Next ();
StructMarshal.Marshaler.ToSwift (result, returnVal);
}
IIteratorProtocol<T> proxiedType;
public SwiftIteratorProtocolProxy (IIteratorProtocol<T> proxiedType)
: base (IteratorProtocolPinvokes.NewIteratorProtocol (StructMarshal.Marshaler.Metatypeof (typeof (T))),
GetSwiftMetatype (), SwiftObjectRegistry.Registry)
{
this.proxiedType = proxiedType;
}
SwiftIteratorProtocolProxy (IntPtr ptr, SwiftObjectRegistry registry)
: base (ptr, GetSwiftMetatype (), registry)
{
}
~SwiftIteratorProtocolProxy ()
{
Dispose (false);
}
public static SwiftIteratorProtocolProxy<T> XamarinFactory (IntPtr p)
{
return new SwiftIteratorProtocolProxy<T> (p, SwiftObjectRegistry.Registry);
}
public static SwiftMetatype GetSwiftMetatype ()
{
return IteratorProtocolPinvokes.IteratorProtocolMetadataAccessor (SwiftMetadataRequest.Complete, StructMarshal.Marshaler.Metatypeof (typeof (T)));
}
#region IIteratorProtocol
public SwiftOptional<T> Next ()
{
if (proxiedType == null)
throw new NotSupportedException ("Non proxied types not supported yet");
return proxiedType.Next ();
}
#endregion
}
internal static class IteratorProtocolPinvokes {
[DllImport (SwiftCore.kXamGlue, EntryPoint = "$s7XamGlue19newIteratorProtocolAA27iteratorprotocol_xam_helperCyxGylF")]
public static extern IntPtr NewIteratorProtocol (SwiftMetatype metadata);
[DllImport (SwiftCore.kXamGlue, EntryPoint = "$s7XamGlue20IteratorProtocolNext6retval4thisySpyxSgG_AA27iteratorprotocol_xam_helperCyxGtlF")]
public static extern void IteratorProtocolNext (IntPtr retval, IntPtr self, SwiftMetatype metadata);
[DllImport (SwiftCore.kXamGlue, EntryPoint = "$s7XamGlue27iteratorprotocol_xam_helperCMa")]
public static extern SwiftMetatype IteratorProtocolMetadataAccessor (SwiftMetadataRequest request, SwiftMetatype mt);
[DllImport (SwiftCore.kXamGlue, EntryPoint = "$s7XamGlue31iteratorprotocol_set_xam_vtableyySV_ypXptF")]
public static extern void SetVtable (ref SwiftIteratorProtocolVtable vtable, SwiftMetatype mt);
[DllImport (SwiftCore.kXamGlue, EntryPoint = "$s7XamGlue13iterateThings3ret4thisySpySSG_AA27iteratorprotocol_xam_helperCySiGtF")]
public static extern void IterateThings (IntPtr ret, IntPtr self);
}
public class EnumerableIterator<T> : IIteratorProtocol<T> {
IEnumerator<T> enumerator;
public EnumerableIterator (IEnumerable<T> enumerable)
{
this.enumerator = enumerable.GetEnumerator ();
}
public SwiftOptional<T> Next ()
{
if (enumerator.MoveNext ()) {
return SwiftOptional<T>.Some (enumerator.Current);
} else {
return SwiftOptional<T>.None ();
}
}
}
}