1 /* The implementation of class Object for Objective-C.
2 Copyright (C) 1993, 1994, 1995, 1997 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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
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 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
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
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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. */
29 #include "objc/Object.h"
30 #include "objc/Protocol.h"
31 #include "objc/objc-api.h"
35 #define MAX_CLASS_NAME_LEN 256
37 @implementation Object
51 return [[self alloc] init];
56 return class_create_instance(self);
61 return object_dispose(self);
66 return [[self shallowCopy] deepen];
71 return object_copy(self);
86 return object_get_class(self);
91 return object_get_super_class(self);
94 - (MetaClass)metaClass
96 return object_get_meta_class(self);
101 return object_get_class_name(self);
114 - (BOOL)isEqual:anObject
116 return self==anObject;
119 - (int)compare:anotherObject;
121 if ([self isEqual:anotherObject])
123 // Ordering objects by their address is pretty useless,
124 // so subclasses should override this is some useful way.
125 else if (self > anotherObject)
138 return object_is_class(self);
143 return object_is_instance(self);
146 - (BOOL)isKindOf:(Class)aClassObject
150 for (class = self->isa; class!=Nil; class = class_get_super_class(class))
151 if (class==aClassObject)
156 - (BOOL)isMemberOf:(Class)aClassObject
158 return self->isa==aClassObject;
161 - (BOOL)isKindOfClassNamed:(const char *)aClassName
165 if (aClassName!=NULL)
166 for (class = self->isa; class!=Nil; class = class_get_super_class(class))
167 if (!strcmp(class_get_class_name(class), aClassName))
172 - (BOOL)isMemberOfClassNamed:(const char *)aClassName
174 return ((aClassName!=NULL)
175 &&!strcmp(class_get_class_name(self->isa), aClassName));
178 + (BOOL)instancesRespondTo:(SEL)aSel
180 return class_get_instance_method(self, aSel)!=METHOD_NULL;
183 - (BOOL)respondsTo:(SEL)aSel
185 return ((object_is_instance(self)
186 ?class_get_instance_method(self->isa, aSel)
187 :class_get_class_method(self->isa, aSel))!=METHOD_NULL);
190 + (IMP)instanceMethodFor:(SEL)aSel
192 return method_get_imp(class_get_instance_method(self, aSel));
195 // Indicates if the receiving class or instance conforms to the given protocol
196 // not usually overridden by subclasses
198 // Modified 9/5/94 to always search the class object's protocol list, rather
199 // than the meta class.
201 + (BOOL) conformsTo: (Protocol*)aProtocol
204 struct objc_protocol_list* proto_list;
207 for (proto_list = ((Class)self)->protocols;
208 proto_list; proto_list = proto_list->next)
210 for (i=0; i < proto_list->count; i++)
212 if ([proto_list->list[i] conformsTo: aProtocol])
217 if ((parent = [self superClass]))
218 return [parent conformsTo: aProtocol];
223 - (BOOL) conformsTo: (Protocol*)aProtocol
225 return [[self class] conformsTo:aProtocol];
228 - (IMP)methodFor:(SEL)aSel
230 return (method_get_imp(object_is_instance(self)
231 ?class_get_instance_method(self->isa, aSel)
232 :class_get_class_method(self->isa, aSel)));
235 + (struct objc_method_description *)descriptionForInstanceMethod:(SEL)aSel
237 return ((struct objc_method_description *)
238 class_get_instance_method(self, aSel));
241 - (struct objc_method_description *)descriptionForMethod:(SEL)aSel
243 return ((struct objc_method_description *)
244 (object_is_instance(self)
245 ?class_get_instance_method(self->isa, aSel)
246 :class_get_class_method(self->isa, aSel)));
251 IMP msg = objc_msg_lookup(self, aSel);
253 return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
254 return (*msg)(self, aSel);
257 - perform:(SEL)aSel with:anObject
259 IMP msg = objc_msg_lookup(self, aSel);
261 return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
262 return (*msg)(self, aSel, anObject);
265 - perform:(SEL)aSel with:anObject1 with:anObject2
267 IMP msg = objc_msg_lookup(self, aSel);
269 return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
270 return (*msg)(self, aSel, anObject1, anObject2);
273 - (retval_t)forward:(SEL)aSel :(arglist_t)argFrame
275 return (retval_t)[self doesNotRecognize: aSel];
278 - (retval_t)performv:(SEL)aSel :(arglist_t)argFrame
280 return objc_msg_sendv(self, aSel, argFrame);
283 + poseAs:(Class)aClassObject
285 return class_pose_as(self, aClassObject);
288 - (Class)transmuteClassTo:(Class)aClassObject
290 if (object_is_instance(self))
291 if (class_is_class(aClassObject))
292 if (class_get_instance_size(aClassObject)==class_get_instance_size(isa))
293 if ([self isKindOf:aClassObject])
302 - subclassResponsibility:(SEL)aSel
304 return [self error:"subclass should override %s", sel_get_name(aSel)];
307 - notImplemented:(SEL)aSel
309 return [self error:"method %s not implemented", sel_get_name(aSel)];
312 - shouldNotImplement:(SEL)aSel
314 return [self error:"%s should not implement %s",
315 object_get_class_name(self), sel_get_name(aSel)];
318 - doesNotRecognize:(SEL)aSel
320 return [self error:"%s does not recognize %s",
321 object_get_class_name(self), sel_get_name(aSel)];
325 extern size_t strlen(const char*);
328 - error:(const char *)aString, ...
330 #define FMT "error: %s (%s)\n%s\n"
331 char fmt[(strlen((char*)FMT)+strlen((char*)object_get_class_name(self))
332 +((aString!=NULL)?strlen((char*)aString):0)+8)];
335 sprintf(fmt, FMT, object_get_class_name(self),
336 object_is_instance(self) ? "instance" : "class",
337 (aString!=NULL)?aString:"");
338 va_start(ap, aString);
339 objc_verror(self, OBJC_ERR_UNKNOWN, fmt, ap);
347 return class_get_version(self);
350 + setVersion:(int)aVersion
352 class_set_version(self, aVersion);
356 + (int)streamVersion: (TypedStream*)aStream
358 if (aStream->mode == OBJC_READONLY)
359 return objc_get_stream_class_version (aStream, self);
361 return class_get_version (self);
364 // These are used to write or read the instance variables
365 // declared in this particular part of the object. Subclasses
366 // should extend these, by calling [super read/write: aStream]
367 // before doing their own archiving. These methods are private, in
368 // the sense that they should only be called from subclasses.
370 - read: (TypedStream*)aStream
372 // [super read: aStream];
376 - write: (TypedStream*)aStream
378 // [super write: aStream];