NSObject.odin 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package objc_Foundation
  2. import "base:intrinsics"
  3. methodSignatureForSelector :: proc "c" (obj: ^Object, selector: SEL) -> rawptr {
  4. return msgSend(rawptr, obj, "methodSignatureForSelector:", selector)
  5. }
  6. respondsToSelector :: proc "c" (obj: ^Object, selector: SEL) -> BOOL {
  7. return msgSend(BOOL, obj, "respondsToSelector:", selector)
  8. }
  9. msgSendSafeCheck :: proc "c" (obj: ^Object, selector: SEL) -> BOOL {
  10. return respondsToSelector(obj, selector) || methodSignatureForSelector(obj, selector) != nil
  11. }
  12. @(objc_class="NSObject")
  13. Object :: struct {using _: intrinsics.objc_object}
  14. @(objc_class="NSObject")
  15. Copying :: struct($T: typeid) {using _: Object}
  16. alloc :: proc "c" ($T: typeid) -> ^T where intrinsics.type_is_subtype_of(T, Object) {
  17. return msgSend(^T, T, "alloc")
  18. }
  19. @(objc_type=Object, objc_name="init")
  20. init :: proc "c" (self: ^$T) -> ^T where intrinsics.type_is_subtype_of(T, Object) {
  21. return msgSend(^T, self, "init")
  22. }
  23. @(objc_type=Object, objc_name="copy")
  24. copy :: proc "c" (self: ^Copying($T)) -> ^T where intrinsics.type_is_subtype_of(T, Object) {
  25. return msgSend(^T, self, "copy")
  26. }
  27. new :: proc "c" ($T: typeid) -> ^T where intrinsics.type_is_subtype_of(T, Object) {
  28. return init(alloc(T))
  29. }
  30. @(objc_type=Object, objc_name="retain")
  31. retain :: proc "c" (self: ^Object) {
  32. _ = msgSend(^Object, self, "retain")
  33. }
  34. @(objc_type=Object, objc_name="release")
  35. release :: proc "c" (self: ^Object) {
  36. msgSend(nil, self, "release")
  37. }
  38. @(objc_type=Object, objc_name="autorelease")
  39. autorelease :: proc "c" (self: ^Object) {
  40. msgSend(nil, self, "autorelease")
  41. }
  42. @(objc_type=Object, objc_name="retainCount")
  43. retainCount :: proc "c" (self: ^Object) -> UInteger {
  44. return msgSend(UInteger, self, "retainCount")
  45. }
  46. @(objc_type=Object, objc_name="class")
  47. class :: proc "c" (self: ^Object) -> Class {
  48. return msgSend(Class, self, "class")
  49. }
  50. @(objc_type=Object, objc_name="hash")
  51. hash :: proc "c" (self: ^Object) -> UInteger {
  52. return msgSend(UInteger, self, "hash")
  53. }
  54. @(objc_type=Object, objc_name="isEqual")
  55. isEqual :: proc "c" (self, pObject: ^Object) -> BOOL {
  56. return msgSend(BOOL, self, "isEqual:", pObject)
  57. }
  58. @(objc_type=Object, objc_name="description")
  59. description :: proc "c" (self: ^Object) -> ^String {
  60. return msgSend(^String, self, "description")
  61. }
  62. @(objc_type=Object, objc_name="debugDescription")
  63. debugDescription :: proc "c" (self: ^Object) -> ^String {
  64. if msgSendSafeCheck(self, intrinsics.objc_find_selector("debugDescription")) {
  65. return msgSend(^String, self, "debugDescription")
  66. }
  67. return nil
  68. }
  69. bridgingCast :: proc "c" ($T: typeid, obj: ^Object) where intrinsics.type_is_pointer(T), intrinsics.type_is_subtype_of(T, ^Object) {
  70. return (T)(obj)
  71. }
  72. @(objc_class="NSCoder")
  73. Coder :: struct {using _: Object}
  74. // TODO(bill): Implement all the methods for this massive type