forked from dotnet/macios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelegates.cs.t4
146 lines (135 loc) · 3.7 KB
/
Delegates.cs.t4
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// vim: set filetype=c :
//
// Delegates.generated.cs:
//
// Authors:
// Rolf Bjarne Kvinge <[email protected]>
//
// Copyright 2013-2014 Xamarin Inc.
//
/*
* This file was generated from Delegates.cs.t4, any modifications will be lost once this file is re-generated.
*/
<#@ include file="delegates.t4" #>
#if !COREBUILD
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using Foundation;
using Registrar;
namespace ObjCRuntime {
public unsafe partial class Runtime {
#if !NET
<# foreach (var d in delegates) { #>
<#
if (d.OnlyCoreCLR)
continue;
#>
internal delegate <#= d.ReturnType.MType #> <#= d.SimpleEntryPoint #>_delegate (<#= d.MArgumentSignature #>);
<# } #>
#endif // !NET
internal struct Delegates {
<# foreach (var d in delegates) { #>
<#
if (d.OnlyCoreCLR)
Write ("#if NET\n");
#>
public IntPtr <#= d.SimpleEntryPoint #>;
<#
if (d.OnlyCoreCLR)
Write ("#endif // NET\n");
#>
<# } #>
}
<# foreach (var d in delegates) { #>
<#
if (d.OnlyCoreCLR)
Write ("#if NET\n");
#>
#if NET
[UnmanagedCallersOnly]
#else
[MonoPInvokeCallback (typeof (<#= d.SimpleEntryPoint #>_delegate))]
#endif
static unsafe <#= d.ReturnType.MType #> <#= d.SimpleEntryPoint #> (<#= d.MArgumentSignature #>)
<# if (d.ExceptionHandling) { #>
{
*exception_gchandle = IntPtr.Zero;
try {
<# if (string.IsNullOrEmpty (d.WrappedManagedFunction)) { #>
throw new NotImplementedException ();
<# } else { #>
<# if (d.ReturnType.MType != "void") { #>return <# } #><#=d.WrappedManagedFunction#> (<#=d.MArgumentNames#>);
<# } #>
} catch (Exception ex) {
var handle = GCHandle.Alloc (ex, GCHandleType.Normal);
*exception_gchandle = GCHandle.ToIntPtr (handle);
<# if (d.SimpleEntryPoint == "get_nsobject_with_type") { #> *created = 0;
<# } #>
<# if (d.ReturnType.MType != "void") { #> return default (<#= d.ReturnType.MType #>);
<# } #>
}
}
<# } else { #>
{
<# if (string.IsNullOrEmpty (d.WrappedManagedFunction)) { #>
throw new NotImplementedException ();
<# } else { #>
<# if (d.ReturnType.MType != "void") { #>return <# } #><#=d.WrappedManagedFunction#> (<#=d.MArgumentNames#>);
<# } #>
}
<# } #>
<#
if (d.OnlyCoreCLR)
Write ("#endif // NET\n");
#>
<# } #>
[BindingImpl (BindingImplOptions.Optimizable)] // To inline the Runtime.DynamicRegistrationSupported code if possible.
unsafe static void RegisterDelegates (InitializationOptions* options)
{
<# foreach (var d in delegates) {
if (d.OnlyDynamicUsage) continue; #>
<#
if (d.OnlyCoreCLR) {
Write ("#if NET\n");
Write ("\t\t\tif (IsCoreCLR)\n\t");
}
#>#if NET
options->Delegates-><#= d.SimpleEntryPoint #> = (IntPtr) (void *) <#= d.UnmanagedDelegateCast #> &<#= d.SimpleEntryPoint #>;
#else
options->Delegates-><#= d.SimpleEntryPoint #> = GetFunctionPointer (new <#= d.SimpleEntryPoint #>_delegate (<#= d.SimpleEntryPoint #>));
#endif
<#
if (d.OnlyCoreCLR)
Write ("#endif // NET\n");
#>
<# } #>
// The linker will remove this condition (and the subsequent method call) if possible
if (DynamicRegistrationSupported)
RegisterDelegatesDynamic (options);
}
static void RegisterDelegatesDynamic (InitializationOptions* options)
{
<# foreach (var d in delegates) {
if (!d.OnlyDynamicUsage) continue; #>
<#
if (d.OnlyCoreCLR) {
Write ("#if NET\n");
Write ("\t\t\tif (IsCoreCLR)\n\t");
}
#>
#if NET
options->Delegates-><#= d.SimpleEntryPoint #> = (IntPtr) (void *) <#= d.UnmanagedDelegateCast #> &<#= d.SimpleEntryPoint #>;
#else
options->Delegates-><#= d.SimpleEntryPoint #> = GetFunctionPointer (new <#= d.SimpleEntryPoint #>_delegate (<#= d.SimpleEntryPoint #>));
#endif
<#
if (d.OnlyCoreCLR)
Write ("#endif // NET\n");
#>
<# } #>
}
}
}
#endif