This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathObjCClass.cs
56 lines (47 loc) · 1.85 KB
/
ObjCClass.cs
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
using System;
using System.Runtime.CompilerServices;
using System.Text;
namespace Veldrid.MetalBindings
{
public unsafe struct ObjCClass
{
public readonly IntPtr NativePtr;
public static implicit operator IntPtr(ObjCClass c) => c.NativePtr;
public ObjCClass(string name)
{
int byteCount = Encoding.UTF8.GetMaxByteCount(name.Length);
byte* utf8BytesPtr = stackalloc byte[byteCount];
fixed (char* namePtr = name)
{
Encoding.UTF8.GetBytes(namePtr, name.Length, utf8BytesPtr, byteCount);
}
NativePtr = ObjectiveCRuntime.objc_getClass(utf8BytesPtr);
}
public IntPtr GetProperty(string propertyName)
{
int byteCount = Encoding.UTF8.GetMaxByteCount(propertyName.Length);
byte* utf8BytesPtr = stackalloc byte[byteCount];
fixed (char* namePtr = propertyName)
{
Encoding.UTF8.GetBytes(namePtr, propertyName.Length, utf8BytesPtr, byteCount);
}
return ObjectiveCRuntime.class_getProperty(this, utf8BytesPtr);
}
public string Name => MTLUtil.GetUtf8String(ObjectiveCRuntime.class_getName(this));
public T Alloc<T>() where T : struct
{
IntPtr value = ObjectiveCRuntime.IntPtr_objc_msgSend(NativePtr, Selectors.alloc);
return Unsafe.AsRef<T>(&value);
}
public T AllocInit<T>() where T : struct
{
IntPtr value = ObjectiveCRuntime.IntPtr_objc_msgSend(NativePtr, Selectors.alloc);
ObjectiveCRuntime.objc_msgSend(value, Selectors.init);
return Unsafe.AsRef<T>(&value);
}
public ObjectiveCMethod* class_copyMethodList(out uint count)
{
return ObjectiveCRuntime.class_copyMethodList(this, out count);
}
}
}