]>
Commit | Line | Data |
---|---|---|
c72fc2d9 | 1 | /* The implementation of class Object for Objective-C. |
5b9b7438 | 2 | Copyright (C) 1993, 1994, 1995, 1997 Free Software Foundation, Inc. |
c72fc2d9 TW |
3 | |
4 | This file is part of GNU CC. | |
5 | ||
6 | GNU CC is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU General Public License as published by the | |
8 | Free Software Foundation; either version 2, or (at your option) any | |
9 | later version. | |
10 | ||
11 | GNU CC is distributed in the hope that it will be useful, but WITHOUT | |
12 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | |
14 | License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU CC; see the file COPYING. If not, write to | |
6bc06b8f RK |
18 | the Free Software Foundation, 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
c72fc2d9 TW |
20 | |
21 | /* As a special exception, if you link this library with files compiled | |
22 | with GCC to produce an executable, this does not cause the resulting | |
23 | executable to be covered by the GNU General Public License. This | |
24 | exception does not however invalidate any other reasons why the | |
25 | executable file might be covered by the GNU General Public License. */ | |
26 | ||
ccccb527 | 27 | #include <stdarg.h> |
b0033312 KKT |
28 | #include "objc/Object.h" |
29 | #include "objc/Protocol.h" | |
30 | #include "objc/objc-api.h" | |
31 | ||
c72fc2d9 TW |
32 | extern int errno; |
33 | ||
34 | #define MAX_CLASS_NAME_LEN 256 | |
35 | ||
36 | @implementation Object | |
37 | ||
c72fc2d9 TW |
38 | + initialize |
39 | { | |
40 | return self; | |
41 | } | |
42 | ||
c72fc2d9 TW |
43 | - init |
44 | { | |
45 | return self; | |
46 | } | |
47 | ||
c72fc2d9 TW |
48 | + new |
49 | { | |
50 | return [[self alloc] init]; | |
51 | } | |
52 | ||
c72fc2d9 TW |
53 | + alloc |
54 | { | |
55 | return class_create_instance(self); | |
56 | } | |
57 | ||
c72fc2d9 TW |
58 | - free |
59 | { | |
60 | return object_dispose(self); | |
61 | } | |
62 | ||
c72fc2d9 TW |
63 | - copy |
64 | { | |
65 | return [[self shallowCopy] deepen]; | |
66 | } | |
67 | ||
c72fc2d9 TW |
68 | - shallowCopy |
69 | { | |
70 | return object_copy(self); | |
71 | } | |
72 | ||
c72fc2d9 TW |
73 | - deepen |
74 | { | |
75 | return self; | |
76 | } | |
77 | ||
c72fc2d9 TW |
78 | - deepCopy |
79 | { | |
80 | return [self copy]; | |
81 | } | |
82 | ||
0994488a | 83 | - (Class)class |
c72fc2d9 TW |
84 | { |
85 | return object_get_class(self); | |
86 | } | |
87 | ||
0994488a | 88 | - (Class)superClass |
c72fc2d9 TW |
89 | { |
90 | return object_get_super_class(self); | |
91 | } | |
92 | ||
0994488a | 93 | - (MetaClass)metaClass |
c72fc2d9 TW |
94 | { |
95 | return object_get_meta_class(self); | |
96 | } | |
97 | ||
c72fc2d9 TW |
98 | - (const char *)name |
99 | { | |
100 | return object_get_class_name(self); | |
101 | } | |
102 | ||
c72fc2d9 TW |
103 | - self |
104 | { | |
105 | return self; | |
106 | } | |
107 | ||
c72fc2d9 TW |
108 | - (unsigned int)hash |
109 | { | |
a7ab3794 | 110 | return (size_t)self; |
c72fc2d9 TW |
111 | } |
112 | ||
c72fc2d9 TW |
113 | - (BOOL)isEqual:anObject |
114 | { | |
115 | return self==anObject; | |
116 | } | |
117 | ||
214a36e8 KKT |
118 | - (int)compare:anotherObject; |
119 | { | |
120 | if ([self isEqual:anotherObject]) | |
121 | return 0; | |
122 | // Ordering objects by their address is pretty useless, | |
123 | // so subclasses should override this is some useful way. | |
124 | else if (self > anotherObject) | |
125 | return 1; | |
126 | else | |
127 | return -1; | |
128 | } | |
129 | ||
c72fc2d9 TW |
130 | - (BOOL)isMetaClass |
131 | { | |
132 | return NO; | |
133 | } | |
134 | ||
c72fc2d9 TW |
135 | - (BOOL)isClass |
136 | { | |
137 | return object_is_class(self); | |
138 | } | |
139 | ||
c72fc2d9 TW |
140 | - (BOOL)isInstance |
141 | { | |
142 | return object_is_instance(self); | |
143 | } | |
144 | ||
0994488a | 145 | - (BOOL)isKindOf:(Class)aClassObject |
c72fc2d9 | 146 | { |
0994488a | 147 | Class class; |
c72fc2d9 TW |
148 | |
149 | for (class = self->isa; class!=Nil; class = class_get_super_class(class)) | |
150 | if (class==aClassObject) | |
151 | return YES; | |
152 | return NO; | |
153 | } | |
154 | ||
0994488a | 155 | - (BOOL)isMemberOf:(Class)aClassObject |
c72fc2d9 TW |
156 | { |
157 | return self->isa==aClassObject; | |
158 | } | |
159 | ||
c72fc2d9 TW |
160 | - (BOOL)isKindOfClassNamed:(const char *)aClassName |
161 | { | |
0994488a | 162 | Class class; |
c72fc2d9 TW |
163 | |
164 | if (aClassName!=NULL) | |
165 | for (class = self->isa; class!=Nil; class = class_get_super_class(class)) | |
166 | if (!strcmp(class_get_class_name(class), aClassName)) | |
167 | return YES; | |
168 | return NO; | |
169 | } | |
170 | ||
c72fc2d9 TW |
171 | - (BOOL)isMemberOfClassNamed:(const char *)aClassName |
172 | { | |
173 | return ((aClassName!=NULL) | |
174 | &&!strcmp(class_get_class_name(self->isa), aClassName)); | |
175 | } | |
176 | ||
c72fc2d9 TW |
177 | + (BOOL)instancesRespondTo:(SEL)aSel |
178 | { | |
179 | return class_get_instance_method(self, aSel)!=METHOD_NULL; | |
180 | } | |
181 | ||
c72fc2d9 TW |
182 | - (BOOL)respondsTo:(SEL)aSel |
183 | { | |
184 | return ((object_is_instance(self) | |
185 | ?class_get_instance_method(self->isa, aSel) | |
186 | :class_get_class_method(self->isa, aSel))!=METHOD_NULL); | |
187 | } | |
188 | ||
c72fc2d9 TW |
189 | + (IMP)instanceMethodFor:(SEL)aSel |
190 | { | |
191 | return method_get_imp(class_get_instance_method(self, aSel)); | |
192 | } | |
193 | ||
194 | // Indicates if the receiving class or instance conforms to the given protocol | |
195 | // not usually overridden by subclasses | |
ad4f2e42 KKT |
196 | // |
197 | // Modified 9/5/94 to always search the class object's protocol list, rather | |
198 | // than the meta class. | |
199 | ||
200 | + (BOOL) conformsTo: (Protocol*)aProtocol | |
c72fc2d9 TW |
201 | { |
202 | int i; | |
203 | struct objc_protocol_list* proto_list; | |
ad4f2e42 | 204 | id parent; |
c72fc2d9 | 205 | |
0994488a | 206 | for (proto_list = ((Class)self)->protocols; |
c72fc2d9 TW |
207 | proto_list; proto_list = proto_list->next) |
208 | { | |
209 | for (i=0; i < proto_list->count; i++) | |
210 | { | |
211 | if ([proto_list->list[i] conformsTo: aProtocol]) | |
212 | return YES; | |
213 | } | |
214 | } | |
215 | ||
9c37957a | 216 | if ((parent = [self superClass])) |
ad4f2e42 | 217 | return [parent conformsTo: aProtocol]; |
991d3e71 KKT |
218 | else |
219 | return NO; | |
c72fc2d9 TW |
220 | } |
221 | ||
ad4f2e42 KKT |
222 | - (BOOL) conformsTo: (Protocol*)aProtocol |
223 | { | |
224 | return [[self class] conformsTo:aProtocol]; | |
225 | } | |
226 | ||
c72fc2d9 TW |
227 | - (IMP)methodFor:(SEL)aSel |
228 | { | |
229 | return (method_get_imp(object_is_instance(self) | |
230 | ?class_get_instance_method(self->isa, aSel) | |
231 | :class_get_class_method(self->isa, aSel))); | |
232 | } | |
233 | ||
c72fc2d9 TW |
234 | + (struct objc_method_description *)descriptionForInstanceMethod:(SEL)aSel |
235 | { | |
236 | return ((struct objc_method_description *) | |
237 | class_get_instance_method(self, aSel)); | |
238 | } | |
239 | ||
c72fc2d9 TW |
240 | - (struct objc_method_description *)descriptionForMethod:(SEL)aSel |
241 | { | |
242 | return ((struct objc_method_description *) | |
243 | (object_is_instance(self) | |
244 | ?class_get_instance_method(self->isa, aSel) | |
245 | :class_get_class_method(self->isa, aSel))); | |
246 | } | |
247 | ||
c72fc2d9 TW |
248 | - perform:(SEL)aSel |
249 | { | |
250 | IMP msg = objc_msg_lookup(self, aSel); | |
251 | if (!msg) | |
252 | return [self error:"invalid selector passed to %s", sel_get_name(_cmd)]; | |
253 | return (*msg)(self, aSel); | |
254 | } | |
255 | ||
c72fc2d9 TW |
256 | - perform:(SEL)aSel with:anObject |
257 | { | |
258 | IMP msg = objc_msg_lookup(self, aSel); | |
259 | if (!msg) | |
260 | return [self error:"invalid selector passed to %s", sel_get_name(_cmd)]; | |
261 | return (*msg)(self, aSel, anObject); | |
262 | } | |
263 | ||
c72fc2d9 TW |
264 | - perform:(SEL)aSel with:anObject1 with:anObject2 |
265 | { | |
266 | IMP msg = objc_msg_lookup(self, aSel); | |
267 | if (!msg) | |
268 | return [self error:"invalid selector passed to %s", sel_get_name(_cmd)]; | |
269 | return (*msg)(self, aSel, anObject1, anObject2); | |
270 | } | |
271 | ||
7a1dd323 | 272 | - (retval_t)forward:(SEL)aSel :(arglist_t)argFrame |
c72fc2d9 | 273 | { |
7a1dd323 | 274 | return (retval_t)[self doesNotRecognize: aSel]; |
c72fc2d9 TW |
275 | } |
276 | ||
7a1dd323 | 277 | - (retval_t)performv:(SEL)aSel :(arglist_t)argFrame |
c72fc2d9 | 278 | { |
0b3d89ca | 279 | return objc_msg_sendv(self, aSel, argFrame); |
c72fc2d9 TW |
280 | } |
281 | ||
0994488a | 282 | + poseAs:(Class)aClassObject |
c72fc2d9 TW |
283 | { |
284 | return class_pose_as(self, aClassObject); | |
285 | } | |
286 | ||
0994488a | 287 | - (Class)transmuteClassTo:(Class)aClassObject |
c72fc2d9 TW |
288 | { |
289 | if (object_is_instance(self)) | |
290 | if (class_is_class(aClassObject)) | |
291 | if (class_get_instance_size(aClassObject)==class_get_instance_size(isa)) | |
292 | if ([self isKindOf:aClassObject]) | |
293 | { | |
0994488a | 294 | Class old_isa = isa; |
c72fc2d9 TW |
295 | isa = aClassObject; |
296 | return old_isa; | |
297 | } | |
298 | return nil; | |
299 | } | |
300 | ||
c72fc2d9 TW |
301 | - subclassResponsibility:(SEL)aSel |
302 | { | |
303 | return [self error:"subclass should override %s", sel_get_name(aSel)]; | |
304 | } | |
305 | ||
c72fc2d9 TW |
306 | - notImplemented:(SEL)aSel |
307 | { | |
308 | return [self error:"method %s not implemented", sel_get_name(aSel)]; | |
309 | } | |
310 | ||
214a36e8 KKT |
311 | - shouldNotImplement:(SEL)aSel |
312 | { | |
313 | return [self error:"%s should not implement %s", | |
314 | object_get_class_name(self), sel_get_name(aSel)]; | |
315 | } | |
316 | ||
c72fc2d9 TW |
317 | - doesNotRecognize:(SEL)aSel |
318 | { | |
319 | return [self error:"%s does not recognize %s", | |
320 | object_get_class_name(self), sel_get_name(aSel)]; | |
321 | } | |
322 | ||
7a1dd323 KKT |
323 | #ifdef __alpha__ |
324 | extern size_t strlen(const char*); | |
325 | #endif | |
326 | ||
c72fc2d9 TW |
327 | - error:(const char *)aString, ... |
328 | { | |
329 | #define FMT "error: %s (%s)\n%s\n" | |
a7ab3794 KKT |
330 | char fmt[(strlen((char*)FMT)+strlen((char*)object_get_class_name(self)) |
331 | +((aString!=NULL)?strlen((char*)aString):0)+8)]; | |
c72fc2d9 TW |
332 | va_list ap; |
333 | ||
334 | sprintf(fmt, FMT, object_get_class_name(self), | |
335 | object_is_instance(self)?"instance":"class", | |
336 | (aString!=NULL)?aString:""); | |
337 | va_start(ap, aString); | |
9c37957a | 338 | objc_verror(self, OBJC_ERR_UNKNOWN, fmt, ap); |
c72fc2d9 TW |
339 | va_end(ap); |
340 | return nil; | |
341 | #undef FMT | |
342 | } | |
343 | ||
c72fc2d9 TW |
344 | + (int)version |
345 | { | |
346 | return class_get_version(self); | |
347 | } | |
348 | ||
c72fc2d9 TW |
349 | + setVersion:(int)aVersion |
350 | { | |
351 | class_set_version(self, aVersion); | |
352 | return self; | |
353 | } | |
354 | ||
034dec73 KKT |
355 | + (int)streamVersion: (TypedStream*)aStream |
356 | { | |
357 | if (aStream->mode == OBJC_READONLY) | |
358 | return objc_get_stream_class_version (aStream, self); | |
359 | else | |
360 | return class_get_version (self); | |
361 | } | |
362 | ||
c72fc2d9 TW |
363 | // These are used to write or read the instance variables |
364 | // declared in this particular part of the object. Subclasses | |
365 | // should extend these, by calling [super read/write: aStream] | |
366 | // before doing their own archiving. These methods are private, in | |
367 | // the sense that they should only be called from subclasses. | |
368 | ||
369 | - read: (TypedStream*)aStream | |
370 | { | |
371 | // [super read: aStream]; | |
372 | return self; | |
373 | } | |
374 | ||
375 | - write: (TypedStream*)aStream | |
376 | { | |
377 | // [super write: aStream]; | |
378 | return self; | |
379 | } | |
380 | ||
034dec73 | 381 | - awake |
c72fc2d9 | 382 | { |
034dec73 | 383 | // [super awake]; |
c72fc2d9 TW |
384 | return self; |
385 | } | |
386 | ||
c72fc2d9 | 387 | @end |