1 /* The implementation of class Object for Objective-C.
2 Copyright (C) 1993, 1994, 1995, 1997, 2002, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "objc-private/common.h"
28 #include <string.h> /* For strcmp. */
30 #include "objc/Object.h"
31 #include "objc/Protocol.h"
32 #include "objc/objc-api.h"
34 @implementation Object
38 return object_get_class (self);
41 - (BOOL)isEqual: (id)anObject
43 return self == anObject;
48 /* The following methods were deprecated in GCC 4.6.0 and will be
49 removed in the next GCC release. */
50 @implementation Object (Deprecated)
64 return [[self alloc] init];
69 return class_create_instance(self);
74 return object_dispose(self);
79 return [[self shallowCopy] deepen];
84 return object_copy(self);
99 return object_get_super_class(self);
102 - (MetaClass)metaClass
104 return object_get_meta_class(self);
109 return object_get_class_name(self);
122 - (int)compare:(id)anotherObject;
124 if ([self isEqual:anotherObject])
126 // Ordering objects by their address is pretty useless,
127 // so subclasses should override this is some useful way.
128 else if ((id)self > anotherObject)
141 return object_is_class(self);
146 return object_is_instance(self);
149 - (BOOL)isKindOf:(Class)aClassObject
153 for (class = self->isa; class!=Nil; class = class_get_super_class(class))
154 if (class==aClassObject)
159 - (BOOL)isMemberOf:(Class)aClassObject
161 return self->isa==aClassObject;
164 - (BOOL)isKindOfClassNamed:(const char *)aClassName
168 if (aClassName!=NULL)
169 for (class = self->isa; class!=Nil; class = class_get_super_class(class))
170 if (!strcmp(class_get_class_name(class), aClassName))
175 - (BOOL)isMemberOfClassNamed:(const char *)aClassName
177 return ((aClassName!=NULL)
178 &&!strcmp(class_get_class_name(self->isa), aClassName));
181 + (BOOL)instancesRespondTo:(SEL)aSel
183 return class_get_instance_method(self, aSel) != (Method_t)0;
186 - (BOOL)respondsTo:(SEL)aSel
188 return ((object_is_instance(self)
189 ?class_get_instance_method(self->isa, aSel)
190 :class_get_class_method(self->isa, aSel)) != (Method_t)0);
193 + (IMP)instanceMethodFor:(SEL)aSel
195 return method_get_imp(class_get_instance_method(self, aSel));
198 // Indicates if the receiving class or instance conforms to the given protocol
199 // not usually overridden by subclasses
201 // Modified 9/5/94 to always search the class object's protocol list, rather
202 // than the meta class.
204 + (BOOL) conformsTo: (Protocol*)aProtocol
207 struct objc_protocol_list* proto_list;
210 for (proto_list = ((Class)self)->protocols;
211 proto_list; proto_list = proto_list->next)
213 for (i=0; i < proto_list->count; i++)
215 if ([proto_list->list[i] conformsTo: aProtocol])
220 if ((parent = [self superClass]))
221 return [parent conformsTo: aProtocol];
226 - (BOOL) conformsTo: (Protocol*)aProtocol
228 return [[self class] conformsTo:aProtocol];
231 - (IMP)methodFor:(SEL)aSel
233 return (method_get_imp(object_is_instance(self)
234 ?class_get_instance_method(self->isa, aSel)
235 :class_get_class_method(self->isa, aSel)));
238 + (struct objc_method_description *)descriptionForInstanceMethod:(SEL)aSel
240 return ((struct objc_method_description *)
241 class_get_instance_method(self, aSel));
244 - (struct objc_method_description *)descriptionForMethod:(SEL)aSel
246 return ((struct objc_method_description *)
247 (object_is_instance(self)
248 ?class_get_instance_method(self->isa, aSel)
249 :class_get_class_method(self->isa, aSel)));
252 - (retval_t)performv:(SEL)aSel :(arglist_t)argFrame
254 return objc_msg_sendv(self, aSel, argFrame);
257 + poseAs:(Class)aClassObject
259 return class_pose_as(self, aClassObject);
262 - (Class)transmuteClassTo:(Class)aClassObject
264 if (object_is_instance(self))
265 if (class_is_class(aClassObject))
266 if (class_get_instance_size(aClassObject)==class_get_instance_size(isa))
267 if ([self isKindOf:aClassObject])
278 return class_get_version(self);
281 + setVersion:(int)aVersion
283 class_set_version(self, aVersion);