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. */
28 #include "objc/Object.h"
29 #include "objc/Protocol.h"
30 #include "objc/objc-api.h"
34 #define MAX_CLASS_NAME_LEN 256
36 @implementation Object
50 return [[self alloc] init];
55 return class_create_instance(self);
60 return object_dispose(self);
65 return [[self shallowCopy] deepen];
70 return object_copy(self);
85 return object_get_class(self);
90 return object_get_super_class(self);
93 - (MetaClass)metaClass
95 return object_get_meta_class(self);
100 return object_get_class_name(self);
113 - (BOOL)isEqual:anObject
115 return self==anObject;
118 - (int)compare:anotherObject;
120 if ([self isEqual:anotherObject])
122 // Ordering objects by their address is pretty useless,
123 // so subclasses should override this is some useful way.
124 else if (self > anotherObject)
137 return object_is_class(self);
142 return object_is_instance(self);
145 - (BOOL)isKindOf:(Class)aClassObject
149 for (class = self->isa; class!=Nil; class = class_get_super_class(class))
150 if (class==aClassObject)
155 - (BOOL)isMemberOf:(Class)aClassObject
157 return self->isa==aClassObject;
160 - (BOOL)isKindOfClassNamed:(const char *)aClassName
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))
171 - (BOOL)isMemberOfClassNamed:(const char *)aClassName
173 return ((aClassName!=NULL)
174 &&!strcmp(class_get_class_name(self->isa), aClassName));
177 + (BOOL)instancesRespondTo:(SEL)aSel
179 return class_get_instance_method(self, aSel)!=METHOD_NULL;
182 - (BOOL)respondsTo:(SEL)aSel
184 return ((object_is_instance(self)
185 ?class_get_instance_method(self->isa, aSel)
186 :class_get_class_method(self->isa, aSel))!=METHOD_NULL);
189 + (IMP)instanceMethodFor:(SEL)aSel
191 return method_get_imp(class_get_instance_method(self, aSel));
194 // Indicates if the receiving class or instance conforms to the given protocol
195 // not usually overridden by subclasses
197 // Modified 9/5/94 to always search the class object's protocol list, rather
198 // than the meta class.
200 + (BOOL) conformsTo: (Protocol*)aProtocol
203 struct objc_protocol_list* proto_list;
206 for (proto_list = ((Class)self)->protocols;
207 proto_list; proto_list = proto_list->next)
209 for (i=0; i < proto_list->count; i++)
211 if ([proto_list->list[i] conformsTo: aProtocol])
216 if ((parent = [self superClass]))
217 return [parent conformsTo: aProtocol];
222 - (BOOL) conformsTo: (Protocol*)aProtocol
224 return [[self class] conformsTo:aProtocol];
227 - (IMP)methodFor:(SEL)aSel
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)));
234 + (struct objc_method_description *)descriptionForInstanceMethod:(SEL)aSel
236 return ((struct objc_method_description *)
237 class_get_instance_method(self, aSel));
240 - (struct objc_method_description *)descriptionForMethod:(SEL)aSel
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)));
250 IMP msg = objc_msg_lookup(self, aSel);
252 return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
253 return (*msg)(self, aSel);
256 - perform:(SEL)aSel with:anObject
258 IMP msg = objc_msg_lookup(self, aSel);
260 return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
261 return (*msg)(self, aSel, anObject);
264 - perform:(SEL)aSel with:anObject1 with:anObject2
266 IMP msg = objc_msg_lookup(self, aSel);
268 return [self error:"invalid selector passed to %s", sel_get_name(_cmd)];
269 return (*msg)(self, aSel, anObject1, anObject2);
272 - (retval_t)forward:(SEL)aSel :(arglist_t)argFrame
274 return (retval_t)[self doesNotRecognize: aSel];
277 - (retval_t)performv:(SEL)aSel :(arglist_t)argFrame
279 return objc_msg_sendv(self, aSel, argFrame);
282 + poseAs:(Class)aClassObject
284 return class_pose_as(self, aClassObject);
287 - (Class)transmuteClassTo:(Class)aClassObject
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])
301 - subclassResponsibility:(SEL)aSel
303 return [self error:"subclass should override %s", sel_get_name(aSel)];
306 - notImplemented:(SEL)aSel
308 return [self error:"method %s not implemented", sel_get_name(aSel)];
311 - shouldNotImplement:(SEL)aSel
313 return [self error:"%s should not implement %s",
314 object_get_class_name(self), sel_get_name(aSel)];
317 - doesNotRecognize:(SEL)aSel
319 return [self error:"%s does not recognize %s",
320 object_get_class_name(self), sel_get_name(aSel)];
323 - error:(const char *)aString, ...
325 #define FMT "error: %s (%s)\n%s\n"
326 char fmt[(strlen((char*)FMT)+strlen((char*)object_get_class_name(self))
327 +((aString!=NULL)?strlen((char*)aString):0)+8)];
330 sprintf(fmt, FMT, object_get_class_name(self),
331 object_is_instance(self)?"instance":"class",
332 (aString!=NULL)?aString:"");
333 va_start(ap, aString);
334 objc_verror(self, OBJC_ERR_UNKNOWN, fmt, ap);
342 return class_get_version(self);
345 + setVersion:(int)aVersion
347 class_set_version(self, aVersion);
351 + (int)streamVersion: (TypedStream*)aStream
353 if (aStream->mode == OBJC_READONLY)
354 return objc_get_stream_class_version (aStream, self);
356 return class_get_version (self);
359 // These are used to write or read the instance variables
360 // declared in this particular part of the object. Subclasses
361 // should extend these, by calling [super read/write: aStream]
362 // before doing their own archiving. These methods are private, in
363 // the sense that they should only be called from subclasses.
365 - read: (TypedStream*)aStream
367 // [super read: aStream];
371 - write: (TypedStream*)aStream
373 // [super write: aStream];