xamarin-macios/runtime/Delegates.cs.t4

157 строки
3.9 KiB
C

// vim: set filetype=c :
//
// Delegates.generated.cs:
//
// Authors:
// Rolf Bjarne Kvinge <rolf@xamarin.com>
//
// 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)
{
#if NET
<# foreach (var d in delegates.NotOnlyDynamic) { #>
<#
if (d.OnlyCoreCLR) {
Write ("\t\t\tif (IsCoreCLR) {\n\t");
}
if (d.SkipManagedStaticRegistrar) {
Write ("\t\t\tif (!Runtime.IsManagedStaticRegistrar) {\n\t");
}
#>
options->Delegates-><#= d.SimpleEntryPoint #> = (IntPtr) (void *) <#= d.UnmanagedDelegateCast #> &<#= d.SimpleEntryPoint #>;
<#
if (d.OnlyCoreCLR) {
WriteLine ("\t\t\t}");
}
if (d.SkipManagedStaticRegistrar) {
WriteLine ("\t\t\t}");
}
#>
<# } #>
#else
<# foreach (var d in delegates.NotOnlyDynamic) {
if (d.OnlyCoreCLR)
continue;
#>
options->Delegates-><#= d.SimpleEntryPoint #> = GetFunctionPointer (new <#= d.SimpleEntryPoint #>_delegate (<#= d.SimpleEntryPoint #>));
<# } #>
#endif
// 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.OnlyDynamic) { #>
<#
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