1 /* The implementation of class Object for Objective-C.
2 Copyright (C) 1993 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 or 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* As a special exception, if you link this library with files compiled
21 with GCC to produce an executable, this does not cause the resulting
22 executable to be covered by the GNU General Public License. This
23 exception does not however invalidate any other reasons why the
24 executable file might be covered by the GNU General Public License. */
26 #include "objc/Object.h"
27 #include "objc/Protocol.h"
28 #include "objc/objc-api.h"
31 extern void (*_objc_error)(id object, const char *format, va_list);
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
197 - (BOOL) conformsTo: (Protocol*)aProtocol
200 struct objc_protocol_list* proto_list;
202 for (proto_list = isa->protocols;
203 proto_list; proto_list = proto_list->next)
205 for (i=0; i < proto_list->count; i++)
207 if ([proto_list->list[i] conformsTo: aProtocol])
212 if ([self superClass])
213 return [[self superClass] conformsTo: aProtocol];
218 - (IMP)methodFor:(SEL)aSel
220 return (method_get_imp(object_is_instance(self)
221 ?class_get_instance_method(self->isa, aSel)
222 :class_get_class_method(self->isa, aSel)));
225 + (struct objc_method_description *)descriptionForInstanceMethod:(SEL)aSel
227 return ((struct objc_method_description *)
228 class_get_instance_method(self, aSel));
231 - (struct objc_method_description *)descriptionForMethod:(SEL)aSel
233 return ((struct objc_method_description *)
234 (object_is_instance(self)
235 ?class_get_instance_method(self->isa, aSel)
236 :class_get_class_method(self->isa, aSel)));
241 IMP msg = objc_msg_lookup(self, aSel);
243 return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
244 return (*msg)(self, aSel);
247 - perform:(SEL)aSel with:anObject
249 IMP msg = objc_msg_lookup(self, aSel);
251 return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
252 return (*msg)(self, aSel, anObject);
255 - perform:(SEL)aSel with:anObject1 with:anObject2
257 IMP msg = objc_msg_lookup(self, aSel);
259 return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
260 return (*msg)(self, aSel, anObject1, anObject2);
263 - forward:(SEL)aSel :(arglist_t)argFrame
265 return [self doesNotRecognize: aSel];
268 - performv:(SEL)aSel :(arglist_t)argFrame
270 return objc_msg_sendv(self, aSel, argFrame);
273 + poseAs:(Class*)aClassObject
275 return class_pose_as(self, aClassObject);
278 - (Class*)transmuteClassTo:(Class*)aClassObject
280 if (object_is_instance(self))
281 if (class_is_class(aClassObject))
282 if (class_get_instance_size(aClassObject)==class_get_instance_size(isa))
283 if ([self isKindOf:aClassObject])
285 Class* old_isa = isa;
292 - subclassResponsibility:(SEL)aSel
294 return [self error:"subclass should override %s", sel_get_name(aSel)];
297 - notImplemented:(SEL)aSel
299 return [self error:"method %s not implemented", sel_get_name(aSel)];
302 - shouldNotImplement:(SEL)aSel
304 return [self error:"%s should not implement %s",
305 object_get_class_name(self), sel_get_name(aSel)];
308 - doesNotRecognize:(SEL)aSel
310 return [self error:"%s does not recognize %s",
311 object_get_class_name(self), sel_get_name(aSel)];
314 - error:(const char *)aString, ...
316 #define FMT "error: %s (%s)\n%s\n"
317 char fmt[(strlen((char*)FMT)+strlen((char*)object_get_class_name(self))
318 +((aString!=NULL)?strlen((char*)aString):0)+8)];
321 sprintf(fmt, FMT, object_get_class_name(self),
322 object_is_instance(self)?"instance":"class",
323 (aString!=NULL)?aString:"");
324 va_start(ap, aString);
325 (*_objc_error)(self, fmt, ap);
333 return class_get_version(self);
336 + setVersion:(int)aVersion
338 class_set_version(self, aVersion);
342 + (int)streamVersion: (TypedStream*)aStream
345 if (aStream->mode == OBJC_READONLY)
346 return objc_get_stream_class_version (aStream, self);
349 return class_get_version (self);
352 // These are used to write or read the instance variables
353 // declared in this particular part of the object. Subclasses
354 // should extend these, by calling [super read/write: aStream]
355 // before doing their own archiving. These methods are private, in
356 // the sense that they should only be called from subclasses.
358 - read: (TypedStream*)aStream
360 // [super read: aStream];
364 - write: (TypedStream*)aStream
366 // [super write: aStream];