naett_objc.h (2179B)
1 #ifndef NAETT_OBJC_H 2 #define NAETT_OBJC_H 3 4 #if defined(__IOS__) || defined (__MACOS__) 5 #include <assert.h> 6 #include <math.h> 7 8 #include <objc/NSObjCRuntime.h> 9 #include <objc/message.h> 10 #include <objc/objc.h> 11 #include <objc/runtime.h> 12 13 #if defined(__OBJC__) && __has_feature(objc_arc) 14 #error "ARC is not supported" 15 #endif 16 17 // ABI is a bit different between platforms 18 #ifdef __arm64__ 19 #define abi_objc_msgSend_stret objc_msgSend 20 #else 21 #define abi_objc_msgSend_stret objc_msgSend_stret 22 #endif 23 #ifdef __i386__ 24 #define abi_objc_msgSend_fpret objc_msgSend_fpret 25 #else 26 #define abi_objc_msgSend_fpret objc_msgSend 27 #endif 28 29 #define objc_msgSendSuper_t(RET, ...) ((RET(*)(struct objc_super*, SEL, ##__VA_ARGS__))objc_msgSendSuper) 30 #define objc_msgSend_t(RET, ...) ((RET(*)(id, SEL, ##__VA_ARGS__))objc_msgSend) 31 #define objc_msgSend_stret_t(RET, ...) ((RET(*)(id, SEL, ##__VA_ARGS__))abi_objc_msgSend_stret) 32 #define objc_msgSend_id objc_msgSend_t(id) 33 #define objc_msgSend_void objc_msgSend_t(void) 34 #define objc_msgSend_void_id objc_msgSend_t(void, id) 35 #define objc_msgSend_void_bool objc_msgSend_t(void, bool) 36 37 #define sel(NAME) sel_registerName(NAME) 38 #define class(NAME) ((id)objc_getClass(NAME)) 39 #define makeClass(NAME, SUPER) \ 40 objc_allocateClassPair((Class)objc_getClass(SUPER), NAME, 0) 41 42 // Check here to get the signature right: 43 // https://nshipster.com/type-encodings/ 44 // https://ko9.org/posts/encode-types/ 45 #define addMethod(CLASS, NAME, IMPL, SIGNATURE) \ 46 if (!class_addMethod(CLASS, sel(NAME), (IMP) (IMPL), (SIGNATURE))) assert(false) 47 48 #define addIvar(CLASS, NAME, SIZE, SIGNATURE) \ 49 if (!class_addIvar(CLASS, NAME, SIZE, rint(log2(SIZE)), SIGNATURE)) assert(false) 50 51 #define objc_alloc(CLASS) objc_msgSend_id(class(CLASS), sel("alloc")) 52 #define autorelease(OBJ) objc_msgSend_void(OBJ, sel("autorelease")) 53 #define retain(OBJ) objc_msgSend_void(OBJ, sel("retain")) 54 #define release(OBJ) objc_msgSend_void(OBJ, sel("release")) 55 56 #if __LP64__ || NS_BUILD_32_LIKE_64 57 #define NSIntegerEncoding "q" 58 #define NSUIntegerEncoding "L" 59 #else 60 #define NSIntegerEncoding "i" 61 #define NSUIntegerEncoding "I" 62 #endif 63 64 #endif // defined(__IOS__) || defined (__MACOS__) 65 #endif // NAETT_OBJC_H