forked from ForNeVeR/Cesium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(ForNeVeR#354) Runtime: introduce the CPtr type
- Loading branch information
Showing
7 changed files
with
90 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace Cesium.Runtime; | ||
|
||
/// <summary>A class encapsulating an opaque pointer (aka <code>void*</code> in C).</summary> | ||
public unsafe readonly struct CPtr | ||
{ | ||
private readonly long _value; | ||
|
||
private CPtr(long value) | ||
{ | ||
_value = value; | ||
} | ||
|
||
public static implicit operator CPtr(void* ptr) => new((long)ptr); | ||
public void* AsPtr() => (void*)_value; | ||
public TResult* AsPtr<TResult>() where TResult : unmanaged => (TResult*)_value; | ||
public IntPtr AsIntPtr() => (IntPtr)AsPtr(); | ||
} | ||
|
||
|
||
/// <summary>A class encapsulating an object pointer.</summary> | ||
/// <typeparam name="T">Type this pointer may be resolved to.</typeparam> | ||
public unsafe readonly struct CPtr<T> where T : unmanaged | ||
{ | ||
private readonly long _value; | ||
|
||
private CPtr(long value) | ||
{ | ||
_value = value; | ||
} | ||
|
||
public static implicit operator CPtr<T>(T* ptr) => new((long)ptr); | ||
public T* AsPtr() => (T*)_value; | ||
public TResult* AsPtr<TResult>() where TResult : unmanaged => (TResult*)_value; | ||
public IntPtr AsIntPtr() => (IntPtr)AsPtr(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,33 @@ | ||
namespace Cesium.Runtime; | ||
|
||
#if NETSTANDARD | ||
using System.Text; | ||
#endif | ||
#if NET6_0 | ||
using System.Runtime.InteropServices; | ||
using System.Diagnostics; | ||
#endif | ||
|
||
namespace Cesium.Runtime; | ||
|
||
/// <summary> | ||
/// Functions declared in the string.h | ||
/// </summary> | ||
public unsafe static class StringFunctions | ||
{ | ||
public static uint StrLen(byte* str) | ||
public static uint StrLen(CPtr<byte> str) | ||
{ | ||
#if NETSTANDARD | ||
Encoding encoding = Encoding.UTF8; | ||
int byteLength = 0; | ||
byte* search = str; | ||
byte* search = str.AsPtr(); | ||
while (*search != '\0') | ||
{ | ||
byteLength++; | ||
search++; | ||
} | ||
|
||
int stringLength = encoding.GetCharCount(str, byteLength); | ||
int stringLength = encoding.GetCharCount(str.AsPtr(), byteLength); | ||
return (uint)stringLength; | ||
#else | ||
return (uint)(Marshal.PtrToStringUTF8((nint)str)?.Length ?? 0); | ||
return (uint)(Marshal.PtrToStringUTF8(str.AsIntPtr())?.Length ?? 0); | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters