Skip to content

Commit

Permalink
(#354) VoidPtr, FuncPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Feb 17, 2023
1 parent 8ed11c6 commit 26efe8c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
16 changes: 10 additions & 6 deletions Cesium.Runtime.Tests/PtrTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ namespace Cesium.Runtime.Tests;
public unsafe class PtrTests
{
[Fact]
public void CPtrTests()
public void VoidPtrTests()
{
CPtr v = (void*)0x1234;
VoidPtr v = (void*)0x1234;
Assert.Equal(0x1234L, (long)v.AsPtr());
Assert.Equal(0x1234L, (long)v.AsPtr<byte>());

Assert.Equal(sizeof(long), sizeof(CPtr));
Assert.Equal(sizeof(long), sizeof(VoidPtr));
}

[Fact]
public void CPtrTests()
{
CPtr<int> t = (int*)0x2345;
Assert.Equal(0x2345L, (long)t.AsPtr());
Assert.Equal((IntPtr)0x2345, t.AsIntPtr());
Expand All @@ -20,10 +24,10 @@ public void CPtrTests()
}

[Fact]
public void FPtrTests()
public void FuncPtrTests()
{
var a = new FPtr<Action>((void*)0x1234);
var a = new FuncPtr<Action>((void*)0x1234);
Assert.Equal(0x1234L, (long)a.AsPtr());
Assert.Equal(sizeof(long), sizeof(FPtr<Action>));
Assert.Equal(sizeof(long), sizeof(FuncPtr<Action>));
}
}
4 changes: 2 additions & 2 deletions Cesium.Runtime/FPtr.cs → Cesium.Runtime/FuncPtr.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Cesium.Runtime;

/// <summary>A class encapsulating a C function pointer.</summary>
public readonly unsafe struct FPtr<TDelegate> where TDelegate : Delegate // TODO: Think about vararg and empty parameter list encoding.
public readonly unsafe struct FuncPtr<TDelegate> where TDelegate : Delegate // TODO: Think about vararg and empty parameter list encoding.
{
private readonly long _value;

public FPtr(void* ptr)
public FuncPtr(void* ptr)
{
_value = (long)ptr;
}
Expand Down
2 changes: 1 addition & 1 deletion Cesium.Runtime/StdIoFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void PutS(CPtr<byte> str)
}
}

public static int PrintF(CPtr<byte> str, CPtr varargs)
public static int PrintF(CPtr<byte> str, VoidPtr varargs)
{
var formatString = RuntimeHelpers.Unmarshal(str.AsPtr());
if (formatString == null)
Expand Down
6 changes: 3 additions & 3 deletions Cesium.Runtime/CPtr.cs → Cesium.Runtime/VoidPtr.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
namespace Cesium.Runtime;

/// <summary>A class encapsulating an opaque pointer (aka <code>void*</code> in C).</summary>
public readonly unsafe struct CPtr
public readonly unsafe struct VoidPtr
{
private readonly long _value;

private CPtr(long value)
private VoidPtr(long value)
{
_value = value;
}

public static implicit operator CPtr(void* ptr) => new((long)ptr);
public static implicit operator VoidPtr(void* ptr) => new((long)ptr);
public void* AsPtr() => (void*)_value;
public TResult* AsPtr<TResult>() where TResult : unmanaged => (TResult*)_value;
public IntPtr AsIntPtr() => (IntPtr)AsPtr();
Expand Down

0 comments on commit 26efe8c

Please sign in to comment.