From 10655bc37c51645ecdcfa2cd8c059a5e0fb17335 Mon Sep 17 00:00:00 2001 From: Arslaan Pathan Date: Sat, 3 Oct 2026 21:27:26 +1300 Subject: object introspection and respondsToSelector --- src/Object.m | 63 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 14 deletions(-) (limited to 'src/Object.m') diff --git a/src/Object.m b/src/Object.m index de0f343..b9250ff 100644 --- a/src/Object.m +++ b/src/Object.m @@ -6,24 +6,12 @@ @implementation PXObject +/* init and lifecycle */ + + (instancetype)alloc { return class_createInstance(self, 0); } -#ifdef PARADOX_DEBUG_FEATURES -+ (instancetype)allocDebug { - printf("alloc: self = %p\n", (void*)self); - printf("alloc: instance size = %zu\n", class_getInstanceSize(self)); - printf("alloc: sizeof(Class) = %zu\n", sizeof(Class)); - - id object = [self alloc]; - - printf("alloc: object = %p\n", (void*)object); - - return object; -} -#endif - + (instancetype) new { return [[self alloc] init]; } @@ -48,10 +36,57 @@ object_dispose(self); } +/* object introspection */ +- (Class)class { + return object_getClass(self); +} + +- (Class)superclass { + return object_getSuperclass([self class]); +} + +- (const char *)className { + return class_getName([self class]); +} + +- (BOOL)isKindOfClass:(Class)class { + Class current = [self class]; + + while (current != nil) { + if (current == class) + return YES; + + current = class_getSuperclass(current); + } + + return NO; +} + +- (BOOL)isMemberOfClass:(Class)class { + return ([self class] == class); +} + +/* selector stuff */ +- (BOOL)respondsToSelector:(SEL)selector { + return object_respondsToSelector([self class], selector); +} + #ifdef PARADOX_DEBUG_FEATURES - (void)healthCheck { printf("PXObject health check: %p\n", (void*)self); } + ++ (instancetype)allocDebug { + printf("alloc: self = %p\n", (void*)self); + printf("alloc: instance size = %zu\n", class_getInstanceSize(self)); + printf("alloc: sizeof(Class) = %zu\n", sizeof(Class)); + + id object = [self alloc]; + + printf("alloc: object = %p\n", (void*)object); + + return object; +} #endif @end -- cgit v1.2.3