Skip to content

Commit

Permalink
[ObjCRuntime] Add explicit conversion operators between NativeHandle …
Browse files Browse the repository at this point in the history
…and void*. Fixes xamarin#13867.

Fixes xamarin#13867.
  • Loading branch information
rolfbjarne committed Jan 27, 2022
1 parent fb6ab21 commit 0cd2cac
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ObjCRuntime/NativeHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ public static implicit operator NativeHandle (IntPtr value)
return new NativeHandle (value);
}

public unsafe static explicit operator void* (NativeHandle value)
{
return (void *) (IntPtr) value;
}

public unsafe static explicit operator NativeHandle (void * value)
{
return new NativeHandle ((IntPtr) value);
}

public override bool Equals (object? o)
{
if (o is NativeHandle nh)
Expand Down
26 changes: 26 additions & 0 deletions tests/monotouch-test/ObjCRuntime/NativeHandleTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

using Foundation;
using ObjCRuntime;

using NUnit.Framework;

#if NET

namespace MonoTouchFixtures.ObjCRuntime {
[TestFixture]
[Preserve (AllMembers = true)]
public class NativeHandleTest {
[Test]
public unsafe void Operators ()
{
IntPtr value = new IntPtr (0xdadf00d);

Assert.AreEqual (value, ((NativeHandle) value).Handle, "IntPtr -> NativeHandle");
Assert.AreEqual (value, (IntPtr) new NativeHandle (value), "NativeHandle -> IntPtr");
Assert.AreEqual (value, ((NativeHandle) ((void *) value)).Handle, "void* -> NativeHandle");
Assert.AreEqual (value, (IntPtr) (void *) new NativeHandle (value), "NativeHandle -> void*");
}
}
}
#endif

0 comments on commit 0cd2cac

Please sign in to comment.