ObjCClass.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Text;
  4. namespace Veldrid.MetalBindings
  5. {
  6. public unsafe struct ObjCClass
  7. {
  8. public readonly IntPtr NativePtr;
  9. public static implicit operator IntPtr(ObjCClass c) => c.NativePtr;
  10. public ObjCClass(string name)
  11. {
  12. int byteCount = Encoding.UTF8.GetMaxByteCount(name.Length);
  13. byte* utf8BytesPtr = stackalloc byte[byteCount];
  14. fixed (char* namePtr = name)
  15. {
  16. Encoding.UTF8.GetBytes(namePtr, name.Length, utf8BytesPtr, byteCount);
  17. }
  18. NativePtr = ObjectiveCRuntime.objc_getClass(utf8BytesPtr);
  19. }
  20. public IntPtr GetProperty(string propertyName)
  21. {
  22. int byteCount = Encoding.UTF8.GetMaxByteCount(propertyName.Length);
  23. byte* utf8BytesPtr = stackalloc byte[byteCount];
  24. fixed (char* namePtr = propertyName)
  25. {
  26. Encoding.UTF8.GetBytes(namePtr, propertyName.Length, utf8BytesPtr, byteCount);
  27. }
  28. return ObjectiveCRuntime.class_getProperty(this, utf8BytesPtr);
  29. }
  30. public string Name => MTLUtil.GetUtf8String(ObjectiveCRuntime.class_getName(this));
  31. public T Alloc<T>() where T : struct
  32. {
  33. IntPtr value = ObjectiveCRuntime.IntPtr_objc_msgSend(NativePtr, Selectors.alloc);
  34. return Unsafe.AsRef<T>(&value);
  35. }
  36. public T AllocInit<T>() where T : struct
  37. {
  38. IntPtr value = ObjectiveCRuntime.IntPtr_objc_msgSend(NativePtr, Selectors.alloc);
  39. ObjectiveCRuntime.objc_msgSend(value, Selectors.init);
  40. return Unsafe.AsRef<T>(&value);
  41. }
  42. public ObjectiveCMethod* class_copyMethodList(out uint count)
  43. {
  44. return ObjectiveCRuntime.class_copyMethodList(this, out count);
  45. }
  46. }
  47. }