2008-05-09 19:51:36 +00:00
|
|
|
{%MainUnit Foundation.pas}
|
2008-02-17 07:38:48 +00:00
|
|
|
{ NSObject.h
|
|
|
|
Copyright (c) 1994-2005, Apple, Inc. All rights reserved.
|
|
|
|
}
|
|
|
|
|
2008-02-22 22:39:15 +00:00
|
|
|
{$ifdef HEADER}
|
|
|
|
{$ifndef NSOBJECT_PAS_H}
|
|
|
|
{$define NSOBJECT_PAS_H}
|
|
|
|
|
2008-02-17 07:38:48 +00:00
|
|
|
//#import <Foundation/NSObjCRuntime.h>
|
|
|
|
//#import <Foundation/NSZone.h>
|
|
|
|
|
2008-03-10 11:01:03 +00:00
|
|
|
{ Class and method name strings }
|
2008-02-17 07:38:48 +00:00
|
|
|
const
|
2008-03-10 11:01:03 +00:00
|
|
|
{*********** Base class ***********}
|
|
|
|
|
2008-03-04 09:16:58 +00:00
|
|
|
Str_NSObject = 'NSObject';
|
|
|
|
|
2008-02-17 07:38:48 +00:00
|
|
|
Str_alloc = 'alloc';
|
|
|
|
Str_init = 'init';
|
2008-03-10 11:01:03 +00:00
|
|
|
|
|
|
|
{*************** Basic protocols ***************}
|
|
|
|
|
|
|
|
Str_retain = 'retain';
|
2008-02-17 07:38:48 +00:00
|
|
|
Str_release = 'release';
|
2008-09-29 00:47:50 +00:00
|
|
|
Str_autorelease = 'autorelease';
|
2008-02-17 07:38:48 +00:00
|
|
|
|
2008-02-22 22:39:15 +00:00
|
|
|
{*********** Object Allocation / Deallocation *******}
|
|
|
|
|
|
|
|
{FOUNDATION_EXPORT id <NSObject> NSAllocateObject(Class aClass, unsigned extraBytes, NSZone *zone);
|
|
|
|
|
|
|
|
FOUNDATION_EXPORT void NSDeallocateObject(id <NSObject>object);
|
|
|
|
|
|
|
|
FOUNDATION_EXPORT id <NSObject> NSCopyObject(id <NSObject>object, unsigned extraBytes, NSZone *zone);
|
|
|
|
|
|
|
|
FOUNDATION_EXPORT BOOL NSShouldRetainWithZone(id <NSObject> anObject, NSZone *requestedZone);
|
|
|
|
|
|
|
|
FOUNDATION_EXPORT void NSIncrementExtraRefCount(id object);
|
|
|
|
|
|
|
|
FOUNDATION_EXPORT BOOL NSDecrementExtraRefCountWasZero(id object);
|
|
|
|
|
|
|
|
FOUNDATION_EXPORT unsigned NSExtraRefCount(id object);}
|
|
|
|
|
|
|
|
{$endif}
|
|
|
|
{$endif}
|
|
|
|
{$ifdef CLASSES}
|
|
|
|
{$ifndef NSOBJECT_PAS_C}
|
|
|
|
{$define NSOBJECT_PAS_C}
|
|
|
|
|
2008-03-10 11:01:03 +00:00
|
|
|
{*********** Base class ***********}
|
|
|
|
|
2008-02-17 07:38:48 +00:00
|
|
|
NSObject = class
|
|
|
|
public
|
|
|
|
{ class id }
|
|
|
|
ClassId: objc.id;
|
|
|
|
{ object references }
|
|
|
|
allocbuf, Handle: objc.id;
|
|
|
|
{ Constructor / Destructor }
|
|
|
|
constructor Create; virtual;
|
2008-03-02 02:17:37 +00:00
|
|
|
constructor CreateWithHandle(aHandle: objc.id);
|
2008-02-17 07:38:48 +00:00
|
|
|
destructor Destroy; override;
|
2008-03-04 09:16:58 +00:00
|
|
|
{ Extra binding functions }
|
2008-05-09 19:51:36 +00:00
|
|
|
class function getClass: objc.id; virtual;
|
2008-03-14 01:40:16 +00:00
|
|
|
procedure AddMethods; virtual;
|
2008-03-10 11:51:50 +00:00
|
|
|
{ Class creation methods }
|
|
|
|
procedure AddMethod(aName, aParameters: string; aPointer: Pointer);
|
2008-03-14 01:05:09 +00:00
|
|
|
function CreateClassDefinition(const name, superclassName: string): Boolean;
|
2008-03-04 09:16:58 +00:00
|
|
|
public
|
2008-02-17 07:38:48 +00:00
|
|
|
{+ (void)load;
|
|
|
|
|
|
|
|
+ (void)initialize;
|
|
|
|
- (id)init;
|
|
|
|
|
|
|
|
+ (id)new;
|
|
|
|
+ (id)allocWithZone:(NSZone *)zone;
|
|
|
|
+ (id)alloc;
|
|
|
|
- (void)dealloc;
|
|
|
|
|
|
|
|
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
|
|
|
|
- (void)finalize;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
- (id)copy;
|
|
|
|
- (id)mutableCopy;
|
|
|
|
|
|
|
|
+ (id)copyWithZone:(NSZone *)zone;
|
|
|
|
+ (id)mutableCopyWithZone:(NSZone *)zone;
|
|
|
|
|
|
|
|
+ (Class)superclass;
|
|
|
|
+ (Class)class;
|
|
|
|
+ (void)poseAsClass:(Class)aClass;
|
|
|
|
+ (BOOL)instancesRespondToSelector:(SEL)aSelector;
|
|
|
|
+ (BOOL)conformsToProtocol:(Protocol *)protocol;
|
|
|
|
- (IMP)methodForSelector:(SEL)aSelector;
|
|
|
|
+ (IMP)instanceMethodForSelector:(SEL)aSelector;
|
|
|
|
+ (int)version;
|
|
|
|
+ (void)setVersion:(int)aVersion;
|
|
|
|
- (void)doesNotRecognizeSelector:(SEL)aSelector;
|
|
|
|
- (void)forwardInvocation:(NSInvocation *)anInvocation;
|
|
|
|
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;
|
|
|
|
|
|
|
|
+ (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelector;
|
|
|
|
|
|
|
|
#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED
|
|
|
|
+ (BOOL)isSubclassOfClass:(Class)aClass;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
+ (NSString *)description;
|
|
|
|
|
|
|
|
- (Class)classForCoder;
|
|
|
|
- (id)replacementObjectForCoder:(NSCoder *)aCoder;
|
|
|
|
- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder;}
|
|
|
|
|
2008-03-10 11:01:03 +00:00
|
|
|
//@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
|
|
|
|
//@class Protocol;
|
|
|
|
|
|
|
|
{*************** Basic protocols ***************}
|
|
|
|
|
|
|
|
{@protocol NSObject
|
|
|
|
|
|
|
|
- (BOOL)isEqual:(id)object;
|
|
|
|
- (unsigned)hash;
|
|
|
|
|
|
|
|
- (Class)superclass;
|
|
|
|
- (Class)class;
|
|
|
|
- (id)self;
|
|
|
|
- (NSZone *)zone;
|
|
|
|
|
|
|
|
- (id)performSelector:(SEL)aSelector;
|
|
|
|
- (id)performSelector:(SEL)aSelector withObject:(id)object;
|
|
|
|
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
|
|
|
|
|
|
|
|
- (BOOL)isProxy;
|
|
|
|
|
|
|
|
- (BOOL)isKindOfClass:(Class)aClass;
|
|
|
|
- (BOOL)isMemberOfClass:(Class)aClass;
|
|
|
|
- (BOOL)conformsToProtocol:(Protocol *)aProtocol;
|
|
|
|
|
|
|
|
- (BOOL)respondsToSelector:(SEL)aSelector;}
|
|
|
|
|
|
|
|
function retain: objc.id;
|
|
|
|
procedure release;
|
2008-09-29 00:47:50 +00:00
|
|
|
function autorelease: objc.id;
|
|
|
|
{- (unsigned)retainCount;
|
2008-03-10 11:01:03 +00:00
|
|
|
|
|
|
|
- (NSString *)description;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@protocol NSCopying
|
|
|
|
|
|
|
|
- (id)copyWithZone:(NSZone *)zone;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@protocol NSMutableCopying
|
|
|
|
|
|
|
|
- (id)mutableCopyWithZone:(NSZone *)zone;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@protocol NSCoding
|
|
|
|
|
|
|
|
- (void)encodeWithCoder:(NSCoder *)aCoder;
|
|
|
|
- (id)initWithCoder:(NSCoder *)aDecoder;
|
|
|
|
|
|
|
|
@end }
|
|
|
|
|
2008-02-17 07:38:48 +00:00
|
|
|
end;
|
|
|
|
|
2008-02-22 22:39:15 +00:00
|
|
|
{$endif}
|
|
|
|
{$endif}
|
|
|
|
{$ifdef IMPLEMENTATION}
|
2008-02-17 07:38:48 +00:00
|
|
|
|
2008-03-10 11:01:03 +00:00
|
|
|
{*********** Base class ***********}
|
|
|
|
|
2008-02-22 22:39:15 +00:00
|
|
|
constructor NSObject.Create;
|
|
|
|
begin
|
2008-03-07 11:33:19 +00:00
|
|
|
ClassId := getClass();
|
|
|
|
allocbuf := objc_msgSend(ClassId, sel_registerName(PChar(Str_alloc)), []);
|
|
|
|
Handle := objc_msgSend(allocbuf, sel_registerName(PChar(Str_init)), []);
|
2008-03-14 01:40:16 +00:00
|
|
|
|
|
|
|
{ Adds custom methods, if any }
|
|
|
|
AddMethods();
|
2008-02-22 22:39:15 +00:00
|
|
|
end;
|
2008-02-17 07:38:48 +00:00
|
|
|
|
2008-03-02 02:17:37 +00:00
|
|
|
constructor NSObject.CreateWithHandle(aHandle: objc.id);
|
|
|
|
begin
|
2008-03-10 11:01:03 +00:00
|
|
|
ClassId := getClass();
|
2008-03-02 02:17:37 +00:00
|
|
|
Handle := aHandle;
|
|
|
|
end;
|
|
|
|
|
2008-02-22 22:39:15 +00:00
|
|
|
destructor NSObject.Destroy;
|
|
|
|
begin
|
2008-03-14 01:05:09 +00:00
|
|
|
if Handle <> nil then release;
|
2008-02-22 22:39:15 +00:00
|
|
|
end;
|
2008-02-17 07:38:48 +00:00
|
|
|
|
2008-05-09 19:51:36 +00:00
|
|
|
class function NSObject.getClass: objc.id;
|
2008-06-24 02:01:19 +00:00
|
|
|
var
|
|
|
|
Str: string;
|
2008-03-04 09:16:58 +00:00
|
|
|
begin
|
2008-06-24 02:01:19 +00:00
|
|
|
Str := ClassName();
|
|
|
|
Result := objc_getClass(PChar(Str));
|
2008-03-04 09:16:58 +00:00
|
|
|
end;
|
|
|
|
|
2008-03-14 01:40:16 +00:00
|
|
|
{ Defines the appropriate place to register methods in the objective-c runtime }
|
|
|
|
procedure NSObject.AddMethods;
|
|
|
|
begin
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
2008-03-10 11:51:50 +00:00
|
|
|
procedure NSObject.AddMethod(aName, aParameters: string; aPointer: Pointer);
|
|
|
|
var
|
|
|
|
method_list: Pobjc_method_list;
|
|
|
|
begin
|
|
|
|
method_list := GetMem(SizeOf(objc_method_list)); { We can't free this until the last instance is freed }
|
|
|
|
|
|
|
|
method_list^.method_count := 1;
|
|
|
|
method_list^.method_list[0].method_name := sel_registerName(PChar(aName));
|
|
|
|
method_list^.method_list[0].method_types := PChar(aParameters);
|
|
|
|
method_list^.method_list[0].method_imp := IMP(aPointer);
|
|
|
|
class_addMethods(ClassId, method_list);
|
|
|
|
end;
|
|
|
|
|
2008-03-14 01:05:09 +00:00
|
|
|
function NSObject.CreateClassDefinition(const name, superclassName: string): Boolean;
|
|
|
|
var
|
|
|
|
meta_class, super_class, new_class, root_class: Pobjc_class;
|
|
|
|
begin
|
|
|
|
// Ensure that the superclass exists and that someone
|
|
|
|
// hasn't already implemented a class with the same name
|
|
|
|
//
|
|
|
|
super_class := Pobjc_class(objc_lookUpClass (PChar(superclassName)));
|
|
|
|
|
|
|
|
if (super_class = nil) then Exit(False);
|
|
|
|
|
|
|
|
if (objc_lookUpClass (PChar(name)) <> nil) then Exit(False);
|
|
|
|
|
|
|
|
// Find the root class
|
|
|
|
//
|
|
|
|
root_class := super_class;
|
|
|
|
while ( root_class^.super_class <> nil ) do
|
|
|
|
begin
|
|
|
|
root_class := root_class^.super_class;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Allocate space for the class and its metaclass
|
|
|
|
//
|
|
|
|
new_class := CFAllocatorAllocate(kCFAllocatorMalloc, 2 * SizeOf(objc_class), 0);
|
2008-10-08 11:28:15 +00:00
|
|
|
FillChar(new_class^, 2 * SizeOf(objc_class), $0);
|
2008-03-14 01:05:09 +00:00
|
|
|
meta_class := @new_class[1];
|
|
|
|
|
|
|
|
// setup class
|
|
|
|
new_class^.isa := meta_class;
|
|
|
|
new_class^.info := CLS_CLASS;
|
|
|
|
meta_class^.info := CLS_META;
|
|
|
|
|
|
|
|
// Create a copy of the class name.
|
|
|
|
// For efficiency, we have the metaclass and the class itself
|
|
|
|
// to share this copy of the name, but this is not a requirement
|
|
|
|
// imposed by the runtime.
|
|
|
|
//
|
|
|
|
new_class^.name := CFAllocatorAllocate(kCFAllocatorMalloc, Length(name) + 1, 0);
|
|
|
|
SysUtils.strcopy(new_class^.name, PChar(name));
|
|
|
|
meta_class^.name := new_class^.name;
|
|
|
|
|
|
|
|
// Allocate empty method lists.
|
|
|
|
// We can add methods later.
|
|
|
|
//
|
|
|
|
new_class^.methodLists := CFAllocatorAllocate(kCFAllocatorMalloc, sizeof(Pobjc_method_list), 0);
|
|
|
|
new_class^.methodLists^ := Pointer(-1);
|
|
|
|
meta_class^.methodLists := CFAllocatorAllocate(kCFAllocatorMalloc, sizeof(Pobjc_method_list), 0);
|
|
|
|
meta_class^.methodLists^ := Pointer(-1);
|
|
|
|
|
|
|
|
// Connect the class definition to the class hierarchy:
|
|
|
|
// Connect the class to the superclass.
|
|
|
|
// Connect the metaclass to the metaclass of the superclass.
|
|
|
|
// Connect the metaclass of the metaclass to the metaclass of the root class.
|
|
|
|
//
|
|
|
|
new_class^.super_class := super_class;
|
|
|
|
meta_class^.super_class := super_class^.isa;
|
|
|
|
meta_class^.isa := Pointer(root_class^.isa);
|
|
|
|
|
|
|
|
// Set the sizes of the class and the metaclass.
|
|
|
|
//
|
|
|
|
new_class^.instance_size := super_class^.instance_size;
|
|
|
|
meta_class^.instance_size := meta_class^.super_class^.instance_size;
|
|
|
|
|
|
|
|
// Finally, register the class with the runtime.
|
|
|
|
//
|
|
|
|
objc_addClass( new_class );
|
|
|
|
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
2008-03-10 11:01:03 +00:00
|
|
|
{*************** Basic protocols ***************}
|
|
|
|
|
|
|
|
function NSObject.retain: objc.id;
|
|
|
|
begin
|
|
|
|
Result := objc_msgSend(Handle, sel_registerName(PChar(Str_retain)), []);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure NSObject.release;
|
|
|
|
begin
|
|
|
|
objc_msgSend(Handle, sel_registerName(PChar(Str_release)), []);
|
2009-03-06 08:18:26 +00:00
|
|
|
Handle:=nil;
|
2008-03-10 11:01:03 +00:00
|
|
|
end;
|
|
|
|
|
2008-09-29 00:47:50 +00:00
|
|
|
function NSObject.autorelease: objc.id;
|
|
|
|
begin
|
|
|
|
Result := objc_msgSend(Handle, sel_registerName(PChar(Str_autorelease)), []);
|
|
|
|
end;
|
|
|
|
|
2008-02-22 22:39:15 +00:00
|
|
|
{$endif}
|
2008-02-17 07:38:48 +00:00
|
|
|
|