2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #import <Foundation/Foundation.h>
24 #include <NGExtensions/NGObjCRuntime.h>
25 #include <objc/objc.h>
27 #if NeXT_Foundation_LIBRARY || COCOA_Foundation_LIBRARY
28 # include <NGExtensions/NGObjectMacros.h>
32 # include <objc/objc-class.h>
34 # define sel_get_name sel_getName
38 static void myPrint(id self, SEL _cmd, int arg) {
39 NSLog(@"%s: self=%@, _cmd=%@, arg=%i",
41 self, NSStringFromSelector(_cmd), arg);
44 @interface NSObject(MyPrint)
45 + (void)myPrint:(int)i;
46 - (void)myPrint:(int)i;
49 @interface TestSubclassing : NSObject
53 @implementation TestSubclassing
55 - (void)printPreInfo {
56 NSLog(@"mt: %s", [NSWillBecomeMultiThreadedNotification cString]);
58 NSLog(@"class NSObject: size=%d", [NSObject instanceSize]);
59 NSLog(@"class NSString: size=%d", [NSString instanceSize]);
60 NSLog(@"class NSRunLoop: size=%d", [NSRunLoop instanceSize]);
63 - (void)testNSObjectSubclassing {
67 /* subclass NSObject */
69 c = [NSObject subclass:@"MyObject"
70 ivars:@"blah", @"@", @"blub", @"c", @"blab", @"s", nil];
72 printf("MyObject is 0x%08X\n", (unsigned)c);
73 printf("MyObject name is %s\n", c->name);
74 printf("MyObject super-name is %s\n", c->super_class->name);
76 NSLog(@"MyObject: %@", c);
78 NSLog(@"MyObject-instance: %@", o);
82 - (void)testInstanceAddMethods {
86 c = NSClassFromString(@"MyObject");
88 NSLog(@"MyObject-instance: %@", o);
90 NSLog(@" new instance respondsto 'myPrint:': %s",
91 [o respondsToSelector:@selector(myPrint:)] ? "yes" : "no");
93 NSLog(@" adding selector 'myPrint:' ...");
95 [c addMethods:@selector(myPrint:), @"v@:i", myPrint, nil];
96 NSLog(@" instance respondsto 'myPrint' after add: %s",
97 [o respondsToSelector:@selector(myPrint:)] ? "yes" : "no");
99 NSLog(@" call 'myPrint:14':");
102 [o release]; o = nil;
105 - (void)testClassAddMethods {
108 c = NSClassFromString(@"MyObject");
110 NSLog(@" class respondsto 'myPrint': %s",
111 [c respondsToSelector:@selector(myPrint:)] ? "yes" : "no");
113 NSLog(@" adding selector 'myPrint:' ...");
114 [c addClassMethods:@selector(myPrint:), @"v@:i", myPrint, nil];
116 NSLog(@" class respondsto 'myPrint' after add: %s",
117 [c respondsToSelector:@selector(myPrint:)] ? "yes" : "no");
119 NSLog(@" call 'myPrint:42':");
123 - (void)testNSRunLoopSubclassing {
126 c = [NSRunLoop subclass:@"MyRunLoop"
127 ivars:@"blah", @"@", @"blub", @"c", @"blab", @"s", nil];
129 printf("MyRunLoop is 0x%08X\n", (unsigned int)c);
130 printf("MyRunLoop name is %s\n", c->name);
131 printf("MyRunLoop super-name is %s\n", c->super_class->name);
133 NSLog(@"MyRunLoop: %@", c);
134 NSLog(@"MyRunLoop-instance: %@", [[c alloc] init]);
135 NSLog(@"MyRunLoop ivars: class=%@ all=%@",
136 [c instanceVariableNames], [c allInstanceVariableNames]);
137 NSLog(@" signature of blub: %@",
138 [c signatureOfInstanceVariableWithName:@"blub"]);
143 [self testNSObjectSubclassing];
144 [self testInstanceAddMethods];
145 [self testClassAddMethods];
146 [self testNSRunLoopSubclassing];
150 [[[[self alloc] init] autorelease] run];
153 @end /* TestSubclassing */
155 int main(int argc, char **argv, char **env) {
156 NSAutoreleasePool *pool;
158 pool = [[NSAutoreleasePool alloc] init];
159 #if LIB_FOUNDATION_LIBRARY
160 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
163 [TestSubclassing run];