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