2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #import <Foundation/Foundation.h>
23 #include <NGExtensions/NGObjCRuntime.h>
24 #include <objc/objc.h>
26 #if NeXT_Foundation_LIBRARY || COCOA_Foundation_LIBRARY
27 # include <NGExtensions/NGObjectMacros.h>
31 # include <objc/objc-class.h>
33 # define sel_get_name sel_getName
37 static void myPrint(id self, SEL _cmd, int arg) {
38 NSLog(@"%s: self=%@, _cmd=%@, arg=%i",
40 self, NSStringFromSelector(_cmd), arg);
43 @interface NSObject(MyPrint)
44 + (void)myPrint:(int)i;
45 - (void)myPrint:(int)i;
48 @interface TestSubclassing : NSObject
52 @implementation TestSubclassing
54 - (void)printPreInfo {
55 NSLog(@"mt: %s", [NSWillBecomeMultiThreadedNotification cString]);
57 NSLog(@"class NSObject: size=%d", [NSObject instanceSize]);
58 NSLog(@"class NSString: size=%d", [NSString instanceSize]);
59 NSLog(@"class NSRunLoop: size=%d", [NSRunLoop instanceSize]);
62 - (void)testNSObjectSubclassing {
66 /* subclass NSObject */
68 c = [NSObject subclass:@"MyObject"
69 ivars:@"blah", @"@", @"blub", @"c", @"blab", @"s", nil];
71 printf("MyObject is 0x%08X\n", (unsigned)c);
72 printf("MyObject name is %s\n", c->name);
73 printf("MyObject super-name is %s\n", c->super_class->name);
75 NSLog(@"MyObject: %@", c);
77 NSLog(@"MyObject-instance: %@", o);
81 - (void)testInstanceAddMethods {
85 c = NSClassFromString(@"MyObject");
87 NSLog(@"MyObject-instance: %@", o);
89 NSLog(@" new instance respondsto 'myPrint:': %s",
90 [o respondsToSelector:@selector(myPrint:)] ? "yes" : "no");
92 NSLog(@" adding selector 'myPrint:' ...");
94 [c addMethods:@selector(myPrint:), @"v@:i", myPrint, nil];
95 NSLog(@" instance respondsto 'myPrint' after add: %s",
96 [o respondsToSelector:@selector(myPrint:)] ? "yes" : "no");
98 NSLog(@" call 'myPrint:14':");
101 [o release]; o = nil;
104 - (void)testClassAddMethods {
107 c = NSClassFromString(@"MyObject");
109 NSLog(@" class respondsto 'myPrint': %s",
110 [c respondsToSelector:@selector(myPrint:)] ? "yes" : "no");
112 NSLog(@" adding selector 'myPrint:' ...");
113 [c addClassMethods:@selector(myPrint:), @"v@:i", myPrint, nil];
115 NSLog(@" class respondsto 'myPrint' after add: %s",
116 [c respondsToSelector:@selector(myPrint:)] ? "yes" : "no");
118 NSLog(@" call 'myPrint:42':");
122 - (void)testNSRunLoopSubclassing {
125 c = [NSRunLoop subclass:@"MyRunLoop"
126 ivars:@"blah", @"@", @"blub", @"c", @"blab", @"s", nil];
128 printf("MyRunLoop is 0x%08X\n", (unsigned int)c);
129 printf("MyRunLoop name is %s\n", c->name);
130 printf("MyRunLoop super-name is %s\n", c->super_class->name);
132 NSLog(@"MyRunLoop: %@", c);
133 NSLog(@"MyRunLoop-instance: %@", [[c alloc] init]);
134 NSLog(@"MyRunLoop ivars: class=%@ all=%@",
135 [c instanceVariableNames], [c allInstanceVariableNames]);
136 NSLog(@" signature of blub: %@",
137 [c signatureOfInstanceVariableWithName:@"blub"]);
142 [self testNSObjectSubclassing];
143 [self testInstanceAddMethods];
144 [self testClassAddMethods];
145 [self testNSRunLoopSubclassing];
149 [[[[self alloc] init] autorelease] run];
152 @end /* TestSubclassing */
154 int main(int argc, char **argv, char **env) {
155 NSAutoreleasePool *pool;
157 pool = [[NSAutoreleasePool alloc] init];
158 #if LIB_FOUNDATION_LIBRARY
159 [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
162 [TestSubclassing run];