]> err.no Git - sope/blob - sope-core/samples/subclassing.m
added OSX framework support
[sope] / sope-core / samples / subclassing.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
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
9   later version.
10
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.
15
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
19   02111-1307, USA.
20 */
21
22 #import <Foundation/Foundation.h>
23 #include <NGExtensions/NGObjCRuntime.h>
24 #include <objc/objc.h>
25
26 #if NeXT_Foundation_LIBRARY || COCOA_Foundation_LIBRARY
27 #  include <NGExtensions/NGObjectMacros.h>
28 #endif
29
30 #if APPLE_RUNTIME
31 #  include <objc/objc-class.h>
32 #  ifndef sel_get_name
33 #    define sel_get_name sel_getName
34 #  endif
35 #endif
36
37 static void myPrint(id self, SEL _cmd, int arg) {
38   NSLog(@"%s: self=%@, _cmd=%@, arg=%i", 
39         __PRETTY_FUNCTION__,
40         self, NSStringFromSelector(_cmd), arg);
41 }
42
43 @interface NSObject(MyPrint)
44 + (void)myPrint:(int)i;
45 - (void)myPrint:(int)i;
46 @end
47
48 @interface TestSubclassing : NSObject
49 + (void)run;
50 @end
51
52 @implementation TestSubclassing
53
54 - (void)printPreInfo {
55   NSLog(@"mt: %s", [NSWillBecomeMultiThreadedNotification cString]);
56
57   NSLog(@"class NSObject:  size=%d", [NSObject  instanceSize]);
58   NSLog(@"class NSString:  size=%d", [NSString  instanceSize]);
59   NSLog(@"class NSRunLoop: size=%d", [NSRunLoop instanceSize]);
60 }
61
62 - (void)testNSObjectSubclassing {
63   Class c;
64   id    o;
65
66   /* subclass NSObject */
67   
68   c = [NSObject subclass:@"MyObject"
69                 ivars:@"blah", @"@", @"blub", @"c", @"blab", @"s", nil];
70   
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);
74   
75   NSLog(@"MyObject: %@", c);
76   o = [[c alloc] init];
77   NSLog(@"MyObject-instance: %@", o);
78   [o release]; o = nil;
79 }
80
81 - (void)testInstanceAddMethods {
82   Class c;
83   id    o;
84   
85   c = NSClassFromString(@"MyObject");
86   o = [[c alloc] init];
87   NSLog(@"MyObject-instance: %@", o);
88
89   NSLog(@" new instance respondsto 'myPrint:': %s",
90         [o respondsToSelector:@selector(myPrint:)] ? "yes" : "no");
91
92   NSLog(@" adding selector 'myPrint:' ...");
93   
94   [c addMethods:@selector(myPrint:), @"v@:i", myPrint, nil];
95   NSLog(@" instance respondsto 'myPrint' after add: %s",
96         [o respondsToSelector:@selector(myPrint:)] ? "yes" : "no");
97
98   NSLog(@" call 'myPrint:14':");
99   [o myPrint:14];
100   
101   [o release]; o = nil;
102 }
103
104 - (void)testClassAddMethods {
105   Class c;
106   
107   c = NSClassFromString(@"MyObject");
108
109   NSLog(@" class respondsto 'myPrint': %s",
110         [c respondsToSelector:@selector(myPrint:)] ? "yes" : "no");
111   
112   NSLog(@" adding selector 'myPrint:' ...");
113   [c addClassMethods:@selector(myPrint:), @"v@:i", myPrint, nil];
114   
115   NSLog(@" class respondsto 'myPrint' after add: %s",
116         [c respondsToSelector:@selector(myPrint:)] ? "yes" : "no");
117
118   NSLog(@" call 'myPrint:42':");
119   [c myPrint:42];
120 }
121
122 - (void)testNSRunLoopSubclassing {
123   Class c;
124   
125   c = [NSRunLoop subclass:@"MyRunLoop"
126                  ivars:@"blah", @"@", @"blub", @"c", @"blab", @"s", nil];
127
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);
131   
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"]);
138 }
139
140 - (void)run {
141   [self printPreInfo];
142   [self testNSObjectSubclassing];
143   [self testInstanceAddMethods];
144   [self testClassAddMethods];
145   [self testNSRunLoopSubclassing];
146 }
147
148 + (void)run {
149   [[[[self alloc] init] autorelease] run];
150 }
151
152 @end /* TestSubclassing */
153
154 int main(int argc, char **argv, char **env) {
155   NSAutoreleasePool *pool;
156   
157   pool = [[NSAutoreleasePool alloc] init];
158 #if LIB_FOUNDATION_LIBRARY
159   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
160 #endif
161
162   [TestSubclassing run];
163   
164   [pool release];
165   return 0;
166 }