1{%MainUnit Foundation.pas} 2{ NSObject.h 3 Copyright (c) 1994-2005, Apple, Inc. All rights reserved. 4} 5 6{$ifdef HEADER} 7{$ifndef NSOBJECT_PAS_H} 8{$define NSOBJECT_PAS_H} 9 10//#import <Foundation/NSObjCRuntime.h> 11//#import <Foundation/NSZone.h> 12 13{ Class and method name strings } 14const 15 {*********** Base class ***********} 16 17 Str_NSObject = 'NSObject'; 18 19 Str_alloc = 'alloc'; 20 Str_init = 'init'; 21 22 Str_version = 'version'; 23 Str_setVersion = 'setVersion:'; 24 25 {*************** Basic protocols ***************} 26 27 Str_retain = 'retain'; 28 Str_release = 'release'; 29 Str_autorelease = 'autorelease'; 30 31{*********** Object Allocation / Deallocation *******} 32 33{FOUNDATION_EXPORT id <NSObject> NSAllocateObject(Class aClass, unsigned extraBytes, NSZone *zone); 34 35FOUNDATION_EXPORT void NSDeallocateObject(id <NSObject>object); 36 37FOUNDATION_EXPORT id <NSObject> NSCopyObject(id <NSObject>object, unsigned extraBytes, NSZone *zone); 38 39FOUNDATION_EXPORT BOOL NSShouldRetainWithZone(id <NSObject> anObject, NSZone *requestedZone); 40 41FOUNDATION_EXPORT void NSIncrementExtraRefCount(id object); 42 43FOUNDATION_EXPORT BOOL NSDecrementExtraRefCountWasZero(id object); 44 45FOUNDATION_EXPORT unsigned NSExtraRefCount(id object);} 46 47{$endif} 48{$endif} 49{$ifdef CLASSES} 50{$ifndef NSOBJECT_PAS_C} 51{$define NSOBJECT_PAS_C} 52 53{*********** Base class ***********} 54 55 NSObject = class 56 public 57 { class id } 58 ClassId: lobjc.id; 59 { object references } 60 allocbuf, Handle: lobjc.id; 61 { Constructor / Destructor } 62 constructor Create; virtual; 63 constructor CreateWithHandle(aHandle: lobjc.id); 64 destructor Destroy; override; 65 { Extra binding functions } 66 class function getClass: lobjc.id; virtual; 67 procedure AddMethods; virtual; 68 { Class creation methods } 69 procedure AddMethod(aName, aParameters: string; aPointer: Pointer); 70 function CreateClassDefinition(const name, superclassName: string): Boolean; 71 public 72{+ (void)load; 73 74+ (void)initialize; 75- (id)init; 76 77+ (id)new; 78+ (id)allocWithZone:(NSZone *)zone; 79+ (id)alloc; 80- (void)dealloc; 81 82#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 83- (void)finalize; 84#endif 85 86- (id)copy; 87- (id)mutableCopy; 88 89+ (id)copyWithZone:(NSZone *)zone; 90+ (id)mutableCopyWithZone:(NSZone *)zone; 91 92+ (Class)superclass; 93+ (Class)class; 94+ (void)poseAsClass:(Class)aClass; 95+ (BOOL)instancesRespondToSelector:(SEL)aSelector; 96+ (BOOL)conformsToProtocol:(Protocol *)protocol; 97- (IMP)methodForSelector:(SEL)aSelector; 98+ (IMP)instanceMethodForSelector:(SEL)aSelector;} 99 function version: PtrInt; 100 procedure setVersion(aVersion: PtrInt); 101{- (void)doesNotRecognizeSelector:(SEL)aSelector; 102- (void)forwardInvocation:(NSInvocation *)anInvocation; 103- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector; 104 105+ (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector; 106 107#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED 108+ (BOOL)isSubclassOfClass:(Class)aClass; 109#endif 110 111+ (NSString *)description; 112 113- (Class)classForCoder; 114- (id)replacementObjectForCoder:(NSCoder *)aCoder; 115- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder;} 116 117//@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator; 118//@class Protocol; 119 120{*************** Basic protocols ***************} 121 122{@protocol NSObject 123 124- (BOOL)isEqual:(id)object; 125- (unsigned)hash; 126 127- (Class)superclass; 128- (Class)class; 129- (id)self; 130- (NSZone *)zone; 131 132- (id)performSelector:(SEL)aSelector; 133- (id)performSelector:(SEL)aSelector withObject:(id)object; 134- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2; 135 136- (BOOL)isProxy; 137 138- (BOOL)isKindOfClass:(Class)aClass; 139- (BOOL)isMemberOfClass:(Class)aClass; 140- (BOOL)conformsToProtocol:(Protocol *)aProtocol; 141 142- (BOOL)respondsToSelector:(SEL)aSelector;} 143 144 function retain: lobjc.id; 145 procedure release; 146 function autorelease: lobjc.id; 147{- (unsigned)retainCount; 148 149- (NSString *)description; 150 151@end 152 153@protocol NSCopying 154 155- (id)copyWithZone:(NSZone *)zone; 156 157@end 158 159@protocol NSMutableCopying 160 161- (id)mutableCopyWithZone:(NSZone *)zone; 162 163@end 164 165@protocol NSCoding 166 167- (void)encodeWithCoder:(NSCoder *)aCoder; 168- (id)initWithCoder:(NSCoder *)aDecoder; 169 170@end } 171 172 end; 173 174{$endif} 175{$endif} 176{$ifdef IMPLEMENTATION} 177 178{*********** Base class ***********} 179 180constructor NSObject.Create; 181begin 182 ClassId := getClass(); 183 allocbuf := objc_msgSend(ClassId, sel_registerName(PChar(Str_alloc)), []); 184 Handle := objc_msgSend(allocbuf, sel_registerName(PChar(Str_init)), []); 185 186 { Adds custom methods, if any } 187 AddMethods(); 188end; 189 190constructor NSObject.CreateWithHandle(aHandle: lobjc.id); 191begin 192 ClassId := getClass(); 193 Handle := aHandle; 194end; 195 196destructor NSObject.Destroy; 197begin 198 if Handle <> nil then release; 199end; 200 201class function NSObject.getClass: lobjc.id; 202var 203 Str: string; 204begin 205 Str := ClassName(); 206 Result := objc_getClass(PChar(Str)); 207end; 208 209{ Defines the appropriate place to register methods in the objective-c runtime } 210procedure NSObject.AddMethods; 211begin 212 213end; 214 215procedure NSObject.AddMethod(aName, aParameters: string; aPointer: Pointer); 216{$ifdef CPU64} 217begin 218 class_addMethod(ClassId, sel_registerName(PChar(aName)), IMP(aPointer), PChar(aParameters)); 219end; 220{$else} 221var 222 method_list: Pobjc_method_list; 223begin 224 method_list := GetMem(SizeOf(objc_method_list)); { We can't free this until the last instance is freed } 225 226 method_list^.method_count := 1; 227 method_list^.method_list[0].method_name := sel_registerName(PChar(aName)); 228 method_list^.method_list[0].method_types := PChar(aParameters); 229 method_list^.method_list[0].method_imp := IMP(aPointer); 230 class_addMethods(ClassId, method_list); 231end; 232{$endif} 233 234function NSObject.CreateClassDefinition(const name, superclassName: string): Boolean; 235var 236 meta_class, super_class, new_class, root_class: Pobjc_class; 237begin 238 // Ensure that the superclass exists and that someone 239 // hasn't already implemented a class with the same name 240 // 241 super_class := Pobjc_class(objc_lookUpClass (PChar(superclassName))); 242 243 if (super_class = nil) then Exit(False); 244 245 if (objc_lookUpClass (PChar(name)) <> nil) then Exit(False); 246 247 {$ifdef CPU64} 248 new_class := objc_allocateClassPair(super_class, PChar(name), 0); 249 if new_class = nil then Exit(False); 250 251 //meta_class = object_getClass(new_class); 252 objc_registerClassPair(new_class); 253 {$else} 254 // Find the root class 255 // 256 root_class := super_class; 257 while ( root_class^.super_class <> nil ) do 258 begin 259 root_class := root_class^.super_class; 260 end; 261 262 // Allocate space for the class and its metaclass 263 // 264 new_class := CFAllocatorAllocate(kCFAllocatorMalloc, 2 * SizeOf(objc_class), 0); 265 FillChar(new_class^, 2 * SizeOf(objc_class), $0); 266 meta_class := @new_class[1]; 267 268 // setup class 269 new_class^.isa := meta_class; 270 new_class^.info := CLS_CLASS; 271 meta_class^.info := CLS_META; 272 273 // Create a copy of the class name. 274 // For efficiency, we have the metaclass and the class itself 275 // to share this copy of the name, but this is not a requirement 276 // imposed by the runtime. 277 // 278 new_class^.name := CFAllocatorAllocate(kCFAllocatorMalloc, Length(name) + 1, 0); 279 SysUtils.strcopy(new_class^.name, PChar(name)); 280 meta_class^.name := new_class^.name; 281 282 // Allocate empty method lists. 283 // We can add methods later. 284 // 285 new_class^.methodLists := CFAllocatorAllocate(kCFAllocatorMalloc, sizeof(Pobjc_method_list), 0); 286 new_class^.methodLists^ := Pointer(-1); 287 meta_class^.methodLists := CFAllocatorAllocate(kCFAllocatorMalloc, sizeof(Pobjc_method_list), 0); 288 meta_class^.methodLists^ := Pointer(-1); 289 290 // Connect the class definition to the class hierarchy: 291 // Connect the class to the superclass. 292 // Connect the metaclass to the metaclass of the superclass. 293 // Connect the metaclass of the metaclass to the metaclass of the root class. 294 // 295 new_class^.super_class := super_class; 296 meta_class^.super_class := super_class^.isa; 297 meta_class^.isa := Pointer(root_class^.isa); 298 299 // Set the sizes of the class and the metaclass. 300 // 301 new_class^.instance_size := super_class^.instance_size; 302 meta_class^.instance_size := meta_class^.super_class^.instance_size; 303 304 // Finally, register the class with the runtime. 305 // 306 objc_addClass( new_class ); 307 {$endif} 308 309 Result := True; 310end; 311 312function NSObject.version: PtrInt; 313begin 314 Result := {%H-}PtrInt(objc_msgSend(Handle, sel_registerName(PChar(Str_version)), [])); 315end; 316 317procedure NSObject.setVersion(aVersion: PtrInt); 318type 319 TmsgSendWrapper = procedure (param1: lobjc.id; param2: SEL;_anInt: PtrInt); cdecl; 320var 321 vmethod: TmsgSendWrapper; 322begin 323 vmethod := TmsgSendWrapper(@objc_msgSend); 324 vmethod(Handle, sel_registerName(PChar(Str_setVersion)), aVersion); 325end; 326 327{*************** Basic protocols ***************} 328 329function NSObject.retain: lobjc.id; 330begin 331 Result := objc_msgSend(Handle, sel_registerName(PChar(Str_retain)), []); 332end; 333 334procedure NSObject.release; 335begin 336 objc_msgSend(Handle, sel_registerName(PChar(Str_release)), []); 337 Handle:=nil; 338end; 339 340function NSObject.autorelease: lobjc.id; 341begin 342 Result := objc_msgSend(Handle, sel_registerName(PChar(Str_autorelease)), []); 343end; 344 345{$endif} 346 347